]> git.donarmstrong.com Git - mothur.git/blob - tree.cpp
print tree debugger
[mothur.git] / tree.cpp
1 /*
2  *  tree.cpp
3  *  Mothur
4  *
5  *  Created by Sarah Westcott on 1/22/09.
6  *  Copyright 2009 Schloss Lab UMASS Amherst. All rights reserved.
7  *
8  */
9
10 #include "tree.h"
11
12
13 /*****************************************************************/
14 Tree::Tree() {
15         try {
16         
17                 globaldata = GlobalData::getInstance();
18                 numLeaves = globaldata->gTreemap->getNumSeqs();
19                 numNodes = 2*numLeaves - 1;
20                 
21                 tree.resize(numNodes);
22
23                 //initialize tree with correct number of nodes, name and group info.
24                 for (int i = 0; i < numNodes; i++) {
25                         //initialize leaf nodes
26                         if (i <= (numLeaves-1)) {
27                                 tree[i].setName(globaldata->gTreemap->namesOfSeqs[i]);
28                                 tree[i].setGroup(globaldata->gTreemap->getGroup(globaldata->gTreemap->namesOfSeqs[i]));
29                                 //the node knows its index
30                                 tree[i].setIndex(i);
31                                 //set pcount and pGroup for groupname to 1.
32                                 tree[i].pcount[globaldata->gTreemap->getGroup(globaldata->gTreemap->namesOfSeqs[i])] = 1;
33                                 tree[i].pGroups[globaldata->gTreemap->getGroup(globaldata->gTreemap->namesOfSeqs[i])] = 1;
34                                 //Treemap knows name, group and index to speed up search
35                                 globaldata->gTreemap->setIndex(globaldata->gTreemap->namesOfSeqs[i], i);
36         
37                         //intialize non leaf nodes
38                         }else if (i > (numLeaves-1)) {
39                                 tree[i].setName("");
40                                 tree[i].setGroup("");
41                                 //the node knows its index
42                                 tree[i].setIndex(i);
43                         }
44                 }
45         }
46         catch(exception& e) {
47                 cout << "Standard Error: " << e.what() << " has occurred in the Tree class Function Tree. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
48                 exit(1);
49         }
50         catch(...) {
51                 cout << "An unknown error has occurred in the Tree class function Tree. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
52                 exit(1);
53         }               
54 }
55
56 /*****************************************************************/
57
58 int Tree::getIndex(string searchName) {
59         try {
60                 //Treemap knows name, group and index to speed up search
61                 // getIndex function will return the vector index or -1 if seq is not found.
62                 int index = globaldata->gTreemap->getIndex(searchName);
63                 return index;
64                 
65         }
66         catch(exception& e) {
67                 cout << "Standard Error: " << e.what() << " has occurred in the Tree class Function getIndex. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
68                 exit(1);
69         }
70         catch(...) {
71                 cout << "An unknown error has occurred in the Tree class function getIndex. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
72                 exit(1);
73         }               
74 }
75 /*****************************************************************/
76
77 void Tree::setIndex(string searchName, int index) {
78         try {
79                 //set index in treemap
80                 globaldata->gTreemap->setIndex(searchName, index);
81         }
82         catch(exception& e) {
83                 cout << "Standard Error: " << e.what() << " has occurred in the Tree class Function setIndex. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
84                 exit(1);
85         }
86         catch(...) {
87                 cout << "An unknown error has occurred in the Tree class function setIndex. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
88                 exit(1);
89         }               
90 }
91 /*****************************************************************/
92 void Tree::assembleTree() {
93         try {
94                 //build the pGroups in non leaf nodes to be used in the parsimony calcs.
95                 for (int i = numLeaves; i < numNodes; i++) {
96                         tree[i].pGroups = (mergeGroups(i));
97                         tree[i].pcount = (mergeGcounts(i));
98                 }
99         }
100         catch(exception& e) {
101                 cout << "Standard Error: " << e.what() << " has occurred in the Tree class Function assembleTree. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
102                 exit(1);
103         }
104         catch(...) {
105                 cout << "An unknown error has occurred in the Tree class function assembleTree. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
106                 exit(1);
107         }               
108 }
109 /*****************************************************************/
110 void Tree::getCopy(Tree* copy) {
111         try {
112         
113                 //for each node in the tree copy its info
114                 for (int i = 0; i < numNodes; i++) {
115                         //copy name
116                         tree[i].setName(copy->tree[i].getName());
117                 
118                         //copy group
119                         tree[i].setGroup(copy->tree[i].getGroup());
120                         
121                         //copy branch length
122                         tree[i].setBranchLength(copy->tree[i].getBranchLength());
123                 
124                         //copy parent
125                         tree[i].setParent(copy->tree[i].getParent());
126                 
127                         //copy children
128                         tree[i].setChildren(copy->tree[i].getLChild(), copy->tree[i].getRChild());
129                 
130                         //copy index in node and tmap
131                         tree[i].setIndex(copy->tree[i].getIndex());
132                         setIndex(copy->tree[i].getName(), getIndex(copy->tree[i].getName()));
133                         
134                         //copy pGroups
135                         tree[i].pGroups = copy->tree[i].pGroups;
136                 
137                         //copy pcount
138                         tree[i].pcount = copy->tree[i].pcount;
139                 }
140         }
141         catch(exception& e) {
142                 cout << "Standard Error: " << e.what() << " has occurred in the Tree class Function getCopy. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
143                 exit(1);
144         }
145         catch(...) {
146                 cout << "An unknown error has occurred in the Tree class function getCopy. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
147                 exit(1);
148         }               
149 }
150 /*****************************************************************/
151 //returns a map with a groupname and the number of times that group was seen in the children
152 //for instance if your children are white and black then it would return a map with 2 entries
153 // p[white] = 1 and p[black] = 1.  Now go up a level and merge that with a node who has p[white] = 1
154 //and you get p[white] = 2, p[black] = 1, but you erase the p[black] because you have a p value higher than 1.
155
156 map<string, int> Tree::mergeGroups(int i) {
157         try {
158                 int lc = tree[i].getLChild();
159                 int rc = tree[i].getRChild();
160                 
161                 //set parsimony groups to left child
162                 map<string,int> parsimony = tree[lc].pGroups;
163                 
164                 int maxPars = 1;
165
166                 //look at right child groups and update maxPars if right child has something higher for that group.
167                 for(it=tree[rc].pGroups.begin();it!=tree[rc].pGroups.end();it++){
168                         it2 = parsimony.find(it->first);
169                         if (it2 != parsimony.end()) {
170                                 parsimony[it->first]++;
171                         }else {
172                                 parsimony[it->first] = 1;
173                         }
174                         
175                         if(parsimony[it->first] > maxPars){
176                                 maxPars = parsimony[it->first];
177                         }
178                 }
179         
180                 // this is true if right child had a greater parsimony for a certain group
181                 if(maxPars > 1){
182                         //erase all the groups that are only 1 because you found something with 2.
183                         for(it=parsimony.begin();it!=parsimony.end();it++){
184                                 if(it->second == 1){
185                                         parsimony.erase(it->first);
186                                         it--;
187                                 }
188                         }
189                         //set one remaining groups to 1
190                         //so with our above example p[white] = 2 would be left and it would become p[white] = 1
191                         for(it=parsimony.begin();it!=parsimony.end();it++){
192                                 parsimony[it->first] = 1;
193                         }
194                 
195                 }
196         
197                 return parsimony;
198         }
199         catch(exception& e) {
200                 cout << "Standard Error: " << e.what() << " has occurred in the Tree class Function mergeGroups. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
201                 exit(1);
202         }
203         catch(...) {
204                 cout << "An unknown error has occurred in the Tree class function mergeGroups. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
205                 exit(1);
206         }               
207 }
208 /*****************************************************************/
209 //returns a map with a groupname and the number of times that group was seen in the children
210 //for instance if your children are white and black then it would return a map with 2 entries
211 // p[white] = 1 and p[black] = 1.  Now go up a level and merge that with a node who has p[white] = 1
212 //and you get p[white] = 2, p[black] = 1, but you erase the p[black] because you have a p value higher than 1.
213
214 map<string, int> Tree::mergeUserGroups(int i) {
215         try {
216         
217                 int lc = tree[i].getLChild();
218                 int rc = tree[i].getRChild();
219                 
220                 //loop through nodes groups removing the ones the user doesn't want
221                 for (it = tree[lc].pGroups.begin(); it != tree[lc].pGroups.end(); it++) {
222                         if (inUsersGroups(it->first, globaldata->Groups) != true) { tree[lc].pGroups.erase(it->first); }
223                 }
224                 
225                 //loop through nodes groups removing the ones the user doesn't want
226                 for (it = tree[rc].pGroups.begin(); it != tree[rc].pGroups.end(); it++) {
227                         if (inUsersGroups(it->first, globaldata->Groups) != true) { tree[rc].pGroups.erase(it->first); }
228                 }
229
230                 //set parsimony groups to left child
231                 map<string,int> parsimony = tree[lc].pGroups;
232                 
233                 int maxPars = 1;
234
235                 //look at right child groups and update maxPars if right child has something higher for that group.
236                 for(it=tree[rc].pGroups.begin();it!=tree[rc].pGroups.end();it++){
237                         it2 = parsimony.find(it->first);
238                         if (it2 != parsimony.end()) {
239                                 parsimony[it->first]++;
240                         }else {
241                                 parsimony[it->first] = 1;
242                         }
243                         
244                         if(parsimony[it->first] > maxPars){
245                                 maxPars = parsimony[it->first];
246                         }
247                 }
248         
249                 // this is true if right child had a greater parsimony for a certain group
250                 if(maxPars > 1){
251                         //erase all the groups that are only 1 because you found something with 2.
252                         for(it=parsimony.begin();it!=parsimony.end();it++){
253                                 if(it->second == 1){
254                                         parsimony.erase(it->first);
255                                         it--;
256                                 }
257                         }
258                         //set one remaining groups to 1
259                         //so with our above example p[white] = 2 would be left and it would become p[white] = 1
260                         for(it=parsimony.begin();it!=parsimony.end();it++){
261                                 parsimony[it->first] = 1;
262                         }
263                 
264                 }
265         
266                 return parsimony;
267         }
268         catch(exception& e) {
269                 cout << "Standard Error: " << e.what() << " has occurred in the Tree class Function mergeGroups. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
270                 exit(1);
271         }
272         catch(...) {
273                 cout << "An unknown error has occurred in the Tree class function mergeGroups. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
274                 exit(1);
275         }               
276 }
277
278
279 /**************************************************************************************************/
280
281 map<string,int> Tree::mergeGcounts(int position) {
282         try{
283                 map<string,int>::iterator pos;
284         
285                 int lc = tree[position].getLChild();
286                 int rc = tree[position].getRChild();
287         
288                 map<string,int> sum = tree[lc].pcount;
289     
290                 for(it=tree[rc].pcount.begin();it!=tree[rc].pcount.end();it++){
291                         sum[it->first] += it->second;
292                 }
293                 return sum;
294         }
295         catch(exception& e) {
296                 cout << "Standard Error: " << e.what() << " has occurred in the Tree class Function mergeGcounts. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
297                 exit(1);
298         }
299         catch(...) {
300                 cout << "An unknown error has occurred in the Tree class function mergeGcounts. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
301                 exit(1);
302         }               
303 }
304 /**************************************************************************************************/
305
306 void Tree::randomLabels() {
307         try {
308                 
309                 //set up the groups the user wants to include
310                 setGroups();
311                 
312                 for(int i=numLeaves-1;i>=0;i--){
313                         if(tree[i].pGroups.size() == 0){
314                                 continue;
315                         }
316                         
317                         int escape = 1;
318                         int z;
319                 
320                         while(escape == 1){
321                                 //get random index to switch with
322                                 z = int((float)(i+1) * (float)(rand()) / ((float)RAND_MAX+1.0));        
323                         
324                                 if(tree[z].pGroups.size() != 0){
325                                         escape = 0;
326                                 }
327                         }
328                         
329                         //you only want to randomize the nodes that are from a group the user wants analyzed, so
330                         //if either of the leaf nodes you are about to switch are not in the users groups then you don't want to switch them.
331                         bool treez, treei;
332                         
333                         //leaves have only one group so you can just set it to begin()
334                         it = tree[z].pGroups.begin();
335                         treez = inUsersGroups(it->first, globaldata->Groups);
336                         
337                         it = tree[i].pGroups.begin();
338                         treei = inUsersGroups(it->first, globaldata->Groups);
339                         
340                         if ((treez == true) && (treei == true)) {
341                                 //switches node i and node z's info.
342                                 map<string,int> lib_hold = tree[z].pGroups;
343                                 tree[z].pGroups = (tree[i].pGroups);
344                                 tree[i].pGroups = (lib_hold);
345                 
346                                 tree[z].setGroup(tree[z].pGroups.begin()->first);
347                                 tree[i].setGroup(tree[i].pGroups.begin()->first);
348                 
349                                 map<string,int> gcount_hold = tree[z].pcount;
350                                 tree[z].pcount = (tree[i].pcount);
351                                 tree[i].pcount = (gcount_hold);
352                         }
353                 }
354         }
355         catch(exception& e) {
356                 cout << "Standard Error: " << e.what() << " has occurred in the Tree class Function randomLabels. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
357                 exit(1);
358         }
359         catch(...) {
360                 cout << "An unknown error has occurred in the Tree class function randomLabels. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
361                 exit(1);
362         }               
363 }
364 /**************************************************************************************************/
365
366 void Tree::randomBlengths()  {
367         try {
368                 for(int i=numNodes-1;i>=0;i--){
369                         int z = int((float)(i+1) * (float)(rand()) / ((float)RAND_MAX+1.0));    
370                 
371                         float bl_hold = tree[z].getBranchLength();
372                         tree[z].setBranchLength(tree[i].getBranchLength());
373                         tree[i].setBranchLength(bl_hold);
374                 }
375         }
376         catch(exception& e) {
377                 cout << "Standard Error: " << e.what() << " has occurred in the Tree class Function randomBlengths. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
378                 exit(1);
379         }
380         catch(...) {
381                 cout << "An unknown error has occurred in the Tree class function randomBlengths. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
382                 exit(1);
383         }               
384 }
385 /*************************************************************************************************/
386 void Tree::assembleRandomUnifracTree() {
387         randomLabels();
388         assembleTree();
389 }
390
391 /*************************************************************************************************/
392 //for now it's just random topology but may become random labels as well later that why this is such a simple function now...
393 void Tree::assembleRandomTree() {
394         randomTopology();
395         assembleTree();
396 }
397 /**************************************************************************************************/
398
399 void Tree::randomTopology() {
400         try {
401                 for(int i=0;i<numNodes;i++){
402                         tree[i].setParent(-1);
403                 }
404                 for(int i=numLeaves;i<numNodes;i++){
405                         tree[i].setChildren(-1, -1); 
406                 }
407     
408                 for(int i=numLeaves;i<numNodes;i++){
409                         int escape =0;
410                         int rnd_index1, rnd_index2;
411                         while(escape == 0){
412                                 rnd_index1 = (int)(((double)rand() / (double) RAND_MAX)*i);
413                                 if(tree[rnd_index1].getParent() == -1){escape = 1;}
414                         }
415                 
416                         escape = 0;
417                         while(escape == 0){
418                                 rnd_index2 = (int)(((double)rand() / (double) RAND_MAX)*i);
419                                 if(rnd_index2 != rnd_index1 && tree[rnd_index2].getParent() == -1){
420                                         escape = 1;
421                                 }               
422                         }
423                 
424                         tree[i].setChildren(rnd_index1,rnd_index2);
425                         tree[i].setParent(-1);
426                         tree[rnd_index1].setParent(i);
427                         tree[rnd_index2].setParent(i);
428                 }
429         }
430         catch(exception& e) {
431                 cout << "Standard Error: " << e.what() << " has occurred in the Tree class Function randomTopology. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
432                 exit(1);
433         }
434         catch(...) {
435                 cout << "An unknown error has occurred in the Tree class function randomTopology. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
436                 exit(1);
437         }               
438 }
439
440 /*****************************************************************/
441 // This prints out the tree in Newick form.
442 void Tree::createNewickFile() {
443         try {
444                 int root = findRoot();
445                 filename = getRootName(globaldata->getTreeFile()) + "newick";
446                 openOutputFile(filename, out);
447                 
448                 printBranch(root);
449                 
450                 // you are at the end of the tree
451                 out << ";" << endl;
452         }
453         catch(exception& e) {
454                 cout << "Standard Error: " << e.what() << " has occurred in the Tree class Function createNewickFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
455                 exit(1);
456         }
457         catch(...) {
458                 cout << "An unknown error has occurred in the Tree class function createNewickFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
459                 exit(1);
460         }               
461 }
462
463 /*****************************************************************/
464 //This function finds the index of the root node.
465
466 int Tree::findRoot() {
467         try {
468                 for (int i = 0; i < numNodes; i++) {
469                         //you found the root
470                         if (tree[i].getParent() == -1) { return i; }
471                 }
472                 return -1;
473         }
474         catch(exception& e) {
475                 cout << "Standard Error: " << e.what() << " has occurred in the Tree class Function findRoot. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
476                 exit(1);
477         }
478         catch(...) {
479                 cout << "An unknown error has occurred in the Tree class function findRoot. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
480                 exit(1);
481         }               
482 }
483
484 /*****************************************************************/
485 void Tree::printBranch(int node) {
486         try {
487                 
488                 // you are not a leaf
489                 if (tree[node].getLChild() != -1) {
490                         out << "(";
491                         printBranch(tree[node].getLChild());
492                         out << ",";
493                         printBranch(tree[node].getRChild());
494                         out << ")";
495                 }else { //you are a leaf
496                         tree[node].printNode();  //prints out name and branch length
497                 }
498                 
499         }
500         catch(exception& e) {
501                 cout << "Standard Error: " << e.what() << " has occurred in the Tree class Function printBranch. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
502                 exit(1);
503         }
504         catch(...) {
505                 cout << "An unknown error has occurred in the Tree class function printBranch. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
506                 exit(1);
507         }               
508 }
509
510 /*****************************************************************/
511
512 void Tree::setGroups() {
513         try {
514                 //if the user has not entered specific groups to analyze then do them all
515                 if (globaldata->Groups.size() != 0) {
516                         //check that groups are valid
517                         for (int i = 0; i < globaldata->Groups.size(); i++) {
518                                 if (globaldata->gTreemap->isValidGroup(globaldata->Groups[i]) != true) {
519                                         cout << globaldata->Groups[i] << " is not a valid group, and will be disregarded." << endl;
520                                         // erase the invalid group from globaldata->Groups
521                                         globaldata->Groups.erase (globaldata->Groups.begin()+i);
522                                 }
523                         }
524                         
525                         //if the user only entered invalid groups
526                         if (globaldata->Groups.size() == 0) { 
527                                 cout << "When using the groups parameter you must have at least 1 valid group. I will run the command using all the groups in your groupfile." << endl; 
528                                 for (int i = 0; i < globaldata->gTreemap->namesOfGroups.size(); i++) {
529                                         globaldata->Groups.push_back(globaldata->gTreemap->namesOfGroups[i]);
530                                 }
531                         }
532                                         
533                 }else {
534                         for (int i = 0; i < globaldata->gTreemap->namesOfGroups.size(); i++) {
535                                 globaldata->Groups.push_back(globaldata->gTreemap->namesOfGroups[i]);
536                         }
537                 }
538         }
539         catch(exception& e) {
540                 cout << "Standard Error: " << e.what() << " has occurred in the Tree class Function setGroups. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
541                 exit(1);
542         }
543         catch(...) {
544                 cout << "An unknown error has occurred in the Tree class function setGroups. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
545                 exit(1);
546         }               
547
548 }
549
550 /*****************************************************************/
551
552 void Tree::printTree() {
553         
554         for(int i=0;i<numNodes;i++){
555                 cout << i << '\t';
556                 tree[i].printNode();
557         }
558         
559 }
560
561 /*****************************************************************/
562
563