]> git.donarmstrong.com Git - mothur.git/blobdiff - nast.cpp
changes for 1.12.2
[mothur.git] / nast.cpp
index c91c9fab537172b3e02808b700af881affffc368..8ed69fa6820cca9003dd8af1a0cb148c85899753 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);
        }
 }
@@ -37,13 +38,12 @@ Nast::Nast(Alignment* method, Sequence* cand, Sequence* temp) : alignment(method
 
 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();
-       
+
                if(candAln == ""){
 
                        candidateSeq->setPairwise("");
@@ -78,7 +78,7 @@ void Nast::pairwiseAlignSeqs(){       //      Here we call one of the pairwise alignment me
                templateSeq->setPairwise(tempAln);                                      //      the candidate and template sequences
        }
        catch(exception& e) {
-               errorOut(e, "Nast", "pairwiseAlignSeqs");
+               m->errorOut(e, "Nast", "pairwiseAlignSeqs");
                exit(1);
        }       
 }
@@ -90,6 +90,11 @@ void Nast::removeExtraGaps(string& candAln, string tempAln, string newTemplateAl
 //     here we do steps C-F of Fig. 2 from DeSantis et al.
        try {
        
+               //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
@@ -101,7 +106,7 @@ 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++;             }
@@ -116,7 +121,7 @@ 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; }
@@ -127,79 +132,86 @@ void Nast::removeExtraGaps(string& candAln, string tempAln, string newTemplateAl
                                        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
-       
+                       //cout << "lr newTemplateAlign = " << newTemplateAlign.length() << '\t' << i << '\t' << i+insertLength << endl;
                                                        string leftTemplateString = newTemplateAlign.substr(0,i);
-                                                       string rightTemplateString = newTemplateAlign.substr(i+insertLength);
+                                                       string rightTemplateString = newTemplateAlign.substr((i+insertLength));
                                                        newTemplateAlign = leftTemplateString + rightTemplateString;
                                                        longAlignmentLength = newTemplateAlign.length();
-                                                       
-                                                       string leftCandidateString = candAln.substr(0,leftIndex-insertLength+1);
-                                                       string rightCandidateString = candAln.substr(leftIndex+1);
+                       //cout << "lr candAln = " << candAln.length() << '\t' << leftIndex-insertLength+1 << '\t' << leftIndex+1 << endl;                               
+                                                       string leftCandidateString = candAln.substr(0,(leftIndex-insertLength+1));
+                                                       string rightCandidateString = candAln.substr((leftIndex+1));
                                                        candAln = leftCandidateString + rightCandidateString;
                
                                                }
                                                else{                                                                   //      not enough room to the left, have to steal some space to
+                                               
+                       //cout << "in else lr newTemplateAlign = " << newTemplateAlign.length() << '\t' << i << '\t' << i+insertLength << endl;
                                                        string leftTemplateString = newTemplateAlign.substr(0,i);       //      the right
-                                                       string rightTemplateString = newTemplateAlign.substr(i+insertLength);
+                                                       string rightTemplateString = newTemplateAlign.substr((i+insertLength));
                                                        newTemplateAlign = leftTemplateString + rightTemplateString;
                                                        longAlignmentLength = newTemplateAlign.length();
-                                                       
-                                                       string leftCandidateString = candAln.substr(0,leftIndex-leftRoom+1);
-                                                       string insertString = candAln.substr(leftIndex+1,rightIndex-leftIndex-1);
-                                                       string rightCandidateString = candAln.substr(rightIndex+(insertLength-leftRoom));
+                       //cout << " in else lr candAln = " << candAln.length() << '\t' << leftIndex-leftRoom+1 << '\t' << rightIndex-leftIndex-1 << '\t' << rightIndex+(insertLength-leftRoom) << endl;                                 
+                                                       string leftCandidateString = candAln.substr(0,(leftIndex-leftRoom+1));
+                                                       string insertString = candAln.substr((leftIndex+1),(rightIndex-leftIndex-1));
+                                                       string rightCandidateString = candAln.substr((rightIndex+(insertLength-leftRoom)));
                                                        candAln = leftCandidateString + insertString + rightCandidateString;
                                
                                                }
                                        }
                                        else{                                                                           //      the right gap is closer - > move stuff right there's
                                                if(rightRoom >= insertLength){                  //      enough room to the right to move
-                       
+                       //cout << "rr newTemplateAlign = " << newTemplateAlign.length() << '\t' << i << '\t' << i+insertLength << endl;
                                                        string leftTemplateString = newTemplateAlign.substr(0,i);
-                                                       string rightTemplateString = newTemplateAlign.substr(i+insertLength);
+                                                       string rightTemplateString = newTemplateAlign.substr((i+insertLength));
                                                        newTemplateAlign = leftTemplateString + rightTemplateString;
                                                        longAlignmentLength = newTemplateAlign.length();
-                                                       
+                       //cout << "rr candAln = " << candAln.length() << '\t' << i << '\t' << rightIndex << '\t' << rightIndex+insertLength << endl;                            
                                                        string leftCandidateString = candAln.substr(0,rightIndex);
-                                                       string rightCandidateString = candAln.substr(rightIndex+insertLength);
+                                                       string rightCandidateString = candAln.substr((rightIndex+insertLength));
                                                        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...
+                                       //cout << "in else rr newTemplateAlign = " << newTemplateAlign.length() << '\t' << i << '\t' << i+insertLength << endl;
                                                        string leftTemplateString = newTemplateAlign.substr(0,i);
-                                                       string rightTemplateString = newTemplateAlign.substr(i+insertLength);
+                                                       string rightTemplateString = newTemplateAlign.substr((i+insertLength));
                                                        newTemplateAlign = leftTemplateString + rightTemplateString;
                                                        longAlignmentLength = newTemplateAlign.length();
-                                                                       
-                                                       string leftCandidateString = candAln.substr(0,leftIndex-(insertLength-rightRoom)+1);
-                                                       string insertString = candAln.substr(leftIndex+1,rightIndex-leftIndex-1);
-                                                       string rightCandidateString = candAln.substr(rightIndex+rightRoom);
+                                       //cout << "in else rr candAln = " << candAln.length() << '\t' << '\t' << (leftIndex-(insertLength-rightRoom)+1) << '\t' <<  (leftIndex+1,rightIndex-leftIndex-1) << '\t' << (rightIndex+rightRoom) << endl;                             
+                                                       string leftCandidateString = candAln.substr(0,(leftIndex-(insertLength-rightRoom)+1));
+                                                       string insertString = candAln.substr((leftIndex+1),(rightIndex-leftIndex-1));
+                                                       string rightCandidateString = candAln.substr((rightIndex+rightRoom));
                                                        candAln = leftCandidateString + insertString + rightCandidateString;    
                                                                        
                                                }
                                        }
+                                       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
-                                       string rightTemplateString = newTemplateAlign.substr(i+leftRoom+rightRoom);
+                       //      there could be a case where there isn't enough room in either direction to move stuff
+//cout << "in else else newTemplateAlign = " << newTemplateAlign.length() << '\t' << i << '\t' << (i+leftRoom+rightRoom) << endl;
+                                       string leftTemplateString = newTemplateAlign.substr(0,i);       
+                                       string rightTemplateString = newTemplateAlign.substr((i+leftRoom+rightRoom));
                                        newTemplateAlign = leftTemplateString + rightTemplateString;
                                        longAlignmentLength = newTemplateAlign.length();
                                                        
-                                       string leftCandidateString = candAln.substr(0,leftIndex-leftRoom+1);
-                                       string insertString = candAln.substr(leftIndex+1,rightIndex-leftIndex-1);
-                                       string rightCandidateString = candAln.substr(rightIndex+rightRoom);
+               //cout << "in else else newTemplateAlign = " << candAln.length() << '\t' << (leftIndex-leftRoom+1) << '\t' << (leftIndex+1) << '\t' << (rightIndex-leftIndex-1) << '\t' << (rightIndex+rightRoom) << endl;      
+                                       string leftCandidateString = candAln.substr(0,(leftIndex-leftRoom+1));
+                                       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);
        }       
 }
@@ -302,14 +314,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++;                       
                        }               
@@ -345,7 +357,7 @@ void Nast::regapSequences(){        //This is essentially part B in Fig 2. of DeSantis
                candidateSeq->setAligned(candAln);
        }
        catch(exception& e) {
-               errorOut(e, "Nast", "regapSequences");
+               m->errorOut(e, "Nast", "regapSequences");
                exit(1);
        }       
 }
@@ -380,7 +392,7 @@ float Nast::getSimilarityScore(){
                
        }
        catch(exception& e) {
-               errorOut(e, "Nast", "getSimilarityScore");
+               m->errorOut(e, "Nast", "getSimilarityScore");
                exit(1);
        }       
 }