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