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