]> git.donarmstrong.com Git - mothur.git/blobdiff - sequence.cpp
fixes while testing 1.33.0
[mothur.git] / sequence.cpp
index 224ecb10cb1529db7d6bdb10c268be047dae5834..efeb88ce65183135c2a0aaef36d8d3a1c8f432cc 100644 (file)
@@ -21,9 +21,7 @@ Sequence::Sequence(string newName, string sequence) {
                initialize();   
                name = newName;
         
-        for (int i = 0; i < name.length(); i++) {
-            if (name[i] == ':') { name[i] = '_'; m->changedSeqNames = true; }
-        }
+        m->checkName(name);
                
                //setUnaligned removes any gap characters for us
                setUnaligned(sequence);
@@ -41,9 +39,7 @@ Sequence::Sequence(string newName, string sequence, string justUnAligned) {
                initialize();   
                name = newName;
         
-        for (int i = 0; i < name.length(); i++) {
-            if (name[i] == ':') { name[i] = '_'; m->changedSeqNames = true; }
-        }
+        m->checkName(name);
                
                //setUnaligned removes any gap characters for us
                setUnaligned(sequence);
@@ -80,7 +76,8 @@ Sequence::Sequence(istringstream& fastaString){
                                }
                        }
                        
-                       while (!fastaString.eof())      {       char c = fastaString.get();  if (c == 10 || c == 13){ break;    }       } // get rest of line if there's any crap there
+                       //while (!fastaString.eof())    {       char c = fastaString.get();  if (c == 10 || c == 13){ break;    }       } // get rest of line if there's any crap there
+            comment = getCommentString(fastaString);
                        
                        int numAmbig = 0;
                        sequence = getSequenceString(fastaString, numAmbig);
@@ -124,7 +121,8 @@ Sequence::Sequence(istringstream& fastaString, string JustUnaligned){
                                }
                        }
                        
-                       while (!fastaString.eof())      {       char c = fastaString.get();  if (c == 10 || c == 13){ break;    }       } // get rest of line if there's any crap there
+                       //while (!fastaString.eof())    {       char c = fastaString.get();  if (c == 10 || c == 13){ break;    }       } // get rest of line if there's any crap there
+            comment = getCommentString(fastaString);
                        
                        int numAmbig = 0;
                        sequence = getSequenceString(fastaString, numAmbig);
@@ -170,8 +168,8 @@ Sequence::Sequence(ifstream& fastaFile){
                                }
                        }
                        
-                       //read real sequence
-                       while (!fastaFile.eof())        {       char c = fastaFile.get(); if (c == 10 || c == 13){  break;      }       } // get rest of line if there's any crap there
+                       //while (!fastaFile.eof())      {       char c = fastaFile.get(); if (c == 10 || c == 13){  break;      }       } // get rest of line if there's any crap there
+            comment = getCommentString(fastaFile);
                        
                        int numAmbig = 0;
                        sequence = getSequenceString(fastaFile, numAmbig);
@@ -220,9 +218,11 @@ Sequence::Sequence(ifstream& fastaFile, string& extraInfo, bool getInfo){
                        //read info after sequence name
                        while (!fastaFile.eof())        {       
                 char c = fastaFile.get(); 
-                if (c == 10 || c == 13){  break;       }       
+                if (c == 10 || c == 13 || c == -1){   break;   }
                 extraInfo += c;
-            } 
+            }
+            
+            comment = extraInfo;
                        
                        int numAmbig = 0;
                        sequence = getSequenceString(fastaFile, numAmbig);
@@ -265,8 +265,8 @@ Sequence::Sequence(ifstream& fastaFile, string JustUnaligned){
                                }
                        }
                        
-                       //read real sequence
-                       while (!fastaFile.eof())        {       char c = fastaFile.get(); if (c == 10 || c == 13){       break; }       } // get rest of line if there's any crap there
+                       //while (!fastaFile.eof())      {       char c = fastaFile.get(); if (c == 10 || c == 13){       break; }       } // get rest of line if there's any crap there
+            comment = getCommentString(fastaFile);
                        
                        int numAmbig = 0;
                        sequence = getSequenceString(fastaFile, numAmbig);
@@ -295,9 +295,7 @@ string Sequence::getSequenceName(ifstream& fastaFile) {
             
                        name = name.substr(1); 
             
-            for (int i = 0; i < name.length(); i++) {
-                if (name[i] == ':') { name[i] = '_'; m->changedSeqNames = true; }
-            }
+            m->checkName(name);
             
         }else{ m->mothurOut("Error in reading your fastafile, at position " + toString(fastaFile.tellg()) + ". Blank name."); m->mothurOutEndLine(); m->control_pressed = true;  }
         
@@ -319,9 +317,7 @@ string Sequence::getSequenceName(istringstream& fastaFile) {
             
                        name = name.substr(1); 
             
-            for (int i = 0; i < name.length(); i++) {
-                if (name[i] == ':') { name[i] = '_'; m->changedSeqNames = true; }
-            }
+            m->checkName(name);
             
         }else{ m->mothurOut("Error in reading your fastafile, at position " + toString(fastaFile.tellg()) + ". Blank name."); m->mothurOutEndLine(); m->control_pressed = true;  }
         
@@ -368,17 +364,19 @@ string Sequence::getSequenceString(ifstream& fastaFile, int& numAmbig) {
 string Sequence::getCommentString(ifstream& fastaFile) {
        try {
                char letter;
-               string sequence = "";
+               string temp = "";
                
                while(fastaFile){
                        letter=fastaFile.get();
-                       if((letter == '\r') || (letter == '\n')){  
+                       if((letter == '\r') || (letter == '\n') || letter == -1){
                                m->gobble(fastaFile);  //in case its a \r\n situation
                                break;
-                       }
+                       }else {
+                temp += letter;
+            }
                }
                
-               return sequence;
+               return temp;
        }
        catch(exception& e) {
                m->errorOut(e, "Sequence", "getCommentString");
@@ -422,17 +420,19 @@ string Sequence::getSequenceString(istringstream& fastaFile, int& numAmbig) {
 string Sequence::getCommentString(istringstream& fastaFile) {
        try {
                char letter;
-               string sequence = "";
+               string temp = "";
                
                while(fastaFile){
                        letter=fastaFile.get();
-                       if((letter == '\r') || (letter == '\n')){  
+                       if((letter == '\r') || (letter == '\n') || letter == -1){  
                                m->gobble(fastaFile);  //in case its a \r\n situation
                                break;
-                       }
+                       }else {
+                temp += letter;
+            }
                }
                
-               return sequence;
+               return temp;
        }
        catch(exception& e) {
                m->errorOut(e, "Sequence", "getCommentString");
@@ -447,6 +447,7 @@ void Sequence::initialize(){
        unaligned = "";
        aligned = "";
        pairwise = "";
+    comment = "";
        
        numBases = 0;
        alignmentLength = 0;
@@ -589,7 +590,7 @@ int Sequence::getNumNs(){
 
 void Sequence::printSequence(ostream& out){
 
-       out << ">" << name << endl;
+       out << ">" << name << comment << endl;
        if(isAligned){
                out << aligned << endl;
        }
@@ -683,12 +684,12 @@ int Sequence::filterToPos(int start){
     
     if (start > aligned.length()) { start = aligned.length(); m->mothurOut("[ERROR]: start to large.\n"); }
     
-       for(int j = 0; j < start-1; j++) {
+       for(int j = 0; j < start; j++) {
                aligned[j] = '.';
        }
        
     //things like ......----------AT become ................AT
-    for(int j = start-1; j < aligned.length(); j++) {
+    for(int j = start; j < aligned.length(); j++) {
         if (isalpha(aligned[j])) { break; }
         else { aligned[j] = '.'; }
     }
@@ -772,6 +773,8 @@ void Sequence::trim(int length){
        if(numBases > length){
                unaligned = unaligned.substr(0,length);
                numBases = length;
+        aligned = "";
+        isAligned = 0;
        }
        
 }