]> git.donarmstrong.com Git - mothur.git/blobdiff - nast.cpp
working on pam
[mothur.git] / nast.cpp
index 50d3b2440f3a2523257d20faabba51098b345cd4..647e0e4f1e17e205c183c78615a966b7a98545cc 100644 (file)
--- a/nast.cpp
+++ b/nast.cpp
@@ -25,8 +25,10 @@ Nast::Nast(Alignment* method, Sequence* cand, Sequence* temp) : alignment(method
        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) {
                m->errorOut(e, "Nast", "Nast");
@@ -38,8 +40,7 @@ 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();
@@ -77,6 +78,7 @@ 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) {
                m->errorOut(e, "Nast", "pairwiseAlignSeqs");
@@ -90,12 +92,12 @@ 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();    
        
                for(int i=0; i<longAlignmentLength; i++){                               //      use the long alignment as the standard
                        int rightIndex, rightRoom, leftIndex, leftRoom;
-                       
+       
                        //      Part C of Fig. 2 from DeSantis et al.
                        if((isalpha(newTemplateAlign[i]) != isalpha(tempAln[i]))){      //if there is a discrepancy between the regapped
                                
@@ -128,75 +130,84 @@ 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' << 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 << '\t'  << 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
-                                                       string leftTemplateString = newTemplateAlign.substr(0,i);       //      the right
-                                                       string rightTemplateString = newTemplateAlign.substr(i+insertLength);
+                                                       
+                                               }else{                                                                  //      not enough room to the left, have to steal some space to the right
+                                               
+                       //cout << "in else lr newTemplateAlign = " << newTemplateAlign.length() << '\t' << i << '\t' << insertLength << endl;
+                                                       string leftTemplateString = newTemplateAlign.substr(0,i);       
+                                                       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 = " << leftIndex << " leftroom = " << leftRoom << " rightIndex = " << rightIndex << '\t' << " rightroom = " << rightRoom << '\t' << 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
+                                       }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        
                                                        //      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;
+                                       
+                                       if ((i - insertLength) < 0) {  i = 0; }
+                                       else { i -= insertLength; }
 
                                }
                                else{
                        //      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);
+                                       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;
+                               
+                               //if i is negative, we want to remove the extra gaps to the right
+                               if (i < 0) { m->mothurOut("i is negative"); m->mothurOutEndLine(); } 
                        } 
                }
        }
@@ -210,7 +221,7 @@ void Nast::removeExtraGaps(string& candAln, string tempAln, string newTemplateAl
 
 void Nast::regapSequences(){   //This is essentially part B in Fig 2. of DeSantis et al.
        try { 
-       
+       //cout << candidateSeq->getName() << endl;
                string candPair = candidateSeq->getPairwise();
                string candAln = "";
                
@@ -239,6 +250,7 @@ void Nast::regapSequences(){        //This is essentially part B in Fig 2. of DeSantis
                string lastLoop = "";
                
                while(pairwiseAlignIndex<pairwiseLength){
+       //cout << pairwiseAlignIndex << '\t' << fullAlignIndex << '\t' << pairwiseLength << endl;
                        if(isalpha(tempPair[pairwiseAlignIndex]) && isalpha(tempAln[fullAlignIndex])
                           && isalpha(candPair[pairwiseAlignIndex])){
                                //  the template and candidate pairwise and template aligned have characters
@@ -345,6 +357,7 @@ void Nast::regapSequences(){        //This is essentially part B in Fig 2. of DeSantis
                }                                                                                               //      2 of Desantis et al.
 
                candidateSeq->setAligned(candAln);
+       //cout << "here" << endl;
        }
        catch(exception& e) {
                m->errorOut(e, "Nast", "regapSequences");