5 * Created by Sarah Westcott on 1/22/09.
6 * Copyright 2009 Schloss Lab UMASS Amherst. All rights reserved.
12 /*****************************************************************/
13 Tree::Tree(int num, CountTable* t) : ct(t) {
15 m = MothurOut::getInstance();
18 numNodes = 2*numLeaves - 1;
20 tree.resize(numNodes);
23 m->errorOut(e, "Tree", "Tree - numNodes");
27 /*****************************************************************/
28 Tree::Tree(string g) { //do not use tree generated by this its just to extract the treenames, its a chicken before the egg thing that needs to be revisited.
30 m = MothurOut::getInstance();
31 parseTreeFile(); m->runParse = false;
34 m->errorOut(e, "Tree", "Tree - just parse");
38 /*****************************************************************/
39 Tree::Tree(CountTable* t) : ct(t) {
41 m = MothurOut::getInstance();
43 if (m->runParse == true) { parseTreeFile(); m->runParse = false; }
45 numLeaves = m->Treenames.size();
46 numNodes = 2*numLeaves - 1;
48 tree.resize(numNodes);
50 //initialize groupNodeInfo
51 vector<string> namesOfGroups = ct->getNamesOfGroups();
52 for (int i = 0; i < namesOfGroups.size(); i++) { groupNodeInfo[namesOfGroups[i]].resize(0); }
54 //initialize tree with correct number of nodes, name and group info.
55 for (int i = 0; i < numNodes; i++) {
56 //initialize leaf nodes
57 if (i <= (numLeaves-1)) {
58 tree[i].setName(m->Treenames[i]);
63 vector<int> counts = ct->getGroupCounts(m->Treenames[i]);
64 for (int j = 0; j < namesOfGroups.size(); j++) {
65 if (counts[j] != 0) { //you have seqs from this group
66 groupNodeInfo[namesOfGroups[j]].push_back(i);
67 group.push_back(namesOfGroups[j]);
68 tree[i].pGroups[namesOfGroups[j]] = counts[j];
69 tree[i].pcount[namesOfGroups[j]] = counts[j];
71 if(counts[j] > maxPars){ maxPars = counts[j]; }
74 tree[i].setGroup(group);
75 setIndex(m->Treenames[i], i);
77 if (maxPars > 1) { //then we have some more dominant groups
78 //erase all the groups that are less than maxPars because you found a more dominant group.
79 for(it=tree[i].pGroups.begin();it!=tree[i].pGroups.end();){
80 if(it->second < maxPars){
81 tree[i].pGroups.erase(it++);
84 //set one remaining groups to 1
85 for(it=tree[i].pGroups.begin();it!=tree[i].pGroups.end();it++){
86 tree[i].pGroups[it->first] = 1;
90 //intialize non leaf nodes
91 }else if (i > (numLeaves-1)) {
93 vector<string> tempGroups;
94 tree[i].setGroup(tempGroups);
100 m->errorOut(e, "Tree", "Tree");
104 /*****************************************************************/
105 Tree::Tree(CountTable* t, vector< vector<double> >& sims) : ct(t) {
107 m = MothurOut::getInstance();
109 if (m->runParse == true) { parseTreeFile(); m->runParse = false; }
110 numLeaves = m->Treenames.size();
111 numNodes = 2*numLeaves - 1;
113 tree.resize(numNodes);
115 //initialize groupNodeInfo
116 vector<string> namesOfGroups = ct->getNamesOfGroups();
117 for (int i = 0; i < namesOfGroups.size(); i++) { groupNodeInfo[namesOfGroups[i]].resize(0); }
119 //initialize tree with correct number of nodes, name and group info.
120 for (int i = 0; i < numNodes; i++) {
121 //initialize leaf nodes
122 if (i <= (numLeaves-1)) {
123 tree[i].setName(m->Treenames[i]);
127 vector<string> group;
128 vector<int> counts = ct->getGroupCounts(m->Treenames[i]);
129 for (int j = 0; j < namesOfGroups.size(); j++) {
130 if (counts[j] != 0) { //you have seqs from this group
131 groupNodeInfo[namesOfGroups[j]].push_back(i);
132 group.push_back(namesOfGroups[j]);
133 tree[i].pGroups[namesOfGroups[j]] = counts[j];
134 tree[i].pcount[namesOfGroups[j]] = counts[j];
136 if(counts[j] > maxPars){ maxPars = counts[j]; }
139 tree[i].setGroup(group);
140 setIndex(m->Treenames[i], i);
142 if (maxPars > 1) { //then we have some more dominant groups
143 //erase all the groups that are less than maxPars because you found a more dominant group.
144 for(it=tree[i].pGroups.begin();it!=tree[i].pGroups.end();){
145 if(it->second < maxPars){
146 tree[i].pGroups.erase(it++);
149 //set one remaining groups to 1
150 for(it=tree[i].pGroups.begin();it!=tree[i].pGroups.end();it++){
151 tree[i].pGroups[it->first] = 1;
155 //intialize non leaf nodes
156 }else if (i > (numLeaves-1)) {
158 vector<string> tempGroups;
159 tree[i].setGroup(tempGroups);
164 //build tree from matrix
166 map<int, int> thisIndexes; //maps row in simMatrix to vector index in the tree
167 for (int g = 0; g < numLeaves; g++) { thisIndexes[g] = g; }
169 //do merges and create tree structure by setting parents and children
170 //there are numGroups - 1 merges to do
171 for (int i = 0; i < (numLeaves - 1); i++) {
172 float largest = -1000.0;
174 if (m->control_pressed) { break; }
177 //find largest value in sims matrix by searching lower triangle
178 for (int j = 1; j < sims.size(); j++) {
179 for (int k = 0; k < j; k++) {
180 if (sims[j][k] > largest) { largest = sims[j][k]; row = j; column = k; }
184 //set non-leaf node info and update leaves to know their parents
186 tree[numLeaves + i].setChildren(thisIndexes[row], thisIndexes[column]);
189 tree[thisIndexes[row]].setParent(numLeaves + i);
190 tree[thisIndexes[column]].setParent(numLeaves + i);
192 //blength = distance / 2;
193 float blength = ((1.0 - largest) / 2);
196 tree[thisIndexes[row]].setBranchLength(blength - tree[thisIndexes[row]].getLengthToLeaves());
197 tree[thisIndexes[column]].setBranchLength(blength - tree[thisIndexes[column]].getLengthToLeaves());
199 //set your length to leaves to your childs length plus branchlength
200 tree[numLeaves + i].setLengthToLeaves(tree[thisIndexes[row]].getLengthToLeaves() + tree[thisIndexes[row]].getBranchLength());
204 thisIndexes[row] = numLeaves+i;
205 thisIndexes[column] = numLeaves+i;
207 //remove highest value that caused the merge.
208 sims[row][column] = -1000.0;
209 sims[column][row] = -1000.0;
211 //merge values in simsMatrix
212 for (int n = 0; n < sims.size(); n++) {
213 //row becomes merge of 2 groups
214 sims[row][n] = (sims[row][n] + sims[column][n]) / 2;
215 sims[n][row] = sims[row][n];
217 sims[column][n] = -1000.0;
218 sims[n][column] = -1000.0;
222 //adjust tree to make sure root to tip length is .5
223 int root = findRoot();
224 tree[root].setBranchLength((0.5 - tree[root].getLengthToLeaves()));
227 catch(exception& e) {
228 m->errorOut(e, "Tree", "Tree");
232 /*****************************************************************/
234 /*****************************************************************
235 void Tree::addNamesToCounts(map<string, string> nameMap) {
237 //ex. seq1 seq2,seq3,se4
243 //before this function seq1.pcount = pasture -> 1
244 //after seq1.pcount = pasture -> 2, forest -> 1, ocean -> 1
246 //before this function seq1.pgroups = pasture -> 1
247 //after seq1.pgroups = pasture -> 1 since that is the dominant group
250 //go through each leaf and update its pcounts and pgroups
254 for (int i = 0; i < numLeaves; i++) {
256 string name = tree[i].getName();
258 map<string, string>::iterator itNames = nameMap.find(name);
260 if (itNames == nameMap.end()) { m->mothurOut(name + " is not in your name file, please correct."); m->mothurOutEndLine(); exit(1); }
262 vector<string> dupNames;
263 m->splitAtComma(nameMap[name], dupNames);
265 map<string, int>::iterator itCounts;
267 set<string> groupsAddedForThisNode;
268 for (int j = 0; j < dupNames.size(); j++) {
270 string group = tmap->getGroup(dupNames[j]);
272 if (dupNames[j] != name) {//you already added yourself in the constructor
274 if (groupsAddedForThisNode.count(group) == 0) { groupNodeInfo[group].push_back(i); groupsAddedForThisNode.insert(group); } //if you have not already added this node for this group, then add it
277 itCounts = tree[i].pcount.find(group);
278 if (itCounts == tree[i].pcount.end()) { //new group, add it
279 tree[i].pcount[group] = 1;
281 tree[i].pcount[group]++;
285 itCounts = tree[i].pGroups.find(group);
286 if (itCounts == tree[i].pGroups.end()) { //new group, add it
287 tree[i].pGroups[group] = 1;
289 tree[i].pGroups[group]++;
293 if(tree[i].pGroups[group] > maxPars){
294 maxPars = tree[i].pGroups[group];
296 }else { groupsAddedForThisNode.insert(group); } //add it so you don't add it to groupNodeInfo again
299 if (maxPars > 1) { //then we have some more dominant groups
300 //erase all the groups that are less than maxPars because you found a more dominant group.
301 for(it=tree[i].pGroups.begin();it!=tree[i].pGroups.end();){
302 if(it->second < maxPars){
303 tree[i].pGroups.erase(it++);
306 //set one remaining groups to 1
307 for(it=tree[i].pGroups.begin();it!=tree[i].pGroups.end();it++){
308 tree[i].pGroups[it->first] = 1;
312 //update groups to reflect all the groups this node represents
313 vector<string> nodeGroups;
314 map<string, int>::iterator itGroups;
315 for (itGroups = tree[i].pcount.begin(); itGroups != tree[i].pcount.end(); itGroups++) {
316 nodeGroups.push_back(itGroups->first);
318 tree[i].setGroup(nodeGroups);
324 //cout << "addNamesToCounts\t" << (B - A) / CLOCKS_PER_SEC << endl;
327 catch(exception& e) {
328 m->errorOut(e, "Tree", "addNamesToCounts");
332 /*****************************************************************/
333 int Tree::getIndex(string searchName) {
335 map<string, int>::iterator itIndex = indexes.find(searchName);
336 if (itIndex != indexes.end()) {
337 return itIndex->second;
341 catch(exception& e) {
342 m->errorOut(e, "Tree", "getIndex");
346 /*****************************************************************/
348 void Tree::setIndex(string searchName, int index) {
350 map<string, int>::iterator itIndex = indexes.find(searchName);
351 if (itIndex == indexes.end()) {
352 indexes[searchName] = index;
355 catch(exception& e) {
356 m->errorOut(e, "Tree", "setIndex");
360 /*****************************************************************/
361 int Tree::assembleTree() {
363 //build the pGroups in non leaf nodes to be used in the parsimony calcs.
364 for (int i = numLeaves; i < numNodes; i++) {
365 if (m->control_pressed) { return 1; }
367 tree[i].pGroups = (mergeGroups(i));
368 tree[i].pcount = (mergeGcounts(i));
373 catch(exception& e) {
374 m->errorOut(e, "Tree", "assembleTree");
378 /*****************************************************************/
379 //assumes leaf node names are in groups and no names file - used by indicator command
380 void Tree::getSubTree(Tree* Ctree, vector<string> Groups) {
383 //copy Tree since we are going to destroy it
384 Tree* copy = new Tree(ct);
385 copy->getCopy(Ctree);
386 copy->assembleTree();
388 //we want to select some of the leaf nodes to create the output tree
389 //go through the input Tree starting at parents of leaves
390 //initialize groupNodeInfo
391 vector<string> namesOfGroups = ct->getNamesOfGroups();
392 for (int i = 0; i < namesOfGroups.size(); i++) { groupNodeInfo[namesOfGroups[i]].resize(0); }
394 //initialize tree with correct number of nodes, name and group info.
395 for (int i = 0; i < numNodes; i++) {
396 //initialize leaf nodes
397 if (i <= (numLeaves-1)) {
398 tree[i].setName(Groups[i]);
402 vector<string> group;
403 vector<int> counts = ct->getGroupCounts(Groups[i]);
404 for (int j = 0; j < namesOfGroups.size(); j++) {
405 if (counts[j] != 0) { //you have seqs from this group
406 groupNodeInfo[namesOfGroups[j]].push_back(i);
407 group.push_back(namesOfGroups[j]);
408 tree[i].pGroups[namesOfGroups[j]] = counts[j];
409 tree[i].pcount[namesOfGroups[j]] = counts[j];
411 if(counts[j] > maxPars){ maxPars = counts[j]; }
414 tree[i].setGroup(group);
415 setIndex(Groups[i], i);
417 if (maxPars > 1) { //then we have some more dominant groups
418 //erase all the groups that are less than maxPars because you found a more dominant group.
419 for(it=tree[i].pGroups.begin();it!=tree[i].pGroups.end();){
420 if(it->second < maxPars){
421 tree[i].pGroups.erase(it++);
424 //set one remaining groups to 1
425 for(it=tree[i].pGroups.begin();it!=tree[i].pGroups.end();it++){
426 tree[i].pGroups[it->first] = 1;
430 //intialize non leaf nodes
431 }else if (i > (numLeaves-1)) {
433 vector<string> tempGroups;
434 tree[i].setGroup(tempGroups);
438 set<int> removedLeaves;
439 for (int i = 0; i < copy->getNumLeaves(); i++) {
441 if (removedLeaves.count(i) == 0) {
444 int parent = copy->tree[i].getParent();
448 if (m->inUsersGroups(copy->tree[i].getName(), Groups)) {
449 //find my siblings name
450 int parentRC = copy->tree[parent].getRChild();
451 int parentLC = copy->tree[parent].getLChild();
453 //if I am the right child, then my sib is the left child
454 int sibIndex = parentRC;
455 if (parentRC == i) { sibIndex = parentLC; }
457 string sibsName = copy->tree[sibIndex].getName();
459 //if yes, is my sibling
460 if ((m->inUsersGroups(sibsName, Groups)) || (sibsName == "")) {
461 //we both are okay no trimming required
463 //i am, my sib is not, so remove sib by setting my parent to my grandparent
464 int grandparent = copy->tree[parent].getParent();
465 int grandparentLC = copy->tree[grandparent].getLChild();
466 int grandparentRC = copy->tree[grandparent].getRChild();
468 //whichever of my granparents children was my parent now equals me
469 if (grandparentLC == parent) { grandparentLC = i; }
470 else { grandparentRC = i; }
472 copy->tree[i].setParent(grandparent);
473 copy->tree[i].setBranchLength((copy->tree[i].getBranchLength()+copy->tree[parent].getBranchLength()));
474 if (grandparent != -1) {
475 copy->tree[grandparent].setChildren(grandparentLC, grandparentRC);
477 removedLeaves.insert(sibIndex);
480 //find my siblings name
481 int parentRC = copy->tree[parent].getRChild();
482 int parentLC = copy->tree[parent].getLChild();
484 //if I am the right child, then my sib is the left child
485 int sibIndex = parentRC;
486 if (parentRC == i) { sibIndex = parentLC; }
488 string sibsName = copy->tree[sibIndex].getName();
490 //if no is my sibling
491 if ((m->inUsersGroups(sibsName, Groups)) || (sibsName == "")) {
492 //i am not, but my sib is
493 int grandparent = copy->tree[parent].getParent();
494 int grandparentLC = copy->tree[grandparent].getLChild();
495 int grandparentRC = copy->tree[grandparent].getRChild();
497 //whichever of my granparents children was my parent now equals my sib
498 if (grandparentLC == parent) { grandparentLC = sibIndex; }
499 else { grandparentRC = sibIndex; }
501 copy->tree[sibIndex].setParent(grandparent);
502 copy->tree[sibIndex].setBranchLength((copy->tree[sibIndex].getBranchLength()+copy->tree[parent].getBranchLength()));
503 if (grandparent != -1) {
504 copy->tree[grandparent].setChildren(grandparentLC, grandparentRC);
506 removedLeaves.insert(i);
508 //neither of us are, so we want to eliminate ourselves and our parent
509 //so set our parents sib to our great-grandparent
510 int parent = copy->tree[i].getParent();
511 int grandparent = copy->tree[parent].getParent();
513 if (grandparent != -1) {
514 int greatgrandparent = copy->tree[grandparent].getParent();
515 int greatgrandparentLC, greatgrandparentRC;
516 if (greatgrandparent != -1) {
517 greatgrandparentLC = copy->tree[greatgrandparent].getLChild();
518 greatgrandparentRC = copy->tree[greatgrandparent].getRChild();
521 int grandparentLC = copy->tree[grandparent].getLChild();
522 int grandparentRC = copy->tree[grandparent].getRChild();
524 parentsSibIndex = grandparentLC;
525 if (grandparentLC == parent) { parentsSibIndex = grandparentRC; }
527 //whichever of my greatgrandparents children was my grandparent
528 if (greatgrandparentLC == grandparent) { greatgrandparentLC = parentsSibIndex; }
529 else { greatgrandparentRC = parentsSibIndex; }
531 copy->tree[parentsSibIndex].setParent(greatgrandparent);
532 copy->tree[parentsSibIndex].setBranchLength((copy->tree[parentsSibIndex].getBranchLength()+copy->tree[grandparent].getBranchLength()));
533 if (greatgrandparent != -1) {
534 copy->tree[greatgrandparent].setChildren(greatgrandparentLC, greatgrandparentRC);
537 copy->tree[parent].setParent(-1);
538 //cout << "issues with making subtree" << endl;
540 removedLeaves.insert(sibIndex);
541 removedLeaves.insert(i);
549 for (int i = 0; i < copy->getNumNodes(); i++) {
551 if (copy->tree[i].getParent() == -1) { root = i; break; }
554 int nextSpot = numLeaves;
555 populateNewTree(copy->tree, root, nextSpot);
559 catch(exception& e) {
560 m->errorOut(e, "Tree", "getSubTree");
564 /*****************************************************************
565 //assumes nameMap contains unique names as key or is empty.
566 //assumes numLeaves defined in tree constructor equals size of seqsToInclude and seqsToInclude only contains unique seqs.
567 int Tree::getSubTree(Tree* copy, vector<string> seqsToInclude, map<string, string> nameMap) {
570 if (numLeaves != seqsToInclude.size()) { m->mothurOut("[ERROR]: numLeaves does not equal numUniques, cannot create subtree.\n"); m->control_pressed = true; return 0; }
572 getSubTree(copy, seqsToInclude);
573 if (nameMap.size() != 0) { addNamesToCounts(nameMap); }
575 //build the pGroups in non leaf nodes to be used in the parsimony calcs.
576 for (int i = numLeaves; i < numNodes; i++) {
577 if (m->control_pressed) { return 1; }
579 tree[i].pGroups = (mergeGroups(i));
580 tree[i].pcount = (mergeGcounts(i));
585 catch(exception& e) {
586 m->errorOut(e, "Tree", "getSubTree");
590 /*****************************************************************/
591 int Tree::populateNewTree(vector<Node>& oldtree, int node, int& index) {
594 if (oldtree[node].getLChild() != -1) {
595 int rc = populateNewTree(oldtree, oldtree[node].getLChild(), index);
596 int lc = populateNewTree(oldtree, oldtree[node].getRChild(), index);
598 tree[index].setChildren(lc, rc);
599 tree[rc].setParent(index);
600 tree[lc].setParent(index);
602 tree[index].setBranchLength(oldtree[node].getBranchLength());
603 tree[rc].setBranchLength(oldtree[oldtree[node].getLChild()].getBranchLength());
604 tree[lc].setBranchLength(oldtree[oldtree[node].getRChild()].getBranchLength());
607 }else { //you are a leaf
608 int indexInNewTree = getIndex(oldtree[node].getName());
609 return indexInNewTree;
612 catch(exception& e) {
613 m->errorOut(e, "Tree", "populateNewTree");
617 /*****************************************************************/
618 void Tree::getCopy(Tree* copy, bool subsample) {
621 //for each node in the tree copy its info
622 for (int i = 0; i < numNodes; i++) {
624 tree[i].setBranchLength(copy->tree[i].getBranchLength());
627 tree[i].setParent(copy->tree[i].getParent());
630 tree[i].setChildren(copy->tree[i].getLChild(), copy->tree[i].getRChild());
633 //build the pGroups in non leaf nodes to be used in the parsimony calcs.
634 for (int i = numLeaves; i < numNodes; i++) {
635 if (m->control_pressed) { break; }
637 tree[i].pGroups = (mergeGroups(i));
638 tree[i].pcount = (mergeGcounts(i));
641 catch(exception& e) {
642 m->errorOut(e, "Tree", "getCopy");
646 /*****************************************************************/
647 void Tree::getCopy(Tree* copy) {
650 //for each node in the tree copy its info
651 for (int i = 0; i < numNodes; i++) {
653 tree[i].setName(copy->tree[i].getName());
656 tree[i].setGroup(copy->tree[i].getGroup());
659 tree[i].setBranchLength(copy->tree[i].getBranchLength());
662 tree[i].setParent(copy->tree[i].getParent());
665 tree[i].setChildren(copy->tree[i].getLChild(), copy->tree[i].getRChild());
667 //copy index in node and tmap
668 setIndex(copy->tree[i].getName(), getIndex(copy->tree[i].getName()));
669 tree[i].setIndex(copy->tree[i].getIndex());
672 tree[i].pGroups = copy->tree[i].pGroups;
675 tree[i].pcount = copy->tree[i].pcount;
678 groupNodeInfo = copy->groupNodeInfo;
681 catch(exception& e) {
682 m->errorOut(e, "Tree", "getCopy");
686 /*****************************************************************/
687 //returns a map with a groupname and the number of times that group was seen in the children
688 //for instance if your children are white and black then it would return a map with 2 entries
689 // p[white] = 1 and p[black] = 1. Now go up a level and merge that with a node who has p[white] = 1
690 //and you get p[white] = 2, p[black] = 1, but you erase the p[black] because you have a p value higher than 1.
692 map<string, int> Tree::mergeGroups(int i) {
694 int lc = tree[i].getLChild();
695 int rc = tree[i].getRChild();
697 //set parsimony groups to left child
698 map<string,int> parsimony = tree[lc].pGroups;
702 //look at right child groups and update maxPars if right child has something higher for that group.
703 for(it=tree[rc].pGroups.begin();it!=tree[rc].pGroups.end();it++){
704 it2 = parsimony.find(it->first);
705 if (it2 != parsimony.end()) {
706 parsimony[it->first]++;
708 parsimony[it->first] = 1;
711 if(parsimony[it->first] > maxPars){
712 maxPars = parsimony[it->first];
716 // this is true if right child had a greater parsimony for a certain group
718 //erase all the groups that are only 1 because you found something with 2.
719 for(it=parsimony.begin();it!=parsimony.end();){
721 parsimony.erase(it++);
724 //set one remaining groups to 1
725 //so with our above example p[white] = 2 would be left and it would become p[white] = 1
726 for(it=parsimony.begin();it!=parsimony.end();it++){
727 parsimony[it->first] = 1;
734 catch(exception& e) {
735 m->errorOut(e, "Tree", "mergeGroups");
739 /*****************************************************************/
740 //returns a map with a groupname and the number of times that group was seen in the children
741 //for instance if your children are white and black then it would return a map with 2 entries
742 // p[white] = 1 and p[black] = 1. Now go up a level and merge that with a node who has p[white] = 1
743 //and you get p[white] = 2, p[black] = 1, but you erase the p[black] because you have a p value higher than 1.
745 map<string, int> Tree::mergeUserGroups(int i, vector<string> g) {
748 int lc = tree[i].getLChild();
749 int rc = tree[i].getRChild();
751 //loop through nodes groups removing the ones the user doesn't want
752 for(it=tree[lc].pGroups.begin();it!=tree[lc].pGroups.end();){
753 if (m->inUsersGroups(it->first, g) != true) {
754 tree[lc].pGroups.erase(it++);
758 //loop through nodes groups removing the ones the user doesn't want
759 for(it=tree[rc].pGroups.begin();it!=tree[rc].pGroups.end();){
760 if (m->inUsersGroups(it->first, g) != true) {
761 tree[rc].pGroups.erase(it++);
765 //set parsimony groups to left child
766 map<string,int> parsimony = tree[lc].pGroups;
770 //look at right child groups and update maxPars if right child has something higher for that group.
771 for(it=tree[rc].pGroups.begin();it!=tree[rc].pGroups.end();it++){
772 it2 = parsimony.find(it->first);
773 if (it2 != parsimony.end()) {
774 parsimony[it->first]++;
776 parsimony[it->first] = 1;
779 if(parsimony[it->first] > maxPars){
780 maxPars = parsimony[it->first];
784 // this is true if right child had a greater parsimony for a certain group
786 //erase all the groups that are only 1 because you found something with 2.
787 for(it=parsimony.begin();it!=parsimony.end();){
789 parsimony.erase(it++);
793 for(it=parsimony.begin();it!=parsimony.end();it++){
794 parsimony[it->first] = 1;
800 catch(exception& e) {
801 m->errorOut(e, "Tree", "mergeUserGroups");
807 /**************************************************************************************************/
809 map<string,int> Tree::mergeGcounts(int position) {
811 map<string,int>::iterator pos;
813 int lc = tree[position].getLChild();
814 int rc = tree[position].getRChild();
816 map<string,int> sum = tree[lc].pcount;
818 for(it=tree[rc].pcount.begin();it!=tree[rc].pcount.end();it++){
819 sum[it->first] += it->second;
823 catch(exception& e) {
824 m->errorOut(e, "Tree", "mergeGcounts");
828 /**************************************************************************************************/
829 void Tree::randomLabels(vector<string> g) {
832 //initialize groupNodeInfo
833 for (int i = 0; i < (ct->getNamesOfGroups()).size(); i++) {
834 groupNodeInfo[(ct->getNamesOfGroups())[i]].resize(0);
837 for(int i = 0; i < numLeaves; i++){
839 //get random index to switch with
840 z = int((float)(i+1) * (float)(rand()) / ((float)RAND_MAX+1.0));
842 //you only want to randomize the nodes that are from a group the user wants analyzed, so
843 //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.
846 treez = m->inUsersGroups(tree[z].getGroup(), g);
847 treei = m->inUsersGroups(tree[i].getGroup(), g);
849 if ((treez == true) && (treei == true)) {
850 //switches node i and node z's info.
851 map<string,int> lib_hold = tree[z].pGroups;
852 tree[z].pGroups = (tree[i].pGroups);
853 tree[i].pGroups = (lib_hold);
855 vector<string> zgroup = tree[z].getGroup();
856 tree[z].setGroup(tree[i].getGroup());
857 tree[i].setGroup(zgroup);
859 string zname = tree[z].getName();
860 tree[z].setName(tree[i].getName());
861 tree[i].setName(zname);
863 map<string,int> gcount_hold = tree[z].pcount;
864 tree[z].pcount = (tree[i].pcount);
865 tree[i].pcount = (gcount_hold);
868 for (int k = 0; k < (tree[i].getGroup()).size(); k++) { groupNodeInfo[(tree[i].getGroup())[k]].push_back(i); }
869 for (int k = 0; k < (tree[z].getGroup()).size(); k++) { groupNodeInfo[(tree[z].getGroup())[k]].push_back(z); }
872 catch(exception& e) {
873 m->errorOut(e, "Tree", "randomLabels");
877 /**************************************************************************************************/
878 void Tree::randomBlengths() {
880 for(int i=numNodes-1;i>=0;i--){
881 int z = int((float)(i+1) * (float)(rand()) / ((float)RAND_MAX+1.0));
883 float bl_hold = tree[z].getBranchLength();
884 tree[z].setBranchLength(tree[i].getBranchLength());
885 tree[i].setBranchLength(bl_hold);
888 catch(exception& e) {
889 m->errorOut(e, "Tree", "randomBlengths");
893 /*************************************************************************************************/
894 void Tree::assembleRandomUnifracTree(vector<string> g) {
898 /*************************************************************************************************/
899 void Tree::assembleRandomUnifracTree(string groupA, string groupB) {
900 vector<string> temp; temp.push_back(groupA); temp.push_back(groupB);
905 /*************************************************************************************************/
906 //for now it's just random topology but may become random labels as well later that why this is such a simple function now...
907 void Tree::assembleRandomTree() {
911 /**************************************************************************************************/
913 void Tree::randomTopology() {
915 for(int i=0;i<numNodes;i++){
916 tree[i].setParent(-1);
918 for(int i=numLeaves;i<numNodes;i++){
919 tree[i].setChildren(-1, -1);
922 for(int i=numLeaves;i<numNodes;i++){
924 int rnd_index1, rnd_index2;
926 rnd_index1 = (int)(((double)rand() / (double) RAND_MAX)*i);
927 if(tree[rnd_index1].getParent() == -1){escape = 1;}
932 rnd_index2 = (int)(((double)rand() / (double) RAND_MAX)*i);
933 if(rnd_index2 != rnd_index1 && tree[rnd_index2].getParent() == -1){
938 tree[i].setChildren(rnd_index1,rnd_index2);
939 tree[i].setParent(-1);
940 tree[rnd_index1].setParent(i);
941 tree[rnd_index2].setParent(i);
944 catch(exception& e) {
945 m->errorOut(e, "Tree", "randomTopology");
949 /*****************************************************************/
950 void Tree::print(ostream& out) {
952 int root = findRoot();
953 printBranch(root, out, "branch");
956 catch(exception& e) {
957 m->errorOut(e, "Tree", "print");
961 /*****************************************************************/
962 void Tree::print(ostream& out, map<string, string> nameMap) {
964 int root = findRoot();
965 printBranch(root, out, nameMap);
968 catch(exception& e) {
969 m->errorOut(e, "Tree", "print");
973 /*****************************************************************/
974 void Tree::print(ostream& out, string mode) {
976 int root = findRoot();
977 printBranch(root, out, mode);
980 catch(exception& e) {
981 m->errorOut(e, "Tree", "print");
985 /*****************************************************************/
986 // This prints out the tree in Newick form.
987 void Tree::createNewickFile(string f) {
989 int root = findRoot();
993 m->openOutputFile(filename, out);
995 printBranch(root, out, "branch");
997 // you are at the end of the tree
1001 catch(exception& e) {
1002 m->errorOut(e, "Tree", "createNewickFile");
1007 /*****************************************************************/
1008 //This function finds the index of the root node.
1010 int Tree::findRoot() {
1012 for (int i = 0; i < numNodes; i++) {
1013 //you found the root
1014 if (tree[i].getParent() == -1) { return i; }
1015 //cout << "i = " << i << endl;
1016 //cout << "i's parent = " << tree[i].getParent() << endl;
1020 catch(exception& e) {
1021 m->errorOut(e, "Tree", "findRoot");
1025 /*****************************************************************/
1026 void Tree::printBranch(int node, ostream& out, map<string, string> names) {
1029 // you are not a leaf
1030 if (tree[node].getLChild() != -1) {
1032 printBranch(tree[node].getLChild(), out, names);
1034 printBranch(tree[node].getRChild(), out, names);
1037 //if there is a branch length then print it
1038 if (tree[node].getBranchLength() != -1) {
1039 out << ":" << tree[node].getBranchLength();
1042 }else { //you are a leaf
1043 map<string, string>::iterator itNames = names.find(tree[node].getName());
1045 string outputString = "";
1046 if (itNames != names.end()) {
1048 vector<string> dupNames;
1049 m->splitAtComma((itNames->second), dupNames);
1051 if (dupNames.size() == 1) {
1052 outputString += tree[node].getName();
1053 if (tree[node].getBranchLength() != -1) {
1054 outputString += ":" + toString(tree[node].getBranchLength());
1057 outputString += "(";
1059 for (int u = 0; u < dupNames.size()-1; u++) {
1060 outputString += dupNames[u];
1062 if (tree[node].getBranchLength() != -1) {
1063 outputString += ":" + toString(0.0);
1065 outputString += ",";
1068 outputString += dupNames[dupNames.size()-1];
1069 if (tree[node].getBranchLength() != -1) {
1070 outputString += ":" + toString(0.0);
1073 outputString += ")";
1074 if (tree[node].getBranchLength() != -1) {
1075 outputString += ":" + toString(tree[node].getBranchLength());
1079 outputString = tree[node].getName();
1080 //if there is a branch length then print it
1081 if (tree[node].getBranchLength() != -1) {
1082 outputString += ":" + toString(tree[node].getBranchLength());
1085 m->mothurOut("[ERROR]: " + tree[node].getName() + " is not in your namefile, please correct."); m->mothurOutEndLine();
1088 out << outputString;
1092 catch(exception& e) {
1093 m->errorOut(e, "Tree", "printBranch");
1097 /*****************************************************************/
1098 void Tree::printBranch(int node, ostream& out, string mode) {
1101 // you are not a leaf
1102 if (tree[node].getLChild() != -1) {
1104 printBranch(tree[node].getLChild(), out, mode);
1106 printBranch(tree[node].getRChild(), out, mode);
1108 if (mode == "branch") {
1109 //if there is a branch length then print it
1110 if (tree[node].getBranchLength() != -1) {
1111 out << ":" << tree[node].getBranchLength();
1113 }else if (mode == "boot") {
1114 //if there is a label then print it
1115 if (tree[node].getLabel() != -1) {
1116 out << tree[node].getLabel();
1118 }else if (mode == "both") {
1119 if (tree[node].getLabel() != -1) {
1120 out << tree[node].getLabel();
1122 //if there is a branch length then print it
1123 if (tree[node].getBranchLength() != -1) {
1124 out << ":" << tree[node].getBranchLength();
1127 }else { //you are a leaf
1128 vector<string> leafGroup = ct->getGroups(tree[node].getName());
1130 if (mode == "branch") {
1131 out << leafGroup[0];
1132 //if there is a branch length then print it
1133 if (tree[node].getBranchLength() != -1) {
1134 out << ":" << tree[node].getBranchLength();
1136 }else if (mode == "boot") {
1137 out << leafGroup[0];
1138 //if there is a label then print it
1139 if (tree[node].getLabel() != -1) {
1140 out << tree[node].getLabel();
1142 }else if (mode == "both") {
1143 out << tree[node].getName();
1144 if (tree[node].getLabel() != -1) {
1145 out << tree[node].getLabel();
1147 //if there is a branch length then print it
1148 if (tree[node].getBranchLength() != -1) {
1149 out << ":" << tree[node].getBranchLength();
1155 catch(exception& e) {
1156 m->errorOut(e, "Tree", "printBranch");
1160 /*****************************************************************/
1161 void Tree::printBranch(int node, ostream& out, string mode, vector<Node>& theseNodes) {
1164 // you are not a leaf
1165 if (theseNodes[node].getLChild() != -1) {
1167 printBranch(theseNodes[node].getLChild(), out, mode);
1169 printBranch(theseNodes[node].getRChild(), out, mode);
1171 if (mode == "branch") {
1172 //if there is a branch length then print it
1173 if (theseNodes[node].getBranchLength() != -1) {
1174 out << ":" << theseNodes[node].getBranchLength();
1176 }else if (mode == "boot") {
1177 //if there is a label then print it
1178 if (theseNodes[node].getLabel() != -1) {
1179 out << theseNodes[node].getLabel();
1181 }else if (mode == "both") {
1182 if (theseNodes[node].getLabel() != -1) {
1183 out << theseNodes[node].getLabel();
1185 //if there is a branch length then print it
1186 if (theseNodes[node].getBranchLength() != -1) {
1187 out << ":" << theseNodes[node].getBranchLength();
1190 }else { //you are a leaf
1191 vector<string> leafGroup = ct->getGroups(theseNodes[node].getName());
1193 if (mode == "branch") {
1194 out << leafGroup[0];
1195 //if there is a branch length then print it
1196 if (theseNodes[node].getBranchLength() != -1) {
1197 out << ":" << theseNodes[node].getBranchLength();
1199 }else if (mode == "boot") {
1200 out << leafGroup[0];
1201 //if there is a label then print it
1202 if (theseNodes[node].getLabel() != -1) {
1203 out << theseNodes[node].getLabel();
1205 }else if (mode == "both") {
1206 out << theseNodes[node].getName();
1207 if (theseNodes[node].getLabel() != -1) {
1208 out << theseNodes[node].getLabel();
1210 //if there is a branch length then print it
1211 if (theseNodes[node].getBranchLength() != -1) {
1212 out << ":" << theseNodes[node].getBranchLength();
1218 catch(exception& e) {
1219 m->errorOut(e, "Tree", "printBranch");
1223 /*****************************************************************/
1225 void Tree::printTree() {
1227 for(int i=0;i<numNodes;i++){
1229 tree[i].printNode();
1234 /*****************************************************************/
1235 //this code is a mess and should be rethought...-slw
1236 int Tree::parseTreeFile() {
1238 //only takes names from the first tree and assumes that all trees use the same names.
1240 string filename = m->getTreeFile();
1241 ifstream filehandle;
1242 m->openInputFile(filename, filehandle);
1247 //ifyou are not a nexus file
1248 if((c = filehandle.peek()) != '#') {
1249 while((c = filehandle.peek()) != ';') {
1250 if (m->control_pressed) { filehandle.close(); return 0; }
1251 while ((c = filehandle.peek()) != ';') {
1252 if (m->control_pressed) { filehandle.close(); return 0; }
1253 // get past comments
1260 if((c == '(') && (comment != 1)){ break; }
1264 done = readTreeString(filehandle);
1265 if (done == 0) { break; }
1267 //ifyou are a nexus file
1268 }else if((c = filehandle.peek()) == '#') {
1271 // get past comments
1272 while(holder != "translate" && holder != "Translate"){
1273 if (m->control_pressed) { filehandle.close(); return 0; }
1274 if(holder == "[" || holder == "[!"){
1280 filehandle >> holder;
1282 //if there is no translate then you must read tree string otherwise use translate to get names
1283 if((holder == "tree") && (comment != 1)){
1284 //pass over the "tree rep.6878900 = "
1285 while (((c = filehandle.get()) != '(') && ((c = filehandle.peek()) != EOF)) {;}
1287 if(c == EOF) { break; }
1288 filehandle.putback(c); //put back first ( of tree.
1289 done = readTreeString(filehandle);
1294 if (done == 0) { break; }
1297 //use nexus translation rather than parsing tree to save time
1298 if((holder == "translate") || (holder == "Translate")) {
1300 string number, name, h;
1301 h = ""; // so it enters the loop the first time
1302 while((h != ";") && (number != ";")) {
1303 if (m->control_pressed) { filehandle.close(); return 0; }
1304 filehandle >> number;
1307 //c = , until done with translation then c = ;
1308 h = name.substr(name.length()-1, name.length());
1309 name.erase(name.end()-1); //erase the comma
1310 m->Treenames.push_back(number);
1312 if(number == ";") { m->Treenames.pop_back(); } //in case ';' from translation is on next line instead of next to last name
1317 //for (int i = 0; i < globaldata->Treenames.size(); i++) {
1318 //cout << globaldata->Treenames[i] << endl; }
1319 //cout << globaldata->Treenames.size() << endl;
1321 catch(exception& e) {
1322 m->errorOut(e, "Tree", "parseTreeFile");
1326 /*******************************************************/
1328 /*******************************************************/
1329 int Tree::readTreeString(ifstream& filehandle) {
1334 while((c = filehandle.peek()) != ';') {
1335 if (m->control_pressed) { return 0; }
1337 //cout << " at beginning of while " << k << endl;
1339 //to pass over labels in trees
1341 while((c!=',') && (c != -1) && (c!= ':') && (c!=';')){ c=filehandle.get(); }
1342 filehandle.putback(c);
1344 if(c == ';') { return 0; }
1345 if(c == -1) { return 0; }
1347 if((c != '(') && (c != ')') && (c != ',') && (c != ':') && (c != '\n') && (c != '\t') && (c != 32)) { //32 is space
1349 c = filehandle.get();
1351 //cout << k << endl;
1352 while ((c != '(') && (c != ')') && (c != ',') && (c != ':') && (c != '\n') && (c != 32) && (c != '\t')) {
1354 c = filehandle.get();
1356 //cout << " in name while " << k << endl;
1359 //cout << "name = " << name << endl;
1360 if (name != "\r" ) {
1361 m->Treenames.push_back(name); } //cout << m->Treenames.size() << '\t' << name << endl;
1363 filehandle.putback(c);
1365 //cout << " after putback" << k << endl;
1368 if(c == ':') { //read until you reach the end of the branch length
1369 while ((c != '(') && (c != ')') && (c != ',') && (c != ';') && (c != '\n') && (c != '\t') && (c != 32)) {
1370 c = filehandle.get();
1372 //cout << " in branch while " << k << endl;
1374 filehandle.putback(c);
1377 c = filehandle.get();
1379 //cout << " here after get " << k << endl;
1380 if(c == ';') { return 0; }
1381 if(c == ')') { filehandle.putback(c); }
1383 //cout << k << endl;
1388 catch(exception& e) {
1389 m->errorOut(e, "Tree", "readTreeString");
1394 /*******************************************************/
1396 /*******************************************************/