ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/gclib/gclib/GAlnExtend.h
(Generate patch)
# Line 5 | Line 5
5  
6   #include "GBase.h"
7   #include "GList.hh"
8 < #include <string.h>
8 > #include "gdna.h"
9  
10 < #define GDEBUG 1
10 > //#define GDEBUG 1
11  
12   enum {
13      gxEDIT_OP_MASK = 0x3,
# Line 98 | Line 98
98  
99          if (GX_EDITOP_GET(oplast) == op) {
100              uint32 l=ops[opnum-1];
101 <            ops[opnum-1]=GX_EDITOP_CONS((GX_EDITOP_GET(l)),
101 >            ops[opnum-1]=GX_EDITOP_CONS((GX_EDITOP_GET(l)),
102                 (GX_EDITOP_VAL(l) + k));
103              }
104          else {
# 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 346 | Line 346
346         a_len=0;
347             b_len=0;
348             if (ed_script==NULL) return;
349 <           for (uint32 i=0; i<ed_script->opnum; i++) {
349 >           for (uint32 i=0; i<ed_script->opnum; i++) {
350                    int num=((ed_script->ops[i]) >> 2);
351                    char op_type = 3 - ( ed_script->ops[i] & gxEDIT_OP_MASK );
352                    if (op_type == 3 || op_type < 0 )
# Line 371 | Line 371
371             }
372  
373   #ifdef GDEBUG
374 <        void printAlignment(FILE* f, const char* sa, int sa_len,
374 >        void printAlignment(FILE* f, const char* sa, int sa_len,
375                       const char* sb, int sb_len) {
376                  //print seq A
377             char al[1024]; //display buffer for seq A
# Line 436 | Line 436
436         }
437   #endif
438    };
439 <  
439 >
440   struct GXAlnInfo {
441   const char *qseq;
442   int ql,qr;
# Line 466 | Line 466
466    bool operator<(GXAlnInfo& d) {
467      return ((score==d.score)? pid>d.pid : score>d.score);
468      }
469  bool operator>(GXAlnInfo& d) {
470    return ((score==d.score)? pid<d.pid : score<d.score);
471    }
469    bool operator==(GXAlnInfo& d) {
470      return (score==d.score && pid==d.pid);
471      }
# Line 484 | 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        }
487   bool operator>(GXSeed& d){
488      return ((b_ofs==d.b_ofs) ? a_ofs>d.a_ofs : b_ofs>d.b_ofs);
489      }
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 554 | 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) { _penalty=GMAX((-min_gap), max_gap); }
# Line 574 | Line 568
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       }
577  bool operator>(GXBand& d){
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     }
571    bool operator==(GXBand& d){
572      //return (score==d.score && seeds.Count()==d.seeds.Count());
573       return (score==d.score && w_min_b==d.w_min_b);
# Line 588 | Line 578
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
581 >   GXSeed* tmatch_r; //terminal match to be used if there is no better alignment
582 >   GXSeed* tmatch_l; //terminal match to be used if there is no better alignment
583     int idxoffset; //global anti-diagonal->index offset (a_len-1)
584     //used to convert a diagonal to an index
585     //diagonal is always b_ofs-a_ofs, so the minimum value is -a_len+1
# Line 602 | Line 593
593     GXBandSet(int a_len, int b_len):GList<GXBand>(a_len+b_len-1, false, true, false) {
594        idxoffset=a_len-1;
595        qmatch=NULL;
596 <      tmatch=NULL; //terminal match to be used if everything else fails
596 >      tmatch_l=NULL; //terminal match to be used if everything else fails
597 >      tmatch_r=NULL;
598            //diag will range from -a_len+1 to b_len-1, so after adjustment
599            //by idxoffset we get a max of a_len+b_len-2
600        int bcount=a_len+b_len-1;
601        for (int i=0;i<bcount;i++)
602 <                   this->Add(new GXBand(i-idxoffset));
602 >              this->Add(new GXBand(i-idxoffset));
603             //unsorted, this should set fList[i]
604        }
605     ~GXBandSet() {
# Line 622 | Line 614
614       }
615   };
616  
617 + struct GXSeqData {
618 +  const char* aseq;
619 +  int alen;
620 +  const char* bseq;
621 +  int blen;
622 +  GVec<uint16>** amers;
623 +  int amlen; //minimum alignment length that's sufficient to
624 +             //trigger the quick extension heuristics
625 +  GXSeqData(const char* sa=NULL, int la=0, const char* sb=NULL, int lb=0,
626 +  GVec<uint16>* mers[]=NULL):aseq(sa), alen(la),
627 +     bseq(sb),  blen(lb), amers(mers), amlen(0) {
628 +   calc_amlen();
629 +   calc_bmlen();
630 +   }
631 +  void calc_amlen() {
632 +    if (alen) {
633 +       int ah=iround(double(alen)*0.8);
634 +       if (ah<12) ah=12;
635 +       if (amlen>ah) amlen=ah;
636 +       }
637 +    }
638 +  void calc_bmlen() {
639 +    if (blen) {
640 +      int bh = iround(double(alen)*0.6);
641 +      if (bh<12) bh=12;
642 +      if (amlen>bh) amlen=bh;
643 +      }
644 +    }
645 +  void update(const char* sa, int la, GVec<uint16>** mers,
646 +          const char* sb, int lb, int mlen=0) {
647 +     aseq=sa;
648 +     alen=la;
649 +     amers=mers;
650 +     if (mlen) {
651 +       amlen=mlen;
652 +       }
653 +       else calc_amlen();
654 +     if (sb==bseq && blen==lb) return;
655 +     bseq=sb;
656 +     blen=lb;
657 +     calc_bmlen();
658 +     }
659 +  /*
660 +  void update_b(const char* sb, int lb) {
661 +     if (sb==bseq && blen==lb) return;
662 +     bseq=sb;
663 +     blen=lb;
664 +     calc_bmlen();
665 +     }*/
666 + };
667 +
668 + uint16 get6mer(char* p);
669 + void table6mers(const char* s, int slen, GVec<uint16>* amers[]);
670  
671 < GXBandSet* collectSeeds_R(GList<GXSeed>& seeds, const char* seqa, int a_len, const char* seqb, int b_len); //for overlap at 3' end of seqb
671 > //GXBandSet* collectSeeds_R(GList<GXSeed>& seeds, GXSeqData& sd); //for overlap at 3' end of seqb
672  
673 < GXBandSet* collectSeeds_L(GList<GXSeed>& seeds, const char* seqa, int a_len, const char* seqb, int b_len); //for overlap at 5' end of seqb
673 > GXBandSet* collectSeeds(GList<GXSeed>& seeds, GXSeqData& sd); //for overlap at 5' end of seqb
674  
675   void printEditScript(GXEditScript* ed_script);
676  
# Line 640 | Line 685
685  
686  
687   enum GAlnTrimType {
688 <  galn_NoTrim=0,
688 >  //Describes trimming intent
689 >  galn_None=0, //no trimming, just alignment
690    galn_TrimLeft,
691 <  galn_TrimRight
691 >  galn_TrimRight,
692 >  galn_TrimEither //adaptor should be trimmed from either end
693    };
694  
695   struct CAlnTrim {
696    GAlnTrimType type;
697 <  int boundary; //base index (either left or right) excluding terminal poly-A stretches
698 <  void prepare(GAlnTrimType trim_type, const char* s, int s_len) {
699 <    type=trim_type;
700 <    boundary=0;
701 <    if (type==galn_TrimLeft) {
697 >  int l_boundary; //base index (either left or right) excluding terminal poly-A stretches
698 >  int r_boundary; //base index (either left or right) excluding terminal poly-A stretches
699 >  int alen; //query/adaptor seq length (for validate())
700 >  int safelen; //alignment length > amlen should be automatically validated
701 >  int seedlen;
702 >  void prepare(const char* s, int s_len) {
703 >    //type=trim_type;
704 >    //amlen=smlen;
705 >    l_boundary=0;
706 >    r_boundary=0;
707 >    //if (type==galn_TrimLeft) {
708          int s_lbound=0;
709          if (s[0]=='A' && s[1]=='A' && s[2]=='A') {
710             s_lbound=3;
# Line 661 | Line 714
714             s_lbound=4;
715             while (s_lbound<s_len-1 && s[s_lbound]=='A') s_lbound++;
716             }
717 <        boundary=s_lbound+3;
718 <        return;
719 <        }
720 <    if (type==galn_TrimRight) {
717 >        l_boundary=s_lbound+3;
718 >    //    return;
719 >    //    }
720 >    //if (type==galn_TrimRight) {
721         int r=s_len-1;
722         if (s[r]=='A' && s[r-1]=='A' && s[r-2]=='A') {
723            r-=3;
# Line 674 | Line 727
727            r-=4;
728            while (r>0 && s[r]=='A') r--;
729            }
730 <       boundary=r-3;
731 <       }
730 >       r_boundary=r-3;
731 >    //   }
732      }
733  
734 <  CAlnTrim(GAlnTrimType trim_type, const char* s, int s_len) {
735 <    prepare(trim_type, s, s_len);
734 >  CAlnTrim(GAlnTrimType trim_type, const char* s, int s_len, int a_len, int smlen):
735 >                   type(trim_type), l_boundary(0), r_boundary(0), alen(a_len), safelen(smlen) {
736 >    prepare(s, s_len);
737      }
738  
739 <  bool validate(int sl, int sr, int alnpid, int adist) {
740 <   int alnlen=sr-sl+1;
739 >  bool validate_R(int sr, int admax, int badj, int adist) {
740 >        if (adist>admax) return false;
741 >        return (sr>=r_boundary+badj);
742 >   }
743 >  bool validate_L(int sl, int alnlen, int admax, int badj, int alnpid, int adist) {
744 >        if (adist>admax) return false;
745 >    //left match should be more stringent (5')
746 >    if (alnpid<93) {
747 >      if (alnlen<13) return false;
748 >      admax=0;
749 >      badj++;
750 >      }
751 >    return (sl<=l_boundary-badj);
752 >  }
753 >
754 >  bool validate(GXAlnInfo* alninfo) {
755 >   int alnlen=alninfo->sr - alninfo->sl + 1;
756 >   if (alninfo->pid>90.0 && alnlen>safelen)
757 >           //special case: heavy match, could be in the middle
758 >           return true;
759 >   int sl=alninfo->sl;
760 >   int sr=alninfo->sr;
761     sl--;sr--; //boundary is 0-based
762 <   int badj=0;
763 <   int admax=3;
764 <   if (alnlen<11) {
762 >   int badj=0; //default boundary is 3 bases distance to end
763 >   int admax=1;
764 >   if (alnlen<13) {
765        //stricter boundary check
766 +      if (alninfo->pid<90) return false;
767        badj=2;
768 <      admax=1;
694 <      if (alnlen<=6) badj++;
768 >      if (alnlen<=7) { badj++; admax=0; }
769        }
770 <   if (adist>admax) return false;
770 >   if (type==galn_TrimLeft) {
771 >         return validate_L(sl, alnlen, admax, badj, alninfo->pid, alen-alninfo->qr);
772 >     }
773 >   else if (type==galn_TrimRight) {
774 >         return validate_R(sr, admax, badj, alninfo->ql-1);
775 >     }
776 >   else if (type==galn_TrimEither) {
777 >     return (validate_R(sr, admax, badj, alninfo->ql-1) ||
778 >           validate_L(sl, alnlen, admax, badj, alninfo->pid, alen-alninfo->qr));
779 >     }
780 >   return true;
781 >   /*
782     if (type==galn_TrimRight) {
783        return (sr>=boundary+badj);
784        }
785     else {
786 <      //left side should be more stringent
787 <      if (alnpid<91) {
788 <        if (alnlen<11) return false;
786 >      //left match should be more stringent (5')
787 >      if (alnpid<93) {
788 >        if (alnlen<13) return false;
789 >        admax=0;
790          badj++;
791          }
792        return (sl<=boundary-badj);
793        }
794 +    */
795     }
709
796   };
797  
798  
# Line 722 | Line 808
808   GXAlnInfo* GreedyAlign(const char* q_seq,  int q_alnstart, const char* s_seq, int s_alnstart,
809          bool editscript=false, int reward=2, int penalty=3, int xdrop=8);
810  
811 < GXAlnInfo* match_LeftEnd(const char* seqa, int seqa_len, const char* seqb, int seqb_len,
812 <          CGreedyAlignData* gxmem=NULL, int min_pid=83);
813 < GXAlnInfo* match_RightEnd(const char* seqa, int seqa_len, const char* seqb, int seqb_len,
728 <          CGreedyAlignData* gxmem=NULL, int min_pid=73);
811 > GXAlnInfo* match_adaptor(GXSeqData& sd, GAlnTrimType trim_type,
812 >                                CGreedyAlignData* gxmem=NULL, int min_pid=90);
813 > //GXAlnInfo* match_RightEnd(GXSeqData& sd, CGreedyAlignData* gxmem=NULL, int min_pid=90);
814   #endif

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines