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 vector<string> CatchAllCommand::getValidParameters(){
16 string AlignArray[] = {"sabund","label","inputdir","outputdir"};
17 vector<string> myArray (AlignArray, AlignArray+(sizeof(AlignArray)/sizeof(string)));
21 m->errorOut(e, "CatchAllCommand", "getValidParameters");
25 //**********************************************************************************************************************
26 CatchAllCommand::CatchAllCommand(){
28 //initialize outputTypes
30 //need to determine outputFIles and types
33 m->errorOut(e, "CatchAllCommand", "CatchAllCommand");
37 //**********************************************************************************************************************
38 vector<string> CatchAllCommand::getRequiredParameters(){
40 string AlignArray[] = {"sabund"};
41 vector<string> myArray (AlignArray, AlignArray+(sizeof(AlignArray)/sizeof(string)));
45 m->errorOut(e, "CatchAllCommand", "getRequiredParameters");
49 //**********************************************************************************************************************
50 vector<string> CatchAllCommand::getRequiredFiles(){
52 vector<string> myArray;
56 m->errorOut(e, "CatchAllCommand", "getRequiredFiles");
60 /**************************************************************************************/
61 CatchAllCommand::CatchAllCommand(string option) {
63 globaldata = GlobalData::getInstance();
67 //allow user to run help
68 if(option == "help") { help(); abort = true; }
71 //valid paramters for this command
72 string Array[] = {"sabund","label","inputdir","outputdir"};
73 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
75 OptionParser parser(option);
76 map<string,string> parameters = parser.getParameters();
78 ValidParameters validParameter;
79 map<string, string>::iterator it;
81 //check to make sure all parameters are valid for command
82 for (it = parameters.begin(); it != parameters.end(); it++) {
83 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) { abort = true; }
87 //initialize outputTypes
88 //need to determine outputFIles and types
91 //if the user changes the input directory command factory will send this info to us in the output parameter
92 string inputDir = validParameter.validFile(parameters, "inputdir", false);
93 if (inputDir == "not found"){ inputDir = ""; }
96 it = parameters.find("sabund");
97 //user has given a template file
98 if(it != parameters.end()){
99 path = m->hasPath(it->second);
100 //if the user has not given a path then, add inputdir. else leave path alone.
101 if (path == "") { parameters["sabund"] = inputDir + it->second; }
105 //check for required parameters
106 sabundfile = validParameter.validFile(parameters, "sabund", true);
107 if (sabundfile == "not open") { sabundfile = ""; abort = true; }
108 else if (sabundfile == "not found") { sabundfile = ""; m->mothurOut("You must provide a sabund file for the catchall command."); m->mothurOutEndLine(); abort=true; }
109 else { globaldata->setSabundFile(sabundfile); globaldata->setFormat("sabund"); }
111 string label = validParameter.validFile(parameters, "label", false);
112 if (label == "not found") { label = ""; }
114 if(label != "all") { m->splitAtDash(label, labels); allLines = 0; }
115 else { allLines = 1; }
119 //if the user changes the output directory command factory will send this info to us in the output parameter
120 outputDir = validParameter.validFile(parameters, "outputdir", false); if (outputDir == "not found"){ outputDir = "./"; }
124 catch(exception& e) {
125 m->errorOut(e, "CatchAllCommand", "CatchAllCommand");
129 //**********************************************************************************************************************
131 void CatchAllCommand::help(){
133 m->mothurOut("The catchall command interfaces mothur with the catchall program written by ...citation goes here...\n");
134 m->mothurOut("For more information about clearcut refer to http://catchall.cac.cornell.edu/ \n");
135 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");
136 m->mothurOut("The catchall command parameters are sabund and label, sabund is required. \n");
137 m->mothurOut("The label parameter is used to analyze specific labels in your input.\n");
138 m->mothurOut("The catchall command should be in the following format: \n");
139 m->mothurOut("catchall(sabund=yourSabundFile) \n");
140 m->mothurOut("Example: catchall(sabund=abrecovery.fn.sabund) \n");
142 catch(exception& e) {
143 m->errorOut(e, "CatchAllCommand", "help");
148 /**************************************************************************************/
149 int CatchAllCommand::execute() {
152 if (abort == true) { return 0; }
154 //prepare full output directory
155 outputDir = m->getFullPathName(outputDir);
157 //get location of catchall
158 GlobalData* globaldata = GlobalData::getInstance();
159 string path = globaldata->argv;
160 path = path.substr(0, (path.find_last_of('m')));
161 path = m->getFullPathName(path);
163 string catchAllCommandExe = "";
164 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
165 catchAllCommandExe += "mono " + path + "catchall/CatchAllcmdL.exe ";
167 catchAllCommandExe += path + "catchall/CatchAllcmdW.exe ";
170 read = new ReadOTUFile(sabundfile);
171 read->read(&*globaldata);
173 SAbundVector* sabund = globaldata->sabund;
174 string lastLabel = sabund->getLabel();
175 input = globaldata->ginput;
177 set<string> processedLabels;
178 set<string> userLabels = labels;
180 //for each label the user selected
181 while((sabund != NULL) && ((allLines == 1) || (userLabels.size() != 0))) {
184 if(allLines == 1 || labels.count(sabund->getLabel()) == 1){
185 m->mothurOut(sabund->getLabel()); m->mothurOutEndLine();
187 //create list of output files catchall will make
188 //datasetname_Analysis.csv
189 //datasetname_BestModelsAnalysis.csv
190 //datasetname_BestModelsFits.csv
191 //datasetname_BubblePlot.csv
192 //datasetname_Poisson_fits.csv
193 //datasetname_SingleExp_fits.csv
194 //datasetname_ThreeMixedExp_fits.csv
195 //datasetname_TwoMixedExp_fits.csv
197 //create catchall input file from mothur's inputfile
198 string filename = process(sabund);
200 //create system command
201 string catchAllCommand = catchAllCommandExe + filename + " " + path;
202 cout << catchAllCommand << endl;
204 system(catchAllCommand.c_str());
206 if (m->control_pressed) { delete read; delete input; globaldata->ginput = NULL; delete sabund; return 0; }
208 processedLabels.insert(sabund->getLabel());
209 userLabels.erase(sabund->getLabel());
212 if ((m->anyLabelsToProcess(sabund->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
213 string saveLabel = sabund->getLabel();
216 sabund = (input->getSAbundVector(lastLabel));
218 m->mothurOut(sabund->getLabel()); m->mothurOutEndLine();
220 //create list of output files catchall will make
221 //datasetname_Analysis.csv
222 //datasetname_BestModelsAnalysis.csv
223 //datasetname_BestModelsFits.csv
224 //datasetname_BubblePlot.csv
225 //datasetname_Poisson_fits.csv
226 //datasetname_SingleExp_fits.csv
227 //datasetname_ThreeMixedExp_fits.csv
228 //datasetname_TwoMixedExp_fits.csv
231 //create catchall input file from mothur's inputfile
232 string filename = process(sabund);
234 //create system command
235 string catchAllCommand = catchAllCommandExe + filename + " " + path;
236 cout << catchAllCommand << endl;
238 system(catchAllCommand.c_str());
240 if (m->control_pressed) { delete read; delete input; globaldata->ginput = NULL; delete sabund; return 0; }
242 processedLabels.insert(sabund->getLabel());
243 userLabels.erase(sabund->getLabel());
245 //restore real lastlabel to save below
246 sabund->setLabel(saveLabel);
250 lastLabel = sabund->getLabel();
253 sabund = (input->getSAbundVector());
256 //output error messages about any remaining user labels
257 set<string>::iterator it;
258 bool needToRun = false;
259 for (it = userLabels.begin(); it != userLabels.end(); it++) {
260 m->mothurOut("Your file does not include the label " + *it);
261 if (processedLabels.count(lastLabel) != 1) {
262 m->mothurOut(". I will use " + lastLabel + "."); m->mothurOutEndLine();
265 m->mothurOut(". Please refer to " + lastLabel + "."); m->mothurOutEndLine();
269 //run last label if you need to
270 if (needToRun == true) {
271 if (sabund != NULL) { delete sabund; }
272 sabund = (input->getSAbundVector(lastLabel));
274 m->mothurOut(sabund->getLabel()); m->mothurOutEndLine();
275 //create list of output files catchall will make
276 //datasetname_Analysis.csv
277 //datasetname_BestModelsAnalysis.csv
278 //datasetname_BestModelsFits.csv
279 //datasetname_BubblePlot.csv
280 //datasetname_Poisson_fits.csv
281 //datasetname_SingleExp_fits.csv
282 //datasetname_ThreeMixedExp_fits.csv
283 //datasetname_TwoMixedExp_fits.csv
285 //create catchall input file from mothur's inputfile
286 string filename = process(sabund);
288 //create system command
289 string catchAllCommand = catchAllCommandExe + filename + " " + path;
290 cout << catchAllCommand << endl;
292 system(catchAllCommand.c_str());
299 delete input; globaldata->ginput = NULL;
301 m->mothurOutEndLine();
302 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
303 for (int i = 0; i < outputNames.size(); i++) { m->mothurOut(outputNames[i]); m->mothurOutEndLine(); }
304 m->mothurOutEndLine();
309 catch(exception& e) {
310 m->errorOut(e, "CatchAllCommand", "execute");
314 //**********************************************************************************************************************
315 string CatchAllCommand::process(SAbundVector* sabund) {
317 string filename = outputDir + m->getRootName(m->getSimpleName(sabundfile)) + sabund->getLabel() + ".catchall";
318 filename = m->getFullPathName(filename);
321 m->openOutputFile(filename, out);
323 for (int i = 1; i <= sabund->getMaxRank(); i++) {
324 int temp = sabund->get(i);
327 out << i << "," << temp << endl;
335 catch(exception& e) {
336 m->errorOut(e, "CatchAllCommand", "process");
340 /**************************************************************************************/