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     int idxoffset; //global anti-diagonal->index offset (a_len-1)
592     //used to convert a diagonal to an index
593     //diagonal is always b_ofs-a_ofs, so the minimum value is -a_len+1
# Line 572 | Line 600
600        }
601     GXBandSet(int a_len, int b_len):GList<GXBand>(a_len+b_len-1, false, true, false) {
602        idxoffset=a_len-1;
603 +      qmatch=NULL;
604            //diag will range from -a_len+1 to b_len-1, so after adjustment
605            //by idxoffset we get a max of a_len+b_len-2
606        int bcount=a_len+b_len-1;
# Line 579 | Line 608
608                     this->Add(new GXBand(i-idxoffset));
609             //unsorted, this should set fList[i]
610        }
611 +   ~GXBandSet() {
612 +      delete qmatch;
613 +      }
614     void addSeed(GXSeed* seed) {
615           //MUST be unsorted !!!
616           int idx=(seed->b_ofs-seed->a_ofs)+idxoffset;
# Line 601 | Line 633
633                         bool reverse, int xdrop_threshold,
634                         int match_cost, int mismatch_cost,
635                         int& seq1_align_len, int& seq2_align_len,
636 <                       SGreedyAlignMem& aux_data,
636 >                       CGreedyAlignData& aux_data,
637                         GXEditScript *edit_block);
638  
639  
# Line 611 | Line 643
643    galn_TrimRight
644    };
645  
646 + struct CAlnTrim {
647 +  GAlnTrimType type;
648 +  int boundary; //base index (either left or right) excluding terminal poly-A stretches
649 +  void prepare(GAlnTrimType trim_type, const char* s, int s_len) {
650 +    type=trim_type;
651 +    boundary=0;
652 +    if (type==galn_TrimLeft) {
653 +        int s_lbound=0;
654 +        if (s[0]=='A' && s[1]=='A' && s[2]=='A') {
655 +           s_lbound=3;
656 +           while (s_lbound<s_len-1 && s[s_lbound]=='A') s_lbound++;
657 +           }
658 +        else if (s[1]=='A' && s[2]=='A' && s[3]=='A') {
659 +           s_lbound=4;
660 +           while (s_lbound<s_len-1 && s[s_lbound]=='A') s_lbound++;
661 +           }
662 +        boundary=s_lbound+3;
663 +        return;
664 +        }
665 +    if (type==galn_TrimRight) {
666 +       int r=s_len-1;
667 +       if (s[r]=='A' && s[r-1]=='A' && s[r-2]=='A') {
668 +          r-=3;
669 +          while (r>0 && s[r]=='A') r--;
670 +          }
671 +       else if (s[r-1]=='A' && s[r-2]=='A' && s[r-3]=='A') {
672 +          r-=4;
673 +          while (r>0 && s[r]=='A') r--;
674 +          }
675 +       boundary=r-3;
676 +       }
677 +    }
678 +
679 +  CAlnTrim(GAlnTrimType trim_type, const char* s, int s_len) {
680 +    prepare(trim_type, s, s_len);
681 +    }
682 +
683 +  bool validate(int sl, int sr, int alnpid, int adist) {
684 +   int alnlen=sr-sl+1;
685 +   sl--;sr--; //boundary is 0-based
686 +   int badj=0;
687 +   int admax=3;
688 +   if (alnlen<11) {
689 +      //stricter boundary check
690 +      badj=2;
691 +      admax=1;
692 +      }
693 +   if (adist>admax) return false;
694 +   if (type==galn_TrimRight) {
695 +      return (sr>=boundary+badj);
696 +      }
697 +   else {
698 +      //left side should be more stringent
699 +      if (alnpid<91) {
700 +        if (alnlen<11) return false;
701 +        badj++;
702 +        }
703 +      return (sl<=boundary-badj);
704 +      }
705 +   }
706 +
707 + };
708 +
709 +
710   // reward MUST be >1, always
711   GXAlnInfo* GreedyAlignRegion(const char* q_seq, int q_alnstart, int q_max,
712 <                  const char* s_seq, int s_alnstart, int s_max, GAlnTrimType trimtype=galn_NoTrim,
713 <                     bool editscript=false, SGreedyAlignMem* gxmem=NULL, int reward=2, int penalty=3, int xdrop=8);
712 >                  const char* s_seq, int s_alnstart, int s_max,
713 >                  int reward, int penalty, int xdrop, CGreedyAlignData* gxmem=NULL,
714 >                  CAlnTrim* trim=NULL, bool editscript=false);
715 > GXAlnInfo* GreedyAlignRegion(const char* q_seq, int q_alnstart, int q_max,
716 >                       const char* s_seq, int s_alnstart, int s_max, CGreedyAlignData* gxmem,
717 >                       CAlnTrim* trim=NULL, bool editscript=false);
718 >
719   GXAlnInfo* GreedyAlign(const char* q_seq,  int q_alnstart, const char* s_seq, int s_alnstart,
720          bool editscript=false, int reward=2, int penalty=3, int xdrop=8);
721  
722   GXAlnInfo* match_LeftEnd(const char* seqa, int seqa_len, const char* seqb, int seqb_len,
723 <          SGreedyAlignMem* gxmem, int match_reward=2, int mismatch_penalty=3, int Xdrop=8);
723 >          CGreedyAlignData* gxmem=NULL, int min_pid=83);
724   GXAlnInfo* match_RightEnd(const char* seqa, int seqa_len, const char* seqb, int seqb_len,
725 <            SGreedyAlignMem* gxmem, int match_reward=2, int mismatch_penalty=3, int Xdrop=8);
725 >          CGreedyAlignData* gxmem=NULL, int min_pid=73);
726   #endif

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines