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