]> git.donarmstrong.com Git - mothur.git/blob - matrixoutputcommand.cpp
working on current change
[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                         else { m->setSharedFile(sharedfile); }
161                         
162                         //if the user changes the output directory command factory will send this info to us in the output parameter 
163                         outputDir = validParameter.validFile(parameters, "outputdir", false);           if (outputDir == "not found"){  
164                                 outputDir = ""; 
165                                 outputDir += m->hasPath(sharedfile); //if user entered a file with a path then preserve it      
166                         }
167                         
168                         //check for optional parameter and set defaults
169                         // ...at some point should added some additional type checking...
170                         label = validParameter.validFile(parameters, "label", false);                   
171                         if (label == "not found") { label = ""; }
172                         else { 
173                                 if(label != "all") {  m->splitAtDash(label, labels);  allLines = 0;  }
174                                 else { allLines = 1;  }
175                         }
176                         
177                         output = validParameter.validFile(parameters, "output", false);         if(output == "not found"){      output = "lt"; }
178                         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"; }
179                         
180                         groups = validParameter.validFile(parameters, "groups", false);                 
181                         if (groups == "not found") { groups = ""; }
182                         else { 
183                                 m->splitAtDash(groups, Groups);
184                                 m->Groups = Groups;
185                         }
186                         
187                         string temp = validParameter.validFile(parameters, "processors", false);        if (temp == "not found"){       temp = m->getProcessors();      }
188                         m->setProcessors(temp);
189                         convert(temp, processors); 
190                                 
191                         calc = validParameter.validFile(parameters, "calc", false);                     
192                         if (calc == "not found") { calc = "jclass-thetayc";  }
193                         else { 
194                                  if (calc == "default")  {  calc = "jclass-thetayc";  }
195                         }
196                         m->splitAtDash(calc, Estimators);
197                         if (m->inUsersGroups("citation", Estimators)) { 
198                                 ValidCalculators validCalc; validCalc.printCitations(Estimators); 
199                                 //remove citation from list of calcs
200                                 for (int i = 0; i < Estimators.size(); i++) { if (Estimators[i] == "citation") {  Estimators.erase(Estimators.begin()+i); break; } }
201                         }
202
203                         if (abort == false) {
204                         
205                                 ValidCalculators validCalculator;
206                                 
207                                 int i;
208                                 for (i=0; i<Estimators.size(); i++) {
209                                         if (validCalculator.isValidCalculator("matrix", Estimators[i]) == true) { 
210                                                 if (Estimators[i] == "sharedsobs") { 
211                                                         matrixCalculators.push_back(new SharedSobsCS());
212                                                 }else if (Estimators[i] == "sharedchao") { 
213                                                         matrixCalculators.push_back(new SharedChao1());
214                                                 }else if (Estimators[i] == "sharedace") { 
215                                                         matrixCalculators.push_back(new SharedAce());
216                                                 }else if (Estimators[i] == "jabund") {  
217                                                         matrixCalculators.push_back(new JAbund());
218                                                 }else if (Estimators[i] == "sorabund") { 
219                                                         matrixCalculators.push_back(new SorAbund());
220                                                 }else if (Estimators[i] == "jclass") { 
221                                                         matrixCalculators.push_back(new Jclass());
222                                                 }else if (Estimators[i] == "sorclass") { 
223                                                         matrixCalculators.push_back(new SorClass());
224                                                 }else if (Estimators[i] == "jest") { 
225                                                         matrixCalculators.push_back(new Jest());
226                                                 }else if (Estimators[i] == "sorest") { 
227                                                         matrixCalculators.push_back(new SorEst());
228                                                 }else if (Estimators[i] == "thetayc") { 
229                                                         matrixCalculators.push_back(new ThetaYC());
230                                                 }else if (Estimators[i] == "thetan") { 
231                                                         matrixCalculators.push_back(new ThetaN());
232                                                 }else if (Estimators[i] == "kstest") { 
233                                                         matrixCalculators.push_back(new KSTest());
234                                                 }else if (Estimators[i] == "sharednseqs") { 
235                                                         matrixCalculators.push_back(new SharedNSeqs());
236                                                 }else if (Estimators[i] == "ochiai") { 
237                                                         matrixCalculators.push_back(new Ochiai());
238                                                 }else if (Estimators[i] == "anderberg") { 
239                                                         matrixCalculators.push_back(new Anderberg());
240                                                 }else if (Estimators[i] == "kulczynski") { 
241                                                         matrixCalculators.push_back(new Kulczynski());
242                                                 }else if (Estimators[i] == "kulczynskicody") { 
243                                                         matrixCalculators.push_back(new KulczynskiCody());
244                                                 }else if (Estimators[i] == "lennon") { 
245                                                         matrixCalculators.push_back(new Lennon());
246                                                 }else if (Estimators[i] == "morisitahorn") { 
247                                                         matrixCalculators.push_back(new MorHorn());
248                                                 }else if (Estimators[i] == "braycurtis") { 
249                                                         matrixCalculators.push_back(new BrayCurtis());
250                                                 }else if (Estimators[i] == "whittaker") { 
251                                                         matrixCalculators.push_back(new Whittaker());
252                                                 }else if (Estimators[i] == "odum") { 
253                                                         matrixCalculators.push_back(new Odum());
254                                                 }else if (Estimators[i] == "canberra") { 
255                                                         matrixCalculators.push_back(new Canberra());
256                                                 }else if (Estimators[i] == "structeuclidean") { 
257                                                         matrixCalculators.push_back(new StructEuclidean());
258                                                 }else if (Estimators[i] == "structchord") { 
259                                                         matrixCalculators.push_back(new StructChord());
260                                                 }else if (Estimators[i] == "hellinger") { 
261                                                         matrixCalculators.push_back(new Hellinger());
262                                                 }else if (Estimators[i] == "manhattan") { 
263                                                         matrixCalculators.push_back(new Manhattan());
264                                                 }else if (Estimators[i] == "structpearson") { 
265                                                         matrixCalculators.push_back(new StructPearson());
266                                                 }else if (Estimators[i] == "soergel") { 
267                                                         matrixCalculators.push_back(new Soergel());
268                                                 }else if (Estimators[i] == "spearman") { 
269                                                         matrixCalculators.push_back(new Spearman());
270                                                 }else if (Estimators[i] == "structkulczynski") { 
271                                                         matrixCalculators.push_back(new StructKulczynski());
272                                                 }else if (Estimators[i] == "speciesprofile") { 
273                                                         matrixCalculators.push_back(new SpeciesProfile());
274                                                 }else if (Estimators[i] == "hamming") { 
275                                                         matrixCalculators.push_back(new Hamming());
276                                                 }else if (Estimators[i] == "structchi2") { 
277                                                         matrixCalculators.push_back(new StructChi2());
278                                                 }else if (Estimators[i] == "gower") { 
279                                                         matrixCalculators.push_back(new Gower());
280                                                 }else if (Estimators[i] == "memchi2") { 
281                                                         matrixCalculators.push_back(new MemChi2());
282                                                 }else if (Estimators[i] == "memchord") { 
283                                                         matrixCalculators.push_back(new MemChord());
284                                                 }else if (Estimators[i] == "memeuclidean") { 
285                                                         matrixCalculators.push_back(new MemEuclidean());
286                                                 }else if (Estimators[i] == "mempearson") { 
287                                                         matrixCalculators.push_back(new MemPearson());
288                                                 }
289                                         }
290                                 }
291                                 
292                         }
293                 }
294                 
295         }
296         catch(exception& e) {
297                 m->errorOut(e, "MatrixOutputCommand", "MatrixOutputCommand");
298                 exit(1);
299         }
300 }
301
302 //**********************************************************************************************************************
303
304 MatrixOutputCommand::~MatrixOutputCommand(){}
305
306 //**********************************************************************************************************************
307
308 int MatrixOutputCommand::execute(){
309         try {
310                 
311                 if (abort == true) { if (calledHelp) { return 0; }  return 2;   }
312                         
313                 //if the users entered no valid calculators don't execute command
314                 if (matrixCalculators.size() == 0) { m->mothurOut("No valid calculators."); m->mothurOutEndLine();  return 0; }
315                         
316                 input = new InputData(sharedfile, "sharedfile");
317                 lookup = input->getSharedRAbundVectors();
318                 string lastLabel = lookup[0]->getLabel();
319                 
320                 //if the users enters label "0.06" and there is no "0.06" in their file use the next lowest label.
321                 set<string> processedLabels;
322                 set<string> userLabels = labels;
323                                         
324                 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;}
325                 
326                 numGroups = lookup.size();
327                 lines.resize(processors);
328                 for (int i = 0; i < processors; i++) {
329                         lines[i].start = int (sqrt(float(i)/float(processors)) * numGroups);
330                         lines[i].end = int (sqrt(float(i+1)/float(processors)) * numGroups);
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, 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