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
29 vector<string> tempOutNames;
30 outputTypes["csv"] = tempOutNames;
31 outputTypes["summary"] = tempOutNames;
34 m->errorOut(e, "CatchAllCommand", "CatchAllCommand");
38 //**********************************************************************************************************************
39 vector<string> CatchAllCommand::getRequiredParameters(){
41 string AlignArray[] = {"sabund"};
42 vector<string> myArray (AlignArray, AlignArray+(sizeof(AlignArray)/sizeof(string)));
46 m->errorOut(e, "CatchAllCommand", "getRequiredParameters");
50 //**********************************************************************************************************************
51 vector<string> CatchAllCommand::getRequiredFiles(){
53 vector<string> myArray;
57 m->errorOut(e, "CatchAllCommand", "getRequiredFiles");
61 /**************************************************************************************/
62 CatchAllCommand::CatchAllCommand(string option) {
64 globaldata = GlobalData::getInstance();
68 //allow user to run help
69 if(option == "help") { help(); abort = true; }
72 //valid paramters for this command
73 string Array[] = {"sabund","label","inputdir","outputdir"};
74 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
76 OptionParser parser(option);
77 map<string,string> parameters = parser.getParameters();
79 ValidParameters validParameter;
80 map<string, string>::iterator it;
82 //check to make sure all parameters are valid for command
83 for (it = parameters.begin(); it != parameters.end(); it++) {
84 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) { abort = true; }
87 //initialize outputTypes
88 vector<string> tempOutNames;
89 outputTypes["csv"] = tempOutNames;
90 outputTypes["summary"] = tempOutNames;
92 //if the user changes the input directory command factory will send this info to us in the output parameter
93 string inputDir = validParameter.validFile(parameters, "inputdir", false);
94 if (inputDir == "not found"){ inputDir = ""; }
97 it = parameters.find("sabund");
98 //user has given a template file
99 if(it != parameters.end()){
100 path = m->hasPath(it->second);
101 //if the user has not given a path then, add inputdir. else leave path alone.
102 if (path == "") { parameters["sabund"] = inputDir + it->second; }
106 //check for required parameters
107 sabundfile = validParameter.validFile(parameters, "sabund", true);
108 if (sabundfile == "not open") { sabundfile = ""; abort = true; }
109 else if (sabundfile == "not found") { sabundfile = ""; m->mothurOut("You must provide a sabund file for the catchall command."); m->mothurOutEndLine(); abort=true; }
110 else { globaldata->setSabundFile(sabundfile); globaldata->setFormat("sabund"); }
112 string label = validParameter.validFile(parameters, "label", false);
113 if (label == "not found") { label = ""; }
115 if(label != "all") { m->splitAtDash(label, labels); allLines = 0; }
116 else { allLines = 1; }
120 //if the user changes the output directory command factory will send this info to us in the output parameter
121 outputDir = validParameter.validFile(parameters, "outputdir", false); if (outputDir == "not found"){ outputDir = m->hasPath(sabundfile); }
125 catch(exception& e) {
126 m->errorOut(e, "CatchAllCommand", "CatchAllCommand");
130 //**********************************************************************************************************************
132 void CatchAllCommand::help(){
134 m->mothurOut("The catchall command interfaces mothur with the catchall program written by Linda Woodard, Sean Connolly and John Bunge.\n");
135 m->mothurOut("For more information about catchall refer to http://www.northeastern.edu/catchall/index.html \n");
136 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");
137 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");
138 m->mothurOut("The catchall command parameters are sabund and label, sabund is required. \n");
139 m->mothurOut("The label parameter is used to analyze specific labels in your input.\n");
140 m->mothurOut("The catchall command should be in the following format: \n");
141 m->mothurOut("catchall(sabund=yourSabundFile) \n");
142 m->mothurOut("Example: catchall(sabund=abrecovery.fn.sabund) \n");
144 catch(exception& e) {
145 m->errorOut(e, "CatchAllCommand", "help");
150 /**************************************************************************************/
151 int CatchAllCommand::execute() {
154 if (abort == true) { return 0; }
156 //prepare full output directory
157 outputDir = m->getFullPathName(outputDir);
159 //get location of catchall
160 GlobalData* globaldata = GlobalData::getInstance();
161 path = globaldata->argv;
162 path = path.substr(0, (path.find_last_of('m')));
163 path = m->getFullPathName(path);
165 string catchAllCommandExe = "";
166 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
167 catchAllCommandExe += "mono " + path + "catchall/CatchAllcmdL.exe ";
169 catchAllCommandExe += path + "catchall/CatchAllcmdW.exe ";
172 read = new ReadOTUFile(sabundfile);
173 read->read(&*globaldata);
175 SAbundVector* sabund = globaldata->sabund;
176 string lastLabel = sabund->getLabel();
177 input = globaldata->ginput;
179 set<string> processedLabels;
180 set<string> userLabels = labels;
182 string summaryfilename = outputDir + m->getRootName(m->getSimpleName(sabundfile)) + "catchall.summary";
183 summaryfilename = m->getFullPathName(summaryfilename);
184 outputNames.push_back(summaryfilename); outputTypes["summary"].push_back(summaryfilename);
187 m->openOutputFile(summaryfilename, out);
189 out << "label\tmodel\testimate\tlci\tuci" << endl;
191 //for each label the user selected
192 while((sabund != NULL) && ((allLines == 1) || (userLabels.size() != 0))) {
195 if(allLines == 1 || labels.count(sabund->getLabel()) == 1){
196 m->mothurOut(sabund->getLabel()); m->mothurOutEndLine();
198 //create catchall input file from mothur's inputfile
199 string filename = process(sabund);
200 string outputPath = m->getPathName(filename);
202 //create system command
203 string catchAllCommand = catchAllCommandExe + filename + " " + outputPath + " 1";
206 system(catchAllCommand.c_str());
208 remove(filename.c_str());
210 filename = m->getRootName(filename); filename = filename.substr(0, filename.length()-1); //rip off extra .
212 outputNames.push_back(filename + "_Analysis.csv"); outputTypes["csv"].push_back(filename + "_Analysis.csv");
213 outputNames.push_back(filename + "_BestModelsAnalysis.csv"); outputTypes["csv"].push_back(filename + "_BestModelsAnalysis.csv");
214 outputNames.push_back(filename + "_BestModelsFits.csv"); outputTypes["csv"].push_back(filename + "_BestModelsFits.csv");
215 outputNames.push_back(filename + "_BubblePlot.csv"); outputTypes["csv"].push_back(filename + "_BubblePlot.csv");
217 createSummaryFile(filename + "_BestModelsAnalysis.csv", sabund->getLabel(), out);
219 if (m->control_pressed) { out.close(); for (int i = 0; i < outputNames.size(); i++) {remove(outputNames[i].c_str()); } delete read; delete input; globaldata->ginput = NULL; delete sabund; return 0; }
221 processedLabels.insert(sabund->getLabel());
222 userLabels.erase(sabund->getLabel());
225 if ((m->anyLabelsToProcess(sabund->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
226 string saveLabel = sabund->getLabel();
229 sabund = (input->getSAbundVector(lastLabel));
231 m->mothurOut(sabund->getLabel()); m->mothurOutEndLine();
234 //create catchall input file from mothur's inputfile
235 string filename = process(sabund);
236 string outputPath = m->getPathName(filename);
238 //create system command
239 string catchAllCommand = catchAllCommandExe + filename + " " + outputPath + " 1";
242 system(catchAllCommand.c_str());
244 remove(filename.c_str());
246 filename = m->getRootName(filename); filename = filename.substr(0, filename.length()-1); //rip off extra .
248 outputNames.push_back(filename + "_Analysis.csv"); outputTypes["csv"].push_back(filename + "_Analysis.csv");
249 outputNames.push_back(filename + "_BestModelsAnalysis.csv"); outputTypes["csv"].push_back(filename + "_BestModelsAnalysis.csv");
250 outputNames.push_back(filename + "_BestModelsFits.csv"); outputTypes["csv"].push_back(filename + "_BestModelsFits.csv");
251 outputNames.push_back(filename + "_BubblePlot.csv"); outputTypes["csv"].push_back(filename + "_BubblePlot.csv");
253 createSummaryFile(filename + "_BestModelsAnalysis.csv", sabund->getLabel(), out);
255 if (m->control_pressed) { out.close(); for (int i = 0; i < outputNames.size(); i++) {remove(outputNames[i].c_str()); } delete read; delete input; globaldata->ginput = NULL; delete sabund; return 0; }
257 processedLabels.insert(sabund->getLabel());
258 userLabels.erase(sabund->getLabel());
260 //restore real lastlabel to save below
261 sabund->setLabel(saveLabel);
265 lastLabel = sabund->getLabel();
268 sabund = (input->getSAbundVector());
271 //output error messages about any remaining user labels
272 set<string>::iterator it;
273 bool needToRun = false;
274 for (it = userLabels.begin(); it != userLabels.end(); it++) {
275 m->mothurOut("Your file does not include the label " + *it);
276 if (processedLabels.count(lastLabel) != 1) {
277 m->mothurOut(". I will use " + lastLabel + "."); m->mothurOutEndLine();
280 m->mothurOut(". Please refer to " + lastLabel + "."); m->mothurOutEndLine();
284 //run last label if you need to
285 if (needToRun == true) {
286 if (sabund != NULL) { delete sabund; }
287 sabund = (input->getSAbundVector(lastLabel));
289 m->mothurOut(sabund->getLabel()); m->mothurOutEndLine();
291 //create catchall input file from mothur's inputfile
292 string filename = process(sabund);
293 string outputPath = m->getPathName(filename);
295 //create system command
296 string catchAllCommand = catchAllCommandExe + filename + " " + outputPath + " 1";
299 system(catchAllCommand.c_str());
301 remove(filename.c_str());
303 filename = m->getRootName(filename); filename = filename.substr(0, filename.length()-1); //rip off extra .
305 outputNames.push_back(filename + "_Analysis.csv"); outputTypes["csv"].push_back(filename + "_Analysis.csv");
306 outputNames.push_back(filename + "_BestModelsAnalysis.csv"); outputTypes["csv"].push_back(filename + "_BestModelsAnalysis.csv");
307 outputNames.push_back(filename + "_BestModelsFits.csv"); outputTypes["csv"].push_back(filename + "_BestModelsFits.csv");
308 outputNames.push_back(filename + "_BubblePlot.csv"); outputTypes["csv"].push_back(filename + "_BubblePlot.csv");
310 createSummaryFile(filename + "_BestModelsAnalysis.csv", sabund->getLabel(), out);
317 delete input; globaldata->ginput = NULL;
319 if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) {remove(outputNames[i].c_str()); } return 0; }
321 m->mothurOutEndLine();
322 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
323 for (int i = 0; i < outputNames.size(); i++) { m->mothurOut(outputNames[i]); m->mothurOutEndLine(); }
324 m->mothurOutEndLine();
329 catch(exception& e) {
330 m->errorOut(e, "CatchAllCommand", "execute");
334 //**********************************************************************************************************************
335 string CatchAllCommand::process(SAbundVector* sabund) {
337 string filename = outputDir + m->getRootName(m->getSimpleName(sabundfile)) + sabund->getLabel() + ".csv";
338 filename = m->getFullPathName(filename);
341 m->openOutputFile(filename, out);
343 for (int i = 1; i <= sabund->getMaxRank(); i++) {
344 int temp = sabund->get(i);
347 out << i << "," << temp << endl;
355 catch(exception& e) {
356 m->errorOut(e, "CatchAllCommand", "process");
360 //**********************************************************************************************************************
361 int CatchAllCommand::createSummaryFile(string file1, string label, ofstream& out) {
365 m->openInputFile(file1, in);
369 string header = m->getline(in); m->gobble(in);
371 int pos = header.find("Total Number of Observed Species =");
372 string numString = "";
375 if (pos == string::npos) { m->mothurOut("[ERROR]: cannot parse " + file1); m->mothurOutEndLine(); }
377 //pos will be the position of the T in total, so we want to count to the position of =
388 if (pos > header.length()) { m->mothurOut("Cannot find number of OTUs in " + file1); m->mothurOutEndLine(); in.close(); return 0; }
392 string firstline = m->getline(in); m->gobble(in);
393 vector<string> values;
394 m->splitAtComma(firstline, values);
396 values.pop_back(); //last value is always a blank string since the last character in the line is always a ','
398 if (values.size() == 1) { //grab next line if firstline didn't have what you wanted
399 string secondline = m->getline(in); m->gobble(in);
401 m->splitAtComma(secondline, values);
403 values.pop_back(); //last value is always a blank string since the last character in the line is always a ','
406 if (values.size() == 1) { //still not what we wanted fill values with numOTUs
407 values.resize(8, "");
409 values[4] = numString;
410 values[6] = numString;
411 values[7] = numString;
414 if (values.size() < 8) { values.resize(8, ""); }
416 out << label << '\t' << values[1] << '\t' << values[4] << '\t' << values[6] << '\t' << values[7] << endl;
424 catch(exception& e) {
425 m->errorOut(e, "CatchAllCommand", "createSummaryFile");
429 /**************************************************************************************/