]> git.donarmstrong.com Git - mothur.git/blob - catchallcommand.cpp
changes while testing
[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
12 //**********************************************************************************************************************
13 vector<string> CatchAllCommand::setParameters(){        
14         try {
15                 CommandParameter plabel("label", "String", "", "", "", "", "","",false,false); parameters.push_back(plabel);
16                 //can choose shared or sabund not both, so put them in the same chooseOnlyOneGroup
17                 CommandParameter pshared("shared", "InputTypes", "", "", "catchallInputs", "catchallInputs", "none","analysis-bestanalysis-models-bubble-summary",false,false,true); parameters.push_back(pshared);
18                 CommandParameter psabund("sabund", "InputTypes", "", "", "catchallInputs", "catchallInputs", "none","analysis-bestanalysis-models-bubble-summary",false,false,true); parameters.push_back(psabund);
19                 CommandParameter pinputdir("inputdir", "String", "", "", "", "", "","",false,false); parameters.push_back(pinputdir);
20                 CommandParameter poutputdir("outputdir", "String", "", "", "", "", "","",false,false); parameters.push_back(poutputdir);
21                 
22                 vector<string> myArray;
23                 for (int i = 0; i < parameters.size(); i++) {   myArray.push_back(parameters[i].name);          }
24                 return myArray;
25         }
26         catch(exception& e) {
27                 m->errorOut(e, "CatchAllCommand", "setParameters");
28                 exit(1);
29         }
30 }
31 //**********************************************************************************************************************
32 string CatchAllCommand::getHelpString(){        
33         try {
34                 string helpString = "";
35                 helpString += "The catchall command interfaces mothur with the catchall program written by Linda Woodard, Sean Connolly and John Bunge.\n";
36                 helpString += "For more information about catchall refer to http://www.northeastern.edu/catchall/index.html \n";
37                 helpString += "The catchall executable must be in the same folder as your mothur executable. \n";
38                 helpString += "If you are a MAC or Linux user you must also have installed mono, a link to mono is on the webpage. \n";
39                 helpString += "The catchall command parameters are shared, sabund and label.  shared or sabund is required. \n";
40                 helpString += "The label parameter is used to analyze specific labels in your input.\n";
41                 helpString += "The catchall command should be in the following format: \n";
42                 helpString += "catchall(sabund=yourSabundFile) \n";
43                 helpString += "Example: catchall(sabund=abrecovery.fn.sabund) \n";      
44                 return helpString;
45         }
46         catch(exception& e) {
47                 m->errorOut(e, "CatchAllCommand", "getHelpString");
48                 exit(1);
49         }
50 }
51 //**********************************************************************************************************************
52 string CatchAllCommand::getOutputPattern(string type) {
53     try {
54         string pattern = "";
55         
56         if (type == "analysis") {  pattern = "[filename],_Analysis.csv"; } 
57         else if (type == "bestanalysis") {  pattern = "[filename],_BestModelsAnalysis.csv"; }
58         else if (type == "models") {  pattern = "[filename],_BestModelsAnalysis.csv"; }
59         else if (type == "bubble") {  pattern = "[filename],_BubblePlot.csv"; }
60         else if (type == "summary") {  pattern =  "[filename],catchall.summary"; }
61         else if (type == "sabund") {  pattern =  "[filename],[distance],csv"; }
62         else { m->mothurOut("[ERROR]: No definition for type " + type + " output pattern.\n"); m->control_pressed = true;  }
63         
64         return pattern;
65     }
66     catch(exception& e) {
67         m->errorOut(e, "CatchAllCommand", "getOutputPattern");
68         exit(1);
69     }
70 }
71 //**********************************************************************************************************************
72 CatchAllCommand::CatchAllCommand(){     
73         try {
74                 abort = true; calledHelp = true;
75                 setParameters();
76                 //initialize outputTypes
77                 vector<string> tempOutNames;
78                 outputTypes["analysis"] = tempOutNames;
79                 outputTypes["bestanalysis"] = tempOutNames;
80         outputTypes["models"] = tempOutNames;
81                 outputTypes["bubble"] = tempOutNames;
82                 outputTypes["summary"] = tempOutNames;
83         outputTypes["sabund"] = tempOutNames;
84         }
85         catch(exception& e) {
86                 m->errorOut(e, "CatchAllCommand", "CatchAllCommand");
87                 exit(1);
88         }
89 }
90 /**************************************************************************************/
91 CatchAllCommand::CatchAllCommand(string option)  {      
92         try {
93                 
94                 abort = false; calledHelp = false;   
95                 allLines = 1;
96                 
97                 //allow user to run help
98                 if(option == "help") { help(); abort = true; calledHelp = true; }
99                 else if(option == "citation") { citation(); abort = true; calledHelp = true;}
100                 
101                 else {
102                         vector<string> myArray = setParameters();
103                         
104                         OptionParser parser(option);
105                         map<string,string> parameters = parser.getParameters();
106                         
107                         ValidParameters validParameter;
108                         map<string, string>::iterator it;
109                 
110                         //check to make sure all parameters are valid for command
111                         for (it = parameters.begin(); it != parameters.end(); it++) { 
112                                 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) {  abort = true;  }
113                         }
114                         
115                         //initialize outputTypes
116                         vector<string> tempOutNames;
117                         outputTypes["analysis"] = tempOutNames;
118             outputTypes["bestanalysis"] = tempOutNames;
119             outputTypes["models"] = tempOutNames;
120             outputTypes["bubble"] = tempOutNames;
121             outputTypes["summary"] = tempOutNames;
122             outputTypes["sabund"] = tempOutNames;
123
124                         
125                         //if the user changes the input directory command factory will send this info to us in the output parameter 
126                         string inputDir = validParameter.validFile(parameters, "inputdir", false);              
127                         if (inputDir == "not found"){   inputDir = "";          }
128                         else {
129                                 string path;
130                                 it = parameters.find("sabund");
131                                 //user has given a template file
132                                 if(it != parameters.end()){ 
133                                         path = m->hasPath(it->second);
134                                         //if the user has not given a path then, add inputdir. else leave path alone.
135                                         if (path == "") {       parameters["sabund"] = inputDir + it->second;           }
136                                 }
137                                 
138                                 it = parameters.find("shared");
139                                 //user has given a template file
140                                 if(it != parameters.end()){ 
141                                         path = m->hasPath(it->second);
142                                         //if the user has not given a path then, add inputdir. else leave path alone.
143                                         if (path == "") {       parameters["shared"] = inputDir + it->second;           }
144                                 }
145                         }
146
147                         //check for required parameters
148                         sabundfile = validParameter.validFile(parameters, "sabund", true);
149                         if (sabundfile == "not open") { sabundfile = ""; abort = true; }
150                         else if (sabundfile == "not found") { sabundfile = "";  }
151                         else { m->setSabundFile(sabundfile); }
152                         
153                         sharedfile = validParameter.validFile(parameters, "shared", true);
154                         if (sharedfile == "not open") { sharedfile = ""; abort = true; }
155                         else if (sharedfile == "not found") { sharedfile = "";   }
156                         else { m->setSharedFile(sharedfile); }
157                         
158                         string label = validParameter.validFile(parameters, "label", false);                    
159                         if (label == "not found") { label = ""; }
160                         else { 
161                                 if(label != "all") {  m->splitAtDash(label, labels);  allLines = 0;  }
162                                 else { allLines = 1;  }
163                         }
164                 
165                         if ((sharedfile == "") && (sabundfile == "")) { 
166                                 //is there are current file available for either of these?
167                                 //give priority to shared, then sabund
168                                 //if there is a current shared file, use it
169                                 sharedfile = m->getSharedFile(); 
170                                 if (sharedfile != "") {  m->mothurOut("Using " + sharedfile + " as input file for the shared parameter."); m->mothurOutEndLine(); }
171                                 else { 
172                                         sabundfile = m->getSabundFile(); 
173                                         if (sabundfile != "") {  m->mothurOut("Using " + sabundfile + " as input file for the sabund parameter."); m->mothurOutEndLine(); }
174                                         else { 
175                                                 m->mothurOut("No valid current files. You must provide a sabund or shared file before you can use the catchall command."); m->mothurOutEndLine(); 
176                                                 abort = true;
177                                         }
178                                 }
179                         }
180                         
181                         //if the user changes the output directory command factory will send this info to us in the output parameter 
182                         outputDir = validParameter.validFile(parameters, "outputdir", false);           
183                         if (outputDir == "not found"){  
184                                 if (sabundfile != "") {  outputDir = m->hasPath(sabundfile); }
185                                 else { outputDir = m->hasPath(sharedfile); }
186                         }
187                 }
188
189         }
190         catch(exception& e) {
191                 m->errorOut(e, "CatchAllCommand", "CatchAllCommand");
192                 exit(1);
193         }
194 }
195 /**************************************************************************************/
196 int CatchAllCommand::execute() {        
197         try {
198                 
199                 if (abort == true) { if (calledHelp) { return 0; }  return 2;   }
200                 
201                 //get location of catchall
202                 path = m->argv;
203                 path = path.substr(0, (path.find_last_of("othur")-5));
204                 path = m->getFullPathName(path);
205                 
206         if (m->debug) { m->mothurOut("[DEBUG]: mothur's path = " + path + "\n"); }
207        
208                 savedOutputDir = outputDir;
209                 string catchAllCommandExe = "";
210         string catchAllTest = "";
211                 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux) || (__linux__) || (__unix__) || (__unix)
212                         if (outputDir == "") { outputDir = "./"; } //force full pathname to be created for catchall, this is necessary because if catchall is in the path it will look for input file whereever the exe is and not the cwd.
213             catchAllTest = path + "CatchAllcmdL.exe";
214                 #else
215                         if (outputDir == "") { outputDir = ".\\"; } //force full pathname to be created for catchall, this is necessary because if catchall is in the path it will look for input file whereever the exe is and not the cwd.
216             catchAllTest = path + "CatchAllcmdW.exe";
217                 #endif
218                 
219         //test to make sure formatdb exists
220                 ifstream in;
221                 catchAllTest = m->getFullPathName(catchAllTest);
222                 int ableToOpen = m->openInputFile(catchAllTest, in, "no error"); in.close();
223                 if(ableToOpen == 1) {   
224             m->mothurOut(catchAllTest + " file does not exist. Checking path... \n");
225             //check to see if uchime is in the path??
226             
227             string programName = "CatchAllcmdW.exe";
228 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux) || (__linux__) || (__unix__) || (__unix)
229             programName = "CatchAllcmdL.exe";
230 #endif
231             string cLocation = m->findProgramPath(programName);
232             
233             ifstream in2;
234             ableToOpen = m->openInputFile(cLocation, in2, "no error"); in2.close();
235
236             if(ableToOpen == 1) { m->mothurOut("[ERROR]: " + cLocation + " file does not exist. mothur requires the catchall executable."); m->mothurOutEndLine();  return 0; } 
237             else {  m->mothurOut("Found catchall in your path, using " + cLocation + "\n"); catchAllTest = cLocation; }
238         }
239         catchAllTest = m->getFullPathName(catchAllTest);
240         
241 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux) || (__linux__) || (__unix__) || (__unix)
242         catchAllCommandExe += "mono \"" + catchAllTest + "\" ";
243 #else
244         catchAllCommandExe += "\"" + catchAllTest + "\" ";
245 #endif
246         
247                 //prepare full output directory
248                 outputDir = m->getFullPathName(outputDir);
249                 
250         if (m->debug) { m->mothurOut("[DEBUG]: catchall location = " + catchAllCommandExe + "\n[DEBUG]: outputDir = " + outputDir + "\n"); }
251         
252                 vector<string> inputFileNames;
253                 if (sharedfile != "") { inputFileNames = parseSharedFile(sharedfile);   }
254                 else {  inputFileNames.push_back(sabundfile);  }                
255                 
256                 for (int p = 0; p < inputFileNames.size(); p++) {
257                         if (inputFileNames.size() > 1) {
258                                 m->mothurOutEndLine(); m->mothurOut("Processing group " + groups[p]); m->mothurOutEndLine(); m->mothurOutEndLine();
259                         }
260                         
261                         InputData input(inputFileNames[p], "sabund");
262                         SAbundVector* sabund = input.getSAbundVector();
263                         string lastLabel = sabund->getLabel();
264                                                         
265                         set<string> processedLabels;
266                         set<string> userLabels = labels;
267                         
268             map<string, string> variables;
269             variables["[filename]"] = outputDir + m->getRootName(m->getSimpleName(inputFileNames[p]));
270                         string summaryfilename = getOutputFileName("summary", variables);
271                         summaryfilename = m->getFullPathName(summaryfilename);
272                         
273             if (m->debug) { m->mothurOut("[DEBUG]: Input File = " + inputFileNames[p] + ".\n[DEBUG]: inputdata address = " + toString(&input) + ".\n[DEBUG]: sabund address = " + toString(&sabund) + ".\n"); } 
274             
275                         ofstream out;
276                         m->openOutputFile(summaryfilename, out);        
277                         
278                         out << "label\tmodel\testimate\tlci\tuci" << endl;
279             
280             if (m->debug) { string open = "no"; if (out.is_open()) { open = "yes"; } m->mothurOut("[DEBUG]: output stream is open = " + open + ".\n"); }
281                         
282                         //for each label the user selected
283                         while((sabund != NULL) && ((allLines == 1) || (userLabels.size() != 0))) {
284                                 
285                                                 
286                                 if(allLines == 1 || labels.count(sabund->getLabel()) == 1){
287                                                 m->mothurOut(sabund->getLabel());  m->mothurOutEndLine();
288                                                 
289                                                 //create catchall input file from mothur's inputfile
290                                                 string filename = process(sabund, inputFileNames[p]);
291                                                 string outputPath = m->getPathName(filename);
292                                                                                         
293                                                 //create system command
294                                                 string catchAllCommand = "";
295                                                 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux) || (__linux__) || (__unix__) || (__unix)
296                                                         catchAllCommand += catchAllCommandExe + "\"" + filename + "\" \""  + outputPath + + "\" 1";
297                                                 #else
298                             //removes extra '\\' catchall doesnt like that
299                             vector<string> tempNames;
300                             string tempFilename = filename;
301                             m->splitAtDash(tempFilename, tempNames);
302                             tempFilename = tempNames[0];
303                             tempNames.clear();
304                             string tempOutputPath = outputPath;
305                             m->splitAtDash(tempOutputPath, tempNames);
306                             tempOutputPath = tempNames[0];
307                             if (tempOutputPath.length() > 0) { tempOutputPath = tempOutputPath.substr(0, tempOutputPath.length()-1); }
308                             catchAllCommand += catchAllCommandExe + "\"" + tempFilename + "\" \""  + tempOutputPath + "\" 1";
309                             catchAllCommand = "\"" + catchAllCommand + "\"";
310                                                 #endif
311                         
312                         if (m->debug) {  m->mothurOut("[DEBUG]: catchall command = " + catchAllCommand + ". About to call system.\n"); }
313                     
314                                                 //run catchall
315                                                 system(catchAllCommand.c_str());
316                     
317                         if (m->debug) { m->mothurOut("[DEBUG]: back from system call. Keeping file: " + filename + ".\n"); }
318                     
319                         if (!m->debug) { m->mothurRemove(filename); }
320
321                                         
322                                                 filename = m->getRootName(filename); filename = filename.substr(0, filename.length()-1); //rip off extra .
323                                                 if (savedOutputDir == "") { filename = m->getSimpleName(filename); }
324                                         
325                         variables["[filename]"] = filename;
326                                                 outputNames.push_back(getOutputFileName("analysis", variables)); outputTypes["analysis"].push_back(getOutputFileName("analysis", variables));
327                                                 outputNames.push_back(getOutputFileName("bestanalysis", variables)); outputTypes["bestanalysis"].push_back(getOutputFileName("bestanalysis", variables));
328                         outputNames.push_back(getOutputFileName("models", variables)); outputTypes["models"].push_back(getOutputFileName("models", variables));
329                         outputNames.push_back(getOutputFileName("bubble", variables)); outputTypes["bubble"].push_back(getOutputFileName("bubble", variables));
330                         
331                         if (m->debug) { m->mothurOut("[DEBUG]: About to create summary file for: " + filename + ".\n[DEBUG]: sabund label = " + sabund->getLabel() + ".\n"); }
332                     
333                                                 createSummaryFile(filename + "_BestModelsAnalysis.csv", sabund->getLabel(), out);
334                     
335                         if (m->debug) { m->mothurOut("[DEBUG]: Done creating summary file.\n"); }
336                                                                                         
337                                                 if (m->control_pressed) { out.close(); for (int i = 0; i < outputNames.size(); i++) {m->mothurRemove(outputNames[i]);   }  delete sabund;  return 0; }
338
339                                                 processedLabels.insert(sabund->getLabel());
340                                                 userLabels.erase(sabund->getLabel());
341                                 }
342                                 
343                                 if ((m->anyLabelsToProcess(sabund->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
344                                                 string saveLabel = sabund->getLabel();
345                                                 
346                                                 delete sabund;          
347                                                 sabund = (input.getSAbundVector(lastLabel));
348                                                 
349                                                 m->mothurOut(sabund->getLabel());  m->mothurOutEndLine();
350                                                 
351
352                                                 //create catchall input file from mothur's inputfile
353                                                 string filename = process(sabund, inputFileNames[p]);
354                                                 string outputPath = m->getPathName(filename);
355                                                                                         
356                                                 //create system command
357                                                 string catchAllCommand = "";
358                                                 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux) || (__linux__) || (__unix__) || (__unix)
359                             catchAllCommand += catchAllCommandExe + "\"" + filename + "\" \""  + outputPath + + "\" 1";
360                                                 #else
361                             //removes extra '\\' catchall doesnt like that
362                             vector<string> tempNames;
363                             string tempFilename = filename;
364                             m->splitAtDash(tempFilename, tempNames);
365                             tempFilename = tempNames[0];
366                             tempNames.clear();
367                             string tempOutputPath = outputPath;
368                             m->splitAtDash(tempOutputPath, tempNames);
369                             tempOutputPath = tempNames[0];
370                                                         if (tempOutputPath.length() > 0) { tempOutputPath = tempOutputPath.substr(0, tempOutputPath.length()-1); }
371                                                         catchAllCommand += catchAllCommandExe + "\"" + tempFilename + "\" \""  + tempOutputPath + "\" 1";
372                                                         catchAllCommand = "\"" + catchAllCommand + "\"";
373                                                 #endif
374                         
375                         if (m->debug) {  m->mothurOut("[DEBUG]: catchall command = " + catchAllCommand + ". About to call system.\n"); }
376                     
377                                                 //run catchall
378                                                 system(catchAllCommand.c_str());
379                     
380                         if (m->debug) { m->mothurOut("[DEBUG]: back from system call. Keeping file: " + filename + ".\n"); }
381                     
382                         if (!m->debug) { m->mothurRemove(filename); }
383
384                                         
385                                                 filename = m->getRootName(filename); filename = filename.substr(0, filename.length()-1); //rip off extra .
386                                                 if (savedOutputDir == "") { filename = m->getSimpleName(filename); }
387                                         
388                         variables["[filename]"] = filename;
389                         outputNames.push_back(getOutputFileName("analysis", variables)); outputTypes["analysis"].push_back(getOutputFileName("analysis", variables));
390                         outputNames.push_back(getOutputFileName("bestanalysis", variables)); outputTypes["bestanalysis"].push_back(getOutputFileName("bestanalysis", variables));
391                         outputNames.push_back(getOutputFileName("models", variables)); outputTypes["models"].push_back(getOutputFileName("models", variables));
392                         outputNames.push_back(getOutputFileName("bubble", variables)); outputTypes["bubble"].push_back(getOutputFileName("bubble", variables));
393                         
394                     
395                         if (m->debug) { m->mothurOut("[DEBUG]: About to create summary file for: " + filename + ".\n[DEBUG]: sabund label = " + sabund->getLabel() + ".\n"); }
396                     
397                                                 createSummaryFile(filename + "_BestModelsAnalysis.csv", sabund->getLabel(), out);
398                     
399                         if (m->debug) { m->mothurOut("[DEBUG]: Done creating summary file.\n"); }
400                                         
401                                                 if (m->control_pressed) { out.close(); for (int i = 0; i < outputNames.size(); i++) {m->mothurRemove(outputNames[i]);   }   delete sabund;  return 0; }
402
403                                                 processedLabels.insert(sabund->getLabel());
404                                                 userLabels.erase(sabund->getLabel());
405                                                 
406                                                 //restore real lastlabel to save below
407                                                 sabund->setLabel(saveLabel);
408                                 }
409                                 
410                                 
411                                 lastLabel = sabund->getLabel(); 
412                                 
413                                 delete sabund;          
414                                 sabund = (input.getSAbundVector());
415                         }
416                         
417                         //output error messages about any remaining user labels
418                         set<string>::iterator it;
419                         bool needToRun = false;
420                         for (it = userLabels.begin(); it != userLabels.end(); it++) {  
421                                 m->mothurOut("Your file does not include the label " + *it); 
422                                 if (processedLabels.count(lastLabel) != 1) {
423                                         m->mothurOut(". I will use " + lastLabel + ".");  m->mothurOutEndLine();
424                                         needToRun = true;
425                                 }else {
426                                         m->mothurOut(". Please refer to " + lastLabel + ".");  m->mothurOutEndLine();
427                                 }
428                         }
429                         
430                         //run last label if you need to
431                         if (needToRun == true)  {
432                                 if (sabund != NULL) {   delete sabund;  }
433                                 sabund = (input.getSAbundVector(lastLabel));
434                                 
435                                 m->mothurOut(sabund->getLabel());  m->mothurOutEndLine();
436                                 
437                                 //create catchall input file from mothur's inputfile
438                                 string filename = process(sabund, inputFileNames[p]);
439                                 string outputPath = m->getPathName(filename);
440                                 
441                                 //create system command
442                                 string catchAllCommand = "";
443                                 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux) || (__linux__) || (__unix__) || (__unix)
444                     catchAllCommand += catchAllCommandExe + "\"" + filename + "\" \""  + outputPath + + "\" 1";
445                                 #else
446                     //removes extra '\\' catchall doesnt like that
447                     vector<string> tempNames;
448                     string tempFilename = filename;
449                     m->splitAtDash(tempFilename, tempNames);
450                     tempFilename = tempNames[0];
451                     tempNames.clear();
452                     string tempOutputPath = outputPath;
453                     m->splitAtDash(tempOutputPath, tempNames);
454                     tempOutputPath = tempNames[0];
455                     if (tempOutputPath.length() > 0) { tempOutputPath = tempOutputPath.substr(0, tempOutputPath.length()-1); }
456                     catchAllCommand += catchAllCommandExe + "\"" + tempFilename + "\" \""  + tempOutputPath + "\" 1";
457                     catchAllCommand = "\"" + catchAllCommand + "\"";
458                                 #endif
459                 
460                 if (m->debug) {  m->mothurOut("[DEBUG]: catchall command = " + catchAllCommand + ". About to call system.\n"); }
461                                 
462                                 //run catchall
463                                 system(catchAllCommand.c_str());
464                 
465                 if (m->debug) { m->mothurOut("[DEBUG]: back from system call. Keeping file: " + filename + ".\n"); }
466                                 
467                                 if (!m->debug) { m->mothurRemove(filename); }
468                                 
469                                 filename = m->getRootName(filename); filename = filename.substr(0, filename.length()-1); //rip off extra .
470                                 if (savedOutputDir == "") { filename = m->getSimpleName(filename); }
471                                 
472                                 variables["[filename]"] = filename;
473                 outputNames.push_back(getOutputFileName("analysis", variables)); outputTypes["analysis"].push_back(getOutputFileName("analysis", variables));
474                 outputNames.push_back(getOutputFileName("bestanalysis", variables)); outputTypes["bestanalysis"].push_back(getOutputFileName("bestanalysis", variables));
475                 outputNames.push_back(getOutputFileName("models", variables)); outputTypes["models"].push_back(getOutputFileName("models", variables));
476                 outputNames.push_back(getOutputFileName("bubble", variables)); outputTypes["bubble"].push_back(getOutputFileName("bubble", variables));
477                 if (m->debug) { m->mothurOut("[DEBUG]: About to create summary file for: " + filename + ".\n[DEBUG]: sabund label = " + sabund->getLabel() + ".\n"); }
478                 
479                                 createSummaryFile(filename + "_BestModelsAnalysis.csv", sabund->getLabel(), out);
480                 
481                 if (m->debug) { m->mothurOut("[DEBUG]: Done creating summary file.\n"); }
482                                 
483                                 delete sabund;
484                         }
485                         
486                         out.close();
487                         
488                         if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) {m->mothurRemove(outputNames[i]);        } return 0; }
489                                 
490                 }
491                 
492                 if (sharedfile == "") {  
493                         string summaryfilename = savedOutputDir + m->getRootName(m->getSimpleName(inputFileNames[0])) + "catchall.summary";
494                         summaryfilename = m->getFullPathName(summaryfilename);
495                         outputNames.push_back(summaryfilename); outputTypes["summary"].push_back(summaryfilename);
496                 }else { //combine summaries
497                         vector<string> sumNames;
498                         for (int i = 0; i < inputFileNames.size(); i++) {
499                                 sumNames.push_back(m->getFullPathName(outputDir + m->getRootName(m->getSimpleName(inputFileNames[i])) + "catchall.summary"));
500                         }
501                         string summaryfilename = combineSummmary(sumNames);
502                         outputNames.push_back(summaryfilename); outputTypes["summary"].push_back(summaryfilename);
503                 }
504                 
505                 m->mothurOutEndLine();
506                 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
507                 for (int i = 0; i < outputNames.size(); i++) {  m->mothurOut(outputNames[i]); m->mothurOutEndLine();    }       
508                 m->mothurOutEndLine();
509                 
510
511                 return 0;
512         }
513         catch(exception& e) {
514                 m->errorOut(e, "CatchAllCommand", "execute");
515                 exit(1);
516         }
517 }
518 //**********************************************************************************************************************
519 string CatchAllCommand::process(SAbundVector* sabund, string file1) {
520         try {
521         map<string, string> variables;
522         variables["[filename]"] = outputDir + m->getRootName(m->getSimpleName(file1));
523         variables["[distance]"] = sabund->getLabel();
524         string filename = getOutputFileName("sabund", variables);
525                 filename = m->getFullPathName(filename);
526         
527                 ofstream out;
528                 m->openOutputFile(filename, out);
529                 
530         if (m->debug) { m->mothurOut("[DEBUG]: Creating " + filename + " file for catchall, shown below.\n\n"); }
531         
532                 for (int i = 1; i <= sabund->getMaxRank(); i++) {
533                         int temp = sabund->get(i);
534                         
535                         if (temp != 0) {
536                                 out << i << "," << temp << endl;
537                 if (m->debug) { m->mothurOut(toString(i) + "," + toString(temp) + "\n"); }
538                         }
539                 }
540                 out.close();
541         
542         if (m->debug) { m->mothurOut("[DEBUG]: Done creating " + filename + " file for catchall, shown above.\n\n"); }
543                 
544                 return filename;
545         
546         }
547         catch(exception& e) {
548                 m->errorOut(e, "CatchAllCommand", "process");
549                 exit(1);
550         }
551 }
552 //*********************************************************************************************************************
553 string CatchAllCommand::combineSummmary(vector<string>& outputNames) {
554         try {
555                 
556                 ofstream out;
557         map<string, string> variables;
558         variables["[filename]"] = savedOutputDir + m->getRootName(m->getSimpleName(sharedfile));
559         string combineFileName = getOutputFileName("summary", variables);
560                 
561                 //open combined file
562                 m->openOutputFile(combineFileName, out);
563                 
564                 out << "label\tgroup\tmodel\testimate\tlci\tuci" << endl;
565                 
566                 //open each groups summary file
567                 string newLabel = "";
568                 int numLines = 0;
569                 map<string, vector<string> > files;
570                 for (int i=0; i<outputNames.size(); i++) {
571                         vector<string> thisFilesLines;
572                         
573                         ifstream temp;
574                         m->openInputFile(outputNames[i], temp);
575                         
576                         //read through first line - labels
577                         m->getline(temp);                       
578                         m->gobble(temp);
579                         
580                         //for each label
581                         while (!temp.eof()) {
582                                 
583                                 string thisLine = "";
584                                 string tempLabel;
585                                 
586                                 for (int j = 0; j < 5; j++) {  
587                                         temp >> tempLabel; 
588                                         
589                                         //save for later
590                                         if (j == 1) { thisLine += groups[i] + "\t" + tempLabel + "\t";  }
591                                         else{  thisLine += tempLabel + "\t";    }
592                                 }
593                                 
594                                 thisLine += "\n";
595                                 
596                                 thisFilesLines.push_back(thisLine);
597                                 
598                                 m->gobble(temp);
599                         }
600                         
601                         files[outputNames[i]] = thisFilesLines;
602                         
603                         numLines = thisFilesLines.size();
604                         
605                         temp.close();
606                         m->mothurRemove(outputNames[i]);
607                 }
608                 
609                 //for each label
610                 for (int k = 0; k < numLines; k++) {
611                         
612                         //grab summary data for each group
613                         for (int i=0; i<outputNames.size(); i++) {
614                                 out << files[outputNames[i]][k];
615                         }
616                 }       
617                 
618                 
619                 out.close();
620                 
621                 //return combine file name
622                 return combineFileName;
623                 
624         }
625         catch(exception& e) {
626                 m->errorOut(e, "CatchAllCommand", "combineSummmary");
627                 exit(1);
628         }
629 }
630 //**********************************************************************************************************************
631 int CatchAllCommand::createSummaryFile(string file1, string label, ofstream& out) {
632         try {
633                 
634                 ifstream in;
635                 int able = m->openInputFile(file1, in, "noerror");
636                 
637                 if (able == 1) {  m->mothurOut("[ERROR]: the catchall program did not run properly. Please check to make sure it is located in the same folder as your mothur executable.");m->mothurOutEndLine();  m->control_pressed = true; return 0; }
638                         
639                 if (!in.eof()) {
640                         
641                         string header = m->getline(in); m->gobble(in);
642                         
643                         int pos = header.find("Total Number of Observed Species =");
644                         string numString = "";
645                         
646                         
647                         if (pos == string::npos) { m->mothurOut("[ERROR]: cannot parse " + file1); m->mothurOutEndLine(); }
648                         else {
649                                 //pos will be the position of the T in total, so we want to count to the position of =
650                                 pos += 34;
651                                 char c=header[pos];
652                                 while (c != ','){
653                                         if (c != ' ') {
654                                                 numString += c;
655                                         }
656                                         pos++;
657                                         c=header[pos];
658                                         
659                                         //sanity check
660                                         if (pos > header.length()) { m->mothurOut("Cannot find number of OTUs in " + file1); m->mothurOutEndLine(); in.close(); return 0; }
661                                 }
662                         }
663                                                                                                                           
664                         string firstline = m->getline(in); m->gobble(in);
665                         vector<string> values;
666                         m->splitAtComma(firstline, values);
667                         
668                         values.pop_back(); //last value is always a blank string since the last character in the line is always a ','
669                         
670                         if (values.size() == 1) { //grab next line if firstline didn't have what you wanted
671                                 string secondline = m->getline(in); m->gobble(in);
672                                 values.clear();
673                                 m->splitAtComma(secondline, values);
674                                 
675                                 values.pop_back(); //last value is always a blank string since the last character in the line is always a ','
676                         }
677                         
678                         if (values.size() == 1) { //still not what we wanted fill values with numOTUs
679                                 values.resize(8, "");
680                                 values[1] = "Sobs";
681                                 values[4] = numString;
682                                 values[6] = numString;
683                                 values[7] = numString;
684                         }
685                         
686                         if (values.size() < 8) { values.resize(8, ""); }
687                         
688                         out << label << '\t' << values[1] << '\t' << values[4] << '\t' << values[6] << '\t' << values[7] << endl;
689                 }
690                 
691                 in.close();
692                 
693                 return 0;
694                 
695         }
696         catch(exception& e) {
697                 m->errorOut(e, "CatchAllCommand", "createSummaryFile");
698                 exit(1);
699         }
700 }
701 //**********************************************************************************************************************
702 vector<string> CatchAllCommand::parseSharedFile(string filename) {
703         try {
704                 vector<string> filenames;
705                 
706                 //read first line
707                 InputData input(filename, "sharedfile");
708                 vector<SharedRAbundVector*> lookup = input.getSharedRAbundVectors();
709                 
710                 string sharedFileRoot = outputDir + m->getRootName(m->getSimpleName(filename));
711                 
712                 //clears file before we start to write to it below
713                 for (int i=0; i<lookup.size(); i++) {
714                         m->mothurRemove((sharedFileRoot + lookup[i]->getGroup() + ".sabund"));
715                         filenames.push_back((sharedFileRoot + lookup[i]->getGroup() + ".sabund"));
716                         groups.push_back(lookup[i]->getGroup());
717                 }
718                 
719                 while(lookup[0] != NULL) {
720                         
721                         for (int i = 0; i < lookup.size(); i++) {
722                                 SAbundVector sav = lookup[i]->getSAbundVector();
723                                 ofstream out;
724                                 m->openOutputFileAppend(sharedFileRoot + lookup[i]->getGroup() + ".sabund", out);
725                                 sav.print(out);
726                                 out.close();
727                         }
728                         
729                         for (int i = 0; i < lookup.size(); i++) {  delete lookup[i];  } 
730                         lookup = input.getSharedRAbundVectors();
731                 }
732                 
733                 return filenames;
734         }
735         catch(exception& e) {
736                 m->errorOut(e, "CatchAllCommand", "parseSharedFile");
737                 exit(1);
738         }
739 }
740 /**************************************************************************************/