5 * Created by westcott on 5/11/10.
6 * Copyright 2010 Schloss Lab. All rights reserved.
10 #include "catchallcommand.h"
11 #include "globaldata.hpp"
13 /**************************************************************************************/
14 CatchAllCommand::CatchAllCommand(string option) {
16 globaldata = GlobalData::getInstance();
20 //allow user to run help
21 if(option == "help") { help(); abort = true; }
24 //valid paramters for this command
25 string Array[] = {"sabund","label","inputdir","outputdir"};
26 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
28 OptionParser parser(option);
29 map<string,string> parameters = parser.getParameters();
31 ValidParameters validParameter;
32 map<string, string>::iterator it;
34 //check to make sure all parameters are valid for command
35 for (it = parameters.begin(); it != parameters.end(); it++) {
36 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) { abort = true; }
39 //if the user changes the input directory command factory will send this info to us in the output parameter
40 string inputDir = validParameter.validFile(parameters, "inputdir", false);
41 if (inputDir == "not found"){ inputDir = ""; }
44 it = parameters.find("sabund");
45 //user has given a template file
46 if(it != parameters.end()){
47 path = hasPath(it->second);
48 //if the user has not given a path then, add inputdir. else leave path alone.
49 if (path == "") { parameters["sabund"] = inputDir + it->second; }
53 //check for required parameters
54 sabundfile = validParameter.validFile(parameters, "sabund", true);
55 if (sabundfile == "not open") { sabundfile = ""; abort = true; }
56 else if (sabundfile == "not found") { sabundfile = ""; m->mothurOut("You must provide either a sabund file for the catchall command."); m->mothurOutEndLine(); abort=true; }
57 else { globaldata->setSabundFile(sabundfile); globaldata->setFormat("sabund"); }
59 string label = validParameter.validFile(parameters, "label", false);
60 if (label == "not found") { label = ""; }
62 if(label != "all") { splitAtDash(label, labels); allLines = 0; }
63 else { allLines = 1; }
67 //if the user changes the output directory command factory will send this info to us in the output parameter
68 outputDir = validParameter.validFile(parameters, "outputdir", false); if (outputDir == "not found"){ outputDir = "./"; }
73 m->errorOut(e, "CatchAllCommand", "CatchAllCommand");
77 //**********************************************************************************************************************
79 void CatchAllCommand::help(){
81 m->mothurOut("The catchall command interfaces mothur with the catchall program written by ...citation goes here...\n");
82 m->mothurOut("For more information about clearcut refer to http://catchall.cac.cornell.edu/ \n");
83 m->mothurOut("The catchall executable must be in a folder called catchall in the same folder as your mothur executable, similar to mothur's requirements for using blast. \n");
84 m->mothurOut("The catchall command parameters are sabund and label, sabund is required. \n");
85 m->mothurOut("The label parameter is used to analyze specific labels in your input.\n");
86 m->mothurOut("The catchall command should be in the following format: \n");
87 m->mothurOut("catchall(sabund=yourSabundFile) \n");
88 m->mothurOut("Example: catchall(sabund=abrecovery.fn.sabund) \n");
91 m->errorOut(e, "CatchAllCommand", "help");
96 /**************************************************************************************/
97 int CatchAllCommand::execute() {
100 if (abort == true) { return 0; }
102 vector<string> outputNames;
104 //prepare full output directory
105 outputDir = getFullPathName(outputDir);
107 //get location of catchall
108 GlobalData* globaldata = GlobalData::getInstance();
109 string path = globaldata->argv;
110 path = path.substr(0, (path.find_last_of('m')));
111 path = getFullPathName(path);
113 string catchAllCommandExe = "";
114 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
115 catchAllCommandExe += "mono " + path + "catchall/CatchAllcmdL.exe ";
117 catchAllCommandExe += path + "catchall/CatchAllcmdW.exe ";
120 read = new ReadOTUFile(sabundfile);
121 read->read(&*globaldata);
123 SAbundVector* sabund = globaldata->sabund;
124 string lastLabel = sabund->getLabel();
125 input = globaldata->ginput;
127 set<string> processedLabels;
128 set<string> userLabels = labels;
130 //for each label the user selected
131 while((sabund != NULL) && ((allLines == 1) || (userLabels.size() != 0))) {
134 if(allLines == 1 || labels.count(sabund->getLabel()) == 1){
135 m->mothurOut(sabund->getLabel()); m->mothurOutEndLine();
137 //create list of output files catchall will make
138 //datasetname_Analysis.csv
139 //datasetname_BestModelsAnalysis.csv
140 //datasetname_BestModelsFits.csv
141 //datasetname_BubblePlot.csv
142 //datasetname_Poisson_fits.csv
143 //datasetname_SingleExp_fits.csv
144 //datasetname_ThreeMixedExp_fits.csv
145 //datasetname_TwoMixedExp_fits.csv
147 //create catchall input file from mothur's inputfile
148 string filename = process(sabund);
150 //create system command
151 string catchAllCommand = catchAllCommandExe + filename + " " + path;
152 cout << catchAllCommand << endl;
154 system(catchAllCommand.c_str());
156 if (m->control_pressed) { delete read; delete input; globaldata->ginput = NULL; delete sabund; return 0; }
158 processedLabels.insert(sabund->getLabel());
159 userLabels.erase(sabund->getLabel());
162 if ((anyLabelsToProcess(sabund->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
163 string saveLabel = sabund->getLabel();
166 sabund = (input->getSAbundVector(lastLabel));
168 m->mothurOut(sabund->getLabel()); m->mothurOutEndLine();
170 //create list of output files catchall will make
171 //datasetname_Analysis.csv
172 //datasetname_BestModelsAnalysis.csv
173 //datasetname_BestModelsFits.csv
174 //datasetname_BubblePlot.csv
175 //datasetname_Poisson_fits.csv
176 //datasetname_SingleExp_fits.csv
177 //datasetname_ThreeMixedExp_fits.csv
178 //datasetname_TwoMixedExp_fits.csv
181 //create catchall input file from mothur's inputfile
182 string filename = process(sabund);
184 //create system command
185 string catchAllCommand = catchAllCommandExe + filename + " " + path;
186 cout << catchAllCommand << endl;
188 system(catchAllCommand.c_str());
190 if (m->control_pressed) { delete read; delete input; globaldata->ginput = NULL; delete sabund; return 0; }
192 processedLabels.insert(sabund->getLabel());
193 userLabels.erase(sabund->getLabel());
195 //restore real lastlabel to save below
196 sabund->setLabel(saveLabel);
200 lastLabel = sabund->getLabel();
203 sabund = (input->getSAbundVector());
206 //output error messages about any remaining user labels
207 set<string>::iterator it;
208 bool needToRun = false;
209 for (it = userLabels.begin(); it != userLabels.end(); it++) {
210 m->mothurOut("Your file does not include the label " + *it);
211 if (processedLabels.count(lastLabel) != 1) {
212 m->mothurOut(". I will use " + lastLabel + "."); m->mothurOutEndLine();
215 m->mothurOut(". Please refer to " + lastLabel + "."); m->mothurOutEndLine();
219 //run last label if you need to
220 if (needToRun == true) {
221 if (sabund != NULL) { delete sabund; }
222 sabund = (input->getSAbundVector(lastLabel));
224 m->mothurOut(sabund->getLabel()); m->mothurOutEndLine();
225 //create list of output files catchall will make
226 //datasetname_Analysis.csv
227 //datasetname_BestModelsAnalysis.csv
228 //datasetname_BestModelsFits.csv
229 //datasetname_BubblePlot.csv
230 //datasetname_Poisson_fits.csv
231 //datasetname_SingleExp_fits.csv
232 //datasetname_ThreeMixedExp_fits.csv
233 //datasetname_TwoMixedExp_fits.csv
235 //create catchall input file from mothur's inputfile
236 string filename = process(sabund);
238 //create system command
239 string catchAllCommand = catchAllCommandExe + filename + " " + path;
240 cout << catchAllCommand << endl;
242 system(catchAllCommand.c_str());
249 delete input; globaldata->ginput = NULL;
251 m->mothurOutEndLine();
252 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
253 for (int i = 0; i < outputNames.size(); i++) { m->mothurOut(outputNames[i]); m->mothurOutEndLine(); }
254 m->mothurOutEndLine();
259 catch(exception& e) {
260 m->errorOut(e, "CatchAllCommand", "execute");
264 //**********************************************************************************************************************
265 string CatchAllCommand::process(SAbundVector* sabund) {
267 string filename = outputDir + getRootName(getSimpleName(sabundfile)) + sabund->getLabel() + ".catchall";
268 filename = getFullPathName(filename);
271 openOutputFile(filename, out);
273 for (int i = 1; i <= sabund->getMaxRank(); i++) {
274 int temp = sabund->get(i);
277 out << i << "," << temp << endl;
285 catch(exception& e) {
286 m->errorOut(e, "CatchAllCommand", "process");
290 /**************************************************************************************/