]> git.donarmstrong.com Git - mothur.git/blob - treegroupscommand.cpp
added [ERROR] flag if command aborts
[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 "sharedsobscollectsummary.h"
12 #include "sharedchao1.h"
13 #include "sharedace.h"
14 #include "sharednseqs.h"
15 #include "sharedjabund.h"
16 #include "sharedsorabund.h"
17 #include "sharedjclass.h"
18 #include "sharedsorclass.h"
19 #include "sharedjest.h"
20 #include "sharedsorest.h"
21 #include "sharedthetayc.h"
22 #include "sharedthetan.h"
23 #include "sharedkstest.h"
24 #include "whittaker.h"
25 #include "sharedochiai.h"
26 #include "sharedanderbergs.h"
27 #include "sharedkulczynski.h"
28 #include "sharedkulczynskicody.h"
29 #include "sharedlennon.h"
30 #include "sharedmorisitahorn.h"
31 #include "sharedbraycurtis.h"
32 #include "sharedjackknife.h"
33 #include "whittaker.h"
34 #include "odum.h"
35 #include "canberra.h"
36 #include "structeuclidean.h"
37 #include "structchord.h"
38 #include "hellinger.h"
39 #include "manhattan.h"
40 #include "structpearson.h"
41 #include "soergel.h"
42 #include "spearman.h"
43 #include "structkulczynski.h"
44 #include "structchi2.h"
45 #include "speciesprofile.h"
46 #include "hamming.h"
47 #include "gower.h"
48 #include "memchi2.h"
49 #include "memchord.h"
50 #include "memeuclidean.h"
51 #include "mempearson.h"
52
53 //**********************************************************************************************************************
54 vector<string> TreeGroupCommand::getValidParameters(){  
55         try {
56                 string Array[] =  {"label","calc","groups", "phylip", "column", "name", "precision","cutoff","outputdir","inputdir"};
57                 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
58                 return myArray;
59         }
60         catch(exception& e) {
61                 m->errorOut(e, "TreeGroupCommand", "getValidParameters");
62                 exit(1);
63         }
64 }
65 //**********************************************************************************************************************
66 TreeGroupCommand::TreeGroupCommand(){   
67         try {
68                 abort = true; calledHelp = true;
69                 globaldata = GlobalData::getInstance();
70                 //initialize outputTypes
71                 vector<string> tempOutNames;
72                 outputTypes["tree"] = tempOutNames;
73         }
74         catch(exception& e) {
75                 m->errorOut(e, "TreeGroupCommand", "TreeGroupCommand");
76                 exit(1);
77         }
78 }
79 //**********************************************************************************************************************
80 vector<string> TreeGroupCommand::getRequiredParameters(){       
81         try {
82                 vector<string> myArray;
83                 return myArray;
84         }
85         catch(exception& e) {
86                 m->errorOut(e, "TreeGroupCommand", "getRequiredParameters");
87                 exit(1);
88         }
89 }
90 //**********************************************************************************************************************
91 vector<string> TreeGroupCommand::getRequiredFiles(){    
92         try {
93                 string Array[] =  {"phylip","column","shared","or"};
94                 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
95                 return myArray;
96         }
97         catch(exception& e) {
98                 m->errorOut(e, "TreeGroupCommand", "getRequiredFiles");
99                 exit(1);
100         }
101 }
102 //**********************************************************************************************************************
103
104 TreeGroupCommand::TreeGroupCommand(string option)  {
105         try {
106                 globaldata = GlobalData::getInstance();
107                 abort = false; calledHelp = false;   
108                 allLines = 1;
109                 labels.clear();
110                 Groups.clear();
111                 Estimators.clear();
112                 
113                 //allow user to run help
114                 if(option == "help") { validCalculator = new ValidCalculators(); help(); abort = true; calledHelp = true; }
115                 
116                 else {
117                         //valid paramters for this command
118                         string Array[] =  {"label","calc","groups", "phylip", "column", "name", "precision","cutoff","outputdir","inputdir"};
119                         vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
120                         
121                         OptionParser parser(option);
122                         map<string, string> parameters = parser. getParameters();
123                         
124                         ValidParameters validParameter;
125                         map<string, string>::iterator it;
126                 
127                         //check to make sure all parameters are valid for command
128                         for (it = parameters.begin(); it != parameters.end(); it++) { 
129                                 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) {  abort = true;  }
130                         }
131                         
132                         //initialize outputTypes
133                         vector<string> tempOutNames;
134                         outputTypes["tree"] = tempOutNames;
135                         
136                         //if the user changes the input directory command factory will send this info to us in the output parameter 
137                         string inputDir = validParameter.validFile(parameters, "inputdir", false);              
138                         if (inputDir == "not found"){   inputDir = "";          }
139                         else {
140                                 string path;
141                                 it = parameters.find("phylip");
142                                 //user has given a template file
143                                 if(it != parameters.end()){ 
144                                         path = m->hasPath(it->second);
145                                         //if the user has not given a path then, add inputdir. else leave path alone.
146                                         if (path == "") {       parameters["phylip"] = inputDir + it->second;           }
147                                 }
148                                 
149                                 it = parameters.find("column");
150                                 //user has given a template file
151                                 if(it != parameters.end()){ 
152                                         path = m->hasPath(it->second);
153                                         //if the user has not given a path then, add inputdir. else leave path alone.
154                                         if (path == "") {       parameters["column"] = inputDir + it->second;           }
155                                 }
156                                 
157                                 it = parameters.find("name");
158                                 //user has given a template file
159                                 if(it != parameters.end()){ 
160                                         path = m->hasPath(it->second);
161                                         //if the user has not given a path then, add inputdir. else leave path alone.
162                                         if (path == "") {       parameters["name"] = inputDir + it->second;             }
163                                 }
164                         }
165                         
166                         format = globaldata->getFormat();
167                         
168                         //required parameters
169                         phylipfile = validParameter.validFile(parameters, "phylip", true);
170                         if (phylipfile == "not open") { abort = true; }
171                         else if (phylipfile == "not found") { phylipfile = ""; }        
172                         else {  globaldata->newRead(); format = "phylip";  globaldata->setPhylipFile(phylipfile);       }
173                         
174                         columnfile = validParameter.validFile(parameters, "column", true);
175                         if (columnfile == "not open") { abort = true; } 
176                         else if (columnfile == "not found") { columnfile = ""; }
177                         else {  globaldata->newRead(); format = "column"; globaldata->setColumnFile(columnfile);        }
178                         
179                         namefile = validParameter.validFile(parameters, "name", true);
180                         if (namefile == "not open") { abort = true; }   
181                         else if (namefile == "not found") { namefile = ""; }
182                         else {  globaldata->setNameFile(namefile);      }
183                         
184                         //error checking on files                       
185                         if ((globaldata->getSharedFile() == "") && ((phylipfile == "") && (columnfile == "")))  { m->mothurOut("You must run the read.otu command or provide a distance file before running the tree.shared command."); m->mothurOutEndLine(); abort = true; }
186                         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; }
187                         
188                         if (columnfile != "") {
189                                 if (namefile == "") {  m->mothurOut("You need to provide a namefile if you are going to use the column format."); m->mothurOutEndLine(); abort = true; }
190                         }
191
192                         //check for optional parameter and set defaults
193                         // ...at some point should added some additional type checking...
194                         label = validParameter.validFile(parameters, "label", false);                   
195                         if (label == "not found") { label = ""; }
196                         else { 
197                                 if(label != "all") {  m->splitAtDash(label, labels);  allLines = 0;  }
198                                 else { allLines = 1;  }
199                         }
200                         
201                         //if the user has not specified any labels use the ones from read.otu
202                         if(label == "") {  
203                                 allLines = globaldata->allLines; 
204                                 labels = globaldata->labels; 
205                         }
206                                 
207                         groups = validParameter.validFile(parameters, "groups", false);                 
208                         if (groups == "not found") { groups = ""; }
209                         else { 
210                                 m->splitAtDash(groups, Groups);
211                                 globaldata->Groups = Groups;
212                         }
213                                 
214                         calc = validParameter.validFile(parameters, "calc", false);                     
215                         if (calc == "not found") { calc = "jclass-thetayc";  }
216                         else { 
217                                  if (calc == "default")  {  calc = "jclass-thetayc";  }
218                         }
219                         m->splitAtDash(calc, Estimators);
220
221                         string temp;
222                         temp = validParameter.validFile(parameters, "precision", false);                        if (temp == "not found") { temp = "100"; }
223                         convert(temp, precision); 
224                         
225                         temp = validParameter.validFile(parameters, "cutoff", false);                   if (temp == "not found") { temp = "10"; }
226                         convert(temp, cutoff); 
227                         cutoff += (5 / (precision * 10.0));
228                         
229                         //if the user changes the output directory command factory will send this info to us in the output parameter 
230                         outputDir = validParameter.validFile(parameters, "outputdir", false);           if (outputDir == "not found"){  
231                                 outputDir = ""; 
232                                 outputDir += m->hasPath(globaldata->inputFileName); //if user entered a file with a path then preserve it       
233                         }
234
235                                 
236                         if (abort == false) {
237                         
238                                 validCalculator = new ValidCalculators();
239                                 
240                                 if (format == "sharedfile") {
241                                         int i;
242                                         for (i=0; i<Estimators.size(); i++) {
243                                                 if (validCalculator->isValidCalculator("treegroup", Estimators[i]) == true) { 
244                                                         if (Estimators[i] == "sharedsobs") { 
245                                                                 treeCalculators.push_back(new SharedSobsCS());
246                                                         }else if (Estimators[i] == "sharedchao") { 
247                                                                 treeCalculators.push_back(new SharedChao1());
248                                                         }else if (Estimators[i] == "sharedace") { 
249                                                                 treeCalculators.push_back(new SharedAce());
250                                                         }else if (Estimators[i] == "jabund") {  
251                                                                 treeCalculators.push_back(new JAbund());
252                                                         }else if (Estimators[i] == "sorabund") { 
253                                                                 treeCalculators.push_back(new SorAbund());
254                                                         }else if (Estimators[i] == "jclass") { 
255                                                                 treeCalculators.push_back(new Jclass());
256                                                         }else if (Estimators[i] == "sorclass") { 
257                                                                 treeCalculators.push_back(new SorClass());
258                                                         }else if (Estimators[i] == "jest") { 
259                                                                 treeCalculators.push_back(new Jest());
260                                                         }else if (Estimators[i] == "sorest") { 
261                                                                 treeCalculators.push_back(new SorEst());
262                                                         }else if (Estimators[i] == "thetayc") { 
263                                                                 treeCalculators.push_back(new ThetaYC());
264                                                         }else if (Estimators[i] == "thetan") { 
265                                                                 treeCalculators.push_back(new ThetaN());
266                                                         }else if (Estimators[i] == "kstest") { 
267                                                                 treeCalculators.push_back(new KSTest());
268                                                         }else if (Estimators[i] == "sharednseqs") { 
269                                                                 treeCalculators.push_back(new SharedNSeqs());
270                                                         }else if (Estimators[i] == "ochiai") { 
271                                                                 treeCalculators.push_back(new Ochiai());
272                                                         }else if (Estimators[i] == "anderberg") { 
273                                                                 treeCalculators.push_back(new Anderberg());
274                                                         }else if (Estimators[i] == "kulczynski") { 
275                                                                 treeCalculators.push_back(new Kulczynski());
276                                                         }else if (Estimators[i] == "kulczynskicody") { 
277                                                                 treeCalculators.push_back(new KulczynskiCody());
278                                                         }else if (Estimators[i] == "lennon") { 
279                                                                 treeCalculators.push_back(new Lennon());
280                                                         }else if (Estimators[i] == "morisitahorn") { 
281                                                                 treeCalculators.push_back(new MorHorn());
282                                                         }else if (Estimators[i] == "braycurtis") { 
283                                                                 treeCalculators.push_back(new BrayCurtis());
284                                                         }else if (Estimators[i] == "whittaker") { 
285                                                                 treeCalculators.push_back(new Whittaker());
286                                                         }else if (Estimators[i] == "odum") { 
287                                                                 treeCalculators.push_back(new Odum());
288                                                         }else if (Estimators[i] == "canberra") { 
289                                                                 treeCalculators.push_back(new Canberra());
290                                                         }else if (Estimators[i] == "structeuclidean") { 
291                                                                 treeCalculators.push_back(new StructEuclidean());
292                                                         }else if (Estimators[i] == "structchord") { 
293                                                                 treeCalculators.push_back(new StructChord());
294                                                         }else if (Estimators[i] == "hellinger") { 
295                                                                 treeCalculators.push_back(new Hellinger());
296                                                         }else if (Estimators[i] == "manhattan") { 
297                                                                 treeCalculators.push_back(new Manhattan());
298                                                         }else if (Estimators[i] == "structpearson") { 
299                                                                 treeCalculators.push_back(new StructPearson());
300                                                         }else if (Estimators[i] == "soergel") { 
301                                                                 treeCalculators.push_back(new Soergel());
302                                                         }else if (Estimators[i] == "spearman") { 
303                                                                 treeCalculators.push_back(new Spearman());
304                                                         }else if (Estimators[i] == "structkulczynski") { 
305                                                                 treeCalculators.push_back(new StructKulczynski());
306                                                         }else if (Estimators[i] == "speciesprofile") { 
307                                                                 treeCalculators.push_back(new SpeciesProfile());
308                                                         }else if (Estimators[i] == "hamming") { 
309                                                                 treeCalculators.push_back(new Hamming());
310                                                         }else if (Estimators[i] == "structchi2") { 
311                                                                 treeCalculators.push_back(new StructChi2());
312                                                         }else if (Estimators[i] == "gower") { 
313                                                                 treeCalculators.push_back(new Gower());
314                                                         }else if (Estimators[i] == "memchi2") { 
315                                                                 treeCalculators.push_back(new MemChi2());
316                                                         }else if (Estimators[i] == "memchord") { 
317                                                                 treeCalculators.push_back(new MemChord());
318                                                         }else if (Estimators[i] == "memeuclidean") { 
319                                                                 treeCalculators.push_back(new MemEuclidean());
320                                                         }else if (Estimators[i] == "mempearson") { 
321                                                                 treeCalculators.push_back(new MemPearson());
322                                                         }
323                                                 }
324                                         }
325                                 }
326                         }       
327                 }
328
329         }
330         catch(exception& e) {
331                 m->errorOut(e, "TreeGroupCommand", "TreeGroupCommand");
332                 exit(1);
333         }
334 }
335
336 //**********************************************************************************************************************
337
338 void TreeGroupCommand::help(){
339         try {
340                 m->mothurOut("The tree.shared command creates a .tre to represent the similiarity between groups or sequences.\n");
341                 m->mothurOut("The tree.shared command can only be executed after a successful read.otu command or by providing a distance file.\n");
342                 m->mothurOut("The tree.shared command parameters are groups, calc, phylip, column, name, cutoff, precision and label.\n");
343                 m->mothurOut("The groups parameter allows you to specify which of the groups in your groupfile you would like included used.\n");
344                 m->mothurOut("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");
345                 m->mothurOut("The phylip or column parameter are required if you do not run the read.otu command first, and only one may be used.  If you use a column file the name filename is required. \n");
346                 m->mothurOut("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");
347                 m->mothurOut("The tree.shared command should be in the following format: tree.shared(groups=yourGroups, calc=yourCalcs, label=yourLabels).\n");
348                 m->mothurOut("Example tree.shared(groups=A-B-C, calc=jabund-sorabund).\n");
349                 m->mothurOut("The default value for groups is all the groups in your groupfile.\n");
350                 m->mothurOut("The default value for calc is jclass-thetayc.\n");
351                 m->mothurOut("The tree.shared command outputs a .tre file for each calculator you specify at each distance you choose.\n");
352                 validCalculator->printCalc("treegroup", cout);
353                 m->mothurOut("Or the tree.shared command can be in the following format: tree.shared(phylip=yourPhylipFile).\n");
354                 m->mothurOut("Example tree.shared(phylip=abrecovery.dist).\n");
355                 m->mothurOut("Note: No spaces between parameter labels (i.e. groups), '=' and parameters (i.e.yourGroups).\n\n");
356         }
357         catch(exception& e) {
358                 m->errorOut(e, "TreeGroupCommand", "help");
359                 exit(1);
360         }
361 }
362
363
364 //**********************************************************************************************************************
365
366 TreeGroupCommand::~TreeGroupCommand(){
367         globaldata->Groups.clear();  
368         if (abort == false) {
369                 
370                 if (format == "sharedfile") { delete read;  delete input; globaldata->ginput = NULL; }
371                 else { delete readMatrix;  delete matrix; delete list; }
372                 delete tmap;  globaldata->gTreemap = NULL;
373                 delete validCalculator;
374         }
375         
376 }
377
378 //**********************************************************************************************************************
379
380 int TreeGroupCommand::execute(){
381         try {
382         
383                 if (abort == true) { if (calledHelp) { return 0; }  return 2;   }
384                 
385                 if (format == "sharedfile") {
386                         //if the users entered no valid calculators don't execute command
387                         if (treeCalculators.size() == 0) { m->mothurOut("You have given no valid calculators."); m->mothurOutEndLine(); return 0; }
388                         
389                         if (globaldata->gGroupmap != NULL) {  delete globaldata->gGroupmap;   globaldata->gGroupmap = NULL;  }
390                         //you have groups
391                         read = new ReadOTUFile(globaldata->inputFileName);      
392                         read->read(&*globaldata); 
393                         
394                         input = globaldata->ginput;
395                         lookup = input->getSharedRAbundVectors();
396                         lastLabel = lookup[0]->getLabel();
397                         
398                         if (lookup.size() < 2) { m->mothurOut("You have not provided enough valid groups.  I cannot run the command."); m->mothurOutEndLine(); return 0; }
399                         
400                         //used in tree constructor 
401                         globaldata->runParse = false;
402                         
403                         //create treemap class from groupmap for tree class to use
404                         tmap = new TreeMap();
405                         tmap->makeSim(globaldata->gGroupmap);
406                         globaldata->gTreemap = tmap;
407                         
408                         //clear globaldatas old tree names if any
409                         globaldata->Treenames.clear();
410                         
411                         //fills globaldatas tree names
412                         globaldata->Treenames = globaldata->Groups;
413                 
414                         if (m->control_pressed) { return 0; }
415                         
416                         //create tree file
417                         makeSimsShared();
418                         
419                         if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) {        remove(outputNames[i].c_str());  } return 0; }
420                 }else{
421                         //read in dist file
422                         filename = globaldata->inputFileName;
423                 
424                         if (format == "column") { readMatrix = new ReadColumnMatrix(filename); }        
425                         else if (format == "phylip") { readMatrix = new ReadPhylipMatrix(filename); }
426                                 
427                         readMatrix->setCutoff(cutoff);
428         
429                         if(namefile != ""){     
430                                 nameMap = new NameAssignment(namefile);
431                                 nameMap->readMap();
432                         }
433                         else{
434                                 nameMap = NULL;
435                         }
436         
437                         readMatrix->read(nameMap);
438                         list = readMatrix->getListVector();
439                         matrix = readMatrix->getMatrix();
440
441                         //make treemap
442                         tmap = new TreeMap();
443                         
444                         if (m->control_pressed) { return 0; }
445                         
446                         tmap->makeSim(list);
447                         globaldata->gTreemap = tmap;
448                         
449                         globaldata->Groups = tmap->namesOfGroups;
450                 
451                         //clear globaldatas old tree names if any
452                         globaldata->Treenames.clear();
453                 
454                         //fills globaldatas tree names
455                         globaldata->Treenames = globaldata->Groups;
456                         
457                         //used in tree constructor 
458                         globaldata->runParse = false;
459                         
460                         if (m->control_pressed) { return 0; }
461                         
462                         makeSimsDist();
463                         
464                         if (m->control_pressed) { return 0; }
465
466                         //create a new filename
467                         outputFile = outputDir + m->getRootName(m->getSimpleName(globaldata->inputFileName)) + "tre";   
468                         outputNames.push_back(outputFile); outputTypes["tree"].push_back(outputFile);
469                                 
470                         createTree();
471                         
472                         if (m->control_pressed) { return 0; }
473
474                         m->mothurOut("Tree complete. "); m->mothurOutEndLine();
475                         
476                 }
477                                 
478                 //reset groups parameter
479                 globaldata->Groups.clear();  
480                 
481                 m->mothurOutEndLine();
482                 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
483                 for (int i = 0; i < outputNames.size(); i++) {  m->mothurOut(outputNames[i]); m->mothurOutEndLine();    }
484                 m->mothurOutEndLine();
485
486                 return 0;
487         }
488         catch(exception& e) {
489                 m->errorOut(e, "TreeGroupCommand", "execute");
490                 exit(1);
491         }
492 }
493 //**********************************************************************************************************************
494
495 int TreeGroupCommand::createTree(){
496         try {
497                 //create tree
498                 t = new Tree();
499                 
500                 //do merges and create tree structure by setting parents and children
501                 //there are numGroups - 1 merges to do
502                 for (int i = 0; i < (numGroups - 1); i++) {
503                         float largest = -1000.0;
504                         
505                         if (m->control_pressed) { delete t; return 1; }
506                         
507                         int row, column;
508                         //find largest value in sims matrix by searching lower triangle
509                         for (int j = 1; j < simMatrix.size(); j++) {
510                                 for (int k = 0; k < j; k++) {
511                                         if (simMatrix[j][k] > largest) {  largest = simMatrix[j][k]; row = j; column = k;  }
512                                 }
513                         }
514
515                         //set non-leaf node info and update leaves to know their parents
516                         //non-leaf
517                         t->tree[numGroups + i].setChildren(index[row], index[column]);
518                         
519                         //parents
520                         t->tree[index[row]].setParent(numGroups + i);
521                         t->tree[index[column]].setParent(numGroups + i);
522                         
523                         //blength = distance / 2;
524                         float blength = ((1.0 - largest) / 2);
525                         
526                         //branchlengths
527                         t->tree[index[row]].setBranchLength(blength - t->tree[index[row]].getLengthToLeaves());
528                         t->tree[index[column]].setBranchLength(blength - t->tree[index[column]].getLengthToLeaves());
529                         
530                         //set your length to leaves to your childs length plus branchlength
531                         t->tree[numGroups + i].setLengthToLeaves(t->tree[index[row]].getLengthToLeaves() + t->tree[index[row]].getBranchLength());
532                         
533                         
534                         //update index 
535                         index[row] = numGroups+i;
536                         index[column] = numGroups+i;
537                         
538                         //remove highest value that caused the merge.
539                         simMatrix[row][column] = -1000.0;
540                         simMatrix[column][row] = -1000.0;
541                         
542                         //merge values in simsMatrix
543                         for (int n = 0; n < simMatrix.size(); n++)      {
544                                 //row becomes merge of 2 groups
545                                 simMatrix[row][n] = (simMatrix[row][n] + simMatrix[column][n]) / 2;
546                                 simMatrix[n][row] = simMatrix[row][n];
547                                 //delete column
548                                 simMatrix[column][n] = -1000.0;
549                                 simMatrix[n][column] = -1000.0;
550                         }
551                 }
552                 
553                 //adjust tree to make sure root to tip length is .5
554                 int root = t->findRoot();
555                 t->tree[root].setBranchLength((0.5 - t->tree[root].getLengthToLeaves()));
556                 
557                 //assemble tree
558                 t->assembleTree();
559                 
560                 if (m->control_pressed) { delete t; return 1; }
561                 
562                 //print newick file
563                 t->createNewickFile(outputFile);
564                 
565                 //delete tree
566                 delete t;
567                 
568                 if (m->control_pressed) { remove(outputFile.c_str()); outputNames.pop_back(); return 1; }
569                 
570                 return 0;
571         
572         }
573         catch(exception& e) {
574                 m->errorOut(e, "TreeGroupCommand", "createTree");
575                 exit(1);
576         }
577 }
578 /***********************************************************/
579 void TreeGroupCommand::printSims(ostream& out) {
580         try {
581                 
582                 //output column headers
583                 //out << '\t';
584                 //for (int i = 0; i < lookup.size(); i++) {     out << lookup[i]->getGroup() << '\t';           }
585                 //out << endl;
586                 
587                 
588                 for (int m = 0; m < simMatrix.size(); m++)      {
589                         //out << lookup[m]->getGroup() << '\t';
590                         for (int n = 0; n < simMatrix.size(); n++)      {
591                                 out << simMatrix[m][n] << '\t'; 
592                         }
593                         out << endl;
594                 }
595
596         }
597         catch(exception& e) {
598                 m->errorOut(e, "TreeGroupCommand", "printSims");
599                 exit(1);
600         }
601 }
602 /***********************************************************/
603 int TreeGroupCommand::makeSimsDist() {
604         try {
605                 numGroups = list->size();
606                 
607                 //initialize index
608                 index.clear();
609                 for (int g = 0; g < numGroups; g++) {   index[g] = g;   }
610                 
611                 //initialize simMatrix
612                 simMatrix.clear();
613                 simMatrix.resize(numGroups);
614                 for (int k = 0; k < simMatrix.size(); k++)      {
615                         for (int j = 0; j < simMatrix.size(); j++)      {
616                                 simMatrix[k].push_back(0.0);
617                         }
618                 }
619                 
620                 //go through sparse matrix and fill sims
621                 //go through each cell in the sparsematrix
622                 for(MatData currentCell = matrix->begin(); currentCell != matrix->end(); currentCell++){
623                         //similairity = -(distance-1)
624                         simMatrix[currentCell->row][currentCell->column] = -(currentCell->dist -1.0);   
625                         simMatrix[currentCell->column][currentCell->row] = -(currentCell->dist -1.0);   
626                         
627                         if (m->control_pressed) { return 1; }
628                         
629                 }
630
631                 return 0;
632         }
633         catch(exception& e) {
634                 m->errorOut(e, "TreeGroupCommand", "makeSimsDist");
635                 exit(1);
636         }
637 }
638
639 /***********************************************************/
640 int TreeGroupCommand::makeSimsShared() {
641         try {
642                 set<string> processedLabels;
643                 set<string> userLabels = labels;
644                 
645                 //as long as you are not at the end of the file or done wih the lines you want
646                 while((lookup[0] != NULL) && ((allLines == 1) || (userLabels.size() != 0))) {
647                         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; }
648                 
649                         if(allLines == 1 || labels.count(lookup[0]->getLabel()) == 1){                  
650                                 m->mothurOut(lookup[0]->getLabel()); m->mothurOutEndLine();
651                                 process(lookup);
652                                 
653                                 processedLabels.insert(lookup[0]->getLabel());
654                                 userLabels.erase(lookup[0]->getLabel());
655                         }
656                         
657                         if ((m->anyLabelsToProcess(lookup[0]->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
658                                 string saveLabel = lookup[0]->getLabel();
659                         
660                                 for (int i = 0; i < lookup.size(); i++) {  delete lookup[i];  } 
661                                 lookup = input->getSharedRAbundVectors(lastLabel);
662
663                                 m->mothurOut(lookup[0]->getLabel()); m->mothurOutEndLine();
664                                 process(lookup);
665                                         
666                                 processedLabels.insert(lookup[0]->getLabel());
667                                 userLabels.erase(lookup[0]->getLabel());
668                                 
669                                 //restore real lastlabel to save below
670                                 lookup[0]->setLabel(saveLabel);
671                         }
672
673                         lastLabel = lookup[0]->getLabel();                      
674                         
675                         //get next line to process
676                         for (int i = 0; i < lookup.size(); i++) {  delete lookup[i];  } 
677                         lookup = input->getSharedRAbundVectors();
678                 }
679                 
680                 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; }
681
682                 //output error messages about any remaining user labels
683                 set<string>::iterator it;
684                 bool needToRun = false;
685                 for (it = userLabels.begin(); it != userLabels.end(); it++) {  
686                         m->mothurOut("Your file does not include the label " + *it); 
687                         if (processedLabels.count(lastLabel) != 1) {
688                                 m->mothurOut(". I will use " + lastLabel + "."); m->mothurOutEndLine();
689                                 needToRun = true;
690                         }else {
691                                 m->mothurOut(". Please refer to " + lastLabel + "."); m->mothurOutEndLine();
692                         }
693                 }
694                 
695                 //run last label if you need to
696                 if (needToRun == true)  {
697                         for (int i = 0; i < lookup.size(); i++) {  if (lookup[i] != NULL) {             delete lookup[i]; }             } 
698                         lookup = input->getSharedRAbundVectors(lastLabel);
699
700                         m->mothurOut(lookup[0]->getLabel()); m->mothurOutEndLine();
701                         process(lookup);
702                         for (int i = 0; i < lookup.size(); i++) {  delete lookup[i];  }         
703                 }
704                 
705                 for(int i = 0 ; i < treeCalculators.size(); i++) {  delete treeCalculators[i]; }
706                 
707                 return 0;
708         }
709         catch(exception& e) {
710                 m->errorOut(e, "TreeGroupCommand", "makeSimsShared");
711                 exit(1);
712         }
713 }
714
715 /***********************************************************/
716 int TreeGroupCommand::process(vector<SharedRAbundVector*> thisLookup) {
717         try{
718                                 EstOutput data;
719                                 vector<SharedRAbundVector*> subset;
720                                 numGroups = thisLookup.size();
721                                 
722                                 //for each calculator                                                                                           
723                                 for(int i = 0 ; i < treeCalculators.size(); i++) {
724                                         //initialize simMatrix
725                                         simMatrix.clear();
726                                         simMatrix.resize(numGroups);
727                                         for (int k = 0; k < simMatrix.size(); k++)      {
728                                                 for (int j = 0; j < simMatrix.size(); j++)      {
729                                                         simMatrix[k].push_back(0.0);
730                                                 }
731                                         }
732                 
733                                         //initialize index
734                                         index.clear();
735                                         for (int g = 0; g < numGroups; g++) {   index[g] = g;   }
736                 
737                                         //create a new filename
738                                         outputFile = outputDir + m->getRootName(m->getSimpleName(globaldata->inputFileName)) + treeCalculators[i]->getName() + "." + thisLookup[0]->getLabel() + ".tre";                                
739                                         outputNames.push_back(outputFile); outputTypes["tree"].push_back(outputFile); 
740                                                                                                 
741                                         for (int k = 0; k < thisLookup.size(); k++) { 
742                                                 for (int l = k; l < thisLookup.size(); l++) {
743                                                         if (k != l) { //we dont need to similiarity of a groups to itself
744                                                                 //get estimated similarity between 2 groups
745                                                                 
746                                                                 subset.clear(); //clear out old pair of sharedrabunds
747                                                                 //add new pair of sharedrabunds
748                                                                 subset.push_back(thisLookup[k]); subset.push_back(thisLookup[l]); 
749                                                                 
750                                                                 //if this calc needs all groups to calculate the pair load all groups
751                                                                 if (treeCalculators[i]->getNeedsAll()) { 
752                                                                         //load subset with rest of lookup for those calcs that need everyone to calc for a pair
753                                                                         for (int w = 0; w < thisLookup.size(); w++) {
754                                                                                 if ((w != k) && (w != l)) { subset.push_back(thisLookup[w]); }
755                                                                         }
756                                                                 }
757                                                                 
758                                                                 data = treeCalculators[i]->getValues(subset); //saves the calculator outputs
759                                                 //cout << thisLookup[k]->getGroup() << '\t' << thisLookup[l]->getGroup() << '\t' << (1.0 - data[0]) << endl;
760                                                                 if (m->control_pressed) { return 1; }
761                                                                 
762                                                                 //save values in similarity matrix
763                                                                 simMatrix[k][l] = data[0];
764                                                                 simMatrix[l][k] = data[0];
765                                                         }
766                                                 }
767                                         }
768                                         
769                                         //createdistance file from simMatrix
770                                         /*string o = outputDir + m->getRootName(m->getSimpleName(globaldata->inputFileName)) + treeCalculators[i]->getName() + "." + thisLookup[0]->getLabel() + ".dist";
771                                         ofstream outDist;
772                                         m->openOutputFile(o, outDist);
773                                         outDist << simMatrix.size() << endl;
774                                         for (int k = 0; k < simMatrix.size(); k++) {
775                                                 outDist << thisLookup[k]->getGroup() << '\t';
776                                                 for (int l = 0; l < k; l++) {
777                                                         outDist << (1.0-simMatrix[k][l]) << '\t';
778                                                 }
779                                                 outDist << endl;
780                                         }
781                                         outDist.close();*/
782
783                                         
784                                         if (m->control_pressed) { return 1; }
785                                         //creates tree from similarity matrix and write out file
786                                         createTree();
787                                         
788                                         if (m->control_pressed) { return 1; }
789                                 }
790                                 
791                                 return 0;
792
793         }
794         catch(exception& e) {
795                 m->errorOut(e, "TreeGroupCommand", "process");
796                 exit(1);
797         }
798 }
799 /***********************************************************/
800
801         
802