]> git.donarmstrong.com Git - mothur.git/blob - catchallcommand.cpp
8f2841358dd4bbb91b69c2a530b6332b8fc2d131
[mothur.git] / catchallcommand.cpp
1 /*
2  *  catchallcommand.cpp
3  *  Mothur
4  *
5  *  Created by westcott on 5/11/10.
6  *  Copyright 2010 Schloss Lab. All rights reserved.
7  *
8  */
9
10 #include "catchallcommand.h"
11 #include "globaldata.hpp"
12
13 //**********************************************************************************************************************
14 vector<string> CatchAllCommand::getValidParameters(){   
15         try {
16                 string AlignArray[] =  {"sabund","label","inputdir","outputdir"};
17                 vector<string> myArray (AlignArray, AlignArray+(sizeof(AlignArray)/sizeof(string)));
18                 return myArray;
19         }
20         catch(exception& e) {
21                 m->errorOut(e, "CatchAllCommand", "getValidParameters");
22                 exit(1);
23         }
24 }
25 //**********************************************************************************************************************
26 CatchAllCommand::CatchAllCommand(){     
27         try {
28                 //initialize outputTypes
29                 vector<string> tempOutNames;
30                 outputTypes["csv"] = tempOutNames;
31         }
32         catch(exception& e) {
33                 m->errorOut(e, "CatchAllCommand", "CatchAllCommand");
34                 exit(1);
35         }
36 }
37 //**********************************************************************************************************************
38 vector<string> CatchAllCommand::getRequiredParameters(){        
39         try {
40                 string AlignArray[] =  {"sabund"};
41                 vector<string> myArray (AlignArray, AlignArray+(sizeof(AlignArray)/sizeof(string)));
42                 return myArray;
43         }
44         catch(exception& e) {
45                 m->errorOut(e, "CatchAllCommand", "getRequiredParameters");
46                 exit(1);
47         }
48 }
49 //**********************************************************************************************************************
50 vector<string> CatchAllCommand::getRequiredFiles(){     
51         try {
52                 vector<string> myArray;
53                 return myArray;
54         }
55         catch(exception& e) {
56                 m->errorOut(e, "CatchAllCommand", "getRequiredFiles");
57                 exit(1);
58         }
59 }
60 /**************************************************************************************/
61 CatchAllCommand::CatchAllCommand(string option)  {      
62         try {
63                 globaldata = GlobalData::getInstance();
64                 abort = false;
65                 allLines = 1;
66                 
67                 //allow user to run help
68                 if(option == "help") { help(); abort = true; }
69                 
70                 else {
71                         //valid paramters for this command
72                         string Array[] =  {"sabund","label","inputdir","outputdir"};
73                         vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
74                         
75                         OptionParser parser(option);
76                         map<string,string> parameters = parser.getParameters();
77                         
78                         ValidParameters validParameter;
79                         map<string, string>::iterator it;
80                 
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;  }
84                         }
85                         
86                         //initialize outputTypes
87                         vector<string> tempOutNames;
88                         outputTypes["csv"] = tempOutNames;
89                         
90                         //if the user changes the input directory command factory will send this info to us in the output parameter 
91                         string inputDir = validParameter.validFile(parameters, "inputdir", false);              
92                         if (inputDir == "not found"){   inputDir = "";          }
93                         else {
94                                 string path;
95                                 it = parameters.find("sabund");
96                                 //user has given a template file
97                                 if(it != parameters.end()){ 
98                                         path = m->hasPath(it->second);
99                                         //if the user has not given a path then, add inputdir. else leave path alone.
100                                         if (path == "") {       parameters["sabund"] = inputDir + it->second;           }
101                                 }
102                         }
103
104                         //check for required parameters
105                         sabundfile = validParameter.validFile(parameters, "sabund", true);
106                         if (sabundfile == "not open") { sabundfile = ""; abort = true; }
107                         else if (sabundfile == "not found") { sabundfile = "";  m->mothurOut("You must provide a sabund file for the catchall command."); m->mothurOutEndLine(); abort=true; }
108                         else { globaldata->setSabundFile(sabundfile); globaldata->setFormat("sabund"); }
109                         
110                         string label = validParameter.validFile(parameters, "label", false);                    
111                         if (label == "not found") { label = ""; }
112                         else { 
113                                 if(label != "all") {  m->splitAtDash(label, labels);  allLines = 0;  }
114                                 else { allLines = 1;  }
115                         }
116                 
117
118                         //if the user changes the output directory command factory will send this info to us in the output parameter 
119                         outputDir = validParameter.validFile(parameters, "outputdir", false);           if (outputDir == "not found"){  outputDir = m->hasPath(sabundfile);     }
120                 }
121
122         }
123         catch(exception& e) {
124                 m->errorOut(e, "CatchAllCommand", "CatchAllCommand");
125                 exit(1);
126         }
127 }
128 //**********************************************************************************************************************
129
130 void CatchAllCommand::help(){
131         try {
132                 m->mothurOut("The catchall command interfaces mothur with the catchall program written by Linda Woodard, Sean Connolly and John Bunge.\n");
133                 m->mothurOut("For more information about catchall refer to http://www.northeastern.edu/catchall/index.html \n");
134                 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");
135                 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");
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");      
141         }
142         catch(exception& e) {
143                 m->errorOut(e, "CatchAllCommand", "help");
144                 exit(1);
145         }
146 }
147
148 /**************************************************************************************/
149 int CatchAllCommand::execute() {        
150         try {
151                 
152                 if (abort == true) { return 0; }
153                 
154                 //prepare full output directory
155                 outputDir = m->getFullPathName(outputDir);
156                 
157                 //get location of catchall
158                 GlobalData* globaldata = GlobalData::getInstance();
159                 path = globaldata->argv;
160                 path = path.substr(0, (path.find_last_of('m')));
161                 path = m->getFullPathName(path);
162
163                 string catchAllCommandExe = ""; 
164                 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
165                         catchAllCommandExe += "mono " + path + "catchall/CatchAllcmdL.exe ";
166                 #else
167                         catchAllCommandExe += path + "catchall/CatchAllcmdW.exe ";
168                 #endif
169                 
170                 read = new ReadOTUFile(sabundfile);     
171                 read->read(&*globaldata); 
172                 
173                 SAbundVector* sabund = globaldata->sabund;
174                 string lastLabel = sabund->getLabel();
175                 input = globaldata->ginput;
176                                                 
177                 set<string> processedLabels;
178                 set<string> userLabels = labels;
179                 
180                 //for each label the user selected
181                 while((sabund != NULL) && ((allLines == 1) || (userLabels.size() != 0))) {
182
183                                         
184                         if(allLines == 1 || labels.count(sabund->getLabel()) == 1){
185                                         m->mothurOut(sabund->getLabel());  m->mothurOutEndLine();
186                                         
187                                         //create catchall input file from mothur's inputfile
188                                         string filename = process(sabund);
189                                         string outputPath = m->getPathName(filename);
190                                 
191                                         //create system command
192                                         string catchAllCommand = catchAllCommandExe + filename + " " + outputPath + " 1";
193                                 
194                                         //run catchall
195                                         system(catchAllCommand.c_str());
196                                 
197                                         remove(filename.c_str());
198                                 
199                                         filename = m->getRootName(filename); filename = filename.substr(0, filename.length()-1); //rip off extra .
200                                 
201                                         outputNames.push_back(filename + "_Analysis.csv"); outputTypes["csv"].push_back(filename + "_Analysis.csv");
202                                         outputNames.push_back(filename + "_BestModelsAnalysis.csv"); outputTypes["csv"].push_back(filename + "_BestModelsAnalysis.csv");
203                                         outputNames.push_back(filename + "_BestModelsFits.csv"); outputTypes["csv"].push_back(filename + "_BestModelsFits.csv");
204                                         outputNames.push_back(filename + "_BubblePlot.csv"); outputTypes["csv"].push_back(filename + "_BubblePlot.csv");
205                                 
206                                                                                 
207                                         if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) {remove(outputNames[i].c_str()); } delete read;  delete input; globaldata->ginput = NULL; delete sabund;  return 0; }
208
209                                         processedLabels.insert(sabund->getLabel());
210                                         userLabels.erase(sabund->getLabel());
211                         }
212                         
213                         if ((m->anyLabelsToProcess(sabund->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
214                                         string saveLabel = sabund->getLabel();
215                                         
216                                         delete sabund;          
217                                         sabund = (input->getSAbundVector(lastLabel));
218                                         
219                                         m->mothurOut(sabund->getLabel());  m->mothurOutEndLine();
220                                         
221
222                                         //create catchall input file from mothur's inputfile
223                                         string filename = process(sabund);
224                                         string outputPath = m->getPathName(filename);
225                                         
226                                         //create system command
227                                         string catchAllCommand = catchAllCommandExe + filename + " " + outputPath + " 1";
228
229                                         //run catchall
230                                         system(catchAllCommand.c_str());
231                                 
232                                         remove(filename.c_str());
233                                 
234                                         filename = m->getRootName(filename); filename = filename.substr(0, filename.length()-1); //rip off extra .
235                                 
236                                         outputNames.push_back(filename + "_Analysis.csv"); outputTypes["csv"].push_back(filename + "_Analysis.csv");
237                                         outputNames.push_back(filename + "_BestModelsAnalysis.csv"); outputTypes["csv"].push_back(filename + "_BestModelsAnalysis.csv");
238                                         outputNames.push_back(filename + "_BestModelsFits.csv"); outputTypes["csv"].push_back(filename + "_BestModelsFits.csv");
239                                         outputNames.push_back(filename + "_BubblePlot.csv"); outputTypes["csv"].push_back(filename + "_BubblePlot.csv");
240                                 
241                                         if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) {remove(outputNames[i].c_str()); } delete read;  delete input; globaldata->ginput = NULL; delete sabund;  return 0; }
242
243                                         processedLabels.insert(sabund->getLabel());
244                                         userLabels.erase(sabund->getLabel());
245                                         
246                                         //restore real lastlabel to save below
247                                         sabund->setLabel(saveLabel);
248                         }
249                         
250                         
251                         lastLabel = sabund->getLabel(); 
252                         
253                         delete sabund;          
254                         sabund = (input->getSAbundVector());
255                 }
256                 
257                 //output error messages about any remaining user labels
258                 set<string>::iterator it;
259                 bool needToRun = false;
260                 for (it = userLabels.begin(); it != userLabels.end(); it++) {  
261                         m->mothurOut("Your file does not include the label " + *it); 
262                         if (processedLabels.count(lastLabel) != 1) {
263                                 m->mothurOut(". I will use " + lastLabel + ".");  m->mothurOutEndLine();
264                                 needToRun = true;
265                         }else {
266                                 m->mothurOut(". Please refer to " + lastLabel + ".");  m->mothurOutEndLine();
267                         }
268                 }
269                 
270                 //run last label if you need to
271                 if (needToRun == true)  {
272                         if (sabund != NULL) {   delete sabund;  }
273                         sabund = (input->getSAbundVector(lastLabel));
274                         
275                         m->mothurOut(sabund->getLabel());  m->mothurOutEndLine();
276                         
277                         //create catchall input file from mothur's inputfile
278                         string filename = process(sabund);
279                         string outputPath = m->getPathName(filename);
280                         
281                         //create system command
282                         string catchAllCommand = catchAllCommandExe + filename + " " + outputPath + " 1";
283                         
284                         //run catchall
285                         system(catchAllCommand.c_str());
286                         
287                         remove(filename.c_str());
288                         
289                         filename = m->getRootName(filename); filename = filename.substr(0, filename.length()-1); //rip off extra .
290                         
291                         outputNames.push_back(filename + "_Analysis.csv"); outputTypes["csv"].push_back(filename + "_Analysis.csv");
292                         outputNames.push_back(filename + "_BestModelsAnalysis.csv"); outputTypes["csv"].push_back(filename + "_BestModelsAnalysis.csv");
293                         outputNames.push_back(filename + "_BestModelsFits.csv"); outputTypes["csv"].push_back(filename + "_BestModelsFits.csv");
294                         outputNames.push_back(filename + "_BubblePlot.csv"); outputTypes["csv"].push_back(filename + "_BubblePlot.csv");                        
295                         
296                         delete sabund;
297                 }
298
299                 delete read;
300                 delete input; globaldata->ginput = NULL;
301                 
302                 if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) {remove(outputNames[i].c_str()); } return 0; }
303                 
304                 m->mothurOutEndLine();
305                 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
306                 for (int i = 0; i < outputNames.size(); i++) {  m->mothurOut(outputNames[i]); m->mothurOutEndLine();    }       
307                 m->mothurOutEndLine();
308                 
309
310                 return 0;
311         }
312         catch(exception& e) {
313                 m->errorOut(e, "CatchAllCommand", "execute");
314                 exit(1);
315         }
316 }
317 //**********************************************************************************************************************
318 string CatchAllCommand::process(SAbundVector* sabund) {
319         try {
320                 string filename = outputDir + m->getRootName(m->getSimpleName(sabundfile)) + sabund->getLabel() + ".csv";
321                 filename = m->getFullPathName(filename);
322         
323                 ofstream out;
324                 m->openOutputFile(filename, out);
325                 
326                 for (int i = 1; i <= sabund->getMaxRank(); i++) {
327                         int temp = sabund->get(i);
328                         
329                         if (temp != 0) {
330                                 out << i << "," << temp << endl;
331                         }
332                 }
333                 out.close();
334                 
335                 return filename;
336         
337         }
338         catch(exception& e) {
339                 m->errorOut(e, "CatchAllCommand", "process");
340                 exit(1);
341         }
342 }
343 /**************************************************************************************/
344
345
346