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