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