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