]> git.donarmstrong.com Git - mothur.git/blob - treegroupscommand.cpp
6ce5ee9dfcb29dfd495bb7dc5f83aec8681eb3d6
[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 "sharedjabund.h"
12 #include "sharedsorabund.h"
13 #include "sharedjclass.h"
14 #include "sharedsorclass.h"
15 #include "sharedjest.h"
16 #include "sharedsorest.h"
17 #include "sharedthetayc.h"
18 #include "sharedthetan.h"
19 #include "sharedmorisitahorn.h"
20 #include "sharedbraycurtis.h"
21
22
23 //**********************************************************************************************************************
24
25 TreeGroupCommand::TreeGroupCommand(string option)  {
26         try {
27                 globaldata = GlobalData::getInstance();
28                 abort = false;
29                 allLines = 1;
30                 labels.clear();
31                 Groups.clear();
32                 Estimators.clear();
33                 
34                 //allow user to run help
35                 if(option == "help") { validCalculator = new ValidCalculators(); help(); abort = true; }
36                 
37                 else {
38                         //valid paramters for this command
39                         string Array[] =  {"label","calc","groups", "phylip", "column", "name", "precision","cutoff","outputdir","inputdir"};
40                         vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
41                         
42                         OptionParser parser(option);
43                         map<string, string> parameters = parser. getParameters();
44                         
45                         ValidParameters validParameter;
46                         map<string, string>::iterator it;
47                 
48                         //check to make sure all parameters are valid for command
49                         for (it = parameters.begin(); it != parameters.end(); it++) { 
50                                 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) {  abort = true;  }
51                         }
52                         
53                         //if the user changes the input directory command factory will send this info to us in the output parameter 
54                         string inputDir = validParameter.validFile(parameters, "inputdir", false);              
55                         if (inputDir == "not found"){   inputDir = "";          }
56                         else {
57                                 string path;
58                                 it = parameters.find("phylip");
59                                 //user has given a template file
60                                 if(it != parameters.end()){ 
61                                         path = hasPath(it->second);
62                                         //if the user has not given a path then, add inputdir. else leave path alone.
63                                         if (path == "") {       parameters["phylip"] = inputDir + it->second;           }
64                                 }
65                                 
66                                 it = parameters.find("column");
67                                 //user has given a template file
68                                 if(it != parameters.end()){ 
69                                         path = hasPath(it->second);
70                                         //if the user has not given a path then, add inputdir. else leave path alone.
71                                         if (path == "") {       parameters["column"] = inputDir + it->second;           }
72                                 }
73                                 
74                                 it = parameters.find("name");
75                                 //user has given a template file
76                                 if(it != parameters.end()){ 
77                                         path = hasPath(it->second);
78                                         //if the user has not given a path then, add inputdir. else leave path alone.
79                                         if (path == "") {       parameters["name"] = inputDir + it->second;             }
80                                 }
81                         }
82                         
83                         format = globaldata->getFormat();
84                         
85                         //required parameters
86                         phylipfile = validParameter.validFile(parameters, "phylip", true);
87                         if (phylipfile == "not open") { abort = true; }
88                         else if (phylipfile == "not found") { phylipfile = ""; }        
89                         else {  format = "phylip";  globaldata->setPhylipFile(phylipfile);      }
90                         
91                         columnfile = validParameter.validFile(parameters, "column", true);
92                         if (columnfile == "not open") { abort = true; } 
93                         else if (columnfile == "not found") { columnfile = ""; }
94                         else {  format = "column"; globaldata->setColumnFile(columnfile);       }
95                         
96                         namefile = validParameter.validFile(parameters, "name", true);
97                         if (namefile == "not open") { abort = true; }   
98                         else if (namefile == "not found") { namefile = ""; }
99                         else {  globaldata->setNameFile(namefile);      }
100                         
101                         //error checking on files                       
102                         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; }
103                         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; }
104                         
105                         if (columnfile != "") {
106                                 if (namefile == "") {  m->mothurOut("You need to provide a namefile if you are going to use the column format."); m->mothurOutEndLine(); abort = true; }
107                         }
108
109                         //check for optional parameter and set defaults
110                         // ...at some point should added some additional type checking...
111                         label = validParameter.validFile(parameters, "label", false);                   
112                         if (label == "not found") { label = ""; }
113                         else { 
114                                 if(label != "all") {  splitAtDash(label, labels);  allLines = 0;  }
115                                 else { allLines = 1;  }
116                         }
117                         
118                         //if the user has not specified any labels use the ones from read.otu
119                         if(label == "") {  
120                                 allLines = globaldata->allLines; 
121                                 labels = globaldata->labels; 
122                         }
123                                 
124                         groups = validParameter.validFile(parameters, "groups", false);                 
125                         if (groups == "not found") { groups = ""; }
126                         else { 
127                                 splitAtDash(groups, Groups);
128                                 globaldata->Groups = Groups;
129                         }
130                                 
131                         calc = validParameter.validFile(parameters, "calc", false);                     
132                         if (calc == "not found") { calc = "jclass-thetayc";  }
133                         else { 
134                                  if (calc == "default")  {  calc = "jclass-thetayc";  }
135                         }
136                         splitAtDash(calc, Estimators);
137
138                         string temp;
139                         temp = validParameter.validFile(parameters, "precision", false);                        if (temp == "not found") { temp = "100"; }
140                         convert(temp, precision); 
141                         
142                         temp = validParameter.validFile(parameters, "cutoff", false);                   if (temp == "not found") { temp = "10"; }
143                         convert(temp, cutoff); 
144                         cutoff += (5 / (precision * 10.0));
145                         
146                         //if the user changes the output directory command factory will send this info to us in the output parameter 
147                         outputDir = validParameter.validFile(parameters, "outputdir", false);           if (outputDir == "not found"){  
148                                 outputDir = ""; 
149                                 outputDir += hasPath(globaldata->inputFileName); //if user entered a file with a path then preserve it  
150                         }
151
152                                 
153                         if (abort == false) {
154                         
155                                 validCalculator = new ValidCalculators();
156                                 
157                                 if (format == "sharedfile") {
158                                         int i;
159                                         for (i=0; i<Estimators.size(); i++) {
160                                                 if (validCalculator->isValidCalculator("treegroup", Estimators[i]) == true) { 
161                                                         if (Estimators[i] == "jabund") {        
162                                                                 treeCalculators.push_back(new JAbund());
163                                                         }else if (Estimators[i] == "sorabund") { 
164                                                                 treeCalculators.push_back(new SorAbund());
165                                                         }else if (Estimators[i] == "jclass") { 
166                                                                 treeCalculators.push_back(new Jclass());
167                                                         }else if (Estimators[i] == "sorclass") { 
168                                                                 treeCalculators.push_back(new SorClass());
169                                                         }else if (Estimators[i] == "jest") { 
170                                                                 treeCalculators.push_back(new Jest());
171                                                         }else if (Estimators[i] == "sorest") { 
172                                                                 treeCalculators.push_back(new SorEst());
173                                                         }else if (Estimators[i] == "thetayc") { 
174                                                                 treeCalculators.push_back(new ThetaYC());
175                                                         }else if (Estimators[i] == "thetan") { 
176                                                                 treeCalculators.push_back(new ThetaN());
177                                                         }else if (Estimators[i] == "morisitahorn") { 
178                                                                 treeCalculators.push_back(new MorHorn());
179                                                         }else if (Estimators[i] == "braycurtis") { 
180                                                                 treeCalculators.push_back(new BrayCurtis());
181                                                         }
182                                                 }
183                                         }
184                                 }
185                         }       
186                 }
187
188         }
189         catch(exception& e) {
190                 m->errorOut(e, "TreeGroupCommand", "TreeGroupCommand");
191                 exit(1);
192         }
193 }
194
195 //**********************************************************************************************************************
196
197 void TreeGroupCommand::help(){
198         try {
199                 m->mothurOut("The tree.shared command creates a .tre to represent the similiarity between groups or sequences.\n");
200                 m->mothurOut("The tree.shared command can only be executed after a successful read.otu command or by providing a distance file.\n");
201                 m->mothurOut("The tree.shared command parameters are groups, calc, phylip, column, name, cutoff, precision and label.\n");
202                 m->mothurOut("The groups parameter allows you to specify which of the groups in your groupfile you would like included used.\n");
203                 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");
204                 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");
205                 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");
206                 m->mothurOut("The tree.shared command should be in the following format: tree.shared(groups=yourGroups, calc=yourCalcs, label=yourLabels).\n");
207                 m->mothurOut("Example tree.shared(groups=A-B-C, calc=jabund-sorabund).\n");
208                 m->mothurOut("The default value for groups is all the groups in your groupfile.\n");
209                 m->mothurOut("The default value for calc is jclass-thetayc.\n");
210                 m->mothurOut("The tree.shared command outputs a .tre file for each calculator you specify at each distance you choose.\n");
211                 validCalculator->printCalc("treegroup", cout);
212                 m->mothurOut("Or the tree.shared command can be in the following format: tree.shared(phylip=yourPhylipFile).\n");
213                 m->mothurOut("Example tree.shared(phylip=abrecovery.dist).\n");
214                 m->mothurOut("Note: No spaces between parameter labels (i.e. groups), '=' and parameters (i.e.yourGroups).\n\n");
215         }
216         catch(exception& e) {
217                 m->errorOut(e, "TreeGroupCommand", "help");
218                 exit(1);
219         }
220 }
221
222
223 //**********************************************************************************************************************
224
225 TreeGroupCommand::~TreeGroupCommand(){
226         if (abort == false) {
227                 
228                 if (format == "sharedfile") { delete read;  delete input; globaldata->ginput = NULL; }
229                 else { delete readMatrix;  delete matrix; delete list; }
230                 delete tmap;  globaldata->gTreemap = NULL;
231                 delete validCalculator;
232         }
233         
234 }
235
236 //**********************************************************************************************************************
237
238 int TreeGroupCommand::execute(){
239         try {
240         
241                 if (abort == true) { return 0; }
242                 
243                 if (format == "sharedfile") {
244                         //if the users entered no valid calculators don't execute command
245                         if (treeCalculators.size() == 0) { m->mothurOut("You have given no valid calculators."); m->mothurOutEndLine(); return 0; }
246
247                         //you have groups
248                         read = new ReadOTUFile(globaldata->inputFileName);      
249                         read->read(&*globaldata); 
250                         
251                         input = globaldata->ginput;
252                         lookup = input->getSharedRAbundVectors();
253                         lastLabel = lookup[0]->getLabel();
254                         
255                         if (lookup.size() < 2) { m->mothurOut("You have not provided enough valid groups.  I cannot run the command."); m->mothurOutEndLine(); return 0; }
256                         
257                         //used in tree constructor 
258                         globaldata->runParse = false;
259                         
260                         //create tree file
261                         makeSimsShared();
262                 }else{
263                         //read in dist file
264                         filename = globaldata->inputFileName;
265                 
266                         if (format == "column") { readMatrix = new ReadColumnMatrix(filename); }        
267                         else if (format == "phylip") { readMatrix = new ReadPhylipMatrix(filename); }
268                                 
269                         readMatrix->setCutoff(cutoff);
270         
271                         if(namefile != ""){     
272                                 nameMap = new NameAssignment(namefile);
273                                 nameMap->readMap();
274                         }
275                         else{
276                                 nameMap = NULL;
277                         }
278         
279                         readMatrix->read(nameMap);
280                         list = readMatrix->getListVector();
281                         matrix = readMatrix->getMatrix();
282
283                         //make treemap
284                         tmap = new TreeMap();
285                         tmap->makeSim(list);
286                         globaldata->gTreemap = tmap;
287                         
288                         globaldata->Groups = tmap->namesOfGroups;
289                 
290                         //clear globaldatas old tree names if any
291                         globaldata->Treenames.clear();
292                 
293                         //fills globaldatas tree names
294                         globaldata->Treenames = globaldata->Groups;
295                         
296                         //used in tree constructor 
297                         globaldata->runParse = false;
298                         
299                         makeSimsDist();
300
301                         //create a new filename
302                         outputFile = outputDir + getRootName(getSimpleName(globaldata->inputFileName)) + "tre"; 
303                         outputNames.push_back(outputFile);
304                                 
305                         createTree();
306                         m->mothurOut("Tree complete. "); m->mothurOutEndLine();
307                 }
308                                 
309                 //reset groups parameter
310                 globaldata->Groups.clear();  
311                 
312                 m->mothurOutEndLine();
313                 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
314                 for (int i = 0; i < outputNames.size(); i++) {  m->mothurOut(outputNames[i]); m->mothurOutEndLine();    }
315                 m->mothurOutEndLine();
316
317                 return 0;
318         }
319         catch(exception& e) {
320                 m->errorOut(e, "TreeGroupCommand", "execute");
321                 exit(1);
322         }
323 }
324 //**********************************************************************************************************************
325
326 void TreeGroupCommand::createTree(){
327         try {
328                 //create tree
329                 t = new Tree();
330                 
331                 //do merges and create tree structure by setting parents and children
332                 //there are numGroups - 1 merges to do
333                 for (int i = 0; i < (numGroups - 1); i++) {
334                         float largest = -1000.0;
335
336                         int row, column;
337                         //find largest value in sims matrix by searching lower triangle
338                         for (int j = 1; j < simMatrix.size(); j++) {
339                                 for (int k = 0; k < j; k++) {
340                                         if (simMatrix[j][k] > largest) {  largest = simMatrix[j][k]; row = j; column = k;  }
341                                 }
342                         }
343
344                         //set non-leaf node info and update leaves to know their parents
345                         //non-leaf
346                         t->tree[numGroups + i].setChildren(index[row], index[column]);
347                         
348                         //parents
349                         t->tree[index[row]].setParent(numGroups + i);
350                         t->tree[index[column]].setParent(numGroups + i);
351                         
352                         //blength = distance / 2;
353                         float blength = ((1.0 - largest) / 2);
354                         
355                         //branchlengths
356                         t->tree[index[row]].setBranchLength(blength - t->tree[index[row]].getLengthToLeaves());
357                         t->tree[index[column]].setBranchLength(blength - t->tree[index[column]].getLengthToLeaves());
358                         
359                         //set your length to leaves to your childs length plus branchlength
360                         t->tree[numGroups + i].setLengthToLeaves(t->tree[index[row]].getLengthToLeaves() + t->tree[index[row]].getBranchLength());
361                         
362                         
363                         //update index 
364                         index[row] = numGroups+i;
365                         index[column] = numGroups+i;
366                         
367                         //remove highest value that caused the merge.
368                         simMatrix[row][column] = -1000.0;
369                         simMatrix[column][row] = -1000.0;
370                         
371                         //merge values in simsMatrix
372                         for (int n = 0; n < simMatrix.size(); n++)      {
373                                 //row becomes merge of 2 groups
374                                 simMatrix[row][n] = (simMatrix[row][n] + simMatrix[column][n]) / 2;
375                                 simMatrix[n][row] = simMatrix[row][n];
376                                 //delete column
377                                 simMatrix[column][n] = -1000.0;
378                                 simMatrix[n][column] = -1000.0;
379                         }
380                 }
381                 
382                 //adjust tree to make sure root to tip length is .5
383                 int root = t->findRoot();
384                 t->tree[root].setBranchLength((0.5 - t->tree[root].getLengthToLeaves()));
385                 
386                 //assemble tree
387                 t->assembleTree();
388                 
389                 //print newick file
390                 t->createNewickFile(outputFile);
391                 
392                 //delete tree
393                 delete t;
394         
395         }
396         catch(exception& e) {
397                 m->errorOut(e, "TreeGroupCommand", "createTree");
398                 exit(1);
399         }
400 }
401 /***********************************************************/
402 void TreeGroupCommand::printSims(ostream& out) {
403         try {
404                 
405                 //output column headers
406                 //out << '\t';
407                 //for (int i = 0; i < lookup.size(); i++) {     out << lookup[i]->getGroup() << '\t';           }
408                 //out << endl;
409                 
410                 
411                 for (int m = 0; m < simMatrix.size(); m++)      {
412                         //out << lookup[m]->getGroup() << '\t';
413                         for (int n = 0; n < simMatrix.size(); n++)      {
414                                 out << simMatrix[m][n] << '\t'; 
415                         }
416                         out << endl;
417                 }
418
419         }
420         catch(exception& e) {
421                 m->errorOut(e, "TreeGroupCommand", "printSims");
422                 exit(1);
423         }
424 }
425 /***********************************************************/
426 void TreeGroupCommand::makeSimsDist() {
427         try {
428                 numGroups = list->size();
429                 
430                 //initialize index
431                 index.clear();
432                 for (int g = 0; g < numGroups; g++) {   index[g] = g;   }
433                 
434                 //initialize simMatrix
435                 simMatrix.clear();
436                 simMatrix.resize(numGroups);
437                 for (int m = 0; m < simMatrix.size(); m++)      {
438                         for (int j = 0; j < simMatrix.size(); j++)      {
439                                 simMatrix[m].push_back(0.0);
440                         }
441                 }
442                 
443                 //go through sparse matrix and fill sims
444                 //go through each cell in the sparsematrix
445                 for(MatData currentCell = matrix->begin(); currentCell != matrix->end(); currentCell++){
446                         //similairity = -(distance-1)
447                         simMatrix[currentCell->row][currentCell->column] = -(currentCell->dist -1.0);   
448                         simMatrix[currentCell->column][currentCell->row] = -(currentCell->dist -1.0);                           
449                 }
450
451
452         }
453         catch(exception& e) {
454                 m->errorOut(e, "TreeGroupCommand", "makeSimsDist");
455                 exit(1);
456         }
457 }
458
459 /***********************************************************/
460 void TreeGroupCommand::makeSimsShared() {
461         try {
462         
463                 //clear globaldatas old tree names if any
464                 globaldata->Treenames.clear();
465                 
466                 //fills globaldatas tree names
467                 globaldata->Treenames = globaldata->Groups;
468                 
469                 //create treemap class from groupmap for tree class to use
470                 tmap = new TreeMap();
471                 tmap->makeSim(globaldata->gGroupmap);
472                 globaldata->gTreemap = tmap;
473                 
474                 set<string> processedLabels;
475                 set<string> userLabels = labels;
476                 
477                 //as long as you are not at the end of the file or done wih the lines you want
478                 while((lookup[0] != NULL) && ((allLines == 1) || (userLabels.size() != 0))) {
479                 
480                         if(allLines == 1 || labels.count(lookup[0]->getLabel()) == 1){                  
481                                 m->mothurOut(lookup[0]->getLabel()); m->mothurOutEndLine();
482                                 process(lookup);
483                                 
484                                 processedLabels.insert(lookup[0]->getLabel());
485                                 userLabels.erase(lookup[0]->getLabel());
486                         }
487                         
488                         if ((anyLabelsToProcess(lookup[0]->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
489                                 string saveLabel = lookup[0]->getLabel();
490                         
491                                 for (int i = 0; i < lookup.size(); i++) {  delete lookup[i];  } 
492                                 lookup = input->getSharedRAbundVectors(lastLabel);
493
494                                 m->mothurOut(lookup[0]->getLabel()); m->mothurOutEndLine();
495                                 process(lookup);
496                                         
497                                 processedLabels.insert(lookup[0]->getLabel());
498                                 userLabels.erase(lookup[0]->getLabel());
499                                 
500                                 //restore real lastlabel to save below
501                                 lookup[0]->setLabel(saveLabel);
502                         }
503
504                         lastLabel = lookup[0]->getLabel();                      
505                         
506                         //get next line to process
507                         for (int i = 0; i < lookup.size(); i++) {  delete lookup[i];  } 
508                         lookup = input->getSharedRAbundVectors();
509                 }
510                 
511                 //output error messages about any remaining user labels
512                 set<string>::iterator it;
513                 bool needToRun = false;
514                 for (it = userLabels.begin(); it != userLabels.end(); it++) {  
515                         m->mothurOut("Your file does not include the label " + *it); 
516                         if (processedLabels.count(lastLabel) != 1) {
517                                 m->mothurOut(". I will use " + lastLabel + "."); m->mothurOutEndLine();
518                                 needToRun = true;
519                         }else {
520                                 m->mothurOut(". Please refer to " + lastLabel + "."); m->mothurOutEndLine();
521                         }
522                 }
523                 
524                 //run last label if you need to
525                 if (needToRun == true)  {
526                         for (int i = 0; i < lookup.size(); i++) {  if (lookup[i] != NULL) {             delete lookup[i]; }             } 
527                         lookup = input->getSharedRAbundVectors(lastLabel);
528
529                         m->mothurOut(lookup[0]->getLabel()); m->mothurOutEndLine();
530                         process(lookup);
531                         for (int i = 0; i < lookup.size(); i++) {  delete lookup[i];  }         
532                 }
533                 
534                 for(int i = 0 ; i < treeCalculators.size(); i++) {  delete treeCalculators[i]; }
535         }
536         catch(exception& e) {
537                 m->errorOut(e, "TreeGroupCommand", "makeSimsShared");
538                 exit(1);
539         }
540 }
541
542 /***********************************************************/
543 void TreeGroupCommand::process(vector<SharedRAbundVector*> thisLookup) {
544         try{
545                                 EstOutput data;
546                                 vector<SharedRAbundVector*> subset;
547                                 numGroups = thisLookup.size();
548                                 
549                                 //for each calculator                                                                                           
550                                 for(int i = 0 ; i < treeCalculators.size(); i++) {
551                                         //initialize simMatrix
552                                         simMatrix.clear();
553                                         simMatrix.resize(numGroups);
554                                         for (int m = 0; m < simMatrix.size(); m++)      {
555                                                 for (int j = 0; j < simMatrix.size(); j++)      {
556                                                         simMatrix[m].push_back(0.0);
557                                                 }
558                                         }
559                 
560                                         //initialize index
561                                         index.clear();
562                                         for (int g = 0; g < numGroups; g++) {   index[g] = g;   }
563                 
564                                         //create a new filename
565                                         outputFile = outputDir + getRootName(getSimpleName(globaldata->inputFileName)) + treeCalculators[i]->getName() + "." + thisLookup[0]->getLabel() + ".tre";                              
566                                         outputNames.push_back(outputFile);
567                                                                                                 
568                                         for (int k = 0; k < thisLookup.size(); k++) { 
569                                                 for (int l = k; l < thisLookup.size(); l++) {
570                                                         if (k != l) { //we dont need to similiarity of a groups to itself
571                                                                 //get estimated similarity between 2 groups
572                                                                 
573                                                                 subset.clear(); //clear out old pair of sharedrabunds
574                                                                 //add new pair of sharedrabunds
575                                                                 subset.push_back(thisLookup[k]); subset.push_back(thisLookup[l]); 
576                                                                 
577                                                                 data = treeCalculators[i]->getValues(subset); //saves the calculator outputs
578                                                                 //save values in similarity matrix
579                                                                 simMatrix[k][l] = data[0];
580                                                                 simMatrix[l][k] = data[0];
581                                                         }
582                                                 }
583                                         }
584                                 
585                                         //creates tree from similarity matrix and write out file
586                                         createTree();
587                                 }
588
589         }
590         catch(exception& e) {
591                 m->errorOut(e, "TreeGroupCommand", "process");
592                 exit(1);
593         }
594 }
595 /***********************************************************/
596
597         
598