]> 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                 }       
331                 
332                 if (m->control_pressed) { delete input; for (int i = 0; i < lookup.size(); i++) {  delete lookup[i];  } m->Groups.clear(); return 0;  }
333                                 
334                 //as long as you are not at the end of the file or done wih the lines you want
335                 while((lookup[0] != NULL) && ((allLines == 1) || (userLabels.size() != 0))) {
336                 
337                         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;  }
338                 
339                         if(allLines == 1 || labels.count(lookup[0]->getLabel()) == 1){                  
340                                 m->mothurOut(lookup[0]->getLabel()); m->mothurOutEndLine();
341                                 process(lookup);
342                                 
343                                 processedLabels.insert(lookup[0]->getLabel());
344                                 userLabels.erase(lookup[0]->getLabel());
345                         }
346                         
347                         if ((m->anyLabelsToProcess(lookup[0]->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
348                                 string saveLabel = lookup[0]->getLabel();
349                                 
350                                 for (int i = 0; i < lookup.size(); i++) {  delete lookup[i];  } 
351                                 lookup = input->getSharedRAbundVectors(lastLabel);
352
353                                 m->mothurOut(lookup[0]->getLabel()); m->mothurOutEndLine();
354                                 process(lookup);
355                                 
356                                 processedLabels.insert(lookup[0]->getLabel());
357                                 userLabels.erase(lookup[0]->getLabel());
358                                 
359                                 //restore real lastlabel to save below
360                                 lookup[0]->setLabel(saveLabel);
361                         }
362
363                         lastLabel = lookup[0]->getLabel();                      
364                         
365                         //get next line to process
366                         for (int i = 0; i < lookup.size(); i++) {  delete lookup[i];  } 
367                         lookup = input->getSharedRAbundVectors();
368                 }
369                 
370                 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;  }
371
372                 //output error messages about any remaining user labels
373                 set<string>::iterator it;
374                 bool needToRun = false;
375                 for (it = userLabels.begin(); it != userLabels.end(); it++) {  
376                         m->mothurOut("Your file does not include the label " + *it);  
377                         if (processedLabels.count(lastLabel) != 1) {
378                                 m->mothurOut(". I will use " + lastLabel + "."); m->mothurOutEndLine();
379                                 needToRun = true;
380                         }else {
381                                 m->mothurOut(". Please refer to " + lastLabel + "."); m->mothurOutEndLine();
382                         }
383                 }
384                 
385                 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;  }
386
387                 //run last label if you need to
388                 if (needToRun == true)  {
389                         for (int i = 0; i < lookup.size(); i++) {  if (lookup[i] != NULL) {  delete lookup[i]; }  } 
390                         lookup = input->getSharedRAbundVectors(lastLabel);
391
392                         m->mothurOut(lookup[0]->getLabel()); m->mothurOutEndLine();
393                         process(lookup);
394                         for (int i = 0; i < lookup.size(); i++) {  delete lookup[i];  } 
395                 }
396                 
397                 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;  }
398                 
399                 //reset groups parameter
400                 m->Groups.clear();  
401                 
402                 //set phylip file as new current phylipfile
403                 string current = "";
404                 itTypes = outputTypes.find("phylip");
405                 if (itTypes != outputTypes.end()) {
406                         if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setPhylipFile(current); }
407                 }
408                 
409                 m->mothurOutEndLine();
410                 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
411                 for (int i = 0; i < outputNames.size(); i++) {  m->mothurOut(outputNames[i]); m->mothurOutEndLine();    }
412                 m->mothurOutEndLine();
413
414
415                 return 0;
416         }
417         catch(exception& e) {
418                 m->errorOut(e, "MatrixOutputCommand", "execute");
419                 exit(1);
420         }
421 }
422 /***********************************************************/
423 void MatrixOutputCommand::printSims(ostream& out, vector< vector<float> >& simMatrix) {
424         try {
425                 
426                 out.setf(ios::fixed, ios::floatfield); out.setf(ios::showpoint);
427                 
428                 //output num seqs
429                 out << simMatrix.size() << endl;
430                 
431                 if (output == "lt") {
432                         for (int m = 0; m < simMatrix.size(); m++)      {
433                                 out << lookup[m]->getGroup() << '\t';
434                                 for (int n = 0; n < m; n++)     {
435                                         out << simMatrix[m][n] << '\t'; 
436                                 }
437                                 out << endl;
438                         }
439                 }else{
440                         for (int m = 0; m < simMatrix.size(); m++)      {
441                                 out << lookup[m]->getGroup() << '\t';
442                                 for (int n = 0; n < simMatrix[m].size(); n++)   {
443                                         out << simMatrix[m][n] << '\t'; 
444                                 }
445                                 out << endl;
446                         }
447                 }
448         }
449         catch(exception& e) {
450                 m->errorOut(e, "MatrixOutputCommand", "printSims");
451                 exit(1);
452         }
453 }
454 /***********************************************************/
455 int MatrixOutputCommand::process(vector<SharedRAbundVector*> thisLookup){
456         try {
457                 EstOutput data;
458                 vector<SharedRAbundVector*> subset;
459                 vector< vector<seqDist> > calcDists; calcDists.resize(matrixCalculators.size()); //one for each calc, this will be used to make .dist files
460                 
461         #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
462                 if(processors == 1){
463                         driver(thisLookup, 0, numGroups, calcDists);
464                 }else{
465                         int process = 1;
466                         vector<int> processIDS;
467                         
468                         //loop through and create all the processes you want
469                         while (process != processors) {
470                                 int pid = fork();
471                                 
472                                 if (pid > 0) {
473                                         processIDS.push_back(pid); 
474                                         process++;
475                                 }else if (pid == 0){
476                                         driver(thisLookup, lines[process].start, lines[process].end, calcDists);   
477                                         
478                                         string tempdistFileName = m->getRootName(m->getSimpleName(sharedfile)) + toString(getpid()) + ".dist";
479                                         ofstream outtemp;
480                                         m->openOutputFile(tempdistFileName, outtemp);
481                                                 
482                                         for (int i = 0; i < calcDists.size(); i++) {
483                                                 outtemp << calcDists[i].size() << endl;
484                                                         
485                                                 for (int j = 0; j < calcDists[i].size(); j++) {
486                                                         outtemp << calcDists[i][j].seq1 << '\t' << calcDists[i][j].seq2 << '\t' << calcDists[i][j].dist << endl;
487                                                 }
488                                         }
489                                         outtemp.close();
490                                                                         
491                                         exit(0);
492                                 }else { 
493                                         m->mothurOut("[ERROR]: unable to spawn the necessary processes."); m->mothurOutEndLine(); 
494                                         for (int i = 0; i < processIDS.size(); i++) { kill (processIDS[i], SIGINT); }
495                                         exit(0);
496                                 }
497                         }
498                         
499                         //parent do your part
500                         driver(thisLookup, lines[0].start, lines[0].end, calcDists);   
501                                                 
502                         //force parent to wait until all the processes are done
503                         for (int i = 0; i < processIDS.size(); i++) {
504                                 int temp = processIDS[i];
505                                 wait(&temp);
506                         }
507                         
508                         for (int i = 0; i < processIDS.size(); i++) {
509                                 string tempdistFileName = m->getRootName(m->getSimpleName(sharedfile)) + toString(processIDS[i]) +  ".dist";
510                                 ifstream intemp;
511                                 m->openInputFile(tempdistFileName, intemp);
512                                         
513                                 for (int k = 0; k < calcDists.size(); k++) {
514                                         int size = 0;
515                                         intemp >> size; m->gobble(intemp);
516                                                 
517                                         for (int j = 0; j < size; j++) {
518                                                 int seq1 = 0;
519                                                 int seq2 = 0;
520                                                 float dist = 1.0;
521                                                         
522                                                 intemp >> seq1 >> seq2 >> dist;   m->gobble(intemp);
523                                                         
524                                                 seqDist tempDist(seq1, seq2, dist);
525                                                 calcDists[k].push_back(tempDist);
526                                         }
527                                 }
528                                 intemp.close();
529                                 remove(tempdistFileName.c_str());
530                         }
531                         
532                 }
533 #else
534                 driver(thisLookup, 0, numGroups, calcDists);
535 #endif
536                 
537                 for (int i = 0; i < calcDists.size(); i++) {
538                         if (m->control_pressed) { break; }
539                                 
540                         //initialize matrix
541                         vector< vector<float> > matrix; //square matrix to represent the distance
542                         matrix.resize(thisLookup.size());
543                         for (int k = 0; k < thisLookup.size(); k++) {  matrix[k].resize(thisLookup.size(), 0.0); }
544                                 
545                         for (int j = 0; j < calcDists[i].size(); j++) {
546                                 int row = calcDists[i][j].seq1;
547                                 int column = calcDists[i][j].seq2;
548                                 float dist = calcDists[i][j].dist;
549                                         
550                                 matrix[row][column] = dist;
551                                 matrix[column][row] = dist;
552                         }
553                         
554                         string distFileName = outputDir + m->getRootName(m->getSimpleName(sharedfile)) + matrixCalculators[i]->getName() + "." + thisLookup[0]->getLabel()  + "." + output + ".dist";
555                         outputNames.push_back(distFileName); outputTypes["phylip"].push_back(distFileName);
556                         ofstream outDist;
557                         m->openOutputFile(distFileName, outDist);
558                         outDist.setf(ios::fixed, ios::floatfield); outDist.setf(ios::showpoint);
559                         
560                         printSims(outDist, matrix);
561                         
562                         outDist.close();
563                 }
564                 
565                 return 0;
566         }
567         catch(exception& e) {
568                 m->errorOut(e, "MatrixOutputCommand", "process");
569                 exit(1);
570         }
571 }
572 /**************************************************************************************************/
573 int MatrixOutputCommand::driver(vector<SharedRAbundVector*> thisLookup, int start, int end, vector< vector<seqDist> >& calcDists) { 
574         try {
575                 
576                 vector<SharedRAbundVector*> subset;
577                 for (int k = start; k < end; k++) { // pass cdd each set of groups to compare
578                         
579                         for (int l = 0; l < k; l++) {
580                                 
581                                 if (k != l) { //we dont need to similiarity of a groups to itself
582                                         subset.clear(); //clear out old pair of sharedrabunds
583                                         //add new pair of sharedrabunds
584                                         subset.push_back(thisLookup[k]); subset.push_back(thisLookup[l]); 
585                                         
586                                         for(int i=0;i<matrixCalculators.size();i++) {
587                                                 
588                                                 //if this calc needs all groups to calculate the pair load all groups
589                                                 if (matrixCalculators[i]->getNeedsAll()) { 
590                                                         //load subset with rest of lookup for those calcs that need everyone to calc for a pair
591                                                         for (int w = 0; w < thisLookup.size(); w++) {
592                                                                 if ((w != k) && (w != l)) { subset.push_back(thisLookup[w]); }
593                                                         }
594                                                 }
595                                                 
596                                                 vector<double> tempdata = matrixCalculators[i]->getValues(subset); //saves the calculator outputs
597                                                 
598                                                 if (m->control_pressed) { return 1; }
599                                                 
600                                                 seqDist temp(l, k, (1.0 - tempdata[0]));
601                                                 calcDists[i].push_back(temp);
602                                         }
603                                 }
604                         }
605                 }
606                 
607                 return 0;
608         }
609         catch(exception& e) {
610                 m->errorOut(e, "MatrixOutputCommand", "driver");
611                 exit(1);
612         }
613 }
614 /***********************************************************/
615
616
617