]> git.donarmstrong.com Git - mothur.git/blob - tree.cpp
fixed summary.shared bug and set jumble default to 1.
[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                 globaldata = GlobalData::getInstance();
17
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
26                         //initialize leaf nodes
27                         if (i <= (numLeaves-1)) {
28                                 tree[i].setName(globaldata->gTreemap->namesOfSeqs[i]);
29                                 tree[i].setGroup(globaldata->gTreemap->getGroup(globaldata->gTreemap->namesOfSeqs[i]));
30                                 //the node knows its index
31                                 tree[i].setIndex(i);
32                                 //Treemap knows name, group and index to speed up search
33                                 globaldata->gTreemap->setIndex(globaldata->gTreemap->namesOfSeqs[i], i);
34                         //intialize non leaf nodes
35                         }else if (i > (numLeaves-1)) {
36                                 tree[i].setName("");
37                                 tree[i].setGroup("");
38                                 //the node knows its index
39                                 tree[i].setIndex(i);
40                         }
41                 }
42         }
43         catch(exception& e) {
44                 cout << "Standard Error: " << e.what() << " has occurred in the Tree class Function Tree. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
45                 exit(1);
46         }
47         catch(...) {
48                 cout << "An unknown error has occurred in the Tree class function Tree. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
49                 exit(1);
50         }               
51 }
52
53 /*****************************************************************/
54
55 int Tree::getIndex(string searchName) {
56         try {
57                 //Treemap knows name, group and index to speed up search
58                 // getIndex function will return the vector index or -1 if seq is not found.
59                 int index = globaldata->gTreemap->getIndex(searchName);
60                 return index;
61                 
62         }
63         catch(exception& e) {
64                 cout << "Standard Error: " << e.what() << " has occurred in the Tree class Function getIndex. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
65                 exit(1);
66         }
67         catch(...) {
68                 cout << "An unknown error has occurred in the Tree class function getIndex. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
69                 exit(1);
70         }               
71 }
72 /*****************************************************************/
73
74 void Tree::setIndex(string searchName, int index) {
75         try {
76                 //set index in treemap
77                 globaldata->gTreemap->setIndex(searchName, index);
78         }
79         catch(exception& e) {
80                 cout << "Standard Error: " << e.what() << " has occurred in the Tree class Function setIndex. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
81                 exit(1);
82         }
83         catch(...) {
84                 cout << "An unknown error has occurred in the Tree class function setIndex. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
85                 exit(1);
86         }               
87 }
88
89
90 /*****************************************************************/
91 // This prints out the tree in Newick form.
92 void Tree::createNewickFile() {
93         try {
94                 int root = findRoot();
95                 filename = getRootName(globaldata->getTreeFile()) + "newick";
96                 openOutputFile(filename, out);
97                 
98                 printBranch(root);
99                 
100                 // you are at the end of the tree
101                 out << ";" << endl;
102         }
103         catch(exception& e) {
104                 cout << "Standard Error: " << e.what() << " has occurred in the Tree class Function createNewickFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
105                 exit(1);
106         }
107         catch(...) {
108                 cout << "An unknown error has occurred in the Tree class function createNewickFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
109                 exit(1);
110         }               
111 }
112
113 /*****************************************************************/
114 //This function finds the index of the root node.
115
116 int Tree::findRoot() {
117         try {
118                 for (int i = 0; i < numNodes; i++) {
119                         //you found the root
120                         if (tree[i].getParent() == -1) { return i; }
121                 }
122                 return -1;
123         }
124         catch(exception& e) {
125                 cout << "Standard Error: " << e.what() << " has occurred in the Tree class Function findRoot. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
126                 exit(1);
127         }
128         catch(...) {
129                 cout << "An unknown error has occurred in the Tree class function findRoot. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
130                 exit(1);
131         }               
132 }
133
134 /*****************************************************************/
135 void Tree::printBranch(int node) {
136         try {
137                 
138                 // you are not a leaf
139                 if (tree[node].getLChild() != -1) {
140                         out << "(";
141                         printBranch(tree[node].getLChild());
142                         out << ",";
143                         printBranch(tree[node].getRChild());
144                         out << ")";
145                 }else { //you are a leaf
146                         tree[node].printNode(out);  //prints out name and branch length
147                 }
148                 
149         }
150         catch(exception& e) {
151                 cout << "Standard Error: " << e.what() << " has occurred in the Tree class Function printBranch. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
152                 exit(1);
153         }
154         catch(...) {
155                 cout << "An unknown error has occurred in the Tree class function printBranch. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
156                 exit(1);
157         }               
158 }
159
160 /*****************************************************************/
161
162
163