]> git.donarmstrong.com Git - mothur.git/blobdiff - splitgroupscommand.cpp
added citation function to commands
[mothur.git] / splitgroupscommand.cpp
index 6f8788a28daed38deeed9b419b28b699cffb6b55..220c7cc680b1729880001c5bf94ad1fc284a85e9 100644 (file)
 #include "splitgroupscommand.h"
 #include "sharedutilities.h"
 
+//**********************************************************************************************************************
+vector<string> SplitGroupCommand::setParameters(){     
+       try {           
+               CommandParameter pfasta("fasta", "InputTypes", "", "", "none", "none", "none",false,true); parameters.push_back(pfasta);
+               CommandParameter pname("name", "InputTypes", "", "", "none", "none", "none",false,false); parameters.push_back(pname);
+               CommandParameter pgroup("group", "InputTypes", "", "", "none", "none", "none",false,true); parameters.push_back(pgroup);
+               CommandParameter pgroups("groups", "String", "", "", "", "", "",false,false); parameters.push_back(pgroups);
+               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, "SplitGroupCommand", "setParameters");
+               exit(1);
+       }
+}
+//**********************************************************************************************************************
+string SplitGroupCommand::getHelpString(){     
+       try {
+               string helpString = "";
+               helpString += "The split.group command reads a group file, and parses your fasta and names files by groups. \n";
+               helpString += "The split.group command parameters are fasta, name, group and groups.\n";
+               helpString += "The fasta and group parameters are required.\n";
+               helpString += "The groups parameter allows you to select groups to create files for.  \n";
+               helpString += "For example if you set groups=A-B-C, you will get a .A.fasta, .A.names, .B.fasta, .B.names, .C.fasta, .C.names files.  \n";
+               helpString += "If you want .fasta and .names files for all groups, set groups=all.  \n";
+               helpString += "The split.group command should be used in the following format: split.group(fasta=yourFasta, group=yourGroupFile).\n";
+               helpString += "Example: split.group(fasta=abrecovery.fasta, group=abrecovery.groups).\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, "SplitGroupCommand", "getHelpString");
+               exit(1);
+       }
+}
+//**********************************************************************************************************************
+SplitGroupCommand::SplitGroupCommand(){        
+       try {
+               abort = true; calledHelp = true; 
+               setParameters();
+               vector<string> tempOutNames;
+               outputTypes["fasta"] = tempOutNames;
+               outputTypes["name"] = tempOutNames;
+       }
+       catch(exception& e) {
+               m->errorOut(e, "SplitGroupCommand", "SplitGroupCommand");
+               exit(1);
+       }
+}
 //**********************************************************************************************************************
 SplitGroupCommand::SplitGroupCommand(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[] =  {"name","group","groups","fasta","outputdir","inputdir"}; 
-                       vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
+                       vector<string> myArray = setParameters();
                        
                        OptionParser parser(option);
                        map<string, string> parameters = parser.getParameters();
@@ -33,7 +85,12 @@ SplitGroupCommand::SplitGroupCommand(string option)  {
                        for (it = parameters.begin(); it != parameters.end(); it++) { 
                                if (validParameter.isValidParameter(it->first, myArray, it->second) != true) {  abort = true;  }
                        }
-                                               
+                       
+                       //initialize outputTypes
+                       vector<string> tempOutNames;
+                       outputTypes["fasta"] = tempOutNames;
+                       outputTypes["name"] = 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 = "";          }
@@ -72,11 +129,19 @@ SplitGroupCommand::SplitGroupCommand(string option)  {
                
                        fastafile = validParameter.validFile(parameters, "fasta", true);
                        if (fastafile == "not open") { abort = true; }
-                       else if (fastafile == "not found") { fastafile = ""; m->mothurOut("fasta is a required parameter for the split.group command. "); m->mothurOutEndLine(); abort = true;  }       
+                       else if (fastafile == "not found") {                    
+                               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; }
+                       }       
                        
                        groupfile = validParameter.validFile(parameters, "group", true);
                        if (groupfile == "not open") {  groupfile = ""; abort = true; } 
-                       else if (groupfile == "not found") { groupfile = ""; m->mothurOut("group is a required parameter for the split.group command. "); m->mothurOutEndLine(); abort = true; }
+                       else if (groupfile == "not found") {                    
+                               groupfile = m->getGroupFile(); 
+                               if (groupfile != "") { m->mothurOut("Using " + groupfile + " as input file for the group parameter."); m->mothurOutEndLine(); }
+                               else {  m->mothurOut("You have no current groupfile and the group parameter is required."); m->mothurOutEndLine(); abort = true; }
+                       }
                        
                        groups = validParameter.validFile(parameters, "groups", false);         
                        if (groups == "not found") { groups = ""; }
@@ -93,31 +158,10 @@ SplitGroupCommand::SplitGroupCommand(string option)  {
        }
 }
 //**********************************************************************************************************************
-void SplitGroupCommand::help(){
-       try {
-               m->mothurOut("The split.group command reads a group file, and parses your fasta and names files by groups. \n");
-               m->mothurOut("The split.group command parameters are fasta, name, group and groups.\n");
-               m->mothurOut("The fasta and group parameters are required.\n");
-               m->mothurOut("The groups parameter allows you to select groups to create files for.  \n");
-               m->mothurOut("For example if you set groups=A-B-C, you will get a .A.fasta, .A.names, .B.fasta, .B.names, .C.fasta, .C.names files.  \n");
-               m->mothurOut("If you want .fasta and .names files for all groups, set groups=all.  \n");
-               m->mothurOut("The split.group command should be used in the following format: split.group(fasta=yourFasta, group=yourGroupFile).\n");
-               m->mothurOut("Example: split.group(fasta=abrecovery.fasta, group=abrecovery.groups).\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, "SplitGroupCommand", "help");
-               exit(1);
-       }
-}
-//**********************************************************************************************************************
-SplitGroupCommand::~SplitGroupCommand(){ }
-//**********************************************************************************************************************
 int SplitGroupCommand::execute(){
        try {
        
-               if (abort == true) {    return 0;       }
+               if (abort == true) { if (calledHelp) { return 0; }  return 2;   }
                
                groupMap = new GroupMap(groupfile);
                groupMap->readMap();
@@ -131,6 +175,17 @@ int SplitGroupCommand::execute(){
                
                if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) {        remove(outputNames[i].c_str()); } return 0; }
                
+               string current = "";
+               itTypes = outputTypes.find("fasta");
+               if (itTypes != outputTypes.end()) {
+                       if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setFastaFile(current); }
+               }
+               
+               itTypes = outputTypes.find("name");
+               if (itTypes != outputTypes.end()) {
+                       if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setNameFile(current); }
+               }
+               
                m->mothurOutEndLine();
                m->mothurOut("Output File Names: "); m->mothurOutEndLine();
                for (int i = 0; i < outputNames.size(); i++) {  m->mothurOut(outputNames[i]); m->mothurOutEndLine();    }
@@ -187,13 +242,13 @@ int SplitGroupCommand::splitFasta() {
                        temp = new ofstream;
                        filehandles[Groups[i]+"fasta"] = temp;
                        m->openOutputFile(filerootFasta + Groups[i] + ".fasta", *(filehandles[Groups[i]+"fasta"]));
-                       outputNames.push_back(filerootFasta + Groups[i] + ".fasta");
+                       outputNames.push_back(filerootFasta + Groups[i] + ".fasta"); outputTypes["fasta"].push_back(filerootFasta + Groups[i] + ".fasta");
                        
                        if (namefile != "") {
                                temp2 = new ofstream;
                                filehandles[Groups[i]+"name"] = temp2;
                                m->openOutputFile(filerootName + Groups[i] + ".names", *(filehandles[Groups[i]+"name"]));
-                               outputNames.push_back(filerootName + Groups[i] + ".names");
+                               outputNames.push_back(filerootName + Groups[i] + ".names"); outputTypes["name"].push_back(filerootFasta + Groups[i] + ".names");
                        }
                }
                        
@@ -267,6 +322,15 @@ int SplitGroupCommand::splitFasta() {
                        (*(filehandles[it3->first])).close();
                        delete it3->second;
                }
+               
+               vector<string> newOutputNames;
+               //remove blank files
+               for (int i = 0; i < outputNames.size(); i++) {
+                       if (m->isBlank(outputNames[i])) {
+                               remove(outputNames[i].c_str());
+                       }else { newOutputNames.push_back(outputNames[i]); }
+               }
+               outputNames = newOutputNames;
                                
                return 0;