]> git.donarmstrong.com Git - mothur.git/commitdiff
modified chop.seqs
authorwestcott <westcott>
Mon, 14 Jun 2010 18:28:12 +0000 (18:28 +0000)
committerwestcott <westcott>
Mon, 14 Jun 2010 18:28:12 +0000 (18:28 +0000)
chopseqscommand.cpp
chopseqscommand.h
removeseqscommand.cpp

index 47f151de14c5e1494485b7eebc445125f74a4d30..536aec910c9cc79b54caf34b1f60333b277e8c00 100644 (file)
@@ -21,7 +21,7 @@ ChopSeqsCommand::ChopSeqsCommand(string option)  {
                
                else {
                        //valid paramters for this command
-                       string Array[] =  {"fasta","end","fromend","outputdir","inputdir"};
+                       string Array[] =  {"fasta","numbases","keep","outputdir","inputdir"};
                        vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
                        
                        OptionParser parser(option);
@@ -57,14 +57,12 @@ ChopSeqsCommand::ChopSeqsCommand(string option)  {
                        if (fastafile == "not open") { abort = true; }
                        else if (fastafile == "not found") {  m->mothurOut("You must provide a fasta file."); m->mothurOutEndLine(); abort = true; }    
                        
-                       string temp = validParameter.validFile(parameters, "end", false);       if (temp == "not found") { temp = "0"; } 
-                       convert(temp, end);   
+                       string temp = validParameter.validFile(parameters, "numbases", false);  if (temp == "not found") { temp = "0"; } 
+                       convert(temp, numbases);   
                
-                       temp = validParameter.validFile(parameters, "fromend", false);          if (temp == "not found") { temp = "0"; } 
-                       convert(temp, fromend);   
+                       keep = validParameter.validFile(parameters, "keep", false);             if (keep == "not found") { keep = "front"; } 
                                
-                       if ((end == 0) && (fromend == 0))  { m->mothurOut("You must provide either end or fromend for the chops.seqs command."); m->mothurOutEndLine(); abort = true;  }
-                       if ((end != 0) && (fromend != 0))  { m->mothurOut("You must provide either end or fromend for the chops.seqs command, not both."); m->mothurOutEndLine(); abort = true;  }
+                       if (numbases == 0)  { m->mothurOut("You must provide the number of bases you want to keep for the chops.seqs command."); m->mothurOutEndLine(); abort = true;  }
                }
 
        }
@@ -78,11 +76,11 @@ ChopSeqsCommand::ChopSeqsCommand(string option)  {
 void ChopSeqsCommand::help(){
        try {
                m->mothurOut("The chop.seqs command reads a fasta file and outputs a .chop.fasta containing the trimmed sequences.\n");
-               m->mothurOut("The chop.seqs command parameters are fasta, end and fromend, fasta is required.\n");
-               m->mothurOut("The chop.seqs command should be in the following format: chop.seqs(fasta=yourFasta, end=yourEnd).\n");
-               m->mothurOut("The end parameter allows you to specify an end base position for your sequences, default = 0.\n");
-               m->mothurOut("The fromend parameter allows you to remove the last X bases from the end of the sequence, default = 0.\n");
-               m->mothurOut("Example chop.seqs(fasta=amazon.fasta, end=200).\n");
+               m->mothurOut("The chop.seqs command parameters are fasta, numbases, and keep. fasta and numbases are required required.\n");
+               m->mothurOut("The chop.seqs command should be in the following format: chop.seqs(fasta=yourFasta, numbases=yourNum, keep=yourKeep).\n");
+               m->mothurOut("The numbases parameter allows you to specify the number of bases you want to keep.\n");
+               m->mothurOut("The keep parameter allows you to specify whether you want to keep the front or the back of your sequence, default=front.\n");
+               m->mothurOut("Example chop.seqs(fasta=amazon.fasta, numbases=200, keep=front).\n");
                m->mothurOut("Note: No spaces between parameter labels (i.e. fasta), '=' and parameters (i.e.yourFasta).\n\n");
        }
        catch(exception& e) {
@@ -119,12 +117,7 @@ int ChopSeqsCommand::execute(){
                        if (m->control_pressed) { in.close(); out.close(); outAcc.close(); remove(outputFileName.c_str()); remove(outputFileNameAccnos.c_str()); return 0;  }
                        
                        if (seq.getName() != "") {
-                               string newSeqString = "";
-                               if (seq.getIsAligned()) { //sequence is aligned
-                                       newSeqString = getChoppedAligned(seq);
-                               }else{
-                                       newSeqString = getChoppedUnaligned(seq);
-                               }
+                               string newSeqString = getChopped(seq);
                                
                                //output trimmed sequence
                                if (newSeqString != "") {
@@ -157,62 +150,60 @@ int ChopSeqsCommand::execute(){
        }
 }
 //**********************************************************************************************************************
-string ChopSeqsCommand::getChoppedUnaligned(Sequence seq) {
-       try {
-               string temp = seq.getUnaligned();
-                               
-               //if needed trim sequence
-               if (end != 0) {
-                       if (temp.length() > end) {  temp = temp.substr(0, end);         }
-                       else { temp = "";  }
-               }else { //you are using fromend
-                       if (temp.length() > fromend) { temp = temp.substr(0, (temp.length()-fromend));  }
-                       else {  temp = ""; } //sequence too short
-               }
-
-               return temp;
-       }
-       catch(exception& e) {
-               m->errorOut(e, "ChopSeqsCommand", "getChoppedUnaligned");
-               exit(1);
-       }
-}
-//**********************************************************************************************************************
-string ChopSeqsCommand::getChoppedAligned(Sequence seq) {
+string ChopSeqsCommand::getChopped(Sequence seq) {
        try {
                string temp = seq.getAligned();
                string tempUnaligned = seq.getUnaligned();
                                
                //if needed trim sequence
-               if (end != 0) {
-                       if (tempUnaligned.length() > end) { //you have enough bases to remove some
+               if (keep == "front") {//you want to keep the beginning
+                       int tempLength = tempUnaligned.length();
+
+                       if (tempLength > numbases) { //you have enough bases to remove some
                                
                                int stopSpot = 0;
-                               int numBases = 0;
+                               int numBasesCounted = 0;
                                
                                for (int i = 0; i < temp.length(); i++) {
-                                       if(isalpha(temp[i])) { numBases++; }
-
-                                       if (numBases >= end) { stopSpot = i; break; }
+                                       //eliminate N's
+                                       if (toupper(temp[i]) == 'N') { 
+                                               temp[i] == '.'; 
+                                               tempLength--;
+                                               if (tempLength < numbases) { stopSpot = 0; break; }
+                                       }
+                                       
+                                       if(isalpha(temp[i])) { numBasesCounted++; }
+                                       
+                                       if (numBasesCounted >= numbases) { stopSpot = i; break; }
                                }
                                
-                               temp = temp.substr(0, stopSpot);                
+                               if (stopSpot == 0) { temp = ""; }
+                               else {  temp = temp.substr(0, stopSpot);  }
+                                               
                        }else { temp = ""; } //sequence too short
                        
-               }else { //you are using fromend
-               
-                       if (tempUnaligned.length() > fromend) { 
+               }else { //you are keeping the back
+                       int tempLength = tempUnaligned.length();
+                       if (tempLength > numbases) { //you have enough bases to remove some
                                
                                int stopSpot = 0;
-                               int numBases = 0;
+                               int numBasesCounted = 0;
                                
                                for (int i = (temp.length()-1); i >= 0; i--) {
-                                       if(isalpha(temp[i])) { numBases++; }
+                                       //eliminate N's
+                                       if (toupper(temp[i]) == 'N') { 
+                                               temp[i] == '.'; 
+                                               tempLength--;
+                                               if (tempLength < numbases) { stopSpot = 0; break; }
+                                       }
+                                       
+                                       if(isalpha(temp[i])) { numBasesCounted++; }
 
-                                       if (numBases >= fromend) { stopSpot = i; break; }
+                                       if (numBasesCounted >= numbases) { stopSpot = i; break; }
                                }
-                               
-                               temp = temp.substr(0, stopSpot);
+                       
+                               if (stopSpot == 0) { temp = ""; }
+                               else {  temp = temp.substr(stopSpot+1);  }
                        }
                        else {  temp = ""; } //sequence too short
                }
@@ -220,7 +211,7 @@ string ChopSeqsCommand::getChoppedAligned(Sequence seq) {
                return temp;
        }
        catch(exception& e) {
-               m->errorOut(e, "ChopSeqsCommand", "getChoppedUnaligned");
+               m->errorOut(e, "ChopSeqsCommand", "getChopped");
                exit(1);
        }
 }
index 2da134e4660202410c86f86d6c1ef1e1139bff5e..b6af15466c3f7fde3062760335c12e11025e7393 100644 (file)
@@ -24,12 +24,12 @@ class ChopSeqsCommand : public Command {
                void help();    
                
        private:
-               string fastafile, outputDir;
+               string fastafile, outputDir, keep;
                bool abort;
-               int end, fromend;
+               int numbases;
+               
+               string getChopped(Sequence);
                
-               string getChoppedAligned(Sequence);
-               string getChoppedUnaligned(Sequence);
 };
 
 #endif
index 5f5fe6535d6e159850694204efc83253387b9d4c..35d9bea3aa07255f872e5c7cbe195bd720a531ba 100644 (file)
@@ -334,7 +334,7 @@ int RemoveSeqsCommand::readName(){
                string name, firstCol, secondCol;
                
                bool wroteSomething = false;
-               
+       int numSeqsInNames = 0; 
                while(!in.eof()){
                        if (m->control_pressed) { in.close();  out.close();  remove(outputFileName.c_str());  return 0; }
 
@@ -359,7 +359,7 @@ int RemoveSeqsCommand::readName(){
                                        validSecond.push_back(parsedNames[i]);
                                }
                        }
-                       
+               numSeqsInNames += parsedNames.size();   
                        if ((dups) && (validSecond.size() != parsedNames.size())) {  //if dups is true and we want to get rid of anyone, get rid of everyone
                                for (int i = 0; i < parsedNames.size(); i++) {  names.insert(parsedNames[i]);  }
                        }else {
@@ -394,7 +394,7 @@ int RemoveSeqsCommand::readName(){
                }
                in.close();
                out.close();
-               
+cout << "your names file contains " << numSeqsInNames << endl;         
                if (wroteSomething == false) {
                        m->mothurOut("Your file contains only sequences from the .accnos file."); m->mothurOutEndLine();
                        remove(outputFileName.c_str());