]> git.donarmstrong.com Git - mothur.git/blob - alignment.cpp
fixed bugs in venn and aligner
[mothur.git] / alignment.cpp
1 /*
2  *  alignment.cpp
3  *
4  *  Created by Pat Schloss on 12/15/08.
5  *  Copyright 2008 Patrick D. Schloss. All rights reserved.
6  *
7  *  This is a class for an abstract datatype for classes that implement various types of alignment      algorithms.
8  *      As of 12/18/08 these included alignments based on blastn, needleman-wunsch, and the     Gotoh algorithms
9  *  
10  */
11
12 #include "alignmentcell.hpp"
13 #include "alignment.hpp"
14
15
16 /**************************************************************************************************/
17
18 Alignment::Alignment() {        /*      do nothing      */      }
19
20 /**************************************************************************************************/
21
22 Alignment::Alignment(int A) : nCols(A), nRows(A) {
23         try {
24                 alignment.resize(nRows);                        //      For the Gotoh and Needleman-Wunsch we initialize the dynamic programming
25                 for(int i=0;i<nRows;i++){                       //      matrix by initializing a matrix that is A x A.  By default we will set A
26                         alignment[i].resize(nCols);             //      at 2000 for 16S rRNA gene sequences
27                 }       
28         }
29         catch(exception& e) {
30                 errorOut(e, "Alignment", "Alignment");
31                 exit(1);
32         }
33 }
34
35 /**************************************************************************************************/
36
37 void Alignment::traceBack(){                    //      This traceback routine is used by the dynamic programming algorithms
38         try {   
39                                                                         //      to fill the values of seqAaln and seqBaln
40                 seqAaln = "";
41                 seqBaln = "";
42                 int row = lB-1;
43                 int column = lA-1;
44                 //      seqAstart = 1;
45                 //      seqAend = column;
46                 
47                 AlignmentCell currentCell = alignment[row][column];     //      Start the traceback from the bottom-right corner of the
48                 //      matrix
49                 
50                 if(currentCell.prevCell == 'x'){        seqAaln = seqBaln = "NOALIGNMENT";              }//If there's an 'x' in the bottom-
51                 else{                                                                                           //      right corner bail out because it means nothing got aligned
52                         while(currentCell.prevCell != 'x'){                             //      while the previous cell isn't an 'x', keep going...
53                                 
54                                 if(currentCell.prevCell == 'u'){                        //      if the pointer to the previous cell is 'u', go up in the
55                                         seqAaln = '-' + seqAaln;                                //      matrix.  this indicates that we need to insert a gap in
56                                         seqBaln = seqB[row] + seqBaln;                  //      seqA and a base in seqB
57                                         currentCell = alignment[--row][column];
58                                 }
59                                 else if(currentCell.prevCell == 'l'){           //      if the pointer to the previous cell is 'l', go to the left
60                                         seqBaln = '-' + seqBaln;                                //      in the matrix.  this indicates that we need to insert a gap
61                                         seqAaln = seqA[column] + seqAaln;               //      in seqB and a base in seqA
62                                         currentCell = alignment[row][--column];
63                                 }
64                                 else{
65                                         seqAaln = seqA[column] + seqAaln;               //      otherwise we need to go diagonally up and to the left,
66                                         seqBaln = seqB[row] + seqBaln;                  //      here we add a base to both alignments
67                                         currentCell = alignment[--row][--column];
68                                 }
69                         }
70                 }
71                 
72                 pairwiseLength = seqAaln.length();
73                 seqAstart = 1;  seqAend = 0;
74                 seqBstart = 1;  seqBend = 0;
75                 
76                 for(int i=0;i<seqAaln.length();i++){
77                         if(seqAaln[i] != '-' && seqBaln[i] == '-')              {       seqAstart++;    }
78                         else if(seqAaln[i] == '-' && seqBaln[i] != '-') {       seqBstart++;    }
79                         else                                                                                    {       break;                  }
80                 }
81                 
82                 pairwiseLength -= (seqAstart + seqBstart - 2);
83                 
84                 for(int i=seqAaln.length()-1; i>=0;i--){
85                         if(seqAaln[i] != '-' && seqBaln[i] == '-')              {       seqAend++;              }
86                         else if(seqAaln[i] == '-' && seqBaln[i] != '-') {       seqBend++;              }
87                         else                                                                                    {       break;                  }
88                 }
89                 pairwiseLength -= (seqAend + seqBend);
90                 
91                 seqAend = seqA.length() - seqAend - 1;
92                 seqBend = seqB.length() - seqBend - 1;
93         }
94         catch(exception& e) {
95                 errorOut(e, "Alignment", "traceBack");
96                 exit(1);
97         }
98 }
99 /**************************************************************************************************/
100
101 Alignment::~Alignment(){
102         try {
103                 for (int i = 0; i < alignment.size(); i++) {
104                         for (int j = (alignment[i].size()-1); j >= 0; j--) {  alignment[i].pop_back();  }
105                 }
106         }
107         catch(exception& e) {
108                 errorOut(e, "Alignment", "~Alignment");
109                 exit(1);
110         }
111 }
112
113 /**************************************************************************************************/
114
115 string Alignment::getSeqAAln(){
116         return seqAaln;                                                                         //      this is called to get the alignment of seqA
117 }
118
119 /**************************************************************************************************/
120
121 string Alignment::getSeqBAln(){
122         return seqBaln;                                                                         //      this is called to get the alignment of seqB                                                     
123 }
124
125 /**************************************************************************************************/
126
127 int Alignment::getCandidateStartPos(){
128         return seqAstart;                                                                       //      this is called to report the quality of the alignment
129 }
130
131 /**************************************************************************************************/
132
133 int Alignment::getCandidateEndPos(){
134         return seqAend;                                                                         //      this is called to report the quality of the alignment
135 }
136
137 /**************************************************************************************************/
138
139 int Alignment::getTemplateStartPos(){
140         return seqBstart;                                                                       //      this is called to report the quality of the alignment
141 }
142
143 /**************************************************************************************************/
144
145 int Alignment::getTemplateEndPos(){
146         return seqBend;                                                                         //      this is called to report the quality of the alignment
147 }
148
149 /**************************************************************************************************/
150
151 int Alignment::getPairwiseLength(){
152         return pairwiseLength;                                                          //      this is the pairwise alignment length
153 }
154
155 /**************************************************************************************************/
156
157 //int Alignment::getLongestTemplateGap(){
158 //
159 //      int length = seqBaln.length();
160 //      int longGap = 0;
161 //      int gapLength = 0;
162 //      
163 //      int start = seqAstart;
164 //      if(seqAstart < seqBstart){      start = seqBstart;      }
165 //      for(int i=seqAstart;i<length;i++){
166 //              if(seqBaln[i] == '-'){
167 //                      gapLength++;
168 //              }
169 //              else{
170 //                      if(gapLength > 0){
171 //                              if(gapLength > longGap){        longGap = gapLength;    }
172 //                      }
173 //                      gapLength = 0;
174 //              }
175 //      }
176 //      return longGap;
177 //}
178
179 /**************************************************************************************************/