]> git.donarmstrong.com Git - mothur.git/blobdiff - nast.cpp
fixed problem with chimera.slayer realigner
[mothur.git] / nast.cpp
index 4aed5903aec4985f92d5668d39301cabe3d779e6..9e26b11a7708590389f59b1c82e76ebca72f1bc1 100644 (file)
--- a/nast.cpp
+++ b/nast.cpp
 
 Nast::Nast(Alignment* method, Sequence* cand, Sequence* temp) : alignment(method), candidateSeq(cand), templateSeq(temp) {
        try {
+               m = MothurOut::getInstance();
                maxInsertLength = 0;
                pairwiseAlignSeqs();    //      This is part A in Fig. 2 of DeSantis et al.
                regapSequences();               //      This is parts B-F in Fig. 2 of DeSantis et al.
-
        }
        catch(exception& e) {
-               errorOut(e, "Nast", "Nast");
+               m->errorOut(e, "Nast", "Nast");
                exit(1);
        }
-
 }
 
 /**************************************************************************************************/
 
 void Nast::pairwiseAlignSeqs(){        //      Here we call one of the pairwise alignment methods to align our unaligned candidate
                                                                //      and template sequences
-       try {
-               
+       try {   
                alignment->align(candidateSeq->getUnaligned(), templateSeq->getUnaligned());
-       
+
                string candAln = alignment->getSeqAAln();
                string tempAln = alignment->getSeqBAln();
 
@@ -53,7 +51,6 @@ void Nast::pairwiseAlignSeqs(){       //      Here we call one of the pairwise alignment me
 
                }
                else{
-
                        if(tempAln[0] == '-'){
                                int pairwiseAlignmentLength = tempAln.length(); //      we need to make sure that the candidate sequence alignment
                                for(int i=0;i<pairwiseAlignmentLength;i++){             //      starts where the template sequence alignment starts, if it
@@ -64,7 +61,6 @@ void Nast::pairwiseAlignSeqs(){       //      Here we call one of the pairwise alignment me
                                        }
                                }
                        }
-                       
                        int pairwiseAlignmentLength = tempAln.length();
                        if(tempAln[pairwiseAlignmentLength-1] == '-'){          //      we need to make sure that the candidate sequence alignment
                                for(int i=pairwiseAlignmentLength-1; i>=0; i--){//      ends where the template sequence alignment ends, if it runs
@@ -80,10 +76,9 @@ void Nast::pairwiseAlignSeqs(){      //      Here we call one of the pairwise alignment me
 
                candidateSeq->setPairwise(candAln);                                     //      set the pairwise sequences in the Sequence objects for
                templateSeq->setPairwise(tempAln);                                      //      the candidate and template sequences
-
        }
        catch(exception& e) {
-               errorOut(e, "Nast", "pairwiseAlignSeqs");
+               m->errorOut(e, "Nast", "pairwiseAlignSeqs");
                exit(1);
        }       
 }
@@ -95,8 +90,13 @@ void Nast::removeExtraGaps(string& candAln, string tempAln, string newTemplateAl
 //     here we do steps C-F of Fig. 2 from DeSantis et al.
        try {
        
-               int longAlignmentLength = newTemplateAlign.length();    
+               //cout << candAln << endl;
+               //cout << tempAln << endl;
+               //cout << newTemplateAlign << endl;
+               //cout << endl;
                
+               int longAlignmentLength = newTemplateAlign.length();    
+       
                for(int i=0; i<longAlignmentLength; i++){                               //      use the long alignment as the standard
                        int rightIndex, rightRoom, leftIndex, leftRoom;
                        
@@ -106,15 +106,14 @@ void Nast::removeExtraGaps(string& candAln, string tempAln, string newTemplateAl
                                rightRoom = 0; leftRoom = 0;
                                
                                //      Part D of Fig. 2 from DeSantis et al.           //      template sequence and the official template sequence
-                               for(leftIndex=i-1;leftIndex>0;leftIndex--){     //      then we've got problems...
+                               for(leftIndex=i-1;leftIndex>0;leftIndex--){             //      then we've got problems...
                                        if(!isalpha(candAln[leftIndex])){
                                                leftRoom = 1;   //count how far it is to the nearest gap on the LEFT side of the anomaly
                                                while(leftIndex-leftRoom>=0 && !isalpha(candAln[leftIndex-leftRoom]))   {       leftRoom++;             }
                                                break;
                                        }
                                }
-                               
-                               
+
                                for(rightIndex=i+1;rightIndex<longAlignmentLength-1;rightIndex++){
                                        if(!isalpha(candAln[rightIndex])){
                                                rightRoom = 1;  //count how far it is to the nearest gap on the RIGHT side of the anomaly
@@ -122,18 +121,17 @@ void Nast::removeExtraGaps(string& candAln, string tempAln, string newTemplateAl
                                                break;
                                        }
                                }
-                                       
+                                                               
                                int insertLength = 0;                                                   //      figure out how long the anomaly is
                                while(!isalpha(newTemplateAlign[i + insertLength]))     {       insertLength++; }
                                if(insertLength > maxInsertLength){     maxInsertLength = insertLength; }
-                                       
+               
                                if((leftRoom + rightRoom) >= insertLength){
        
                                        //      Parts D & E from Fig. 2 of DeSantis et al.
                                        if((i-leftIndex) <= (rightIndex-i)){            //      the left gap is closer - > move stuff left there's
        
                                                if(leftRoom >= insertLength){                   //      enough room to the left to move
-       
                                                        string leftTemplateString = newTemplateAlign.substr(0,i);
                                                        string rightTemplateString = newTemplateAlign.substr(i+insertLength);
                                                        newTemplateAlign = leftTemplateString + rightTemplateString;
@@ -159,7 +157,6 @@ void Nast::removeExtraGaps(string& candAln, string tempAln, string newTemplateAl
                                        }
                                        else{                                                                           //      the right gap is closer - > move stuff right there's
                                                if(rightRoom >= insertLength){                  //      enough room to the right to move
-                       
                                                        string leftTemplateString = newTemplateAlign.substr(0,i);
                                                        string rightTemplateString = newTemplateAlign.substr(i+insertLength);
                                                        newTemplateAlign = leftTemplateString + rightTemplateString;
@@ -170,7 +167,7 @@ void Nast::removeExtraGaps(string& candAln, string tempAln, string newTemplateAl
                                                        candAln = leftCandidateString + rightCandidateString;   
                                                                        
                                                }
-                                               else{                                                                   //      not enough room to the right, have to steal some 
+                                               else{                                                                   //      not enough room to the right, have to steal some        
                                                        //      space to the left lets move left and then right...
                                                        string leftTemplateString = newTemplateAlign.substr(0,i);
                                                        string rightTemplateString = newTemplateAlign.substr(i+insertLength);
@@ -184,10 +181,13 @@ void Nast::removeExtraGaps(string& candAln, string tempAln, string newTemplateAl
                                                                        
                                                }
                                        }
+                                       i -= insertLength;
+
                                }
                                else{
-                                                                                                       //      there could be a case where there isn't enough room in
-                                       string leftTemplateString = newTemplateAlign.substr(0,i);                       //      either direction to move stuff
+                       //      there could be a case where there isn't enough room in either direction to move stuff
+
+                                       string leftTemplateString = newTemplateAlign.substr(0,i);       
                                        string rightTemplateString = newTemplateAlign.substr(i+leftRoom+rightRoom);
                                        newTemplateAlign = leftTemplateString + rightTemplateString;
                                        longAlignmentLength = newTemplateAlign.length();
@@ -196,18 +196,18 @@ void Nast::removeExtraGaps(string& candAln, string tempAln, string newTemplateAl
                                        string insertString = candAln.substr(leftIndex+1,rightIndex-leftIndex-1);
                                        string rightCandidateString = candAln.substr(rightIndex+rightRoom);
                                        candAln = leftCandidateString + insertString + rightCandidateString;
-       
+                                       
+                                       i -= (leftRoom + rightRoom);
                                }
-                               
-                               i -= insertLength;
+                       
+//                             i -= insertLength;
                        } 
                }
        }
        catch(exception& e) {
-               errorOut(e, "Nast", "removeExtraGaps");
+               m->errorOut(e, "Nast", "removeExtraGaps");
                exit(1);
        }       
-       
 }
 
 /**************************************************************************************************/
@@ -229,7 +229,7 @@ void Nast::regapSequences(){        //This is essentially part B in Fig 2. of DeSantis
                        candidateSeq->setAligned(candAln);
                        return;
                }
-               
+       
                int fullAlignIndex = 0;
                int pairwiseAlignIndex = 0;
                string newTemplateAlign = "";                                   //      this is going to be messy so we want a temporary template
@@ -239,7 +239,7 @@ void Nast::regapSequences(){        //This is essentially part B in Fig 2. of DeSantis
                        newTemplateAlign += tempAln[fullAlignIndex];//  pairwise sequences
                        fullAlignIndex++;
                }
-               
+
                string lastLoop = "";
                
                while(pairwiseAlignIndex<pairwiseLength){
@@ -308,14 +308,14 @@ void Nast::regapSequences(){      //This is essentially part B in Fig 2. of DeSantis
                                //      would skip the gaps and not progress through full alignment sequence
                                //      not tested yet
                                
-                               mothurOut("We're into D " + toString(fullAlignIndex) + " " +  toString(pairwiseAlignIndex)); mothurOutEndLine();
+                               m->mothurOut("We're into D " + toString(fullAlignIndex) + " " +  toString(pairwiseAlignIndex)); m->mothurOutEndLine();
                                pairwiseAlignIndex++;
                        }
                        else{
                                //      everything has a gap - not possible
                                //      not tested yet
                                
-                               mothurOut("We're into F " +  toString(fullAlignIndex) + " " +  toString(pairwiseAlignIndex)); mothurOutEndLine();
+                               m->mothurOut("We're into F " +  toString(fullAlignIndex) + " " +  toString(pairwiseAlignIndex)); m->mothurOutEndLine();
                                pairwiseAlignIndex++;
                                fullAlignIndex++;                       
                        }               
@@ -326,7 +326,9 @@ void Nast::regapSequences(){        //This is essentially part B in Fig 2. of DeSantis
                        newTemplateAlign += tempAln[i];//
                }
                
-               int start, end;
+               int start = 0;
+               int end = candAln.length()-1;
+
                for(int i=0;i<candAln.length();i++){
                        if(candAln[i] == 'Z' || !isalnum(candAln[i]))   {       candAln[i] = '.';       }       //      if we padded the alignemnt from
                        else{                   start = i;                      break;          }                                                       //      blast with Z's, change them to
@@ -341,16 +343,15 @@ void Nast::regapSequences(){      //This is essentially part B in Fig 2. of DeSantis
                        candAln[i] = toupper(candAln[i]);                       //      everything is upper case
                }
                
-               
+
                if(candAln.length() != tempAln.length()){               //      if the regapped candidate sequence is longer than the official
                        removeExtraGaps(candAln, tempAln, newTemplateAlign);//  template alignment then we need to do steps C-F in Fig.
                }                                                                                               //      2 of Desantis et al.
-               
-               
+
                candidateSeq->setAligned(candAln);
        }
        catch(exception& e) {
-               errorOut(e, "Nast", "regapSequences");
+               m->errorOut(e, "Nast", "regapSequences");
                exit(1);
        }       
 }
@@ -385,7 +386,7 @@ float Nast::getSimilarityScore(){
                
        }
        catch(exception& e) {
-               errorOut(e, "Nast", "getSimilarityScore");
+               m->errorOut(e, "Nast", "getSimilarityScore");
                exit(1);
        }       
 }