5 * Created by Sarah Westcott on 1/22/09.
6 * Copyright 2009 Schloss Lab UMASS Amherst. All rights reserved.
13 /*****************************************************************/
17 globaldata = GlobalData::getInstance();
18 numLeaves = globaldata->gTreemap->getNumSeqs();
19 numNodes = 2*numLeaves - 1;
21 tree.resize(numNodes);
23 //initialize tree with correct number of nodes, name and group info.
24 for (int i = 0; i < numNodes; i++) {
25 //initialize leaf nodes
26 if (i <= (numLeaves-1)) {
27 tree[i].setName(globaldata->gTreemap->namesOfSeqs[i]);
28 tree[i].setGroup(globaldata->gTreemap->getGroup(globaldata->gTreemap->namesOfSeqs[i]));
29 //set pcount and pGroup for groupname to 1.
30 tree[i].pcount[globaldata->gTreemap->getGroup(globaldata->gTreemap->namesOfSeqs[i])] = 1;
31 tree[i].pGroups[globaldata->gTreemap->getGroup(globaldata->gTreemap->namesOfSeqs[i])] = 1;
32 //Treemap knows name, group and index to speed up search
33 globaldata->gTreemap->setIndex(globaldata->gTreemap->namesOfSeqs[i], i);
35 //intialize non leaf nodes
36 }else if (i > (numLeaves-1)) {
43 cout << "Standard Error: " << e.what() << " has occurred in the Tree class Function Tree. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
47 cout << "An unknown error has occurred in the Tree class function Tree. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
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 cout << "Standard Error: " << e.what() << " has occurred in the Tree class Function getIndex. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
66 cout << "An unknown error has occurred in the Tree class function getIndex. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
70 /*****************************************************************/
72 void Tree::setIndex(string searchName, int index) {
74 //set index in treemap
75 globaldata->gTreemap->setIndex(searchName, index);
78 cout << "Standard Error: " << e.what() << " has occurred in the Tree class Function setIndex. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
82 cout << "An unknown error has occurred in the Tree class function setIndex. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
86 /*****************************************************************/
87 void Tree::assembleTree() {
89 //build the pGroups in non leaf nodes to be used in the parsimony calcs.
90 for (int i = numLeaves; i < numNodes; i++) {
91 tree[i].pGroups = (mergeGroups(i));
92 tree[i].pcount = (mergeGcounts(i));
96 cout << "Standard Error: " << e.what() << " has occurred in the Tree class Function assembleTree. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
100 cout << "An unknown error has occurred in the Tree class function assembleTree. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
104 /*****************************************************************/
105 void Tree::getCopy(Tree* copy) {
108 //for each node in the tree copy its info
109 for (int i = 0; i < numNodes; i++) {
111 tree[i].setName(copy->tree[i].getName());
114 tree[i].setGroup(copy->tree[i].getGroup());
117 tree[i].setBranchLength(copy->tree[i].getBranchLength());
120 tree[i].setParent(copy->tree[i].getParent());
123 tree[i].setChildren(copy->tree[i].getLChild(), copy->tree[i].getRChild());
125 //copy index in node and tmap
126 tree[i].setIndex(copy->tree[i].getIndex());
127 setIndex(copy->tree[i].getName(), getIndex(copy->tree[i].getName()));
130 tree[i].pGroups = copy->tree[i].pGroups;
133 tree[i].pcount = copy->tree[i].pcount;
136 catch(exception& e) {
137 cout << "Standard Error: " << e.what() << " has occurred in the Tree class Function getCopy. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
141 cout << "An unknown error has occurred in the Tree class function getCopy. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
145 /*****************************************************************/
146 //returns a map with a groupname and the number of times that group was seen in the children
147 //for instance if your children are white and black then it would return a map with 2 entries
148 // p[white] = 1 and p[black] = 1. Now go up a level and merge that with a node who has p[white] = 1
149 //and you get p[white] = 2, p[black] = 1, but you erase the p[black] because you have a p value higher than 1.
151 map<string, int> Tree::mergeGroups(int i) {
153 int lc = tree[i].getLChild();
154 int rc = tree[i].getRChild();
156 //set parsimony groups to left child
157 map<string,int> parsimony = tree[lc].pGroups;
161 //look at right child groups and update maxPars if right child has something higher for that group.
162 for(it=tree[rc].pGroups.begin();it!=tree[rc].pGroups.end();it++){
163 it2 = parsimony.find(it->first);
164 if (it2 != parsimony.end()) {
165 parsimony[it->first]++;
167 parsimony[it->first] = 1;
170 if(parsimony[it->first] > maxPars){
171 maxPars = parsimony[it->first];
175 // this is true if right child had a greater parsimony for a certain group
177 //erase all the groups that are only 1 because you found something with 2.
178 for(it=parsimony.begin();it!=parsimony.end();it++){
180 parsimony.erase(it->first);
184 //set one remaining groups to 1
185 //so with our above example p[white] = 2 would be left and it would become p[white] = 1
186 for(it=parsimony.begin();it!=parsimony.end();it++){
187 parsimony[it->first] = 1;
194 catch(exception& e) {
195 cout << "Standard Error: " << e.what() << " has occurred in the Tree class Function mergeGroups. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
199 cout << "An unknown error has occurred in the Tree class function mergeGroups. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
203 /*****************************************************************/
204 //returns a map with a groupname and the number of times that group was seen in the children
205 //for instance if your children are white and black then it would return a map with 2 entries
206 // p[white] = 1 and p[black] = 1. Now go up a level and merge that with a node who has p[white] = 1
207 //and you get p[white] = 2, p[black] = 1, but you erase the p[black] because you have a p value higher than 1.
209 map<string, int> Tree::mergeUserGroups(int i, vector<string> g) {
212 int lc = tree[i].getLChild();
213 int rc = tree[i].getRChild();
215 //loop through nodes groups removing the ones the user doesn't want
216 for (it = tree[lc].pGroups.begin(); it != tree[lc].pGroups.end(); it++) {
217 if (inUsersGroups(it->first, g) != true) { tree[lc].pGroups.erase(it->first); }
220 //loop through nodes groups removing the ones the user doesn't want
221 for (it = tree[rc].pGroups.begin(); it != tree[rc].pGroups.end(); it++) {
222 if (inUsersGroups(it->first, g) != true) { tree[rc].pGroups.erase(it->first); }
225 //set parsimony groups to left child
226 map<string,int> parsimony = tree[lc].pGroups;
230 //look at right child groups and update maxPars if right child has something higher for that group.
231 for(it=tree[rc].pGroups.begin();it!=tree[rc].pGroups.end();it++){
232 it2 = parsimony.find(it->first);
233 if (it2 != parsimony.end()) {
234 parsimony[it->first]++;
236 parsimony[it->first] = 1;
239 if(parsimony[it->first] > maxPars){
240 maxPars = parsimony[it->first];
244 // this is true if right child had a greater parsimony for a certain group
246 //erase all the groups that are only 1 because you found something with 2.
247 for(it=parsimony.begin();it!=parsimony.end();it++){
249 parsimony.erase(it->first);
253 //set one remaining groups to 1
254 //so with our above example p[white] = 2 would be left and it would become p[white] = 1
255 for(it=parsimony.begin();it!=parsimony.end();it++){
256 parsimony[it->first] = 1;
263 catch(exception& e) {
264 cout << "Standard Error: " << e.what() << " has occurred in the Tree class Function mergeGroups. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
268 cout << "An unknown error has occurred in the Tree class function mergeGroups. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
274 /**************************************************************************************************/
276 map<string,int> Tree::mergeGcounts(int position) {
278 map<string,int>::iterator pos;
280 int lc = tree[position].getLChild();
281 int rc = tree[position].getRChild();
283 map<string,int> sum = tree[lc].pcount;
285 for(it=tree[rc].pcount.begin();it!=tree[rc].pcount.end();it++){
286 sum[it->first] += it->second;
290 catch(exception& e) {
291 cout << "Standard Error: " << e.what() << " has occurred in the Tree class Function mergeGcounts. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
295 cout << "An unknown error has occurred in the Tree class function mergeGcounts. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
299 /**************************************************************************************************/
301 void Tree::randomLabels(vector<string> g) {
304 for(int i = 0; i < numLeaves; i++){
306 //get random index to switch with
307 z = int((float)(i+1) * (float)(rand()) / ((float)RAND_MAX+1.0));
309 //you only want to randomize the nodes that are from a group the user wants analyzed, so
310 //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.
313 treez = inUsersGroups(tree[z].getGroup(), g);
314 treei = inUsersGroups(tree[i].getGroup(), g);
316 if ((treez == true) && (treei == true)) {
317 //switches node i and node z's info.
318 map<string,int> lib_hold = tree[z].pGroups;
319 tree[z].pGroups = (tree[i].pGroups);
320 tree[i].pGroups = (lib_hold);
322 string zgroup = tree[z].getGroup();
323 tree[z].setGroup(tree[i].getGroup());
324 tree[i].setGroup(zgroup);
326 string zname = tree[z].getName();
327 tree[z].setName(tree[i].getName());
328 tree[i].setName(zname);
330 map<string,int> gcount_hold = tree[z].pcount;
331 tree[z].pcount = (tree[i].pcount);
332 tree[i].pcount = (gcount_hold);
336 catch(exception& e) {
337 cout << "Standard Error: " << e.what() << " has occurred in the Tree class Function randomLabels. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
341 cout << "An unknown error has occurred in the Tree class function randomLabels. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
345 /**************************************************************************************************/
347 void Tree::randomLabels(string groupA, string groupB) {
349 int numSeqsA = globaldata->gTreemap->seqsPerGroup[groupA];
350 int numSeqsB = globaldata->gTreemap->seqsPerGroup[groupB];
352 vector<string> randomGroups(numSeqsA+numSeqsB, groupA);
353 for(int i=numSeqsA;i<randomGroups.size();i++){
354 randomGroups[i] = groupB;
356 random_shuffle(randomGroups.begin(), randomGroups.end());
358 int randomCounter = 0;
359 for(int i=0;i<numLeaves;i++){
360 if(tree[i].getGroup() == groupA || tree[i].getGroup() == groupB){
361 tree[i].setGroup(randomGroups[randomCounter]);
362 tree[i].pcount.clear();
363 tree[i].pcount[randomGroups[randomCounter]] = 1;
364 tree[i].pGroups.clear();
365 tree[i].pGroups[randomGroups[randomCounter]] = 1;
370 catch(exception& e) {
371 cout << "Standard Error: " << e.what() << " has occurred in the Tree class Function randomLabels. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
375 cout << "An unknown error has occurred in the Tree class function randomLabels. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
379 /**************************************************************************************************/
380 void Tree::randomBlengths() {
382 for(int i=numNodes-1;i>=0;i--){
383 int z = int((float)(i+1) * (float)(rand()) / ((float)RAND_MAX+1.0));
385 float bl_hold = tree[z].getBranchLength();
386 tree[z].setBranchLength(tree[i].getBranchLength());
387 tree[i].setBranchLength(bl_hold);
390 catch(exception& e) {
391 cout << "Standard Error: " << e.what() << " has occurred in the Tree class Function randomBlengths. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
395 cout << "An unknown error has occurred in the Tree class function randomBlengths. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
399 /*************************************************************************************************/
400 void Tree::assembleRandomUnifracTree(vector<string> g) {
404 /*************************************************************************************************/
405 void Tree::assembleRandomUnifracTree(string groupA, string groupB) {
406 randomLabels(groupA, groupB);
410 /*************************************************************************************************/
411 //for now it's just random topology but may become random labels as well later that why this is such a simple function now...
412 void Tree::assembleRandomTree() {
416 /**************************************************************************************************/
418 void Tree::randomTopology() {
420 for(int i=0;i<numNodes;i++){
421 tree[i].setParent(-1);
423 for(int i=numLeaves;i<numNodes;i++){
424 tree[i].setChildren(-1, -1);
427 for(int i=numLeaves;i<numNodes;i++){
429 int rnd_index1, rnd_index2;
431 rnd_index1 = (int)(((double)rand() / (double) RAND_MAX)*i);
432 if(tree[rnd_index1].getParent() == -1){escape = 1;}
437 rnd_index2 = (int)(((double)rand() / (double) RAND_MAX)*i);
438 if(rnd_index2 != rnd_index1 && tree[rnd_index2].getParent() == -1){
443 tree[i].setChildren(rnd_index1,rnd_index2);
444 tree[i].setParent(-1);
445 tree[rnd_index1].setParent(i);
446 tree[rnd_index2].setParent(i);
449 catch(exception& e) {
450 cout << "Standard Error: " << e.what() << " has occurred in the Tree class Function randomTopology. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
454 cout << "An unknown error has occurred in the Tree class function randomTopology. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
459 /*****************************************************************/
460 // This prints out the tree in Newick form.
461 void Tree::createNewickFile(string f) {
463 int root = findRoot();
464 //filename = getRootName(globaldata->getTreeFile()) + "newick";
466 openOutputFile(filename, out);
470 // you are at the end of the tree
474 catch(exception& e) {
475 cout << "Standard Error: " << e.what() << " has occurred in the Tree class Function createNewickFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
479 cout << "An unknown error has occurred in the Tree class function createNewickFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
484 /*****************************************************************/
485 //This function finds the index of the root node.
487 int Tree::findRoot() {
489 for (int i = 0; i < numNodes; i++) {
491 if (tree[i].getParent() == -1) { return i; }
495 catch(exception& e) {
496 cout << "Standard Error: " << e.what() << " has occurred in the Tree class Function findRoot. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
500 cout << "An unknown error has occurred in the Tree class function findRoot. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
505 /*****************************************************************/
506 void Tree::printBranch(int node) {
509 // you are not a leaf
510 if (tree[node].getLChild() != -1) {
512 printBranch(tree[node].getLChild());
514 printBranch(tree[node].getRChild());
516 }else { //you are a leaf
517 out << tree[node].getGroup() << ":" << tree[node].getBranchLength();
521 catch(exception& e) {
522 cout << "Standard Error: " << e.what() << " has occurred in the Tree class Function printBranch. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
526 cout << "An unknown error has occurred in the Tree class function printBranch. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
531 /*****************************************************************/
533 void Tree::printTree() {
535 for(int i=0;i<numNodes;i++){
542 /*****************************************************************/