ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/gclib/gclib/GAlnExtend.cpp
(Generate patch)
# Line 53 | Line 53
53    if (flast_d[d-1][diag-1] > GMAX(flast_d[d-1][diag], flast_d[d-1][diag+1])) {
54        *row1 = flast_d[d-1][diag-1];
55        return diag-1;
56 <      }
56 >      }
57    if (flast_d[d-1][diag] > flast_d[d-1][diag+1]) {
58        *row1 = flast_d[d-1][diag];
59        return diag;
# Line 65 | Line 65
65   void GapXEditScript::print() { //debug
66        GapXEditScript* p=this;
67        do {
68 <        GMessage("%d%c ",p->num, xgapcodes[p->op_type]);
69 <        } while ((p=p->next)!=NULL);
68 >        GMessage("%d%c ",p->num, xgapcodes[p->op_type]);
69 >        } while ((p=p->next)!=NULL);
70        GMessage("\n");
71      }
72  
# Line 93 | Line 93
93      else
94          g = BLAST_Gcd(*a, BLAST_Gcd(*b, *c));
95      if (g > 1) {
96 <        *a /= g;
96 >                *a /= g;
97          *b /= g;
98          *c /= g;
99      }
# Line 101 | Line 101
101   }
102  
103  
104 + uint16 get6mer(char* p) {
105 +  uint16 r=gdna2bit(p,3);
106 +  r <<= 6;
107 +  r |= gdna2bit(p,3);
108 +  return r;
109 + }
110 +
111 +
112 + void table6mers(const char* s, int slen, GVec<uint16>* amers[]) {
113 + for (uint16 i=0; i <= slen-6; i++) {
114 +   char* p = (char*)(s+i);
115 +   uint16 v=get6mer(p);
116 +   if (amers[v]==NULL) {
117 +      amers[v]=new GVec<uint16>(1);
118 +      }
119 +   amers[v]->Add(i);
120 + }
121 + }
122 +
123 + GVec<uint16>* match6mer(char* start, GVec<uint16>* amers[]) {
124 +  //careful: this is broken if start+5 falls beyond the end of the string!
125 +  uint16 r=get6mer(start);
126 +  return amers[r];
127 + }
128  
129   //signal that a diagonal is invalid
130   static const int kInvalidOffset = -2;
# Line 129 | Line 153
153               //    }
154        }
155      else { //forward lookup
156 <             while (seq1_index < len1 && seq2_index < len2 &&
156 >             while (seq1_index < len1 && seq2_index < len2 &&
157                     //seq1[seq1_index] < 4 &&
158                     seq1[seq1_index] == seq2[seq2_index]) {
159                  ++seq1_index;
# Line 204 | Line 228
228      int longest_match_run;
229      bool end1_reached, end2_reached;
230      GXMemPool* mem_pool;
231 <
231 >
232      /* ordinary dynamic programming alignment, for each offset
233         in seq1, walks through offsets in seq2 until an X-dropoff
234 <       test fails, saving the best score encountered along
234 >       test fails, saving the best score encountered along
235         the way. Instead of score, this code tracks the 'distance'
236         (number of mismatches plus number of gaps) between seq1
237         and seq2. Instead of walking through sequence offsets, it
238         walks through diagonals that can achieve a given distance.
239 <    
239 >
240         Note that in what follows, the numbering of diagonals implies
241 <       a dot matrix where increasing seq1 offsets go to the right on
241 >       a dot matrix where increasing seq1 offsets go to the right on
242         the x axis, and increasing seq2 offsets go up the y axis.
243         The gapped alignment thus proceeds up and to the right in
244         the graph, and diagonals are numbered increasing to the right */
# Line 223 | Line 247
247      best_diag = 0;
248  
249      /* set the number of distinct distances the algorithm will
250 <       examine in the search for an optimal alignment. The
251 <       heuristic worst-case running time of the algorithm is
250 >       examine in the search for an optimal alignment. The
251 >       heuristic worst-case running time of the algorithm is
252         O(max_dist**2 + (len1+len2)); for sequences which are
253         very similar, the average running time will be sig-
254         nificantly better than this */
255  
256      max_dist = GMIN(GREEDY_MAX_COST,
257 <                   len2 / GREEDY_MAX_COST_FRACTION + 1);
257 >                   (len2/GREEDY_MAX_COST_FRACTION + 1));
258  
259      /* the main loop assumes that the index of all diagonals is
260 <       biased to lie in the middle of allocated bookkeeping
260 >       biased to lie in the middle of allocated bookkeeping
261         structures */
262  
263      diag_origin = max_dist + 2;
# Line 251 | Line 275
275         xdrop_offset gives the distance backwards in the score
276         array to look */
277  
278 <    xdrop_offset = (xdrop_threshold + match_cost / 2) /
278 >    xdrop_offset = (xdrop_threshold + match_cost / 2) /
279                             (match_cost + mismatch_cost) + 1;
280 <    
280 >
281      // find the offset of the first mismatch between seq1 and seq2
282  
283      index = s_FindFirstMismatch(seq1, len1,  seq2, len2, 0, 0, reverse);
# Line 277 | Line 301
301          if (edit_block != NULL)
302              //GapPrelimEditBlockAdd(edit_block, eGapAlignSub, index);
303              edit_block->opRep(index);
304 <        return 0;
304 >        return 0;
305      }
306  
307      // set up the memory pool
# Line 287 | Line 311
311        }
312      else if (mem_pool == NULL) {
313         aux_data.space = mem_pool = new GXMemPool();
314 <    }
314 >    }
315      else {
316          mem_pool->refresh();
317      }
318 <    
318 >
319      /* set up the array of per-distance maximum scores. There
320         are max_diags + xdrop_offset distances to track, the first
321         xdrop_offset of which are 0 */
# Line 299 | Line 323
323      max_score = aux_data.max_score + xdrop_offset;
324      for (index = 0; index < xdrop_offset; index++)
325          aux_data.max_score[index] = 0;
326 <    
326 >
327      // fill in the initial offsets of the distance matrix
328  
329      last_seq2_off[0][diag_origin] = seq1_index;
# Line 328 | Line 352
352  
353          // compute the score for distance d corresponding to the X-dropoff criterion
354  
355 <        xdrop_score = max_score[d - xdrop_offset] +
355 >        xdrop_score = max_score[d - xdrop_offset] +
356                        (match_cost + mismatch_cost) * d - xdrop_threshold;
357 <        xdrop_score = (int)ceil((double)xdrop_score / (match_cost / 2));
357 >        xdrop_score = (int)ceil((double)xdrop_score / (match_cost / 2));
358  
359          // for each diagonal of interest
360          for (k = tmp_diag_lower; k <= tmp_diag_upper; k++) {
361              /* find the largest offset into seq2 that increases
362                 the distance from d-1 to d (i.e. keeps the alignment
363 <               from getting worse for as long as possible), then
363 >               from getting worse for as long as possible), then
364                 choose the offset into seq1 that will keep the
365 <               resulting diagonal fixed at k
366 <            
365 >               resulting diagonal fixed at k
366 >
367                 Note that this requires kInvalidOffset+1 to be smaller
368                 than any valid offset into seq2, i.e. to be negative */
369  
# Line 360 | Line 384
384                  continue;
385              }
386              diag_upper = k;
387 <            
388 <            /* slide down diagonal k until a mismatch
387 >
388 >            /* slide down diagonal k until a mismatch
389                 occurs. As long as only matches are encountered,
390                 the current distance d will not change */
391  
# Line 397 | Line 421
421                 can each be of size at most max_diags+2 */
422  
423              if (seq2_index == len2) {
424 <                diag_lower = k + 1;
424 >                diag_lower = k + 1;
425                  end2_reached = true;
426              }
427              if (seq1_index == len1) {
428 <                diag_upper = k - 1;
428 >                diag_upper = k - 1;
429                  end1_reached = true;
430              }
431          }  // end loop over diagonals
432  
433          // compute the maximum score possible for distance d
434 <        curr_score = curr_extent * (match_cost / 2) -
434 >        curr_score = curr_extent * (match_cost / 2) -
435                          d * (match_cost + mismatch_cost);
436          // if this is the best score seen so far, update the
437          // statistics of the best alignment
# Line 417 | Line 441
441              best_diag = curr_diag;
442              seq2_align_len = curr_seq2_index;
443              seq1_align_len = curr_seq2_index + best_diag - diag_origin;
444 <        }
444 >        }
445          else {
446              max_score[d] = max_score[d - 1];
447          }
# Line 428 | Line 452
452          if (diag_lower > diag_upper)
453              break;
454  
455 <        /* set up for the next distance to examine. Because the
456 <           bounds increase by at most one for each distance,
457 <           diag_lower and diag_upper can each be of size at
455 >        /* set up for the next distance to examine. Because the
456 >           bounds increase by at most one for each distance,
457 >           diag_lower and diag_upper can each be of size at
458             most max_diags+2 */
459  
460          if (!end2_reached)
461 <            diag_lower--;
461 >            diag_lower--;
462          if (!end1_reached)
463              diag_upper++;
464  
# Line 455 | Line 479
479              }
480      }   // end loop over distinct distances
481  
482 <    
482 >
483      if (edit_block == NULL)
484          return best_dist;
485  
486      //----  perform traceback
487 <    d = best_dist;
487 >    d = best_dist;
488      seq1_index = seq1_align_len;
489      seq2_index = seq2_align_len;
490      // for all positive distances
# Line 476 | Line 500
500             traceback operation. best_diag starts off with the
501             value computed during the alignment process */
502  
503 <        new_diag = s_GetNextNonAffineTback(last_seq2_off, d,
503 >        new_diag = s_GetNextNonAffineTback(last_seq2_off, d,
504                                             best_diag, &new_seq2_index);
505  
506          if (new_diag == best_diag) {
# Line 484 | Line 508
508              if (seq2_index - new_seq2_index > 0) {
509                    edit_block->opRep(seq2_index - new_seq2_index);
510              }
511 <        }
511 >        }
512          else if (new_diag < best_diag) {
513              // smaller diagonal: issue a group of substitutions
514              //   and then a gap in seq2 */
# Line 493 | Line 517
517              }
518              //GapPrelimEditBlockAdd(edit_block, eGapAlignIns, 1);
519              edit_block->opIns(1);
520 <        }
520 >        }
521          else {
522              // larger diagonal: issue a group of substitutions
523              //   and then a gap in seq1
# Line 502 | Line 526
526              }
527              edit_block->opDel(1);
528          }
529 <        d--;
530 <        best_diag = new_diag;
531 <        seq2_index = new_seq2_index;
529 >        d--;
530 >        best_diag = new_diag;
531 >        seq2_index = new_seq2_index;
532      }
533   //done:
534      // handle the final group of substitutions back to distance zero,
# Line 526 | Line 550
550        unsigned char op_type = 3 - ( ed_script->ops[i] & gxEDIT_OP_MASK );
551        if (op_type == 3)
552           GError("Error: printEditScript encountered op_type 3 ?!\n");
553 <      GMessage("%d%c ", num, xgapcodes[op_type]);
553 >      GMessage("%d%c ", num, xgapcodes[op_type]);
554        }
555      GMessage("\n");
556    }
# Line 580 | Line 604
604   const int a_dropoff_score=7;
605   const int a_min_score=12; //at least 6 bases full match
606  
607 < // ------------------ adapter matching - simple k-mer seed & extend, no indels for now
607 > // ------------------ adaptor matching - simple k-mer seed & extend, no indels for now
608   //when a k-mer match is found, simply try to extend the alignment using a drop-off scheme
609   //check minimum score and
610 < //for 3' adapter trimming:
610 > //for 3' adaptor trimming:
611   //     require that the right end of the alignment for either the adaptor OR the read must be
612   //     < 3 distance from its right end
613 < // for 5' adapter trimming:
613 > // for 5' adaptor trimming:
614   //     require that the left end of the alignment for either the adaptor OR the read must
615   //     be at coordinate < 3 from start
616  
# Line 659 | Line 683
683    int mmovh5=(a_l<b_l)? a_l : b_l;
684    if (maxscore>=a_min_score && mmovh3<2 && mmovh5<2) {
685       if (a_l<a_ovh3) {
686 <        //adapter closer to the left end (typical for 5' adapter)
686 >        //adaptor closer to the left end (typical for 5' adaptor)
687          l5=a_r+1;
688          l3=alen-1;
689          }
690        else {
691 <        //adapter matching at the right end (typical for 3' adapter)
691 >        //adaptor matching at the right end (typical for 3' adaptor)
692          l5=0;
693          l3=a_l-1;
694          }
# Line 676 | Line 700
700    return false;
701   }
702  
703 <
704 < GXBandSet* collectSeeds_R(GList<GXSeed>& seeds, const char* seqa, int a_len, const char* seqb, int b_len) {
705 < //overlap of right (3') end of seqb
706 < //hash the first 12 bases of seqa:
707 < int aimin=0;
708 < int aimax=GMIN(9,(a_len-4));
709 < int bimin=GMAX(0,(b_len-a_len-2));//allow 2 extraneous bases, just in case there is some garbage at the end
710 < int bimax=b_len-4;
711 < int a_maxlen=aimax+4; //number of rows in the diagonal matrix
712 < int b_maxlen=b_len; //number of cols in the diagonal matrix
713 < GXSeedTable gx(a_maxlen, b_maxlen);
714 < GXBandSet* diagstrips=new GXBandSet(a_maxlen, b_maxlen); //set of overlapping 3-diagonal strips
715 < for (int ai=aimin;ai<=aimax;ai++) {
716 <    int32* av=(int32 *)(&seqa[ai]);
717 <    //for (int bi=b_len-4;bi>=bimin;bi--) {
718 <    for (int bi=bimin;bi<=bimax;bi++) {
719 <       if (gx.x(ai,bi))
720 <            continue; //already have a previous seed covering this region of this diagonal
721 <       int32* bv=(int32 *)(&seqb[bi]);
722 <       if (*av == *bv) {
723 <          //word match
724 <          //see if we can extend to the right
725 <            gx.x(ai+1,bi+1)=1;
726 <            gx.x(ai+2,bi+2)=1;
727 <            gx.x(ai+3,bi+3)=1;
728 <          int aix=ai+4;
729 <          int bix=bi+4;
730 <          int len=4;
731 <          while (bix<b_maxlen && aix<a_maxlen && seqa[aix]==seqb[bix])
732 <               {
733 <                 gx.x(aix,bix)=1;
734 <                 aix++;bix++;
735 <                 len++;
736 <               }
737 <          if (len>7) {
738 <              //heuristics: very likely the best we can get
739 <              int qlen=len;
740 <              while (bix<b_len && aix<a_len && seqa[aix]==seqb[bix]) {
741 <                aix++;
742 <                bix++;
743 <                qlen++;
744 <                }
745 <              if (qlen>10) { //found a quick match shortcut
746 <                 diagstrips->qmatch=new GXSeed(ai,bi,qlen);
747 <                 return diagstrips;
748 <                 }
725 <              }
726 <          GXSeed* newseed=new GXSeed(ai,bi,len);
727 <          seeds.Add(newseed);
728 <          diagstrips->addSeed(newseed);//add it to all 3 adjacent diagonals
729 <          //special last resort terminal match to be used if no better alignment is there
730 <          if (len>4 && ai==0 && (bi+len==b_len || bi+len==b_len-1)) diagstrips->tmatch=newseed;
731 <          }
732 <       }
733 <    }//for each 4-mer at the beginning of seqa
703 > /*
704 > GXBandSet* collectSeeds_R(GList<GXSeed>& seeds, GXSeqData& sd) {
705 > int bimin=GMAX(0,(sd.blen-sd.alen-6));
706 > GXSeedTable gx(sd.alen, sd.blen);
707 > GXBandSet* diagstrips=new GXBandSet(sd.alen, sd.blen); //set of overlapping 3-diagonal strips
708 > //for (int bi=0;bi<=b_len-6;bi++) {
709 > for (int bi=sd.blen-6;bi>=0;bi--) {
710 >   //for each 6-mer of seqb
711 >   uint16 bv = get6mer((char*)&(sd.bseq[bi]));
712 >   GVec<uint16>* alocs = sd.amers[bv];
713 >   if (alocs==NULL) continue;
714 >   //extend each hit
715 >   for (int h=0;h<alocs->Count();h++) {
716 >         int ai=alocs->Get(h);
717 >         //word match
718 >         if (gx.x(ai,bi))
719 >           //already have a previous seed covering this region of this diagonal
720 >           continue;
721 >         for (int i=0;i<6;i++)
722 >            gx.x(ai+i,bi+i)=1;
723 >         //see if we can extend to the left
724 >         int aix=ai-1;
725 >         int bix=bi-1;
726 >         int len=6;
727 >         while (bix>=0 && aix>=0 && sd.aseq[aix]==sd.bseq[bix]) {
728 >                 gx.x(aix,bix)=1;
729 >                 ai=aix;
730 >                 bi=bix;
731 >                 aix--;bix--;
732 >                 len++;
733 >                 }
734 >        if (len>sd.amlen) {
735 >                //heuristics: likely the best we can get
736 >                //quick match shortcut
737 >                diagstrips->qmatch=new GXSeed(ai,bi,len);
738 >                return diagstrips;
739 >                }
740 >         if (bi<bimin && len<9) continue; //skip middle seeds that are not high scoring enough
741 >         GXSeed* newseed=new GXSeed(ai,bi,len);
742 >         seeds.Add(newseed);
743 >         diagstrips->addSeed(newseed);//add it to all 3 adjacent diagonals
744 >         //special last resort terminal match to be used if no better alignment is there
745 >         if (ai<2 && bi+len>sd.blen-2 &&
746 >                 (!diagstrips->tmatch || diagstrips->tmatch->len<len)) diagstrips->tmatch=newseed;
747 >         }
748 >  }
749   for (int i=0;i<diagstrips->Count();i++) {
750      diagstrips->Get(i)->finalize(); //adjust scores due to overlaps or gaps between seeds
751      }
752   diagstrips->setSorted(true); //sort by score
753   return diagstrips;
754   }
755 <
756 < GXBandSet* collectSeeds_L(GList<GXSeed>& seeds, const char* seqa, int a_len, const char* seqb, int b_len) {
757 < //overlap of left (5') end of seqb
758 < //hash the last 12 bases of seqa:
744 < int aimin=GMAX(0,(a_len-16));
745 < int aimax=a_len-4;
746 < int bimin=0;
747 < int bimax=GMIN((a_len-2), (b_len-4));
748 < int a_maxlen=aimax+4; //number of rows in the diagonal matrix
749 < int b_maxlen=b_len; //number of cols in the diagonal matrix
755 > */
756 > GXBandSet* collectSeeds(GList<GXSeed>& seeds, GXSeqData& sd) {
757 > int bimax=GMIN((sd.alen+2), (sd.blen-6));
758 > int bimin=GMAX(0,(sd.blen-sd.alen-6)); //from collectSeeds_R
759   //gx.init(a_maxlen, b_maxlen);
760 < GXSeedTable gx(a_maxlen, b_maxlen);
761 < GXBandSet* diagstrips=new GXBandSet(a_maxlen, b_maxlen); //set of overlapping 3-diagonal strips
762 < for (int ai=aimin;ai<=aimax;ai++) {
763 <    int32* av=(int32 *)(&seqa[ai]);
764 <    for (int bi=bimin;bi<=bimax;bi++) {
765 <       if (gx.x(ai,bi))
766 <          continue; //already have a previous seed covering this region of this diagonal
767 <       int32* bv=(int32 *)(&seqb[bi]);
768 <       if (*av == *bv) {
769 <          //word match
770 <          //see if we can extend to the right
771 <        gx.x(ai+1,bi+1)=1;
772 <        gx.x(ai+2,bi+2)=1;
773 <        gx.x(ai+3,bi+3)=1;
774 <          int aix=ai+4;
775 <          int bix=bi+4;
776 <          int len=4;
777 <          while (bix<b_maxlen && aix<a_maxlen && seqa[aix]==seqb[bix])
778 <               {
779 <                 gx.x(aix,bix)=1;
780 <                 aix++;bix++;
781 <                 len++;
782 <               }
783 <          if (len>7) {
784 <              //heuristics: very likely the best we can get
785 <              int qlen=len;
786 <              while (bix<b_len && aix<a_len && seqa[aix]==seqb[bix]) {
787 <                aix++;
788 <                bix++;
789 <                qlen++;
790 <                }
791 <              if (qlen>10) { //found a quick match shortcut
792 <                 diagstrips->qmatch=new GXSeed(ai,bi,qlen);
793 <                 return diagstrips;
794 <                 }
795 <              }
796 <          GXSeed* newseed=new GXSeed(ai,bi,len);
797 <          seeds.Add(newseed);
798 <          diagstrips->addSeed(newseed);//add it to all 3 adjacent diagonals
799 <          //special last resort terminal match to be used if no better alignment is there
800 <          if (len>4 && bi==0 && ai+len==a_len) diagstrips->tmatch=newseed;
801 <          }
802 <       }
803 <    }//for each 4-mer at the end of seqa
760 > GXSeedTable gx(sd.alen, sd.blen);
761 > GXBandSet* diagstrips=new GXBandSet(sd.alen, sd.blen); //set of overlapping 3-diagonal strips
762 > for (int bi=0;bi<=sd.blen-6;bi++) {
763 >   //for each 6-mer of seqb
764 >   uint16 bv = get6mer((char*) & (sd.bseq[bi]));
765 >   GVec<uint16>* alocs = sd.amers[bv];
766 >   if (alocs==NULL) continue;
767 >   //extend each hit
768 >   for (int h=0;h<alocs->Count();h++) {
769 >         int ai=alocs->Get(h);
770 >         //word match
771 >         if (gx.x(ai,bi))
772 >           //already have a previous seed covering this region of this diagonal
773 >           continue;
774 >         //TODO: there could be Ns in this seed, should we count/adjust score?
775 >         for (int i=0;i<6;i++)
776 >            gx.x(ai+i,bi+i)=1;
777 >         //see if we can extend to the right
778 >         int aix=ai+6;
779 >         int bix=bi+6;
780 >         int len=6;
781 >         while (bix<sd.blen && aix<sd.alen && sd.aseq[aix]==sd.bseq[bix]) {
782 >                 gx.x(aix,bix)=1;
783 >                 aix++;bix++;
784 >                 len++;
785 >                 }
786 >         if (len>sd.amlen) {
787 >                //heuristics: very likely the best we can get
788 >                //quick match shortcut
789 >                diagstrips->qmatch=new GXSeed(ai,bi,len);
790 >                return diagstrips;
791 >                }
792 >         if (bi>bimax && bi<bimin && len<9)
793 >                 //skip mid-sequence seeds that are not high scoring
794 >             continue;
795 >
796 >         GXSeed* newseed=new GXSeed(ai,bi,len);
797 >         seeds.Add(newseed);
798 >         diagstrips->addSeed(newseed);//add it to all 3 adjacent diagonals
799 >     //special last resort terminal match to be used if no better alignment is there
800 >     if (bi<2 && ai+len>=sd.alen-1 &&
801 >                 (!diagstrips->tmatch_l || diagstrips->tmatch_l->len<len))
802 >                      diagstrips->tmatch_l=newseed;
803 >     //collectSeeds_R:
804 >         if (ai<2 && bi+len>sd.blen-2 &&
805 >                 (!diagstrips->tmatch_r || diagstrips->tmatch_r->len<len))
806 >                  diagstrips->tmatch_r=newseed;
807 >     }
808 > } //for each 6-mer of the read
809   for (int i=0;i<diagstrips->Count();i++) {
810      diagstrips->Get(i)->finalize(); //adjust scores due to overlaps or gaps between seeds
811      }
# Line 809 | Line 823
823    else return (s2->len-s1->len);
824   }
825  
826 + int cmpSeedScore_R(const pointer p1, const pointer p2) {
827 +  //return (((GXSeed*)s2)->len-((GXSeed*)s1)->len);
828 +  GXSeed* s1=(GXSeed*)p1;
829 +  GXSeed* s2=(GXSeed*)p2;
830 +  if (s1->len==s2->len) {
831 +     return (s2->b_ofs-s1->b_ofs);
832 +     }
833 +  else return (s2->len-s1->len);
834 + }
835 +
836 +
837   int cmpSeedDiag(const pointer p1, const pointer p2) {
838    GXSeed* s1=(GXSeed*)p1;
839    GXSeed* s2=(GXSeed*)p2;
# Line 840 | Line 865
865    s_alnstart--;
866    if (q_seq==NULL || q_seq[0]==0 || s_seq==NULL || s_seq[0]==0)
867      GError("GreedyAlign() Error: attempt to use an empty sequence string!\n");
868 <  if (q_seq[q_alnstart]!=s_seq[s_alnstart])
869 <    GError("GreedyAlign() Error: improper anchor (mismatch)!\n");
868 >  /*if (q_seq[q_alnstart]!=s_seq[s_alnstart])
869 >    GError("GreedyAlign() Error: improper anchor (mismatch):\n%s (start %d len %d)\n%s (start %d len %d)\n",
870 >           q_seq, q_alnstart, q_max, s_seq, s_alnstart, s_max);
871 >           */
872    int q_ext_l=0, q_ext_r=0, s_ext_l=0, s_ext_r=0;
873    const char* q=q_seq+q_alnstart;
874    int q_avail=q_max-q_alnstart;
875    const char* s=s_seq+s_alnstart;
876    int s_avail=s_max-s_alnstart;
877    if (penalty<0) penalty=-penalty;
878 <  int MIN_GREEDY_SCORE=5*reward; //minimum score for an alignment to be reported for 0 diffs
878 >  int MIN_GREEDY_SCORE=6*reward; //minimum score for an alignment to be reported for 0 diffs
879    GXAlnInfo* alninfo=NULL;
880    bool freeAlnMem=(gxmem==NULL);
881    if (freeAlnMem) {
# Line 858 | Line 885
885    int retscore = 0;
886    int numdiffs = 0;
887    if (trim!=NULL && trim->type==galn_TrimLeft) {
888 +    //intent: trimming the left side
889      if (editscript)
890         ed_script_rev=new GXEditScript();
891  
892      int numdiffs_l = GXGreedyExtend(s_seq, s_alnstart, q_seq, q_alnstart, true, xdrop,
893                     reward, penalty, s_ext_l, q_ext_l, *gxmem, ed_script_rev);
894      //check this extension here and bail out if it's not a good extension
895 <    if (s_alnstart+1-s_ext_l>trim->boundary) {
895 >    if (s_ext_l+(trim->seedlen>>1) < trim->safelen &&
896 >        q_alnstart+1-q_ext_l>1 &&
897 >        s_alnstart+1-s_ext_l>trim->l_boundary) {
898        delete ed_script_rev;
899        if (freeAlnMem) delete gxmem;
900        return NULL;
# Line 888 | Line 918
918                              reward, penalty, s_ext_r, q_ext_r, *gxmem, ed_script_fwd);
919      //check extension here and bail out if not a good right extension
920      //assuming s_max is really at the right end of s_seq
921 <    if (trim!=NULL && trim->type==galn_TrimRight && s_alnstart+s_ext_r<trim->boundary) {
921 >    if (trim!=NULL && trim->type==galn_TrimRight &&
922 >        s_ext_r+(trim->seedlen>>1) < trim->safelen &&
923 >            q_alnstart+q_ext_r<q_max-2 &&
924 >            s_alnstart+s_ext_r<trim->r_boundary) {
925        delete ed_script_fwd;
926        if (freeAlnMem) delete gxmem;
927        return NULL;
# Line 943 | Line 976
976       reward, penalty, xdrop, gxmem, trim, editscript);
977   }
978  
979 < GXAlnInfo* match_RightEnd(const char* seqa, int seqa_len, const char* seqb, int seqb_len,
980 <            CGreedyAlignData* gxmem, int min_pid) {
979 > GXAlnInfo* match_adaptor(GXSeqData& sd, GAlnTrimType trim_type,
980 >                                     CGreedyAlignData* gxmem, int min_pid) {
981    bool editscript=false;
982    #ifdef GDEBUG
983     editscript=true;
984 <  // GMessage("==========> matching Right (3') end ========\n");
984 >   if (trim_type==galn_TrimLeft) {
985 >         GMessage("=======> searching left (5') end : %s\n", sd.aseq);
986 >     }
987 >   else if (trim_type==galn_TrimRight) {
988 >     GMessage("=======> searching right(3') end : %s\n", sd.aseq);
989 >     }
990 >   else if (trim_type==galn_TrimEither) {
991 >     GMessage("==========> searching  both ends : %s\n", sd.aseq);
992 >     }
993    #endif
994 <
954 <  CAlnTrim trimInfo(galn_TrimRight, seqb, seqb_len);
994 >  CAlnTrim trimInfo(trim_type, sd.bseq, sd.blen, sd.alen, sd.amlen);
995    GList<GXSeed> rseeds(true,true,false);
996 <  GXBandSet* alnbands=collectSeeds_R(rseeds, seqa, seqa_len, seqb, seqb_len);
996 >  GXBandSet* alnbands=collectSeeds(rseeds, sd);
997    GList<GXSeed> anchor_seeds(cmpSeedDiag, NULL, true); //stores unique seeds per diagonal
998    //did we find a shortcut?
999    if (alnbands->qmatch) {
1000 <    //#ifdef GDEBUG
1001 <    // GMessage("::: Found a quick long match -- using it instead of diagonal bands\n");
1002 <    //#endif
1000 >    #ifdef GDEBUG
1001 >     GMessage("::: Found a quick long match at %d, len %d\n",
1002 >          alnbands->qmatch->b_ofs, alnbands->qmatch->len);
1003 >    #endif
1004      anchor_seeds.Add(alnbands->qmatch);
1005      }
1006    else {
1007      int max_top_bands=5;
1008      int top_band_count=0;
1009      for (int b=0;b<alnbands->Count();b++) {
1010 <       if (alnbands->Get(b)->score<4) break;
1010 >       if (alnbands->Get(b)->score<6) break;
1011         //#ifdef GDEBUG
1012         //GMessage("\tBand %d score: %d\n", b, alnbands->Get(b)->score);
1013         //#endif
# Line 974 | Line 1015
1015         GXBand& band=*(alnbands->Get(b));
1016         band.seeds.setSorted(cmpSeedScore);
1017         anchor_seeds.Add(band.seeds.First());
1018 <       band.tested=true;
1018 >       //band.tested=true;
1019         if (anchor_seeds.Count()>2 || top_band_count>max_top_bands) break;
1020         }
1021      //#ifdef GDEBUG
1022      //GMessage("::: Collected %d anchor seeds.\n",anchor_seeds.Count());
1023      //#endif
1024      }
984
1025    GList<GXAlnInfo> galns(true,true,false);
1026    for (int i=0;i<anchor_seeds.Count();i++) {
1027      GXSeed& aseed=*anchor_seeds[i];
1028      int a1=aseed.a_ofs+(aseed.len>>1)+1;
1029      int a2=aseed.b_ofs+(aseed.len>>1)+1;
1030 <    GXAlnInfo* alninfo=GreedyAlignRegion(seqa, a1, seqa_len,
1031 <                            seqb, a2, seqb_len, gxmem, &trimInfo, editscript);
1032 <    if (alninfo && alninfo->pid>=min_pid && trimInfo.validate(alninfo->sl, alninfo->sr, alninfo->pid, alninfo->ql-1))
1030 >    trimInfo.seedlen=aseed.len;
1031 > #ifdef GDEBUG
1032 >    GMessage("\t::: align from seed (%d, %d)of len %d.\n",aseed.a_ofs, aseed.b_ofs,
1033 >           aseed.len);
1034 > #endif
1035 >    GXAlnInfo* alninfo=GreedyAlignRegion(sd.aseq, a1, sd.alen,
1036 >                            sd.bseq, a2, sd.blen, gxmem, &trimInfo, editscript);
1037 >    if (alninfo && alninfo->pid>=min_pid && trimInfo.validate(alninfo))
1038               galns.AddIfNew(alninfo, true);
1039          else delete alninfo;
1040      }
1041 <  //special 3' end case: due to the seed scoring scheme being biased towards the 5' end of the read,
997 <  //we should also try some seeds closer to the 3' end
1041 >
1042    if (galns.Count()==0) {
1043 <    anchor_seeds.Clear();
1044 <    alnbands->setSorted(cmpDiagBands_R);
1045 <    int max_top_bands=4;
1046 <    int top_band_count=0;
1047 <    //#ifdef GDEBUG
1048 <    //GMessage(":::>> Retrying adjusting sort order.\n");
1049 <    //#endif
1050 <    if (alnbands->tmatch) {
1051 <      //anchor_seeds.setSorted(false);
1052 <      anchor_seeds.Add(alnbands->tmatch);
1043 >         //last resort: look for weaker terminal seeds
1044 >          GPVec<GXSeed> tmatches(2,false);
1045 >          if (trim_type!=galn_TrimRight) {
1046 >                 if (alnbands->tmatch_l)
1047 >                    tmatches.Add(alnbands->tmatch_l);
1048 >             }
1049 >          if (trim_type!=galn_TrimLeft) {
1050 >                 if (alnbands->tmatch_r)
1051 >                    tmatches.Add(alnbands->tmatch_r);
1052 >             }
1053 >          for (int i=0;i<tmatches.Count();i++) {
1054 >                GXSeed& aseed=*tmatches[i];
1055 >                int halfseed=aseed.len>>1;
1056 >                int a1=aseed.a_ofs+halfseed+1;
1057 >                int a2=aseed.b_ofs+halfseed+1;
1058 >                trimInfo.seedlen=aseed.len;
1059 > #ifdef GDEBUG
1060 >    GMessage("\t::: align from terminal seed (%d, %d)of len %d.\n",aseed.a_ofs, aseed.b_ofs,
1061 >           aseed.len);
1062 > #endif
1063 >        GXAlnInfo* alninfo=GreedyAlignRegion(sd.aseq, a1, sd.alen,
1064 >                                sd.bseq, a2, sd.blen, gxmem, &trimInfo, editscript);
1065 >        if (alninfo && alninfo->pid>=min_pid && trimInfo.validate(alninfo))
1066 >                 galns.AddIfNew(alninfo, true);
1067 >             else delete alninfo;
1068 >        }//for each terminal seed
1069        }
1070 <    for (int b=0;b<alnbands->Count();b++) {
1071 <       if (alnbands->Get(b)->score<4) break;
1072 <       //#ifdef GDEBUG
1073 <       //GMessage("\tBand %d score: %d\n", b, alnbands->Get(b)->score);
1074 <       //#endif
1075 <       if (alnbands->Get(b)->tested) continue;
1076 <       top_band_count++;
1077 <       GXBand& band=*(alnbands->Get(b));
1078 <       band.seeds.setSorted(cmpSeedScore);
1079 <       anchor_seeds.Add(band.seeds.First());
1080 <       if (anchor_seeds.Count()>2 || top_band_count>max_top_bands) break;
1081 <       }
1022 <    //#ifdef GDEBUG
1023 <    //GMessage("::: Collected %d anchor seeds.\n",anchor_seeds.Count());
1024 <    //#endif
1025 <    for (int i=0;i<anchor_seeds.Count();i++) {
1026 <      GXSeed& aseed=*anchor_seeds[i];
1027 <      int a1=aseed.a_ofs+(aseed.len>>1)+1;
1028 <      int a2=aseed.b_ofs+(aseed.len>>1)+1;
1029 <      GXAlnInfo* alninfo=GreedyAlignRegion(seqa, a1, seqa_len,
1030 <                              seqb, a2, seqb_len, gxmem, &trimInfo, editscript);
1031 <      if (alninfo && alninfo->pid>=min_pid && trimInfo.validate(alninfo->sl, alninfo->sr, alninfo->pid, alninfo->ql-1))
1032 <               galns.AddIfNew(alninfo, true);
1033 <          else delete alninfo;
1070 >  //---- found all alignments
1071 >  delete alnbands;
1072 >  /*
1073 >  #ifdef GDEBUG
1074 >  //print all valid alignments found
1075 >  for (int i=0;i<galns.Count();i++) {
1076 >    GXAlnInfo* alninfo=galns[i];
1077 >    GMessage("a(%d..%d) align to b(%d..%d), score=%d, pid=%4.2f\n", alninfo->ql, alninfo->qr,
1078 >                         alninfo->sl, alninfo->sr, alninfo->score, alninfo->pid);
1079 >    if (alninfo->gapinfo!=NULL) {
1080 >      GMessage("Alignment:\n");
1081 >      alninfo->gapinfo->printAlignment(stderr, seqa, seqa_len, seqb, seqb_len);
1082        }
1083      }
1084 <  //---- done
1085 <  delete alnbands;
1084 >  #endif
1085 >  */
1086    if (galns.Count()) {
1087      GXAlnInfo* bestaln=galns.Shift();
1088      #ifdef GDEBUG
1089        GMessage("Best alignment: a(%d..%d) align to b(%d..%d), score=%d, pid=%4.2f\n", bestaln->ql, bestaln->qr,
1090            bestaln->sl, bestaln->sr, bestaln->score, bestaln->pid);
1091        if (bestaln->gapinfo!=NULL) {
1092 <        bestaln->gapinfo->printAlignment(stderr, seqa, seqa_len, seqb, seqb_len);
1092 >        bestaln->gapinfo->printAlignment(stderr, sd.aseq, sd.alen, sd.bseq, sd.blen);
1093          }
1094      #endif
1047
1095      return bestaln;
1096      }
1097    else return NULL;
1098   }
1099 <
1100 < GXAlnInfo* match_LeftEnd(const char* seqa, int seqa_len, const char* seqb, int seqb_len,
1054 <          CGreedyAlignData* gxmem, int min_pid) {
1099 > /*
1100 > GXAlnInfo* match_Left(GXSeqData& sd, CGreedyAlignData* gxmem, int min_pid) {
1101    bool editscript=false;
1102    #ifdef GDEBUG
1103     editscript=true;
1104 <   //GMessage("==========> matching Left (5') end ========\n");
1104 >   GMessage("==========> matching Left (5') end : %s\n", sd.aseq);
1105    #endif
1106 <  CAlnTrim trimInfo(galn_TrimLeft, seqb, seqb_len);
1106 >  CAlnTrim trimInfo(galn_TrimLeft, sd.bseq, sd.blen, sd.alen, sd.amlen);
1107    GList<GXSeed> rseeds(true,true,false);
1108 <  GXBandSet* alnbands=collectSeeds_L(rseeds, seqa, seqa_len, seqb, seqb_len);
1063 <  GList<GXAlnInfo> galns(true,true,false);
1108 >  GXBandSet* alnbands = collectSeeds(rseeds, sd);
1109    GList<GXSeed> anchor_seeds(cmpSeedDiag, NULL, true); //stores unique seeds per diagonal
1065
1110    if (alnbands->qmatch) {
1111 <    //#ifdef GDEBUG
1112 <    // GMessage("::: Found a quick long match -- using it instead of diagonal bands\n");
1113 <    //#endif
1111 >    #ifdef GDEBUG
1112 >     GMessage("::: Found a quick long match at %d, len %d\n",
1113 >          alnbands->qmatch->b_ofs, alnbands->qmatch->len);
1114 >    #endif
1115      anchor_seeds.Add(alnbands->qmatch);
1116      }
1117    else {
1118      int max_top_bands=5;
1119      int top_band_count=0;
1120      for (int b=0;b<alnbands->Count();b++) {
1121 <       if (alnbands->Get(b)->score<4) break;
1121 >       if (alnbands->Get(b)->score<6) break;
1122         //#ifdef GDEBUG
1123         //GMessage("\tBand %d score: %d\n", b, alnbands->Get(b)->score);
1124         //#endif
# Line 1081 | Line 1126
1126         GXBand& band=*(alnbands->Get(b));
1127         band.seeds.setSorted(cmpSeedScore);
1128         anchor_seeds.Add(band.seeds.First());
1129 +       //band.tested=true;
1130         if (anchor_seeds.Count()>2 || top_band_count>max_top_bands) break;
1131         }
1132      //#ifdef GDEBUG
1133      //GMessage("::: Collected %d anchor seeds.\n",anchor_seeds.Count());
1134      //#endif
1135      }
1136 <  for (int i=0;i<anchor_seeds.Count();i++) {
1136 > GList<GXAlnInfo> galns(true,true,false);
1137 > for (int i=0;i<anchor_seeds.Count();i++) {
1138      GXSeed& aseed=*anchor_seeds[i];
1139      int a1=aseed.a_ofs+(aseed.len>>1)+1;
1140      int a2=aseed.b_ofs+(aseed.len>>1)+1;
1141 <    GXAlnInfo* alninfo=GreedyAlignRegion(seqa, a1, seqa_len,
1142 <                            seqb, a2, seqb_len, gxmem, &trimInfo, editscript);
1143 <    if (alninfo && alninfo->pid>=min_pid
1144 <           && trimInfo.validate(alninfo->sl, alninfo->sr, alninfo->pid, seqa_len-alninfo->qr))
1141 >    trimInfo.seedlen=aseed.len;
1142 > #ifdef GDEBUG
1143 >    GMessage("\t::: align from seed (%d, %d)of len %d.\n",aseed.a_ofs, aseed.b_ofs,
1144 >           aseed.len);
1145 > #endif
1146 >    GXAlnInfo* alninfo=GreedyAlignRegion(sd.aseq, a1, sd.alen,
1147 >                            sd.bseq, a2, sd.blen, gxmem, &trimInfo, editscript);
1148 >    if (alninfo && alninfo->pid>=min_pid && trimInfo.validate(alninfo))
1149              galns.AddIfNew(alninfo, true);
1150         else delete alninfo;
1151      }
1152 <  if (galns.Count()==0 && alnbands->tmatch) {
1153 <      GXSeed& aseed=*alnbands->tmatch;
1152 >  if (galns.Count()==0 && alnbands->tmatch_l) {
1153 >      //last resort seed
1154 >      GXSeed& aseed=*alnbands->tmatch_l;
1155        int a1=aseed.a_ofs+(aseed.len>>1)+1;
1156        int a2=aseed.b_ofs+(aseed.len>>1)+1;
1157 <      GXAlnInfo* alninfo=GreedyAlignRegion(seqa, a1, seqa_len,
1158 <                              seqb, a2, seqb_len, gxmem, &trimInfo, editscript);
1159 <      if (alninfo) galns.Add(alninfo);
1157 >      trimInfo.seedlen=aseed.len;
1158 >      GXAlnInfo* alninfo=GreedyAlignRegion(sd.aseq, a1, sd.alen,
1159 >                              sd.bseq, a2, sd.blen, gxmem, &trimInfo, editscript);
1160 >      if (alninfo && alninfo->pid>=min_pid && trimInfo.validate(alninfo))
1161 >         galns.Add(alninfo);
1162        }
1109  #ifdef GDEBUG
1110  /*
1111  for (int i=0;i<galns.Count();i++) {
1112    GXAlnInfo* alninfo=galns[i];
1113    GMessage("a(%d..%d) align to b(%d..%d), score=%d, pid=%4.2f\n", alninfo->ql, alninfo->qr,
1114                         alninfo->sl, alninfo->sr, alninfo->score, alninfo->pid);
1115    if (alninfo->gapinfo!=NULL) {
1116      GMessage("Alignment:\n");
1117      alninfo->gapinfo->printAlignment(stderr, seqa, seqa_len, seqb, seqb_len);
1118      }
1119    }
1120  */
1121  #endif
1163    //---- done
1164    delete alnbands;
1165    if (galns.Count()) {
# Line 1127 | Line 1168
1168        GMessage("Best alignment: a(%d..%d) align to b(%d..%d), score=%d, pid=%4.2f\n", bestaln->ql, bestaln->qr,
1169            bestaln->sl, bestaln->sr, bestaln->score, bestaln->pid);
1170        if (bestaln->gapinfo!=NULL) {
1171 <        bestaln->gapinfo->printAlignment(stderr, seqa, seqa_len, seqb, seqb_len);
1171 >        bestaln->gapinfo->printAlignment(stderr, sd.aseq, sd.alen,
1172 >                                          sd.bseq, sd.blen);
1173          }
1174      #endif
1175      return bestaln;
1176      }
1177    else return NULL;
1178   }
1179 + */

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines