5 * Created by Sarah Westcott on 1/22/09.
6 * Copyright 2009 Schloss Lab UMASS Amherst. All rights reserved.
13 /*****************************************************************/
16 globaldata = GlobalData::getInstance();
17 m = MothurOut::getInstance();
19 if (globaldata->runParse == true) { parseTreeFile(); globaldata->runParse = false; }
20 //for(int i = 0; i < globaldata->Treenames.size(); i++) { cout << i << '\t' << globaldata->Treenames[i] << endl; }
21 numLeaves = globaldata->Treenames.size();
22 numNodes = 2*numLeaves - 1;
24 tree.resize(numNodes);
26 //initialize tree with correct number of nodes, name and group info.
27 for (int i = 0; i < numNodes; i++) {
28 //initialize leaf nodes
29 if (i <= (numLeaves-1)) {
30 tree[i].setName(globaldata->Treenames[i]);
31 vector<string> tempGroups; tempGroups.push_back(globaldata->gTreemap->getGroup(globaldata->Treenames[i]));
32 tree[i].setGroup(tempGroups);
33 //set pcount and pGroup for groupname to 1.
34 tree[i].pcount[globaldata->gTreemap->getGroup(globaldata->Treenames[i])] = 1;
35 tree[i].pGroups[globaldata->gTreemap->getGroup(globaldata->Treenames[i])] = 1;
36 //Treemap knows name, group and index to speed up search
37 globaldata->gTreemap->setIndex(globaldata->Treenames[i], i);
39 //intialize non leaf nodes
40 }else if (i > (numLeaves-1)) {
42 vector<string> tempGroups;
43 tree[i].setGroup(tempGroups);
48 m->errorOut(e, "Tree", "Tree");
53 /*****************************************************************/
55 /*****************************************************************/
56 void Tree::addNamesToCounts() {
58 //ex. seq1 seq2,seq3,se4
64 //before this function seq1.pcount = pasture -> 1
65 //after seq1.pcount = pasture -> 2, forest -> 1, ocean -> 1
67 //before this function seq1.pgroups = pasture -> 1
68 //after seq1.pgroups = pasture -> 1 since that is the dominant group
71 //go through each leaf and update its pcounts and pgroups
75 for (int i = 0; i < numLeaves; i++) {
77 string name = tree[i].getName();
79 map<string, string>::iterator itNames = globaldata->names.find(name);
81 if (itNames == globaldata->names.end()) { m->mothurOut(name + " is not in your name file, please correct."); m->mothurOutEndLine(); exit(1); }
83 vector<string> dupNames;
84 m->splitAtComma(globaldata->names[name], dupNames);
86 map<string, int>::iterator itCounts;
88 for (int j = 0; j < dupNames.size(); j++) {
90 if (dupNames[j] != name) {//you already added yourself in the constructor
91 string group = globaldata->gTreemap->getGroup(dupNames[j]);
94 itCounts = tree[i].pcount.find(group);
95 if (itCounts == tree[i].pcount.end()) { //new group, add it
96 tree[i].pcount[group] = 1;
98 tree[i].pcount[group]++;
102 itCounts = tree[i].pGroups.find(group);
103 if (itCounts == tree[i].pGroups.end()) { //new group, add it
104 tree[i].pGroups[group] = 1;
106 tree[i].pGroups[group]++;
110 if(tree[i].pGroups[group] > maxPars){
111 maxPars = tree[i].pGroups[group];
116 if (maxPars > 1) { //then we have some more dominant groups
117 //erase all the groups that are less than maxPars because you found a more dominant group.
118 for(it=tree[i].pGroups.begin();it!=tree[i].pGroups.end();){
119 if(it->second < maxPars){
120 tree[i].pGroups.erase(it++);
123 //set one remaining groups to 1
124 for(it=tree[i].pGroups.begin();it!=tree[i].pGroups.end();it++){
125 tree[i].pGroups[it->first] = 1;
129 //update groups to reflect all the groups this node represents
130 vector<string> nodeGroups;
131 map<string, int>::iterator itGroups;
132 for (itGroups = tree[i].pcount.begin(); itGroups != tree[i].pcount.end(); itGroups++) {
133 nodeGroups.push_back(itGroups->first);
135 tree[i].setGroup(nodeGroups);
141 //cout << "addNamesToCounts\t" << (B - A) / CLOCKS_PER_SEC << endl;
144 catch(exception& e) {
145 m->errorOut(e, "Tree", "addNamesToCounts");
149 /*****************************************************************/
150 int Tree::getIndex(string searchName) {
152 //Treemap knows name, group and index to speed up search
153 // getIndex function will return the vector index or -1 if seq is not found.
154 int index = globaldata->gTreemap->getIndex(searchName);
158 catch(exception& e) {
159 m->errorOut(e, "Tree", "getIndex");
163 /*****************************************************************/
165 void Tree::setIndex(string searchName, int index) {
167 //set index in treemap
168 globaldata->gTreemap->setIndex(searchName, index);
170 catch(exception& e) {
171 m->errorOut(e, "Tree", "setIndex");
175 /*****************************************************************/
176 int Tree::assembleTree() {
180 //if user has given a names file we want to include that info in the pgroups and pcount info.
181 if(globaldata->names.size() != 0) { addNamesToCounts(); }
183 //build the pGroups in non leaf nodes to be used in the parsimony calcs.
184 for (int i = numLeaves; i < numNodes; i++) {
185 if (m->control_pressed) { return 1; }
187 tree[i].pGroups = (mergeGroups(i));
188 tree[i].pcount = (mergeGcounts(i));
191 //cout << "assembleTree\t" << (B-A) / CLOCKS_PER_SEC << endl;
194 catch(exception& e) {
195 m->errorOut(e, "Tree", "assembleTree");
199 /*****************************************************************/
200 void Tree::getCopy(Tree* copy) {
203 //for each node in the tree copy its info
204 for (int i = 0; i < numNodes; i++) {
206 tree[i].setName(copy->tree[i].getName());
209 tree[i].setGroup(copy->tree[i].getGroup());
212 tree[i].setBranchLength(copy->tree[i].getBranchLength());
215 tree[i].setParent(copy->tree[i].getParent());
218 tree[i].setChildren(copy->tree[i].getLChild(), copy->tree[i].getRChild());
220 //copy index in node and tmap
221 tree[i].setIndex(copy->tree[i].getIndex());
222 setIndex(copy->tree[i].getName(), getIndex(copy->tree[i].getName()));
225 tree[i].pGroups = copy->tree[i].pGroups;
228 tree[i].pcount = copy->tree[i].pcount;
232 catch(exception& e) {
233 m->errorOut(e, "Tree", "getCopy");
237 /*****************************************************************/
238 //returns a map with a groupname and the number of times that group was seen in the children
239 //for instance if your children are white and black then it would return a map with 2 entries
240 // p[white] = 1 and p[black] = 1. Now go up a level and merge that with a node who has p[white] = 1
241 //and you get p[white] = 2, p[black] = 1, but you erase the p[black] because you have a p value higher than 1.
243 map<string, int> Tree::mergeGroups(int i) {
245 int lc = tree[i].getLChild();
246 int rc = tree[i].getRChild();
248 //set parsimony groups to left child
249 map<string,int> parsimony = tree[lc].pGroups;
253 //look at right child groups and update maxPars if right child has something higher for that group.
254 for(it=tree[rc].pGroups.begin();it!=tree[rc].pGroups.end();it++){
255 it2 = parsimony.find(it->first);
256 if (it2 != parsimony.end()) {
257 parsimony[it->first]++;
259 parsimony[it->first] = 1;
262 if(parsimony[it->first] > maxPars){
263 maxPars = parsimony[it->first];
267 // this is true if right child had a greater parsimony for a certain group
269 //erase all the groups that are only 1 because you found something with 2.
270 for(it=parsimony.begin();it!=parsimony.end();){
272 parsimony.erase(it++);
275 //set one remaining groups to 1
276 //so with our above example p[white] = 2 would be left and it would become p[white] = 1
277 for(it=parsimony.begin();it!=parsimony.end();it++){
278 parsimony[it->first] = 1;
285 catch(exception& e) {
286 m->errorOut(e, "Tree", "mergeGroups");
290 /*****************************************************************/
291 //returns a map with a groupname and the number of times that group was seen in the children
292 //for instance if your children are white and black then it would return a map with 2 entries
293 // p[white] = 1 and p[black] = 1. Now go up a level and merge that with a node who has p[white] = 1
294 //and you get p[white] = 2, p[black] = 1, but you erase the p[black] because you have a p value higher than 1.
296 map<string, int> Tree::mergeUserGroups(int i, vector<string> g) {
299 int lc = tree[i].getLChild();
300 int rc = tree[i].getRChild();
302 //loop through nodes groups removing the ones the user doesn't want
303 for(it=tree[lc].pGroups.begin();it!=tree[lc].pGroups.end();){
304 if (m->inUsersGroups(it->first, g) != true) {
305 tree[lc].pGroups.erase(it++);
309 //loop through nodes groups removing the ones the user doesn't want
310 for(it=tree[rc].pGroups.begin();it!=tree[rc].pGroups.end();){
311 if (m->inUsersGroups(it->first, g) != true) {
312 tree[rc].pGroups.erase(it++);
316 //set parsimony groups to left child
317 map<string,int> parsimony = tree[lc].pGroups;
321 //look at right child groups and update maxPars if right child has something higher for that group.
322 for(it=tree[rc].pGroups.begin();it!=tree[rc].pGroups.end();it++){
323 it2 = parsimony.find(it->first);
324 if (it2 != parsimony.end()) {
325 parsimony[it->first]++;
327 parsimony[it->first] = 1;
330 if(parsimony[it->first] > maxPars){
331 maxPars = parsimony[it->first];
335 // this is true if right child had a greater parsimony for a certain group
337 //erase all the groups that are only 1 because you found something with 2.
338 for(it=parsimony.begin();it!=parsimony.end();){
340 parsimony.erase(it++);
344 for(it=parsimony.begin();it!=parsimony.end();it++){
345 parsimony[it->first] = 1;
351 catch(exception& e) {
352 m->errorOut(e, "Tree", "mergeUserGroups");
358 /**************************************************************************************************/
360 map<string,int> Tree::mergeGcounts(int position) {
362 map<string,int>::iterator pos;
364 int lc = tree[position].getLChild();
365 int rc = tree[position].getRChild();
367 map<string,int> sum = tree[lc].pcount;
369 for(it=tree[rc].pcount.begin();it!=tree[rc].pcount.end();it++){
370 sum[it->first] += it->second;
374 catch(exception& e) {
375 m->errorOut(e, "Tree", "mergeGcounts");
379 /**************************************************************************************************/
381 void Tree::randomLabels(vector<string> g) {
384 for(int i = 0; i < numLeaves; i++){
386 //get random index to switch with
387 z = int((float)(i+1) * (float)(rand()) / ((float)RAND_MAX+1.0));
389 //you only want to randomize the nodes that are from a group the user wants analyzed, so
390 //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.
393 treez = m->inUsersGroups(tree[z].getGroup(), g);
394 treei = m->inUsersGroups(tree[i].getGroup(), g);
396 if ((treez == true) && (treei == true)) {
397 //switches node i and node z's info.
398 map<string,int> lib_hold = tree[z].pGroups;
399 tree[z].pGroups = (tree[i].pGroups);
400 tree[i].pGroups = (lib_hold);
402 vector<string> zgroup = tree[z].getGroup();
403 tree[z].setGroup(tree[i].getGroup());
404 tree[i].setGroup(zgroup);
406 string zname = tree[z].getName();
407 tree[z].setName(tree[i].getName());
408 tree[i].setName(zname);
410 map<string,int> gcount_hold = tree[z].pcount;
411 tree[z].pcount = (tree[i].pcount);
412 tree[i].pcount = (gcount_hold);
416 catch(exception& e) {
417 m->errorOut(e, "Tree", "randomLabels");
421 /**************************************************************************************************
423 void Tree::randomLabels(string groupA, string groupB) {
425 int numSeqsA = globaldata->gTreemap->seqsPerGroup[groupA];
426 int numSeqsB = globaldata->gTreemap->seqsPerGroup[groupB];
428 vector<string> randomGroups(numSeqsA+numSeqsB, groupA);
429 for(int i=numSeqsA;i<randomGroups.size();i++){
430 randomGroups[i] = groupB;
432 random_shuffle(randomGroups.begin(), randomGroups.end());
434 int randomCounter = 0;
435 for(int i=0;i<numLeaves;i++){
436 if(tree[i].getGroup() == groupA || tree[i].getGroup() == groupB){
437 tree[i].setGroup(randomGroups[randomCounter]);
438 tree[i].pcount.clear();
439 tree[i].pcount[randomGroups[randomCounter]] = 1;
440 tree[i].pGroups.clear();
441 tree[i].pGroups[randomGroups[randomCounter]] = 1;
446 catch(exception& e) {
447 m->errorOut(e, "Tree", "randomLabels");
451 /**************************************************************************************************/
452 void Tree::randomBlengths() {
454 for(int i=numNodes-1;i>=0;i--){
455 int z = int((float)(i+1) * (float)(rand()) / ((float)RAND_MAX+1.0));
457 float bl_hold = tree[z].getBranchLength();
458 tree[z].setBranchLength(tree[i].getBranchLength());
459 tree[i].setBranchLength(bl_hold);
462 catch(exception& e) {
463 m->errorOut(e, "Tree", "randomBlengths");
467 /*************************************************************************************************/
468 void Tree::assembleRandomUnifracTree(vector<string> g) {
472 /*************************************************************************************************/
473 void Tree::assembleRandomUnifracTree(string groupA, string groupB) {
475 vector<string> temp; temp.push_back(groupA); temp.push_back(groupB);
480 /*************************************************************************************************/
481 //for now it's just random topology but may become random labels as well later that why this is such a simple function now...
482 void Tree::assembleRandomTree() {
486 /**************************************************************************************************/
488 void Tree::randomTopology() {
490 for(int i=0;i<numNodes;i++){
491 tree[i].setParent(-1);
493 for(int i=numLeaves;i<numNodes;i++){
494 tree[i].setChildren(-1, -1);
497 for(int i=numLeaves;i<numNodes;i++){
499 int rnd_index1, rnd_index2;
501 rnd_index1 = (int)(((double)rand() / (double) RAND_MAX)*i);
502 if(tree[rnd_index1].getParent() == -1){escape = 1;}
507 rnd_index2 = (int)(((double)rand() / (double) RAND_MAX)*i);
508 if(rnd_index2 != rnd_index1 && tree[rnd_index2].getParent() == -1){
513 tree[i].setChildren(rnd_index1,rnd_index2);
514 tree[i].setParent(-1);
515 tree[rnd_index1].setParent(i);
516 tree[rnd_index2].setParent(i);
519 catch(exception& e) {
520 m->errorOut(e, "Tree", "randomTopology");
524 /*****************************************************************/
525 void Tree::print(ostream& out) {
527 int root = findRoot();
528 printBranch(root, out, "branch");
531 catch(exception& e) {
532 m->errorOut(e, "Tree", "print");
536 /*****************************************************************/
537 void Tree::printForBoot(ostream& out) {
539 int root = findRoot();
540 printBranch(root, out, "boot");
543 catch(exception& e) {
544 m->errorOut(e, "Tree", "printForBoot");
549 /*****************************************************************/
550 // This prints out the tree in Newick form.
551 void Tree::createNewickFile(string f) {
553 int root = findRoot();
554 //filename = m->getRootName(globaldata->getTreeFile()) + "newick";
557 m->openOutputFile(filename, out);
559 printBranch(root, out, "branch");
561 // you are at the end of the tree
565 catch(exception& e) {
566 m->errorOut(e, "Tree", "createNewickFile");
571 /*****************************************************************/
572 //This function finds the index of the root node.
574 int Tree::findRoot() {
576 for (int i = 0; i < numNodes; i++) {
578 if (tree[i].getParent() == -1) { return i; }
579 //cout << "i = " << i << endl;
580 //cout << "i's parent = " << tree[i].getParent() << endl;
584 catch(exception& e) {
585 m->errorOut(e, "Tree", "findRoot");
590 /*****************************************************************/
591 void Tree::printBranch(int node, ostream& out, string mode) {
594 // you are not a leaf
595 if (tree[node].getLChild() != -1) {
597 printBranch(tree[node].getLChild(), out, mode);
599 printBranch(tree[node].getRChild(), out, mode);
601 if (mode == "branch") {
602 //if there is a branch length then print it
603 if (tree[node].getBranchLength() != -1) {
604 out << ":" << tree[node].getBranchLength();
606 }else if (mode == "boot") {
607 //if there is a label then print it
608 if (tree[node].getLabel() != -1) {
609 out << tree[node].getLabel();
612 }else { //you are a leaf
613 string leafGroup = globaldata->gTreemap->getGroup(tree[node].getName());
616 if (mode == "branch") {
617 //if there is a branch length then print it
618 if (tree[node].getBranchLength() != -1) {
619 out << ":" << tree[node].getBranchLength();
621 }else if (mode == "boot") {
622 //if there is a label then print it
623 if (tree[node].getLabel() != -1) {
624 out << tree[node].getLabel();
630 catch(exception& e) {
631 m->errorOut(e, "Tree", "printBranch");
636 /*****************************************************************/
638 void Tree::printTree() {
640 for(int i=0;i<numNodes;i++){
647 /*****************************************************************/
648 //this code is a mess and should be rethought...-slw
649 void Tree::parseTreeFile() {
651 //only takes names from the first tree and assumes that all trees use the same names.
653 string filename = globaldata->getTreeFile();
655 m->openInputFile(filename, filehandle);
660 //ifyou are not a nexus file
661 if((c = filehandle.peek()) != '#') {
662 while((c = filehandle.peek()) != ';') {
663 while ((c = filehandle.peek()) != ';') {
671 if((c == '(') && (comment != 1)){ break; }
675 done = readTreeString(filehandle);
676 if (done == 0) { break; }
678 //ifyou are a nexus file
679 }else if((c = filehandle.peek()) == '#') {
683 while(holder != "translate" && holder != "Translate"){
684 if(holder == "[" || holder == "[!"){
690 filehandle >> holder;
692 //if there is no translate then you must read tree string otherwise use translate to get names
693 if((holder == "tree") && (comment != 1)){
694 //pass over the "tree rep.6878900 = "
695 while (((c = filehandle.get()) != '(') && ((c = filehandle.peek()) != EOF)) {;}
697 if(c == EOF) { break; }
698 filehandle.putback(c); //put back first ( of tree.
699 done = readTreeString(filehandle);
704 if (done == 0) { break; }
707 //use nexus translation rather than parsing tree to save time
708 if((holder == "translate") || (holder == "Translate")) {
710 string number, name, h;
711 h = ""; // so it enters the loop the first time
712 while((h != ";") && (number != ";")) {
713 filehandle >> number;
716 //c = , until done with translation then c = ;
717 h = name.substr(name.length()-1, name.length());
718 name.erase(name.end()-1); //erase the comma
719 globaldata->Treenames.push_back(number);
721 if(number == ";") { globaldata->Treenames.pop_back(); } //in case ';' from translation is on next line instead of next to last name
726 //for (int i = 0; i < globaldata->Treenames.size(); i++) {
727 //cout << globaldata->Treenames[i] << endl; }
728 //cout << globaldata->Treenames.size() << endl;
730 catch(exception& e) {
731 m->errorOut(e, "Tree", "parseTreeFile");
735 /*******************************************************/
737 /*******************************************************/
738 int Tree::readTreeString(ifstream& filehandle) {
743 while((c = filehandle.peek()) != ';') {
745 //cout << " at beginning of while " << k << endl;
747 //to pass over labels in trees
749 while((c!=',') && (c != -1) && (c!= ':') && (c!=';')){ c=filehandle.get(); }
750 filehandle.putback(c);
752 if(c == ';') { return 0; }
753 if(c == -1) { return 0; }
755 if((c != '(') && (c != ')') && (c != ',') && (c != ':') && (c != '\n') && (c != '\t') && (c != 32)) { //32 is space
757 c = filehandle.get();
760 while ((c != '(') && (c != ')') && (c != ',') && (c != ':') && (c != '\n') && (c != 32) && (c != '\t')) {
762 c = filehandle.get();
764 //cout << " in name while " << k << endl;
767 //cout << "name = " << name << endl;
768 globaldata->Treenames.push_back(name);
769 filehandle.putback(c);
771 //cout << " after putback" << k << endl;
774 if(c == ':') { //read until you reach the end of the branch length
775 while ((c != '(') && (c != ')') && (c != ',') && (c != ';') && (c != '\n') && (c != '\t') && (c != 32)) {
776 c = filehandle.get();
778 //cout << " in branch while " << k << endl;
780 filehandle.putback(c);
783 c = filehandle.get();
785 //cout << " here after get " << k << endl;
786 if(c == ';') { return 0; }
787 if(c == ')') { filehandle.putback(c); }
794 catch(exception& e) {
795 m->errorOut(e, "Tree", "readTreeString");
800 /*******************************************************/
802 /*******************************************************/