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