]> git.donarmstrong.com Git - mothur.git/blob - nast.cpp
modified sequence class to read fasta files with comments. this required modification...
[mothur.git] / nast.cpp
1 /*
2  *  nast.cpp
3  *  
4  *
5  *  Created by Pat Schloss on 12/17/08.
6  *  Copyright 2008 Patrick D. Schloss. All rights reserved.
7  *
8  *      This is my implementation of the NAST (nearest alignment space termination) algorithm as described in:
9  *
10  *      DeSantis TZ, Hugenholtz P, Keller K, Brodie EL, Larsen N, Piceno YM, Phan R, & Anderson GL.  2006.  NAST: a multiple
11  *              sequence alignment server for comparative analysis of 16S rRNA genes.  Nucleic Acids Research.  34:W394-9.
12  *
13  *      To construct an object one needs to provide a method of getting a pairwise alignment (alignment) and the template
14  *      and candidate sequence that are to be aligned to each other.
15  *
16  */
17
18 #include "sequence.hpp"
19 #include "alignment.hpp"
20 #include "nast.hpp"
21
22 /**************************************************************************************************/
23
24 Nast::Nast(Alignment* method, Sequence* cand, Sequence* temp) : alignment(method), candidateSeq(cand), templateSeq(temp) {
25         try {
26                 maxInsertLength = 0;
27                 pairwiseAlignSeqs();    //      This is part A in Fig. 2 of DeSantis et al.
28                 regapSequences();               //      This is parts B-F in Fig. 2 of DeSantis et al.
29
30         }
31         catch(exception& e) {
32                 errorOut(e, "Nast", "Nast");
33                 exit(1);
34         }
35 }
36
37 /**************************************************************************************************/
38
39 void Nast::pairwiseAlignSeqs(){ //      Here we call one of the pairwise alignment methods to align our unaligned candidate
40                                                                 //      and template sequences
41         try {
42
43                 alignment->align(candidateSeq->getUnaligned(), templateSeq->getUnaligned());
44         
45                 string candAln = alignment->getSeqAAln();
46                 string tempAln = alignment->getSeqBAln();
47         
48                 if(candAln == ""){
49
50                         candidateSeq->setPairwise("");
51                         templateSeq->setPairwise(templateSeq->getUnaligned());
52
53                 }
54                 else{
55                         if(tempAln[0] == '-'){
56                                 int pairwiseAlignmentLength = tempAln.length(); //      we need to make sure that the candidate sequence alignment
57                                 for(int i=0;i<pairwiseAlignmentLength;i++){             //      starts where the template sequence alignment starts, if it
58                                         if(isalpha(tempAln[i])){                                        //      starts early, we nuke the beginning of the candidate
59                                                 candAln = candAln.substr(i);                    //      sequence
60                                                 tempAln = tempAln.substr(i);
61                                                 break;
62                                         }
63                                 }
64                         }
65                         int pairwiseAlignmentLength = tempAln.length();
66                         if(tempAln[pairwiseAlignmentLength-1] == '-'){          //      we need to make sure that the candidate sequence alignment
67                                 for(int i=pairwiseAlignmentLength-1; i>=0; i--){//      ends where the template sequence alignment ends, if it runs
68                                         if(isalpha(tempAln[i])){                                        //      long, we nuke the end of the candidate sequence
69                                                 candAln = candAln.substr(0,i+1);
70                                                 tempAln = tempAln.substr(0,i+1);
71                                                 break;
72                                         }               
73                                 }
74                         }
75
76                 }
77
78                 candidateSeq->setPairwise(candAln);                                     //      set the pairwise sequences in the Sequence objects for
79                 templateSeq->setPairwise(tempAln);                                      //      the candidate and template sequences
80         }
81         catch(exception& e) {
82                 errorOut(e, "Nast", "pairwiseAlignSeqs");
83                 exit(1);
84         }       
85 }
86
87 /**************************************************************************************************/
88
89 void Nast::removeExtraGaps(string& candAln, string tempAln, string newTemplateAlign){
90
91 //      here we do steps C-F of Fig. 2 from DeSantis et al.
92         try {
93         
94                 int longAlignmentLength = newTemplateAlign.length();    
95         
96                 for(int i=0; i<longAlignmentLength; i++){                               //      use the long alignment as the standard
97                         int rightIndex, rightRoom, leftIndex, leftRoom;
98                         
99                         //      Part C of Fig. 2 from DeSantis et al.
100                         if((isalpha(newTemplateAlign[i]) != isalpha(tempAln[i]))){      //if there is a discrepancy between the regapped
101                                 
102                                 rightRoom = 0; leftRoom = 0;
103                                 
104                                 //      Part D of Fig. 2 from DeSantis et al.           //      template sequence and the official template sequence
105                                 for(leftIndex=i-1;leftIndex>0;leftIndex--){     //      then we've got problems...
106                                         if(!isalpha(candAln[leftIndex])){
107                                                 leftRoom = 1;   //count how far it is to the nearest gap on the LEFT side of the anomaly
108                                                 while(leftIndex-leftRoom>=0 && !isalpha(candAln[leftIndex-leftRoom]))   {       leftRoom++;             }
109                                                 break;
110                                         }
111                                 }
112
113                                 for(rightIndex=i+1;rightIndex<longAlignmentLength-1;rightIndex++){
114                                         if(!isalpha(candAln[rightIndex])){
115                                                 rightRoom = 1;  //count how far it is to the nearest gap on the RIGHT side of the anomaly
116                                                 while(rightIndex+rightRoom<longAlignmentLength && !isalpha(candAln[rightIndex+rightRoom]))      {       rightRoom++;    }
117                                                 break;
118                                         }
119                                 }
120                                         
121                                 int insertLength = 0;                                                   //      figure out how long the anomaly is
122                                 while(!isalpha(newTemplateAlign[i + insertLength]))     {       insertLength++; }
123                                 if(insertLength > maxInsertLength){     maxInsertLength = insertLength; }
124                 
125                                 if((leftRoom + rightRoom) >= insertLength){
126         
127                                         //      Parts D & E from Fig. 2 of DeSantis et al.
128                                         if((i-leftIndex) <= (rightIndex-i)){            //      the left gap is closer - > move stuff left there's
129         
130                                                 if(leftRoom >= insertLength){                   //      enough room to the left to move
131         
132                                                         string leftTemplateString = newTemplateAlign.substr(0,i);
133                                                         string rightTemplateString = newTemplateAlign.substr(i+insertLength);
134                                                         newTemplateAlign = leftTemplateString + rightTemplateString;
135                                                         longAlignmentLength = newTemplateAlign.length();
136                                                         
137                                                         string leftCandidateString = candAln.substr(0,leftIndex-insertLength+1);
138                                                         string rightCandidateString = candAln.substr(leftIndex+1);
139                                                         candAln = leftCandidateString + rightCandidateString;
140                 
141                                                 }
142                                                 else{                                                                   //      not enough room to the left, have to steal some space to
143                                                         string leftTemplateString = newTemplateAlign.substr(0,i);       //      the right
144                                                         string rightTemplateString = newTemplateAlign.substr(i+insertLength);
145                                                         newTemplateAlign = leftTemplateString + rightTemplateString;
146                                                         longAlignmentLength = newTemplateAlign.length();
147                                                         
148                                                         string leftCandidateString = candAln.substr(0,leftIndex-leftRoom+1);
149                                                         string insertString = candAln.substr(leftIndex+1,rightIndex-leftIndex-1);
150                                                         string rightCandidateString = candAln.substr(rightIndex+(insertLength-leftRoom));
151                                                         candAln = leftCandidateString + insertString + rightCandidateString;
152                                 
153                                                 }
154                                         }
155                                         else{                                                                           //      the right gap is closer - > move stuff right there's
156                                                 if(rightRoom >= insertLength){                  //      enough room to the right to move
157                         
158                                                         string leftTemplateString = newTemplateAlign.substr(0,i);
159                                                         string rightTemplateString = newTemplateAlign.substr(i+insertLength);
160                                                         newTemplateAlign = leftTemplateString + rightTemplateString;
161                                                         longAlignmentLength = newTemplateAlign.length();
162                                                         
163                                                         string leftCandidateString = candAln.substr(0,rightIndex);
164                                                         string rightCandidateString = candAln.substr(rightIndex+insertLength);
165                                                         candAln = leftCandidateString + rightCandidateString;   
166                                                                         
167                                                 }
168                                                 else{                                                                   //      not enough room to the right, have to steal some 
169                         
170                                                         //      space to the left lets move left and then right...
171                                                         string leftTemplateString = newTemplateAlign.substr(0,i);
172                                                         string rightTemplateString = newTemplateAlign.substr(i+insertLength);
173                                                         newTemplateAlign = leftTemplateString + rightTemplateString;
174                                                         longAlignmentLength = newTemplateAlign.length();
175                                                                         
176                                                         string leftCandidateString = candAln.substr(0,leftIndex-(insertLength-rightRoom)+1);
177                                                         string insertString = candAln.substr(leftIndex+1,rightIndex-leftIndex-1);
178                                                         string rightCandidateString = candAln.substr(rightIndex+rightRoom);
179                                                         candAln = leftCandidateString + insertString + rightCandidateString;    
180                                                                         
181                                                 }
182                                         }
183                                 }
184                                 else{
185                         //      there could be a case where there isn't enough room in
186                                         string leftTemplateString = newTemplateAlign.substr(0,i);                       //      either direction to move stuff
187                                         string rightTemplateString = newTemplateAlign.substr(i+leftRoom+rightRoom);
188                                         newTemplateAlign = leftTemplateString + rightTemplateString;
189                                         longAlignmentLength = newTemplateAlign.length();
190                                                         
191                                         string leftCandidateString = candAln.substr(0,leftIndex-leftRoom+1);
192                                         string insertString = candAln.substr(leftIndex+1,rightIndex-leftIndex-1);
193                                         string rightCandidateString = candAln.substr(rightIndex+rightRoom);
194                                         candAln = leftCandidateString + insertString + rightCandidateString;
195
196                                 }
197                         
198                                 i -= insertLength;
199                         } 
200                 }
201         }
202         catch(exception& e) {
203                 errorOut(e, "Nast", "removeExtraGaps");
204                 exit(1);
205         }       
206 }
207
208 /**************************************************************************************************/
209
210 void Nast::regapSequences(){    //This is essentially part B in Fig 2. of DeSantis et al.
211         try { 
212         
213                 string candPair = candidateSeq->getPairwise();
214                 string candAln = "";
215                 
216                 string tempPair = templateSeq->getPairwise();
217                 string tempAln = templateSeq->getAligned();             //      we use the template aligned sequence as our guide
218                 
219                 int pairwiseLength = candPair.length();
220                 int fullAlignLength = tempAln.length();
221                 
222                 if(candPair == ""){
223                         for(int i=0;i<fullAlignLength;i++)      {       candAln += '.';         }
224                         candidateSeq->setAligned(candAln);
225                         return;
226                 }
227         
228                 int fullAlignIndex = 0;
229                 int pairwiseAlignIndex = 0;
230                 string newTemplateAlign = "";                                   //      this is going to be messy so we want a temporary template
231                 //      alignment string
232                 while(tempAln[fullAlignIndex] == '.' || tempAln[fullAlignIndex]  == '-'){
233                         candAln += '.';                                                         //      add the initial '-' and '.' to the candidate and template
234                         newTemplateAlign += tempAln[fullAlignIndex];//  pairwise sequences
235                         fullAlignIndex++;
236                 }
237
238                 string lastLoop = "";
239                 
240                 while(pairwiseAlignIndex<pairwiseLength){
241                         if(isalpha(tempPair[pairwiseAlignIndex]) && isalpha(tempAln[fullAlignIndex])
242                            && isalpha(candPair[pairwiseAlignIndex])){
243                                 //  the template and candidate pairwise and template aligned have characters
244                                 //      need to add character onto the candidatSeq.aligned sequence
245                                 
246                                 candAln += candPair[pairwiseAlignIndex];
247                                 newTemplateAlign += tempPair[pairwiseAlignIndex];//
248                                 
249                                 pairwiseAlignIndex++;
250                                 fullAlignIndex++;
251                         }
252                         else if(isalpha(tempPair[pairwiseAlignIndex]) && !isalpha(tempAln[fullAlignIndex])
253                                         && isalpha(candPair[pairwiseAlignIndex])){
254                                 //      the template pairwise and candidate pairwise are characters and the template aligned is a gap
255                                 //      need to insert gaps into the candidateSeq.aligned sequence
256                                 
257                                 candAln += '-';
258                                 newTemplateAlign += '-';//
259                                 fullAlignIndex++;
260                         }
261                         else if(!isalpha(tempPair[pairwiseAlignIndex]) && isalpha(tempAln[fullAlignIndex])
262                                         && isalpha(candPair[pairwiseAlignIndex])){
263                                 //  the template pairwise is a gap and the template aligned and pairwise sequences have characters
264                                 //      this is the alpha scenario.  add character to the candidateSeq.aligned sequence without progressing
265                                 //      further through the tempAln sequence.
266                                 
267                                 candAln += candPair[pairwiseAlignIndex];
268                                 newTemplateAlign += '-';//
269                                 pairwiseAlignIndex++;
270                         }
271                         else if(isalpha(tempPair[pairwiseAlignIndex]) && isalpha(tempAln[fullAlignIndex])
272                                         && !isalpha(candPair[pairwiseAlignIndex])){
273                                 //  the template pairwise and full alignment are characters and the candidate sequence has a gap
274                                 //      should not be a big deal, just add the gap position to the candidateSeq.aligned sequence;
275                                 
276                                 candAln += candPair[pairwiseAlignIndex];
277                                 newTemplateAlign += tempAln[fullAlignIndex];//
278                                 fullAlignIndex++;                       
279                                 pairwiseAlignIndex++;
280                         }
281                         else if(!isalpha(tempPair[pairwiseAlignIndex]) && !isalpha(tempAln[fullAlignIndex])
282                                         && isalpha(candPair[pairwiseAlignIndex])){
283                                 //      the template pairwise and aligned are gaps while the candidate pairwise has a character
284                                 //      this would be an insertion, go ahead and add the character->seems to be the opposite of the alpha scenario
285                                 
286                                 candAln += candPair[pairwiseAlignIndex];
287                                 newTemplateAlign += tempAln[fullAlignIndex];//
288                                 pairwiseAlignIndex++;
289                                 fullAlignIndex++;                       
290                         }
291                         else if(isalpha(tempPair[pairwiseAlignIndex]) && !isalpha(tempAln[fullAlignIndex])
292                                         && !isalpha(candPair[pairwiseAlignIndex])){
293                                 //      template pairwise has a character, but its full aligned sequence and candidate sequence have gaps
294                                 //      this would happen like we need to add a gap.  basically the opposite of the alpha situation
295                                 
296                                 newTemplateAlign += tempAln[fullAlignIndex];//
297                                 candAln += "-";
298                                 fullAlignIndex++;                       
299                         }
300                         else if(!isalpha(tempPair[pairwiseAlignIndex]) && isalpha(tempAln[fullAlignIndex])
301                                         && !isalpha(candPair[pairwiseAlignIndex])){
302                                 //      template and candidate pairwise are gaps and the template aligned is not a gap this should not be possible
303                                 //      would skip the gaps and not progress through full alignment sequence
304                                 //      not tested yet
305                                 
306                                 mothurOut("We're into D " + toString(fullAlignIndex) + " " +  toString(pairwiseAlignIndex)); mothurOutEndLine();
307                                 pairwiseAlignIndex++;
308                         }
309                         else{
310                                 //      everything has a gap - not possible
311                                 //      not tested yet
312                                 
313                                 mothurOut("We're into F " +  toString(fullAlignIndex) + " " +  toString(pairwiseAlignIndex)); mothurOutEndLine();
314                                 pairwiseAlignIndex++;
315                                 fullAlignIndex++;                       
316                         }               
317                 }
318                 
319                 for(int i=fullAlignIndex;i<fullAlignLength;i++){
320                         candAln += '.';
321                         newTemplateAlign += tempAln[i];//
322                 }
323                 
324                 int start = 0;
325                 int end = candAln.length()-1;
326
327                 for(int i=0;i<candAln.length();i++){
328                         if(candAln[i] == 'Z' || !isalnum(candAln[i]))   {       candAln[i] = '.';       }       //      if we padded the alignemnt from
329                         else{                   start = i;                      break;          }                                                       //      blast with Z's, change them to
330                 }                                                                                                                                                               //      '.' characters
331                 
332                 for(int i=candAln.length()-1;i>=0;i--){                                                                                 //      ditto.
333                         if(candAln[i] == 'Z' || !isalnum(candAln[i]))   {       candAln[i] = '.';       }
334                         else{                   end = i;                        break;          }
335                 }
336                 
337                 for(int i=start;i<=end;i++){                                    //      go through the candidate alignment sequence and make sure that
338                         candAln[i] = toupper(candAln[i]);                       //      everything is upper case
339                 }
340                 
341
342                 if(candAln.length() != tempAln.length()){               //      if the regapped candidate sequence is longer than the official
343                         removeExtraGaps(candAln, tempAln, newTemplateAlign);//  template alignment then we need to do steps C-F in Fig.
344                 }                                                                                               //      2 of Desantis et al.
345
346                 candidateSeq->setAligned(candAln);
347         }
348         catch(exception& e) {
349                 errorOut(e, "Nast", "regapSequences");
350                 exit(1);
351         }       
352 }
353
354 /**************************************************************************************************/
355
356 float Nast::getSimilarityScore(){
357         try {
358         
359                 string cand = candidateSeq->getAligned();
360                 string temp = templateSeq->getAligned();
361                 int alignmentLength = temp.length();
362                 int mismatch = 0;
363                 int denominator = 0;
364                 
365                 for(int i=0;i<alignmentLength;i++){
366                         if(cand[i] == '-' && temp[i] == '-'){
367                                 
368                         }
369                         else if(cand[i] != '.' && temp[i] != '.'){
370                                 denominator++;
371                                 
372                                 if(cand[i] != temp[i]){
373                                         mismatch++;
374                                 }
375                         }
376                 }
377                 float similarity = 100 * (1. - mismatch / (float)denominator);
378                 if(denominator == 0){   similarity = 0.0000;    }
379                 
380                 return similarity;
381                 
382         }
383         catch(exception& e) {
384                 errorOut(e, "Nast", "getSimilarityScore");
385                 exit(1);
386         }       
387 }
388
389 /**************************************************************************************************/
390
391 int Nast::getMaxInsertLength(){
392         
393         return maxInsertLength;
394         
395 }
396         
397 /**************************************************************************************************/