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 101 | Line 101
101   }
102  
103  
104 + uint16 get6mer(char* p) {
105 +  uint16 r=gdna2bit(p,3);
106 +  if (*p) {
107 +   uint16 v=gdna2bit(p,3);
108 +   r |= (v<<8);
109 +   }
110 +  return r;
111 + }
112  
113   //signal that a diagonal is invalid
114   static const int kInvalidOffset = -2;
# Line 129 | Line 137
137               //    }
138        }
139      else { //forward lookup
140 <             while (seq1_index < len1 && seq2_index < len2 &&
140 >             while (seq1_index < len1 && seq2_index < len2 &&
141                     //seq1[seq1_index] < 4 &&
142                     seq1[seq1_index] == seq2[seq2_index]) {
143                  ++seq1_index;
# Line 204 | Line 212
212      int longest_match_run;
213      bool end1_reached, end2_reached;
214      GXMemPool* mem_pool;
215 <
215 >
216      /* ordinary dynamic programming alignment, for each offset
217         in seq1, walks through offsets in seq2 until an X-dropoff
218 <       test fails, saving the best score encountered along
218 >       test fails, saving the best score encountered along
219         the way. Instead of score, this code tracks the 'distance'
220         (number of mismatches plus number of gaps) between seq1
221         and seq2. Instead of walking through sequence offsets, it
222         walks through diagonals that can achieve a given distance.
223 <    
223 >
224         Note that in what follows, the numbering of diagonals implies
225 <       a dot matrix where increasing seq1 offsets go to the right on
225 >       a dot matrix where increasing seq1 offsets go to the right on
226         the x axis, and increasing seq2 offsets go up the y axis.
227         The gapped alignment thus proceeds up and to the right in
228         the graph, and diagonals are numbered increasing to the right */
# Line 223 | Line 231
231      best_diag = 0;
232  
233      /* set the number of distinct distances the algorithm will
234 <       examine in the search for an optimal alignment. The
235 <       heuristic worst-case running time of the algorithm is
234 >       examine in the search for an optimal alignment. The
235 >       heuristic worst-case running time of the algorithm is
236         O(max_dist**2 + (len1+len2)); for sequences which are
237         very similar, the average running time will be sig-
238         nificantly better than this */
239  
240      max_dist = GMIN(GREEDY_MAX_COST,
241 <                   len2 / GREEDY_MAX_COST_FRACTION + 1);
241 >                   (len2/GREEDY_MAX_COST_FRACTION + 1));
242  
243      /* the main loop assumes that the index of all diagonals is
244 <       biased to lie in the middle of allocated bookkeeping
244 >       biased to lie in the middle of allocated bookkeeping
245         structures */
246  
247      diag_origin = max_dist + 2;
# Line 251 | Line 259
259         xdrop_offset gives the distance backwards in the score
260         array to look */
261  
262 <    xdrop_offset = (xdrop_threshold + match_cost / 2) /
262 >    xdrop_offset = (xdrop_threshold + match_cost / 2) /
263                             (match_cost + mismatch_cost) + 1;
264 <    
264 >
265      // find the offset of the first mismatch between seq1 and seq2
266  
267      index = s_FindFirstMismatch(seq1, len1,  seq2, len2, 0, 0, reverse);
# Line 277 | Line 285
285          if (edit_block != NULL)
286              //GapPrelimEditBlockAdd(edit_block, eGapAlignSub, index);
287              edit_block->opRep(index);
288 <        return 0;
288 >        return 0;
289      }
290  
291      // set up the memory pool
# Line 287 | Line 295
295        }
296      else if (mem_pool == NULL) {
297         aux_data.space = mem_pool = new GXMemPool();
298 <    }
298 >    }
299      else {
300          mem_pool->refresh();
301      }
302 <    
302 >
303      /* set up the array of per-distance maximum scores. There
304         are max_diags + xdrop_offset distances to track, the first
305         xdrop_offset of which are 0 */
# Line 299 | Line 307
307      max_score = aux_data.max_score + xdrop_offset;
308      for (index = 0; index < xdrop_offset; index++)
309          aux_data.max_score[index] = 0;
310 <    
310 >
311      // fill in the initial offsets of the distance matrix
312  
313      last_seq2_off[0][diag_origin] = seq1_index;
# Line 328 | Line 336
336  
337          // compute the score for distance d corresponding to the X-dropoff criterion
338  
339 <        xdrop_score = max_score[d - xdrop_offset] +
339 >        xdrop_score = max_score[d - xdrop_offset] +
340                        (match_cost + mismatch_cost) * d - xdrop_threshold;
341 <        xdrop_score = (int)ceil((double)xdrop_score / (match_cost / 2));
341 >        xdrop_score = (int)ceil((double)xdrop_score / (match_cost / 2));
342  
343          // for each diagonal of interest
344          for (k = tmp_diag_lower; k <= tmp_diag_upper; k++) {
345              /* find the largest offset into seq2 that increases
346                 the distance from d-1 to d (i.e. keeps the alignment
347 <               from getting worse for as long as possible), then
347 >               from getting worse for as long as possible), then
348                 choose the offset into seq1 that will keep the
349 <               resulting diagonal fixed at k
350 <            
349 >               resulting diagonal fixed at k
350 >
351                 Note that this requires kInvalidOffset+1 to be smaller
352                 than any valid offset into seq2, i.e. to be negative */
353  
# Line 360 | Line 368
368                  continue;
369              }
370              diag_upper = k;
371 <            
372 <            /* slide down diagonal k until a mismatch
371 >
372 >            /* slide down diagonal k until a mismatch
373                 occurs. As long as only matches are encountered,
374                 the current distance d will not change */
375  
# Line 397 | Line 405
405                 can each be of size at most max_diags+2 */
406  
407              if (seq2_index == len2) {
408 <                diag_lower = k + 1;
408 >                diag_lower = k + 1;
409                  end2_reached = true;
410              }
411              if (seq1_index == len1) {
412 <                diag_upper = k - 1;
412 >                diag_upper = k - 1;
413                  end1_reached = true;
414              }
415          }  // end loop over diagonals
416  
417          // compute the maximum score possible for distance d
418 <        curr_score = curr_extent * (match_cost / 2) -
418 >        curr_score = curr_extent * (match_cost / 2) -
419                          d * (match_cost + mismatch_cost);
420          // if this is the best score seen so far, update the
421          // statistics of the best alignment
# Line 417 | Line 425
425              best_diag = curr_diag;
426              seq2_align_len = curr_seq2_index;
427              seq1_align_len = curr_seq2_index + best_diag - diag_origin;
428 <        }
428 >        }
429          else {
430              max_score[d] = max_score[d - 1];
431          }
# Line 428 | Line 436
436          if (diag_lower > diag_upper)
437              break;
438  
439 <        /* set up for the next distance to examine. Because the
440 <           bounds increase by at most one for each distance,
441 <           diag_lower and diag_upper can each be of size at
439 >        /* set up for the next distance to examine. Because the
440 >           bounds increase by at most one for each distance,
441 >           diag_lower and diag_upper can each be of size at
442             most max_diags+2 */
443  
444          if (!end2_reached)
445 <            diag_lower--;
445 >            diag_lower--;
446          if (!end1_reached)
447              diag_upper++;
448  
# Line 455 | Line 463
463              }
464      }   // end loop over distinct distances
465  
466 <    
466 >
467      if (edit_block == NULL)
468          return best_dist;
469  
470      //----  perform traceback
471 <    d = best_dist;
471 >    d = best_dist;
472      seq1_index = seq1_align_len;
473      seq2_index = seq2_align_len;
474      // for all positive distances
# Line 476 | Line 484
484             traceback operation. best_diag starts off with the
485             value computed during the alignment process */
486  
487 <        new_diag = s_GetNextNonAffineTback(last_seq2_off, d,
487 >        new_diag = s_GetNextNonAffineTback(last_seq2_off, d,
488                                             best_diag, &new_seq2_index);
489  
490          if (new_diag == best_diag) {
# Line 484 | Line 492
492              if (seq2_index - new_seq2_index > 0) {
493                    edit_block->opRep(seq2_index - new_seq2_index);
494              }
495 <        }
495 >        }
496          else if (new_diag < best_diag) {
497              // smaller diagonal: issue a group of substitutions
498              //   and then a gap in seq2 */
# Line 493 | Line 501
501              }
502              //GapPrelimEditBlockAdd(edit_block, eGapAlignIns, 1);
503              edit_block->opIns(1);
504 <        }
504 >        }
505          else {
506              // larger diagonal: issue a group of substitutions
507              //   and then a gap in seq1
# Line 502 | Line 510
510              }
511              edit_block->opDel(1);
512          }
513 <        d--;
514 <        best_diag = new_diag;
515 <        seq2_index = new_seq2_index;
513 >        d--;
514 >        best_diag = new_diag;
515 >        seq2_index = new_seq2_index;
516      }
517   //done:
518      // handle the final group of substitutions back to distance zero,
# Line 526 | Line 534
534        unsigned char op_type = 3 - ( ed_script->ops[i] & gxEDIT_OP_MASK );
535        if (op_type == 3)
536           GError("Error: printEditScript encountered op_type 3 ?!\n");
537 <      GMessage("%d%c ", num, xgapcodes[op_type]);
537 >      GMessage("%d%c ", num, xgapcodes[op_type]);
538        }
539      GMessage("\n");
540    }
# Line 677 | Line 685
685   }
686  
687  
688 < GXBandSet* collectSeeds_R(GList<GXSeed>& seeds, const char* seqa, int a_len, const char* seqb, int b_len) {
688 > GXBandSet* collectSeeds_R(GList<GXSeed>& seeds, const char* seqa, int a_len,
689 >        GVec<uint16> amers[], const char* seqb, int b_len) {
690   //overlap of right (3') end of seqb
691   //hash the first 12 bases of seqa:
692   int aimin=0;
# Line 738 | Line 747
747   return diagstrips;
748   }
749  
750 < GXBandSet* collectSeeds_L(GList<GXSeed>& seeds, const char* seqa, int a_len, const char* seqb, int b_len) {
750 > GXBandSet* collectSeeds_L(GList<GXSeed>& seeds, const char* seqa, int a_len,
751 >        GVec<uint16> amers[], const char* seqb, int b_len) {
752   //overlap of left (5') end of seqb
753   //hash the last 12 bases of seqa:
754   int aimin=GMAX(0,(a_len-16));
# Line 888 | Line 898
898                              reward, penalty, s_ext_r, q_ext_r, *gxmem, ed_script_fwd);
899      //check extension here and bail out if not a good right extension
900      //assuming s_max is really at the right end of s_seq
901 <    if (trim!=NULL && trim->type==galn_TrimRight && s_alnstart+s_ext_r<trim->boundary) {
901 >    if (trim!=NULL && trim->type==galn_TrimRight &&
902 >           s_alnstart+s_ext_r<trim->boundary) {
903        delete ed_script_fwd;
904        if (freeAlnMem) delete gxmem;
905        return NULL;
# Line 943 | Line 954
954       reward, penalty, xdrop, gxmem, trim, editscript);
955   }
956  
957 < GXAlnInfo* match_RightEnd(const char* seqa, int seqa_len, const char* seqb, int seqb_len,
958 <            CGreedyAlignData* gxmem, int min_pid) {
957 > GXAlnInfo* match_RightEnd(const char* seqa, int seqa_len, GVec<uint16> amers[],
958 >                 const char* seqb, int seqb_len, CGreedyAlignData* gxmem, int min_pid) {
959    bool editscript=false;
960    #ifdef GDEBUG
961     editscript=true;
# Line 953 | Line 964
964  
965    CAlnTrim trimInfo(galn_TrimRight, seqb, seqb_len);
966    GList<GXSeed> rseeds(true,true,false);
967 <  GXBandSet* alnbands=collectSeeds_R(rseeds, seqa, seqa_len, seqb, seqb_len);
967 >  GXBandSet* alnbands=collectSeeds_R(rseeds, seqa, seqa_len, amers, seqb, seqb_len);
968    GList<GXSeed> anchor_seeds(cmpSeedDiag, NULL, true); //stores unique seeds per diagonal
969    //did we find a shortcut?
970    if (alnbands->qmatch) {
# Line 1050 | Line 1061
1061    else return NULL;
1062   }
1063  
1064 < GXAlnInfo* match_LeftEnd(const char* seqa, int seqa_len, const char* seqb, int seqb_len,
1065 <          CGreedyAlignData* gxmem, int min_pid) {
1064 > GXAlnInfo* match_LeftEnd(const char* seqa, int seqa_len, GVec<uint16> amers[],
1065 >                 const char* seqb, int seqb_len, CGreedyAlignData* gxmem, int min_pid) {
1066    bool editscript=false;
1067    #ifdef GDEBUG
1068     editscript=true;
# Line 1059 | Line 1070
1070    #endif
1071    CAlnTrim trimInfo(galn_TrimLeft, seqb, seqb_len);
1072    GList<GXSeed> rseeds(true,true,false);
1073 <  GXBandSet* alnbands=collectSeeds_L(rseeds, seqa, seqa_len, seqb, seqb_len);
1073 >  GXBandSet* alnbands=collectSeeds_L(rseeds, seqa, seqa_len, amers, seqb, seqb_len);
1074    GList<GXAlnInfo> galns(true,true,false);
1075    GList<GXSeed> anchor_seeds(cmpSeedDiag, NULL, true); //stores unique seeds per diagonal
1076  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines