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