]> git.donarmstrong.com Git - mothur.git/blob - catchallcommand.cpp
moved utilities out of mothur.h and into mothurOut class.
[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 CatchAllCommand::CatchAllCommand(string option)  {      
15         try {
16                 globaldata = GlobalData::getInstance();
17                 abort = false;
18                 allLines = 0;
19                 
20                 //allow user to run help
21                 if(option == "help") { help(); abort = true; }
22                 
23                 else {
24                         //valid paramters for this command
25                         string Array[] =  {"sabund","label","inputdir","outputdir"};
26                         vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
27                         
28                         OptionParser parser(option);
29                         map<string,string> parameters = parser.getParameters();
30                         
31                         ValidParameters validParameter;
32                         map<string, string>::iterator it;
33                 
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;  }
37                         }
38                         
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 = "";          }
42                         else {
43                                 string path;
44                                 it = parameters.find("sabund");
45                                 //user has given a template file
46                                 if(it != parameters.end()){ 
47                                         path = m->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;           }
50                                 }
51                         }
52
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"); }
58                         
59                         string label = validParameter.validFile(parameters, "label", false);                    
60                         if (label == "not found") { label = ""; }
61                         else { 
62                                 if(label != "all") {  m->splitAtDash(label, labels);  allLines = 0;  }
63                                 else { allLines = 1;  }
64                         }
65                 
66
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 = "./";       }
69                 }
70
71         }
72         catch(exception& e) {
73                 m->errorOut(e, "CatchAllCommand", "CatchAllCommand");
74                 exit(1);
75         }
76 }
77 //**********************************************************************************************************************
78
79 void CatchAllCommand::help(){
80         try {
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");      
89         }
90         catch(exception& e) {
91                 m->errorOut(e, "CatchAllCommand", "help");
92                 exit(1);
93         }
94 }
95
96 /**************************************************************************************/
97 int CatchAllCommand::execute() {        
98         try {
99                 
100                 if (abort == true) { return 0; }
101                 
102                 vector<string> outputNames;
103                 
104                 //prepare full output directory
105                 outputDir = m->getFullPathName(outputDir);
106                 
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 = m->getFullPathName(path);
112
113                 string catchAllCommandExe = ""; 
114                 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
115                         catchAllCommandExe += "mono " + path + "catchall/CatchAllcmdL.exe ";
116                 #else
117                         catchAllCommandExe += path + "catchall/CatchAllcmdW.exe ";
118                 #endif
119                 
120                 read = new ReadOTUFile(sabundfile);     
121                 read->read(&*globaldata); 
122                 
123                 SAbundVector* sabund = globaldata->sabund;
124                 string lastLabel = sabund->getLabel();
125                 input = globaldata->ginput;
126                                                 
127                 set<string> processedLabels;
128                 set<string> userLabels = labels;
129                 
130                 //for each label the user selected
131                 while((sabund != NULL) && ((allLines == 1) || (userLabels.size() != 0))) {
132
133                                         
134                         if(allLines == 1 || labels.count(sabund->getLabel()) == 1){
135                                         m->mothurOut(sabund->getLabel());  m->mothurOutEndLine();
136                                         
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
146
147                                         //create catchall input file from mothur's inputfile
148                                         string filename = process(sabund);
149         sabund->print(cout);                                                    
150                                         //create system command
151                                         string catchAllCommand = catchAllCommandExe + filename + " " + path;
152 cout << catchAllCommand << endl;
153                                         //run catchall
154                                         system(catchAllCommand.c_str());
155                                                                                 
156                                         if (m->control_pressed) { delete read;  delete input; globaldata->ginput = NULL; delete sabund;  return 0; }
157
158                                         processedLabels.insert(sabund->getLabel());
159                                         userLabels.erase(sabund->getLabel());
160                         }
161                         
162                         if ((m->anyLabelsToProcess(sabund->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
163                                         string saveLabel = sabund->getLabel();
164                                         
165                                         delete sabund;          
166                                         sabund = (input->getSAbundVector(lastLabel));
167                                         
168                                         m->mothurOut(sabund->getLabel());  m->mothurOutEndLine();
169                                         
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
179
180                                         
181                                         //create catchall input file from mothur's inputfile
182                                         string filename = process(sabund);
183                         
184                                         //create system command
185                                         string catchAllCommand = catchAllCommandExe + filename + " " + path;
186 cout << catchAllCommand << endl;
187                                         //run catchall
188                                         system(catchAllCommand.c_str());
189
190                                         if (m->control_pressed) { delete read;  delete input; globaldata->ginput = NULL; delete sabund;  return 0; }
191
192                                         processedLabels.insert(sabund->getLabel());
193                                         userLabels.erase(sabund->getLabel());
194                                         
195                                         //restore real lastlabel to save below
196                                         sabund->setLabel(saveLabel);
197                         }
198                         
199                         
200                         lastLabel = sabund->getLabel(); 
201                         
202                         delete sabund;          
203                         sabund = (input->getSAbundVector());
204                 }
205                 
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();
213                                 needToRun = true;
214                         }else {
215                                 m->mothurOut(". Please refer to " + lastLabel + ".");  m->mothurOutEndLine();
216                         }
217                 }
218                 
219                 //run last label if you need to
220                 if (needToRun == true)  {
221                         if (sabund != NULL) {   delete sabund;  }
222                         sabund = (input->getSAbundVector(lastLabel));
223                         
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
234
235                         //create catchall input file from mothur's inputfile
236                         string filename = process(sabund);
237
238                         //create system command
239                         string catchAllCommand = catchAllCommandExe + filename + " " + path;
240 cout << catchAllCommand << endl;
241                         //run catchall
242                         system(catchAllCommand.c_str());
243         
244                         
245                         delete sabund;
246                 }
247
248                 delete read;
249                 delete input; globaldata->ginput = NULL;
250                 
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();
255                 
256
257                 return 0;
258         }
259         catch(exception& e) {
260                 m->errorOut(e, "CatchAllCommand", "execute");
261                 exit(1);
262         }
263 }
264 //**********************************************************************************************************************
265 string CatchAllCommand::process(SAbundVector* sabund) {
266         try {
267                 string filename = outputDir + m->getRootName(m->getSimpleName(sabundfile)) + sabund->getLabel() + ".catchall";
268                 filename = m->getFullPathName(filename);
269         
270                 ofstream out;
271                 m->openOutputFile(filename, out);
272                 
273                 for (int i = 1; i <= sabund->getMaxRank(); i++) {
274                         int temp = sabund->get(i);
275                         
276                         if (temp != 0) {
277                                 out << i << "," << temp << endl;
278                         }
279                 }
280                 out.close();
281                 
282                 return filename;
283         
284         }
285         catch(exception& e) {
286                 m->errorOut(e, "CatchAllCommand", "process");
287                 exit(1);
288         }
289 }
290 /**************************************************************************************/
291
292
293