]> git.donarmstrong.com Git - mothur.git/blob - matrixoutputcommand.cpp
paralellized seq.error and dist.shared added some error checks to libshuff and dist...
[mothur.git] / matrixoutputcommand.cpp
1 /*
2  *  matrixoutputcommand.cpp
3  *  Mothur
4  *
5  *  Created by Sarah Westcott on 5/20/09.
6  *  Copyright 2009 Schloss Lab UMASS Amherst. All rights reserved.
7  *
8  */
9
10 #include "matrixoutputcommand.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 #include "odum.h"
35 #include "canberra.h"
36 #include "structeuclidean.h"
37 #include "structchord.h"
38 #include "hellinger.h"
39 #include "manhattan.h"
40 #include "structpearson.h"
41 #include "soergel.h"
42 #include "spearman.h"
43 #include "structkulczynski.h"
44 #include "structchi2.h"
45 #include "speciesprofile.h"
46 #include "hamming.h"
47 #include "gower.h"
48 #include "memchi2.h"
49 #include "memchord.h"
50 #include "memeuclidean.h"
51 #include "mempearson.h"
52
53 //**********************************************************************************************************************
54 vector<string> MatrixOutputCommand::setParameters(){    
55         try {
56                 CommandParameter pshared("shared", "InputTypes", "", "", "none", "none", "none",false,true); parameters.push_back(pshared);
57                 CommandParameter plabel("label", "String", "", "", "", "", "",false,false); parameters.push_back(plabel);
58                 CommandParameter pgroups("groups", "String", "", "", "", "", "",false,false); parameters.push_back(pgroups);
59                 CommandParameter pcalc("calc", "Multiple", "sharedsobs-sharedchao-sharedace-jabund-sorabund-jclass-sorclass-jest-sorest-thetayc-thetan-kstest-sharednseqs-ochiai-anderberg-kulczynski-kulczynskicody-lennon-morisitahorn-braycurtis-whittaker-odum-canberra-structeuclidean-structchord-hellinger-manhattan-structpearson-soergel-spearman-structkulczynski-speciesprofile-hamming-structchi2-gower-memchi2-memchord-memeuclidean-mempearson", "jclass-thetayc", "", "", "",true,false); parameters.push_back(pcalc);
60                 CommandParameter poutput("output", "Multiple", "lt-square", "lt", "", "", "",false,false); parameters.push_back(poutput);
61                 CommandParameter pprocessors("processors", "Number", "", "1", "", "", "",false,false); parameters.push_back(pprocessors);
62                 CommandParameter pinputdir("inputdir", "String", "", "", "", "", "",false,false); parameters.push_back(pinputdir);
63                 CommandParameter poutputdir("outputdir", "String", "", "", "", "", "",false,false); parameters.push_back(poutputdir);
64                 
65                 vector<string> myArray;
66                 for (int i = 0; i < parameters.size(); i++) {   myArray.push_back(parameters[i].name);          }
67                 return myArray;
68         }
69         catch(exception& e) {
70                 m->errorOut(e, "MatrixOutputCommand", "setParameters");
71                 exit(1);
72         }
73 }
74 //**********************************************************************************************************************
75 string MatrixOutputCommand::getHelpString(){    
76         try {
77                 string helpString = "";
78                 ValidCalculators validCalculator;
79                 helpString += "The dist.shared command parameters are shared, groups, calc, output, processors and label.  shared is a required, unless you have a valid current file.\n";
80                 helpString += "The groups parameter allows you to specify which of the groups in your groupfile you would like included used.\n";
81                 helpString += "The group names are separated by dashes. The label parameter allows you to select what distance levels you would like distance matrices created for, and is also separated by dashes.\n";
82                 helpString += "The dist.shared command should be in the following format: dist.shared(groups=yourGroups, calc=yourCalcs, label=yourLabels).\n";
83                 helpString += "The output parameter allows you to specify format of your distance matrix. Options are lt, and square. The default is lt.\n";
84                 helpString += "Example dist.shared(groups=A-B-C, calc=jabund-sorabund).\n";
85                 helpString += "The default value for groups is all the groups in your groupfile.\n";
86                 helpString += "The default value for calc is jclass and thetayc.\n";
87                 helpString += validCalculator.printCalc("matrix");
88                 helpString += "The dist.shared command outputs a .dist file for each calculator you specify at each distance you choose.\n";
89                 helpString += "Note: No spaces between parameter labels (i.e. groups), '=' and parameters (i.e.yourGroups).\n";
90                 return helpString;
91         }
92         catch(exception& e) {
93                 m->errorOut(e, "MatrixOutputCommand", "getHelpString");
94                 exit(1);
95         }
96 }
97 //**********************************************************************************************************************
98 MatrixOutputCommand::MatrixOutputCommand(){     
99         try {
100                 abort = true; calledHelp = true; 
101                 setParameters();
102                 vector<string> tempOutNames;
103                 outputTypes["phylip"] = tempOutNames;
104         }
105         catch(exception& e) {
106                 m->errorOut(e, "MatrixOutputCommand", "MatrixOutputCommand");
107                 exit(1);
108         }
109 }
110 //**********************************************************************************************************************
111
112 MatrixOutputCommand::MatrixOutputCommand(string option)  {
113         try {
114                 abort = false; calledHelp = false;   
115                 allLines = 1;
116                                 
117                 //allow user to run help
118                 if(option == "help") {  help(); abort = true; calledHelp = true; }
119                 else if(option == "citation") { citation(); abort = true; calledHelp = true;}
120                 
121                 else {
122                         vector<string> myArray = setParameters();
123                         
124                         OptionParser parser(option);
125                         map<string,string> parameters  = parser.getParameters();
126                         map<string,string>::iterator it;
127                         
128                         ValidParameters validParameter;
129                 
130                         //check to make sure all parameters are valid for command
131                         for (it = parameters.begin(); it != parameters.end(); it++) { 
132                                 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) {  abort = true;  }
133                         }
134                         
135                         //initialize outputTypes
136                         vector<string> tempOutNames;
137                         outputTypes["phylip"] = tempOutNames;
138                         
139                         //if the user changes the input directory command factory will send this info to us in the output parameter 
140                         string inputDir = validParameter.validFile(parameters, "inputdir", false);              
141                         if (inputDir == "not found"){   inputDir = "";          }
142                         else {
143                                 string path;
144                                 it = parameters.find("shared");
145                                 //user has given a template file
146                                 if(it != parameters.end()){ 
147                                         path = m->hasPath(it->second);
148                                         //if the user has not given a path then, add inputdir. else leave path alone.
149                                         if (path == "") {       parameters["shared"] = inputDir + it->second;           }
150                                 }
151                         }
152                         
153                         sharedfile = validParameter.validFile(parameters, "shared", true);
154                         if (sharedfile == "not found") {                        
155                                 //if there is a current shared file, use it
156                                 sharedfile = m->getSharedFile(); 
157                                 if (sharedfile != "") { m->mothurOut("Using " + sharedfile + " as input file for the shared parameter."); m->mothurOutEndLine(); }
158                                 else {  m->mothurOut("You have no current sharedfile and the shared parameter is required."); m->mothurOutEndLine(); abort = true; }
159                         }else if (sharedfile == "not open") { sharedfile = ""; abort = true; }
160                         
161                         //if the user changes the output directory command factory will send this info to us in the output parameter 
162                         outputDir = validParameter.validFile(parameters, "outputdir", false);           if (outputDir == "not found"){  
163                                 outputDir = ""; 
164                                 outputDir += m->hasPath(sharedfile); //if user entered a file with a path then preserve it      
165                         }
166                         
167                         //check for optional parameter and set defaults
168                         // ...at some point should added some additional type checking...
169                         label = validParameter.validFile(parameters, "label", false);                   
170                         if (label == "not found") { label = ""; }
171                         else { 
172                                 if(label != "all") {  m->splitAtDash(label, labels);  allLines = 0;  }
173                                 else { allLines = 1;  }
174                         }
175                         
176                         output = validParameter.validFile(parameters, "output", false);         if(output == "not found"){      output = "lt"; }
177                         if ((output != "lt") && (output != "square")) { m->mothurOut(output + " is not a valid output form. Options are lt and square. I will use lt."); m->mothurOutEndLine(); output = "lt"; }
178                         
179                         groups = validParameter.validFile(parameters, "groups", false);                 
180                         if (groups == "not found") { groups = ""; }
181                         else { 
182                                 m->splitAtDash(groups, Groups);
183                                 m->Groups = Groups;
184                         }
185                         
186                         string temp = validParameter.validFile(parameters, "processors", false);        if (temp == "not found"){       temp = m->getProcessors();      }
187                         m->setProcessors(temp);
188                         convert(temp, processors); 
189                                 
190                         calc = validParameter.validFile(parameters, "calc", false);                     
191                         if (calc == "not found") { calc = "jclass-thetayc";  }
192                         else { 
193                                  if (calc == "default")  {  calc = "jclass-thetayc";  }
194                         }
195                         m->splitAtDash(calc, Estimators);
196                         if (m->inUsersGroups("citation", Estimators)) { 
197                                 ValidCalculators validCalc; validCalc.printCitations(Estimators); 
198                                 //remove citation from list of calcs
199                                 for (int i = 0; i < Estimators.size(); i++) { if (Estimators[i] == "citation") {  Estimators.erase(Estimators.begin()+i); break; } }
200                         }
201
202                         if (abort == false) {
203                         
204                                 ValidCalculators validCalculator;
205                                 
206                                 int i;
207                                 for (i=0; i<Estimators.size(); i++) {
208                                         if (validCalculator.isValidCalculator("matrix", Estimators[i]) == true) { 
209                                                 if (Estimators[i] == "sharedsobs") { 
210                                                         matrixCalculators.push_back(new SharedSobsCS());
211                                                 }else if (Estimators[i] == "sharedchao") { 
212                                                         matrixCalculators.push_back(new SharedChao1());
213                                                 }else if (Estimators[i] == "sharedace") { 
214                                                         matrixCalculators.push_back(new SharedAce());
215                                                 }else if (Estimators[i] == "jabund") {  
216                                                         matrixCalculators.push_back(new JAbund());
217                                                 }else if (Estimators[i] == "sorabund") { 
218                                                         matrixCalculators.push_back(new SorAbund());
219                                                 }else if (Estimators[i] == "jclass") { 
220                                                         matrixCalculators.push_back(new Jclass());
221                                                 }else if (Estimators[i] == "sorclass") { 
222                                                         matrixCalculators.push_back(new SorClass());
223                                                 }else if (Estimators[i] == "jest") { 
224                                                         matrixCalculators.push_back(new Jest());
225                                                 }else if (Estimators[i] == "sorest") { 
226                                                         matrixCalculators.push_back(new SorEst());
227                                                 }else if (Estimators[i] == "thetayc") { 
228                                                         matrixCalculators.push_back(new ThetaYC());
229                                                 }else if (Estimators[i] == "thetan") { 
230                                                         matrixCalculators.push_back(new ThetaN());
231                                                 }else if (Estimators[i] == "kstest") { 
232                                                         matrixCalculators.push_back(new KSTest());
233                                                 }else if (Estimators[i] == "sharednseqs") { 
234                                                         matrixCalculators.push_back(new SharedNSeqs());
235                                                 }else if (Estimators[i] == "ochiai") { 
236                                                         matrixCalculators.push_back(new Ochiai());
237                                                 }else if (Estimators[i] == "anderberg") { 
238                                                         matrixCalculators.push_back(new Anderberg());
239                                                 }else if (Estimators[i] == "kulczynski") { 
240                                                         matrixCalculators.push_back(new Kulczynski());
241                                                 }else if (Estimators[i] == "kulczynskicody") { 
242                                                         matrixCalculators.push_back(new KulczynskiCody());
243                                                 }else if (Estimators[i] == "lennon") { 
244                                                         matrixCalculators.push_back(new Lennon());
245                                                 }else if (Estimators[i] == "morisitahorn") { 
246                                                         matrixCalculators.push_back(new MorHorn());
247                                                 }else if (Estimators[i] == "braycurtis") { 
248                                                         matrixCalculators.push_back(new BrayCurtis());
249                                                 }else if (Estimators[i] == "whittaker") { 
250                                                         matrixCalculators.push_back(new Whittaker());
251                                                 }else if (Estimators[i] == "odum") { 
252                                                         matrixCalculators.push_back(new Odum());
253                                                 }else if (Estimators[i] == "canberra") { 
254                                                         matrixCalculators.push_back(new Canberra());
255                                                 }else if (Estimators[i] == "structeuclidean") { 
256                                                         matrixCalculators.push_back(new StructEuclidean());
257                                                 }else if (Estimators[i] == "structchord") { 
258                                                         matrixCalculators.push_back(new StructChord());
259                                                 }else if (Estimators[i] == "hellinger") { 
260                                                         matrixCalculators.push_back(new Hellinger());
261                                                 }else if (Estimators[i] == "manhattan") { 
262                                                         matrixCalculators.push_back(new Manhattan());
263                                                 }else if (Estimators[i] == "structpearson") { 
264                                                         matrixCalculators.push_back(new StructPearson());
265                                                 }else if (Estimators[i] == "soergel") { 
266                                                         matrixCalculators.push_back(new Soergel());
267                                                 }else if (Estimators[i] == "spearman") { 
268                                                         matrixCalculators.push_back(new Spearman());
269                                                 }else if (Estimators[i] == "structkulczynski") { 
270                                                         matrixCalculators.push_back(new StructKulczynski());
271                                                 }else if (Estimators[i] == "speciesprofile") { 
272                                                         matrixCalculators.push_back(new SpeciesProfile());
273                                                 }else if (Estimators[i] == "hamming") { 
274                                                         matrixCalculators.push_back(new Hamming());
275                                                 }else if (Estimators[i] == "structchi2") { 
276                                                         matrixCalculators.push_back(new StructChi2());
277                                                 }else if (Estimators[i] == "gower") { 
278                                                         matrixCalculators.push_back(new Gower());
279                                                 }else if (Estimators[i] == "memchi2") { 
280                                                         matrixCalculators.push_back(new MemChi2());
281                                                 }else if (Estimators[i] == "memchord") { 
282                                                         matrixCalculators.push_back(new MemChord());
283                                                 }else if (Estimators[i] == "memeuclidean") { 
284                                                         matrixCalculators.push_back(new MemEuclidean());
285                                                 }else if (Estimators[i] == "mempearson") { 
286                                                         matrixCalculators.push_back(new MemPearson());
287                                                 }
288                                         }
289                                 }
290                                 
291                         }
292                 }
293                 
294         }
295         catch(exception& e) {
296                 m->errorOut(e, "MatrixOutputCommand", "MatrixOutputCommand");
297                 exit(1);
298         }
299 }
300
301 //**********************************************************************************************************************
302
303 MatrixOutputCommand::~MatrixOutputCommand(){}
304
305 //**********************************************************************************************************************
306
307 int MatrixOutputCommand::execute(){
308         try {
309                 
310                 if (abort == true) { if (calledHelp) { return 0; }  return 2;   }
311                         
312                 //if the users entered no valid calculators don't execute command
313                 if (matrixCalculators.size() == 0) { m->mothurOut("No valid calculators."); m->mothurOutEndLine();  return 0; }
314                         
315                 input = new InputData(sharedfile, "sharedfile");
316                 lookup = input->getSharedRAbundVectors();
317                 string lastLabel = lookup[0]->getLabel();
318                 
319                 //if the users enters label "0.06" and there is no "0.06" in their file use the next lowest label.
320                 set<string> processedLabels;
321                 set<string> userLabels = labels;
322                                         
323                 if (lookup.size() < 2) { m->mothurOut("You have not provided enough valid groups.  I cannot run the command."); m->mothurOutEndLine(); delete input; for (int i = 0; i < lookup.size(); i++) {  delete lookup[i];  } return 0;}
324                 
325                 numGroups = lookup.size();
326                 lines.resize(processors);
327                 for (int i = 0; i < processors; i++) {
328                         lines[i].start = int (sqrt(float(i)/float(processors)) * numGroups);
329                         lines[i].end = int (sqrt(float(i+1)/float(processors)) * numGroups);
330                         cout << i << '\t' << lines[i].start << '\t' << lines[i].end << endl;
331                 }       
332                 
333                 if (m->control_pressed) { delete input; for (int i = 0; i < lookup.size(); i++) {  delete lookup[i];  } m->Groups.clear(); return 0;  }
334                                 
335                 //as long as you are not at the end of the file or done wih the lines you want
336                 while((lookup[0] != NULL) && ((allLines == 1) || (userLabels.size() != 0))) {
337                 
338                         if (m->control_pressed) { outputTypes.clear(); delete input; for (int i = 0; i < lookup.size(); i++) {  delete lookup[i];  } for (int i = 0; i < outputNames.size(); i++) {     remove(outputNames[i].c_str()); } m->Groups.clear(); return 0;  }
339                 
340                         if(allLines == 1 || labels.count(lookup[0]->getLabel()) == 1){                  
341                                 m->mothurOut(lookup[0]->getLabel()); m->mothurOutEndLine();
342                                 process(lookup);
343                                 
344                                 processedLabels.insert(lookup[0]->getLabel());
345                                 userLabels.erase(lookup[0]->getLabel());
346                         }
347                         
348                         if ((m->anyLabelsToProcess(lookup[0]->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
349                                 string saveLabel = lookup[0]->getLabel();
350                                 
351                                 for (int i = 0; i < lookup.size(); i++) {  delete lookup[i];  } 
352                                 lookup = input->getSharedRAbundVectors(lastLabel);
353
354                                 m->mothurOut(lookup[0]->getLabel()); m->mothurOutEndLine();
355                                 process(lookup);
356                                 
357                                 processedLabels.insert(lookup[0]->getLabel());
358                                 userLabels.erase(lookup[0]->getLabel());
359                                 
360                                 //restore real lastlabel to save below
361                                 lookup[0]->setLabel(saveLabel);
362                         }
363
364                         lastLabel = lookup[0]->getLabel();                      
365                         
366                         //get next line to process
367                         for (int i = 0; i < lookup.size(); i++) {  delete lookup[i];  } 
368                         lookup = input->getSharedRAbundVectors();
369                 }
370                 
371                 if (m->control_pressed) { outputTypes.clear(); delete input; for (int i = 0; i < outputNames.size(); i++) {     remove(outputNames[i].c_str()); } m->Groups.clear(); return 0;  }
372
373                 //output error messages about any remaining user labels
374                 set<string>::iterator it;
375                 bool needToRun = false;
376                 for (it = userLabels.begin(); it != userLabels.end(); it++) {  
377                         m->mothurOut("Your file does not include the label " + *it);  
378                         if (processedLabels.count(lastLabel) != 1) {
379                                 m->mothurOut(". I will use " + lastLabel + "."); m->mothurOutEndLine();
380                                 needToRun = true;
381                         }else {
382                                 m->mothurOut(". Please refer to " + lastLabel + "."); m->mothurOutEndLine();
383                         }
384                 }
385                 
386                 if (m->control_pressed) { outputTypes.clear(); delete input;  for (int i = 0; i < outputNames.size(); i++) {    remove(outputNames[i].c_str()); } m->Groups.clear(); return 0;  }
387
388                 //run last label if you need to
389                 if (needToRun == true)  {
390                         for (int i = 0; i < lookup.size(); i++) {  if (lookup[i] != NULL) {  delete lookup[i]; }  } 
391                         lookup = input->getSharedRAbundVectors(lastLabel);
392
393                         m->mothurOut(lookup[0]->getLabel()); m->mothurOutEndLine();
394                         process(lookup);
395                         for (int i = 0; i < lookup.size(); i++) {  delete lookup[i];  } 
396                 }
397                 
398                 if (m->control_pressed) { outputTypes.clear();  delete input;  for (int i = 0; i < outputNames.size(); i++) {   remove(outputNames[i].c_str()); } m->Groups.clear(); return 0;  }
399                 
400                 //reset groups parameter
401                 m->Groups.clear();  
402                 
403                 //set phylip file as new current phylipfile
404                 string current = "";
405                 itTypes = outputTypes.find("phylip");
406                 if (itTypes != outputTypes.end()) {
407                         if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setPhylipFile(current); }
408                 }
409                 
410                 m->mothurOutEndLine();
411                 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
412                 for (int i = 0; i < outputNames.size(); i++) {  m->mothurOut(outputNames[i]); m->mothurOutEndLine();    }
413                 m->mothurOutEndLine();
414
415
416                 return 0;
417         }
418         catch(exception& e) {
419                 m->errorOut(e, "MatrixOutputCommand", "execute");
420                 exit(1);
421         }
422 }
423 /***********************************************************/
424 void MatrixOutputCommand::printSims(ostream& out, vector< vector<float> >& simMatrix) {
425         try {
426                 
427                 out.setf(ios::fixed, ios::floatfield); out.setf(ios::showpoint);
428                 
429                 //output num seqs
430                 out << simMatrix.size() << endl;
431                 
432                 if (output == "lt") {
433                         for (int m = 0; m < simMatrix.size(); m++)      {
434                                 out << lookup[m]->getGroup() << '\t';
435                                 for (int n = 0; n < m; n++)     {
436                                         out << simMatrix[m][n] << '\t'; 
437                                 }
438                                 out << endl;
439                         }
440                 }else{
441                         for (int m = 0; m < simMatrix.size(); m++)      {
442                                 out << lookup[m]->getGroup() << '\t';
443                                 for (int n = 0; n < simMatrix[m].size(); n++)   {
444                                         out << simMatrix[m][n] << '\t'; 
445                                 }
446                                 out << endl;
447                         }
448                 }
449         }
450         catch(exception& e) {
451                 m->errorOut(e, "MatrixOutputCommand", "printSims");
452                 exit(1);
453         }
454 }
455 /***********************************************************/
456 int MatrixOutputCommand::process(vector<SharedRAbundVector*> thisLookup){
457         try {
458                 EstOutput data;
459                 vector<SharedRAbundVector*> subset;
460                 vector< vector<seqDist> > calcDists; calcDists.resize(matrixCalculators.size()); //one for each calc, this will be used to make .dist files
461                 
462         #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
463                 if(processors == 1){
464                         driver(thisLookup, 0, numGroups, calcDists);
465                 }else{
466                         int process = 1;
467                         vector<int> processIDS;
468                         
469                         //loop through and create all the processes you want
470                         while (process != processors) {
471                                 int pid = fork();
472                                 
473                                 if (pid > 0) {
474                                         processIDS.push_back(pid); 
475                                         process++;
476                                 }else if (pid == 0){
477                                         driver(thisLookup, lines[process].start, lines[process].end, calcDists);   
478                                         
479                                         string tempdistFileName = m->getRootName(m->getSimpleName(sharedfile)) + toString(getpid()) + ".dist";
480                                         ofstream outtemp;
481                                         m->openOutputFile(tempdistFileName, outtemp);
482                                                 
483                                         for (int i = 0; i < calcDists.size(); i++) {
484                                                 outtemp << calcDists[i].size() << endl;
485                                                         
486                                                 for (int j = 0; j < calcDists[i].size(); j++) {
487                                                         outtemp << calcDists[i][j].seq1 << '\t' << calcDists[i][j].seq2 << '\t' << calcDists[i][j].dist << endl;
488                                                 }
489                                         }
490                                         outtemp.close();
491                                                                         
492                                         exit(0);
493                                 }else { 
494                                         m->mothurOut("[ERROR]: unable to spawn the necessary processes."); m->mothurOutEndLine(); 
495                                         for (int i = 0; i < processIDS.size(); i++) { kill (processIDS[i], SIGINT); }
496                                         exit(0);
497                                 }
498                         }
499                         
500                         //parent do your part
501                         driver(thisLookup, lines[0].start, lines[0].end, calcDists);   
502                                                 
503                         //force parent to wait until all the processes are done
504                         for (int i = 0; i < processIDS.size(); i++) {
505                                 int temp = processIDS[i];
506                                 wait(&temp);
507                         }
508                         
509                         for (int i = 0; i < processIDS.size(); i++) {
510                                 string tempdistFileName = m->getRootName(m->getSimpleName(sharedfile)) + toString(processIDS[i]) +  ".dist";
511                                 ifstream intemp;
512                                 m->openInputFile(tempdistFileName, intemp);
513                                         
514                                 for (int k = 0; k < calcDists.size(); k++) {
515                                         int size = 0;
516                                         intemp >> size; m->gobble(intemp);
517                                                 
518                                         for (int j = 0; j < size; j++) {
519                                                 int seq1 = 0;
520                                                 int seq2 = 0;
521                                                 float dist = 1.0;
522                                                         
523                                                 intemp >> seq1 >> seq2 >> dist;   m->gobble(intemp);
524                                                         
525                                                 seqDist tempDist(seq1, seq2, dist);
526                                                 calcDists[k].push_back(tempDist);
527                                         }
528                                 }
529                                 intemp.close();
530                                 remove(tempdistFileName.c_str());
531                         }
532                         
533                 }
534 #else
535                 driver(thisLookup, 0, numGroups, calcDists);
536 #endif
537                 
538                 for (int i = 0; i < calcDists.size(); i++) {
539                         if (m->control_pressed) { break; }
540                                 
541                         //initialize matrix
542                         vector< vector<float> > matrix; //square matrix to represent the distance
543                         matrix.resize(thisLookup.size());
544                         for (int k = 0; k < thisLookup.size(); k++) {  matrix[k].resize(thisLookup.size(), 0.0); }
545                                 
546                         for (int j = 0; j < calcDists[i].size(); j++) {
547                                 int row = calcDists[i][j].seq1;
548                                 int column = calcDists[i][j].seq2;
549                                 float dist = calcDists[i][j].dist;
550                                         
551                                 matrix[row][column] = dist;
552                                 matrix[column][row] = dist;
553                         }
554                         
555                         string distFileName = outputDir + m->getRootName(m->getSimpleName(sharedfile)) + matrixCalculators[i]->getName() + "." + thisLookup[0]->getLabel()  + "." + output + ".dist";
556                         outputNames.push_back(distFileName); outputTypes["phylip"].push_back(distFileName);
557                         ofstream outDist;
558                         m->openOutputFile(distFileName, outDist);
559                         outDist.setf(ios::fixed, ios::floatfield); outDist.setf(ios::showpoint);
560                         
561                         printSims(outDist, matrix);
562                         
563                         outDist.close();
564                 }
565                 
566                 return 0;
567         }
568         catch(exception& e) {
569                 m->errorOut(e, "MatrixOutputCommand", "process");
570                 exit(1);
571         }
572 }
573 /**************************************************************************************************/
574 int MatrixOutputCommand::driver(vector<SharedRAbundVector*> thisLookup, int start, int end, vector< vector<seqDist> >& calcDists) { 
575         try {
576                 
577                 vector<SharedRAbundVector*> subset;
578                 for (int k = start; k < end; k++) { // pass cdd each set of groups to compare
579                         
580                         for (int l = 0; l < k; l++) {
581                                 
582                                 if (k != l) { //we dont need to similiarity of a groups to itself
583                                         subset.clear(); //clear out old pair of sharedrabunds
584                                         //add new pair of sharedrabunds
585                                         subset.push_back(thisLookup[k]); subset.push_back(thisLookup[l]); 
586                                         
587                                         for(int i=0;i<matrixCalculators.size();i++) {
588                                                 
589                                                 //if this calc needs all groups to calculate the pair load all groups
590                                                 if (matrixCalculators[i]->getNeedsAll()) { 
591                                                         //load subset with rest of lookup for those calcs that need everyone to calc for a pair
592                                                         for (int w = 0; w < thisLookup.size(); w++) {
593                                                                 if ((w != k) && (w != l)) { subset.push_back(thisLookup[w]); }
594                                                         }
595                                                 }
596                                                 
597                                                 vector<double> tempdata = matrixCalculators[i]->getValues(subset); //saves the calculator outputs
598                                                 
599                                                 if (m->control_pressed) { return 1; }
600                                                 
601                                                 seqDist temp(l, k, (1.0 - tempdata[0]));
602                                                 calcDists[i].push_back(temp);
603                                         }
604                                 }
605                         }
606                 }
607                 
608                 return 0;
609         }
610         catch(exception& e) {
611                 m->errorOut(e, "MatrixOutputCommand", "driver");
612                 exit(1);
613         }
614 }
615 /***********************************************************/
616
617
618