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