]> git.donarmstrong.com Git - mothur.git/blobdiff - catchallcommand.cpp
added sequence name to error string in fastq.info. Changed np_shannon to npshannon.
[mothur.git] / catchallcommand.cpp
index a462bc7df80d0a0543c6cf9722daeabf4159ca40..857f68431c54b8a0076577ae74b70ecfba6e9773 100644 (file)
@@ -8,71 +8,74 @@
  */
 
 #include "catchallcommand.h"
  */
 
 #include "catchallcommand.h"
-#include "globaldata.hpp"
 
 //**********************************************************************************************************************
 
 //**********************************************************************************************************************
-vector<string> CatchAllCommand::getValidParameters(){  
+vector<string> CatchAllCommand::setParameters(){       
        try {
        try {
-               string AlignArray[] =  {"sabund","shared","label","inputdir","outputdir"};
-               vector<string> myArray (AlignArray, AlignArray+(sizeof(AlignArray)/sizeof(string)));
+               CommandParameter plabel("label", "String", "", "", "", "", "",false,false); parameters.push_back(plabel);
+               //can choose shared or sabund not both, so put them in the same chooseOnlyOneGroup
+               CommandParameter pshared("shared", "InputTypes", "", "", "catchallInputs", "catchallInputs", "none",false,false); parameters.push_back(pshared);
+               CommandParameter psabund("sabund", "InputTypes", "", "", "catchallInputs", "catchallInputs", "none",false,false); parameters.push_back(psabund);
+               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) {
                return myArray;
        }
        catch(exception& e) {
-               m->errorOut(e, "CatchAllCommand", "getValidParameters");
-               exit(1);
-       }
-}
-//**********************************************************************************************************************
-CatchAllCommand::CatchAllCommand(){    
-       try {
-               abort = true; calledHelp = true;
-               //initialize outputTypes
-               vector<string> tempOutNames;
-               outputTypes["csv"] = tempOutNames;
-               outputTypes["summary"] = tempOutNames;
-       }
-       catch(exception& e) {
-               m->errorOut(e, "CatchAllCommand", "CatchAllCommand");
+               m->errorOut(e, "CatchAllCommand", "setParameters");
                exit(1);
        }
 }
 //**********************************************************************************************************************
                exit(1);
        }
 }
 //**********************************************************************************************************************
-vector<string> CatchAllCommand::getRequiredParameters(){       
+string CatchAllCommand::getHelpString(){       
        try {
        try {
-               string AlignArray[] =  {"sabund","shared","or"};
-               vector<string> myArray (AlignArray, AlignArray+(sizeof(AlignArray)/sizeof(string)));
-               return myArray;
+               string helpString = "";
+               helpString += "The catchall command interfaces mothur with the catchall program written by Linda Woodard, Sean Connolly and John Bunge.\n";
+               helpString += "For more information about catchall refer to http://www.northeastern.edu/catchall/index.html \n";
+               helpString += "The catchall executable must be in the same folder as your mothur executable. \n";
+               helpString += "If you are a MAC or Linux user you must also have installed mono, a link to mono is on the webpage. \n";
+               helpString += "The catchall command parameters are shared, sabund and label.  shared or sabund is required. \n";
+               helpString += "The label parameter is used to analyze specific labels in your input.\n";
+               helpString += "The catchall command should be in the following format: \n";
+               helpString += "catchall(sabund=yourSabundFile) \n";
+               helpString += "Example: catchall(sabund=abrecovery.fn.sabund) \n";      
+               return helpString;
        }
        catch(exception& e) {
        }
        catch(exception& e) {
-               m->errorOut(e, "CatchAllCommand", "getRequiredParameters");
+               m->errorOut(e, "CatchAllCommand", "getHelpString");
                exit(1);
        }
 }
 //**********************************************************************************************************************
                exit(1);
        }
 }
 //**********************************************************************************************************************
-vector<string> CatchAllCommand::getRequiredFiles(){    
+CatchAllCommand::CatchAllCommand(){    
        try {
        try {
-               vector<string> myArray;
-               return myArray;
+               abort = true; calledHelp = true;
+               setParameters();
+               //initialize outputTypes
+               vector<string> tempOutNames;
+               outputTypes["csv"] = tempOutNames;
+               outputTypes["summary"] = tempOutNames;
        }
        catch(exception& e) {
        }
        catch(exception& e) {
-               m->errorOut(e, "CatchAllCommand", "getRequiredFiles");
+               m->errorOut(e, "CatchAllCommand", "CatchAllCommand");
                exit(1);
        }
 }
 /**************************************************************************************/
 CatchAllCommand::CatchAllCommand(string option)  {     
        try {
                exit(1);
        }
 }
 /**************************************************************************************/
 CatchAllCommand::CatchAllCommand(string option)  {     
        try {
-               globaldata = GlobalData::getInstance();
+               
                abort = false; calledHelp = false;   
                allLines = 1;
                
                //allow user to run help
                if(option == "help") { help(); abort = true; calledHelp = true; }
                abort = false; calledHelp = false;   
                allLines = 1;
                
                //allow user to run help
                if(option == "help") { help(); abort = true; calledHelp = true; }
+               else if(option == "citation") { citation(); abort = true; calledHelp = true;}
                
                else {
                
                else {
-                       //valid paramters for this command
-                       string Array[] =  {"shared","sabund","label","inputdir","outputdir"};
-                       vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
+                       vector<string> myArray = setParameters();
                        
                        OptionParser parser(option);
                        map<string,string> parameters = parser.getParameters();
                        
                        OptionParser parser(option);
                        map<string,string> parameters = parser.getParameters();
@@ -116,16 +119,12 @@ CatchAllCommand::CatchAllCommand(string option)  {
                        sabundfile = validParameter.validFile(parameters, "sabund", true);
                        if (sabundfile == "not open") { sabundfile = ""; abort = true; }
                        else if (sabundfile == "not found") { sabundfile = "";  }
                        sabundfile = validParameter.validFile(parameters, "sabund", true);
                        if (sabundfile == "not open") { sabundfile = ""; abort = true; }
                        else if (sabundfile == "not found") { sabundfile = "";  }
-                       else { globaldata->setSabundFile(sabundfile); globaldata->setFormat("sabund"); }
+                       else { m->setSabundFile(sabundfile); }
                        
                        sharedfile = validParameter.validFile(parameters, "shared", true);
                        if (sharedfile == "not open") { sharedfile = ""; abort = true; }
                        else if (sharedfile == "not found") { sharedfile = "";   }
                        
                        sharedfile = validParameter.validFile(parameters, "shared", true);
                        if (sharedfile == "not open") { sharedfile = ""; abort = true; }
                        else if (sharedfile == "not found") { sharedfile = "";   }
-                       
-                       //check for shared file loaded during read.otu
-                       if (sharedfile == "") {
-                               if (globaldata->getSharedFile() != "") { sharedfile = globaldata->getSharedFile(); }
-                       }
+                       else { m->setSharedFile(sharedfile); }
                        
                        string label = validParameter.validFile(parameters, "label", false);                    
                        if (label == "not found") { label = ""; }
                        
                        string label = validParameter.validFile(parameters, "label", false);                    
                        if (label == "not found") { label = ""; }
@@ -134,8 +133,22 @@ CatchAllCommand::CatchAllCommand(string option)  {
                                else { allLines = 1;  }
                        }
                
                                else { allLines = 1;  }
                        }
                
-                       if ((sharedfile == "") && (sabundfile == "")) { m->mothurOut("You must provide a sabund or shared file for the catchall command."); m->mothurOutEndLine(); abort=true; }
-
+                       if ((sharedfile == "") && (sabundfile == "")) { 
+                               //is there are current file available for either of these?
+                               //give priority to shared, then sabund
+                               //if there is a current shared file, use it
+                               sharedfile = m->getSharedFile(); 
+                               if (sharedfile != "") {  m->mothurOut("Using " + sharedfile + " as input file for the shared parameter."); m->mothurOutEndLine(); }
+                               else { 
+                                       sabundfile = m->getSabundFile(); 
+                                       if (sabundfile != "") {  m->mothurOut("Using " + sabundfile + " as input file for the sabund parameter."); m->mothurOutEndLine(); }
+                                       else { 
+                                               m->mothurOut("No valid current files. You must provide a sabund or shared file before you can use the catchall command."); m->mothurOutEndLine(); 
+                                               abort = true;
+                                       }
+                               }
+                       }
+                       
                        //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"){  
                        //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"){  
@@ -150,50 +163,32 @@ CatchAllCommand::CatchAllCommand(string option)  {
                exit(1);
        }
 }
                exit(1);
        }
 }
-//**********************************************************************************************************************
-
-void CatchAllCommand::help(){
-       try {
-               m->mothurOut("The catchall command interfaces mothur with the catchall program written by Linda Woodard, Sean Connolly and John Bunge.\n");
-               m->mothurOut("For more information about catchall refer to http://www.northeastern.edu/catchall/index.html \n");
-               m->mothurOut("The catchall executable must be in the same folder as your mothur executable. \n");
-               m->mothurOut("If you are a MAC or Linux user you must also have installed mono, a link to mono is on the webpage. \n");
-               m->mothurOut("The catchall command parameters are shared, sabund and label.  shared or sabund is required. \n");
-               m->mothurOut("The label parameter is used to analyze specific labels in your input.\n");
-               m->mothurOut("The catchall command should be in the following format: \n");
-               m->mothurOut("catchall(sabund=yourSabundFile) \n");
-               m->mothurOut("Example: catchall(sabund=abrecovery.fn.sabund) \n");      
-       }
-       catch(exception& e) {
-               m->errorOut(e, "CatchAllCommand", "help");
-               exit(1);
-       }
-}
-
 /**************************************************************************************/
 int CatchAllCommand::execute() {       
        try {
                
                if (abort == true) { if (calledHelp) { return 0; }  return 2;   }
                
 /**************************************************************************************/
 int CatchAllCommand::execute() {       
        try {
                
                if (abort == true) { if (calledHelp) { return 0; }  return 2;   }
                
-               //prepare full output directory
-               outputDir = m->getFullPathName(outputDir);
-               
                //get location of catchall
                //get location of catchall
-               GlobalData* globaldata = GlobalData::getInstance();
-               path = globaldata->argv;
-               path = path.substr(0, (path.find_last_of('m')));
+               path = m->argv;
+               path = path.substr(0, (path.find_last_of("othur")-5));
                path = m->getFullPathName(path);
                path = m->getFullPathName(path);
-
+               
+               savedOutputDir = outputDir;
                string catchAllCommandExe = ""; 
                #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
                        catchAllCommandExe += "mono " + path + "CatchAllcmdL.exe ";
                string catchAllCommandExe = ""; 
                #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
                        catchAllCommandExe += "mono " + path + "CatchAllcmdL.exe ";
+                       if (outputDir == "") { outputDir = "./"; } //force full pathname to be created for catchall, this is necessary because if catchall is in the path it will look for input file whereever the exe is and not the cwd.
                #else
                        catchAllCommandExe += "\"" + path + "CatchAllcmdW.exe\"" + " ";
                #else
                        catchAllCommandExe += "\"" + path + "CatchAllcmdW.exe\"" + " ";
+                       if (outputDir == "") { outputDir = ".\\"; } //force full pathname to be created for catchall, this is necessary because if catchall is in the path it will look for input file whereever the exe is and not the cwd.
                #endif
                
                #endif
                
+               //prepare full output directory
+               outputDir = m->getFullPathName(outputDir);
+               
                vector<string> inputFileNames;
                vector<string> inputFileNames;
-               if (sharedfile != "") { inputFileNames = parseSharedFile(sharedfile);  globaldata->setFormat("sabund");  }
+               if (sharedfile != "") { inputFileNames = parseSharedFile(sharedfile);   }
                else {  inputFileNames.push_back(sabundfile);  }                
                
                for (int p = 0; p < inputFileNames.size(); p++) {
                else {  inputFileNames.push_back(sabundfile);  }                
                
                for (int p = 0; p < inputFileNames.size(); p++) {
@@ -218,7 +213,7 @@ int CatchAllCommand::execute() {
                        
                        //for each label the user selected
                        while((sabund != NULL) && ((allLines == 1) || (userLabels.size() != 0))) {
                        
                        //for each label the user selected
                        while((sabund != NULL) && ((allLines == 1) || (userLabels.size() != 0))) {
-
+                               
                                                
                                if(allLines == 1 || labels.count(sabund->getLabel()) == 1){
                                                m->mothurOut(sabund->getLabel());  m->mothurOutEndLine();
                                                
                                if(allLines == 1 || labels.count(sabund->getLabel()) == 1){
                                                m->mothurOut(sabund->getLabel());  m->mothurOutEndLine();
@@ -226,7 +221,7 @@ int CatchAllCommand::execute() {
                                                //create catchall input file from mothur's inputfile
                                                string filename = process(sabund, inputFileNames[p]);
                                                string outputPath = m->getPathName(filename);
                                                //create catchall input file from mothur's inputfile
                                                string filename = process(sabund, inputFileNames[p]);
                                                string outputPath = m->getPathName(filename);
-                                       
+                                                                                       
                                                //create system command
                                                string catchAllCommand = "";
                                                #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
                                                //create system command
                                                string catchAllCommand = "";
                                                #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
@@ -237,12 +232,14 @@ int CatchAllCommand::execute() {
                                                        //wrap entire string in ""
                                                        catchAllCommand = "\"" + catchAllCommand + "\"";
                                                #endif
                                                        //wrap entire string in ""
                                                        catchAllCommand = "\"" + catchAllCommand + "\"";
                                                #endif
-                                                                               //run catchall
+                                       
+                                               //run catchall
                                                system(catchAllCommand.c_str());
                                        
                                                system(catchAllCommand.c_str());
                                        
-                                               remove(filename.c_str());
+                                               m->mothurRemove(filename);
                                        
                                                filename = m->getRootName(filename); filename = filename.substr(0, filename.length()-1); //rip off extra .
                                        
                                                filename = m->getRootName(filename); filename = filename.substr(0, filename.length()-1); //rip off extra .
+                                               if (savedOutputDir == "") { filename = m->getSimpleName(filename); }
                                        
                                                outputNames.push_back(filename + "_Analysis.csv"); outputTypes["csv"].push_back(filename + "_Analysis.csv");
                                                outputNames.push_back(filename + "_BestModelsAnalysis.csv"); outputTypes["csv"].push_back(filename + "_BestModelsAnalysis.csv");
                                        
                                                outputNames.push_back(filename + "_Analysis.csv"); outputTypes["csv"].push_back(filename + "_Analysis.csv");
                                                outputNames.push_back(filename + "_BestModelsAnalysis.csv"); outputTypes["csv"].push_back(filename + "_BestModelsAnalysis.csv");
@@ -251,7 +248,7 @@ int CatchAllCommand::execute() {
                                        
                                                createSummaryFile(filename + "_BestModelsAnalysis.csv", sabund->getLabel(), out);
                                                                                        
                                        
                                                createSummaryFile(filename + "_BestModelsAnalysis.csv", sabund->getLabel(), out);
                                                                                        
-                                               if (m->control_pressed) { out.close(); for (int i = 0; i < outputNames.size(); i++) {remove(outputNames[i].c_str());    }  delete input;  delete sabund;  return 0; }
+                                               if (m->control_pressed) { out.close(); for (int i = 0; i < outputNames.size(); i++) {m->mothurRemove(outputNames[i]);   }  delete input;  delete sabund;  return 0; }
 
                                                processedLabels.insert(sabund->getLabel());
                                                userLabels.erase(sabund->getLabel());
 
                                                processedLabels.insert(sabund->getLabel());
                                                userLabels.erase(sabund->getLabel());
@@ -269,7 +266,7 @@ int CatchAllCommand::execute() {
                                                //create catchall input file from mothur's inputfile
                                                string filename = process(sabund, inputFileNames[p]);
                                                string outputPath = m->getPathName(filename);
                                                //create catchall input file from mothur's inputfile
                                                string filename = process(sabund, inputFileNames[p]);
                                                string outputPath = m->getPathName(filename);
-                                               
+                                                                                       
                                                //create system command
                                                string catchAllCommand = "";
                                                #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
                                                //create system command
                                                string catchAllCommand = "";
                                                #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
@@ -283,9 +280,10 @@ int CatchAllCommand::execute() {
                                                //run catchall
                                                system(catchAllCommand.c_str());
                                        
                                                //run catchall
                                                system(catchAllCommand.c_str());
                                        
-                                               remove(filename.c_str());
+                                               m->mothurRemove(filename);
                                        
                                                filename = m->getRootName(filename); filename = filename.substr(0, filename.length()-1); //rip off extra .
                                        
                                                filename = m->getRootName(filename); filename = filename.substr(0, filename.length()-1); //rip off extra .
+                                               if (savedOutputDir == "") { filename = m->getSimpleName(filename); }
                                        
                                                outputNames.push_back(filename + "_Analysis.csv"); outputTypes["csv"].push_back(filename + "_Analysis.csv");
                                                outputNames.push_back(filename + "_BestModelsAnalysis.csv"); outputTypes["csv"].push_back(filename + "_BestModelsAnalysis.csv");
                                        
                                                outputNames.push_back(filename + "_Analysis.csv"); outputTypes["csv"].push_back(filename + "_Analysis.csv");
                                                outputNames.push_back(filename + "_BestModelsAnalysis.csv"); outputTypes["csv"].push_back(filename + "_BestModelsAnalysis.csv");
@@ -294,7 +292,7 @@ int CatchAllCommand::execute() {
                                        
                                                createSummaryFile(filename + "_BestModelsAnalysis.csv", sabund->getLabel(), out);
                                        
                                        
                                                createSummaryFile(filename + "_BestModelsAnalysis.csv", sabund->getLabel(), out);
                                        
-                                               if (m->control_pressed) { out.close(); for (int i = 0; i < outputNames.size(); i++) {remove(outputNames[i].c_str());    }  delete input;  delete sabund;  return 0; }
+                                               if (m->control_pressed) { out.close(); for (int i = 0; i < outputNames.size(); i++) {m->mothurRemove(outputNames[i]);   }  delete input;  delete sabund;  return 0; }
 
                                                processedLabels.insert(sabund->getLabel());
                                                userLabels.erase(sabund->getLabel());
 
                                                processedLabels.insert(sabund->getLabel());
                                                userLabels.erase(sabund->getLabel());
@@ -347,9 +345,10 @@ int CatchAllCommand::execute() {
                                //run catchall
                                system(catchAllCommand.c_str());
                                
                                //run catchall
                                system(catchAllCommand.c_str());
                                
-                               remove(filename.c_str());
+                               m->mothurRemove(filename);
                                
                                filename = m->getRootName(filename); filename = filename.substr(0, filename.length()-1); //rip off extra .
                                
                                filename = m->getRootName(filename); filename = filename.substr(0, filename.length()-1); //rip off extra .
+                               if (savedOutputDir == "") { filename = m->getSimpleName(filename); }
                                
                                outputNames.push_back(filename + "_Analysis.csv"); outputTypes["csv"].push_back(filename + "_Analysis.csv");
                                outputNames.push_back(filename + "_BestModelsAnalysis.csv"); outputTypes["csv"].push_back(filename + "_BestModelsAnalysis.csv");
                                
                                outputNames.push_back(filename + "_Analysis.csv"); outputTypes["csv"].push_back(filename + "_Analysis.csv");
                                outputNames.push_back(filename + "_BestModelsAnalysis.csv"); outputTypes["csv"].push_back(filename + "_BestModelsAnalysis.csv");
@@ -364,12 +363,12 @@ int CatchAllCommand::execute() {
                        out.close();
                        delete input; 
                        
                        out.close();
                        delete input; 
                        
-                       if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) {remove(outputNames[i].c_str()); } return 0; }
+                       if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) {m->mothurRemove(outputNames[i]);        } return 0; }
                                
                }
                
                if (sharedfile == "") {  
                                
                }
                
                if (sharedfile == "") {  
-                       string summaryfilename = outputDir + m->getRootName(m->getSimpleName(inputFileNames[0])) + "catchall.summary";
+                       string summaryfilename = savedOutputDir + m->getRootName(m->getSimpleName(inputFileNames[0])) + "catchall.summary";
                        summaryfilename = m->getFullPathName(summaryfilename);
                        outputNames.push_back(summaryfilename); outputTypes["summary"].push_back(summaryfilename);
                }else { //combine summaries
                        summaryfilename = m->getFullPathName(summaryfilename);
                        outputNames.push_back(summaryfilename); outputTypes["summary"].push_back(summaryfilename);
                }else { //combine summaries
@@ -425,7 +424,7 @@ string CatchAllCommand::combineSummmary(vector<string>& outputNames) {
        try {
                
                ofstream out;
        try {
                
                ofstream out;
-               string combineFileName = outputDir + m->getRootName(m->getSimpleName(sharedfile)) + "catchall.summary";
+               string combineFileName = savedOutputDir + m->getRootName(m->getSimpleName(sharedfile)) + "catchall.summary";
                
                //open combined file
                m->openOutputFile(combineFileName, out);
                
                //open combined file
                m->openOutputFile(combineFileName, out);
@@ -472,7 +471,7 @@ string CatchAllCommand::combineSummmary(vector<string>& outputNames) {
                        numLines = thisFilesLines.size();
                        
                        temp.close();
                        numLines = thisFilesLines.size();
                        
                        temp.close();
-                       remove(outputNames[i].c_str());
+                       m->mothurRemove(outputNames[i]);
                }
                
                //for each label
                }
                
                //for each label
@@ -580,7 +579,7 @@ vector<string> CatchAllCommand::parseSharedFile(string filename) {
                
                //clears file before we start to write to it below
                for (int i=0; i<lookup.size(); i++) {
                
                //clears file before we start to write to it below
                for (int i=0; i<lookup.size(); i++) {
-                       remove((sharedFileRoot + lookup[i]->getGroup() + ".sabund").c_str());
+                       m->mothurRemove((sharedFileRoot + lookup[i]->getGroup() + ".sabund"));
                        filenames.push_back((sharedFileRoot + lookup[i]->getGroup() + ".sabund"));
                        groups.push_back(lookup[i]->getGroup());
                }
                        filenames.push_back((sharedFileRoot + lookup[i]->getGroup() + ".sabund"));
                        groups.push_back(lookup[i]->getGroup());
                }