]> git.donarmstrong.com Git - mothur.git/blob - summarycommand.cpp
added getCommandInfoCommand for gui
[mothur.git] / summarycommand.cpp
1 /*
2  *  summarycommand.cpp
3  *  Dotur
4  *
5  *  Created by Sarah Westcott on 1/2/09.
6  *  Copyright 2009 Schloss Lab UMASS Amherst. All rights reserved.
7  *
8  */
9
10 #include "summarycommand.h"
11 #include "ace.h"
12 #include "sobs.h"
13 #include "nseqs.h"
14 #include "chao1.h"
15 #include "bootstrap.h"
16 #include "simpson.h"
17 #include "simpsoneven.h"
18 #include "invsimpson.h"
19 #include "npshannon.h"
20 #include "shannon.h"
21 #include "heip.h"
22 #include "smithwilson.h"
23 #include "shannoneven.h"
24 #include "jackknife.h"
25 #include "geom.h"
26 #include "logsd.h"
27 #include "qstat.h"
28 #include "bergerparker.h"
29 #include "bstick.h"
30 #include "goodscoverage.h"
31 #include "coverage.h"
32 #include "efron.h"
33 #include "boneh.h"
34 #include "solow.h"
35 #include "shen.h"
36
37 //**********************************************************************************************************************
38 vector<string> SummaryCommand::setParameters(){ 
39         try {
40                 CommandParameter plist("list", "InputTypes", "", "", "LRSS", "LRSS", "none",false,false); parameters.push_back(plist);
41                 CommandParameter prabund("rabund", "InputTypes", "", "", "LRSS", "LRSS", "none",false,false); parameters.push_back(prabund);
42                 CommandParameter psabund("sabund", "InputTypes", "", "", "LRSS", "LRSS", "none",false,false); parameters.push_back(psabund);
43                 CommandParameter pshared("shared", "InputTypes", "", "", "LRSS", "LRSS", "none",false,false); parameters.push_back(pshared);
44                 CommandParameter plabel("label", "String", "", "", "", "", "",false,false); parameters.push_back(plabel);
45                 CommandParameter pcalc("calc", "Multiple", "sobs-chao-nseqs-coverage-ace-jack-shannon-shannoneven-np_shannon-heip-smithwilson-simpson-simpsoneven-invsimpson-bootstrap-geometric-qstat-logseries-bergerparker-bstick-goodscoverage-efron-boneh-solow-shen", "sobs-chao-ace-jack-shannon-npshannon-simpson", "", "", "",true,false); parameters.push_back(pcalc);
46                 CommandParameter pabund("abund", "Number", "", "10", "", "", "",false,false); parameters.push_back(pabund);
47                 CommandParameter psize("size", "Number", "", "0", "", "", "",false,false); parameters.push_back(psize);
48                 CommandParameter pgroupmode("groupmode", "Boolean", "", "T", "", "", "",false,false); parameters.push_back(pgroupmode);
49                 CommandParameter pinputdir("inputdir", "String", "", "", "", "", "",false,false); parameters.push_back(pinputdir);
50                 CommandParameter poutputdir("outputdir", "String", "", "", "", "", "",false,false); parameters.push_back(poutputdir);
51                 
52                 vector<string> myArray;
53                 for (int i = 0; i < parameters.size(); i++) {   myArray.push_back(parameters[i].name);          }
54                 return myArray;
55         }
56         catch(exception& e) {
57                 m->errorOut(e, "SummaryCommand", "setParameters");
58                 exit(1);
59         }
60 }
61 //**********************************************************************************************************************
62 string SummaryCommand::getHelpString(){ 
63         try {
64                 string helpString = "";
65                 ValidCalculators validCalculator;
66                 helpString += "The summary.single command parameters are list, sabund, rabund, shared, label, calc, abund and groupmode.  list, sabund, rabund or shared is required unless you have a valid current file.\n";
67                 helpString += "The summary.single command should be in the following format: \n";
68                 helpString += "summary.single(label=yourLabel, calc=yourEstimators).\n";
69                 helpString += "Example summary.single(label=unique-.01-.03, calc=sobs-chao-ace-jack-bootstrap-shannon-npshannon-simpson).\n";
70                 helpString += validCalculator.printCalc("summary");
71                 helpString += "The default value calc is sobs-chao-ace-jack-shannon-npshannon-simpson\n";
72                 helpString += "If you are running summary.single with a shared file and would like your summary results collated in one file, set groupmode=t. (Default=true).\n";
73                 helpString += "The label parameter is used to analyze specific labels in your input.\n";
74                 helpString += "Note: No spaces between parameter labels (i.e. label), '=' and parameters (i.e.yourLabels).\n";
75                 return helpString;
76         }
77         catch(exception& e) {
78                 m->errorOut(e, "SummaryCommand", "getHelpString");
79                 exit(1);
80         }
81 }
82
83 //**********************************************************************************************************************
84 SummaryCommand::SummaryCommand(){       
85         try {
86                 abort = true; calledHelp = true; 
87                 setParameters();
88                 vector<string> tempOutNames;
89                 outputTypes["summary"] = tempOutNames;
90         }
91         catch(exception& e) {
92                 m->errorOut(e, "SummaryCommand", "SummaryCommand");
93                 exit(1);
94         }
95 }
96 //**********************************************************************************************************************
97
98 SummaryCommand::SummaryCommand(string option)  {
99         try {
100                 abort = false; calledHelp = false;   
101                 allLines = 1;
102                                 
103                 //allow user to run help
104                 if(option == "help") {  help();  abort = true; calledHelp = true; }
105                 
106                 else {
107                         vector<string> myArray = setParameters();
108                         
109                         OptionParser parser(option);
110                         map<string,string> parameters = parser.getParameters();
111                         map<string,string>::iterator it;
112                         
113                         ValidParameters validParameter;
114                         
115                         //check to make sure all parameters are valid for command
116                         for (map<string,string>::iterator it = parameters.begin(); it != parameters.end(); it++) { 
117                                 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) {  abort = true;  }
118                         }
119                         
120                         //initialize outputTypes
121                         vector<string> tempOutNames;
122                         outputTypes["summary"] = tempOutNames;
123                         
124                         //if the user changes the input directory command factory will send this info to us in the output parameter 
125                         string inputDir = validParameter.validFile(parameters, "inputdir", false);              
126                         if (inputDir == "not found"){   inputDir = "";          }
127                         else {
128                                 string path;
129                                 it = parameters.find("shared");
130                                 //user has given a template file
131                                 if(it != parameters.end()){ 
132                                         path = m->hasPath(it->second);
133                                         //if the user has not given a path then, add inputdir. else leave path alone.
134                                         if (path == "") {       parameters["shared"] = inputDir + it->second;           }
135                                 }
136                                 
137                                 it = parameters.find("rabund");
138                                 //user has given a template file
139                                 if(it != parameters.end()){ 
140                                         path = m->hasPath(it->second);
141                                         //if the user has not given a path then, add inputdir. else leave path alone.
142                                         if (path == "") {       parameters["rabund"] = inputDir + it->second;           }
143                                 }
144                                 
145                                 it = parameters.find("sabund");
146                                 //user has given a template file
147                                 if(it != parameters.end()){ 
148                                         path = m->hasPath(it->second);
149                                         //if the user has not given a path then, add inputdir. else leave path alone.
150                                         if (path == "") {       parameters["sabund"] = inputDir + it->second;           }
151                                 }
152                                 
153                                 it = parameters.find("list");
154                                 //user has given a template file
155                                 if(it != parameters.end()){ 
156                                         path = m->hasPath(it->second);
157                                         //if the user has not given a path then, add inputdir. else leave path alone.
158                                         if (path == "") {       parameters["list"] = inputDir + it->second;             }
159                                 }
160                         }
161                         
162                         //check for required parameters
163                         listfile = validParameter.validFile(parameters, "list", true);
164                         if (listfile == "not open") { listfile = ""; abort = true; }
165                         else if (listfile == "not found") { listfile = ""; }
166                         else {  format = "list"; inputfile = listfile; }
167                         
168                         sabundfile = validParameter.validFile(parameters, "sabund", true);
169                         if (sabundfile == "not open") { sabundfile = ""; abort = true; }        
170                         else if (sabundfile == "not found") { sabundfile = ""; }
171                         else {  format = "sabund"; inputfile = sabundfile; }
172                         
173                         rabundfile = validParameter.validFile(parameters, "rabund", true);
174                         if (rabundfile == "not open") { rabundfile = ""; abort = true; }        
175                         else if (rabundfile == "not found") { rabundfile = ""; }
176                         else {  format = "rabund"; inputfile = rabundfile; }
177                         
178                         sharedfile = validParameter.validFile(parameters, "shared", true);
179                         if (sharedfile == "not open") { sharedfile = ""; abort = true; }        
180                         else if (sharedfile == "not found") { sharedfile = ""; }
181                         else {  format = "sharedfile"; inputfile = sharedfile; }
182                         
183                         if ((sharedfile == "") && (listfile == "") && (rabundfile == "") && (sabundfile == "")) { 
184                                 //is there are current file available for any of these?
185                                 //give priority to shared, then list, then rabund, then sabund
186                                 //if there is a current shared file, use it
187                                 sharedfile = m->getSharedFile(); 
188                                 if (sharedfile != "") { inputfile = sharedfile; format = "sharedfile"; m->mothurOut("Using " + sharedfile + " as input file for the shared parameter."); m->mothurOutEndLine(); }
189                                 else { 
190                                         listfile = m->getListFile(); 
191                                         if (listfile != "") { inputfile = listfile; format = "list"; m->mothurOut("Using " + listfile + " as input file for the list parameter."); m->mothurOutEndLine(); }
192                                         else { 
193                                                 rabundfile = m->getRabundFile(); 
194                                                 if (rabundfile != "") { inputfile = rabundfile; format = "rabund"; m->mothurOut("Using " + rabundfile + " as input file for the rabund parameter."); m->mothurOutEndLine(); }
195                                                 else { 
196                                                         sabundfile = m->getSabundFile(); 
197                                                         if (sabundfile != "") { inputfile = sabundfile; format = "sabund"; m->mothurOut("Using " + sabundfile + " as input file for the sabund parameter."); m->mothurOutEndLine(); }
198                                                         else { 
199                                                                 m->mothurOut("No valid current files. You must provide a list, sabund, rabund or shared file before you can use the collect.single command."); m->mothurOutEndLine(); 
200                                                                 abort = true;
201                                                         }
202                                                 }
203                                         }
204                                 }
205                         }
206                         
207                         //if the user changes the output directory command factory will send this info to us in the output parameter 
208                         outputDir = validParameter.validFile(parameters, "outputdir", false);           if (outputDir == "not found"){  outputDir = m->hasPath(inputfile);              }
209
210                         //check for optional parameter and set defaults
211                         // ...at some point should added some additional type checking...
212                         label = validParameter.validFile(parameters, "label", false);                   
213                         if (label == "not found") { label = ""; }
214                         else { 
215                                 if(label != "all") {  m->splitAtDash(label, labels);  allLines = 0;  }
216                                 else { allLines = 1;  }
217                         }
218                                 
219                         calc = validParameter.validFile(parameters, "calc", false);                     
220                         if (calc == "not found") { calc = "sobs-chao-ace-jack-shannon-npshannon-simpson";  }
221                         else { 
222                                  if (calc == "default")  {  calc = "sobs-chao-ace-jack-shannon-npshannon-simpson";  }
223                         }
224                         m->splitAtDash(calc, Estimators);
225
226                         string temp;
227                         temp = validParameter.validFile(parameters, "abund", false);            if (temp == "not found") { temp = "10"; }
228                         convert(temp, abund); 
229                         
230                         temp = validParameter.validFile(parameters, "size", false);                     if (temp == "not found") { temp = "0"; }
231                         convert(temp, size); 
232                         
233                         temp = validParameter.validFile(parameters, "groupmode", false);                if (temp == "not found") { temp = "T"; }
234                         groupMode = m->isTrue(temp);
235                         
236         
237                 }
238         }
239         catch(exception& e) {
240                 m->errorOut(e, "SummaryCommand", "SummaryCommand");
241                 exit(1);
242         }
243 }
244 //**********************************************************************************************************************
245
246 int SummaryCommand::execute(){
247         try {
248         
249                 if (abort == true) { if (calledHelp) { return 0; }  return 2;   }
250                 
251                 if ((format != "sharedfile")) { inputFileNames.push_back(inputfile);  }
252                 else {  inputFileNames = parseSharedFile(sharedfile);  format = "rabund"; }
253                 
254                 if (m->control_pressed) { return 0; }
255                 
256                 int numLines = 0;
257                 int numCols = 0;
258                 
259                 for (int p = 0; p < inputFileNames.size(); p++) {
260                         
261                         numLines = 0;
262                         numCols = 0;
263                         
264                         string fileNameRoot = outputDir + m->getRootName(m->getSimpleName(inputFileNames[p])) + "summary";
265                         outputNames.push_back(fileNameRoot); outputTypes["summary"].push_back(fileNameRoot);
266                         
267                         if (inputFileNames.size() > 1) {
268                                 m->mothurOutEndLine(); m->mothurOut("Processing group " + groups[p]); m->mothurOutEndLine(); m->mothurOutEndLine();
269                         }
270                         
271                         sumCalculators.clear();
272                         
273                         ValidCalculators validCalculator;
274                         
275                         for (int i=0; i<Estimators.size(); i++) {
276                                 if (validCalculator.isValidCalculator("summary", Estimators[i]) == true) { 
277                                         if(Estimators[i] == "sobs"){
278                                                 sumCalculators.push_back(new Sobs());
279                                         }else if(Estimators[i] == "chao"){
280                                                 sumCalculators.push_back(new Chao1());
281                                         }else if(Estimators[i] == "coverage"){
282                                                 sumCalculators.push_back(new Coverage());
283                                         }else if(Estimators[i] == "geometric"){
284                                                 sumCalculators.push_back(new Geom());
285                                         }else if(Estimators[i] == "logseries"){
286                                                 sumCalculators.push_back(new LogSD());
287                                         }else if(Estimators[i] == "qstat"){
288                                                 sumCalculators.push_back(new QStat());
289                                         }else if(Estimators[i] == "bergerparker"){
290                                                 sumCalculators.push_back(new BergerParker());
291                                         }else if(Estimators[i] == "bstick"){
292                                                 sumCalculators.push_back(new BStick());
293                                         }else if(Estimators[i] == "ace"){
294                                                 if(abund < 5)
295                                                         abund = 10;
296                                                 sumCalculators.push_back(new Ace(abund));
297                                         }else if(Estimators[i] == "jack"){
298                                                 sumCalculators.push_back(new Jackknife());
299                                         }else if(Estimators[i] == "shannon"){
300                                                 sumCalculators.push_back(new Shannon());
301                                         }else if(Estimators[i] == "shannoneven"){
302                                                 sumCalculators.push_back(new ShannonEven());
303                                         }else if(Estimators[i] == "npshannon"){
304                                                 sumCalculators.push_back(new NPShannon());
305                                         }else if(Estimators[i] == "heip"){
306                                                 sumCalculators.push_back(new Heip());
307                                         }else if(Estimators[i] == "smithwilson"){
308                                                 sumCalculators.push_back(new SmithWilson());
309                                         }else if(Estimators[i] == "simpson"){
310                                                 sumCalculators.push_back(new Simpson());
311                                         }else if(Estimators[i] == "simpsoneven"){
312                                                 sumCalculators.push_back(new SimpsonEven());
313                                         }else if(Estimators[i] == "invsimpson"){
314                                                 sumCalculators.push_back(new InvSimpson());
315                                         }else if(Estimators[i] == "bootstrap"){
316                                                 sumCalculators.push_back(new Bootstrap());
317                                         }else if (Estimators[i] == "nseqs") { 
318                                                 sumCalculators.push_back(new NSeqs());
319                                         }else if (Estimators[i] == "goodscoverage") { 
320                                                 sumCalculators.push_back(new GoodsCoverage());
321                                         }else if (Estimators[i] == "efron") { 
322                                                 sumCalculators.push_back(new Efron(size));
323                                         }else if (Estimators[i] == "boneh") { 
324                                                 sumCalculators.push_back(new Boneh(size));
325                                         }else if (Estimators[i] == "solow") { 
326                                                 sumCalculators.push_back(new Solow(size));
327                                         }else if (Estimators[i] == "shen") { 
328                                                 sumCalculators.push_back(new Shen(size, abund));
329                                         }
330                                 }
331                         }
332                         
333                         //if the users entered no valid calculators don't execute command
334                         if (sumCalculators.size() == 0) {  for (int i = 0; i < outputNames.size(); i++) {       remove(outputNames[i].c_str());  } return 0; }
335                         
336                         ofstream outputFileHandle;
337                         m->openOutputFile(fileNameRoot, outputFileHandle);
338                         outputFileHandle << "label";
339                 
340                         input = new InputData(inputFileNames[p], format);
341                         sabund = input->getSAbundVector();
342                         string lastLabel = sabund->getLabel();
343                 
344                         for(int i=0;i<sumCalculators.size();i++){
345                                 if(sumCalculators[i]->getCols() == 1){
346                                         outputFileHandle << '\t' << sumCalculators[i]->getName();
347                                         numCols++;
348                                 }
349                                 else{
350                                         outputFileHandle << '\t' << sumCalculators[i]->getName() << "\t" << sumCalculators[i]->getName() << "_lci\t" << sumCalculators[i]->getName() << "_hci";
351                                         numCols += 3;
352                                 }
353                         }
354                         outputFileHandle << endl;
355                         
356                         //if the users enters label "0.06" and there is no "0.06" in their file use the next lowest label.
357                         set<string> processedLabels;
358                         set<string> userLabels = labels;
359                         
360                         if (m->control_pressed) {  outputFileHandle.close(); for (int i = 0; i < outputNames.size(); i++) {     remove(outputNames[i].c_str());  } for(int i=0;i<sumCalculators.size();i++){  delete sumCalculators[i]; }  delete sabund;  delete input;  return 0;  }
361                         
362                         while((sabund != NULL) && ((allLines == 1) || (userLabels.size() != 0))) {
363                                 
364                                 if (m->control_pressed) { outputFileHandle.close(); for (int i = 0; i < outputNames.size(); i++) {      remove(outputNames[i].c_str());  } for(int i=0;i<sumCalculators.size();i++){  delete sumCalculators[i]; }  delete sabund;  delete input;  return 0;  }
365                                 
366                                 if(allLines == 1 || labels.count(sabund->getLabel()) == 1){                     
367                                         
368                                         m->mothurOut(sabund->getLabel()); m->mothurOutEndLine();
369                                         processedLabels.insert(sabund->getLabel());
370                                         userLabels.erase(sabund->getLabel());
371                                         
372                                         outputFileHandle << sabund->getLabel();
373                                         for(int i=0;i<sumCalculators.size();i++){
374                                                 vector<double> data = sumCalculators[i]->getValues(sabund);
375                                                 
376                                                 if (m->control_pressed) { outputFileHandle.close(); for (int i = 0; i < outputNames.size(); i++) {      remove(outputNames[i].c_str());  } for(int i=0;i<sumCalculators.size();i++){  delete sumCalculators[i]; }  delete sabund;  delete input;  return 0;  }
377
378                                                 outputFileHandle << '\t';
379                                                 sumCalculators[i]->print(outputFileHandle);
380                                         }
381                                         outputFileHandle << endl;
382                                         numLines++;
383                                 }
384                                 
385                                 if ((m->anyLabelsToProcess(sabund->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
386                                         string saveLabel = sabund->getLabel();
387                                         
388                                         delete sabund;
389                                         sabund = input->getSAbundVector(lastLabel);
390                                         
391                                         m->mothurOut(sabund->getLabel()); m->mothurOutEndLine();
392                                         processedLabels.insert(sabund->getLabel());
393                                         userLabels.erase(sabund->getLabel());
394                                         
395                                         outputFileHandle << sabund->getLabel();
396                                         for(int i=0;i<sumCalculators.size();i++){
397                                                 vector<double> data = sumCalculators[i]->getValues(sabund);
398                                                 
399                                                 if (m->control_pressed) {  outputFileHandle.close(); for (int i = 0; i < outputNames.size(); i++) {     remove(outputNames[i].c_str());  } for(int i=0;i<sumCalculators.size();i++){  delete sumCalculators[i]; } delete sabund;  delete input;  return 0;  }
400                                                 
401                                                 outputFileHandle << '\t';
402                                                 sumCalculators[i]->print(outputFileHandle);
403                                         }
404                                         outputFileHandle << endl;
405                                         numLines++;
406                                         
407                                         //restore real lastlabel to save below
408                                         sabund->setLabel(saveLabel);
409                                 }               
410                                 
411                                 lastLabel = sabund->getLabel();                 
412                                 
413                                 delete sabund;
414                                 sabund = input->getSAbundVector();
415                         }
416                         
417                         if (m->control_pressed) {  outputFileHandle.close(); for (int i = 0; i < outputNames.size(); i++) {     remove(outputNames[i].c_str());  } for(int i=0;i<sumCalculators.size();i++){  delete sumCalculators[i]; }   delete input;  return 0;  }
418
419                         //output error messages about any remaining user labels
420                         set<string>::iterator it;
421                         bool needToRun = false;
422                         for (it = userLabels.begin(); it != userLabels.end(); it++) {  
423                                 m->mothurOut("Your file does not include the label " + *it); 
424                                 if (processedLabels.count(lastLabel) != 1) {
425                                         m->mothurOut(". I will use " + lastLabel + "."); m->mothurOutEndLine();
426                                         needToRun = true;
427                                 }else {
428                                         m->mothurOut(". Please refer to " + lastLabel + "."); m->mothurOutEndLine();
429                                 }
430                         }
431                         
432                         //run last label if you need to
433                         if (needToRun == true)  {
434                                 if (sabund != NULL) {   delete sabund;  }
435                                 sabund = input->getSAbundVector(lastLabel);
436                                 
437                                 m->mothurOut(sabund->getLabel()); m->mothurOutEndLine();
438                                 outputFileHandle << sabund->getLabel();
439                                 for(int i=0;i<sumCalculators.size();i++){
440                                         vector<double> data = sumCalculators[i]->getValues(sabund);
441                                         
442                                         if (m->control_pressed) {  outputFileHandle.close(); for (int i = 0; i < outputNames.size(); i++) {     remove(outputNames[i].c_str());  } for(int i=0;i<sumCalculators.size();i++){  delete sumCalculators[i]; }  delete sabund;  delete input; return 0;  }
443
444                                         outputFileHandle << '\t';
445                                         sumCalculators[i]->print(outputFileHandle);
446                                 }
447                                 outputFileHandle << endl;
448                                 numLines++;
449                                 delete sabund;
450                         }
451                         
452                         outputFileHandle.close();
453                         
454                         if (m->control_pressed) {  for (int i = 0; i < outputNames.size(); i++) {       remove(outputNames[i].c_str());  } for(int i=0;i<sumCalculators.size();i++){  delete sumCalculators[i]; }   delete input;  return 0;  }
455
456                         
457                         delete input;  
458                         for(int i=0;i<sumCalculators.size();i++){  delete sumCalculators[i]; }
459                 }
460                 
461                 if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) {        remove(outputNames[i].c_str());  }  return 0;  }
462                 
463                 //create summary file containing all the groups data for each label - this function just combines the info from the files already created.
464                 if ((sharedfile != "") && (groupMode)) {   outputNames.push_back(createGroupSummaryFile(numLines, numCols, outputNames));  }
465                 
466                 if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) {        remove(outputNames[i].c_str());  }  return 0;  }
467                 
468                 m->mothurOutEndLine();
469                 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
470                 for (int i = 0; i < outputNames.size(); i++) {  m->mothurOut(outputNames[i]); m->mothurOutEndLine();    }
471                 m->mothurOutEndLine();
472                 
473                 return 0;
474         }
475         catch(exception& e) {
476                 m->errorOut(e, "SummaryCommand", "execute");
477                 exit(1);
478         }
479 }
480 //**********************************************************************************************************************
481 vector<string> SummaryCommand::parseSharedFile(string filename) {
482         try {
483                 vector<string> filenames;
484                 
485                 map<string, ofstream*> filehandles;
486                 map<string, ofstream*>::iterator it3;
487                 
488                 input = new InputData(filename, "sharedfile");
489                 vector<SharedRAbundVector*> lookup = input->getSharedRAbundVectors();
490                 
491                 string sharedFileRoot = m->getRootName(filename);
492                 
493                 //clears file before we start to write to it below
494                 for (int i=0; i<lookup.size(); i++) {
495                         remove((sharedFileRoot + lookup[i]->getGroup() + ".rabund").c_str());
496                         filenames.push_back((sharedFileRoot + lookup[i]->getGroup() + ".rabund"));
497                 }
498                 
499                 ofstream* temp;
500                 for (int i=0; i<lookup.size(); i++) {
501                         temp = new ofstream;
502                         filehandles[lookup[i]->getGroup()] = temp;
503                         groups.push_back(lookup[i]->getGroup());
504                 }
505
506                 while(lookup[0] != NULL) {
507                 
508                         for (int i = 0; i < lookup.size(); i++) {
509                                 RAbundVector rav = lookup[i]->getRAbundVector();
510                                 m->openOutputFileAppend(sharedFileRoot + lookup[i]->getGroup() + ".rabund", *(filehandles[lookup[i]->getGroup()]));
511                                 rav.print(*(filehandles[lookup[i]->getGroup()]));
512                                 (*(filehandles[lookup[i]->getGroup()])).close();
513                         }
514                 
515                         for (int i = 0; i < lookup.size(); i++) {  delete lookup[i];  } 
516                         lookup = input->getSharedRAbundVectors();
517                 }
518                 
519                 //free memory
520                 for (it3 = filehandles.begin(); it3 != filehandles.end(); it3++) {
521                         delete it3->second;
522                 }
523                 
524                 delete input;
525
526                 return filenames;
527         }
528         catch(exception& e) {
529                 m->errorOut(e, "SummaryCommand", "parseSharedFile");
530                 exit(1);
531         }
532 }
533 //**********************************************************************************************************************
534 string SummaryCommand::createGroupSummaryFile(int numLines, int numCols, vector<string>& outputNames) {
535         try {
536                 
537                 ofstream out;
538                 string combineFileName = outputDir + m->getRootName(m->getSimpleName(sharedfile)) + "groups.summary";
539                 
540                 //open combined file
541                 m->openOutputFile(combineFileName, out);
542                 
543                 //open each groups summary file
544                 string newLabel = "";
545                 map<string, vector<string> > files;
546                 for (int i=0; i<outputNames.size(); i++) {
547                         vector<string> thisFilesLines;
548                         
549                         ifstream temp;
550                         m->openInputFile(outputNames[i], temp);
551                         
552                         //read through first line - labels
553                         string tempLabel;
554                         if (i == 0) { //we want to save the labels to output below
555                                 for (int j = 0; j < numCols+1; j++) {  
556                                         temp >> tempLabel; 
557                                         
558                                         if (j == 1) {  newLabel += "group\t" + tempLabel + '\t';
559                                         }else{  newLabel += tempLabel + '\t';   }
560                                 }
561                         }else{  for (int j = 0; j < numCols+1; j++) {  temp >> tempLabel;  }  }
562                         
563                         m->gobble(temp);
564                         
565                         //for each label
566                         for (int k = 0; k < numLines; k++) {
567                                 
568                                 string thisLine = "";
569                                 string tempLabel;
570                                         
571                                 for (int j = 0; j < numCols+1; j++) {  
572                                         temp >> tempLabel; 
573                                                 
574                                         //save for later
575                                         if (j == 1) { thisLine += groups[i] + "\t" + tempLabel + "\t";  }
576                                         else{  thisLine += tempLabel + "\t";    }
577                                 }
578                                         
579                                 thisLine += "\n";
580                                 
581                                 thisFilesLines.push_back(thisLine);
582                                         
583                                 m->gobble(temp);
584                         }
585                                 
586                         files[outputNames[i]] = thisFilesLines;
587                         
588                         temp.close();
589                         remove(outputNames[i].c_str());
590                 }
591                 
592                 //output label line to new file
593                 out << newLabel << endl;
594                 
595                 //for each label
596                 for (int k = 0; k < numLines; k++) {
597                 
598                         //grab summary data for each group
599                         for (int i=0; i<outputNames.size(); i++) {
600                                 out << files[outputNames[i]][k];
601                         }
602                 }       
603                 
604                 outputNames.clear();
605                 
606                 out.close();
607                 
608                 //return combine file name
609                 return combineFileName;
610                 
611         }
612         catch(exception& e) {
613                 m->errorOut(e, "SummaryCommand", "createGroupSummaryFile");
614                 exit(1);
615         }
616 }
617 //**********************************************************************************************************************