]> git.donarmstrong.com Git - mothur.git/blob - tree.cpp
fixed unweighted calculator
[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         
159                 int lc = tree[i].getLChild();
160                 int rc = tree[i].getRChild();
161                 
162                 //set parsimony groups to left child
163                 map<string,int> parsimony = tree[lc].pGroups;
164                 
165                 int maxPars = 1;
166
167                 //look at right child groups and update maxPars if right child has something higher for that group.
168                 for(it=tree[rc].pGroups.begin();it!=tree[rc].pGroups.end();it++){
169                         parsimony[it->first]++;
170                         
171                         if(parsimony[it->first] > maxPars){
172                                 maxPars = parsimony[it->first];
173                         }
174                 }
175         
176                 // this is true if right child had a greater parsimony for a certain group
177                 if(maxPars > 1){
178                         //erase all the groups that are only 1 because you found something with 2.
179                         for(it=parsimony.begin();it!=parsimony.end();it++){
180                                 if(it->second == 1){
181                                         parsimony.erase(it->first);
182                                         it--;
183                                 }
184                         }
185                         //set one remaining groups to 1
186                         //so with our above example p[white] = 2 would be left and it would become p[white] = 1
187                         for(it=parsimony.begin();it!=parsimony.end();it++){
188                                 parsimony[it->first] = 1;
189                         }
190                 
191                 }
192         
193                 return parsimony;
194         }
195         catch(exception& e) {
196                 cout << "Standard Error: " << e.what() << " has occurred in the Tree class Function mergeGroups. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
197                 exit(1);
198         }
199         catch(...) {
200                 cout << "An unknown error has occurred in the Tree class function mergeGroups. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
201                 exit(1);
202         }               
203 }
204
205 /**************************************************************************************************/
206
207 map<string,int> Tree::mergeGcounts(int position) {
208         try{
209                 map<string,int>::iterator pos;
210         
211                 int lc = tree[position].getLChild();
212                 int rc = tree[position].getRChild();
213         
214                 map<string,int> sum = tree[lc].pcount;
215     
216                 for(it=tree[rc].pcount.begin();it!=tree[rc].pcount.end();it++){
217                         sum[it->first] += it->second;
218                 }
219                 return sum;
220         }
221         catch(exception& e) {
222                 cout << "Standard Error: " << e.what() << " has occurred in the Tree class Function mergeGcounts. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
223                 exit(1);
224         }
225         catch(...) {
226                 cout << "An unknown error has occurred in the Tree class function mergeGcounts. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
227                 exit(1);
228         }               
229 }
230 /**************************************************************************************************/
231
232 void Tree::randomLabels() {
233         try {
234                 
235                 //set up the groups the user wants to include
236                 setGroups();
237                 
238                 for(int i=numLeaves-1;i>=0;i--){
239                         if(tree[i].pGroups.size() == 0){
240                                 continue;
241                         }
242                         
243                         int escape = 1;
244                         int z;
245                 
246                         while(escape == 1){
247                                 //get random index to switch with
248                                 z = int((float)(i+1) * (float)(rand()) / ((float)RAND_MAX+1.0));        
249                         
250                                 if(tree[z].pGroups.size() != 0){
251                                         escape = 0;
252                                 }
253                         }
254                         
255                         //you only want to randomize the nodes that are from a group the user wants analyzed, so
256                         //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.
257                         bool treez, treei;
258                         
259                         //leaves have only one group so you can just set it to begin()
260                         it = tree[z].pGroups.begin();
261                         treez = inUsersGroups(it->first, globaldata->Groups);
262                         
263                         it = tree[i].pGroups.begin();
264                         treei = inUsersGroups(it->first, globaldata->Groups);
265                         
266                         if ((treez == true) && (treei == true)) {
267                                 //switches node i and node z's info.
268                                 map<string,int> lib_hold = tree[z].pGroups;
269                                 tree[z].pGroups = (tree[i].pGroups);
270                                 tree[i].pGroups = (lib_hold);
271                 
272                                 tree[z].setGroup(tree[z].pGroups.begin()->first);
273                                 tree[i].setGroup(tree[i].pGroups.begin()->first);
274                 
275                                 map<string,int> gcount_hold = tree[z].pcount;
276                                 tree[z].pcount = (tree[i].pcount);
277                                 tree[i].pcount = (gcount_hold);
278                         }
279                 }
280         }
281         catch(exception& e) {
282                 cout << "Standard Error: " << e.what() << " has occurred in the Tree class Function randomLabels. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
283                 exit(1);
284         }
285         catch(...) {
286                 cout << "An unknown error has occurred in the Tree class function randomLabels. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
287                 exit(1);
288         }               
289 }
290 /**************************************************************************************************/
291
292 void Tree::randomBlengths()  {
293         try {
294                 for(int i=numNodes-1;i>=0;i--){
295                         int z = int((float)(i+1) * (float)(rand()) / ((float)RAND_MAX+1.0));    
296                 
297                         float bl_hold = tree[z].getBranchLength();
298                         tree[z].setBranchLength(tree[i].getBranchLength());
299                         tree[i].setBranchLength(bl_hold);
300                 }
301         }
302         catch(exception& e) {
303                 cout << "Standard Error: " << e.what() << " has occurred in the Tree class Function randomBlengths. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
304                 exit(1);
305         }
306         catch(...) {
307                 cout << "An unknown error has occurred in the Tree class function randomBlengths. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
308                 exit(1);
309         }               
310 }
311 /*************************************************************************************************/
312 void Tree::assembleRandomUnifracTree() {
313         randomLabels();
314         assembleTree();
315 }
316
317 /*************************************************************************************************/
318 //for now it's just random topology but may become random labels as well later that why this is such a simple function now...
319 void Tree::assembleRandomTree() {
320         randomTopology();
321         assembleTree();
322 }
323 /**************************************************************************************************/
324
325 void Tree::randomTopology() {
326         try {
327                 for(int i=0;i<numNodes;i++){
328                         tree[i].setParent(-1);
329                 }
330                 for(int i=numLeaves;i<numNodes;i++){
331                         tree[i].setChildren(-1, -1); 
332                 }
333     
334                 for(int i=numLeaves;i<numNodes;i++){
335                         int escape =0;
336                         int rnd_index1, rnd_index2;
337                         while(escape == 0){
338                                 rnd_index1 = (int)(((double)rand() / (double) RAND_MAX)*i);
339                                 if(tree[rnd_index1].getParent() == -1){escape = 1;}
340                         }
341                 
342                         escape = 0;
343                         while(escape == 0){
344                                 rnd_index2 = (int)(((double)rand() / (double) RAND_MAX)*i);
345                                 if(rnd_index2 != rnd_index1 && tree[rnd_index2].getParent() == -1){
346                                         escape = 1;
347                                 }               
348                         }
349                 
350                         tree[i].setChildren(rnd_index1,rnd_index2);
351                         tree[i].setParent(-1);
352                         tree[rnd_index1].setParent(i);
353                         tree[rnd_index2].setParent(i);
354                 }
355         }
356         catch(exception& e) {
357                 cout << "Standard Error: " << e.what() << " has occurred in the Tree class Function randomTopology. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
358                 exit(1);
359         }
360         catch(...) {
361                 cout << "An unknown error has occurred in the Tree class function randomTopology. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
362                 exit(1);
363         }               
364 }
365
366 /*****************************************************************/
367 // This prints out the tree in Newick form.
368 void Tree::createNewickFile() {
369         try {
370                 int root = findRoot();
371                 filename = getRootName(globaldata->getTreeFile()) + "newick";
372                 openOutputFile(filename, out);
373                 
374                 printBranch(root);
375                 
376                 // you are at the end of the tree
377                 out << ";" << endl;
378         }
379         catch(exception& e) {
380                 cout << "Standard Error: " << e.what() << " has occurred in the Tree class Function createNewickFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
381                 exit(1);
382         }
383         catch(...) {
384                 cout << "An unknown error has occurred in the Tree class function createNewickFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
385                 exit(1);
386         }               
387 }
388
389 /*****************************************************************/
390 //This function finds the index of the root node.
391
392 int Tree::findRoot() {
393         try {
394                 for (int i = 0; i < numNodes; i++) {
395                         //you found the root
396                         if (tree[i].getParent() == -1) { return i; }
397                 }
398                 return -1;
399         }
400         catch(exception& e) {
401                 cout << "Standard Error: " << e.what() << " has occurred in the Tree class Function findRoot. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
402                 exit(1);
403         }
404         catch(...) {
405                 cout << "An unknown error has occurred in the Tree class function findRoot. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
406                 exit(1);
407         }               
408 }
409
410 /*****************************************************************/
411 void Tree::printBranch(int node) {
412         try {
413                 
414                 // you are not a leaf
415                 if (tree[node].getLChild() != -1) {
416                         out << "(";
417                         printBranch(tree[node].getLChild());
418                         out << ",";
419                         printBranch(tree[node].getRChild());
420                         out << ")";
421                 }else { //you are a leaf
422                         tree[node].printNode(out);  //prints out name and branch length
423                 }
424                 
425         }
426         catch(exception& e) {
427                 cout << "Standard Error: " << e.what() << " has occurred in the Tree class Function printBranch. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
428                 exit(1);
429         }
430         catch(...) {
431                 cout << "An unknown error has occurred in the Tree class function printBranch. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
432                 exit(1);
433         }               
434 }
435
436 /*****************************************************************/
437 void Tree::setGroups() {
438         try {
439                 //if the user has not entered specific groups to analyze then do them all
440                 if (globaldata->Groups.size() != 0) {
441                         //check that groups are valid
442                         for (int i = 0; i < globaldata->Groups.size(); i++) {
443                                 if (globaldata->gTreemap->isValidGroup(globaldata->Groups[i]) != true) {
444                                         cout << globaldata->Groups[i] << " is not a valid group, and will be disregarded." << endl;
445                                         // erase the invalid group from globaldata->Groups
446                                         globaldata->Groups.erase (globaldata->Groups.begin()+i);
447                                 }
448                         }
449                         
450                         //if the user only entered invalid groups
451                         if (globaldata->Groups.size() == 0) { 
452                                 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; 
453                                 for (int i = 0; i < globaldata->gTreemap->namesOfGroups.size(); i++) {
454                                         globaldata->Groups.push_back(globaldata->gTreemap->namesOfGroups[i]);
455                                 }
456                         }
457                                         
458                 }else {
459                         for (int i = 0; i < globaldata->gTreemap->namesOfGroups.size(); i++) {
460                                 globaldata->Groups.push_back(globaldata->gTreemap->namesOfGroups[i]);
461                         }
462                 }
463         }
464         catch(exception& e) {
465                 cout << "Standard Error: " << e.what() << " has occurred in the Tree class Function setGroups. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
466                 exit(1);
467         }
468         catch(...) {
469                 cout << "An unknown error has occurred in the Tree class function setGroups. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
470                 exit(1);
471         }               
472
473 }
474