]> git.donarmstrong.com Git - mothur.git/blob - alignment.cpp
added check to cluster.classic to make sure file type is phylip. added mapping funct...
[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() {        m = MothurOut::getInstance(); /*        do nothing      */      }
19
20 /**************************************************************************************************/
21
22 Alignment::Alignment(int A) : nCols(A), nRows(A) {
23         try {
24  
25                 m = MothurOut::getInstance();
26                 alignment.resize(nRows);                        //      For the Gotoh and Needleman-Wunsch we initialize the dynamic programming
27                 for(int i=0;i<nRows;i++){                       //      matrix by initializing a matrix that is A x A.  By default we will set A
28                         alignment[i].resize(nCols);             //      at 2000 for 16S rRNA gene sequences
29                 }       
30         }
31         catch(exception& e) {
32                 m->errorOut(e, "Alignment", "Alignment");
33                 exit(1);
34         }
35 }
36 /**************************************************************************************************/
37 void Alignment::resize(int A) {
38         try {
39                 nCols = A;
40                 nRows = A;
41
42                 alignment.resize(nRows);                        
43                 for(int i=0;i<nRows;i++){                       
44                         alignment[i].resize(nCols);             
45                 }       
46         }
47         catch(exception& e) {
48                 m->errorOut(e, "Alignment", "resize");
49                 exit(1);
50         }
51 }
52 /**************************************************************************************************/
53
54 void Alignment::traceBack(){                    //      This traceback routine is used by the dynamic programming algorithms
55         try {   
56                 BBaseMap.clear();
57         ABaseMap.clear(); //    to fill the values of seqAaln and seqBaln
58                 seqAaln = "";
59                 seqBaln = "";
60                 int row = lB-1;
61                 int column = lA-1;
62                 //      seqAstart = 1;
63                 //      seqAend = column;
64                 
65                 AlignmentCell currentCell = alignment[row][column];     //      Start the traceback from the bottom-right corner of the
66                 //      matrix
67                 
68                 if(currentCell.prevCell == 'x'){        seqAaln = seqBaln = "NOALIGNMENT";              }//If there's an 'x' in the bottom-
69                 else{   //      right corner bail out because it means nothing got aligned
70             int count = 0;
71                         while(currentCell.prevCell != 'x'){                             //      while the previous cell isn't an 'x', keep going...
72                                 
73                                 if(currentCell.prevCell == 'u'){                        //      if the pointer to the previous cell is 'u', go up in the
74                                         seqAaln = '-' + seqAaln;                                //      matrix.  this indicates that we need to insert a gap in
75                                         seqBaln = seqB[row] + seqBaln;                  //      seqA and a base in seqB
76                     BBaseMap[row] = count;
77                                         currentCell = alignment[--row][column];
78                                 }
79                                 else if(currentCell.prevCell == 'l'){           //      if the pointer to the previous cell is 'l', go to the left
80                                         seqBaln = '-' + seqBaln;                                //      in the matrix.  this indicates that we need to insert a gap
81                                         seqAaln = seqA[column] + seqAaln;               //      in seqB and a base in seqA
82                     ABaseMap[column] = count;
83                                         currentCell = alignment[row][--column];
84                                 }
85                                 else{
86                                         seqAaln = seqA[column] + seqAaln;               //      otherwise we need to go diagonally up and to the left,
87                                         seqBaln = seqB[row] + seqBaln;                  //      here we add a base to both alignments
88                     BBaseMap[row] = count;
89                     ABaseMap[column] = count;
90                                         currentCell = alignment[--row][--column];
91                                 }
92                 count++;
93                         }
94                 }
95                 
96        
97         pairwiseLength = seqAaln.length();
98                 seqAstart = 1;  seqAend = 0;
99                 seqBstart = 1;  seqBend = 0;
100         //flip maps since we now know the total length
101         map<int, int> newAMap;
102         for (map<int, int>::iterator it = ABaseMap.begin(); it != ABaseMap.end(); it++) {
103             int spot = it->second;
104             newAMap[pairwiseLength-spot-1] = it->first-1;
105         }
106         ABaseMap = newAMap;
107         map<int, int> newBMap;
108         for (map<int, int>::iterator it = BBaseMap.begin(); it != BBaseMap.end(); it++) {
109             int spot = it->second;
110             newBMap[pairwiseLength-spot-1] = it->first-1;
111         }
112                 BBaseMap = newBMap;
113                 for(int i=0;i<seqAaln.length();i++){
114                         if(seqAaln[i] != '-' && seqBaln[i] == '-')              {       seqAstart++;    }
115                         else if(seqAaln[i] == '-' && seqBaln[i] != '-') {       seqBstart++;    }
116                         else                                                                                    {       break;                  }
117                 }
118                 
119                 pairwiseLength -= (seqAstart + seqBstart - 2);
120                 
121                 for(int i=seqAaln.length()-1; i>=0;i--){
122                         if(seqAaln[i] != '-' && seqBaln[i] == '-')              {       seqAend++;              }
123                         else if(seqAaln[i] == '-' && seqBaln[i] != '-') {       seqBend++;              }
124                         else                                                                                    {       break;                  }
125                 }
126                 pairwiseLength -= (seqAend + seqBend);
127                 
128                 seqAend = seqA.length() - seqAend - 1;
129                 seqBend = seqB.length() - seqBend - 1;
130         }
131         catch(exception& e) {
132                 m->errorOut(e, "Alignment", "traceBack");
133                 exit(1);
134         }
135 }
136 /**************************************************************************************************/
137
138 Alignment::~Alignment(){
139         try {
140                 for (int i = 0; i < alignment.size(); i++) {
141                         for (int j = (alignment[i].size()-1); j >= 0; j--) {  alignment[i].pop_back();  }
142                 }
143         }
144         catch(exception& e) {
145                 m->errorOut(e, "Alignment", "~Alignment");
146                 exit(1);
147         }
148 }
149
150 /**************************************************************************************************/
151
152 string Alignment::getSeqAAln(){
153         return seqAaln;                                                                         //      this is called to get the alignment of seqA
154 }
155
156 /**************************************************************************************************/
157
158 string Alignment::getSeqBAln(){
159         return seqBaln;                                                                         //      this is called to get the alignment of seqB                                                     
160 }
161
162 /**************************************************************************************************/
163
164 int Alignment::getCandidateStartPos(){
165         return seqAstart;                                                                       //      this is called to report the quality of the alignment
166 }
167
168 /**************************************************************************************************/
169
170 int Alignment::getCandidateEndPos(){
171         return seqAend;                                                                         //      this is called to report the quality of the alignment
172 }
173
174 /**************************************************************************************************/
175
176 int Alignment::getTemplateStartPos(){
177         return seqBstart;                                                                       //      this is called to report the quality of the alignment
178 }
179 /**************************************************************************************************/
180
181 map<int, int> Alignment::getSeqAAlnBaseMap(){
182         return ABaseMap;                                                                        
183 }
184 /**************************************************************************************************/
185
186 map<int, int> Alignment::getSeqBAlnBaseMap(){
187         return BBaseMap;                                                                        
188 }
189 /**************************************************************************************************/
190
191 int Alignment::getTemplateEndPos(){
192         return seqBend;                                                                         //      this is called to report the quality of the alignment
193 }
194
195 /**************************************************************************************************/
196
197 int Alignment::getPairwiseLength(){
198         return pairwiseLength;                                                          //      this is the pairwise alignment length
199 }
200
201 /**************************************************************************************************/
202
203 //int Alignment::getLongestTemplateGap(){
204 //
205 //      int length = seqBaln.length();
206 //      int longGap = 0;
207 //      int gapLength = 0;
208 //      
209 //      int start = seqAstart;
210 //      if(seqAstart < seqBstart){      start = seqBstart;      }
211 //      for(int i=seqAstart;i<length;i++){
212 //              if(seqBaln[i] == '-'){
213 //                      gapLength++;
214 //              }
215 //              else{
216 //                      if(gapLength > 0){
217 //                              if(gapLength > longGap){        longGap = gapLength;    }
218 //                      }
219 //                      gapLength = 0;
220 //              }
221 //      }
222 //      return longGap;
223 //}
224
225 /**************************************************************************************************/