]> git.donarmstrong.com Git - mothur.git/blob - summarycommand.cpp
working on citations
[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                         if (m->inUsersGroups("citation", Estimators)) { 
227                                 ValidCalculators validCalc; validCalc.printCitations(Estimators); 
228                                 //remove citation from list of calcs
229                                 for (int i = 0; i < Estimators.size(); i++) { if (Estimators[i] == "citation") {  Estimators.erase(Estimators.begin()+i); break; } }
230                         }
231
232                         string temp;
233                         temp = validParameter.validFile(parameters, "abund", false);            if (temp == "not found") { temp = "10"; }
234                         convert(temp, abund); 
235                         
236                         temp = validParameter.validFile(parameters, "size", false);                     if (temp == "not found") { temp = "0"; }
237                         convert(temp, size); 
238                         
239                         temp = validParameter.validFile(parameters, "groupmode", false);                if (temp == "not found") { temp = "T"; }
240                         groupMode = m->isTrue(temp);
241                         
242         
243                 }
244         }
245         catch(exception& e) {
246                 m->errorOut(e, "SummaryCommand", "SummaryCommand");
247                 exit(1);
248         }
249 }
250 //**********************************************************************************************************************
251
252 int SummaryCommand::execute(){
253         try {
254         
255                 if (abort == true) { if (calledHelp) { return 0; }  return 2;   }
256                 
257                 if ((format != "sharedfile")) { inputFileNames.push_back(inputfile);  }
258                 else {  inputFileNames = parseSharedFile(sharedfile);  format = "rabund"; }
259                 
260                 if (m->control_pressed) { return 0; }
261                 
262                 int numLines = 0;
263                 int numCols = 0;
264                 
265                 for (int p = 0; p < inputFileNames.size(); p++) {
266                         
267                         numLines = 0;
268                         numCols = 0;
269                         
270                         string fileNameRoot = outputDir + m->getRootName(m->getSimpleName(inputFileNames[p])) + "summary";
271                         outputNames.push_back(fileNameRoot); outputTypes["summary"].push_back(fileNameRoot);
272                         
273                         if (inputFileNames.size() > 1) {
274                                 m->mothurOutEndLine(); m->mothurOut("Processing group " + groups[p]); m->mothurOutEndLine(); m->mothurOutEndLine();
275                         }
276                         
277                         sumCalculators.clear();
278                         
279                         ValidCalculators validCalculator;
280                         
281                         for (int i=0; i<Estimators.size(); i++) {
282                                 if (validCalculator.isValidCalculator("summary", Estimators[i]) == true) { 
283                                         if(Estimators[i] == "sobs"){
284                                                 sumCalculators.push_back(new Sobs());
285                                         }else if(Estimators[i] == "chao"){
286                                                 sumCalculators.push_back(new Chao1());
287                                         }else if(Estimators[i] == "coverage"){
288                                                 sumCalculators.push_back(new Coverage());
289                                         }else if(Estimators[i] == "geometric"){
290                                                 sumCalculators.push_back(new Geom());
291                                         }else if(Estimators[i] == "logseries"){
292                                                 sumCalculators.push_back(new LogSD());
293                                         }else if(Estimators[i] == "qstat"){
294                                                 sumCalculators.push_back(new QStat());
295                                         }else if(Estimators[i] == "bergerparker"){
296                                                 sumCalculators.push_back(new BergerParker());
297                                         }else if(Estimators[i] == "bstick"){
298                                                 sumCalculators.push_back(new BStick());
299                                         }else if(Estimators[i] == "ace"){
300                                                 if(abund < 5)
301                                                         abund = 10;
302                                                 sumCalculators.push_back(new Ace(abund));
303                                         }else if(Estimators[i] == "jack"){
304                                                 sumCalculators.push_back(new Jackknife());
305                                         }else if(Estimators[i] == "shannon"){
306                                                 sumCalculators.push_back(new Shannon());
307                                         }else if(Estimators[i] == "shannoneven"){
308                                                 sumCalculators.push_back(new ShannonEven());
309                                         }else if(Estimators[i] == "npshannon"){
310                                                 sumCalculators.push_back(new NPShannon());
311                                         }else if(Estimators[i] == "heip"){
312                                                 sumCalculators.push_back(new Heip());
313                                         }else if(Estimators[i] == "smithwilson"){
314                                                 sumCalculators.push_back(new SmithWilson());
315                                         }else if(Estimators[i] == "simpson"){
316                                                 sumCalculators.push_back(new Simpson());
317                                         }else if(Estimators[i] == "simpsoneven"){
318                                                 sumCalculators.push_back(new SimpsonEven());
319                                         }else if(Estimators[i] == "invsimpson"){
320                                                 sumCalculators.push_back(new InvSimpson());
321                                         }else if(Estimators[i] == "bootstrap"){
322                                                 sumCalculators.push_back(new Bootstrap());
323                                         }else if (Estimators[i] == "nseqs") { 
324                                                 sumCalculators.push_back(new NSeqs());
325                                         }else if (Estimators[i] == "goodscoverage") { 
326                                                 sumCalculators.push_back(new GoodsCoverage());
327                                         }else if (Estimators[i] == "efron") { 
328                                                 sumCalculators.push_back(new Efron(size));
329                                         }else if (Estimators[i] == "boneh") { 
330                                                 sumCalculators.push_back(new Boneh(size));
331                                         }else if (Estimators[i] == "solow") { 
332                                                 sumCalculators.push_back(new Solow(size));
333                                         }else if (Estimators[i] == "shen") { 
334                                                 sumCalculators.push_back(new Shen(size, abund));
335                                         }
336                                 }
337                         }
338                         
339                         //if the users entered no valid calculators don't execute command
340                         if (sumCalculators.size() == 0) {  for (int i = 0; i < outputNames.size(); i++) {       remove(outputNames[i].c_str());  } return 0; }
341                         
342                         ofstream outputFileHandle;
343                         m->openOutputFile(fileNameRoot, outputFileHandle);
344                         outputFileHandle << "label";
345                 
346                         input = new InputData(inputFileNames[p], format);
347                         sabund = input->getSAbundVector();
348                         string lastLabel = sabund->getLabel();
349                 
350                         for(int i=0;i<sumCalculators.size();i++){
351                                 if(sumCalculators[i]->getCols() == 1){
352                                         outputFileHandle << '\t' << sumCalculators[i]->getName();
353                                         numCols++;
354                                 }
355                                 else{
356                                         outputFileHandle << '\t' << sumCalculators[i]->getName() << "\t" << sumCalculators[i]->getName() << "_lci\t" << sumCalculators[i]->getName() << "_hci";
357                                         numCols += 3;
358                                 }
359                         }
360                         outputFileHandle << endl;
361                         
362                         //if the users enters label "0.06" and there is no "0.06" in their file use the next lowest label.
363                         set<string> processedLabels;
364                         set<string> userLabels = labels;
365                         
366                         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;  }
367                         
368                         while((sabund != NULL) && ((allLines == 1) || (userLabels.size() != 0))) {
369                                 
370                                 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;  }
371                                 
372                                 if(allLines == 1 || labels.count(sabund->getLabel()) == 1){                     
373                                         
374                                         m->mothurOut(sabund->getLabel()); m->mothurOutEndLine();
375                                         processedLabels.insert(sabund->getLabel());
376                                         userLabels.erase(sabund->getLabel());
377                                         
378                                         outputFileHandle << sabund->getLabel();
379                                         for(int i=0;i<sumCalculators.size();i++){
380                                                 vector<double> data = sumCalculators[i]->getValues(sabund);
381                                                 
382                                                 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;  }
383
384                                                 outputFileHandle << '\t';
385                                                 sumCalculators[i]->print(outputFileHandle);
386                                         }
387                                         outputFileHandle << endl;
388                                         numLines++;
389                                 }
390                                 
391                                 if ((m->anyLabelsToProcess(sabund->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
392                                         string saveLabel = sabund->getLabel();
393                                         
394                                         delete sabund;
395                                         sabund = input->getSAbundVector(lastLabel);
396                                         
397                                         m->mothurOut(sabund->getLabel()); m->mothurOutEndLine();
398                                         processedLabels.insert(sabund->getLabel());
399                                         userLabels.erase(sabund->getLabel());
400                                         
401                                         outputFileHandle << sabund->getLabel();
402                                         for(int i=0;i<sumCalculators.size();i++){
403                                                 vector<double> data = sumCalculators[i]->getValues(sabund);
404                                                 
405                                                 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;  }
406                                                 
407                                                 outputFileHandle << '\t';
408                                                 sumCalculators[i]->print(outputFileHandle);
409                                         }
410                                         outputFileHandle << endl;
411                                         numLines++;
412                                         
413                                         //restore real lastlabel to save below
414                                         sabund->setLabel(saveLabel);
415                                 }               
416                                 
417                                 lastLabel = sabund->getLabel();                 
418                                 
419                                 delete sabund;
420                                 sabund = input->getSAbundVector();
421                         }
422                         
423                         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;  }
424
425                         //output error messages about any remaining user labels
426                         set<string>::iterator it;
427                         bool needToRun = false;
428                         for (it = userLabels.begin(); it != userLabels.end(); it++) {  
429                                 m->mothurOut("Your file does not include the label " + *it); 
430                                 if (processedLabels.count(lastLabel) != 1) {
431                                         m->mothurOut(". I will use " + lastLabel + "."); m->mothurOutEndLine();
432                                         needToRun = true;
433                                 }else {
434                                         m->mothurOut(". Please refer to " + lastLabel + "."); m->mothurOutEndLine();
435                                 }
436                         }
437                         
438                         //run last label if you need to
439                         if (needToRun == true)  {
440                                 if (sabund != NULL) {   delete sabund;  }
441                                 sabund = input->getSAbundVector(lastLabel);
442                                 
443                                 m->mothurOut(sabund->getLabel()); m->mothurOutEndLine();
444                                 outputFileHandle << sabund->getLabel();
445                                 for(int i=0;i<sumCalculators.size();i++){
446                                         vector<double> data = sumCalculators[i]->getValues(sabund);
447                                         
448                                         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;  }
449
450                                         outputFileHandle << '\t';
451                                         sumCalculators[i]->print(outputFileHandle);
452                                 }
453                                 outputFileHandle << endl;
454                                 numLines++;
455                                 delete sabund;
456                         }
457                         
458                         outputFileHandle.close();
459                         
460                         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;  }
461
462                         
463                         delete input;  
464                         for(int i=0;i<sumCalculators.size();i++){  delete sumCalculators[i]; }
465                 }
466                 
467                 if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) {        remove(outputNames[i].c_str());  }  return 0;  }
468                 
469                 //create summary file containing all the groups data for each label - this function just combines the info from the files already created.
470                 if ((sharedfile != "") && (groupMode)) {   outputNames.push_back(createGroupSummaryFile(numLines, numCols, outputNames));  }
471                 
472                 if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) {        remove(outputNames[i].c_str());  }  return 0;  }
473                 
474                 m->mothurOutEndLine();
475                 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
476                 for (int i = 0; i < outputNames.size(); i++) {  m->mothurOut(outputNames[i]); m->mothurOutEndLine();    }
477                 m->mothurOutEndLine();
478                 
479                 return 0;
480         }
481         catch(exception& e) {
482                 m->errorOut(e, "SummaryCommand", "execute");
483                 exit(1);
484         }
485 }
486 //**********************************************************************************************************************
487 vector<string> SummaryCommand::parseSharedFile(string filename) {
488         try {
489                 vector<string> filenames;
490                 
491                 map<string, ofstream*> filehandles;
492                 map<string, ofstream*>::iterator it3;
493                 
494                 input = new InputData(filename, "sharedfile");
495                 vector<SharedRAbundVector*> lookup = input->getSharedRAbundVectors();
496                 
497                 string sharedFileRoot = m->getRootName(filename);
498                 
499                 //clears file before we start to write to it below
500                 for (int i=0; i<lookup.size(); i++) {
501                         remove((sharedFileRoot + lookup[i]->getGroup() + ".rabund").c_str());
502                         filenames.push_back((sharedFileRoot + lookup[i]->getGroup() + ".rabund"));
503                 }
504                 
505                 ofstream* temp;
506                 for (int i=0; i<lookup.size(); i++) {
507                         temp = new ofstream;
508                         filehandles[lookup[i]->getGroup()] = temp;
509                         groups.push_back(lookup[i]->getGroup());
510                 }
511
512                 while(lookup[0] != NULL) {
513                 
514                         for (int i = 0; i < lookup.size(); i++) {
515                                 RAbundVector rav = lookup[i]->getRAbundVector();
516                                 m->openOutputFileAppend(sharedFileRoot + lookup[i]->getGroup() + ".rabund", *(filehandles[lookup[i]->getGroup()]));
517                                 rav.print(*(filehandles[lookup[i]->getGroup()]));
518                                 (*(filehandles[lookup[i]->getGroup()])).close();
519                         }
520                 
521                         for (int i = 0; i < lookup.size(); i++) {  delete lookup[i];  } 
522                         lookup = input->getSharedRAbundVectors();
523                 }
524                 
525                 //free memory
526                 for (it3 = filehandles.begin(); it3 != filehandles.end(); it3++) {
527                         delete it3->second;
528                 }
529                 
530                 delete input;
531
532                 return filenames;
533         }
534         catch(exception& e) {
535                 m->errorOut(e, "SummaryCommand", "parseSharedFile");
536                 exit(1);
537         }
538 }
539 //**********************************************************************************************************************
540 string SummaryCommand::createGroupSummaryFile(int numLines, int numCols, vector<string>& outputNames) {
541         try {
542                 
543                 ofstream out;
544                 string combineFileName = outputDir + m->getRootName(m->getSimpleName(sharedfile)) + "groups.summary";
545                 
546                 //open combined file
547                 m->openOutputFile(combineFileName, out);
548                 
549                 //open each groups summary file
550                 string newLabel = "";
551                 map<string, vector<string> > files;
552                 for (int i=0; i<outputNames.size(); i++) {
553                         vector<string> thisFilesLines;
554                         
555                         ifstream temp;
556                         m->openInputFile(outputNames[i], temp);
557                         
558                         //read through first line - labels
559                         string tempLabel;
560                         if (i == 0) { //we want to save the labels to output below
561                                 for (int j = 0; j < numCols+1; j++) {  
562                                         temp >> tempLabel; 
563                                         
564                                         if (j == 1) {  newLabel += "group\t" + tempLabel + '\t';
565                                         }else{  newLabel += tempLabel + '\t';   }
566                                 }
567                         }else{  for (int j = 0; j < numCols+1; j++) {  temp >> tempLabel;  }  }
568                         
569                         m->gobble(temp);
570                         
571                         //for each label
572                         for (int k = 0; k < numLines; k++) {
573                                 
574                                 string thisLine = "";
575                                 string tempLabel;
576                                         
577                                 for (int j = 0; j < numCols+1; j++) {  
578                                         temp >> tempLabel; 
579                                                 
580                                         //save for later
581                                         if (j == 1) { thisLine += groups[i] + "\t" + tempLabel + "\t";  }
582                                         else{  thisLine += tempLabel + "\t";    }
583                                 }
584                                         
585                                 thisLine += "\n";
586                                 
587                                 thisFilesLines.push_back(thisLine);
588                                         
589                                 m->gobble(temp);
590                         }
591                                 
592                         files[outputNames[i]] = thisFilesLines;
593                         
594                         temp.close();
595                         remove(outputNames[i].c_str());
596                 }
597                 
598                 //output label line to new file
599                 out << newLabel << endl;
600                 
601                 //for each label
602                 for (int k = 0; k < numLines; k++) {
603                 
604                         //grab summary data for each group
605                         for (int i=0; i<outputNames.size(); i++) {
606                                 out << files[outputNames[i]][k];
607                         }
608                 }       
609                 
610                 outputNames.clear();
611                 
612                 out.close();
613                 
614                 //return combine file name
615                 return combineFileName;
616                 
617         }
618         catch(exception& e) {
619                 m->errorOut(e, "SummaryCommand", "createGroupSummaryFile");
620                 exit(1);
621         }
622 }
623 //**********************************************************************************************************************