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