5 * Created by Sarah Westcott on 1/22/09.
6 * Copyright 2009 Schloss Lab UMASS Amherst. All rights reserved.
13 /*****************************************************************/
17 globaldata = GlobalData::getInstance();
18 numLeaves = globaldata->gTreemap->getNumSeqs();
19 numNodes = 2*numLeaves - 1;
21 tree.resize(numNodes);
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 //set pcount and pGroup for groupname to 1.
30 tree[i].pcount[globaldata->gTreemap->getGroup(globaldata->gTreemap->namesOfSeqs[i])] = 1;
31 tree[i].pGroups[globaldata->gTreemap->getGroup(globaldata->gTreemap->namesOfSeqs[i])] = 1;
32 //Treemap knows name, group and index to speed up search
33 globaldata->gTreemap->setIndex(globaldata->gTreemap->namesOfSeqs[i], i);
35 //intialize non leaf nodes
36 }else if (i > (numLeaves-1)) {
43 cout << "Standard Error: " << e.what() << " has occurred in the Tree class Function Tree. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
47 cout << "An unknown error has occurred in the Tree class function Tree. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
52 /*****************************************************************/
53 int Tree::getIndex(string searchName) {
55 //Treemap knows name, group and index to speed up search
56 // getIndex function will return the vector index or -1 if seq is not found.
57 int index = globaldata->gTreemap->getIndex(searchName);
62 cout << "Standard Error: " << e.what() << " has occurred in the Tree class Function getIndex. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
66 cout << "An unknown error has occurred in the Tree class function getIndex. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
70 /*****************************************************************/
72 void Tree::setIndex(string searchName, int index) {
74 //set index in treemap
75 globaldata->gTreemap->setIndex(searchName, index);
78 cout << "Standard Error: " << e.what() << " has occurred in the Tree class Function setIndex. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
82 cout << "An unknown error has occurred in the Tree class function setIndex. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
86 /*****************************************************************/
87 void Tree::assembleTree() {
89 //build the pGroups in non leaf nodes to be used in the parsimony calcs.
90 for (int i = numLeaves; i < numNodes; i++) {
91 tree[i].pGroups = (mergeGroups(i));
92 tree[i].pcount = (mergeGcounts(i));
96 cout << "Standard Error: " << e.what() << " has occurred in the Tree class Function assembleTree. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
100 cout << "An unknown error has occurred in the Tree class function assembleTree. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
104 /*****************************************************************/
105 void Tree::getCopy(Tree* copy) {
108 //for each node in the tree copy its info
109 for (int i = 0; i < numNodes; i++) {
111 tree[i].setName(copy->tree[i].getName());
114 tree[i].setGroup(copy->tree[i].getGroup());
117 tree[i].setBranchLength(copy->tree[i].getBranchLength());
120 tree[i].setParent(copy->tree[i].getParent());
123 tree[i].setChildren(copy->tree[i].getLChild(), copy->tree[i].getRChild());
125 //copy index in node and tmap
126 tree[i].setIndex(copy->tree[i].getIndex());
127 setIndex(copy->tree[i].getName(), getIndex(copy->tree[i].getName()));
130 tree[i].pGroups = copy->tree[i].pGroups;
133 tree[i].pcount = copy->tree[i].pcount;
136 catch(exception& e) {
137 cout << "Standard Error: " << e.what() << " has occurred in the Tree class Function getCopy. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
141 cout << "An unknown error has occurred in the Tree class function getCopy. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
145 /*****************************************************************/
146 //returns a map with a groupname and the number of times that group was seen in the children
147 //for instance if your children are white and black then it would return a map with 2 entries
148 // p[white] = 1 and p[black] = 1. Now go up a level and merge that with a node who has p[white] = 1
149 //and you get p[white] = 2, p[black] = 1, but you erase the p[black] because you have a p value higher than 1.
151 map<string, int> Tree::mergeGroups(int i) {
153 int lc = tree[i].getLChild();
154 int rc = tree[i].getRChild();
156 //set parsimony groups to left child
157 map<string,int> parsimony = tree[lc].pGroups;
161 //look at right child groups and update maxPars if right child has something higher for that group.
162 for(it=tree[rc].pGroups.begin();it!=tree[rc].pGroups.end();it++){
163 it2 = parsimony.find(it->first);
164 if (it2 != parsimony.end()) {
165 parsimony[it->first]++;
167 parsimony[it->first] = 1;
170 if(parsimony[it->first] > maxPars){
171 maxPars = parsimony[it->first];
175 // this is true if right child had a greater parsimony for a certain group
177 //erase all the groups that are only 1 because you found something with 2.
178 for(it=parsimony.begin();it!=parsimony.end();it++){
180 parsimony.erase(it->first);
184 //set one remaining groups to 1
185 //so with our above example p[white] = 2 would be left and it would become p[white] = 1
186 for(it=parsimony.begin();it!=parsimony.end();it++){
187 parsimony[it->first] = 1;
194 catch(exception& e) {
195 cout << "Standard Error: " << e.what() << " has occurred in the Tree class Function mergeGroups. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
199 cout << "An unknown error has occurred in the Tree class function mergeGroups. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
203 /*****************************************************************/
204 //returns a map with a groupname and the number of times that group was seen in the children
205 //for instance if your children are white and black then it would return a map with 2 entries
206 // p[white] = 1 and p[black] = 1. Now go up a level and merge that with a node who has p[white] = 1
207 //and you get p[white] = 2, p[black] = 1, but you erase the p[black] because you have a p value higher than 1.
209 map<string, int> Tree::mergeUserGroups(int i, vector<string> g) {
212 int lc = tree[i].getLChild();
213 int rc = tree[i].getRChild();
215 //loop through nodes groups removing the ones the user doesn't want
216 for (it = tree[lc].pGroups.begin(); it != tree[lc].pGroups.end(); it++) {
217 if (inUsersGroups(it->first, g) != true) { tree[lc].pGroups.erase(it->first); }
220 //loop through nodes groups removing the ones the user doesn't want
221 for (it = tree[rc].pGroups.begin(); it != tree[rc].pGroups.end(); it++) {
222 if (inUsersGroups(it->first, g) != true) { tree[rc].pGroups.erase(it->first); }
225 //set parsimony groups to left child
226 map<string,int> parsimony = tree[lc].pGroups;
230 //look at right child groups and update maxPars if right child has something higher for that group.
231 for(it=tree[rc].pGroups.begin();it!=tree[rc].pGroups.end();it++){
232 it2 = parsimony.find(it->first);
233 if (it2 != parsimony.end()) {
234 parsimony[it->first]++;
236 parsimony[it->first] = 1;
239 if(parsimony[it->first] > maxPars){
240 maxPars = parsimony[it->first];
244 // this is true if right child had a greater parsimony for a certain group
246 //erase all the groups that are only 1 because you found something with 2.
247 for(it=parsimony.begin();it!=parsimony.end();it++){
249 parsimony.erase(it->first);
252 for(it=parsimony.begin();it!=parsimony.end();it++){
253 parsimony[it->first] = 1;
259 catch(exception& e) {
260 cout << "Standard Error: " << e.what() << " has occurred in the Tree class Function mergeGroups. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
264 cout << "An unknown error has occurred in the Tree class function mergeGroups. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
270 /**************************************************************************************************/
272 map<string,int> Tree::mergeGcounts(int position) {
274 map<string,int>::iterator pos;
276 int lc = tree[position].getLChild();
277 int rc = tree[position].getRChild();
279 map<string,int> sum = tree[lc].pcount;
281 for(it=tree[rc].pcount.begin();it!=tree[rc].pcount.end();it++){
282 sum[it->first] += it->second;
286 catch(exception& e) {
287 cout << "Standard Error: " << e.what() << " has occurred in the Tree class Function mergeGcounts. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
291 cout << "An unknown error has occurred in the Tree class function mergeGcounts. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
295 /**************************************************************************************************/
297 void Tree::randomLabels(vector<string> g) {
300 for(int i = 0; i < numLeaves; i++){
302 //get random index to switch with
303 z = int((float)(i+1) * (float)(rand()) / ((float)RAND_MAX+1.0));
305 //you only want to randomize the nodes that are from a group the user wants analyzed, so
306 //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.
309 treez = inUsersGroups(tree[z].getGroup(), g);
310 treei = inUsersGroups(tree[i].getGroup(), g);
312 if ((treez == true) && (treei == true)) {
313 //switches node i and node z's info.
314 map<string,int> lib_hold = tree[z].pGroups;
315 tree[z].pGroups = (tree[i].pGroups);
316 tree[i].pGroups = (lib_hold);
318 string zgroup = tree[z].getGroup();
319 tree[z].setGroup(tree[i].getGroup());
320 tree[i].setGroup(zgroup);
322 string zname = tree[z].getName();
323 tree[z].setName(tree[i].getName());
324 tree[i].setName(zname);
326 map<string,int> gcount_hold = tree[z].pcount;
327 tree[z].pcount = (tree[i].pcount);
328 tree[i].pcount = (gcount_hold);
332 catch(exception& e) {
333 cout << "Standard Error: " << e.what() << " has occurred in the Tree class Function randomLabels. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
337 cout << "An unknown error has occurred in the Tree class function randomLabels. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
341 /**************************************************************************************************/
343 void Tree::randomLabels(string groupA, string groupB) {
345 int numSeqsA = globaldata->gTreemap->seqsPerGroup[groupA];
346 int numSeqsB = globaldata->gTreemap->seqsPerGroup[groupB];
348 vector<string> randomGroups(numSeqsA+numSeqsB, groupA);
349 for(int i=numSeqsA;i<randomGroups.size();i++){
350 randomGroups[i] = groupB;
352 random_shuffle(randomGroups.begin(), randomGroups.end());
354 int randomCounter = 0;
355 for(int i=0;i<numLeaves;i++){
356 if(tree[i].getGroup() == groupA || tree[i].getGroup() == groupB){
357 tree[i].setGroup(randomGroups[randomCounter]);
358 tree[i].pcount.clear();
359 tree[i].pcount[randomGroups[randomCounter]] = 1;
360 tree[i].pGroups.clear();
361 tree[i].pGroups[randomGroups[randomCounter]] = 1;
366 catch(exception& e) {
367 cout << "Standard Error: " << e.what() << " has occurred in the Tree class Function randomLabels. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
371 cout << "An unknown error has occurred in the Tree class function randomLabels. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
375 /**************************************************************************************************/
376 void Tree::randomBlengths() {
378 for(int i=numNodes-1;i>=0;i--){
379 int z = int((float)(i+1) * (float)(rand()) / ((float)RAND_MAX+1.0));
381 float bl_hold = tree[z].getBranchLength();
382 tree[z].setBranchLength(tree[i].getBranchLength());
383 tree[i].setBranchLength(bl_hold);
386 catch(exception& e) {
387 cout << "Standard Error: " << e.what() << " has occurred in the Tree class Function randomBlengths. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
391 cout << "An unknown error has occurred in the Tree class function randomBlengths. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
395 /*************************************************************************************************/
396 void Tree::assembleRandomUnifracTree(vector<string> g) {
400 /*************************************************************************************************/
401 void Tree::assembleRandomUnifracTree(string groupA, string groupB) {
402 randomLabels(groupA, groupB);
406 /*************************************************************************************************/
407 //for now it's just random topology but may become random labels as well later that why this is such a simple function now...
408 void Tree::assembleRandomTree() {
412 /**************************************************************************************************/
414 void Tree::randomTopology() {
416 for(int i=0;i<numNodes;i++){
417 tree[i].setParent(-1);
419 for(int i=numLeaves;i<numNodes;i++){
420 tree[i].setChildren(-1, -1);
423 for(int i=numLeaves;i<numNodes;i++){
425 int rnd_index1, rnd_index2;
427 rnd_index1 = (int)(((double)rand() / (double) RAND_MAX)*i);
428 if(tree[rnd_index1].getParent() == -1){escape = 1;}
433 rnd_index2 = (int)(((double)rand() / (double) RAND_MAX)*i);
434 if(rnd_index2 != rnd_index1 && tree[rnd_index2].getParent() == -1){
439 tree[i].setChildren(rnd_index1,rnd_index2);
440 tree[i].setParent(-1);
441 tree[rnd_index1].setParent(i);
442 tree[rnd_index2].setParent(i);
445 catch(exception& e) {
446 cout << "Standard Error: " << e.what() << " has occurred in the Tree class Function randomTopology. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
450 cout << "An unknown error has occurred in the Tree class function randomTopology. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
455 /*****************************************************************/
456 // This prints out the tree in Newick form.
457 void Tree::createNewickFile(string f) {
459 int root = findRoot();
460 //filename = getRootName(globaldata->getTreeFile()) + "newick";
462 openOutputFile(filename, out);
466 // you are at the end of the tree
470 catch(exception& e) {
471 cout << "Standard Error: " << e.what() << " has occurred in the Tree class Function createNewickFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
475 cout << "An unknown error has occurred in the Tree class function createNewickFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
480 /*****************************************************************/
481 //This function finds the index of the root node.
483 int Tree::findRoot() {
485 for (int i = 0; i < numNodes; i++) {
487 if (tree[i].getParent() == -1) { return i; }
491 catch(exception& e) {
492 cout << "Standard Error: " << e.what() << " has occurred in the Tree class Function findRoot. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
496 cout << "An unknown error has occurred in the Tree class function findRoot. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
501 /*****************************************************************/
502 void Tree::printBranch(int node) {
505 // you are not a leaf
506 if (tree[node].getLChild() != -1) {
508 printBranch(tree[node].getLChild());
510 printBranch(tree[node].getRChild());
512 }else { //you are a leaf
513 out << tree[node].getGroup() << ":" << tree[node].getBranchLength();
517 catch(exception& e) {
518 cout << "Standard Error: " << e.what() << " has occurred in the Tree class Function printBranch. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
522 cout << "An unknown error has occurred in the Tree class function printBranch. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
527 /*****************************************************************/
529 void Tree::printTree() {
531 for(int i=0;i<numNodes;i++){
538 /*****************************************************************/