]> git.donarmstrong.com Git - mothur.git/blob - alignment.cpp
Merge branch 'master' of https://github.com/mothur/mothur.git
[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                                                                         //      to fill the values of seqAaln and seqBaln
57                 seqAaln = "";
58                 seqBaln = "";
59                 int row = lB-1;
60                 int column = lA-1;
61                 //      seqAstart = 1;
62                 //      seqAend = column;
63                 
64                 AlignmentCell currentCell = alignment[row][column];     //      Start the traceback from the bottom-right corner of the
65                 //      matrix
66                 
67                 if(currentCell.prevCell == 'x'){        seqAaln = seqBaln = "NOALIGNMENT";              }//If there's an 'x' in the bottom-
68                 else{                                                                                           //      right corner bail out because it means nothing got aligned
69                         while(currentCell.prevCell != 'x'){                             //      while the previous cell isn't an 'x', keep going...
70                                 
71                                 if(currentCell.prevCell == 'u'){                        //      if the pointer to the previous cell is 'u', go up in the
72                                         seqAaln = '-' + seqAaln;                                //      matrix.  this indicates that we need to insert a gap in
73                                         seqBaln = seqB[row] + seqBaln;                  //      seqA and a base in seqB
74                                         currentCell = alignment[--row][column];
75                                 }
76                                 else if(currentCell.prevCell == 'l'){           //      if the pointer to the previous cell is 'l', go to the left
77                                         seqBaln = '-' + seqBaln;                                //      in the matrix.  this indicates that we need to insert a gap
78                                         seqAaln = seqA[column] + seqAaln;               //      in seqB and a base in seqA
79                                         currentCell = alignment[row][--column];
80                                 }
81                                 else{
82                                         seqAaln = seqA[column] + seqAaln;               //      otherwise we need to go diagonally up and to the left,
83                                         seqBaln = seqB[row] + seqBaln;                  //      here we add a base to both alignments
84                                         currentCell = alignment[--row][--column];
85                                 }
86                         }
87                 }
88                 
89                 pairwiseLength = seqAaln.length();
90                 seqAstart = 1;  seqAend = 0;
91                 seqBstart = 1;  seqBend = 0;
92                 
93                 for(int i=0;i<seqAaln.length();i++){
94                         if(seqAaln[i] != '-' && seqBaln[i] == '-')              {       seqAstart++;    }
95                         else if(seqAaln[i] == '-' && seqBaln[i] != '-') {       seqBstart++;    }
96                         else                                                                                    {       break;                  }
97                 }
98                 
99                 pairwiseLength -= (seqAstart + seqBstart - 2);
100                 
101                 for(int i=seqAaln.length()-1; i>=0;i--){
102                         if(seqAaln[i] != '-' && seqBaln[i] == '-')              {       seqAend++;              }
103                         else if(seqAaln[i] == '-' && seqBaln[i] != '-') {       seqBend++;              }
104                         else                                                                                    {       break;                  }
105                 }
106                 pairwiseLength -= (seqAend + seqBend);
107                 
108                 seqAend = seqA.length() - seqAend - 1;
109                 seqBend = seqB.length() - seqBend - 1;
110         }
111         catch(exception& e) {
112                 m->errorOut(e, "Alignment", "traceBack");
113                 exit(1);
114         }
115 }
116 /**************************************************************************************************/
117
118 Alignment::~Alignment(){
119         try {
120                 for (int i = 0; i < alignment.size(); i++) {
121                         for (int j = (alignment[i].size()-1); j >= 0; j--) {  alignment[i].pop_back();  }
122                 }
123         }
124         catch(exception& e) {
125                 m->errorOut(e, "Alignment", "~Alignment");
126                 exit(1);
127         }
128 }
129
130 /**************************************************************************************************/
131
132 string Alignment::getSeqAAln(){
133         return seqAaln;                                                                         //      this is called to get the alignment of seqA
134 }
135
136 /**************************************************************************************************/
137
138 string Alignment::getSeqBAln(){
139         return seqBaln;                                                                         //      this is called to get the alignment of seqB                                                     
140 }
141
142 /**************************************************************************************************/
143
144 int Alignment::getCandidateStartPos(){
145         return seqAstart;                                                                       //      this is called to report the quality of the alignment
146 }
147
148 /**************************************************************************************************/
149
150 int Alignment::getCandidateEndPos(){
151         return seqAend;                                                                         //      this is called to report the quality of the alignment
152 }
153
154 /**************************************************************************************************/
155
156 int Alignment::getTemplateStartPos(){
157         return seqBstart;                                                                       //      this is called to report the quality of the alignment
158 }
159
160 /**************************************************************************************************/
161
162 int Alignment::getTemplateEndPos(){
163         return seqBend;                                                                         //      this is called to report the quality of the alignment
164 }
165
166 /**************************************************************************************************/
167
168 int Alignment::getPairwiseLength(){
169         return pairwiseLength;                                                          //      this is the pairwise alignment length
170 }
171
172 /**************************************************************************************************/
173
174 //int Alignment::getLongestTemplateGap(){
175 //
176 //      int length = seqBaln.length();
177 //      int longGap = 0;
178 //      int gapLength = 0;
179 //      
180 //      int start = seqAstart;
181 //      if(seqAstart < seqBstart){      start = seqBstart;      }
182 //      for(int i=seqAstart;i<length;i++){
183 //              if(seqBaln[i] == '-'){
184 //                      gapLength++;
185 //              }
186 //              else{
187 //                      if(gapLength > 0){
188 //                              if(gapLength > longGap){        longGap = gapLength;    }
189 //                      }
190 //                      gapLength = 0;
191 //              }
192 //      }
193 //      return longGap;
194 //}
195
196 /**************************************************************************************************/