ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/gclib/gclib/GAlnExtend.h
(Generate patch)
# Line 7 | Line 7
7   #include "GList.hh"
8   #include <string.h>
9  
10 < //#define GDEBUG 1
10 > #define GDEBUG 1
11  
12   enum {
13      gxEDIT_OP_MASK = 0x3,
# Line 227 | Line 227
227   #define GX_GALLOC_ERROR "Error: failed to allocate memory for greedy alignment!\n"
228  
229   // all auxiliary memory needed for the greedy extension algorithm
230 < struct SGreedyAlignMem {
230 > class CGreedyAlignData {
231 >   int d_diff;
232 >   int max_d;
233 > public:
234     int** last_seq2_off;              // 2-D array of distances
235     int* max_score;                   // array of maximum scores
236     GXMemPool* space;                // local memory pool for SGreedyOffset structs
237 <   int d_diff;
238 <   int max_d;
237 >   //
238 >   int match_reward;
239 >   int mismatch_penalty;
240 >   int x_drop;
241     // Allocate memory for the greedy gapped alignment algorithm
242 <   SGreedyAlignMem(int reward, int penalty, int Xdrop) {
242 >   CGreedyAlignData(int reward, int penalty, int xdrop) {
243        //int max_d, diff_d;
244        if (penalty<0) penalty=-penalty;
245        if (reward % 2) {
246           //scale params
247 <         reward = reward << 1;
248 <         penalty = (penalty << 1);
249 <         Xdrop = Xdrop<<1;
247 >         match_reward = reward << 1;
248 >         mismatch_penalty = (penalty << 1);
249 >         x_drop = xdrop<<1;
250 >         }
251 >      else {
252 >         match_reward=reward;
253 >         mismatch_penalty = penalty;
254 >         x_drop=xdrop;
255           }
256        //if (gap_open == 0 && gap_extend == 0)
257        //   gap_extend = (reward >> 1) + penalty;
# Line 254 | Line 264
264        space=NULL;           // local memory pool for SGreedyOffset structs
265        //if (score_params.gap_open==0 && score_params.gap_extend==0) {
266           //non-affine, simpler Greedy algorithm
267 <       d_diff = (Xdrop+reward/2)/(penalty+reward)+1;
267 >       d_diff = (x_drop+match_reward/2)/(mismatch_penalty+match_reward)+1;
268         GMALLOC(last_seq2_off, ((max_d + 2) * sizeof(int*)));
269         if (!last_seq2_off)
270           GError(GX_GALLOC_ERROR);
# Line 283 | Line 293
293       GCALLOC(max_score, (sizeof(int) * (max_d + 1 + d_diff)));
294       if (!max_score) GError(GX_GALLOC_ERROR);
295       }
296 <   ~SGreedyAlignMem() {
296 >   ~CGreedyAlignData() {
297       if (last_seq2_off) {
298           GFREE(last_seq2_off[0]);
299           GFREE(last_seq2_off);
# Line 499 | Line 509
509     //seeds for this, and diag+1 and diag+2 are stored here
510    int min_a, max_a; //maximal coordinates of the bundle
511    int min_b, max_b;
512 +  int w_min_b; //weighted average of left start coordinate
513 +  int avg_len;
514    GList<GXSeed> seeds; //sorted by x coordinate (b_ofs)
515    int score; //sum of seed scores (- overlapping_bases/2 - gaps)
516 +  bool tested;
517    GXBand(int start_diag=-1, GXSeed* seed=NULL):seeds(true, false, false) {
518            diag=start_diag;
519            min_a=MAX_INT;
# Line 508 | Line 521
521            max_a=0;
522            max_b=0;
523            score=0;
524 +          avg_len=0;
525 +          w_min_b=0;
526 +          tested=false;
527      if (seed!=NULL) addSeed(seed);
528      }
529    void addSeed(GXSeed* seed) {
530       seeds.Add(seed);
531       score+=seed->len;
532 +     avg_len+=seed->len;
533 +     w_min_b+=seed->b_ofs * seed->len;
534       //if (diag<0) diag=seed->diag; //should NOT be done like this
535       if (seed->a_ofs < min_a) min_a=seed->a_ofs;
536       if (seed->a_ofs+ seed->len > max_a) max_a=seed->a_ofs+seed->len;
537       if (seed->b_ofs < min_b) min_b=seed->b_ofs;
538       if (seed->b_ofs+seed->len > max_b) max_b=seed->b_ofs+seed->len;
539       }
540 +
541    void finalize() {
542            //!! to be called only AFTER all seeds have been added
543            // seeds are sorted by b_ofs
544            //penalize seed gaps and overlaps on b sequence
545 +    if (avg_len==0) return;
546 +    w_min_b/=avg_len;
547 +    avg_len>>=1;
548            for (int i=1;i<seeds.Count();i++) {
549          GXSeed& sprev=*seeds[i-1];
550          GXSeed& scur=*seeds[i];
# Line 533 | Line 555
555          int max_gap=b_gap;
556          int min_gap=a_gap;
557          if (min_gap>max_gap) swap(max_gap, min_gap);
558 +        int _penalty=0;
559          if (min_gap<0) { //overlap
560 <               if (max_gap>0) { score-=GMAX((-min_gap), max_gap); }
561 <                  else score+=min_gap;
560 >               if (max_gap>0) { _penalty=GMAX((-min_gap), max_gap); }
561 >                  else _penalty=-min_gap;
562                 }
563              else { //gap
564 <               score-=max_gap;
564 >               _penalty=max_gap;
565                 }
566 <              }//for each seed
566 >        score-=(_penalty>>1);
567 >        //score-=_penalty;
568 >              }//for each seed
569       }
570  
571    //bands will be sorted by decreasing score eventually, after all seeds are added
572    //more seeds better than one longer seed?
573    bool operator<(GXBand& d){
574 <     return ((score==d.score) ? seeds.Count()>d.seeds.Count() : score>d.score);
574 >     //return ((score==d.score) ? seeds.Count()>d.seeds.Count() : score>d.score);
575 >     return ((score==d.score) ? w_min_b<d.w_min_b : score>d.score);
576       }
577    bool operator>(GXBand& d){
578 <     return ((score==d.score) ? seeds.Count()<d.seeds.Count() : score<d.score);
578 >     //return ((score==d.score) ? seeds.Count()<d.seeds.Count() : score<d.score);
579 >    return ((score==d.score) ? w_min_b>d.w_min_b : score<d.score);
580       }
581    bool operator==(GXBand& d){
582 <     //return (score==d.score && seeds.Count()==d.seeds.Count());
583 <    return (score==d.score && seeds.Count()==d.seeds.Count());
582 >    //return (score==d.score && seeds.Count()==d.seeds.Count());
583 >     return (score==d.score && w_min_b==d.w_min_b);
584       }
585  
586   };
587  
588   class GXBandSet:public GList<GXBand> {
589    public:
590 +   GXSeed* qmatch; //long match (mismatches allowed) if a very good match was extended well
591 +   GXSeed* tmatch; //terminal match to be used if there is no better alignment
592     int idxoffset; //global anti-diagonal->index offset (a_len-1)
593     //used to convert a diagonal to an index
594     //diagonal is always b_ofs-a_ofs, so the minimum value is -a_len+1
# Line 572 | Line 601
601        }
602     GXBandSet(int a_len, int b_len):GList<GXBand>(a_len+b_len-1, false, true, false) {
603        idxoffset=a_len-1;
604 +      qmatch=NULL;
605 +      tmatch=NULL; //terminal match to be used if everything else fails
606            //diag will range from -a_len+1 to b_len-1, so after adjustment
607            //by idxoffset we get a max of a_len+b_len-2
608        int bcount=a_len+b_len-1;
# Line 579 | Line 610
610                     this->Add(new GXBand(i-idxoffset));
611             //unsorted, this should set fList[i]
612        }
613 +   ~GXBandSet() {
614 +      delete qmatch;
615 +      }
616     void addSeed(GXSeed* seed) {
617           //MUST be unsorted !!!
618           int idx=(seed->b_ofs-seed->a_ofs)+idxoffset;
# Line 601 | Line 635
635                         bool reverse, int xdrop_threshold,
636                         int match_cost, int mismatch_cost,
637                         int& seq1_align_len, int& seq2_align_len,
638 <                       SGreedyAlignMem& aux_data,
638 >                       CGreedyAlignData& aux_data,
639                         GXEditScript *edit_block);
640  
641  
# Line 611 | Line 645
645    galn_TrimRight
646    };
647  
648 + struct CAlnTrim {
649 +  GAlnTrimType type;
650 +  int boundary; //base index (either left or right) excluding terminal poly-A stretches
651 +  void prepare(GAlnTrimType trim_type, const char* s, int s_len) {
652 +    type=trim_type;
653 +    boundary=0;
654 +    if (type==galn_TrimLeft) {
655 +        int s_lbound=0;
656 +        if (s[0]=='A' && s[1]=='A' && s[2]=='A') {
657 +           s_lbound=3;
658 +           while (s_lbound<s_len-1 && s[s_lbound]=='A') s_lbound++;
659 +           }
660 +        else if (s[1]=='A' && s[2]=='A' && s[3]=='A') {
661 +           s_lbound=4;
662 +           while (s_lbound<s_len-1 && s[s_lbound]=='A') s_lbound++;
663 +           }
664 +        boundary=s_lbound+3;
665 +        return;
666 +        }
667 +    if (type==galn_TrimRight) {
668 +       int r=s_len-1;
669 +       if (s[r]=='A' && s[r-1]=='A' && s[r-2]=='A') {
670 +          r-=3;
671 +          while (r>0 && s[r]=='A') r--;
672 +          }
673 +       else if (s[r-1]=='A' && s[r-2]=='A' && s[r-3]=='A') {
674 +          r-=4;
675 +          while (r>0 && s[r]=='A') r--;
676 +          }
677 +       boundary=r-3;
678 +       }
679 +    }
680 +
681 +  CAlnTrim(GAlnTrimType trim_type, const char* s, int s_len) {
682 +    prepare(trim_type, s, s_len);
683 +    }
684 +
685 +  bool validate(int sl, int sr, int alnpid, int adist) {
686 +   int alnlen=sr-sl+1;
687 +   sl--;sr--; //boundary is 0-based
688 +   int badj=0;
689 +   int admax=3;
690 +   if (alnlen<11) {
691 +      //stricter boundary check
692 +      badj=2;
693 +      admax=1;
694 +      if (alnlen<=6) badj++;
695 +      }
696 +   if (adist>admax) return false;
697 +   if (type==galn_TrimRight) {
698 +      return (sr>=boundary+badj);
699 +      }
700 +   else {
701 +      //left side should be more stringent
702 +      if (alnpid<91) {
703 +        if (alnlen<11) return false;
704 +        badj++;
705 +        }
706 +      return (sl<=boundary-badj);
707 +      }
708 +   }
709 +
710 + };
711 +
712 +
713   // reward MUST be >1, always
714   GXAlnInfo* GreedyAlignRegion(const char* q_seq, int q_alnstart, int q_max,
715 <                  const char* s_seq, int s_alnstart, int s_max, GAlnTrimType trimtype=galn_NoTrim,
716 <                     bool editscript=false, SGreedyAlignMem* gxmem=NULL, int reward=2, int penalty=3, int xdrop=8);
715 >                  const char* s_seq, int s_alnstart, int s_max,
716 >                  int reward, int penalty, int xdrop, CGreedyAlignData* gxmem=NULL,
717 >                  CAlnTrim* trim=NULL, bool editscript=false);
718 > GXAlnInfo* GreedyAlignRegion(const char* q_seq, int q_alnstart, int q_max,
719 >                       const char* s_seq, int s_alnstart, int s_max, CGreedyAlignData* gxmem,
720 >                       CAlnTrim* trim=NULL, bool editscript=false);
721 >
722   GXAlnInfo* GreedyAlign(const char* q_seq,  int q_alnstart, const char* s_seq, int s_alnstart,
723          bool editscript=false, int reward=2, int penalty=3, int xdrop=8);
724  
725 + GXAlnInfo* match_LeftEnd(const char* seqa, int seqa_len, const char* seqb, int seqb_len,
726 +          CGreedyAlignData* gxmem=NULL, int min_pid=83);
727 + GXAlnInfo* match_RightEnd(const char* seqa, int seqa_len, const char* seqb, int seqb_len,
728 +          CGreedyAlignData* gxmem=NULL, int min_pid=73);
729   #endif

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines