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