]> git.donarmstrong.com Git - mothur.git/blob - summarysharedcommand.cpp
cluster.split fix
[mothur.git] / summarysharedcommand.cpp
1 /*
2  *  summarysharedcommand.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 "summarysharedcommand.h"
11 #include "sharedsobscollectsummary.h"
12 #include "sharedchao1.h"
13 #include "sharedace.h"
14 #include "sharednseqs.h"
15 #include "sharedjabund.h"
16 #include "sharedsorabund.h"
17 #include "sharedjclass.h"
18 #include "sharedsorclass.h"
19 #include "sharedjest.h"
20 #include "sharedsorest.h"
21 #include "sharedthetayc.h"
22 #include "sharedthetan.h"
23 #include "sharedkstest.h"
24 #include "whittaker.h"
25 #include "sharedochiai.h"
26 #include "sharedanderbergs.h"
27 #include "sharedkulczynski.h"
28 #include "sharedkulczynskicody.h"
29 #include "sharedlennon.h"
30 #include "sharedmorisitahorn.h"
31 #include "sharedbraycurtis.h"
32 #include "sharedjackknife.h"
33 #include "whittaker.h"
34
35 //**********************************************************************************************************************
36 vector<string> SummarySharedCommand::getValidParameters(){      
37         try {
38                 string Array[] =  {"label","calc","groups","all","outputdir","distance","inputdir", "processors"};
39                 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
40                 return myArray;
41         }
42         catch(exception& e) {
43                 m->errorOut(e, "SummarySharedCommand", "getValidParameters");
44                 exit(1);
45         }
46 }
47 //**********************************************************************************************************************
48 SummarySharedCommand::SummarySharedCommand(){   
49         try {
50                 abort = true;
51                 //initialize outputTypes
52                 vector<string> tempOutNames;
53                 outputTypes["summary"] = tempOutNames;
54         }
55         catch(exception& e) {
56                 m->errorOut(e, "SummarySharedCommand", "SummarySharedCommand");
57                 exit(1);
58         }
59 }
60 //**********************************************************************************************************************
61 vector<string> SummarySharedCommand::getRequiredParameters(){   
62         try {
63                 vector<string> myArray;
64                 return myArray;
65         }
66         catch(exception& e) {
67                 m->errorOut(e, "SummarySharedCommand", "getRequiredParameters");
68                 exit(1);
69         }
70 }
71 //**********************************************************************************************************************
72 vector<string> SummarySharedCommand::getRequiredFiles(){        
73         try {
74                 string Array[] =  {"shared"};
75                 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
76                 return myArray;
77         }
78         catch(exception& e) {
79                 m->errorOut(e, "SummarySharedCommand", "getRequiredFiles");
80                 exit(1);
81         }
82 }
83 //**********************************************************************************************************************
84
85 SummarySharedCommand::SummarySharedCommand(string option)  {
86         try {
87                 globaldata = GlobalData::getInstance();
88                 abort = false;
89                 allLines = 1;
90                 labels.clear();
91                 Estimators.clear();
92                 
93                 //allow user to run help
94                 if(option == "help") { validCalculator = new ValidCalculators(); help(); abort = true; }
95                 
96                 else {
97                         //valid paramters for this command
98                         string Array[] =  {"label","calc","groups","all","outputdir","distance","inputdir", "processors"};
99                         vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
100                         
101                         OptionParser parser(option);
102                         map<string, string> parameters = parser.getParameters();
103                         
104                         ValidParameters validParameter;
105                 
106                         //check to make sure all parameters are valid for command
107                         for (map<string, string>::iterator it = parameters.begin(); it != parameters.end(); it++) { 
108                                 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) {  abort = true;  }
109                         }
110                         
111                         //make sure the user has already run the read.otu command
112                         if (globaldata->getSharedFile() == "") {
113                                  m->mothurOut("You must read a list and a group, or a shared before you can use the summary.shared command."); m->mothurOutEndLine(); abort = true; 
114                         }
115                         
116                         //initialize outputTypes
117                         vector<string> tempOutNames;
118                         outputTypes["summary"] = tempOutNames;
119                         
120                         //if the user changes the output directory command factory will send this info to us in the output parameter 
121                         outputDir = validParameter.validFile(parameters, "outputdir", false);           if (outputDir == "not found"){  
122                                 outputDir = ""; 
123                                 outputDir += m->hasPath(globaldata->getSharedFile()); //if user entered a file with a path then preserve it     
124                         }
125
126                         //check for optional parameter and set defaults
127                         // ...at some point should added some additional type checking...
128                         label = validParameter.validFile(parameters, "label", false);                   
129                         if (label == "not found") { label = ""; }
130                         else { 
131                                 if(label != "all") {  m->splitAtDash(label, labels);  allLines = 0;  }
132                                 else { allLines = 1;  }
133                         }
134                         
135                         //if the user has not specified any labels use the ones from read.otu
136                         if(label == "") {  
137                                 allLines = globaldata->allLines; 
138                                 labels = globaldata->labels; 
139                         }
140                                 
141                         calc = validParameter.validFile(parameters, "calc", false);                     
142                         if (calc == "not found") { calc = "sharedsobs-sharedchao-sharedace-jabund-sorabund-jclass-sorclass-jest-sorest-thetayc-thetan";  }
143                         else { 
144                                  if (calc == "default")  {  calc = "sharedsobs-sharedchao-sharedace-jabund-sorabund-jclass-sorclass-jest-sorest-thetayc-thetan";  }
145                         }
146                         m->splitAtDash(calc, Estimators);
147                         
148                         groups = validParameter.validFile(parameters, "groups", false);                 
149                         if (groups == "not found") { groups = ""; }
150                         else { 
151                                 m->splitAtDash(groups, Groups);
152                                 globaldata->Groups = Groups;
153                         }
154                         
155                         string temp = validParameter.validFile(parameters, "all", false);                               if (temp == "not found") { temp = "false"; }
156                         all = m->isTrue(temp);
157                         
158                         temp = validParameter.validFile(parameters, "distance", false);                                 if (temp == "not found") { temp = "false"; }
159                         createPhylip = m->isTrue(temp);
160                         
161                         temp = validParameter.validFile(parameters, "processors", false);       if(temp == "not found"){        temp = "1"; }
162                         convert(temp, processors); 
163                         
164                         if (abort == false) {
165                         
166                                 validCalculator = new ValidCalculators();
167                                 int i;
168                                 
169                                 for (i=0; i<Estimators.size(); i++) {
170                                         if (validCalculator->isValidCalculator("sharedsummary", Estimators[i]) == true) { 
171                                                 if (Estimators[i] == "sharedsobs") { 
172                                                         sumCalculators.push_back(new SharedSobsCS());
173                                                 }else if (Estimators[i] == "sharedchao") { 
174                                                         sumCalculators.push_back(new SharedChao1());
175                                                 }else if (Estimators[i] == "sharedace") { 
176                                                         sumCalculators.push_back(new SharedAce());
177                                                 }else if (Estimators[i] == "jabund") {  
178                                                         sumCalculators.push_back(new JAbund());
179                                                 }else if (Estimators[i] == "sorabund") { 
180                                                         sumCalculators.push_back(new SorAbund());
181                                                 }else if (Estimators[i] == "jclass") { 
182                                                         sumCalculators.push_back(new Jclass());
183                                                 }else if (Estimators[i] == "sorclass") { 
184                                                         sumCalculators.push_back(new SorClass());
185                                                 }else if (Estimators[i] == "jest") { 
186                                                         sumCalculators.push_back(new Jest());
187                                                 }else if (Estimators[i] == "sorest") { 
188                                                         sumCalculators.push_back(new SorEst());
189                                                 }else if (Estimators[i] == "thetayc") { 
190                                                         sumCalculators.push_back(new ThetaYC());
191                                                 }else if (Estimators[i] == "thetan") { 
192                                                         sumCalculators.push_back(new ThetaN());
193                                                 }else if (Estimators[i] == "kstest") { 
194                                                         sumCalculators.push_back(new KSTest());
195                                                 }else if (Estimators[i] == "sharednseqs") { 
196                                                         sumCalculators.push_back(new SharedNSeqs());
197                                                 }else if (Estimators[i] == "ochiai") { 
198                                                         sumCalculators.push_back(new Ochiai());
199                                                 }else if (Estimators[i] == "anderberg") { 
200                                                         sumCalculators.push_back(new Anderberg());
201                                                 }else if (Estimators[i] == "kulczynski") { 
202                                                         sumCalculators.push_back(new Kulczynski());
203                                                 }else if (Estimators[i] == "kulczynskicody") { 
204                                                         sumCalculators.push_back(new KulczynskiCody());
205                                                 }else if (Estimators[i] == "lennon") { 
206                                                         sumCalculators.push_back(new Lennon());
207                                                 }else if (Estimators[i] == "morisitahorn") { 
208                                                         sumCalculators.push_back(new MorHorn());
209                                                 }else if (Estimators[i] == "braycurtis") { 
210                                                         sumCalculators.push_back(new BrayCurtis());
211                                                 }else if (Estimators[i] == "whittaker") { 
212                                                         sumCalculators.push_back(new Whittaker());
213                                                 }
214                                         }
215                                 }
216                                 
217                                 mult = false;
218                         }
219                 }
220         }
221         catch(exception& e) {
222                 m->errorOut(e, "SummarySharedCommand", "SummarySharedCommand");
223                 exit(1);
224         }
225 }
226
227 //**********************************************************************************************************************
228
229 void SummarySharedCommand::help(){
230         try {
231                 m->mothurOut("The summary.shared command can only be executed after a successful read.otu command.\n");
232                 m->mothurOut("The summary.shared command parameters are label, calc, distance and all.  No parameters are required.\n");
233                 m->mothurOut("The summary.shared command should be in the following format: \n");
234                 m->mothurOut("summary.shared(label=yourLabel, calc=yourEstimators, groups=yourGroups).\n");
235                 m->mothurOut("Example summary.shared(label=unique-.01-.03, groups=B-C, calc=sharedchao-sharedace-jabund-sorensonabund-jclass-sorclass-jest-sorest-thetayc-thetan).\n");
236                 validCalculator->printCalc("sharedsummary", cout);
237                 m->mothurOut("The default value for calc is sharedsobs-sharedchao-sharedace-jabund-sorensonabund-jclass-sorclass-jest-sorest-thetayc-thetan\n");
238                 m->mothurOut("The default value for groups is all the groups in your groupfile.\n");
239                 m->mothurOut("The distance parameter allows you to indicate you would like a distance file created for each calculator for each label, default=f.\n");
240                 m->mothurOut("The label parameter is used to analyze specific labels in your input.\n");
241                 m->mothurOut("The all parameter is used to specify if you want the estimate of all your groups together.  This estimate can only be made for sharedsobs and sharedchao calculators. The default is false.\n");
242                 m->mothurOut("If you use sharedchao and run into memory issues, set all to false. \n");
243                 m->mothurOut("The groups parameter allows you to specify which of the groups in your groupfile you would like analyzed.  You must enter at least 2 valid groups.\n");
244                 m->mothurOut("Note: No spaces between parameter labels (i.e. label), '=' and parameters (i.e.yourLabel).\n\n");
245         }
246         catch(exception& e) {
247                 m->errorOut(e, "SummarySharedCommand", "help");
248                 exit(1);
249         }
250 }
251
252 //**********************************************************************************************************************
253
254 SummarySharedCommand::~SummarySharedCommand(){
255         if (abort == false) {
256                 delete read;
257                 delete validCalculator;
258         }
259 }
260
261 //**********************************************************************************************************************
262
263 int SummarySharedCommand::execute(){
264         try {
265         
266                 if (abort == true) { return 0; }
267                 
268                 ofstream outputFileHandle, outAll;
269                 string outputFileName = outputDir + m->getRootName(m->getSimpleName(globaldata->inputFileName)) + "shared.summary";
270                 
271                 //if the users entered no valid calculators don't execute command
272                 if (sumCalculators.size() == 0) { return 0; }
273                 //check if any calcs can do multiples
274                 else{
275                         if (all){ 
276                                 for (int i = 0; i < sumCalculators.size(); i++) {
277                                         if (sumCalculators[i]->getMultiple() == true) { mult = true; }
278                                 }
279                         }
280                 }
281                 
282                 //read first line
283                 read = new ReadOTUFile(globaldata->inputFileName);      
284                 read->read(&*globaldata); 
285                         
286                 input = globaldata->ginput;
287                 lookup = input->getSharedRAbundVectors();
288                 string lastLabel = lookup[0]->getLabel();
289         
290                 /******************************************************/
291                 //output headings for files
292                 /******************************************************/
293                 //output estimator names as column headers
294                 m->openOutputFile(outputFileName, outputFileHandle);
295                 outputFileHandle << "label" <<'\t' << "comparison" << '\t'; 
296                 for(int i=0;i<sumCalculators.size();i++){
297                         outputFileHandle << '\t' << sumCalculators[i]->getName();
298                         if (sumCalculators[i]->getCols() == 3) {   outputFileHandle << "\t" << sumCalculators[i]->getName() << "_lci\t" << sumCalculators[i]->getName() << "_hci";  }
299                 }
300                 outputFileHandle << endl;
301                 outputFileHandle.close();
302                 
303                 //create file and put column headers for multiple groups file
304                 string outAllFileName = ((m->getRootName(globaldata->inputFileName)) + "sharedmultiple.summary");
305                 if (mult == true) {
306                         m->openOutputFile(outAllFileName, outAll);
307                         outputNames.push_back(outAllFileName);
308                         
309                         outAll << "label" <<'\t' << "comparison" << '\t'; 
310                         for(int i=0;i<sumCalculators.size();i++){
311                                 if (sumCalculators[i]->getMultiple() == true) { 
312                                         outAll << '\t' << sumCalculators[i]->getName();
313                                 }
314                         }
315                         outAll << endl;
316                         outAll.close();
317                 }
318                 
319                 if (lookup.size() < 2) { 
320                         m->mothurOut("I cannot run the command without at least 2 valid groups."); 
321                         for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; }
322                         
323                         //close files and clean up
324                         remove(outputFileName.c_str());
325                         if (mult == true) { remove(outAllFileName.c_str());  }
326                         return 0;
327                 //if you only have 2 groups you don't need a .sharedmultiple file
328                 }else if ((lookup.size() == 2) && (mult == true)) { 
329                         mult = false;
330                         remove(outAllFileName.c_str());
331                         outputNames.pop_back();
332                 }
333                 
334                 if (m->control_pressed) {
335                         if (mult) {  remove(outAllFileName.c_str());  }
336                         remove(outputFileName.c_str()); 
337                         delete input; globaldata->ginput = NULL;
338                         for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; }
339                         for(int i=0;i<sumCalculators.size();i++){  delete sumCalculators[i]; }
340                         globaldata->Groups.clear(); 
341                         return 0;
342                 }
343                 /******************************************************/
344                 
345                 
346                 /******************************************************/
347                 //comparison breakup to be used by different processes later
348                 numGroups = globaldata->Groups.size();
349                 lines.resize(processors);
350                 for (int i = 0; i < processors; i++) {
351                         lines[i].start = int (sqrt(float(i)/float(processors)) * numGroups);
352                         lines[i].end = int (sqrt(float(i+1)/float(processors)) * numGroups);
353                 }               
354                 /******************************************************/
355                 
356                 //if the users enters label "0.06" and there is no "0.06" in their file use the next lowest label.
357                 set<string> processedLabels;
358                 set<string> userLabels = labels;
359                         
360                 //as long as you are not at the end of the file or done wih the lines you want
361                 while((lookup[0] != NULL) && ((allLines == 1) || (userLabels.size() != 0))) {
362                         if (m->control_pressed) {
363                                 if (mult) {  remove(outAllFileName.c_str());  }
364                                 remove(outputFileName.c_str()); 
365                                 delete input; globaldata->ginput = NULL;
366                                 for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; }
367                                 for(int i=0;i<sumCalculators.size();i++){  delete sumCalculators[i]; }
368                                 globaldata->Groups.clear(); 
369                                 return 0;
370                         }
371
372                 
373                         if(allLines == 1 || labels.count(lookup[0]->getLabel()) == 1){                  
374                                 m->mothurOut(lookup[0]->getLabel()); m->mothurOutEndLine();
375                                 process(lookup, outputFileName, outAllFileName);
376                                 
377                                 processedLabels.insert(lookup[0]->getLabel());
378                                 userLabels.erase(lookup[0]->getLabel());
379                         }
380                         
381                         if ((m->anyLabelsToProcess(lookup[0]->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
382                                         string saveLabel = lookup[0]->getLabel();
383                                         
384                                         for (int i = 0; i < lookup.size(); i++) {  delete lookup[i];  } 
385                                         lookup = input->getSharedRAbundVectors(lastLabel);
386
387                                         m->mothurOut(lookup[0]->getLabel()); m->mothurOutEndLine();
388                                         process(lookup, outputFileName, outAllFileName);
389                                         
390                                         processedLabels.insert(lookup[0]->getLabel());
391                                         userLabels.erase(lookup[0]->getLabel());
392                                         
393                                         //restore real lastlabel to save below
394                                         lookup[0]->setLabel(saveLabel);
395                         }
396                         
397                         lastLabel = lookup[0]->getLabel();                      
398                                 
399                         //get next line to process
400                         //prevent memory leak
401                         for (int i = 0; i < lookup.size(); i++) {  delete lookup[i];  } 
402                         lookup = input->getSharedRAbundVectors();
403                 }
404                 
405                 if (m->control_pressed) {
406                         if (mult) { remove(outAllFileName.c_str());  }
407                         remove(outputFileName.c_str()); 
408                         delete input; globaldata->ginput = NULL;
409                         for(int i=0;i<sumCalculators.size();i++){  delete sumCalculators[i]; }
410                         globaldata->Groups.clear(); 
411                         return 0;
412                 }
413
414                 //output error messages about any remaining user labels
415                 set<string>::iterator it;
416                 bool needToRun = false;
417                 for (it = userLabels.begin(); it != userLabels.end(); it++) {  
418                         m->mothurOut("Your file does not include the label " + *it); 
419                         if (processedLabels.count(lastLabel) != 1) {
420                                 m->mothurOut(". I will use " + lastLabel + "."); m->mothurOutEndLine();
421                                 needToRun = true;
422                         }else {
423                                 m->mothurOut(". Please refer to " + lastLabel + "."); m->mothurOutEndLine();
424                         }
425                 }
426                 
427                 //run last label if you need to
428                 if (needToRun == true)  {
429                                 for (int i = 0; i < lookup.size(); i++) {  if (lookup[i] != NULL) {     delete lookup[i];       } } 
430                                 lookup = input->getSharedRAbundVectors(lastLabel);
431
432                                 m->mothurOut(lookup[0]->getLabel()); m->mothurOutEndLine();
433                                 process(lookup, outputFileName, outAllFileName);
434                                 for (int i = 0; i < lookup.size(); i++) {  delete lookup[i];  } 
435                 }
436                 
437                                 
438                 //reset groups parameter
439                 globaldata->Groups.clear();  
440                 
441                 for(int i=0;i<sumCalculators.size();i++){  delete sumCalculators[i]; }
442                 delete input;  globaldata->ginput = NULL;
443                 
444                 if (m->control_pressed) {
445                         remove(outAllFileName.c_str());  
446                         remove(outputFileName.c_str()); 
447                         return 0;
448                 }
449                 
450                 m->mothurOutEndLine();
451                 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
452                 m->mothurOut(outputFileName); m->mothurOutEndLine();    
453                 if (mult) { m->mothurOut(outAllFileName); m->mothurOutEndLine();        outputTypes["summary"].push_back(outAllFileName); }
454                 for (int i = 0; i < outputNames.size(); i++) {  m->mothurOut(outputNames[i]); m->mothurOutEndLine();    } outputTypes["summary"].push_back(outputFileName);
455                 m->mothurOutEndLine();
456
457                 return 0;
458         }
459         catch(exception& e) {
460                 m->errorOut(e, "SummarySharedCommand", "execute");
461                 exit(1);
462         }
463 }
464
465 /***********************************************************/
466 int SummarySharedCommand::process(vector<SharedRAbundVector*> thisLookup, string sumFileName, string sumAllFileName) {
467         try {
468                         vector< vector<seqDist> > calcDists;  //vector containing vectors that contains the summary results for each group compare
469                         calcDists.resize(sumCalculators.size()); //one for each calc, this will be used to make .dist files
470                                 
471                         #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
472                                 if(processors == 1){
473                                         driver(thisLookup, 0, numGroups, sumFileName+".temp", sumAllFileName+".temp", calcDists);
474                                         m->appendFiles((sumFileName + ".temp"), sumFileName);
475                                         remove((sumFileName + ".temp").c_str());
476                                         if (mult) {
477                                                 m->appendFiles((sumAllFileName + ".temp"), sumAllFileName);
478                                                 remove((sumAllFileName + ".temp").c_str());
479                                         }
480                                 }else{
481                                         int process = 1;
482                                         vector<int> processIDS;
483                 
484                                         //loop through and create all the processes you want
485                                         while (process != processors) {
486                                                 int pid = fork();
487                                                 
488                                                 if (pid > 0) {
489                                                         processIDS.push_back(pid); 
490                                                         process++;
491                                                 }else if (pid == 0){
492                                                         driver(thisLookup, lines[process].start, lines[process].end, sumFileName + toString(getpid()) + ".temp", sumAllFileName + toString(getpid()) + ".temp", calcDists);   
493                                                         
494                                                         //only do this if you want a distance file
495                                                         if (createPhylip) {
496                                                                 string tempdistFileName = m->getRootName(m->getSimpleName(sumFileName)) + toString(getpid()) + ".dist";
497                                                                 ofstream outtemp;
498                                                                 m->openOutputFile(tempdistFileName, outtemp);
499                                                                 
500                                                                 for (int i = 0; i < calcDists.size(); i++) {
501                                                                         outtemp << calcDists[i].size() << endl;
502                                                                         
503                                                                         for (int j = 0; j < calcDists[i].size(); j++) {
504                                                                                 outtemp << calcDists[i][j].seq1 << '\t' << calcDists[i][j].seq2 << '\t' << calcDists[i][j].dist << endl;
505                                                                         }
506                                                                 }
507                                                                 outtemp.close();
508                                                         }
509                                                         
510                                                         exit(0);
511                                                 }else { 
512                                                         m->mothurOut("[ERROR]: unable to spawn the necessary processes."); m->mothurOutEndLine(); 
513                                                         for (int i = 0; i < processIDS.size(); i++) { kill (processIDS[i], SIGINT); }
514                                                         exit(0);
515                                                 }
516                                         }
517                                         
518                                         //parent do your part
519                                         driver(thisLookup, lines[0].start, lines[0].end, sumFileName + toString(getpid()) + ".temp", sumAllFileName + toString(getpid()) + ".temp", calcDists);   
520                                         m->appendFiles((sumFileName + toString(getpid()) + ".temp"), sumFileName);
521                                         remove((sumFileName + toString(getpid()) + ".temp").c_str());
522                                         if (mult) { m->appendFiles((sumAllFileName + toString(getpid()) + ".temp"), sumAllFileName); }
523                                                 
524                                         //force parent to wait until all the processes are done
525                                         for (int i = 0; i < processIDS.size(); i++) {
526                                                 int temp = processIDS[i];
527                                                 wait(&temp);
528                                         }
529                                         
530                                         for (int i = 0; i < processIDS.size(); i++) {
531                                                 m->appendFiles((sumFileName + toString(processIDS[i]) + ".temp"), sumFileName);
532                                                 remove((sumFileName + toString(processIDS[i]) + ".temp").c_str());
533                                                 if (mult) {     remove((sumAllFileName + toString(processIDS[i]) + ".temp").c_str());   }
534                                                 
535                                                 if (createPhylip) {
536                                                         string tempdistFileName = m->getRootName(m->getSimpleName(sumFileName)) + toString(processIDS[i]) +  ".dist";
537                                                         ifstream intemp;
538                                                         m->openInputFile(tempdistFileName, intemp);
539                                                         
540                                                         for (int i = 0; i < calcDists.size(); i++) {
541                                                                 int size = 0;
542                                                                 intemp >> size; m->gobble(intemp);
543                                                                         
544                                                                 for (int j = 0; j < size; j++) {
545                                                                         int seq1 = 0;
546                                                                         int seq2 = 0;
547                                                                         float dist = 1.0;
548                                                                         
549                                                                         intemp >> seq1 >> seq2 >> dist;   m->gobble(intemp);
550                                                                         
551                                                                         seqDist tempDist(seq1, seq2, dist);
552                                                                         calcDists[i].push_back(tempDist);
553                                                                 }
554                                                         }
555                                                         intemp.close();
556                                                         remove(tempdistFileName.c_str());
557                                                 }
558                                         }
559
560                                 }
561                         #else
562                                 driver(thisLookup, 0, numGroups, (sumFileName + ".temp"), (sumAllFileName + ".temp"), calcDists);
563                                 m->appendFiles((sumFileName + ".temp"), sumFileName);
564                                 remove((sumFileName + ".temp").c_str());
565                                 if (mult) {
566                                         m->appendFiles((sumAllFileName + ".temp"), sumAllFileName);
567                                         remove((sumAllFileName + ".temp").c_str());
568                                 }
569                         #endif
570                         
571                         if (createPhylip) {
572                                 for (int i = 0; i < calcDists.size(); i++) {
573                                         if (m->control_pressed) { break; }
574                                 
575                                         string distFileName = outputDir + m->getRootName(m->getSimpleName(sumFileName)) + sumCalculators[i]->getName() + "." + thisLookup[0]->getLabel() + ".dist";
576                                         outputNames.push_back(distFileName);
577                                         ofstream outDist;
578                                         m->openOutputFile(distFileName, outDist);
579                                         outDist.setf(ios::fixed, ios::floatfield); outDist.setf(ios::showpoint);
580                                         
581                                         //initialize matrix
582                                         vector< vector<float> > matrix; //square matrix to represent the distance
583                                         matrix.resize(thisLookup.size());
584                                         for (int k = 0; k < thisLookup.size(); k++) {  matrix[k].resize(thisLookup.size(), 0.0); }
585                                         
586                                         
587                                         for (int j = 0; j < calcDists[i].size(); j++) {
588                                                 int row = calcDists[i][j].seq1;
589                                                 int column = calcDists[i][j].seq2;
590                                                 float dist = calcDists[i][j].dist;
591                                                 
592                                                 matrix[row][column] = dist;
593                                                 matrix[column][row] = dist;
594                                         }
595                                         
596                                         //output to file
597                                         outDist << thisLookup.size() << endl;
598                                         for (int r=0; r<thisLookup.size(); r++) { 
599                                                 //output name
600                                                 string name = thisLookup[r]->getGroup();
601                                                 if (name.length() < 10) { //pad with spaces to make compatible
602                                                         while (name.length() < 10) {  name += " ";  }
603                                                 }
604                                                 outDist << name << '\t';
605                                         
606                                                 //output distances
607                                                 for (int l = 0; l < r; l++) {   outDist  << matrix[r][l] << '\t';  }
608                                                 outDist << endl;
609                                         }
610                                         
611                                         outDist.close();
612                                 }
613                         }
614         }
615         catch(exception& e) {
616                 m->errorOut(e, "SummarySharedCommand", "process");
617                 exit(1);
618         }
619 }
620 /**************************************************************************************************/
621 int SummarySharedCommand::driver(vector<SharedRAbundVector*> thisLookup, int start, int end, string sumFile, string sumAllFile, vector< vector<seqDist> >& calcDists) { 
622         try {
623                 
624                 //loop through calculators and add to file all for all calcs that can do mutiple groups
625                 if (mult == true) {
626                         ofstream outAll;
627                         m->openOutputFile(sumAllFile, outAll);
628                         
629                         //output label
630                         outAll << thisLookup[0]->getLabel() << '\t';
631                         
632                         //output groups names
633                         string outNames = "";
634                         for (int j = 0; j < thisLookup.size(); j++) {
635                                 outNames += thisLookup[j]->getGroup() +  "-";
636                         }
637                         outNames = outNames.substr(0, outNames.length()-1); //rip off extra '-';
638                         outAll << outNames << '\t';
639                         
640                         for(int i=0;i<sumCalculators.size();i++){
641                                 if (sumCalculators[i]->getMultiple() == true) { 
642                                         sumCalculators[i]->getValues(thisLookup);
643                                         
644                                         if (m->control_pressed) { outAll.close(); return 1; }
645                                         
646                                         outAll << '\t';
647                                         sumCalculators[i]->print(outAll);
648                                 }
649                         }
650                         outAll << endl;
651                         outAll.close();
652                 }
653                 
654                 ofstream outputFileHandle;
655                 m->openOutputFile(sumFile, outputFileHandle);
656                 
657                 vector<SharedRAbundVector*> subset;
658                 for (int k = start; k < end; k++) { // pass cdd each set of groups to compare
659
660                         for (int l = 0; l < k; l++) {
661                                 
662                                 outputFileHandle << thisLookup[0]->getLabel() << '\t';
663                                 
664                                 subset.clear(); //clear out old pair of sharedrabunds
665                                 //add new pair of sharedrabunds
666                                 subset.push_back(thisLookup[k]); subset.push_back(thisLookup[l]); 
667                                 
668                                 //sort groups to be alphanumeric
669                                 if (thisLookup[k]->getGroup() > thisLookup[l]->getGroup()) {
670                                         outputFileHandle << (thisLookup[l]->getGroup() +'\t' + thisLookup[k]->getGroup()) << '\t'; //print out groups
671                                 }else{
672                                         outputFileHandle << (thisLookup[k]->getGroup() +'\t' + thisLookup[l]->getGroup()) << '\t'; //print out groups
673                                 }
674                                 
675                                 for(int i=0;i<sumCalculators.size();i++) {
676
677                                         vector<double> tempdata = sumCalculators[i]->getValues(subset); //saves the calculator outputs
678                                         
679                                         if (m->control_pressed) { outputFileHandle.close(); return 1; }
680                                         
681                                         outputFileHandle << '\t';
682                                         sumCalculators[i]->print(outputFileHandle);
683                                         
684                                         seqDist temp(l, k, tempdata[0]);
685                                         calcDists[i].push_back(temp);
686                                 }
687                                 outputFileHandle << endl;
688                         }
689                 }
690                 
691                 outputFileHandle.close();
692                 
693                 return 0;
694         }
695         catch(exception& e) {
696                 m->errorOut(e, "SummarySharedCommand", "driver");
697                 exit(1);
698         }
699 }
700 /**************************************************************************************************/
701
702