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