]> git.donarmstrong.com Git - mothur.git/blob - catchallcommand.cpp
finished wrapper for catchall
[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                 
30                 //need to determine outputFIles and types
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                         
87                         //initialize outputTypes
88                         //need to determine outputFIles and types
89                         
90                         
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 = "";          }
94                         else {
95                                 string path;
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;           }
102                                 }
103                         }
104
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"); }
110                         
111                         string label = validParameter.validFile(parameters, "label", false);                    
112                         if (label == "not found") { label = ""; }
113                         else { 
114                                 if(label != "all") {  m->splitAtDash(label, labels);  allLines = 0;  }
115                                 else { allLines = 1;  }
116                         }
117                 
118
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 = m->hasPath(sabundfile);     }
121                 }
122
123         }
124         catch(exception& e) {
125                 m->errorOut(e, "CatchAllCommand", "CatchAllCommand");
126                 exit(1);
127         }
128 }
129 //**********************************************************************************************************************
130
131 void CatchAllCommand::help(){
132         try {
133                 m->mothurOut("The catchall command interfaces mothur with the catchall program written by Linda Woodard, Sean Connolly and John Bunge.\n");
134                 m->mothurOut("For more information about catchall refer to http://www.northeastern.edu/catchall/index.html \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("If you are a MAC or Linux user you must also have installed mono, a link to mono is on the webpage. \n");
137                 m->mothurOut("The catchall command parameters are sabund and label, sabund is required. \n");
138                 m->mothurOut("The label parameter is used to analyze specific labels in your input.\n");
139                 m->mothurOut("The catchall command should be in the following format: \n");
140                 m->mothurOut("catchall(sabund=yourSabundFile) \n");
141                 m->mothurOut("Example: catchall(sabund=abrecovery.fn.sabund) \n");      
142         }
143         catch(exception& e) {
144                 m->errorOut(e, "CatchAllCommand", "help");
145                 exit(1);
146         }
147 }
148
149 /**************************************************************************************/
150 int CatchAllCommand::execute() {        
151         try {
152                 
153                 if (abort == true) { return 0; }
154                 
155                 //prepare full output directory
156                 outputDir = m->getFullPathName(outputDir);
157                 
158                 //get location of catchall
159                 GlobalData* globaldata = GlobalData::getInstance();
160                 path = globaldata->argv;
161                 path = path.substr(0, (path.find_last_of('m')));
162                 path = m->getFullPathName(path);
163
164                 string catchAllCommandExe = ""; 
165                 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
166                         catchAllCommandExe += "mono " + path + "catchall/CatchAllcmdL.exe ";
167                 #else
168                         catchAllCommandExe += path + "catchall/CatchAllcmdW.exe ";
169                 #endif
170                 
171                 read = new ReadOTUFile(sabundfile);     
172                 read->read(&*globaldata); 
173                 
174                 SAbundVector* sabund = globaldata->sabund;
175                 string lastLabel = sabund->getLabel();
176                 input = globaldata->ginput;
177                                                 
178                 set<string> processedLabels;
179                 set<string> userLabels = labels;
180                 
181                 //for each label the user selected
182                 while((sabund != NULL) && ((allLines == 1) || (userLabels.size() != 0))) {
183
184                                         
185                         if(allLines == 1 || labels.count(sabund->getLabel()) == 1){
186                                         m->mothurOut(sabund->getLabel());  m->mothurOutEndLine();
187                                         
188                                         //create catchall input file from mothur's inputfile
189                                         string filename = process(sabund);
190                                         string outputPath = m->getPathName(filename);
191                                 
192                                         //create system command
193                                         string catchAllCommand = catchAllCommandExe + filename + " " + outputPath + " 1";
194                                 
195                                         //run catchall
196                                         system(catchAllCommand.c_str());
197                                 
198                                         remove(filename.c_str());
199                                 
200                                         filename = m->getRootName(filename); filename = filename.substr(0, filename.length()-1); //rip off extra .
201                                 
202                                         outputNames.push_back(filename + "_Analysis.csv");
203                                         outputNames.push_back(filename + "_BestModelsAnalysis.csv");
204                                         outputNames.push_back(filename + "_BestModelsFits.csv");
205                                         outputNames.push_back(filename + "_BubblePlot.csv");
206                                 
207                                                                                 
208                                         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; }
209
210                                         processedLabels.insert(sabund->getLabel());
211                                         userLabels.erase(sabund->getLabel());
212                         }
213                         
214                         if ((m->anyLabelsToProcess(sabund->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
215                                         string saveLabel = sabund->getLabel();
216                                         
217                                         delete sabund;          
218                                         sabund = (input->getSAbundVector(lastLabel));
219                                         
220                                         m->mothurOut(sabund->getLabel());  m->mothurOutEndLine();
221                                         
222
223                                         //create catchall input file from mothur's inputfile
224                                         string filename = process(sabund);
225                                         string outputPath = m->getPathName(filename);
226                                         
227                                         //create system command
228                                         string catchAllCommand = catchAllCommandExe + filename + " " + outputPath + " 1";
229
230                                         //run catchall
231                                         system(catchAllCommand.c_str());
232                                 
233                                         remove(filename.c_str());
234                                 
235                                         filename = m->getRootName(filename); filename = filename.substr(0, filename.length()-1); //rip off extra .
236                                 
237                                         outputNames.push_back(filename + "_Analysis.csv");
238                                         outputNames.push_back(filename + "_BestModelsAnalysis.csv");
239                                         outputNames.push_back(filename + "_BestModelsFits.csv");
240                                         outputNames.push_back(filename + "_BubblePlot.csv");
241
242                                         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; }
243
244                                         processedLabels.insert(sabund->getLabel());
245                                         userLabels.erase(sabund->getLabel());
246                                         
247                                         //restore real lastlabel to save below
248                                         sabund->setLabel(saveLabel);
249                         }
250                         
251                         
252                         lastLabel = sabund->getLabel(); 
253                         
254                         delete sabund;          
255                         sabund = (input->getSAbundVector());
256                 }
257                 
258                 //output error messages about any remaining user labels
259                 set<string>::iterator it;
260                 bool needToRun = false;
261                 for (it = userLabels.begin(); it != userLabels.end(); it++) {  
262                         m->mothurOut("Your file does not include the label " + *it); 
263                         if (processedLabels.count(lastLabel) != 1) {
264                                 m->mothurOut(". I will use " + lastLabel + ".");  m->mothurOutEndLine();
265                                 needToRun = true;
266                         }else {
267                                 m->mothurOut(". Please refer to " + lastLabel + ".");  m->mothurOutEndLine();
268                         }
269                 }
270                 
271                 //run last label if you need to
272                 if (needToRun == true)  {
273                         if (sabund != NULL) {   delete sabund;  }
274                         sabund = (input->getSAbundVector(lastLabel));
275                         
276                         m->mothurOut(sabund->getLabel());  m->mothurOutEndLine();
277                         
278                         //create catchall input file from mothur's inputfile
279                         string filename = process(sabund);
280                         string outputPath = m->getPathName(filename);
281                         
282                         //create system command
283                         string catchAllCommand = catchAllCommandExe + filename + " " + outputPath + " 1";
284                         
285                         //run catchall
286                         system(catchAllCommand.c_str());
287                         
288                         remove(filename.c_str());
289                         
290                         filename = m->getRootName(filename); filename = filename.substr(0, filename.length()-1); //rip off extra .
291                         
292                         outputNames.push_back(filename + "_Analysis.csv");
293                         outputNames.push_back(filename + "_BestModelsAnalysis.csv");
294                         outputNames.push_back(filename + "_BestModelsFits.csv");
295                         outputNames.push_back(filename + "_BubblePlot.csv");
296                         
297                         
298                         delete sabund;
299                 }
300
301                 delete read;
302                 delete input; globaldata->ginput = NULL;
303                 
304                 if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) {remove(outputNames[i].c_str()); } return 0; }
305                 
306                 m->mothurOutEndLine();
307                 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
308                 for (int i = 0; i < outputNames.size(); i++) {  m->mothurOut(outputNames[i]); m->mothurOutEndLine();    }       
309                 m->mothurOutEndLine();
310                 
311
312                 return 0;
313         }
314         catch(exception& e) {
315                 m->errorOut(e, "CatchAllCommand", "execute");
316                 exit(1);
317         }
318 }
319 //**********************************************************************************************************************
320 string CatchAllCommand::process(SAbundVector* sabund) {
321         try {
322                 string filename = outputDir + m->getRootName(m->getSimpleName(sabundfile)) + sabund->getLabel() + ".csv";
323                 filename = m->getFullPathName(filename);
324         
325                 ofstream out;
326                 m->openOutputFile(filename, out);
327                 
328                 for (int i = 1; i <= sabund->getMaxRank(); i++) {
329                         int temp = sabund->get(i);
330                         
331                         if (temp != 0) {
332                                 out << i << "," << temp << endl;
333                         }
334                 }
335                 out.close();
336                 
337                 return filename;
338         
339         }
340         catch(exception& e) {
341                 m->errorOut(e, "CatchAllCommand", "process");
342                 exit(1);
343         }
344 }
345 /**************************************************************************************/
346
347
348