]> git.donarmstrong.com Git - mothur.git/blobdiff - chopseqscommand.cpp
removed unused copy constructors and comments within comments that where causing...
[mothur.git] / chopseqscommand.cpp
index b2810741ae7b2b6591a7f0aa8e3b52b143dcfe3d..4e06201cd352104b5ee9c9d1c41764d16473003e 100644 (file)
 #include "sequence.hpp"
 
 //**********************************************************************************************************************
+vector<string> ChopSeqsCommand::setParameters(){       
+       try {
+               CommandParameter pfasta("fasta", "InputTypes", "", "", "none", "none", "none",false,true); parameters.push_back(pfasta);
+               CommandParameter pnumbases("numbases", "Number", "", "0", "", "", "",false,true); parameters.push_back(pnumbases);
+               CommandParameter pcountgaps("countgaps", "Boolean", "", "F", "", "", "",false,false); parameters.push_back(pcountgaps);
+               CommandParameter pshort("short", "Boolean", "", "F", "", "", "",false,false); parameters.push_back(pshort);
+               CommandParameter pkeep("keep", "Multiple", "front-back", "front", "", "", "",false,false); parameters.push_back(pkeep);
+               CommandParameter pinputdir("inputdir", "String", "", "", "", "", "",false,false); parameters.push_back(pinputdir);
+               CommandParameter poutputdir("outputdir", "String", "", "", "", "", "",false,false); parameters.push_back(poutputdir);
+               
+               vector<string> myArray;
+               for (int i = 0; i < parameters.size(); i++) {   myArray.push_back(parameters[i].name);          }
+               return myArray;
+       }
+       catch(exception& e) {
+               m->errorOut(e, "ChopSeqsCommand", "setParameters");
+               exit(1);
+       }
+}
+//**********************************************************************************************************************
+string ChopSeqsCommand::getHelpString(){       
+       try {
+               string helpString = "";
+               helpString += "The chop.seqs command reads a fasta file and outputs a .chop.fasta containing the trimmed sequences. Note: If a sequence is completely 'chopped', an accnos file will be created with the names of the sequences removed. \n";
+               helpString += "The chop.seqs command parameters are fasta, numbases, countgaps and keep. fasta is required unless you have a valid current fasta file. numbases is required.\n";
+               helpString += "The chop.seqs command should be in the following format: chop.seqs(fasta=yourFasta, numbases=yourNum, keep=yourKeep).\n";
+               helpString += "The numbases parameter allows you to specify the number of bases you want to keep.\n";
+               helpString += "The keep parameter allows you to specify whether you want to keep the front or the back of your sequence, default=front.\n";
+               helpString += "The countgaps parameter allows you to specify whether you want to count gaps as bases, default=false.\n";
+               helpString += "The short parameter allows you to specify you want to keep sequences that are too short to chop, default=false.\n";
+               helpString += "For example, if you ran chop.seqs with numbases=200 and short=t, if a sequence had 100 bases mothur would keep the sequence rather than eliminate it.\n";
+               helpString += "Example chop.seqs(fasta=amazon.fasta, numbases=200, keep=front).\n";
+               helpString += "Note: No spaces between parameter labels (i.e. fasta), '=' and parameters (i.e.yourFasta).\n";
+               return helpString;
+       }
+       catch(exception& e) {
+               m->errorOut(e, "ChopSeqsCommand", "getHelpString");
+               exit(1);
+       }
+}
 
+//**********************************************************************************************************************
+ChopSeqsCommand::ChopSeqsCommand(){    
+       try {
+               abort = true; calledHelp = true; 
+               setParameters();
+               vector<string> tempOutNames;
+               outputTypes["fasta"] = tempOutNames;
+               outputTypes["accnos"] = tempOutNames;
+       }
+       catch(exception& e) {
+               m->errorOut(e, "ChopSeqsCommand", "ChopSeqsCommand");
+               exit(1);
+       }
+}
+//**********************************************************************************************************************
 ChopSeqsCommand::ChopSeqsCommand(string option)  {
        try {
-               abort = false;
+               abort = false; calledHelp = false;   
                
                //allow user to run help
-               if(option == "help") { help(); abort = true; }
+               if(option == "help") { help(); abort = true; calledHelp = true; }
+               else if(option == "citation") { citation(); abort = true; calledHelp = true;}
                
                else {
-                       //valid paramters for this command
-                       string Array[] =  {"fasta","numbases","countgaps","keep","outputdir","inputdir"};
-                       vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
+                       vector<string> myArray = setParameters();
                        
                        OptionParser parser(option);
                        map<string,string> parameters = parser.getParameters();
@@ -35,6 +89,11 @@ ChopSeqsCommand::ChopSeqsCommand(string option)  {
                                if (validParameter.isValidParameter(it->first, myArray, it->second) != true) {  abort = true;  }
                        }
                        
+                       //initialize outputTypes
+                       vector<string> tempOutNames;
+                       outputTypes["fasta"] = tempOutNames;
+                       outputTypes["accnos"] = tempOutNames;
+               
                        //if the user changes the input directory command factory will send this info to us in the output parameter 
                        string inputDir = validParameter.validFile(parameters, "inputdir", false);              
                        if (inputDir == "not found"){   inputDir = "";          }
@@ -52,16 +111,23 @@ ChopSeqsCommand::ChopSeqsCommand(string option)  {
                        //check for required parameters
                        fastafile = validParameter.validFile(parameters, "fasta", true);
                        if (fastafile == "not open") { abort = true; }
-                       else if (fastafile == "not found") {  m->mothurOut("You must provide a fasta file."); m->mothurOutEndLine(); abort = true; }    
+                       else if (fastafile == "not found") {                            //if there is a current fasta file, use it
+                               fastafile = m->getFastaFile(); 
+                               if (fastafile != "") { m->mothurOut("Using " + fastafile + " as input file for the fasta parameter."); m->mothurOutEndLine(); }
+                               else {  m->mothurOut("You have no current fastafile and the fasta parameter is required."); m->mothurOutEndLine(); abort = true; }
+                       }else { m->setFastaFile(fastafile); }   
                        
                        //if the user changes the output directory command factory will send this info to us in the output parameter 
                        outputDir = validParameter.validFile(parameters, "outputdir", false);           if (outputDir == "not found"){  outputDir = m->hasPath(fastafile);      }
                        
                        string temp = validParameter.validFile(parameters, "numbases", false);  if (temp == "not found") { temp = "0"; } 
-                       convert(temp, numbases);   
+                       m->mothurConvert(temp, numbases);   
                        
                        temp = validParameter.validFile(parameters, "countgaps", false);        if (temp == "not found") { temp = "f"; } 
-                       countGaps = m->isTrue(temp);   
+                       countGaps = m->isTrue(temp);  
+                       
+                       temp = validParameter.validFile(parameters, "short", false);    if (temp == "not found") { temp = "f"; } 
+                       Short = m->isTrue(temp);   
                
                        keep = validParameter.validFile(parameters, "keep", false);             if (keep == "not found") { keep = "front"; } 
                                
@@ -76,29 +142,10 @@ 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, numbases, countgaps 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("The countgaps parameter allows you to specify whether you want to count gaps as bases, default=false.\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) {
-               m->errorOut(e, "ChopSeqsCommand", "help");
-               exit(1);
-       }
-}
-
-//**********************************************************************************************************************
-
 int ChopSeqsCommand::execute(){
        try {
                
-               if (abort == true) { return 0; }
+               if (abort == true) { if (calledHelp) { return 0; }  return 2;   }
                
                string outputFileName = outputDir + m->getRootName(m->getSimpleName(fastafile)) + "chop.fasta";
                string outputFileNameAccnos = outputDir + m->getRootName(m->getSimpleName(fastafile)) + "chop.accnos";
@@ -118,7 +165,7 @@ int ChopSeqsCommand::execute(){
                        
                        Sequence seq(in);
                        
-                       if (m->control_pressed) { in.close(); out.close(); outAcc.close(); remove(outputFileName.c_str()); remove(outputFileNameAccnos.c_str()); return 0;  }
+                       if (m->control_pressed) { outputTypes.clear(); in.close(); out.close(); outAcc.close(); m->mothurRemove(outputFileName); m->mothurRemove(outputFileNameAccnos); return 0;  }
                        
                        if (seq.getName() != "") {
                                string newSeqString = getChopped(seq);
@@ -138,13 +185,28 @@ int ChopSeqsCommand::execute(){
                
                m->mothurOutEndLine();
                m->mothurOut("Output File Name: "); m->mothurOutEndLine();
-               m->mothurOut(outputFileName); m->mothurOutEndLine();    
+               m->mothurOut(outputFileName); m->mothurOutEndLine();    outputNames.push_back(outputFileName); outputTypes["fasta"].push_back(outputFileName);
                
-               if (wroteAccnos) { m->mothurOut(outputFileNameAccnos); m->mothurOutEndLine();  }
-               else {  remove(outputFileNameAccnos.c_str());  }
+               if (wroteAccnos) { m->mothurOut(outputFileNameAccnos); m->mothurOutEndLine(); outputNames.push_back(outputFileNameAccnos); outputTypes["accnos"].push_back(outputFileNameAccnos); }
+               else {  m->mothurRemove(outputFileNameAccnos);  }
                
                m->mothurOutEndLine();
                
+               //set fasta file as new current fastafile
+               string current = "";
+               itTypes = outputTypes.find("fasta");
+               if (itTypes != outputTypes.end()) {
+                       if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setFastaFile(current); }
+               }
+               
+               if (wroteAccnos) { //set accnos file as new current accnosfile
+                       itTypes = outputTypes.find("accnos");
+                       if (itTypes != outputTypes.end()) {
+                               if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setAccnosFile(current); }
+                       }
+               }
+               
+               
                return 0;               
        }
 
@@ -171,7 +233,7 @@ string ChopSeqsCommand::getChopped(Sequence seq) {
                                        
                                        for (int i = 0; i < temp.length(); i++) {
                                                //eliminate N's
-                                               if (toupper(temp[i]) == 'N') { temp[i] == '.'; }
+                                               if (toupper(temp[i]) == 'N') { temp[i] = '.'; }
                                                
                                                numBasesCounted++; 
                                                
@@ -179,10 +241,11 @@ string ChopSeqsCommand::getChopped(Sequence seq) {
                                        }
                                        
                                        if (stopSpot == 0) { temp = ""; }
-                                       else {  temp = temp.substr(0, stopSpot);  }
+                                       else {  temp = temp.substr(0, stopSpot+1);  }
                                                        
-                               }else { temp = ""; } //sequence too short
-                               
+                               }else { 
+                                       if (!Short) { temp = ""; } //sequence too short
+                               }
                        }else { //you are keeping the back
                                int tempLength = temp.length();
                                if (tempLength > numbases) { //you have enough bases to remove some
@@ -192,7 +255,7 @@ string ChopSeqsCommand::getChopped(Sequence seq) {
                                        
                                        for (int i = (temp.length()-1); i >= 0; i--) {
                                                //eliminate N's
-                                               if (toupper(temp[i]) == 'N') { temp[i] == '.'; }
+                                               if (toupper(temp[i]) == 'N') { temp[i] = '.'; }
                                                
                                                numBasesCounted++; 
 
@@ -201,8 +264,9 @@ string ChopSeqsCommand::getChopped(Sequence seq) {
                                
                                        if (stopSpot == 0) { temp = ""; }
                                        else {  temp = temp.substr(stopSpot+1);  }
+                               }else { 
+                                       if (!Short) { temp = ""; } //sequence too short
                                }
-                               else {  temp = ""; } //sequence too short
                        }
 
                }else{
@@ -219,7 +283,7 @@ string ChopSeqsCommand::getChopped(Sequence seq) {
                                        for (int i = 0; i < temp.length(); i++) {
                                                //eliminate N's
                                                if (toupper(temp[i]) == 'N') { 
-                                                       temp[i] == '.'; 
+                                                       temp[i] = '.'; 
                                                        tempLength--;
                                                        if (tempLength < numbases) { stopSpot = 0; break; }
                                                }
@@ -230,10 +294,11 @@ string ChopSeqsCommand::getChopped(Sequence seq) {
                                        }
                                        
                                        if (stopSpot == 0) { temp = ""; }
-                                       else {  temp = temp.substr(0, stopSpot);  }
+                                       else {  temp = temp.substr(0, stopSpot+1);  }
                                                        
-                               }else { temp = ""; } //sequence too short
-                               
+                               }else { 
+                                       if (!Short) { temp = ""; } //sequence too short
+                               }                               
                        }else { //you are keeping the back
                                int tempLength = tempUnaligned.length();
                                if (tempLength > numbases) { //you have enough bases to remove some
@@ -244,7 +309,7 @@ string ChopSeqsCommand::getChopped(Sequence seq) {
                                        for (int i = (temp.length()-1); i >= 0; i--) {
                                                //eliminate N's
                                                if (toupper(temp[i]) == 'N') { 
-                                                       temp[i] == '.'; 
+                                                       temp[i] = '.'; 
                                                        tempLength--;
                                                        if (tempLength < numbases) { stopSpot = 0; break; }
                                                }
@@ -255,9 +320,10 @@ string ChopSeqsCommand::getChopped(Sequence seq) {
                                        }
                                
                                        if (stopSpot == 0) { temp = ""; }
-                                       else {  temp = temp.substr(stopSpot+1);  }
+                                       else {  temp = temp.substr(stopSpot);  }
+                               }else { 
+                                       if (!Short) { temp = ""; } //sequence too short
                                }
-                               else {  temp = ""; } //sequence too short
                        }
                }