ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/gclib/gclib/GAlnExtend.h
(Generate patch)
# Line 215 | Line 215
215  
216   };
217  
218 < #define GREEDY_MAX_COST_FRACTION 5
218 > #define GREEDY_MAX_COST_FRACTION 8
219   /* (was 2) sequence_length / (this number) is a measure of how hard the
220    alignment code will work to find the optimal alignment; in fact
221    this gives a worst case bound on the number of loop iterations */
# 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 456 | Line 466
466    bool operator<(GXAlnInfo& d) {
467      return ((score==d.score)? pid>d.pid : score>d.score);
468      }
459  bool operator>(GXAlnInfo& d) {
460    return ((score==d.score)? pid<d.pid : score<d.score);
461    }
469    bool operator==(GXAlnInfo& d) {
470      return (score==d.score && pid==d.pid);
471      }
# Line 474 | Line 481
481     bool operator<(GXSeed& d){
482        return ((b_ofs==d.b_ofs) ? a_ofs<d.a_ofs : b_ofs<d.b_ofs);
483        }
477   bool operator>(GXSeed& d){
478      return ((b_ofs==d.b_ofs) ? a_ofs>d.a_ofs : b_ofs>d.b_ofs);
479      }
484     bool operator==(GXSeed& d){
485        return (b_ofs==d.b_ofs && a_ofs==d.a_ofs); //should never be the case, seeds are uniquely constructed
486        }
# Line 499 | Line 503
503     //seeds for this, and diag+1 and diag+2 are stored here
504    int min_a, max_a; //maximal coordinates of the bundle
505    int min_b, max_b;
506 +  int w_min_b; //weighted average of left start coordinate
507 +  int avg_len;
508    GList<GXSeed> seeds; //sorted by x coordinate (b_ofs)
509    int score; //sum of seed scores (- overlapping_bases/2 - gaps)
510 +  bool tested;
511    GXBand(int start_diag=-1, GXSeed* seed=NULL):seeds(true, false, false) {
512            diag=start_diag;
513            min_a=MAX_INT;
# Line 508 | Line 515
515            max_a=0;
516            max_b=0;
517            score=0;
518 +          avg_len=0;
519 +          w_min_b=0;
520 +          tested=false;
521      if (seed!=NULL) addSeed(seed);
522      }
523    void addSeed(GXSeed* seed) {
524       seeds.Add(seed);
525       score+=seed->len;
526 +     avg_len+=seed->len;
527 +     w_min_b+=seed->b_ofs * seed->len;
528       //if (diag<0) diag=seed->diag; //should NOT be done like this
529       if (seed->a_ofs < min_a) min_a=seed->a_ofs;
530       if (seed->a_ofs+ seed->len > max_a) max_a=seed->a_ofs+seed->len;
531       if (seed->b_ofs < min_b) min_b=seed->b_ofs;
532       if (seed->b_ofs+seed->len > max_b) max_b=seed->b_ofs+seed->len;
533       }
534 +
535    void finalize() {
536            //!! to be called only AFTER all seeds have been added
537            // seeds are sorted by b_ofs
538            //penalize seed gaps and overlaps on b sequence
539 +    if (avg_len==0) return;
540 +    w_min_b/=avg_len;
541 +    avg_len>>=1;
542            for (int i=1;i<seeds.Count();i++) {
543          GXSeed& sprev=*seeds[i-1];
544          GXSeed& scur=*seeds[i];
# Line 532 | Line 548
548          int a_gap=scur.a_ofs-sprev.a_ofs-sprev.len;
549          int max_gap=b_gap;
550          int min_gap=a_gap;
551 <        if (min_gap>max_gap) swap(max_gap, min_gap);
551 >        if (min_gap>max_gap) Gswap(max_gap, min_gap);
552 >        int _penalty=0;
553          if (min_gap<0) { //overlap
554 <               if (max_gap>0) { score-=GMAX((-min_gap), max_gap); }
555 <                  else score+=min_gap;
554 >               if (max_gap>0) { _penalty=GMAX((-min_gap), max_gap); }
555 >                  else _penalty=-min_gap;
556                 }
557              else { //gap
558 <               score-=max_gap;
558 >               _penalty=max_gap;
559                 }
560 <              }//for each seed
560 >        score-=(_penalty>>1);
561 >        //score-=_penalty;
562 >              }//for each seed
563       }
564  
565    //bands will be sorted by decreasing score eventually, after all seeds are added
566    //more seeds better than one longer seed?
567    bool operator<(GXBand& d){
568 <     return ((score==d.score) ? seeds.Count()>d.seeds.Count() : score>d.score);
569 <     }
551 <  bool operator>(GXBand& d){
552 <     return ((score==d.score) ? seeds.Count()<d.seeds.Count() : score<d.score);
568 >     //return ((score==d.score) ? seeds.Count()>d.seeds.Count() : score>d.score);
569 >     return ((score==d.score) ? w_min_b<d.w_min_b : score>d.score);
570       }
571    bool operator==(GXBand& d){
572 <     //return (score==d.score && seeds.Count()==d.seeds.Count());
573 <    return (score==d.score && seeds.Count()==d.seeds.Count());
572 >    //return (score==d.score && seeds.Count()==d.seeds.Count());
573 >     return (score==d.score && w_min_b==d.w_min_b);
574       }
575  
576   };
577  
578   class GXBandSet:public GList<GXBand> {
579    public:
580 +   GXSeed* qmatch; //long match (mismatches allowed) if a very good match was extended well
581 +   GXSeed* tmatch; //terminal match to be used if there is no better alignment
582     int idxoffset; //global anti-diagonal->index offset (a_len-1)
583     //used to convert a diagonal to an index
584     //diagonal is always b_ofs-a_ofs, so the minimum value is -a_len+1
# Line 572 | Line 591
591        }
592     GXBandSet(int a_len, int b_len):GList<GXBand>(a_len+b_len-1, false, true, false) {
593        idxoffset=a_len-1;
594 +      qmatch=NULL;
595 +      tmatch=NULL; //terminal match to be used if everything else fails
596            //diag will range from -a_len+1 to b_len-1, so after adjustment
597            //by idxoffset we get a max of a_len+b_len-2
598        int bcount=a_len+b_len-1;
# Line 579 | Line 600
600                     this->Add(new GXBand(i-idxoffset));
601             //unsorted, this should set fList[i]
602        }
603 +   ~GXBandSet() {
604 +      delete qmatch;
605 +      }
606     void addSeed(GXSeed* seed) {
607           //MUST be unsorted !!!
608           int idx=(seed->b_ofs-seed->a_ofs)+idxoffset;
# Line 601 | Line 625
625                         bool reverse, int xdrop_threshold,
626                         int match_cost, int mismatch_cost,
627                         int& seq1_align_len, int& seq2_align_len,
628 <                       SGreedyAlignMem& aux_data,
628 >                       CGreedyAlignData& aux_data,
629                         GXEditScript *edit_block);
630  
631  
# Line 611 | Line 635
635    galn_TrimRight
636    };
637  
638 + struct CAlnTrim {
639 +  GAlnTrimType type;
640 +  int boundary; //base index (either left or right) excluding terminal poly-A stretches
641 +  void prepare(GAlnTrimType trim_type, const char* s, int s_len) {
642 +    type=trim_type;
643 +    boundary=0;
644 +    if (type==galn_TrimLeft) {
645 +        int s_lbound=0;
646 +        if (s[0]=='A' && s[1]=='A' && s[2]=='A') {
647 +           s_lbound=3;
648 +           while (s_lbound<s_len-1 && s[s_lbound]=='A') s_lbound++;
649 +           }
650 +        else if (s[1]=='A' && s[2]=='A' && s[3]=='A') {
651 +           s_lbound=4;
652 +           while (s_lbound<s_len-1 && s[s_lbound]=='A') s_lbound++;
653 +           }
654 +        boundary=s_lbound+3;
655 +        return;
656 +        }
657 +    if (type==galn_TrimRight) {
658 +       int r=s_len-1;
659 +       if (s[r]=='A' && s[r-1]=='A' && s[r-2]=='A') {
660 +          r-=3;
661 +          while (r>0 && s[r]=='A') r--;
662 +          }
663 +       else if (s[r-1]=='A' && s[r-2]=='A' && s[r-3]=='A') {
664 +          r-=4;
665 +          while (r>0 && s[r]=='A') r--;
666 +          }
667 +       boundary=r-3;
668 +       }
669 +    }
670 +
671 +  CAlnTrim(GAlnTrimType trim_type, const char* s, int s_len) {
672 +    prepare(trim_type, s, s_len);
673 +    }
674 +
675 +  bool validate(int sl, int sr, int alnpid, int adist) {
676 +   int alnlen=sr-sl+1;
677 +   sl--;sr--; //boundary is 0-based
678 +   int badj=0; //default boundary is 3 bases distance to end
679 +   int admax=1;
680 +   if (alnlen<13) {
681 +      //stricter boundary check
682 +      if (alnpid<90) return false;
683 +      badj=2;
684 +      if (alnlen<=7) { badj++; admax=0; }
685 +      }
686 +   if (adist>admax) return false;
687 +   if (type==galn_TrimRight) {
688 +      return (sr>=boundary+badj);
689 +      }
690 +   else {
691 +      //left match should be more stringent (5')
692 +      if (alnpid<93) {
693 +        if (alnlen<13) return false;
694 +        admax=0;
695 +        badj++;
696 +        }
697 +      return (sl<=boundary-badj);
698 +      }
699 +   }
700 +
701 + };
702 +
703 +
704   // reward MUST be >1, always
705   GXAlnInfo* GreedyAlignRegion(const char* q_seq, int q_alnstart, int q_max,
706 <                  const char* s_seq, int s_alnstart, int s_max, GAlnTrimType trimtype=galn_NoTrim,
707 <                     bool editscript=false, SGreedyAlignMem* gxmem=NULL, int reward=2, int penalty=3, int xdrop=8);
706 >                  const char* s_seq, int s_alnstart, int s_max,
707 >                  int reward, int penalty, int xdrop, CGreedyAlignData* gxmem=NULL,
708 >                  CAlnTrim* trim=NULL, bool editscript=false);
709 > GXAlnInfo* GreedyAlignRegion(const char* q_seq, int q_alnstart, int q_max,
710 >                       const char* s_seq, int s_alnstart, int s_max, CGreedyAlignData* gxmem,
711 >                       CAlnTrim* trim=NULL, bool editscript=false);
712 >
713   GXAlnInfo* GreedyAlign(const char* q_seq,  int q_alnstart, const char* s_seq, int s_alnstart,
714          bool editscript=false, int reward=2, int penalty=3, int xdrop=8);
715  
716 + GXAlnInfo* match_LeftEnd(const char* seqa, int seqa_len, const char* seqb, int seqb_len,
717 +          CGreedyAlignData* gxmem=NULL, int min_pid=83);
718 + GXAlnInfo* match_RightEnd(const char* seqa, int seqa_len, const char* seqb, int seqb_len,
719 +          CGreedyAlignData* gxmem=NULL, int min_pid=73);
720   #endif

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines