5 * Created by Sarah Westcott on 1/22/09.
6 * Copyright 2009 Schloss Lab UMASS Amherst. All rights reserved.
13 /*****************************************************************/
16 globaldata = GlobalData::getInstance();
18 if (globaldata->runParse == true) { parseTreeFile(); globaldata->runParse = false; }
20 numLeaves = globaldata->Treenames.size();
21 numNodes = 2*numLeaves - 1;
23 tree.resize(numNodes);
25 //initialize tree with correct number of nodes, name and group info.
26 for (int i = 0; i < numNodes; i++) {
27 //initialize leaf nodes
28 if (i <= (numLeaves-1)) {
29 tree[i].setName(globaldata->Treenames[i]);
30 tree[i].setGroup(globaldata->gTreemap->getGroup(globaldata->Treenames[i]));
31 //set pcount and pGroup for groupname to 1.
32 tree[i].pcount[globaldata->gTreemap->getGroup(globaldata->Treenames[i])] = 1;
33 tree[i].pGroups[globaldata->gTreemap->getGroup(globaldata->Treenames[i])] = 1;
34 //Treemap knows name, group and index to speed up search
35 globaldata->gTreemap->setIndex(globaldata->Treenames[i], i);
37 //intialize non leaf nodes
38 }else if (i > (numLeaves-1)) {
45 errorOut(e, "Tree", "Tree");
50 /*****************************************************************/
52 /*****************************************************************/
53 int Tree::getIndex(string searchName) {
55 //Treemap knows name, group and index to speed up search
56 // getIndex function will return the vector index or -1 if seq is not found.
57 int index = globaldata->gTreemap->getIndex(searchName);
62 errorOut(e, "Tree", "getIndex");
66 /*****************************************************************/
68 void Tree::setIndex(string searchName, int index) {
70 //set index in treemap
71 globaldata->gTreemap->setIndex(searchName, index);
74 errorOut(e, "Tree", "setIndex");
78 /*****************************************************************/
79 void Tree::assembleTree() {
81 //build the pGroups in non leaf nodes to be used in the parsimony calcs.
82 for (int i = numLeaves; i < numNodes; i++) {
83 tree[i].pGroups = (mergeGroups(i));
84 tree[i].pcount = (mergeGcounts(i));
88 errorOut(e, "Tree", "assembleTree");
92 /*****************************************************************/
93 void Tree::getCopy(Tree* copy) {
96 //for each node in the tree copy its info
97 for (int i = 0; i < numNodes; i++) {
99 tree[i].setName(copy->tree[i].getName());
102 tree[i].setGroup(copy->tree[i].getGroup());
105 tree[i].setBranchLength(copy->tree[i].getBranchLength());
108 tree[i].setParent(copy->tree[i].getParent());
111 tree[i].setChildren(copy->tree[i].getLChild(), copy->tree[i].getRChild());
113 //copy index in node and tmap
114 tree[i].setIndex(copy->tree[i].getIndex());
115 setIndex(copy->tree[i].getName(), getIndex(copy->tree[i].getName()));
118 tree[i].pGroups = copy->tree[i].pGroups;
121 tree[i].pcount = copy->tree[i].pcount;
124 catch(exception& e) {
125 errorOut(e, "Tree", "getCopy");
129 /*****************************************************************/
130 //returns a map with a groupname and the number of times that group was seen in the children
131 //for instance if your children are white and black then it would return a map with 2 entries
132 // p[white] = 1 and p[black] = 1. Now go up a level and merge that with a node who has p[white] = 1
133 //and you get p[white] = 2, p[black] = 1, but you erase the p[black] because you have a p value higher than 1.
135 map<string, int> Tree::mergeGroups(int i) {
137 int lc = tree[i].getLChild();
138 int rc = tree[i].getRChild();
140 //set parsimony groups to left child
141 map<string,int> parsimony = tree[lc].pGroups;
145 //look at right child groups and update maxPars if right child has something higher for that group.
146 for(it=tree[rc].pGroups.begin();it!=tree[rc].pGroups.end();it++){
147 it2 = parsimony.find(it->first);
148 if (it2 != parsimony.end()) {
149 parsimony[it->first]++;
151 parsimony[it->first] = 1;
154 if(parsimony[it->first] > maxPars){
155 maxPars = parsimony[it->first];
159 // this is true if right child had a greater parsimony for a certain group
161 //erase all the groups that are only 1 because you found something with 2.
162 for(it=parsimony.begin();it!=parsimony.end();it++){
164 parsimony.erase(it->first);
168 //set one remaining groups to 1
169 //so with our above example p[white] = 2 would be left and it would become p[white] = 1
170 for(it=parsimony.begin();it!=parsimony.end();it++){
171 parsimony[it->first] = 1;
178 catch(exception& e) {
179 errorOut(e, "Tree", "mergeGroups");
183 /*****************************************************************/
184 //returns a map with a groupname and the number of times that group was seen in the children
185 //for instance if your children are white and black then it would return a map with 2 entries
186 // p[white] = 1 and p[black] = 1. Now go up a level and merge that with a node who has p[white] = 1
187 //and you get p[white] = 2, p[black] = 1, but you erase the p[black] because you have a p value higher than 1.
189 map<string, int> Tree::mergeUserGroups(int i, vector<string> g) {
192 int lc = tree[i].getLChild();
193 int rc = tree[i].getRChild();
195 //loop through nodes groups removing the ones the user doesn't want
196 for (it = tree[lc].pGroups.begin(); it != tree[lc].pGroups.end(); it++) {
197 if (inUsersGroups(it->first, g) != true) { tree[lc].pGroups.erase(it->first); }
200 //loop through nodes groups removing the ones the user doesn't want
201 for (it = tree[rc].pGroups.begin(); it != tree[rc].pGroups.end(); it++) {
202 if (inUsersGroups(it->first, g) != true) { tree[rc].pGroups.erase(it->first); }
205 //set parsimony groups to left child
206 map<string,int> parsimony = tree[lc].pGroups;
210 //look at right child groups and update maxPars if right child has something higher for that group.
211 for(it=tree[rc].pGroups.begin();it!=tree[rc].pGroups.end();it++){
212 it2 = parsimony.find(it->first);
213 if (it2 != parsimony.end()) {
214 parsimony[it->first]++;
216 parsimony[it->first] = 1;
219 if(parsimony[it->first] > maxPars){
220 maxPars = parsimony[it->first];
224 // this is true if right child had a greater parsimony for a certain group
226 //erase all the groups that are only 1 because you found something with 2.
227 for(it=parsimony.begin();it!=parsimony.end();it++){
229 parsimony.erase(it->first);
232 for(it=parsimony.begin();it!=parsimony.end();it++){
233 parsimony[it->first] = 1;
239 catch(exception& e) {
240 errorOut(e, "Tree", "mergeUserGroups");
246 /**************************************************************************************************/
248 map<string,int> Tree::mergeGcounts(int position) {
250 map<string,int>::iterator pos;
252 int lc = tree[position].getLChild();
253 int rc = tree[position].getRChild();
255 map<string,int> sum = tree[lc].pcount;
257 for(it=tree[rc].pcount.begin();it!=tree[rc].pcount.end();it++){
258 sum[it->first] += it->second;
262 catch(exception& e) {
263 errorOut(e, "Tree", "mergeGcounts");
267 /**************************************************************************************************/
269 void Tree::randomLabels(vector<string> g) {
272 for(int i = 0; i < numLeaves; i++){
274 //get random index to switch with
275 z = int((float)(i+1) * (float)(rand()) / ((float)RAND_MAX+1.0));
277 //you only want to randomize the nodes that are from a group the user wants analyzed, so
278 //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.
281 treez = inUsersGroups(tree[z].getGroup(), g);
282 treei = inUsersGroups(tree[i].getGroup(), g);
284 if ((treez == true) && (treei == true)) {
285 //switches node i and node z's info.
286 map<string,int> lib_hold = tree[z].pGroups;
287 tree[z].pGroups = (tree[i].pGroups);
288 tree[i].pGroups = (lib_hold);
290 string zgroup = tree[z].getGroup();
291 tree[z].setGroup(tree[i].getGroup());
292 tree[i].setGroup(zgroup);
294 string zname = tree[z].getName();
295 tree[z].setName(tree[i].getName());
296 tree[i].setName(zname);
298 map<string,int> gcount_hold = tree[z].pcount;
299 tree[z].pcount = (tree[i].pcount);
300 tree[i].pcount = (gcount_hold);
304 catch(exception& e) {
305 errorOut(e, "Tree", "randomLabels");
309 /**************************************************************************************************/
311 void Tree::randomLabels(string groupA, string groupB) {
313 int numSeqsA = globaldata->gTreemap->seqsPerGroup[groupA];
314 int numSeqsB = globaldata->gTreemap->seqsPerGroup[groupB];
316 vector<string> randomGroups(numSeqsA+numSeqsB, groupA);
317 for(int i=numSeqsA;i<randomGroups.size();i++){
318 randomGroups[i] = groupB;
320 random_shuffle(randomGroups.begin(), randomGroups.end());
322 int randomCounter = 0;
323 for(int i=0;i<numLeaves;i++){
324 if(tree[i].getGroup() == groupA || tree[i].getGroup() == groupB){
325 tree[i].setGroup(randomGroups[randomCounter]);
326 tree[i].pcount.clear();
327 tree[i].pcount[randomGroups[randomCounter]] = 1;
328 tree[i].pGroups.clear();
329 tree[i].pGroups[randomGroups[randomCounter]] = 1;
334 catch(exception& e) {
335 errorOut(e, "Tree", "randomLabels");
339 /**************************************************************************************************/
340 void Tree::randomBlengths() {
342 for(int i=numNodes-1;i>=0;i--){
343 int z = int((float)(i+1) * (float)(rand()) / ((float)RAND_MAX+1.0));
345 float bl_hold = tree[z].getBranchLength();
346 tree[z].setBranchLength(tree[i].getBranchLength());
347 tree[i].setBranchLength(bl_hold);
350 catch(exception& e) {
351 errorOut(e, "Tree", "randomBlengths");
355 /*************************************************************************************************/
356 void Tree::assembleRandomUnifracTree(vector<string> g) {
360 /*************************************************************************************************/
361 void Tree::assembleRandomUnifracTree(string groupA, string groupB) {
362 randomLabels(groupA, groupB);
366 /*************************************************************************************************/
367 //for now it's just random topology but may become random labels as well later that why this is such a simple function now...
368 void Tree::assembleRandomTree() {
372 /**************************************************************************************************/
374 void Tree::randomTopology() {
376 for(int i=0;i<numNodes;i++){
377 tree[i].setParent(-1);
379 for(int i=numLeaves;i<numNodes;i++){
380 tree[i].setChildren(-1, -1);
383 for(int i=numLeaves;i<numNodes;i++){
385 int rnd_index1, rnd_index2;
387 rnd_index1 = (int)(((double)rand() / (double) RAND_MAX)*i);
388 if(tree[rnd_index1].getParent() == -1){escape = 1;}
393 rnd_index2 = (int)(((double)rand() / (double) RAND_MAX)*i);
394 if(rnd_index2 != rnd_index1 && tree[rnd_index2].getParent() == -1){
399 tree[i].setChildren(rnd_index1,rnd_index2);
400 tree[i].setParent(-1);
401 tree[rnd_index1].setParent(i);
402 tree[rnd_index2].setParent(i);
405 catch(exception& e) {
406 errorOut(e, "Tree", "randomTopology");
410 /*****************************************************************/
411 void Tree::print(ostream& out) {
413 int root = findRoot();
414 printBranch(root, out, "branch");
417 catch(exception& e) {
418 errorOut(e, "Tree", "print");
422 /*****************************************************************/
423 void Tree::printForBoot(ostream& out) {
425 int root = findRoot();
426 printBranch(root, out, "boot");
429 catch(exception& e) {
430 errorOut(e, "Tree", "printForBoot");
435 /*****************************************************************/
436 // This prints out the tree in Newick form.
437 void Tree::createNewickFile(string f) {
439 int root = findRoot();
440 //filename = getRootName(globaldata->getTreeFile()) + "newick";
443 openOutputFile(filename, out);
445 printBranch(root, out, "branch");
447 // you are at the end of the tree
451 catch(exception& e) {
452 errorOut(e, "Tree", "createNewickFile");
457 /*****************************************************************/
458 //This function finds the index of the root node.
460 int Tree::findRoot() {
462 for (int i = 0; i < numNodes; i++) {
464 if (tree[i].getParent() == -1) { return i; }
465 //cout << "i = " << i << endl;
466 //cout << "i's parent = " << tree[i].getParent() << endl;
470 catch(exception& e) {
471 errorOut(e, "Tree", "findRoot");
476 /*****************************************************************/
477 void Tree::printBranch(int node, ostream& out, string mode) {
480 // you are not a leaf
481 if (tree[node].getLChild() != -1) {
483 printBranch(tree[node].getLChild(), out, mode);
485 printBranch(tree[node].getRChild(), out, mode);
487 if (mode == "branch") {
488 //if there is a branch length then print it
489 if (tree[node].getBranchLength() != -1) {
490 out << ":" << tree[node].getBranchLength();
492 }else if (mode == "boot") {
493 //if there is a label then print it
494 if (tree[node].getLabel() != -1) {
495 out << tree[node].getLabel();
498 }else { //you are a leaf
499 out << tree[node].getGroup();
500 if (mode == "branch") {
501 //if there is a branch length then print it
502 if (tree[node].getBranchLength() != -1) {
503 out << ":" << tree[node].getBranchLength();
505 }else if (mode == "boot") {
506 //if there is a label then print it
507 if (tree[node].getLabel() != -1) {
508 out << tree[node].getLabel();
514 catch(exception& e) {
515 errorOut(e, "Tree", "printBranch");
520 /*****************************************************************/
522 void Tree::printTree() {
524 for(int i=0;i<numNodes;i++){
531 /*****************************************************************/
533 void Tree::parseTreeFile() {
535 //only takes names from the first tree and assumes that all trees use the same names.
537 string filename = globaldata->getTreeFile();
539 openInputFile(filename, filehandle);
543 //ifyou are not a nexus file
544 if((c = filehandle.peek()) != '#') {
545 while((c = filehandle.peek()) != ';') {
546 while ((c = filehandle.peek()) != ';') {
554 if((c == '(') && (comment != 1)){ break; }
558 readTreeString(filehandle);
560 //ifyou are a nexus file
561 }else if((c = filehandle.peek()) == '#') {
565 while(holder != "translate" && holder != "Translate"){
566 if(holder == "[" || holder == "[!"){
572 filehandle >> holder;
574 //ifthere is no translate then you must read tree string otherwise use translate to get names
575 if(holder == "tree" && comment != 1){
576 //pass over the "tree rep.6878900 = "
577 while (((c = filehandle.get()) != '(') && ((c = filehandle.peek()) != EOF)) {;}
579 if(c == EOF) { break; }
580 filehandle.putback(c); //put back first ( of tree.
581 readTreeString(filehandle);
586 //use nexus translation rather than parsing tree to save time
587 if((holder == "translate") || (holder == "Translate")) {
589 string number, name, h;
590 h = ""; // so it enters the loop the first time
591 while((h != ";") && (number != ";")) {
592 filehandle >> number;
595 //c = , until done with translation then c = ;
596 h = name.substr(name.length()-1, name.length());
597 name.erase(name.end()-1); //erase the comma
598 globaldata->Treenames.push_back(number);
600 if(number == ";") { globaldata->Treenames.pop_back(); } //in case ';' from translation is on next line instead of next to last name
605 catch(exception& e) {
606 errorOut(e, "Tree", "parseTreeFile");
610 /*******************************************************/
612 /*******************************************************/
613 void Tree::readTreeString(ifstream& filehandle) {
618 while((c = filehandle.peek()) != ';') {
620 //cout << " at beginning of while " << k << endl;
622 //to pass over labels in trees
624 while((c!=',') && (c != -1) && (c!= ':') && (c!=';')){ c=filehandle.get(); }
625 filehandle.putback(c);
627 if(c == ';') { break; }
629 if((c != '(') && (c != ')') && (c != ',') && (c != ':') && (c != '\n') && (c != '\t') && (c != 32)) { //32 is space
631 c = filehandle.get();
634 while ((c != '(') && (c != ')') && (c != ',') && (c != ':') && (c != '\n') && (c != 32) && (c != '\t')) {
636 c = filehandle.get();
638 //cout << " in name while " << k << endl;
641 //cout << "name = " << name << endl;
642 globaldata->Treenames.push_back(name);
643 filehandle.putback(c);
645 //cout << " after putback" << k << endl;
648 if(c == ':') { //read until you reach the end of the branch length
649 while ((c != '(') && (c != ')') && (c != ',') && (c != ';') && (c != '\n') && (c != '\t') && (c != 32)) {
650 c = filehandle.get();
652 //cout << " in branch while " << k << endl;
654 filehandle.putback(c);
657 c = filehandle.get();
658 if(c == ';') { break; }
659 if(c == ')') { filehandle.putback(c); }
665 catch(exception& e) {
666 errorOut(e, "Tree", "readTreeString");
671 /*******************************************************/
673 /*******************************************************/