5 * Created by Sarah Westcott on 1/22/09.
6 * Copyright 2009 Schloss Lab UMASS Amherst. All rights reserved.
13 /*****************************************************************/
16 globaldata = GlobalData::getInstance();
18 if (globaldata->runParse == true) { parseTreeFile(); globaldata->runParse = false; }
19 //for(int i = 0; i < globaldata->Treenames.size(); i++) { cout << i << '\t' << globaldata->Treenames[i] << endl; }
20 numLeaves = globaldata->Treenames.size();
21 numNodes = 2*numLeaves - 1;
23 tree.resize(numNodes);
25 //initialize tree with correct number of nodes, name and group info.
26 for (int i = 0; i < numNodes; i++) {
27 //initialize leaf nodes
28 if (i <= (numLeaves-1)) {
29 tree[i].setName(globaldata->Treenames[i]);
30 tree[i].setGroup(globaldata->gTreemap->getGroup(globaldata->Treenames[i]));
31 //set pcount and pGroup for groupname to 1.
32 tree[i].pcount[globaldata->gTreemap->getGroup(globaldata->Treenames[i])] = 1;
33 tree[i].pGroups[globaldata->gTreemap->getGroup(globaldata->Treenames[i])] = 1;
34 //Treemap knows name, group and index to speed up search
35 globaldata->gTreemap->setIndex(globaldata->Treenames[i], i);
37 //intialize non leaf nodes
38 }else if (i > (numLeaves-1)) {
45 errorOut(e, "Tree", "Tree");
50 /*****************************************************************/
52 /*****************************************************************/
53 void Tree::addNamesToCounts() {
55 //ex. seq1 seq2,seq3,se4
61 //before this function seq1.pcount = pasture -> 1
62 //after seq1.pcount = pasture -> 2, forest -> 1, ocean -> 1
64 //before this function seq1.pgroups = pasture -> 1
65 //after seq1.pgroups = pasture -> 1 since that is the dominant group
68 //go through each leaf and update its pcounts and pgroups
69 for (int i = 0; i < numLeaves; i++) {
70 string name = tree[i].getName();
72 map<string, string>::iterator itNames = globaldata->names.find(name);
74 if (itNames == globaldata->names.end()) { mothurOut(name + " is not in your name file, please correct."); mothurOutEndLine(); exit(1); }
76 vector<string> dupNames;
77 splitAtComma(globaldata->names[name], dupNames);
79 map<string, int>::iterator itCounts;
81 for (int j = 0; j < dupNames.size(); j++) {
83 if (dupNames[j] != name) {//you already added yourself in the constructor
84 string group = globaldata->gTreemap->getGroup(dupNames[j]);
87 itCounts = tree[i].pcount.find(group);
88 if (itCounts == tree[i].pcount.end()) { //new group, add it
89 tree[i].pcount[group] = 1;
91 tree[i].pcount[group]++;
95 itCounts = tree[i].pGroups.find(group);
96 if (itCounts == tree[i].pGroups.end()) { //new group, add it
97 tree[i].pGroups[group] = 1;
99 tree[i].pGroups[group]++;
103 if(tree[i].pGroups[group] > maxPars){
104 maxPars = tree[i].pGroups[group];
109 if (maxPars > 1) { //then we have some more dominant groups
110 //erase all the groups that are less than maxPars because you found a more dominant group.
111 for(it=tree[i].pGroups.begin();it!=tree[i].pGroups.end();){
112 if(it->second < maxPars){
113 tree[i].pGroups.erase(it++);
116 //set one remaining groups to 1
117 for(it=tree[i].pGroups.begin();it!=tree[i].pGroups.end();it++){
118 tree[i].pGroups[it->first] = 1;
126 catch(exception& e) {
127 errorOut(e, "Tree", "addNamesToCounts");
131 /*****************************************************************/
132 int Tree::getIndex(string searchName) {
134 //Treemap knows name, group and index to speed up search
135 // getIndex function will return the vector index or -1 if seq is not found.
136 int index = globaldata->gTreemap->getIndex(searchName);
140 catch(exception& e) {
141 errorOut(e, "Tree", "getIndex");
145 /*****************************************************************/
147 void Tree::setIndex(string searchName, int index) {
149 //set index in treemap
150 globaldata->gTreemap->setIndex(searchName, index);
152 catch(exception& e) {
153 errorOut(e, "Tree", "setIndex");
157 /*****************************************************************/
158 void Tree::assembleTree() {
161 //if user has given a names file we want to include that info in the pgroups and pcount info.
162 if(globaldata->names.size() != 0) { addNamesToCounts(); }
164 //build the pGroups in non leaf nodes to be used in the parsimony calcs.
165 for (int i = numLeaves; i < numNodes; i++) {
166 tree[i].pGroups = (mergeGroups(i));
167 tree[i].pcount = (mergeGcounts(i));
170 catch(exception& e) {
171 errorOut(e, "Tree", "assembleTree");
175 /*****************************************************************/
176 void Tree::getCopy(Tree* copy) {
179 //for each node in the tree copy its info
180 for (int i = 0; i < numNodes; i++) {
182 tree[i].setName(copy->tree[i].getName());
185 tree[i].setGroup(copy->tree[i].getGroup());
188 tree[i].setBranchLength(copy->tree[i].getBranchLength());
191 tree[i].setParent(copy->tree[i].getParent());
194 tree[i].setChildren(copy->tree[i].getLChild(), copy->tree[i].getRChild());
196 //copy index in node and tmap
197 tree[i].setIndex(copy->tree[i].getIndex());
198 setIndex(copy->tree[i].getName(), getIndex(copy->tree[i].getName()));
201 tree[i].pGroups = copy->tree[i].pGroups;
204 tree[i].pcount = copy->tree[i].pcount;
208 catch(exception& e) {
209 errorOut(e, "Tree", "getCopy");
213 /*****************************************************************/
214 //returns a map with a groupname and the number of times that group was seen in the children
215 //for instance if your children are white and black then it would return a map with 2 entries
216 // p[white] = 1 and p[black] = 1. Now go up a level and merge that with a node who has p[white] = 1
217 //and you get p[white] = 2, p[black] = 1, but you erase the p[black] because you have a p value higher than 1.
219 map<string, int> Tree::mergeGroups(int i) {
221 int lc = tree[i].getLChild();
222 int rc = tree[i].getRChild();
224 //set parsimony groups to left child
225 map<string,int> parsimony = tree[lc].pGroups;
229 //look at right child groups and update maxPars if right child has something higher for that group.
230 for(it=tree[rc].pGroups.begin();it!=tree[rc].pGroups.end();it++){
231 it2 = parsimony.find(it->first);
232 if (it2 != parsimony.end()) {
233 parsimony[it->first]++;
235 parsimony[it->first] = 1;
238 if(parsimony[it->first] > maxPars){
239 maxPars = parsimony[it->first];
243 // this is true if right child had a greater parsimony for a certain group
245 //erase all the groups that are only 1 because you found something with 2.
246 for(it=parsimony.begin();it!=parsimony.end();){
248 parsimony.erase(it++);
251 //set one remaining groups to 1
252 //so with our above example p[white] = 2 would be left and it would become p[white] = 1
253 for(it=parsimony.begin();it!=parsimony.end();it++){
254 parsimony[it->first] = 1;
261 catch(exception& e) {
262 errorOut(e, "Tree", "mergeGroups");
266 /*****************************************************************/
267 //returns a map with a groupname and the number of times that group was seen in the children
268 //for instance if your children are white and black then it would return a map with 2 entries
269 // p[white] = 1 and p[black] = 1. Now go up a level and merge that with a node who has p[white] = 1
270 //and you get p[white] = 2, p[black] = 1, but you erase the p[black] because you have a p value higher than 1.
272 map<string, int> Tree::mergeUserGroups(int i, vector<string> g) {
275 int lc = tree[i].getLChild();
276 int rc = tree[i].getRChild();
278 //loop through nodes groups removing the ones the user doesn't want
279 for(it=tree[lc].pGroups.begin();it!=tree[lc].pGroups.end();){
280 if (inUsersGroups(it->first, g) != true) {
281 tree[lc].pGroups.erase(it++);
285 //loop through nodes groups removing the ones the user doesn't want
286 for(it=tree[rc].pGroups.begin();it!=tree[rc].pGroups.end();){
287 if (inUsersGroups(it->first, g) != true) {
288 tree[rc].pGroups.erase(it++);
292 //set parsimony groups to left child
293 map<string,int> parsimony = tree[lc].pGroups;
297 //look at right child groups and update maxPars if right child has something higher for that group.
298 for(it=tree[rc].pGroups.begin();it!=tree[rc].pGroups.end();it++){
299 it2 = parsimony.find(it->first);
300 if (it2 != parsimony.end()) {
301 parsimony[it->first]++;
303 parsimony[it->first] = 1;
306 if(parsimony[it->first] > maxPars){
307 maxPars = parsimony[it->first];
311 // this is true if right child had a greater parsimony for a certain group
313 //erase all the groups that are only 1 because you found something with 2.
314 for(it=parsimony.begin();it!=parsimony.end();){
316 parsimony.erase(it++);
320 for(it=parsimony.begin();it!=parsimony.end();it++){
321 parsimony[it->first] = 1;
327 catch(exception& e) {
328 errorOut(e, "Tree", "mergeUserGroups");
334 /**************************************************************************************************/
336 map<string,int> Tree::mergeGcounts(int position) {
338 map<string,int>::iterator pos;
340 int lc = tree[position].getLChild();
341 int rc = tree[position].getRChild();
343 map<string,int> sum = tree[lc].pcount;
345 for(it=tree[rc].pcount.begin();it!=tree[rc].pcount.end();it++){
346 sum[it->first] += it->second;
350 catch(exception& e) {
351 errorOut(e, "Tree", "mergeGcounts");
355 /**************************************************************************************************/
357 void Tree::randomLabels(vector<string> g) {
360 for(int i = 0; i < numLeaves; i++){
362 //get random index to switch with
363 z = int((float)(i+1) * (float)(rand()) / ((float)RAND_MAX+1.0));
365 //you only want to randomize the nodes that are from a group the user wants analyzed, so
366 //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.
369 treez = inUsersGroups(tree[z].getGroup(), g);
370 treei = inUsersGroups(tree[i].getGroup(), g);
372 if ((treez == true) && (treei == true)) {
373 //switches node i and node z's info.
374 map<string,int> lib_hold = tree[z].pGroups;
375 tree[z].pGroups = (tree[i].pGroups);
376 tree[i].pGroups = (lib_hold);
378 string zgroup = tree[z].getGroup();
379 tree[z].setGroup(tree[i].getGroup());
380 tree[i].setGroup(zgroup);
382 string zname = tree[z].getName();
383 tree[z].setName(tree[i].getName());
384 tree[i].setName(zname);
386 map<string,int> gcount_hold = tree[z].pcount;
387 tree[z].pcount = (tree[i].pcount);
388 tree[i].pcount = (gcount_hold);
392 catch(exception& e) {
393 errorOut(e, "Tree", "randomLabels");
397 /**************************************************************************************************/
399 void Tree::randomLabels(string groupA, string groupB) {
401 int numSeqsA = globaldata->gTreemap->seqsPerGroup[groupA];
402 int numSeqsB = globaldata->gTreemap->seqsPerGroup[groupB];
404 vector<string> randomGroups(numSeqsA+numSeqsB, groupA);
405 for(int i=numSeqsA;i<randomGroups.size();i++){
406 randomGroups[i] = groupB;
408 random_shuffle(randomGroups.begin(), randomGroups.end());
410 int randomCounter = 0;
411 for(int i=0;i<numLeaves;i++){
412 if(tree[i].getGroup() == groupA || tree[i].getGroup() == groupB){
413 tree[i].setGroup(randomGroups[randomCounter]);
414 tree[i].pcount.clear();
415 tree[i].pcount[randomGroups[randomCounter]] = 1;
416 tree[i].pGroups.clear();
417 tree[i].pGroups[randomGroups[randomCounter]] = 1;
422 catch(exception& e) {
423 errorOut(e, "Tree", "randomLabels");
427 /**************************************************************************************************/
428 void Tree::randomBlengths() {
430 for(int i=numNodes-1;i>=0;i--){
431 int z = int((float)(i+1) * (float)(rand()) / ((float)RAND_MAX+1.0));
433 float bl_hold = tree[z].getBranchLength();
434 tree[z].setBranchLength(tree[i].getBranchLength());
435 tree[i].setBranchLength(bl_hold);
438 catch(exception& e) {
439 errorOut(e, "Tree", "randomBlengths");
443 /*************************************************************************************************/
444 void Tree::assembleRandomUnifracTree(vector<string> g) {
448 /*************************************************************************************************/
449 void Tree::assembleRandomUnifracTree(string groupA, string groupB) {
450 randomLabels(groupA, groupB);
454 /*************************************************************************************************/
455 //for now it's just random topology but may become random labels as well later that why this is such a simple function now...
456 void Tree::assembleRandomTree() {
460 /**************************************************************************************************/
462 void Tree::randomTopology() {
464 for(int i=0;i<numNodes;i++){
465 tree[i].setParent(-1);
467 for(int i=numLeaves;i<numNodes;i++){
468 tree[i].setChildren(-1, -1);
471 for(int i=numLeaves;i<numNodes;i++){
473 int rnd_index1, rnd_index2;
475 rnd_index1 = (int)(((double)rand() / (double) RAND_MAX)*i);
476 if(tree[rnd_index1].getParent() == -1){escape = 1;}
481 rnd_index2 = (int)(((double)rand() / (double) RAND_MAX)*i);
482 if(rnd_index2 != rnd_index1 && tree[rnd_index2].getParent() == -1){
487 tree[i].setChildren(rnd_index1,rnd_index2);
488 tree[i].setParent(-1);
489 tree[rnd_index1].setParent(i);
490 tree[rnd_index2].setParent(i);
493 catch(exception& e) {
494 errorOut(e, "Tree", "randomTopology");
498 /*****************************************************************/
499 void Tree::print(ostream& out) {
501 int root = findRoot();
502 printBranch(root, out, "branch");
505 catch(exception& e) {
506 errorOut(e, "Tree", "print");
510 /*****************************************************************/
511 void Tree::printForBoot(ostream& out) {
513 int root = findRoot();
514 printBranch(root, out, "boot");
517 catch(exception& e) {
518 errorOut(e, "Tree", "printForBoot");
523 /*****************************************************************/
524 // This prints out the tree in Newick form.
525 void Tree::createNewickFile(string f) {
527 int root = findRoot();
528 //filename = getRootName(globaldata->getTreeFile()) + "newick";
531 openOutputFile(filename, out);
533 printBranch(root, out, "branch");
535 // you are at the end of the tree
539 catch(exception& e) {
540 errorOut(e, "Tree", "createNewickFile");
545 /*****************************************************************/
546 //This function finds the index of the root node.
548 int Tree::findRoot() {
550 for (int i = 0; i < numNodes; i++) {
552 if (tree[i].getParent() == -1) { return i; }
553 //cout << "i = " << i << endl;
554 //cout << "i's parent = " << tree[i].getParent() << endl;
558 catch(exception& e) {
559 errorOut(e, "Tree", "findRoot");
564 /*****************************************************************/
565 void Tree::printBranch(int node, ostream& out, string mode) {
568 // you are not a leaf
569 if (tree[node].getLChild() != -1) {
571 printBranch(tree[node].getLChild(), out, mode);
573 printBranch(tree[node].getRChild(), out, mode);
575 if (mode == "branch") {
576 //if there is a branch length then print it
577 if (tree[node].getBranchLength() != -1) {
578 out << ":" << tree[node].getBranchLength();
580 }else if (mode == "boot") {
581 //if there is a label then print it
582 if (tree[node].getLabel() != -1) {
583 out << tree[node].getLabel();
586 }else { //you are a leaf
587 out << tree[node].getGroup();
588 if (mode == "branch") {
589 //if there is a branch length then print it
590 if (tree[node].getBranchLength() != -1) {
591 out << ":" << tree[node].getBranchLength();
593 }else if (mode == "boot") {
594 //if there is a label then print it
595 if (tree[node].getLabel() != -1) {
596 out << tree[node].getLabel();
602 catch(exception& e) {
603 errorOut(e, "Tree", "printBranch");
608 /*****************************************************************/
610 void Tree::printTree() {
612 for(int i=0;i<numNodes;i++){
619 /*****************************************************************/
620 //this code is a mess and should be rethought...-slw
621 void Tree::parseTreeFile() {
623 //only takes names from the first tree and assumes that all trees use the same names.
625 string filename = globaldata->getTreeFile();
627 openInputFile(filename, filehandle);
632 //ifyou are not a nexus file
633 if((c = filehandle.peek()) != '#') {
634 while((c = filehandle.peek()) != ';') {
635 while ((c = filehandle.peek()) != ';') {
643 if((c == '(') && (comment != 1)){ break; }
647 done = readTreeString(filehandle);
648 if (done == 0) { break; }
650 //ifyou are a nexus file
651 }else if((c = filehandle.peek()) == '#') {
655 while(holder != "translate" && holder != "Translate"){
656 if(holder == "[" || holder == "[!"){
662 filehandle >> holder;
664 //if there is no translate then you must read tree string otherwise use translate to get names
665 if((holder == "tree") && (comment != 1)){
666 //pass over the "tree rep.6878900 = "
667 while (((c = filehandle.get()) != '(') && ((c = filehandle.peek()) != EOF)) {;}
669 if(c == EOF) { break; }
670 filehandle.putback(c); //put back first ( of tree.
671 done = readTreeString(filehandle);
676 if (done == 0) { break; }
679 //use nexus translation rather than parsing tree to save time
680 if((holder == "translate") || (holder == "Translate")) {
682 string number, name, h;
683 h = ""; // so it enters the loop the first time
684 while((h != ";") && (number != ";")) {
685 filehandle >> number;
688 //c = , until done with translation then c = ;
689 h = name.substr(name.length()-1, name.length());
690 name.erase(name.end()-1); //erase the comma
691 globaldata->Treenames.push_back(number);
693 if(number == ";") { globaldata->Treenames.pop_back(); } //in case ';' from translation is on next line instead of next to last name
698 catch(exception& e) {
699 errorOut(e, "Tree", "parseTreeFile");
703 /*******************************************************/
705 /*******************************************************/
706 int Tree::readTreeString(ifstream& filehandle) {
711 while((c = filehandle.peek()) != ';') {
713 //cout << " at beginning of while " << k << endl;
715 //to pass over labels in trees
717 while((c!=',') && (c != -1) && (c!= ':') && (c!=';')){ c=filehandle.get(); }
718 filehandle.putback(c);
720 if(c == ';') { return 0; }
721 if(c == -1) { return 0; }
723 if((c != '(') && (c != ')') && (c != ',') && (c != ':') && (c != '\n') && (c != '\t') && (c != 32)) { //32 is space
725 c = filehandle.get();
728 while ((c != '(') && (c != ')') && (c != ',') && (c != ':') && (c != '\n') && (c != 32) && (c != '\t')) {
730 c = filehandle.get();
732 //cout << " in name while " << k << endl;
735 //cout << "name = " << name << endl;
736 globaldata->Treenames.push_back(name);
737 filehandle.putback(c);
739 //cout << " after putback" << k << endl;
742 if(c == ':') { //read until you reach the end of the branch length
743 while ((c != '(') && (c != ')') && (c != ',') && (c != ';') && (c != '\n') && (c != '\t') && (c != 32)) {
744 c = filehandle.get();
746 //cout << " in branch while " << k << endl;
748 filehandle.putback(c);
751 c = filehandle.get();
753 //cout << " here after get " << k << endl;
754 if(c == ';') { return 0; }
755 if(c == ')') { filehandle.putback(c); }
762 catch(exception& e) {
763 errorOut(e, "Tree", "readTreeString");
768 /*******************************************************/
770 /*******************************************************/