]> git.donarmstrong.com Git - mothur.git/blobdiff - chopseqscommand.cpp
changed how we count sequences in a fastafile to allow for '>' in sequence names
[mothur.git] / chopseqscommand.cpp
index adf0910589c7d65161027bedae6cc74236e2ef5f..536aec910c9cc79b54caf34b1f60333b277e8c00 100644 (file)
@@ -21,7 +21,7 @@ ChopSeqsCommand::ChopSeqsCommand(string option)  {
                
                else {
                        //valid paramters for this command
-                       string Array[] =  {"fasta","end","outputdir","inputdir"};
+                       string Array[] =  {"fasta","numbases","keep","outputdir","inputdir"};
                        vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
                        
                        OptionParser parser(option);
@@ -57,13 +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") {  m->mothurOut("You must provide an end for the chops.seqs command."); m->mothurOutEndLine(); abort = true; } 
-                       else {  
-                               convert(temp, end);   
-                               if (end < 0) { m->mothurOut("End must be positive."); m->mothurOutEndLine(); abort = true;  }
-                       }
-                       
+                       string temp = validParameter.validFile(parameters, "numbases", false);  if (temp == "not found") { temp = "0"; } 
+                       convert(temp, numbases);   
+               
+                       keep = validParameter.validFile(parameters, "keep", false);             if (keep == "not found") { keep = "front"; } 
+                               
+                       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;  }
                }
 
        }
@@ -76,10 +75,12 @@ ChopSeqsCommand::ChopSeqsCommand(string option)  {
 
 void ChopSeqsCommand::help(){
        try {
-               m->mothurOut("The chop.seqs command reads a fasta file and outputs a .chop.fasta with sequences trimmed to the end position.\n");
-               m->mothurOut("The chop.seqs command parameters are fasta and end, both are required.\n");
-               m->mothurOut("The chop.seqs command should be in the following format: chop.seqs(fasta=yourFasta, end=yourEnd).\n");
-               m->mothurOut("Example chop.seqs(fasta=amazon.fasta, end=200).\n");
+               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, 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) {
@@ -96,38 +97,48 @@ int ChopSeqsCommand::execute(){
                if (abort == true) { return 0; }
                
                string outputFileName = outputDir + getRootName(getSimpleName(fastafile)) + "chop.fasta";
+               string outputFileNameAccnos = outputDir + getRootName(getSimpleName(fastafile)) + "chop.accnos";
                
                ofstream out;
                openOutputFile(outputFileName, out);
                
+               ofstream outAcc;
+               openOutputFile(outputFileNameAccnos, outAcc);
+               
                ifstream in;
                openInputFile(fastafile, in);
                
-               while (!in.eof()) {
+               bool wroteAccnos = false;
                
-                       Sequence seq(in, "no align");
+               while (!in.eof()) {
                        
-                       if (m->control_pressed) { in.close(); out.close(); remove(outputFileName.c_str()); return 0;  }
+                       Sequence seq(in);
+                       
+                       if (m->control_pressed) { in.close(); out.close(); outAcc.close(); remove(outputFileName.c_str()); remove(outputFileNameAccnos.c_str()); return 0;  }
                        
                        if (seq.getName() != "") {
-                               string temp = seq.getUnaligned();
-                               
-                               //output sequence name
-                               out << ">" << seq.getName() << endl;
+                               string newSeqString = getChopped(seq);
                                
-                               //if needed trim sequence
-                               if (temp.length() > end) {  temp = temp.substr(0, end);         }
-                               
-                               //output trimmed sequence       
-                               out << temp << endl;
+                               //output trimmed sequence
+                               if (newSeqString != "") {
+                                       out << ">" << seq.getName() << endl << newSeqString << endl;
+                               }else{
+                                       outAcc << seq.getName() << endl;
+                                       wroteAccnos = true;
+                               }
                        }
                }
                in.close();
                out.close();
+               outAcc.close();
                
                m->mothurOutEndLine();
                m->mothurOut("Output File Name: "); m->mothurOutEndLine();
                m->mothurOut(outputFileName); m->mothurOutEndLine();    
+               
+               if (wroteAccnos) { m->mothurOut(outputFileNameAccnos); m->mothurOutEndLine();  }
+               else {  remove(outputFileNameAccnos.c_str());  }
+               
                m->mothurOutEndLine();
                
                return 0;               
@@ -138,7 +149,72 @@ int ChopSeqsCommand::execute(){
                exit(1);
        }
 }
+//**********************************************************************************************************************
+string ChopSeqsCommand::getChopped(Sequence seq) {
+       try {
+               string temp = seq.getAligned();
+               string tempUnaligned = seq.getUnaligned();
+                               
+               //if needed trim sequence
+               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 numBasesCounted = 0;
+                               
+                               for (int i = 0; i < temp.length(); i++) {
+                                       //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; }
+                               }
+                               
+                               if (stopSpot == 0) { temp = ""; }
+                               else {  temp = temp.substr(0, stopSpot);  }
+                                               
+                       }else { temp = ""; } //sequence too short
+                       
+               }else { //you are keeping the back
+                       int tempLength = tempUnaligned.length();
+                       if (tempLength > numbases) { //you have enough bases to remove some
+                               
+                               int stopSpot = 0;
+                               int numBasesCounted = 0;
+                               
+                               for (int i = (temp.length()-1); i >= 0; i--) {
+                                       //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; }
+                               }
+                       
+                               if (stopSpot == 0) { temp = ""; }
+                               else {  temp = temp.substr(stopSpot+1);  }
+                       }
+                       else {  temp = ""; } //sequence too short
+               }
+
+               return temp;
+       }
+       catch(exception& e) {
+               m->errorOut(e, "ChopSeqsCommand", "getChopped");
+               exit(1);
+       }
+}
 //**********************************************************************************************************************