]> git.donarmstrong.com Git - mothur.git/blob - treegroupscommand.cpp
Merge remote-tracking branch 'origin/master'
[mothur.git] / treegroupscommand.cpp
1 /*
2  *  treegroupscommand.cpp
3  *  Mothur
4  *
5  *  Created by Sarah Westcott on 4/8/09.
6  *  Copyright 2009 Schloss Lab UMASS Amherst. All rights reserved.
7  *
8  */
9
10 #include "treegroupscommand.h"
11 #include "subsample.h"
12 #include "consensus.h"
13
14 //**********************************************************************************************************************
15 vector<string> TreeGroupCommand::setParameters(){       
16         try {
17                 CommandParameter pshared("shared", "InputTypes", "", "", "PhylipColumnShared", "PhylipColumnShared", "none",false,false); parameters.push_back(pshared);
18                 CommandParameter pphylip("phylip", "InputTypes", "", "", "PhylipColumnShared", "PhylipColumnShared", "none",false,false); parameters.push_back(pphylip);
19                 CommandParameter pname("name", "InputTypes", "", "", "NameCount", "none", "ColumnName",false,false); parameters.push_back(pname);
20                 CommandParameter pcount("count", "InputTypes", "", "", "NameCount", "none", "countcolumn",false,false); parameters.push_back(pcount);
21         CommandParameter pcolumn("column", "InputTypes", "", "", "PhylipColumnShared", "PhylipColumnShared", "ColumnName-countcolumn",false,false); parameters.push_back(pcolumn);              
22         CommandParameter piters("iters", "Number", "", "1000", "", "", "",false,false); parameters.push_back(piters);
23         CommandParameter psubsample("subsample", "String", "", "", "", "", "",false,false); parameters.push_back(psubsample);
24         CommandParameter pcutoff("cutoff", "Number", "", "10", "", "", "",false,false); parameters.push_back(pcutoff);
25                 CommandParameter pprecision("precision", "Number", "", "100", "", "", "",false,false); parameters.push_back(pprecision);                
26                 CommandParameter plabel("label", "String", "", "", "", "", "",false,false); parameters.push_back(plabel);
27                 CommandParameter pgroups("groups", "String", "", "", "", "", "",false,false); parameters.push_back(pgroups);
28                 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);
29                 
30         CommandParameter pprocessors("processors", "Number", "", "1", "", "", "",false,false); parameters.push_back(pprocessors);
31 //CommandParameter poutput("output", "Multiple", "lt-square", "lt", "", "", "",false,false); parameters.push_back(poutput);
32                 CommandParameter pinputdir("inputdir", "String", "", "", "", "", "",false,false); parameters.push_back(pinputdir);
33                 CommandParameter poutputdir("outputdir", "String", "", "", "", "", "",false,false); parameters.push_back(poutputdir);
34                 
35                 vector<string> myArray;
36                 for (int i = 0; i < parameters.size(); i++) {   myArray.push_back(parameters[i].name);          }
37                 return myArray;
38         }
39         catch(exception& e) {
40                 m->errorOut(e, "TreeGroupCommand", "setParameters");
41                 exit(1);
42         }
43 }
44 //**********************************************************************************************************************
45 string TreeGroupCommand::getHelpString(){       
46         try {
47                 string helpString = "";
48                 ValidCalculators validCalculator;
49                 helpString += "The tree.shared command creates a .tre to represent the similiarity between groups or sequences.\n";
50                 helpString += "The tree.shared command parameters are shared, groups, calc, phylip, column, name, cutoff, precision, processors, subsample, iters and label.\n";
51                 helpString += "The groups parameter allows you to specify which of the groups in your groupfile you would like included used.\n";
52                 helpString += "The group names are separated by dashes. The label allow you to select what distance levels you would like trees created for, and are also separated by dashes.\n";
53                 helpString += "The phylip or column parameter are required if you do not provide a sharedfile, and only one may be used.  If you use a column file the name filename is required. \n";
54                 helpString += "If you do not provide a cutoff value 10.00 is assumed. If you do not provide a precision value then 100 is assumed.\n";
55                 helpString += "The tree.shared command should be in the following format: tree.shared(groups=yourGroups, calc=yourCalcs, label=yourLabels).\n";
56         helpString += "The iters parameter allows you to choose the number of times you would like to run the subsample.\n";
57         helpString += "The subsample parameter allows you to enter the size pergroup of the sample or you can set subsample=T and mothur will use the size of your smallest group. The subsample parameter may only be used with a shared file.\n";
58                 helpString += "Example tree.shared(groups=A-B-C, calc=jabund-sorabund).\n";
59                 helpString += "The default value for groups is all the groups in your groupfile.\n";
60                 helpString += "The default value for calc is jclass-thetayc.\n";
61                 helpString += "The tree.shared command outputs a .tre file for each calculator you specify at each distance you choose.\n";
62                 helpString += validCalculator.printCalc("treegroup");
63                 helpString += "Or the tree.shared command can be in the following format: tree.shared(phylip=yourPhylipFile).\n";
64                 helpString += "Example tree.shared(phylip=abrecovery.dist).\n";
65                 helpString += "Note: No spaces between parameter labels (i.e. groups), '=' and parameters (i.e.yourGroups).\n";
66                 return helpString;
67         }
68         catch(exception& e) {
69                 m->errorOut(e, "TreeGroupCommand", "getHelpString");
70                 exit(1);
71         }
72 }
73 //**********************************************************************************************************************
74 string TreeGroupCommand::getOutputFileNameTag(string type, string inputName=""){        
75         try {
76         string outputFileName = "";
77                 map<string, vector<string> >::iterator it;
78         
79         //is this a type this command creates
80         it = outputTypes.find(type);
81         if (it == outputTypes.end()) {  m->mothurOut("[ERROR]: this command doesn't create a " + type + " output file.\n"); }
82         else {
83             if (type == "tree")            {   outputFileName =  "tre";   }
84             else { m->mothurOut("[ERROR]: No definition for type " + type + " output file tag.\n"); m->control_pressed = true;  }
85         }
86         return outputFileName;
87         }
88         catch(exception& e) {
89                 m->errorOut(e, "TreeGroupCommand", "getOutputFileNameTag");
90                 exit(1);
91         }
92 }
93 //**********************************************************************************************************************
94 TreeGroupCommand::TreeGroupCommand(){   
95         try {
96                 abort = true; calledHelp = true;
97                 setParameters();
98                 //initialize outputTypes
99                 vector<string> tempOutNames;
100                 outputTypes["tree"] = tempOutNames;
101         }
102         catch(exception& e) {
103                 m->errorOut(e, "TreeGroupCommand", "TreeGroupCommand");
104                 exit(1);
105         }
106 }
107 //**********************************************************************************************************************
108
109 TreeGroupCommand::TreeGroupCommand(string option)  {
110         try {
111                 abort = false; calledHelp = false;   
112                 allLines = 1;
113                 
114                 //allow user to run help
115                 if(option == "help") { help(); abort = true; calledHelp = true; }
116                 else if(option == "citation") { citation(); abort = true; calledHelp = true;}
117                 
118                 else {
119                         vector<string> myArray = setParameters();
120                         
121                         OptionParser parser(option);
122                         map<string, string> parameters = parser. getParameters();
123                         
124                         ValidParameters validParameter;
125                         map<string, string>::iterator it;
126                 
127                         //check to make sure all parameters are valid for command
128                         for (it = parameters.begin(); it != parameters.end(); it++) { 
129                                 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) {  abort = true;  }
130                         }
131                         
132                         //initialize outputTypes
133                         vector<string> tempOutNames;
134                         outputTypes["tree"] = tempOutNames;
135                         
136                         //if the user changes the input directory command factory will send this info to us in the output parameter 
137                         string inputDir = validParameter.validFile(parameters, "inputdir", false);              
138                         if (inputDir == "not found"){   inputDir = "";          }
139                         else {
140                                 string path;
141                                 it = parameters.find("phylip");
142                                 //user has given a template file
143                                 if(it != parameters.end()){ 
144                                         path = m->hasPath(it->second);
145                                         //if the user has not given a path then, add inputdir. else leave path alone.
146                                         if (path == "") {       parameters["phylip"] = inputDir + it->second;           }
147                                 }
148                                 
149                                 it = parameters.find("column");
150                                 //user has given a template file
151                                 if(it != parameters.end()){ 
152                                         path = m->hasPath(it->second);
153                                         //if the user has not given a path then, add inputdir. else leave path alone.
154                                         if (path == "") {       parameters["column"] = inputDir + it->second;           }
155                                 }
156                                 
157                                 it = parameters.find("name");
158                                 //user has given a template file
159                                 if(it != parameters.end()){ 
160                                         path = m->hasPath(it->second);
161                                         //if the user has not given a path then, add inputdir. else leave path alone.
162                                         if (path == "") {       parameters["name"] = inputDir + it->second;             }
163                                 }
164                 
165                 it = parameters.find("count");
166                                 //user has given a template file
167                                 if(it != parameters.end()){ 
168                                         path = m->hasPath(it->second);
169                                         //if the user has not given a path then, add inputdir. else leave path alone.
170                                         if (path == "") {       parameters["count"] = inputDir + it->second;            }
171                                 }
172                         }
173                         
174                         //check for required parameters
175                         phylipfile = validParameter.validFile(parameters, "phylip", true);
176                         if (phylipfile == "not open") { phylipfile = ""; abort = true; }
177                         else if (phylipfile == "not found") { phylipfile = ""; }        
178                         else {  inputfile = phylipfile;  format = "phylip"; m->setPhylipFile(phylipfile);       }
179                         
180                         columnfile = validParameter.validFile(parameters, "column", true);
181                         if (columnfile == "not open") { columnfile = ""; abort = true; }        
182                         else if (columnfile == "not found") { columnfile = ""; }
183                         else {  inputfile = columnfile; format = "column";      m->setColumnFile(columnfile); }
184                         
185                         sharedfile = validParameter.validFile(parameters, "shared", true);
186                         if (sharedfile == "not open") { sharedfile = ""; abort = true; }        
187                         else if (sharedfile == "not found") { sharedfile = ""; }
188                         else {  inputfile = sharedfile; format = "sharedfile";  m->setSharedFile(sharedfile); }
189                         
190                         namefile = validParameter.validFile(parameters, "name", true);
191                         if (namefile == "not open") { abort = true; }   
192                         else if (namefile == "not found") { namefile = ""; }
193                         else { m->setNameFile(namefile); }
194             
195             countfile = validParameter.validFile(parameters, "count", true);
196                         if (countfile == "not open") { abort = true; countfile = ""; }  
197                         else if (countfile == "not found") { countfile = ""; }
198                         else { m->setCountTableFile(countfile); }
199                         
200                         if ((phylipfile == "") && (columnfile == "") && (sharedfile == "")) { 
201                                 //is there are current file available for either of these?
202                                 //give priority to shared, then column, then phylip
203                                 sharedfile = m->getSharedFile(); 
204                                 if (sharedfile != "") {  inputfile = sharedfile; format = "sharedfile"; m->mothurOut("Using " + sharedfile + " as input file for the shared parameter."); m->mothurOutEndLine(); }
205                                 else { 
206                                         columnfile = m->getColumnFile(); 
207                                         if (columnfile != "") { inputfile = columnfile; format = "column";  m->mothurOut("Using " + columnfile + " as input file for the column parameter."); m->mothurOutEndLine(); }
208                                         else { 
209                                                 phylipfile = m->getPhylipFile(); 
210                                                 if (phylipfile != "") { inputfile = phylipfile;  format = "phylip";  m->mothurOut("Using " + phylipfile + " as input file for the phylip parameter."); m->mothurOutEndLine(); }
211                                                 else { 
212                                                         m->mothurOut("No valid current files. You must provide a shared, phylip or column file."); m->mothurOutEndLine(); 
213                                                         abort = true;
214                                                 }
215                                         }
216                                 }
217                         }
218                         else if ((phylipfile != "") && (columnfile != "")) { m->mothurOut("When running the tree.shared command with a distance file you may not use both the column and the phylip parameters."); m->mothurOutEndLine(); abort = true; }
219                         
220                         if (columnfile != "") {
221                                 if ((namefile == "") && (countfile == "")){ 
222                                         namefile = m->getNameFile(); 
223                                         if (namefile != "") {  m->mothurOut("Using " + namefile + " as input file for the name parameter."); m->mothurOutEndLine(); }
224                                         else { 
225                                                 countfile = m->getCountTableFile();
226                         if (countfile != "") {  m->mothurOut("Using " + countfile + " as input file for the count parameter."); m->mothurOutEndLine(); }
227                         else { 
228                             m->mothurOut("You need to provide a namefile or countfile if you are going to use the column format."); m->mothurOutEndLine(); 
229                             abort = true; 
230                         }       
231                                         }       
232                                 }
233                         }
234
235                         
236                         //check for optional parameter and set defaults
237                         // ...at some point should added some additional type checking...
238                         label = validParameter.validFile(parameters, "label", false);                   
239                         if (label == "not found") { label = ""; }
240                         else { 
241                                 if(label != "all") {  m->splitAtDash(label, labels);  allLines = 0;  }
242                                 else { allLines = 1;  }
243                         }
244                         
245                         groups = validParameter.validFile(parameters, "groups", false);                 
246                         if (groups == "not found") { groups = ""; }
247                         else { 
248                                 m->splitAtDash(groups, Groups);
249                                 m->setGroups(Groups);
250                         }
251                                 
252                         calc = validParameter.validFile(parameters, "calc", false);                     
253                         if (calc == "not found") { calc = "jclass-thetayc";  }
254                         else { 
255                                  if (calc == "default")  {  calc = "jclass-thetayc";  }
256                         }
257                         m->splitAtDash(calc, Estimators);
258                         if (m->inUsersGroups("citation", Estimators)) { 
259                                 ValidCalculators validCalc; validCalc.printCitations(Estimators); 
260                                 //remove citation from list of calcs
261                                 for (int i = 0; i < Estimators.size(); i++) { if (Estimators[i] == "citation") {  Estimators.erase(Estimators.begin()+i); break; } }
262                         }
263
264                         string temp;
265                         temp = validParameter.validFile(parameters, "precision", false);                        if (temp == "not found") { temp = "100"; }
266                         m->mothurConvert(temp, precision); 
267                         
268                         temp = validParameter.validFile(parameters, "cutoff", false);                   if (temp == "not found") { temp = "10"; }
269                         m->mothurConvert(temp, cutoff); 
270                         cutoff += (5 / (precision * 10.0));
271                         
272             temp = validParameter.validFile(parameters, "processors", false);   if (temp == "not found"){       temp = m->getProcessors();      }
273                         m->setProcessors(temp);
274                         m->mothurConvert(temp, processors); 
275             
276             temp = validParameter.validFile(parameters, "iters", false);                        if (temp == "not found") { temp = "1000"; }
277                         m->mothurConvert(temp, iters); 
278             
279             temp = validParameter.validFile(parameters, "subsample", false);            if (temp == "not found") { temp = "F"; }
280                         if (m->isNumeric1(temp)) { m->mothurConvert(temp, subsampleSize); subsample = true; }
281             else {  
282                 if (m->isTrue(temp)) { subsample = true; subsampleSize = -1; }  //we will set it to smallest group later 
283                 else { subsample = false; }
284             }
285             
286             if (subsample == false) { iters = 1; }
287             
288             if (subsample && (format != "sharedfile")) { m->mothurOut("[ERROR]: the subsample parameter can only be used with a shared file.\n"); abort=true; }
289             
290                         //if the user changes the output directory command factory will send this info to us in the output parameter 
291                         outputDir = validParameter.validFile(parameters, "outputdir", false);           if (outputDir == "not found"){  
292                                 outputDir = ""; 
293                                 outputDir += m->hasPath(inputfile); //if user entered a file with a path then preserve it       
294                         }
295                 }
296
297         }
298         catch(exception& e) {
299                 m->errorOut(e, "TreeGroupCommand", "TreeGroupCommand");
300                 exit(1);
301         }
302 }
303 //**********************************************************************************************************************
304
305 TreeGroupCommand::~TreeGroupCommand(){
306         if (abort == false) {
307                 if (format == "sharedfile") {  delete input; }
308                 else { delete list; }
309                 delete ct;  
310         }
311         
312 }
313
314 //**********************************************************************************************************************
315
316 int TreeGroupCommand::execute(){
317         try {
318         
319                 if (abort == true) { if (calledHelp) { return 0; }  return 2;   }
320                 
321                 if (format == "sharedfile") {
322                         
323                         ValidCalculators validCalculator;
324                 
325                         for (int i=0; i<Estimators.size(); i++) {
326                                 if (validCalculator.isValidCalculator("treegroup", Estimators[i]) == true) { 
327                                         if (Estimators[i] == "sharedsobs") { 
328                                                 treeCalculators.push_back(new SharedSobsCS());
329                                         }else if (Estimators[i] == "sharedchao") { 
330                                                 treeCalculators.push_back(new SharedChao1());
331                                         }else if (Estimators[i] == "sharedace") { 
332                                                 treeCalculators.push_back(new SharedAce());
333                                         }else if (Estimators[i] == "jabund") {  
334                                                 treeCalculators.push_back(new JAbund());
335                                         }else if (Estimators[i] == "sorabund") { 
336                                                 treeCalculators.push_back(new SorAbund());
337                                         }else if (Estimators[i] == "jclass") { 
338                                                 treeCalculators.push_back(new Jclass());
339                                         }else if (Estimators[i] == "sorclass") { 
340                                                 treeCalculators.push_back(new SorClass());
341                                         }else if (Estimators[i] == "jest") { 
342                                                 treeCalculators.push_back(new Jest());
343                                         }else if (Estimators[i] == "sorest") { 
344                                                 treeCalculators.push_back(new SorEst());
345                                         }else if (Estimators[i] == "thetayc") { 
346                                                 treeCalculators.push_back(new ThetaYC());
347                                         }else if (Estimators[i] == "thetan") { 
348                                                 treeCalculators.push_back(new ThetaN());
349                                         }else if (Estimators[i] == "kstest") { 
350                                                 treeCalculators.push_back(new KSTest());
351                                         }else if (Estimators[i] == "sharednseqs") { 
352                                                 treeCalculators.push_back(new SharedNSeqs());
353                                         }else if (Estimators[i] == "ochiai") { 
354                                                 treeCalculators.push_back(new Ochiai());
355                                         }else if (Estimators[i] == "anderberg") { 
356                                                 treeCalculators.push_back(new Anderberg());
357                                         }else if (Estimators[i] == "kulczynski") { 
358                                                 treeCalculators.push_back(new Kulczynski());
359                                         }else if (Estimators[i] == "kulczynskicody") { 
360                                                 treeCalculators.push_back(new KulczynskiCody());
361                                         }else if (Estimators[i] == "lennon") { 
362                                                 treeCalculators.push_back(new Lennon());
363                                         }else if (Estimators[i] == "morisitahorn") { 
364                                                 treeCalculators.push_back(new MorHorn());
365                                         }else if (Estimators[i] == "braycurtis") { 
366                                                 treeCalculators.push_back(new BrayCurtis());
367                                         }else if (Estimators[i] == "whittaker") { 
368                                                 treeCalculators.push_back(new Whittaker());
369                                         }else if (Estimators[i] == "odum") { 
370                                                 treeCalculators.push_back(new Odum());
371                                         }else if (Estimators[i] == "canberra") { 
372                                                 treeCalculators.push_back(new Canberra());
373                                         }else if (Estimators[i] == "structeuclidean") { 
374                                                 treeCalculators.push_back(new StructEuclidean());
375                                         }else if (Estimators[i] == "structchord") { 
376                                                 treeCalculators.push_back(new StructChord());
377                                         }else if (Estimators[i] == "hellinger") { 
378                                                 treeCalculators.push_back(new Hellinger());
379                                         }else if (Estimators[i] == "manhattan") { 
380                                                 treeCalculators.push_back(new Manhattan());
381                                         }else if (Estimators[i] == "structpearson") { 
382                                                 treeCalculators.push_back(new StructPearson());
383                                         }else if (Estimators[i] == "soergel") { 
384                                                 treeCalculators.push_back(new Soergel());
385                                         }else if (Estimators[i] == "spearman") { 
386                                                 treeCalculators.push_back(new Spearman());
387                                         }else if (Estimators[i] == "structkulczynski") { 
388                                                 treeCalculators.push_back(new StructKulczynski());
389                                         }else if (Estimators[i] == "speciesprofile") { 
390                                                 treeCalculators.push_back(new SpeciesProfile());
391                                         }else if (Estimators[i] == "hamming") { 
392                                                 treeCalculators.push_back(new Hamming());
393                                         }else if (Estimators[i] == "structchi2") { 
394                                                 treeCalculators.push_back(new StructChi2());
395                                         }else if (Estimators[i] == "gower") { 
396                                                 treeCalculators.push_back(new Gower());
397                                         }else if (Estimators[i] == "memchi2") { 
398                                                 treeCalculators.push_back(new MemChi2());
399                                         }else if (Estimators[i] == "memchord") { 
400                                                 treeCalculators.push_back(new MemChord());
401                                         }else if (Estimators[i] == "memeuclidean") { 
402                                                 treeCalculators.push_back(new MemEuclidean());
403                                         }else if (Estimators[i] == "mempearson") { 
404                                                 treeCalculators.push_back(new MemPearson());
405                                         }
406                                 }
407                         }
408                         
409                         //if the users entered no valid calculators don't execute command
410                         if (treeCalculators.size() == 0) { m->mothurOut("You have given no valid calculators."); m->mothurOutEndLine(); return 0; }
411                         
412                         input = new InputData(sharedfile, "sharedfile");
413                         lookup = input->getSharedRAbundVectors();
414                         lastLabel = lookup[0]->getLabel();
415                         
416                         if (lookup.size() < 2) { m->mothurOut("You have not provided enough valid groups.  I cannot run the command."); m->mothurOutEndLine(); return 0; }
417                         
418                         //used in tree constructor 
419                         m->runParse = false;
420                         
421                         //create treemap class from groupmap for tree class to use
422                         ct = new CountTable();
423             set<string> nameMap;
424             map<string, string> groupMap;
425             set<string> gps;
426             for (int i = 0; i < m->getAllGroups().size(); i++) { 
427                 nameMap.insert(m->getAllGroups()[i]); 
428                 gps.insert(m->getAllGroups()[i]); 
429                 groupMap[m->getAllGroups()[i]] = m->getAllGroups()[i];
430             }
431             ct->createTable(nameMap, groupMap, gps);
432                         
433                         //clear globaldatas old tree names if any
434                         m->Treenames.clear();
435                         
436                         //fills globaldatas tree names
437                         m->Treenames = m->getGroups();
438                 
439                         if (m->control_pressed) { return 0; }
440                         
441                         //create tree file
442                         makeSimsShared();
443                         
444                         if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) {        m->mothurRemove(outputNames[i]);  } return 0; }
445                 }else{
446                         //read in dist file
447                         filename = inputfile;
448             
449             ReadMatrix* readMatrix;
450                         if (format == "column") { readMatrix = new ReadColumnMatrix(filename); }        
451                         else if (format == "phylip") { readMatrix = new ReadPhylipMatrix(filename); }
452                                 
453                         readMatrix->setCutoff(cutoff);
454         
455             ct = NULL;
456             nameMap = NULL;
457             if(namefile != ""){ 
458                 nameMap = new NameAssignment(namefile);
459                 nameMap->readMap();
460                 readMatrix->read(nameMap);
461             }else if (countfile != "") {
462                 ct = new CountTable();
463                 ct->readTable(countfile);
464                 readMatrix->read(ct);
465             }else {
466                 readMatrix->read(nameMap);
467             }
468
469                         list = readMatrix->getListVector();
470                         SparseDistanceMatrix* dMatrix = readMatrix->getDMatrix();
471
472                         //make treemap
473             if (ct != NULL) { delete ct; }
474                         ct = new CountTable();
475             set<string> nameMap;
476             map<string, string> groupMap;
477             set<string> gps;
478             for (int i = 0; i < list->getNumBins(); i++) {
479                 string bin = list->get(i);
480                 nameMap.insert(bin); 
481                 gps.insert(bin); 
482                 groupMap[bin] = bin;
483             }
484             ct->createTable(nameMap, groupMap, gps);
485                         
486                         vector<string> namesGroups = ct->getNamesOfGroups();
487                         m->setGroups(namesGroups);
488                 
489                         //clear globaldatas old tree names if any
490                         m->Treenames.clear();
491             
492                         //fills globaldatas tree names
493                         m->Treenames = m->getGroups();
494                         
495                         //used in tree constructor 
496                         m->runParse = false;
497                         
498                         if (m->control_pressed) { return 0; }
499                         
500                         vector< vector<double> > matrix = makeSimsDist(dMatrix);
501             delete readMatrix;
502             delete dMatrix;
503                         
504                         if (m->control_pressed) { return 0; }
505
506                         //create a new filename
507                         string outputFile = outputDir + m->getRootName(m->getSimpleName(inputfile)) + getOutputFileNameTag("tree");     
508                         outputNames.push_back(outputFile); outputTypes["tree"].push_back(outputFile);
509                                 
510                         Tree* newTree = createTree(matrix);
511             
512             if (newTree != NULL) {  writeTree(outputFile, newTree); delete newTree; }
513                         
514                         if (m->control_pressed) { return 0; }
515
516                         m->mothurOut("Tree complete. "); m->mothurOutEndLine();
517                         
518                 }
519                                 
520                 //reset groups parameter
521                 m->clearGroups(); 
522                 
523                 //set tree file as new current treefile
524                 string current = "";
525                 itTypes = outputTypes.find("tree");
526                 if (itTypes != outputTypes.end()) {
527                         if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setTreeFile(current); }
528                 }
529                 
530                 m->mothurOutEndLine();
531                 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
532                 for (int i = 0; i < outputNames.size(); i++) {  m->mothurOut(outputNames[i]); m->mothurOutEndLine();    }
533                 m->mothurOutEndLine();
534
535                 return 0;
536         }
537         catch(exception& e) {
538                 m->errorOut(e, "TreeGroupCommand", "execute");
539                 exit(1);
540         }
541 }
542 //**********************************************************************************************************************
543
544 Tree* TreeGroupCommand::createTree(vector< vector<double> >& simMatrix){
545         try {
546                 //create tree
547                 t = new Tree(ct, simMatrix);
548         
549         if (m->control_pressed) { delete t; t = NULL; return t; }
550                 
551         //assemble tree
552                 t->assembleTree();
553
554                 return t;
555         }
556         catch(exception& e) {
557                 m->errorOut(e, "TreeGroupCommand", "createTree");
558                 exit(1);
559         }
560 }
561 /***********************************************************/
562 int TreeGroupCommand::writeTree(string out, Tree* T) {
563         try {
564                 
565         //print newick file
566                 t->createNewickFile(out);
567                 
568         if (m->control_pressed) { m->mothurRemove(out); outputNames.pop_back(); return 1; }
569         
570         return 0;
571         
572         }
573         catch(exception& e) {
574                 m->errorOut(e, "TreeGroupCommand", "printSims");
575                 exit(1);
576         }
577 }
578
579 /***********************************************************/
580 void TreeGroupCommand::printSims(ostream& out, vector< vector<double> >& simMatrix) {
581         try {
582                 
583         for (int m = 0; m < simMatrix.size(); m++)      {
584                         //out << lookup[m]->getGroup() << '\t';
585                         for (int n = 0; n < simMatrix.size(); n++)      {
586                                 out << simMatrix[m][n] << '\t'; 
587                         }
588                         out << endl;
589                 }
590
591         }
592         catch(exception& e) {
593                 m->errorOut(e, "TreeGroupCommand", "printSims");
594                 exit(1);
595         }
596 }
597 /***********************************************************/
598 vector< vector<double> > TreeGroupCommand::makeSimsDist(SparseDistanceMatrix* matrix) {
599         try {
600                 numGroups = list->size();
601                 
602                 //initialize simMatrix
603                 vector< vector<double> > simMatrix;
604                 simMatrix.resize(numGroups);
605                 for (int k = 0; k < simMatrix.size(); k++)      {
606                         for (int j = 0; j < simMatrix.size(); j++)      {
607                                 simMatrix[k].push_back(0.0);
608                         }
609                 }
610                 
611                 //go through sparse matrix and fill sims
612                 //go through each cell in the sparsematrix
613         for (int i = 0; i < matrix->seqVec.size(); i++) {
614             for (int j = 0; j < matrix->seqVec[i].size(); j++) {
615                 
616                 //already checked everyone else in row
617                 if (i < matrix->seqVec[i][j].index) {   
618                     simMatrix[i][matrix->seqVec[i][j].index] = -(matrix->seqVec[i][j].dist -1.0);       
619                     simMatrix[matrix->seqVec[i][j].index][i] = -(matrix->seqVec[i][j].dist -1.0);       
620                         
621                     if (m->control_pressed) { return simMatrix; }
622                 }
623             }
624                 }
625
626                 return simMatrix;
627         }
628         catch(exception& e) {
629                 m->errorOut(e, "TreeGroupCommand", "makeSimsDist");
630                 exit(1);
631         }
632 }
633
634 /***********************************************************/
635 int TreeGroupCommand::makeSimsShared() {
636         try {
637         
638         if (subsample) { 
639             if (subsampleSize == -1) { //user has not set size, set size = smallest samples size
640                 subsampleSize = lookup[0]->getNumSeqs();
641                 for (int i = 1; i < lookup.size(); i++) {
642                     int thisSize = lookup[i]->getNumSeqs();
643                     
644                     if (thisSize < subsampleSize) {     subsampleSize = thisSize;       }
645                 }
646             }else {
647                 m->clearGroups();
648                 Groups.clear();
649                 vector<SharedRAbundVector*> temp;
650                 for (int i = 0; i < lookup.size(); i++) {
651                     if (lookup[i]->getNumSeqs() < subsampleSize) { 
652                         m->mothurOut(lookup[i]->getGroup() + " contains " + toString(lookup[i]->getNumSeqs()) + ". Eliminating."); m->mothurOutEndLine();
653                         delete lookup[i];
654                     }else { 
655                         Groups.push_back(lookup[i]->getGroup()); 
656                         temp.push_back(lookup[i]);
657                     }
658                 } 
659                 lookup = temp;
660                 m->setGroups(Groups);
661             }
662             
663             if (lookup.size() < 2) { m->mothurOut("You have not provided enough valid groups.  I cannot run the command."); m->mothurOutEndLine(); m->control_pressed = true; return 0; }
664         }
665         
666         numGroups = lookup.size();
667                 lines.resize(processors);
668                 for (int i = 0; i < processors; i++) {
669                         lines[i].start = int (sqrt(float(i)/float(processors)) * numGroups);
670                         lines[i].end = int (sqrt(float(i+1)/float(processors)) * numGroups);
671                 }       
672         
673                 set<string> processedLabels;
674                 set<string> userLabels = labels;
675                 
676                 //as long as you are not at the end of the file or done wih the lines you want
677                 while((lookup[0] != NULL) && ((allLines == 1) || (userLabels.size() != 0))) {
678                         if (m->control_pressed) { for (int i = 0; i < lookup.size(); i++) {  delete lookup[i];  } for(int i = 0 ; i < treeCalculators.size(); i++) {  delete treeCalculators[i]; } return 1; }
679                 
680                         if(allLines == 1 || labels.count(lookup[0]->getLabel()) == 1){                  
681                                 m->mothurOut(lookup[0]->getLabel()); m->mothurOutEndLine();
682                                 process(lookup);
683                                 
684                                 processedLabels.insert(lookup[0]->getLabel());
685                                 userLabels.erase(lookup[0]->getLabel());
686                         }
687                         
688                         if ((m->anyLabelsToProcess(lookup[0]->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
689                                 string saveLabel = lookup[0]->getLabel();
690                         
691                                 for (int i = 0; i < lookup.size(); i++) {  delete lookup[i];  } 
692                                 lookup = input->getSharedRAbundVectors(lastLabel);
693
694                                 m->mothurOut(lookup[0]->getLabel()); m->mothurOutEndLine();
695                                 process(lookup);
696                                         
697                                 processedLabels.insert(lookup[0]->getLabel());
698                                 userLabels.erase(lookup[0]->getLabel());
699                                 
700                                 //restore real lastlabel to save below
701                                 lookup[0]->setLabel(saveLabel);
702                         }
703
704                         lastLabel = lookup[0]->getLabel();                      
705                         
706                         //get next line to process
707                         for (int i = 0; i < lookup.size(); i++) {  delete lookup[i];  } 
708                         lookup = input->getSharedRAbundVectors();
709                 }
710                 
711                 if (m->control_pressed) { for (int i = 0; i < lookup.size(); i++) {  delete lookup[i];  } for(int i = 0 ; i < treeCalculators.size(); i++) {  delete treeCalculators[i]; } return 1; }
712
713                 //output error messages about any remaining user labels
714                 set<string>::iterator it;
715                 bool needToRun = false;
716                 for (it = userLabels.begin(); it != userLabels.end(); it++) {  
717                         m->mothurOut("Your file does not include the label " + *it); 
718                         if (processedLabels.count(lastLabel) != 1) {
719                                 m->mothurOut(". I will use " + lastLabel + "."); m->mothurOutEndLine();
720                                 needToRun = true;
721                         }else {
722                                 m->mothurOut(". Please refer to " + lastLabel + "."); m->mothurOutEndLine();
723                         }
724                 }
725                 
726                 //run last label if you need to
727                 if (needToRun == true)  {
728                         for (int i = 0; i < lookup.size(); i++) {  if (lookup[i] != NULL) {             delete lookup[i]; }             } 
729                         lookup = input->getSharedRAbundVectors(lastLabel);
730
731                         m->mothurOut(lookup[0]->getLabel()); m->mothurOutEndLine();
732                         process(lookup);
733                         for (int i = 0; i < lookup.size(); i++) {  delete lookup[i];  }         
734                 }
735                 
736                 for(int i = 0 ; i < treeCalculators.size(); i++) {  delete treeCalculators[i]; }
737                 
738                 return 0;
739         }
740         catch(exception& e) {
741                 m->errorOut(e, "TreeGroupCommand", "makeSimsShared");
742                 exit(1);
743         }
744 }
745
746 /***********************************************************/
747 int TreeGroupCommand::process(vector<SharedRAbundVector*> thisLookup) {
748         try{
749                 vector< vector< vector<seqDist> > > calcDistsTotals;  //each iter, one for each calc, then each groupCombos dists. this will be used to make .dist files
750         vector< vector<seqDist>  > calcDists; calcDists.resize(treeCalculators.size());                 
751         
752         for (int thisIter = 0; thisIter < iters; thisIter++) {
753             
754             vector<SharedRAbundVector*> thisItersLookup = thisLookup;
755             
756             if (subsample) {
757                 SubSample sample;
758                 vector<string> tempLabels; //dont need since we arent printing the sampled sharedRabunds
759                 
760                 //make copy of lookup so we don't get access violations
761                 vector<SharedRAbundVector*> newLookup;
762                 for (int k = 0; k < thisItersLookup.size(); k++) {
763                     SharedRAbundVector* temp = new SharedRAbundVector();
764                     temp->setLabel(thisItersLookup[k]->getLabel());
765                     temp->setGroup(thisItersLookup[k]->getGroup());
766                     newLookup.push_back(temp);
767                 }
768                 
769                 //for each bin
770                 for (int k = 0; k < thisItersLookup[0]->getNumBins(); k++) {
771                     if (m->control_pressed) { for (int j = 0; j < newLookup.size(); j++) {  delete newLookup[j];  } return 0; }
772                     for (int j = 0; j < thisItersLookup.size(); j++) { newLookup[j]->push_back(thisItersLookup[j]->getAbundance(k), thisItersLookup[j]->getGroup()); }
773                 }
774                 
775                 tempLabels = sample.getSample(newLookup, subsampleSize);
776                 thisItersLookup = newLookup;
777             }
778             
779             if(processors == 1){
780                 driver(thisItersLookup, 0, numGroups, calcDists);
781             }else{
782                 int process = 1;
783                 vector<int> processIDS;
784                 
785 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux) || (__linux__) || (__unix__) || (__unix)
786                 //loop through and create all the processes you want
787                 while (process != processors) {
788                     int pid = fork();
789                     
790                     if (pid > 0) {
791                         processIDS.push_back(pid); 
792                         process++;
793                     }else if (pid == 0){
794                         
795                         driver(thisItersLookup, lines[process].start, lines[process].end, calcDists);   
796                         
797                         string tempdistFileName = m->getRootName(m->getSimpleName(sharedfile)) + toString(getpid()) + ".dist";
798                         ofstream outtemp;
799                         m->openOutputFile(tempdistFileName, outtemp);
800                         
801                         for (int i = 0; i < calcDists.size(); i++) {
802                             outtemp << calcDists[i].size() << endl;
803                             
804                             for (int j = 0; j < calcDists[i].size(); j++) {
805                                 outtemp << calcDists[i][j].seq1 << '\t' << calcDists[i][j].seq2 << '\t' << calcDists[i][j].dist << endl;
806                             }
807                         }
808                         outtemp.close();
809                         
810                         exit(0);
811                     }else { 
812                         m->mothurOut("[ERROR]: unable to spawn the necessary processes."); m->mothurOutEndLine(); 
813                         for (int i = 0; i < processIDS.size(); i++) { kill (processIDS[i], SIGINT); }
814                         exit(0);
815                     }
816                 }
817                 
818                 //parent do your part
819                 driver(thisItersLookup, lines[0].start, lines[0].end, calcDists);   
820                 
821                 //force parent to wait until all the processes are done
822                 for (int i = 0; i < processIDS.size(); i++) {
823                     int temp = processIDS[i];
824                     wait(&temp);
825                 }
826                 
827                 for (int i = 0; i < processIDS.size(); i++) {
828                     string tempdistFileName = m->getRootName(m->getSimpleName(sharedfile)) + toString(processIDS[i]) +  ".dist";
829                     ifstream intemp;
830                     m->openInputFile(tempdistFileName, intemp);
831                     
832                     for (int k = 0; k < calcDists.size(); k++) {
833                         int size = 0;
834                         intemp >> size; m->gobble(intemp);
835                         
836                         for (int j = 0; j < size; j++) {
837                             int seq1 = 0;
838                             int seq2 = 0;
839                             float dist = 1.0;
840                             
841                             intemp >> seq1 >> seq2 >> dist;   m->gobble(intemp);
842                             
843                             seqDist tempDist(seq1, seq2, dist);
844                             calcDists[k].push_back(tempDist);
845                         }
846                     }
847                     intemp.close();
848                     m->mothurRemove(tempdistFileName);
849                 }
850 #else
851                 //////////////////////////////////////////////////////////////////////////////////////////////////////
852                 //Windows version shared memory, so be careful when passing variables through the treeSharedData struct. 
853                 //Above fork() will clone, so memory is separate, but that's not the case with windows, 
854                 //Taking advantage of shared memory to pass results vectors.
855                 //////////////////////////////////////////////////////////////////////////////////////////////////////
856                 
857                 vector<treeSharedData*> pDataArray; 
858                 DWORD   dwThreadIdArray[processors-1];
859                 HANDLE  hThreadArray[processors-1]; 
860                 
861                 //Create processor worker threads.
862                 for( int i=1; i<processors; i++ ){
863                     
864                     //make copy of lookup so we don't get access violations
865                     vector<SharedRAbundVector*> newLookup;
866                     for (int k = 0; k < thisItersLookup.size(); k++) {
867                         SharedRAbundVector* temp = new SharedRAbundVector();
868                         temp->setLabel(thisItersLookup[k]->getLabel());
869                         temp->setGroup(thisItersLookup[k]->getGroup());
870                         newLookup.push_back(temp);
871                     }
872                     
873                     //for each bin
874                     for (int k = 0; k < thisItersLookup[0]->getNumBins(); k++) {
875                         if (m->control_pressed) { for (int j = 0; j < newLookup.size(); j++) {  delete newLookup[j];  } return 0; }
876                         for (int j = 0; j < thisItersLookup.size(); j++) { newLookup[j]->push_back(thisItersLookup[j]->getAbundance(k), thisItersLookup[j]->getGroup()); }
877                     }
878                     
879                     // Allocate memory for thread data.
880                     treeSharedData* tempSum = new treeSharedData(m, lines[i].start, lines[i].end, Estimators, newLookup);
881                     pDataArray.push_back(tempSum);
882                     processIDS.push_back(i);
883                     
884                     hThreadArray[i-1] = CreateThread(NULL, 0, MyTreeSharedThreadFunction, pDataArray[i-1], 0, &dwThreadIdArray[i-1]);   
885                 }
886                 
887                 //parent do your part
888                 driver(thisItersLookup, lines[0].start, lines[0].end, calcDists);   
889                 
890                 //Wait until all threads have terminated.
891                 WaitForMultipleObjects(processors-1, hThreadArray, TRUE, INFINITE);
892                 
893                 //Close all thread handles and free memory allocations.
894                 for(int i=0; i < pDataArray.size(); i++){
895                     for (int j = 0; j < pDataArray[i]->thisLookup.size(); j++) {  delete pDataArray[i]->thisLookup[j];  } 
896                     
897                     for (int k = 0; k < calcDists.size(); k++) {
898                         int size = pDataArray[i]->calcDists[k].size();
899                         for (int j = 0; j < size; j++) {    calcDists[k].push_back(pDataArray[i]->calcDists[k][j]);    }
900                     }
901                     
902                     CloseHandle(hThreadArray[i]);
903                     delete pDataArray[i];
904                 }
905                 
906 #endif
907             }
908             
909             calcDistsTotals.push_back(calcDists);
910             
911             if (subsample) {  
912                 
913                 //clean up memory
914                 for (int i = 0; i < thisItersLookup.size(); i++) { delete thisItersLookup[i]; }
915                 thisItersLookup.clear();
916                 for (int i = 0; i < calcDists.size(); i++) {  calcDists[i].clear(); }
917             }
918                 }
919                 
920         if (iters != 1) {
921             //we need to find the average distance and standard deviation for each groups distance
922             
923             vector< vector<seqDist>  > calcAverages; calcAverages.resize(treeCalculators.size()); 
924             for (int i = 0; i < calcAverages.size(); i++) {  //initialize sums to zero.
925                 calcAverages[i].resize(calcDistsTotals[0][i].size());
926                 
927                 for (int j = 0; j < calcAverages[i].size(); j++) {
928                     calcAverages[i][j].seq1 = calcDists[i][j].seq1;
929                     calcAverages[i][j].seq2 = calcDists[i][j].seq2;
930                     calcAverages[i][j].dist = 0.0;
931                 }
932             }
933             
934             for (int thisIter = 0; thisIter < iters; thisIter++) { //sum all groups dists for each calculator
935                 for (int i = 0; i < calcAverages.size(); i++) {  //initialize sums to zero.
936                     for (int j = 0; j < calcAverages[i].size(); j++) {
937                         calcAverages[i][j].dist += calcDistsTotals[thisIter][i][j].dist;
938                     }
939                 }
940             }
941             
942             for (int i = 0; i < calcAverages.size(); i++) {  //finds average.
943                 for (int j = 0; j < calcAverages[i].size(); j++) {
944                     calcAverages[i][j].dist /= (float) iters;
945                 }
946             }
947             
948             //create average tree for each calc
949             for (int i = 0; i < calcDists.size(); i++) {
950                 vector< vector<double> > matrix; //square matrix to represent the distance
951                 matrix.resize(thisLookup.size());
952                 for (int k = 0; k < thisLookup.size(); k++) {  matrix[k].resize(thisLookup.size(), 0.0); }
953                 
954                 for (int j = 0; j < calcAverages[i].size(); j++) {
955                     int row = calcAverages[i][j].seq1;
956                     int column = calcAverages[i][j].seq2;
957                     float dist = calcAverages[i][j].dist;
958                     
959                     matrix[row][column] = dist;
960                     matrix[column][row] = dist;
961                 }
962                 
963                 //create a new filename
964                 string outputFile = outputDir + m->getRootName(m->getSimpleName(inputfile)) + treeCalculators[i]->getName() + "." + thisLookup[0]->getLabel() + ".ave." + getOutputFileNameTag("tree");                         
965                 outputNames.push_back(outputFile); outputTypes["tree"].push_back(outputFile); 
966                 
967                 //creates tree from similarity matrix and write out file
968                 Tree* newTree = createTree(matrix);
969                 if (newTree != NULL) { writeTree(outputFile, newTree); }                
970             }
971             
972             //create all trees for each calc and find their consensus tree
973             for (int i = 0; i < calcDists.size(); i++) {
974                 if (m->control_pressed) { break; }
975                 
976                 //create a new filename
977                 string outputFile = outputDir + m->getRootName(m->getSimpleName(inputfile)) + treeCalculators[i]->getName() + "." + thisLookup[0]->getLabel() + ".all." + getOutputFileNameTag("tree");                         
978                 outputNames.push_back(outputFile); outputTypes["tree"].push_back(outputFile); 
979                 
980                 ofstream outAll;
981                 m->openOutputFile(outputFile, outAll);
982                 
983                 vector<Tree*> trees; 
984                 for (int myIter = 0; myIter < iters; myIter++) {
985                     
986                     if(m->control_pressed) { break; }
987                     
988                     //initialize matrix
989                     vector< vector<double> > matrix; //square matrix to represent the distance
990                     matrix.resize(thisLookup.size());
991                     for (int k = 0; k < thisLookup.size(); k++) {  matrix[k].resize(thisLookup.size(), 0.0); }
992                     
993                     for (int j = 0; j < calcDistsTotals[myIter][i].size(); j++) {
994                         int row = calcDistsTotals[myIter][i][j].seq1;
995                         int column = calcDistsTotals[myIter][i][j].seq2;
996                         double dist = calcDistsTotals[myIter][i][j].dist;
997                         
998                         matrix[row][column] = dist;
999                         matrix[column][row] = dist;
1000                     }
1001                     
1002                     //creates tree from similarity matrix and write out file
1003                     Tree* newTree = createTree(matrix);
1004                     if (newTree != NULL) { 
1005                         newTree->print(outAll);
1006                         trees.push_back(newTree);
1007                     }
1008                 }
1009                 outAll.close();
1010                 if (m->control_pressed) { for (int k = 0; k < trees.size(); k++) { delete trees[k]; } }
1011                 
1012                 Consensus consensus;
1013                 //clear old tree names if any
1014                 m->Treenames.clear(); m->Treenames = m->getGroups(); //may have changed if subsample eliminated groups
1015                 Tree* conTree = consensus.getTree(trees);
1016                 
1017                 //create a new filename
1018                 string conFile = outputDir + m->getRootName(m->getSimpleName(inputfile)) + treeCalculators[i]->getName() + "." + thisLookup[0]->getLabel() + ".cons." + getOutputFileNameTag("tree");                           
1019                 outputNames.push_back(conFile); outputTypes["tree"].push_back(conFile); 
1020                 ofstream outTree;
1021                 m->openOutputFile(conFile, outTree);
1022                 
1023                 if (conTree != NULL) { conTree->print(outTree, "boot"); delete conTree; }
1024             }
1025
1026         }else {
1027             
1028             for (int i = 0; i < calcDists.size(); i++) {
1029                 if (m->control_pressed) { break; }
1030                 
1031                 //initialize matrix
1032                 vector< vector<double> > matrix; //square matrix to represent the distance
1033                 matrix.resize(thisLookup.size());
1034                 for (int k = 0; k < thisLookup.size(); k++) {  matrix[k].resize(thisLookup.size(), 0.0); }
1035                 
1036                 for (int j = 0; j < calcDists[i].size(); j++) {
1037                     int row = calcDists[i][j].seq1;
1038                     int column = calcDists[i][j].seq2;
1039                     double dist = calcDists[i][j].dist;
1040                     
1041                     matrix[row][column] = dist;
1042                     matrix[column][row] = dist;
1043                 }
1044                 
1045                 //create a new filename
1046                 string outputFile = outputDir + m->getRootName(m->getSimpleName(inputfile)) + treeCalculators[i]->getName() + "." + thisLookup[0]->getLabel() + "." + getOutputFileNameTag("tree");                             
1047                 outputNames.push_back(outputFile); outputTypes["tree"].push_back(outputFile); 
1048                 
1049                 //creates tree from similarity matrix and write out file
1050                 Tree* newTree = createTree(matrix);
1051                 if (newTree != NULL) { writeTree(outputFile, newTree); delete newTree; }
1052             }
1053         }
1054                 
1055                 return 0;
1056         }
1057         catch(exception& e) {
1058                 m->errorOut(e, "TreeGroupCommand", "process");
1059                 exit(1);
1060         }
1061 }
1062 /**************************************************************************************************/
1063 int TreeGroupCommand::driver(vector<SharedRAbundVector*> thisLookup, int start, int end, vector< vector<seqDist> >& calcDists) { 
1064         try {
1065                 vector<SharedRAbundVector*> subset;
1066                 for (int k = start; k < end; k++) { // pass cdd each set of groups to compare
1067                         
1068                         for (int l = 0; l < k; l++) {
1069                                 
1070                                 if (k != l) { //we dont need to similiarity of a groups to itself
1071                                         subset.clear(); //clear out old pair of sharedrabunds
1072                                         //add new pair of sharedrabunds
1073                                         subset.push_back(thisLookup[k]); subset.push_back(thisLookup[l]); 
1074                                         
1075                                         for(int i=0;i<treeCalculators.size();i++) {
1076                                                 
1077                                                 //if this calc needs all groups to calculate the pair load all groups
1078                                                 if (treeCalculators[i]->getNeedsAll()) { 
1079                                                         //load subset with rest of lookup for those calcs that need everyone to calc for a pair
1080                                                         for (int w = 0; w < thisLookup.size(); w++) {
1081                                                                 if ((w != k) && (w != l)) { subset.push_back(thisLookup[w]); }
1082                                                         }
1083                                                 }
1084                                                 
1085                                                 vector<double> tempdata = treeCalculators[i]->getValues(subset); //saves the calculator outputs
1086                                                 
1087                                                 if (m->control_pressed) { return 1; }
1088                                                 
1089                                                 seqDist temp(l, k, -(tempdata[0]-1.0));
1090                                                 calcDists[i].push_back(temp);
1091                                         }
1092                                 }
1093                         }
1094                 }
1095                 
1096                 return 0;
1097         }
1098         catch(exception& e) {
1099                 m->errorOut(e, "TreeGroupCommand", "driver");
1100                 exit(1);
1101         }
1102 }
1103 /***********************************************************/
1104
1105         
1106