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
72 for (int i = 0; i < numLeaves; i++) {
73 string name = tree[i].getName();
75 map<string, string>::iterator itNames = globaldata->names.find(name);
77 if (itNames == globaldata->names.end()) { m->mothurOut(name + " is not in your name file, please correct."); m->mothurOutEndLine(); exit(1); }
79 vector<string> dupNames;
80 splitAtComma(globaldata->names[name], dupNames);
82 map<string, int>::iterator itCounts;
84 for (int j = 0; j < dupNames.size(); j++) {
86 if (dupNames[j] != name) {//you already added yourself in the constructor
87 string group = globaldata->gTreemap->getGroup(dupNames[j]);
90 itCounts = tree[i].pcount.find(group);
91 if (itCounts == tree[i].pcount.end()) { //new group, add it
92 tree[i].pcount[group] = 1;
94 tree[i].pcount[group]++;
98 itCounts = tree[i].pGroups.find(group);
99 if (itCounts == tree[i].pGroups.end()) { //new group, add it
100 tree[i].pGroups[group] = 1;
102 tree[i].pGroups[group]++;
106 if(tree[i].pGroups[group] > maxPars){
107 maxPars = tree[i].pGroups[group];
112 if (maxPars > 1) { //then we have some more dominant groups
113 //erase all the groups that are less than maxPars because you found a more dominant group.
114 for(it=tree[i].pGroups.begin();it!=tree[i].pGroups.end();){
115 if(it->second < maxPars){
116 tree[i].pGroups.erase(it++);
119 //set one remaining groups to 1
120 for(it=tree[i].pGroups.begin();it!=tree[i].pGroups.end();it++){
121 tree[i].pGroups[it->first] = 1;
125 //update groups to reflect all the groups this node represents
126 vector<string> nodeGroups;
127 map<string, int>::iterator itGroups;
128 for (itGroups = tree[i].pcount.begin(); itGroups != tree[i].pcount.end(); itGroups++) {
129 nodeGroups.push_back(itGroups->first);
131 tree[i].setGroup(nodeGroups);
137 catch(exception& e) {
138 m->errorOut(e, "Tree", "addNamesToCounts");
142 /*****************************************************************/
143 int Tree::getIndex(string searchName) {
145 //Treemap knows name, group and index to speed up search
146 // getIndex function will return the vector index or -1 if seq is not found.
147 int index = globaldata->gTreemap->getIndex(searchName);
151 catch(exception& e) {
152 m->errorOut(e, "Tree", "getIndex");
156 /*****************************************************************/
158 void Tree::setIndex(string searchName, int index) {
160 //set index in treemap
161 globaldata->gTreemap->setIndex(searchName, index);
163 catch(exception& e) {
164 m->errorOut(e, "Tree", "setIndex");
168 /*****************************************************************/
169 void Tree::assembleTree() {
172 //if user has given a names file we want to include that info in the pgroups and pcount info.
173 if(globaldata->names.size() != 0) { addNamesToCounts(); }
175 //build the pGroups in non leaf nodes to be used in the parsimony calcs.
176 for (int i = numLeaves; i < numNodes; i++) {
177 tree[i].pGroups = (mergeGroups(i));
178 tree[i].pcount = (mergeGcounts(i));
181 catch(exception& e) {
182 m->errorOut(e, "Tree", "assembleTree");
186 /*****************************************************************/
187 void Tree::getCopy(Tree* copy) {
190 //for each node in the tree copy its info
191 for (int i = 0; i < numNodes; i++) {
193 tree[i].setName(copy->tree[i].getName());
196 tree[i].setGroup(copy->tree[i].getGroup());
199 tree[i].setBranchLength(copy->tree[i].getBranchLength());
202 tree[i].setParent(copy->tree[i].getParent());
205 tree[i].setChildren(copy->tree[i].getLChild(), copy->tree[i].getRChild());
207 //copy index in node and tmap
208 tree[i].setIndex(copy->tree[i].getIndex());
209 setIndex(copy->tree[i].getName(), getIndex(copy->tree[i].getName()));
212 tree[i].pGroups = copy->tree[i].pGroups;
215 tree[i].pcount = copy->tree[i].pcount;
219 catch(exception& e) {
220 m->errorOut(e, "Tree", "getCopy");
224 /*****************************************************************/
225 //returns a map with a groupname and the number of times that group was seen in the children
226 //for instance if your children are white and black then it would return a map with 2 entries
227 // p[white] = 1 and p[black] = 1. Now go up a level and merge that with a node who has p[white] = 1
228 //and you get p[white] = 2, p[black] = 1, but you erase the p[black] because you have a p value higher than 1.
230 map<string, int> Tree::mergeGroups(int i) {
232 int lc = tree[i].getLChild();
233 int rc = tree[i].getRChild();
235 //set parsimony groups to left child
236 map<string,int> parsimony = tree[lc].pGroups;
240 //look at right child groups and update maxPars if right child has something higher for that group.
241 for(it=tree[rc].pGroups.begin();it!=tree[rc].pGroups.end();it++){
242 it2 = parsimony.find(it->first);
243 if (it2 != parsimony.end()) {
244 parsimony[it->first]++;
246 parsimony[it->first] = 1;
249 if(parsimony[it->first] > maxPars){
250 maxPars = parsimony[it->first];
254 // this is true if right child had a greater parsimony for a certain group
256 //erase all the groups that are only 1 because you found something with 2.
257 for(it=parsimony.begin();it!=parsimony.end();){
259 parsimony.erase(it++);
262 //set one remaining groups to 1
263 //so with our above example p[white] = 2 would be left and it would become p[white] = 1
264 for(it=parsimony.begin();it!=parsimony.end();it++){
265 parsimony[it->first] = 1;
272 catch(exception& e) {
273 m->errorOut(e, "Tree", "mergeGroups");
277 /*****************************************************************/
278 //returns a map with a groupname and the number of times that group was seen in the children
279 //for instance if your children are white and black then it would return a map with 2 entries
280 // p[white] = 1 and p[black] = 1. Now go up a level and merge that with a node who has p[white] = 1
281 //and you get p[white] = 2, p[black] = 1, but you erase the p[black] because you have a p value higher than 1.
283 map<string, int> Tree::mergeUserGroups(int i, vector<string> g) {
286 int lc = tree[i].getLChild();
287 int rc = tree[i].getRChild();
289 //loop through nodes groups removing the ones the user doesn't want
290 for(it=tree[lc].pGroups.begin();it!=tree[lc].pGroups.end();){
291 if (inUsersGroups(it->first, g) != true) {
292 tree[lc].pGroups.erase(it++);
296 //loop through nodes groups removing the ones the user doesn't want
297 for(it=tree[rc].pGroups.begin();it!=tree[rc].pGroups.end();){
298 if (inUsersGroups(it->first, g) != true) {
299 tree[rc].pGroups.erase(it++);
303 //set parsimony groups to left child
304 map<string,int> parsimony = tree[lc].pGroups;
308 //look at right child groups and update maxPars if right child has something higher for that group.
309 for(it=tree[rc].pGroups.begin();it!=tree[rc].pGroups.end();it++){
310 it2 = parsimony.find(it->first);
311 if (it2 != parsimony.end()) {
312 parsimony[it->first]++;
314 parsimony[it->first] = 1;
317 if(parsimony[it->first] > maxPars){
318 maxPars = parsimony[it->first];
322 // this is true if right child had a greater parsimony for a certain group
324 //erase all the groups that are only 1 because you found something with 2.
325 for(it=parsimony.begin();it!=parsimony.end();){
327 parsimony.erase(it++);
331 for(it=parsimony.begin();it!=parsimony.end();it++){
332 parsimony[it->first] = 1;
338 catch(exception& e) {
339 m->errorOut(e, "Tree", "mergeUserGroups");
345 /**************************************************************************************************/
347 map<string,int> Tree::mergeGcounts(int position) {
349 map<string,int>::iterator pos;
351 int lc = tree[position].getLChild();
352 int rc = tree[position].getRChild();
354 map<string,int> sum = tree[lc].pcount;
356 for(it=tree[rc].pcount.begin();it!=tree[rc].pcount.end();it++){
357 sum[it->first] += it->second;
361 catch(exception& e) {
362 m->errorOut(e, "Tree", "mergeGcounts");
366 /**************************************************************************************************/
368 void Tree::randomLabels(vector<string> g) {
371 for(int i = 0; i < numLeaves; i++){
373 //get random index to switch with
374 z = int((float)(i+1) * (float)(rand()) / ((float)RAND_MAX+1.0));
376 //you only want to randomize the nodes that are from a group the user wants analyzed, so
377 //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.
380 treez = inUsersGroups(tree[z].getGroup(), g);
381 treei = inUsersGroups(tree[i].getGroup(), g);
383 if ((treez == true) && (treei == true)) {
384 //switches node i and node z's info.
385 map<string,int> lib_hold = tree[z].pGroups;
386 tree[z].pGroups = (tree[i].pGroups);
387 tree[i].pGroups = (lib_hold);
389 vector<string> zgroup = tree[z].getGroup();
390 tree[z].setGroup(tree[i].getGroup());
391 tree[i].setGroup(zgroup);
393 string zname = tree[z].getName();
394 tree[z].setName(tree[i].getName());
395 tree[i].setName(zname);
397 map<string,int> gcount_hold = tree[z].pcount;
398 tree[z].pcount = (tree[i].pcount);
399 tree[i].pcount = (gcount_hold);
403 catch(exception& e) {
404 m->errorOut(e, "Tree", "randomLabels");
408 /**************************************************************************************************
410 void Tree::randomLabels(string groupA, string groupB) {
412 int numSeqsA = globaldata->gTreemap->seqsPerGroup[groupA];
413 int numSeqsB = globaldata->gTreemap->seqsPerGroup[groupB];
415 vector<string> randomGroups(numSeqsA+numSeqsB, groupA);
416 for(int i=numSeqsA;i<randomGroups.size();i++){
417 randomGroups[i] = groupB;
419 random_shuffle(randomGroups.begin(), randomGroups.end());
421 int randomCounter = 0;
422 for(int i=0;i<numLeaves;i++){
423 if(tree[i].getGroup() == groupA || tree[i].getGroup() == groupB){
424 tree[i].setGroup(randomGroups[randomCounter]);
425 tree[i].pcount.clear();
426 tree[i].pcount[randomGroups[randomCounter]] = 1;
427 tree[i].pGroups.clear();
428 tree[i].pGroups[randomGroups[randomCounter]] = 1;
433 catch(exception& e) {
434 m->errorOut(e, "Tree", "randomLabels");
438 /**************************************************************************************************/
439 void Tree::randomBlengths() {
441 for(int i=numNodes-1;i>=0;i--){
442 int z = int((float)(i+1) * (float)(rand()) / ((float)RAND_MAX+1.0));
444 float bl_hold = tree[z].getBranchLength();
445 tree[z].setBranchLength(tree[i].getBranchLength());
446 tree[i].setBranchLength(bl_hold);
449 catch(exception& e) {
450 m->errorOut(e, "Tree", "randomBlengths");
454 /*************************************************************************************************/
455 void Tree::assembleRandomUnifracTree(vector<string> g) {
459 /*************************************************************************************************/
460 void Tree::assembleRandomUnifracTree(string groupA, string groupB) {
462 vector<string> temp; temp.push_back(groupA); temp.push_back(groupB);
467 /*************************************************************************************************/
468 //for now it's just random topology but may become random labels as well later that why this is such a simple function now...
469 void Tree::assembleRandomTree() {
473 /**************************************************************************************************/
475 void Tree::randomTopology() {
477 for(int i=0;i<numNodes;i++){
478 tree[i].setParent(-1);
480 for(int i=numLeaves;i<numNodes;i++){
481 tree[i].setChildren(-1, -1);
484 for(int i=numLeaves;i<numNodes;i++){
486 int rnd_index1, rnd_index2;
488 rnd_index1 = (int)(((double)rand() / (double) RAND_MAX)*i);
489 if(tree[rnd_index1].getParent() == -1){escape = 1;}
494 rnd_index2 = (int)(((double)rand() / (double) RAND_MAX)*i);
495 if(rnd_index2 != rnd_index1 && tree[rnd_index2].getParent() == -1){
500 tree[i].setChildren(rnd_index1,rnd_index2);
501 tree[i].setParent(-1);
502 tree[rnd_index1].setParent(i);
503 tree[rnd_index2].setParent(i);
506 catch(exception& e) {
507 m->errorOut(e, "Tree", "randomTopology");
511 /*****************************************************************/
512 void Tree::print(ostream& out) {
514 int root = findRoot();
515 printBranch(root, out, "branch");
518 catch(exception& e) {
519 m->errorOut(e, "Tree", "print");
523 /*****************************************************************/
524 void Tree::printForBoot(ostream& out) {
526 int root = findRoot();
527 printBranch(root, out, "boot");
530 catch(exception& e) {
531 m->errorOut(e, "Tree", "printForBoot");
536 /*****************************************************************/
537 // This prints out the tree in Newick form.
538 void Tree::createNewickFile(string f) {
540 int root = findRoot();
541 //filename = getRootName(globaldata->getTreeFile()) + "newick";
544 openOutputFile(filename, out);
546 printBranch(root, out, "branch");
548 // you are at the end of the tree
552 catch(exception& e) {
553 m->errorOut(e, "Tree", "createNewickFile");
558 /*****************************************************************/
559 //This function finds the index of the root node.
561 int Tree::findRoot() {
563 for (int i = 0; i < numNodes; i++) {
565 if (tree[i].getParent() == -1) { return i; }
566 //cout << "i = " << i << endl;
567 //cout << "i's parent = " << tree[i].getParent() << endl;
571 catch(exception& e) {
572 m->errorOut(e, "Tree", "findRoot");
577 /*****************************************************************/
578 void Tree::printBranch(int node, ostream& out, string mode) {
581 // you are not a leaf
582 if (tree[node].getLChild() != -1) {
584 printBranch(tree[node].getLChild(), out, mode);
586 printBranch(tree[node].getRChild(), out, mode);
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();
599 }else { //you are a leaf
600 string leafGroup = globaldata->gTreemap->getGroup(tree[node].getName());
603 if (mode == "branch") {
604 //if there is a branch length then print it
605 if (tree[node].getBranchLength() != -1) {
606 out << ":" << tree[node].getBranchLength();
608 }else if (mode == "boot") {
609 //if there is a label then print it
610 if (tree[node].getLabel() != -1) {
611 out << tree[node].getLabel();
617 catch(exception& e) {
618 m->errorOut(e, "Tree", "printBranch");
623 /*****************************************************************/
625 void Tree::printTree() {
627 for(int i=0;i<numNodes;i++){
634 /*****************************************************************/
635 //this code is a mess and should be rethought...-slw
636 void Tree::parseTreeFile() {
638 //only takes names from the first tree and assumes that all trees use the same names.
640 string filename = globaldata->getTreeFile();
642 openInputFile(filename, filehandle);
647 //ifyou are not a nexus file
648 if((c = filehandle.peek()) != '#') {
649 while((c = filehandle.peek()) != ';') {
650 while ((c = filehandle.peek()) != ';') {
658 if((c == '(') && (comment != 1)){ break; }
662 done = readTreeString(filehandle);
663 if (done == 0) { break; }
665 //ifyou are a nexus file
666 }else if((c = filehandle.peek()) == '#') {
670 while(holder != "translate" && holder != "Translate"){
671 if(holder == "[" || holder == "[!"){
677 filehandle >> holder;
679 //if there is no translate then you must read tree string otherwise use translate to get names
680 if((holder == "tree") && (comment != 1)){
681 //pass over the "tree rep.6878900 = "
682 while (((c = filehandle.get()) != '(') && ((c = filehandle.peek()) != EOF)) {;}
684 if(c == EOF) { break; }
685 filehandle.putback(c); //put back first ( of tree.
686 done = readTreeString(filehandle);
691 if (done == 0) { break; }
694 //use nexus translation rather than parsing tree to save time
695 if((holder == "translate") || (holder == "Translate")) {
697 string number, name, h;
698 h = ""; // so it enters the loop the first time
699 while((h != ";") && (number != ";")) {
700 filehandle >> number;
703 //c = , until done with translation then c = ;
704 h = name.substr(name.length()-1, name.length());
705 name.erase(name.end()-1); //erase the comma
706 globaldata->Treenames.push_back(number);
708 if(number == ";") { globaldata->Treenames.pop_back(); } //in case ';' from translation is on next line instead of next to last name
713 catch(exception& e) {
714 m->errorOut(e, "Tree", "parseTreeFile");
718 /*******************************************************/
720 /*******************************************************/
721 int Tree::readTreeString(ifstream& filehandle) {
726 while((c = filehandle.peek()) != ';') {
728 //cout << " at beginning of while " << k << endl;
730 //to pass over labels in trees
732 while((c!=',') && (c != -1) && (c!= ':') && (c!=';')){ c=filehandle.get(); }
733 filehandle.putback(c);
735 if(c == ';') { return 0; }
736 if(c == -1) { return 0; }
738 if((c != '(') && (c != ')') && (c != ',') && (c != ':') && (c != '\n') && (c != '\t') && (c != 32)) { //32 is space
740 c = filehandle.get();
743 while ((c != '(') && (c != ')') && (c != ',') && (c != ':') && (c != '\n') && (c != 32) && (c != '\t')) {
745 c = filehandle.get();
747 //cout << " in name while " << k << endl;
750 //cout << "name = " << name << endl;
751 globaldata->Treenames.push_back(name);
752 filehandle.putback(c);
754 //cout << " after putback" << k << endl;
757 if(c == ':') { //read until you reach the end of the branch length
758 while ((c != '(') && (c != ')') && (c != ',') && (c != ';') && (c != '\n') && (c != '\t') && (c != 32)) {
759 c = filehandle.get();
761 //cout << " in branch while " << k << endl;
763 filehandle.putback(c);
766 c = filehandle.get();
768 //cout << " here after get " << k << endl;
769 if(c == ';') { return 0; }
770 if(c == ')') { filehandle.putback(c); }
777 catch(exception& e) {
778 m->errorOut(e, "Tree", "readTreeString");
783 /*******************************************************/
785 /*******************************************************/