]> git.donarmstrong.com Git - mothur.git/blob - gotohoverlap.cpp
fixed some bugs
[mothur.git] / gotohoverlap.cpp
1 /*
2  *  gotohoverlap.cpp
3  *  
4  *
5  *  Created by Pat Schloss on 12/15/08.
6  *  Copyright 2008 Patrick D. Schloss. All rights reserved.
7  *
8  *      This class is an Alignment child class that implements the Gotoh pairwise alignment algorithm as described in:
9  *              
10  *              Gotoh O. 1982.  An improved algorithm for matching biological sequences.  J. Mol. Biol.  162:705-8.
11  *              Myers, EW & Miller, W.  1988.  Optimal alignments in linear space.  Comput Appl Biosci. 4:11-7.
12  *
13  *      This method is nice because it allows for an affine gap penalty to be assessed, which is analogous to what is used
14  *      in blast and is an alternative to Needleman-Wunsch, which only charges the same penalty for each gap position.
15  *      Because this method typically has problems at the ends when two sequences do not full overlap, we employ a separate
16  *      method to fix the ends (see Overlap class documentation)
17  *
18  */
19
20
21 #include "alignmentcell.hpp"
22 #include "overlap.hpp"
23 #include "alignment.hpp"
24 #include "gotohoverlap.hpp"
25
26 /**************************************************************************************************/
27
28 GotohOverlap::GotohOverlap(float gO, float gE, float m, float mm, int r) :
29         gapOpen(gO), gapExtend(gE), match(m), mismatch(mm), Alignment(r) {
30         
31         for(int i=1;i<nCols;i++){                               //      we initialize the dynamic programming matrix by setting the pointers in
32                 alignment[0][i].prevCell = 'l';         //      the first row to the left
33                 alignment[0][i].cValue = 0;
34                 alignment[0][i].dValue = 0;
35         }
36         
37         for(int i=1;i<nRows;i++){                               //      we initialize the dynamic programming matrix by setting the pointers in
38                 alignment[i][0].prevCell = 'u';         //      the first column upward
39                 alignment[i][0].cValue = 0;
40                 alignment[i][0].iValue = 0;
41         }
42 }
43
44 /**************************************************************************************************/
45
46 void GotohOverlap::align(string A, string B){
47         
48         seqA = ' ' + A; lA = seqA.length();             //      the algorithm requires that the first character be a dummy value
49         seqB = ' ' + B; lB = seqB.length();             //      the algorithm requires that the first character be a dummy value
50         
51         for(int i=1;i<lB;i++){                                  //      the recursion here is shown in Webb and Miller, Fig. 1A.  Note that 
52                 for(int j=1;j<lA;j++){                          //      if we need to conserve on space we should see Fig. 1B, which is linear
53                                                                                         //      in space, which I think is unnecessary
54                         float diagonal;
55                         if(seqB[i] == seqA[j])  {       diagonal = alignment[i-1][j-1].cValue + match;          }
56                         else                                    {       diagonal = alignment[i-1][j-1].cValue + mismatch;       }
57                         
58                         alignment[i][j].iValue = max(alignment[i][j-1].iValue, alignment[i][j-1].cValue + gapOpen) + gapExtend;
59                         alignment[i][j].dValue = max(alignment[i-1][j].dValue, alignment[i-1][j].cValue + gapOpen) + gapExtend;
60                         
61                         if(alignment[i][j].iValue > alignment[i][j].dValue){
62                                 if(alignment[i][j].iValue > diagonal){
63                                         alignment[i][j].cValue = alignment[i][j].iValue;
64                                         alignment[i][j].prevCell = 'l';
65                                 }
66                                 else{
67                                         alignment[i][j].cValue = diagonal;
68                                         alignment[i][j].prevCell = 'd';
69                                 }
70                         }
71                         else{
72                                 if(alignment[i][j].dValue > diagonal){
73                                         alignment[i][j].cValue = alignment[i][j].dValue;
74                                         alignment[i][j].prevCell = 'u';
75                                 }
76                                 else{
77                                         alignment[i][j].cValue = diagonal;
78                                         alignment[i][j].prevCell = 'd';
79                                 }
80                         }
81                         
82                 }
83         }
84         Overlap over;
85         over.setOverlap(alignment, lA, lB, 0);  //      Fix the gaps at the ends of the sequences
86         traceBack();                                                    //      Construct the alignment and set seqAaln and seqBaln
87 }
88
89 /**************************************************************************************************/