]> git.donarmstrong.com Git - mothur.git/blob - catchallcommand.cpp
ace1b00dc0b45e9500d6ba9aa8657fc44b854042
[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                 savedOutputDir = outputDir;
186                 string catchAllCommandExe = ""; 
187                 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
188                         catchAllCommandExe += "mono " + path + "CatchAllcmdL.exe ";
189                         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.
190                 #else
191                         catchAllCommandExe += "\"" + path + "CatchAllcmdW.exe\"" + " ";
192                         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.
193                 #endif
194                 
195                 //prepare full output directory
196                 outputDir = m->getFullPathName(outputDir);
197                 
198                 vector<string> inputFileNames;
199                 if (sharedfile != "") { inputFileNames = parseSharedFile(sharedfile);  globaldata->setFormat("sabund");  }
200                 else {  inputFileNames.push_back(sabundfile);  }                
201                 
202                 for (int p = 0; p < inputFileNames.size(); p++) {
203                         if (inputFileNames.size() > 1) {
204                                 m->mothurOutEndLine(); m->mothurOut("Processing group " + groups[p]); m->mothurOutEndLine(); m->mothurOutEndLine();
205                         }
206                         
207                         InputData* input = new InputData(inputFileNames[p], "sabund");
208                         SAbundVector* sabund = input->getSAbundVector();
209                         string lastLabel = sabund->getLabel();
210                                                         
211                         set<string> processedLabels;
212                         set<string> userLabels = labels;
213                         
214                         string summaryfilename = outputDir + m->getRootName(m->getSimpleName(inputFileNames[p])) + "catchall.summary";
215                         summaryfilename = m->getFullPathName(summaryfilename);
216                         
217                         ofstream out;
218                         m->openOutputFile(summaryfilename, out);        
219                         
220                         out << "label\tmodel\testimate\tlci\tuci" << endl;
221                         
222                         //for each label the user selected
223                         while((sabund != NULL) && ((allLines == 1) || (userLabels.size() != 0))) {
224
225                                                 
226                                 if(allLines == 1 || labels.count(sabund->getLabel()) == 1){
227                                                 m->mothurOut(sabund->getLabel());  m->mothurOutEndLine();
228                                                 
229                                                 //create catchall input file from mothur's inputfile
230                                                 string filename = process(sabund, inputFileNames[p]);
231                                                 string outputPath = m->getPathName(filename);
232                                         
233                                                 //create system command
234                                                 string catchAllCommand = "";
235                                                 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
236                                                         catchAllCommand += catchAllCommandExe + filename + " " + outputPath + " 1";
237                                                 #else
238                                                         if (outputPath.length() > 0) { outputPath = outputPath.substr(0, outputPath.length()-1); }
239                                                         catchAllCommand += catchAllCommandExe + "\"" + filename + "\" \""  + outputPath + "\" 1";
240                                                         //wrap entire string in ""
241                                                         catchAllCommand = "\"" + catchAllCommand + "\"";
242                                                 #endif
243                                         
244                                                 //run catchall
245                                                 system(catchAllCommand.c_str());
246                                         
247                                                 remove(filename.c_str());
248                                         
249                                                 filename = m->getRootName(filename); filename = filename.substr(0, filename.length()-1); //rip off extra .
250                                                 if (savedOutputDir == "") { filename = m->getSimpleName(filename); }
251                                         
252                                                 outputNames.push_back(filename + "_Analysis.csv"); outputTypes["csv"].push_back(filename + "_Analysis.csv");
253                                                 outputNames.push_back(filename + "_BestModelsAnalysis.csv"); outputTypes["csv"].push_back(filename + "_BestModelsAnalysis.csv");
254                                                 outputNames.push_back(filename + "_BestModelsFits.csv"); outputTypes["csv"].push_back(filename + "_BestModelsFits.csv");
255                                                 outputNames.push_back(filename + "_BubblePlot.csv"); outputTypes["csv"].push_back(filename + "_BubblePlot.csv");
256                                         
257                                                 createSummaryFile(filename + "_BestModelsAnalysis.csv", sabund->getLabel(), out);
258                                                                                         
259                                                 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; }
260
261                                                 processedLabels.insert(sabund->getLabel());
262                                                 userLabels.erase(sabund->getLabel());
263                                 }
264                                 
265                                 if ((m->anyLabelsToProcess(sabund->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
266                                                 string saveLabel = sabund->getLabel();
267                                                 
268                                                 delete sabund;          
269                                                 sabund = (input->getSAbundVector(lastLabel));
270                                                 
271                                                 m->mothurOut(sabund->getLabel());  m->mothurOutEndLine();
272                                                 
273
274                                                 //create catchall input file from mothur's inputfile
275                                                 string filename = process(sabund, inputFileNames[p]);
276                                                 string outputPath = m->getPathName(filename);
277                                                 
278                                                 //create system command
279                                                 string catchAllCommand = "";
280                                                 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
281                                                         catchAllCommand += catchAllCommandExe + filename + " " + outputPath + " 1";
282                                                 #else
283                                                         if (outputPath.length() > 0) { outputPath = outputPath.substr(0, outputPath.length()-1); }
284                                                         catchAllCommand += catchAllCommandExe + "\"" + filename + "\" \""  + outputPath + "\" 1";
285                                                         catchAllCommand = "\"" + catchAllCommand + "\"";
286                                                 #endif
287
288                                                 //run catchall
289                                                 system(catchAllCommand.c_str());
290                                         
291                                                 remove(filename.c_str());
292                                         
293                                                 filename = m->getRootName(filename); filename = filename.substr(0, filename.length()-1); //rip off extra .
294                                                 if (savedOutputDir == "") { filename = m->getSimpleName(filename); }
295                                         
296                                                 outputNames.push_back(filename + "_Analysis.csv"); outputTypes["csv"].push_back(filename + "_Analysis.csv");
297                                                 outputNames.push_back(filename + "_BestModelsAnalysis.csv"); outputTypes["csv"].push_back(filename + "_BestModelsAnalysis.csv");
298                                                 outputNames.push_back(filename + "_BestModelsFits.csv"); outputTypes["csv"].push_back(filename + "_BestModelsFits.csv");
299                                                 outputNames.push_back(filename + "_BubblePlot.csv"); outputTypes["csv"].push_back(filename + "_BubblePlot.csv");
300                                         
301                                                 createSummaryFile(filename + "_BestModelsAnalysis.csv", sabund->getLabel(), out);
302                                         
303                                                 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; }
304
305                                                 processedLabels.insert(sabund->getLabel());
306                                                 userLabels.erase(sabund->getLabel());
307                                                 
308                                                 //restore real lastlabel to save below
309                                                 sabund->setLabel(saveLabel);
310                                 }
311                                 
312                                 
313                                 lastLabel = sabund->getLabel(); 
314                                 
315                                 delete sabund;          
316                                 sabund = (input->getSAbundVector());
317                         }
318                         
319                         //output error messages about any remaining user labels
320                         set<string>::iterator it;
321                         bool needToRun = false;
322                         for (it = userLabels.begin(); it != userLabels.end(); it++) {  
323                                 m->mothurOut("Your file does not include the label " + *it); 
324                                 if (processedLabels.count(lastLabel) != 1) {
325                                         m->mothurOut(". I will use " + lastLabel + ".");  m->mothurOutEndLine();
326                                         needToRun = true;
327                                 }else {
328                                         m->mothurOut(". Please refer to " + lastLabel + ".");  m->mothurOutEndLine();
329                                 }
330                         }
331                         
332                         //run last label if you need to
333                         if (needToRun == true)  {
334                                 if (sabund != NULL) {   delete sabund;  }
335                                 sabund = (input->getSAbundVector(lastLabel));
336                                 
337                                 m->mothurOut(sabund->getLabel());  m->mothurOutEndLine();
338                                 
339                                 //create catchall input file from mothur's inputfile
340                                 string filename = process(sabund, inputFileNames[p]);
341                                 string outputPath = m->getPathName(filename);
342                                 
343                                 //create system command
344                                 string catchAllCommand = "";
345                                 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
346                                         catchAllCommand += catchAllCommandExe + filename + " " + outputPath + " 1";
347                                 #else
348                                         if (outputPath.length() > 0) { outputPath = outputPath.substr(0, outputPath.length()-1); }
349                                         catchAllCommand += catchAllCommandExe + "\"" + filename + "\" \""  + outputPath + "\" 1";
350                                         catchAllCommand = "\"" + catchAllCommand + "\"";
351                                 #endif
352                                 
353                                 //run catchall
354                                 system(catchAllCommand.c_str());
355                                 
356                                 remove(filename.c_str());
357                                 
358                                 filename = m->getRootName(filename); filename = filename.substr(0, filename.length()-1); //rip off extra .
359                                 if (savedOutputDir == "") { filename = m->getSimpleName(filename); }
360                                 
361                                 outputNames.push_back(filename + "_Analysis.csv"); outputTypes["csv"].push_back(filename + "_Analysis.csv");
362                                 outputNames.push_back(filename + "_BestModelsAnalysis.csv"); outputTypes["csv"].push_back(filename + "_BestModelsAnalysis.csv");
363                                 outputNames.push_back(filename + "_BestModelsFits.csv"); outputTypes["csv"].push_back(filename + "_BestModelsFits.csv");
364                                 outputNames.push_back(filename + "_BubblePlot.csv"); outputTypes["csv"].push_back(filename + "_BubblePlot.csv");        
365                                 
366                                 createSummaryFile(filename + "_BestModelsAnalysis.csv", sabund->getLabel(), out);
367                                 
368                                 delete sabund;
369                         }
370                         
371                         out.close();
372                         delete input; 
373                         
374                         if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) {remove(outputNames[i].c_str()); } return 0; }
375                                 
376                 }
377                 
378                 if (sharedfile == "") {  
379                         string summaryfilename = savedOutputDir + m->getRootName(m->getSimpleName(inputFileNames[0])) + "catchall.summary";
380                         summaryfilename = m->getFullPathName(summaryfilename);
381                         outputNames.push_back(summaryfilename); outputTypes["summary"].push_back(summaryfilename);
382                 }else { //combine summaries
383                         vector<string> sumNames;
384                         for (int i = 0; i < inputFileNames.size(); i++) {
385                                 sumNames.push_back(m->getFullPathName(outputDir + m->getRootName(m->getSimpleName(inputFileNames[i])) + "catchall.summary"));
386                         }
387                         string summaryfilename = combineSummmary(sumNames);
388                         outputNames.push_back(summaryfilename); outputTypes["summary"].push_back(summaryfilename);
389                         
390                         globaldata->setSharedFile(sharedfile); globaldata->setFormat("sharedfile");
391                 }
392                 
393                 m->mothurOutEndLine();
394                 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
395                 for (int i = 0; i < outputNames.size(); i++) {  m->mothurOut(outputNames[i]); m->mothurOutEndLine();    }       
396                 m->mothurOutEndLine();
397                 
398
399                 return 0;
400         }
401         catch(exception& e) {
402                 m->errorOut(e, "CatchAllCommand", "execute");
403                 exit(1);
404         }
405 }
406 //**********************************************************************************************************************
407 string CatchAllCommand::process(SAbundVector* sabund, string file1) {
408         try {
409                 string filename = outputDir + m->getRootName(m->getSimpleName(file1)) + sabund->getLabel() + ".csv";
410                 filename = m->getFullPathName(filename);
411         
412                 ofstream out;
413                 m->openOutputFile(filename, out);
414                 
415                 for (int i = 1; i <= sabund->getMaxRank(); i++) {
416                         int temp = sabund->get(i);
417                         
418                         if (temp != 0) {
419                                 out << i << "," << temp << endl;
420                         }
421                 }
422                 out.close();
423                 
424                 return filename;
425         
426         }
427         catch(exception& e) {
428                 m->errorOut(e, "CatchAllCommand", "process");
429                 exit(1);
430         }
431 }
432 //*********************************************************************************************************************
433 string CatchAllCommand::combineSummmary(vector<string>& outputNames) {
434         try {
435                 
436                 ofstream out;
437                 string combineFileName = savedOutputDir + m->getRootName(m->getSimpleName(sharedfile)) + "catchall.summary";
438                 
439                 //open combined file
440                 m->openOutputFile(combineFileName, out);
441                 
442                 out << "label\tgroup\tmodel\testimate\tlci\tuci" << endl;
443                 
444                 //open each groups summary file
445                 string newLabel = "";
446                 int numLines = 0;
447                 map<string, vector<string> > files;
448                 for (int i=0; i<outputNames.size(); i++) {
449                         vector<string> thisFilesLines;
450                         
451                         ifstream temp;
452                         m->openInputFile(outputNames[i], temp);
453                         
454                         //read through first line - labels
455                         m->getline(temp);                       
456                         m->gobble(temp);
457                         
458                         //for each label
459                         while (!temp.eof()) {
460                                 
461                                 string thisLine = "";
462                                 string tempLabel;
463                                 
464                                 for (int j = 0; j < 5; j++) {  
465                                         temp >> tempLabel; 
466                                         
467                                         //save for later
468                                         if (j == 1) { thisLine += groups[i] + "\t" + tempLabel + "\t";  }
469                                         else{  thisLine += tempLabel + "\t";    }
470                                 }
471                                 
472                                 thisLine += "\n";
473                                 
474                                 thisFilesLines.push_back(thisLine);
475                                 
476                                 m->gobble(temp);
477                         }
478                         
479                         files[outputNames[i]] = thisFilesLines;
480                         
481                         numLines = thisFilesLines.size();
482                         
483                         temp.close();
484                         remove(outputNames[i].c_str());
485                 }
486                 
487                 //for each label
488                 for (int k = 0; k < numLines; k++) {
489                         
490                         //grab summary data for each group
491                         for (int i=0; i<outputNames.size(); i++) {
492                                 out << files[outputNames[i]][k];
493                         }
494                 }       
495                 
496                 
497                 out.close();
498                 
499                 //return combine file name
500                 return combineFileName;
501                 
502         }
503         catch(exception& e) {
504                 m->errorOut(e, "CatchAllCommand", "combineSummmary");
505                 exit(1);
506         }
507 }
508 //**********************************************************************************************************************
509 int CatchAllCommand::createSummaryFile(string file1, string label, ofstream& out) {
510         try {
511                 
512                 ifstream in;
513                 int able = m->openInputFile(file1, in, "noerror");
514                 
515                 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; }
516                         
517                 if (!in.eof()) {
518                         
519                         string header = m->getline(in); m->gobble(in);
520                         
521                         int pos = header.find("Total Number of Observed Species =");
522                         string numString = "";
523                         
524                         
525                         if (pos == string::npos) { m->mothurOut("[ERROR]: cannot parse " + file1); m->mothurOutEndLine(); }
526                         else {
527                                 //pos will be the position of the T in total, so we want to count to the position of =
528                                 pos += 34;
529                                 char c=header[pos];
530                                 while (c != ','){
531                                         if (c != ' ') {
532                                                 numString += c;
533                                         }
534                                         pos++;
535                                         c=header[pos];
536                                         
537                                         //sanity check
538                                         if (pos > header.length()) { m->mothurOut("Cannot find number of OTUs in " + file1); m->mothurOutEndLine(); in.close(); return 0; }
539                                 }
540                         }
541                                                                                                                           
542                         string firstline = m->getline(in); m->gobble(in);
543                         vector<string> values;
544                         m->splitAtComma(firstline, values);
545                         
546                         values.pop_back(); //last value is always a blank string since the last character in the line is always a ','
547                         
548                         if (values.size() == 1) { //grab next line if firstline didn't have what you wanted
549                                 string secondline = m->getline(in); m->gobble(in);
550                                 values.clear();
551                                 m->splitAtComma(secondline, values);
552                                 
553                                 values.pop_back(); //last value is always a blank string since the last character in the line is always a ','
554                         }
555                         
556                         if (values.size() == 1) { //still not what we wanted fill values with numOTUs
557                                 values.resize(8, "");
558                                 values[1] = "Sobs";
559                                 values[4] = numString;
560                                 values[6] = numString;
561                                 values[7] = numString;
562                         }
563                         
564                         if (values.size() < 8) { values.resize(8, ""); }
565                         
566                         out << label << '\t' << values[1] << '\t' << values[4] << '\t' << values[6] << '\t' << values[7] << endl;
567                 }
568                 
569                 in.close();
570                 
571                 return 0;
572                 
573         }
574         catch(exception& e) {
575                 m->errorOut(e, "CatchAllCommand", "createSummaryFile");
576                 exit(1);
577         }
578 }
579 //**********************************************************************************************************************
580 vector<string> CatchAllCommand::parseSharedFile(string filename) {
581         try {
582                 vector<string> filenames;
583                 
584                 //read first line
585                 InputData* input = new InputData(filename, "sharedfile");
586                 vector<SharedRAbundVector*> lookup = input->getSharedRAbundVectors();
587                 
588                 string sharedFileRoot = outputDir + m->getRootName(m->getSimpleName(filename));
589                 
590                 //clears file before we start to write to it below
591                 for (int i=0; i<lookup.size(); i++) {
592                         remove((sharedFileRoot + lookup[i]->getGroup() + ".sabund").c_str());
593                         filenames.push_back((sharedFileRoot + lookup[i]->getGroup() + ".sabund"));
594                         groups.push_back(lookup[i]->getGroup());
595                 }
596                 
597                 while(lookup[0] != NULL) {
598                         
599                         for (int i = 0; i < lookup.size(); i++) {
600                                 SAbundVector sav = lookup[i]->getSAbundVector();
601                                 ofstream out;
602                                 m->openOutputFileAppend(sharedFileRoot + lookup[i]->getGroup() + ".sabund", out);
603                                 sav.print(out);
604                                 out.close();
605                         }
606                         
607                         for (int i = 0; i < lookup.size(); i++) {  delete lookup[i];  } 
608                         lookup = input->getSharedRAbundVectors();
609                 }
610                 
611                 delete input;
612                 
613                 return filenames;
614         }
615         catch(exception& e) {
616                 m->errorOut(e, "CatchAllCommand", "parseSharedFile");
617                 exit(1);
618         }
619 }
620 /**************************************************************************************/