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, TreeMap* t) : tmap(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();
34 parseTreeFile(); m->runParse = false;
37 m->errorOut(e, "Tree", "Tree - just parse");
41 /*****************************************************************/
42 Tree::Tree(TreeMap* t) : tmap(t) {
44 m = MothurOut::getInstance();
46 if (m->runParse == true) { parseTreeFile(); m->runParse = false; }
47 //for(int i = 0; i < globaldata->Treenames.size(); i++) { cout << i << '\t' << globaldata->Treenames[i] << endl; }
48 numLeaves = m->Treenames.size();
49 numNodes = 2*numLeaves - 1;
51 tree.resize(numNodes);
53 //initialize groupNodeInfo
54 for (int i = 0; i < tmap->namesOfGroups.size(); i++) {
55 groupNodeInfo[tmap->namesOfGroups[i]].resize(0);
58 //initialize tree with correct number of nodes, name and group info.
59 for (int i = 0; i < numNodes; i++) {
60 //initialize leaf nodes
61 if (i <= (numLeaves-1)) {
62 tree[i].setName(m->Treenames[i]);
65 string group = tmap->getGroup(m->Treenames[i]);
67 vector<string> tempGroups; tempGroups.push_back(group);
68 tree[i].setGroup(tempGroups);
69 groupNodeInfo[group].push_back(i);
71 //set pcount and pGroup for groupname to 1.
72 tree[i].pcount[group] = 1;
73 tree[i].pGroups[group] = 1;
75 //Treemap knows name, group and index to speed up search
76 tmap->setIndex(m->Treenames[i], i);
78 //intialize non leaf nodes
79 }else if (i > (numLeaves-1)) {
81 vector<string> tempGroups;
82 tree[i].setGroup(tempGroups);
88 m->errorOut(e, "Tree", "Tree");
93 /*****************************************************************/
95 /*****************************************************************/
96 void Tree::addNamesToCounts() {
98 //ex. seq1 seq2,seq3,se4
104 //before this function seq1.pcount = pasture -> 1
105 //after seq1.pcount = pasture -> 2, forest -> 1, ocean -> 1
107 //before this function seq1.pgroups = pasture -> 1
108 //after seq1.pgroups = pasture -> 1 since that is the dominant group
111 //go through each leaf and update its pcounts and pgroups
115 for (int i = 0; i < numLeaves; i++) {
117 string name = tree[i].getName();
119 map<string, string>::iterator itNames = m->names.find(name);
121 if (itNames == m->names.end()) { m->mothurOut(name + " is not in your name file, please correct."); m->mothurOutEndLine(); exit(1); }
123 vector<string> dupNames;
124 m->splitAtComma(m->names[name], dupNames);
126 map<string, int>::iterator itCounts;
128 set<string> groupsAddedForThisNode;
129 for (int j = 0; j < dupNames.size(); j++) {
131 string group = tmap->getGroup(dupNames[j]);
133 if (dupNames[j] != name) {//you already added yourself in the constructor
135 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
138 itCounts = tree[i].pcount.find(group);
139 if (itCounts == tree[i].pcount.end()) { //new group, add it
140 tree[i].pcount[group] = 1;
142 tree[i].pcount[group]++;
146 itCounts = tree[i].pGroups.find(group);
147 if (itCounts == tree[i].pGroups.end()) { //new group, add it
148 tree[i].pGroups[group] = 1;
150 tree[i].pGroups[group]++;
154 if(tree[i].pGroups[group] > maxPars){
155 maxPars = tree[i].pGroups[group];
157 }else { groupsAddedForThisNode.insert(group); } //add it so you don't add it to groupNodeInfo again
160 if (maxPars > 1) { //then we have some more dominant groups
161 //erase all the groups that are less than maxPars because you found a more dominant group.
162 for(it=tree[i].pGroups.begin();it!=tree[i].pGroups.end();){
163 if(it->second < maxPars){
164 tree[i].pGroups.erase(it++);
167 //set one remaining groups to 1
168 for(it=tree[i].pGroups.begin();it!=tree[i].pGroups.end();it++){
169 tree[i].pGroups[it->first] = 1;
173 //update groups to reflect all the groups this node represents
174 vector<string> nodeGroups;
175 map<string, int>::iterator itGroups;
176 for (itGroups = tree[i].pcount.begin(); itGroups != tree[i].pcount.end(); itGroups++) {
177 nodeGroups.push_back(itGroups->first);
179 tree[i].setGroup(nodeGroups);
185 //cout << "addNamesToCounts\t" << (B - A) / CLOCKS_PER_SEC << endl;
188 catch(exception& e) {
189 m->errorOut(e, "Tree", "addNamesToCounts");
193 /*****************************************************************/
194 int Tree::getIndex(string searchName) {
196 //Treemap knows name, group and index to speed up search
197 // getIndex function will return the vector index or -1 if seq is not found.
198 int index = tmap->getIndex(searchName);
202 catch(exception& e) {
203 m->errorOut(e, "Tree", "getIndex");
207 /*****************************************************************/
209 void Tree::setIndex(string searchName, int index) {
211 //set index in treemap
212 tmap->setIndex(searchName, index);
214 catch(exception& e) {
215 m->errorOut(e, "Tree", "setIndex");
219 /*****************************************************************/
220 int Tree::assembleTree() {
224 //if user has given a names file we want to include that info in the pgroups and pcount info.
225 if(m->names.size() != 0) { addNamesToCounts(); }
227 //build the pGroups in non leaf nodes to be used in the parsimony calcs.
228 for (int i = numLeaves; i < numNodes; i++) {
229 if (m->control_pressed) { return 1; }
231 tree[i].pGroups = (mergeGroups(i));
232 tree[i].pcount = (mergeGcounts(i));
235 //cout << "assembleTree\t" << (B-A) / CLOCKS_PER_SEC << endl;
238 catch(exception& e) {
239 m->errorOut(e, "Tree", "assembleTree");
243 /*****************************************************************/
244 int Tree::assembleTree(string n) {
247 //build the pGroups in non leaf nodes to be used in the parsimony calcs.
248 for (int i = numLeaves; i < numNodes; i++) {
249 if (m->control_pressed) { return 1; }
251 tree[i].pGroups = (mergeGroups(i));
252 tree[i].pcount = (mergeGcounts(i));
255 //cout << "assembleTree\t" << (B-A) / CLOCKS_PER_SEC << endl;
258 catch(exception& e) {
259 m->errorOut(e, "Tree", "assembleTree");
263 /*****************************************************************/
264 void Tree::getSubTree(Tree* copy, vector<string> Groups) {
267 //we want to select some of the leaf nodes to create the output tree
268 //go through the input Tree starting at parents of leaves
269 for (int i = 0; i < numNodes; i++) {
271 //initialize leaf nodes
272 if (i <= (numLeaves-1)) {
273 tree[i].setName(Groups[i]);
276 string group = tmap->getGroup(Groups[i]);
277 vector<string> tempGroups; tempGroups.push_back(group);
278 tree[i].setGroup(tempGroups);
279 groupNodeInfo[group].push_back(i);
281 //set pcount and pGroup for groupname to 1.
282 tree[i].pcount[group] = 1;
283 tree[i].pGroups[group] = 1;
285 //Treemap knows name, group and index to speed up search
286 tmap->setIndex(Groups[i], i);
288 //intialize non leaf nodes
289 }else if (i > (numLeaves-1)) {
291 vector<string> tempGroups;
292 tree[i].setGroup(tempGroups);
296 set<int> removedLeaves;
297 for (int i = 0; i < copy->getNumLeaves(); i++) {
299 if (removedLeaves.count(i) == 0) {
302 int parent = copy->tree[i].getParent();
306 if (m->inUsersGroups(copy->tree[i].getName(), Groups)) {
307 //find my siblings name
308 int parentRC = copy->tree[parent].getRChild();
309 int parentLC = copy->tree[parent].getLChild();
311 //if I am the right child, then my sib is the left child
312 int sibIndex = parentRC;
313 if (parentRC == i) { sibIndex = parentLC; }
315 string sibsName = copy->tree[sibIndex].getName();
317 //if yes, is my sibling
318 if ((m->inUsersGroups(sibsName, Groups)) || (sibsName == "")) {
319 //we both are okay no trimming required
321 //i am, my sib is not, so remove sib by setting my parent to my grandparent
322 int grandparent = copy->tree[parent].getParent();
323 int grandparentLC = copy->tree[grandparent].getLChild();
324 int grandparentRC = copy->tree[grandparent].getRChild();
326 //whichever of my granparents children was my parent now equals me
327 if (grandparentLC == parent) { grandparentLC = i; }
328 else { grandparentRC = i; }
330 copy->tree[i].setParent(grandparent);
331 copy->tree[i].setBranchLength((copy->tree[i].getBranchLength()+copy->tree[parent].getBranchLength()));
332 if (grandparent != -1) {
333 copy->tree[grandparent].setChildren(grandparentLC, grandparentRC);
335 removedLeaves.insert(sibIndex);
338 //find my siblings name
339 int parentRC = copy->tree[parent].getRChild();
340 int parentLC = copy->tree[parent].getLChild();
342 //if I am the right child, then my sib is the left child
343 int sibIndex = parentRC;
344 if (parentRC == i) { sibIndex = parentLC; }
346 string sibsName = copy->tree[sibIndex].getName();
348 //if no is my sibling
349 if ((m->inUsersGroups(sibsName, Groups)) || (sibsName == "")) {
350 //i am not, but my sib is
351 int grandparent = copy->tree[parent].getParent();
352 int grandparentLC = copy->tree[grandparent].getLChild();
353 int grandparentRC = copy->tree[grandparent].getRChild();
355 //whichever of my granparents children was my parent now equals my sib
356 if (grandparentLC == parent) { grandparentLC = sibIndex; }
357 else { grandparentRC = sibIndex; }
359 copy->tree[sibIndex].setParent(grandparent);
360 copy->tree[sibIndex].setBranchLength((copy->tree[sibIndex].getBranchLength()+copy->tree[parent].getBranchLength()));
361 if (grandparent != -1) {
362 copy->tree[grandparent].setChildren(grandparentLC, grandparentRC);
364 removedLeaves.insert(i);
366 //neither of us are, so we want to eliminate ourselves and our parent
367 //so set our parents sib to our great-grandparent
368 int parent = copy->tree[i].getParent();
369 int grandparent = copy->tree[parent].getParent();
371 if (grandparent != -1) {
372 int greatgrandparent = copy->tree[grandparent].getParent();
373 int greatgrandparentLC, greatgrandparentRC;
374 if (greatgrandparent != -1) {
375 greatgrandparentLC = copy->tree[greatgrandparent].getLChild();
376 greatgrandparentRC = copy->tree[greatgrandparent].getRChild();
379 int grandparentLC = copy->tree[grandparent].getLChild();
380 int grandparentRC = copy->tree[grandparent].getRChild();
382 parentsSibIndex = grandparentLC;
383 if (grandparentLC == parent) { parentsSibIndex = grandparentRC; }
385 //whichever of my greatgrandparents children was my grandparent
386 if (greatgrandparentLC == grandparent) { greatgrandparentLC = parentsSibIndex; }
387 else { greatgrandparentRC = parentsSibIndex; }
389 copy->tree[parentsSibIndex].setParent(greatgrandparent);
390 copy->tree[parentsSibIndex].setBranchLength((copy->tree[parentsSibIndex].getBranchLength()+copy->tree[grandparent].getBranchLength()));
391 if (greatgrandparent != -1) {
392 copy->tree[greatgrandparent].setChildren(greatgrandparentLC, greatgrandparentRC);
395 copy->tree[parent].setParent(-1);
396 //cout << "issues with making subtree" << endl;
398 removedLeaves.insert(sibIndex);
399 removedLeaves.insert(i);
407 for (int i = 0; i < copy->getNumNodes(); i++) {
409 if (copy->tree[i].getParent() == -1) { root = i; break; }
412 int nextSpot = numLeaves;
413 populateNewTree(copy->tree, root, nextSpot);
415 catch(exception& e) {
416 m->errorOut(e, "Tree", "getCopy");
420 /*****************************************************************/
421 int Tree::populateNewTree(vector<Node>& oldtree, int node, int& index) {
424 if (oldtree[node].getLChild() != -1) {
425 int rc = populateNewTree(oldtree, oldtree[node].getLChild(), index);
426 int lc = populateNewTree(oldtree, oldtree[node].getRChild(), index);
428 tree[index].setChildren(lc, rc);
429 tree[rc].setParent(index);
430 tree[lc].setParent(index);
432 tree[index].setBranchLength(oldtree[node].getBranchLength());
433 tree[rc].setBranchLength(oldtree[oldtree[node].getLChild()].getBranchLength());
434 tree[lc].setBranchLength(oldtree[oldtree[node].getRChild()].getBranchLength());
437 }else { //you are a leaf
438 int indexInNewTree = tmap->getIndex(oldtree[node].getName());
439 return indexInNewTree;
442 catch(exception& e) {
443 m->errorOut(e, "Tree", "populateNewTree");
447 /*****************************************************************/
448 void Tree::getCopy(Tree* copy) {
451 //for each node in the tree copy its info
452 for (int i = 0; i < numNodes; i++) {
454 tree[i].setName(copy->tree[i].getName());
457 tree[i].setGroup(copy->tree[i].getGroup());
460 tree[i].setBranchLength(copy->tree[i].getBranchLength());
463 tree[i].setParent(copy->tree[i].getParent());
466 tree[i].setChildren(copy->tree[i].getLChild(), copy->tree[i].getRChild());
468 //copy index in node and tmap
469 tree[i].setIndex(copy->tree[i].getIndex());
470 setIndex(copy->tree[i].getName(), getIndex(copy->tree[i].getName()));
473 tree[i].pGroups = copy->tree[i].pGroups;
476 tree[i].pcount = copy->tree[i].pcount;
479 groupNodeInfo = copy->groupNodeInfo;
482 catch(exception& e) {
483 m->errorOut(e, "Tree", "getCopy");
487 /*****************************************************************/
488 //returns a map with a groupname and the number of times that group was seen in the children
489 //for instance if your children are white and black then it would return a map with 2 entries
490 // p[white] = 1 and p[black] = 1. Now go up a level and merge that with a node who has p[white] = 1
491 //and you get p[white] = 2, p[black] = 1, but you erase the p[black] because you have a p value higher than 1.
493 map<string, int> Tree::mergeGroups(int i) {
495 int lc = tree[i].getLChild();
496 int rc = tree[i].getRChild();
498 //set parsimony groups to left child
499 map<string,int> parsimony = tree[lc].pGroups;
503 //look at right child groups and update maxPars if right child has something higher for that group.
504 for(it=tree[rc].pGroups.begin();it!=tree[rc].pGroups.end();it++){
505 it2 = parsimony.find(it->first);
506 if (it2 != parsimony.end()) {
507 parsimony[it->first]++;
509 parsimony[it->first] = 1;
512 if(parsimony[it->first] > maxPars){
513 maxPars = parsimony[it->first];
517 // this is true if right child had a greater parsimony for a certain group
519 //erase all the groups that are only 1 because you found something with 2.
520 for(it=parsimony.begin();it!=parsimony.end();){
522 parsimony.erase(it++);
525 //set one remaining groups to 1
526 //so with our above example p[white] = 2 would be left and it would become p[white] = 1
527 for(it=parsimony.begin();it!=parsimony.end();it++){
528 parsimony[it->first] = 1;
535 catch(exception& e) {
536 m->errorOut(e, "Tree", "mergeGroups");
540 /*****************************************************************/
541 //returns a map with a groupname and the number of times that group was seen in the children
542 //for instance if your children are white and black then it would return a map with 2 entries
543 // p[white] = 1 and p[black] = 1. Now go up a level and merge that with a node who has p[white] = 1
544 //and you get p[white] = 2, p[black] = 1, but you erase the p[black] because you have a p value higher than 1.
546 map<string, int> Tree::mergeUserGroups(int i, vector<string> g) {
549 int lc = tree[i].getLChild();
550 int rc = tree[i].getRChild();
552 //loop through nodes groups removing the ones the user doesn't want
553 for(it=tree[lc].pGroups.begin();it!=tree[lc].pGroups.end();){
554 if (m->inUsersGroups(it->first, g) != true) {
555 tree[lc].pGroups.erase(it++);
559 //loop through nodes groups removing the ones the user doesn't want
560 for(it=tree[rc].pGroups.begin();it!=tree[rc].pGroups.end();){
561 if (m->inUsersGroups(it->first, g) != true) {
562 tree[rc].pGroups.erase(it++);
566 //set parsimony groups to left child
567 map<string,int> parsimony = tree[lc].pGroups;
571 //look at right child groups and update maxPars if right child has something higher for that group.
572 for(it=tree[rc].pGroups.begin();it!=tree[rc].pGroups.end();it++){
573 it2 = parsimony.find(it->first);
574 if (it2 != parsimony.end()) {
575 parsimony[it->first]++;
577 parsimony[it->first] = 1;
580 if(parsimony[it->first] > maxPars){
581 maxPars = parsimony[it->first];
585 // this is true if right child had a greater parsimony for a certain group
587 //erase all the groups that are only 1 because you found something with 2.
588 for(it=parsimony.begin();it!=parsimony.end();){
590 parsimony.erase(it++);
594 for(it=parsimony.begin();it!=parsimony.end();it++){
595 parsimony[it->first] = 1;
601 catch(exception& e) {
602 m->errorOut(e, "Tree", "mergeUserGroups");
608 /**************************************************************************************************/
610 map<string,int> Tree::mergeGcounts(int position) {
612 map<string,int>::iterator pos;
614 int lc = tree[position].getLChild();
615 int rc = tree[position].getRChild();
617 map<string,int> sum = tree[lc].pcount;
619 for(it=tree[rc].pcount.begin();it!=tree[rc].pcount.end();it++){
620 sum[it->first] += it->second;
624 catch(exception& e) {
625 m->errorOut(e, "Tree", "mergeGcounts");
629 /**************************************************************************************************/
631 void Tree::randomLabels(vector<string> g) {
634 //initialize groupNodeInfo
635 for (int i = 0; i < tmap->namesOfGroups.size(); i++) {
636 groupNodeInfo[tmap->namesOfGroups[i]].resize(0);
639 for(int i = 0; i < numLeaves; i++){
641 //get random index to switch with
642 z = int((float)(i+1) * (float)(rand()) / ((float)RAND_MAX+1.0));
644 //you only want to randomize the nodes that are from a group the user wants analyzed, so
645 //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.
648 treez = m->inUsersGroups(tree[z].getGroup(), g);
649 treei = m->inUsersGroups(tree[i].getGroup(), g);
651 if ((treez == true) && (treei == true)) {
652 //switches node i and node z's info.
653 map<string,int> lib_hold = tree[z].pGroups;
654 tree[z].pGroups = (tree[i].pGroups);
655 tree[i].pGroups = (lib_hold);
657 vector<string> zgroup = tree[z].getGroup();
658 tree[z].setGroup(tree[i].getGroup());
659 tree[i].setGroup(zgroup);
661 string zname = tree[z].getName();
662 tree[z].setName(tree[i].getName());
663 tree[i].setName(zname);
665 map<string,int> gcount_hold = tree[z].pcount;
666 tree[z].pcount = (tree[i].pcount);
667 tree[i].pcount = (gcount_hold);
670 for (int k = 0; k < (tree[i].getGroup()).size(); k++) { groupNodeInfo[(tree[i].getGroup())[k]].push_back(i); }
671 for (int k = 0; k < (tree[z].getGroup()).size(); k++) { groupNodeInfo[(tree[z].getGroup())[k]].push_back(z); }
674 catch(exception& e) {
675 m->errorOut(e, "Tree", "randomLabels");
679 /**************************************************************************************************
681 void Tree::randomLabels(string groupA, string groupB) {
683 int numSeqsA = globaldata->gTreemap->seqsPerGroup[groupA];
684 int numSeqsB = globaldata->gTreemap->seqsPerGroup[groupB];
686 vector<string> randomGroups(numSeqsA+numSeqsB, groupA);
687 for(int i=numSeqsA;i<randomGroups.size();i++){
688 randomGroups[i] = groupB;
690 random_shuffle(randomGroups.begin(), randomGroups.end());
692 int randomCounter = 0;
693 for(int i=0;i<numLeaves;i++){
694 if(tree[i].getGroup() == groupA || tree[i].getGroup() == groupB){
695 tree[i].setGroup(randomGroups[randomCounter]);
696 tree[i].pcount.clear();
697 tree[i].pcount[randomGroups[randomCounter]] = 1;
698 tree[i].pGroups.clear();
699 tree[i].pGroups[randomGroups[randomCounter]] = 1;
704 catch(exception& e) {
705 m->errorOut(e, "Tree", "randomLabels");
709 /**************************************************************************************************/
710 void Tree::randomBlengths() {
712 for(int i=numNodes-1;i>=0;i--){
713 int z = int((float)(i+1) * (float)(rand()) / ((float)RAND_MAX+1.0));
715 float bl_hold = tree[z].getBranchLength();
716 tree[z].setBranchLength(tree[i].getBranchLength());
717 tree[i].setBranchLength(bl_hold);
720 catch(exception& e) {
721 m->errorOut(e, "Tree", "randomBlengths");
725 /*************************************************************************************************/
726 void Tree::assembleRandomUnifracTree(vector<string> g) {
728 assembleTree("noNameCounts");
730 /*************************************************************************************************/
731 void Tree::assembleRandomUnifracTree(string groupA, string groupB) {
733 vector<string> temp; temp.push_back(groupA); temp.push_back(groupB);
735 assembleTree("noNameCounts");
738 /*************************************************************************************************/
739 //for now it's just random topology but may become random labels as well later that why this is such a simple function now...
740 void Tree::assembleRandomTree() {
744 /**************************************************************************************************/
746 void Tree::randomTopology() {
748 for(int i=0;i<numNodes;i++){
749 tree[i].setParent(-1);
751 for(int i=numLeaves;i<numNodes;i++){
752 tree[i].setChildren(-1, -1);
755 for(int i=numLeaves;i<numNodes;i++){
757 int rnd_index1, rnd_index2;
759 rnd_index1 = (int)(((double)rand() / (double) RAND_MAX)*i);
760 if(tree[rnd_index1].getParent() == -1){escape = 1;}
765 rnd_index2 = (int)(((double)rand() / (double) RAND_MAX)*i);
766 if(rnd_index2 != rnd_index1 && tree[rnd_index2].getParent() == -1){
771 tree[i].setChildren(rnd_index1,rnd_index2);
772 tree[i].setParent(-1);
773 tree[rnd_index1].setParent(i);
774 tree[rnd_index2].setParent(i);
777 catch(exception& e) {
778 m->errorOut(e, "Tree", "randomTopology");
782 /*****************************************************************/
783 void Tree::print(ostream& out) {
785 int root = findRoot();
786 printBranch(root, out, "branch");
789 catch(exception& e) {
790 m->errorOut(e, "Tree", "print");
794 /*****************************************************************/
795 void Tree::print(ostream& out, string mode) {
797 int root = findRoot();
798 printBranch(root, out, mode);
801 catch(exception& e) {
802 m->errorOut(e, "Tree", "print");
806 /*****************************************************************/
807 // This prints out the tree in Newick form.
808 void Tree::createNewickFile(string f) {
810 int root = findRoot();
814 m->openOutputFile(filename, out);
816 printBranch(root, out, "branch");
818 // you are at the end of the tree
822 catch(exception& e) {
823 m->errorOut(e, "Tree", "createNewickFile");
828 /*****************************************************************/
829 //This function finds the index of the root node.
831 int Tree::findRoot() {
833 for (int i = 0; i < numNodes; i++) {
835 if (tree[i].getParent() == -1) { return i; }
836 //cout << "i = " << i << endl;
837 //cout << "i's parent = " << tree[i].getParent() << endl;
841 catch(exception& e) {
842 m->errorOut(e, "Tree", "findRoot");
846 /*****************************************************************/
847 void Tree::printBranch(int node, ostream& out, string mode) {
850 // you are not a leaf
851 if (tree[node].getLChild() != -1) {
853 printBranch(tree[node].getLChild(), out, mode);
855 printBranch(tree[node].getRChild(), out, mode);
857 if (mode == "branch") {
858 //if there is a branch length then print it
859 if (tree[node].getBranchLength() != -1) {
860 out << ":" << tree[node].getBranchLength();
862 }else if (mode == "boot") {
863 //if there is a label then print it
864 if (tree[node].getLabel() != -1) {
865 out << tree[node].getLabel();
867 }else if (mode == "both") {
868 if (tree[node].getLabel() != -1) {
869 out << tree[node].getLabel();
871 //if there is a branch length then print it
872 if (tree[node].getBranchLength() != -1) {
873 out << ":" << tree[node].getBranchLength();
875 }else if (mode == "deunique") {
876 //if there is a branch length then print it
877 if (tree[node].getBranchLength() != -1) {
878 out << ":" << tree[node].getBranchLength();
881 }else { //you are a leaf
882 string leafGroup = tmap->getGroup(tree[node].getName());
884 if (mode == "branch") {
886 //if there is a branch length then print it
887 if (tree[node].getBranchLength() != -1) {
888 out << ":" << tree[node].getBranchLength();
890 }else if (mode == "boot") {
892 //if there is a label then print it
893 if (tree[node].getLabel() != -1) {
894 out << tree[node].getLabel();
896 }else if (mode == "both") {
897 out << tree[node].getName();
898 if (tree[node].getLabel() != -1) {
899 out << tree[node].getLabel();
901 //if there is a branch length then print it
902 if (tree[node].getBranchLength() != -1) {
903 out << ":" << tree[node].getBranchLength();
905 }else if (mode == "deunique") {
906 map<string, string>::iterator itNames = m->names.find(tree[node].getName());
908 string outputString = "";
909 if (itNames != m->names.end()) {
911 vector<string> dupNames;
912 m->splitAtComma((itNames->second), dupNames);
914 if (dupNames.size() == 1) {
915 outputString += tree[node].getName();
916 if (tree[node].getBranchLength() != -1) {
917 outputString += ":" + toString(tree[node].getBranchLength());
922 for (int u = 0; u < dupNames.size()-1; u++) {
923 outputString += dupNames[u];
925 if (tree[node].getBranchLength() != -1) {
926 outputString += ":" + toString(0.0);
931 outputString += dupNames[dupNames.size()-1];
932 if (tree[node].getBranchLength() != -1) {
933 outputString += ":" + toString(0.0);
937 if (tree[node].getBranchLength() != -1) {
938 outputString += ":" + toString(tree[node].getBranchLength());
942 outputString = tree[node].getName();
943 //if there is a branch length then print it
944 if (tree[node].getBranchLength() != -1) {
945 outputString += ":" + toString(tree[node].getBranchLength());
948 m->mothurOut("[ERROR]: " + tree[node].getName() + " is not in your namefile, please correct."); m->mothurOutEndLine();
956 catch(exception& e) {
957 m->errorOut(e, "Tree", "printBranch");
961 /*****************************************************************/
962 void Tree::printBranch(int node, ostream& out, string mode, vector<Node>& theseNodes) {
965 // you are not a leaf
966 if (theseNodes[node].getLChild() != -1) {
968 printBranch(theseNodes[node].getLChild(), out, mode);
970 printBranch(theseNodes[node].getRChild(), out, mode);
972 if (mode == "branch") {
973 //if there is a branch length then print it
974 if (theseNodes[node].getBranchLength() != -1) {
975 out << ":" << theseNodes[node].getBranchLength();
977 }else if (mode == "boot") {
978 //if there is a label then print it
979 if (theseNodes[node].getLabel() != -1) {
980 out << theseNodes[node].getLabel();
982 }else if (mode == "both") {
983 if (theseNodes[node].getLabel() != -1) {
984 out << theseNodes[node].getLabel();
986 //if there is a branch length then print it
987 if (theseNodes[node].getBranchLength() != -1) {
988 out << ":" << theseNodes[node].getBranchLength();
991 }else { //you are a leaf
992 string leafGroup = tmap->getGroup(theseNodes[node].getName());
994 if (mode == "branch") {
996 //if there is a branch length then print it
997 if (theseNodes[node].getBranchLength() != -1) {
998 out << ":" << theseNodes[node].getBranchLength();
1000 }else if (mode == "boot") {
1002 //if there is a label then print it
1003 if (theseNodes[node].getLabel() != -1) {
1004 out << theseNodes[node].getLabel();
1006 }else if (mode == "both") {
1007 out << theseNodes[node].getName();
1008 if (theseNodes[node].getLabel() != -1) {
1009 out << theseNodes[node].getLabel();
1011 //if there is a branch length then print it
1012 if (theseNodes[node].getBranchLength() != -1) {
1013 out << ":" << theseNodes[node].getBranchLength();
1019 catch(exception& e) {
1020 m->errorOut(e, "Tree", "printBranch");
1024 /*****************************************************************/
1026 void Tree::printTree() {
1028 for(int i=0;i<numNodes;i++){
1030 tree[i].printNode();
1035 /*****************************************************************/
1036 //this code is a mess and should be rethought...-slw
1037 void Tree::parseTreeFile() {
1039 //only takes names from the first tree and assumes that all trees use the same names.
1041 string filename = m->getTreeFile();
1042 ifstream filehandle;
1043 m->openInputFile(filename, filehandle);
1048 //ifyou are not a nexus file
1049 if((c = filehandle.peek()) != '#') {
1050 while((c = filehandle.peek()) != ';') {
1051 while ((c = filehandle.peek()) != ';') {
1052 // get past comments
1059 if((c == '(') && (comment != 1)){ break; }
1063 done = readTreeString(filehandle);
1064 if (done == 0) { break; }
1066 //ifyou are a nexus file
1067 }else if((c = filehandle.peek()) == '#') {
1070 // get past comments
1071 while(holder != "translate" && holder != "Translate"){
1072 if(holder == "[" || holder == "[!"){
1078 filehandle >> holder;
1080 //if there is no translate then you must read tree string otherwise use translate to get names
1081 if((holder == "tree") && (comment != 1)){
1082 //pass over the "tree rep.6878900 = "
1083 while (((c = filehandle.get()) != '(') && ((c = filehandle.peek()) != EOF)) {;}
1085 if(c == EOF) { break; }
1086 filehandle.putback(c); //put back first ( of tree.
1087 done = readTreeString(filehandle);
1092 if (done == 0) { break; }
1095 //use nexus translation rather than parsing tree to save time
1096 if((holder == "translate") || (holder == "Translate")) {
1098 string number, name, h;
1099 h = ""; // so it enters the loop the first time
1100 while((h != ";") && (number != ";")) {
1101 filehandle >> number;
1104 //c = , until done with translation then c = ;
1105 h = name.substr(name.length()-1, name.length());
1106 name.erase(name.end()-1); //erase the comma
1107 m->Treenames.push_back(number);
1109 if(number == ";") { m->Treenames.pop_back(); } //in case ';' from translation is on next line instead of next to last name
1114 //for (int i = 0; i < globaldata->Treenames.size(); i++) {
1115 //cout << globaldata->Treenames[i] << endl; }
1116 //cout << globaldata->Treenames.size() << endl;
1118 catch(exception& e) {
1119 m->errorOut(e, "Tree", "parseTreeFile");
1123 /*******************************************************/
1125 /*******************************************************/
1126 int Tree::readTreeString(ifstream& filehandle) {
1131 while((c = filehandle.peek()) != ';') {
1133 //cout << " at beginning of while " << k << endl;
1135 //to pass over labels in trees
1137 while((c!=',') && (c != -1) && (c!= ':') && (c!=';')){ c=filehandle.get(); }
1138 filehandle.putback(c);
1140 if(c == ';') { return 0; }
1141 if(c == -1) { return 0; }
1143 if((c != '(') && (c != ')') && (c != ',') && (c != ':') && (c != '\n') && (c != '\t') && (c != 32)) { //32 is space
1145 c = filehandle.get();
1147 //cout << k << endl;
1148 while ((c != '(') && (c != ')') && (c != ',') && (c != ':') && (c != '\n') && (c != 32) && (c != '\t')) {
1150 c = filehandle.get();
1152 //cout << " in name while " << k << endl;
1155 //cout << "name = " << name << endl;
1156 m->Treenames.push_back(name);
1157 filehandle.putback(c);
1159 //cout << " after putback" << k << endl;
1162 if(c == ':') { //read until you reach the end of the branch length
1163 while ((c != '(') && (c != ')') && (c != ',') && (c != ';') && (c != '\n') && (c != '\t') && (c != 32)) {
1164 c = filehandle.get();
1166 //cout << " in branch while " << k << endl;
1168 filehandle.putback(c);
1171 c = filehandle.get();
1173 //cout << " here after get " << k << endl;
1174 if(c == ';') { return 0; }
1175 if(c == ')') { filehandle.putback(c); }
1177 //cout << k << endl;
1182 catch(exception& e) {
1183 m->errorOut(e, "Tree", "readTreeString");
1188 /*******************************************************/
1190 /*******************************************************/