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 /*****************************************************************/
54 /*****************************************************************/
55 int Tree::getIndex(string searchName) {
57 //Treemap knows name, group and index to speed up search
58 // getIndex function will return the vector index or -1 if seq is not found.
59 int index = globaldata->gTreemap->getIndex(searchName);
64 cout << "Standard Error: " << e.what() << " has occurred in the Tree class Function getIndex. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
68 cout << "An unknown error has occurred in the Tree class function getIndex. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
72 /*****************************************************************/
74 void Tree::setIndex(string searchName, int index) {
76 //set index in treemap
77 globaldata->gTreemap->setIndex(searchName, index);
80 cout << "Standard Error: " << e.what() << " has occurred in the Tree class Function setIndex. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
84 cout << "An unknown error has occurred in the Tree class function setIndex. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
88 /*****************************************************************/
89 void Tree::assembleTree() {
91 //build the pGroups in non leaf nodes to be used in the parsimony calcs.
92 for (int i = numLeaves; i < numNodes; i++) {
93 tree[i].pGroups = (mergeGroups(i));
94 tree[i].pcount = (mergeGcounts(i));
98 cout << "Standard Error: " << e.what() << " has occurred in the Tree class Function assembleTree. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
102 cout << "An unknown error has occurred in the Tree class function assembleTree. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
106 /*****************************************************************/
107 void Tree::getCopy(Tree* copy) {
110 //for each node in the tree copy its info
111 for (int i = 0; i < numNodes; i++) {
113 tree[i].setName(copy->tree[i].getName());
116 tree[i].setGroup(copy->tree[i].getGroup());
119 tree[i].setBranchLength(copy->tree[i].getBranchLength());
122 tree[i].setParent(copy->tree[i].getParent());
125 tree[i].setChildren(copy->tree[i].getLChild(), copy->tree[i].getRChild());
127 //copy index in node and tmap
128 tree[i].setIndex(copy->tree[i].getIndex());
129 setIndex(copy->tree[i].getName(), getIndex(copy->tree[i].getName()));
132 tree[i].pGroups = copy->tree[i].pGroups;
135 tree[i].pcount = copy->tree[i].pcount;
138 catch(exception& e) {
139 cout << "Standard Error: " << e.what() << " has occurred in the Tree class Function getCopy. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
143 cout << "An unknown error has occurred in the Tree class function getCopy. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
147 /*****************************************************************/
148 //returns a map with a groupname and the number of times that group was seen in the children
149 //for instance if your children are white and black then it would return a map with 2 entries
150 // p[white] = 1 and p[black] = 1. Now go up a level and merge that with a node who has p[white] = 1
151 //and you get p[white] = 2, p[black] = 1, but you erase the p[black] because you have a p value higher than 1.
153 map<string, int> Tree::mergeGroups(int i) {
155 int lc = tree[i].getLChild();
156 int rc = tree[i].getRChild();
158 //set parsimony groups to left child
159 map<string,int> parsimony = tree[lc].pGroups;
163 //look at right child groups and update maxPars if right child has something higher for that group.
164 for(it=tree[rc].pGroups.begin();it!=tree[rc].pGroups.end();it++){
165 it2 = parsimony.find(it->first);
166 if (it2 != parsimony.end()) {
167 parsimony[it->first]++;
169 parsimony[it->first] = 1;
172 if(parsimony[it->first] > maxPars){
173 maxPars = parsimony[it->first];
177 // this is true if right child had a greater parsimony for a certain group
179 //erase all the groups that are only 1 because you found something with 2.
180 for(it=parsimony.begin();it!=parsimony.end();it++){
182 parsimony.erase(it->first);
186 //set one remaining groups to 1
187 //so with our above example p[white] = 2 would be left and it would become p[white] = 1
188 for(it=parsimony.begin();it!=parsimony.end();it++){
189 parsimony[it->first] = 1;
196 catch(exception& e) {
197 cout << "Standard Error: " << e.what() << " has occurred in the Tree class Function mergeGroups. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
201 cout << "An unknown error has occurred in the Tree class function mergeGroups. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
205 /*****************************************************************/
206 //returns a map with a groupname and the number of times that group was seen in the children
207 //for instance if your children are white and black then it would return a map with 2 entries
208 // p[white] = 1 and p[black] = 1. Now go up a level and merge that with a node who has p[white] = 1
209 //and you get p[white] = 2, p[black] = 1, but you erase the p[black] because you have a p value higher than 1.
211 map<string, int> Tree::mergeUserGroups(int i, vector<string> g) {
214 int lc = tree[i].getLChild();
215 int rc = tree[i].getRChild();
217 //loop through nodes groups removing the ones the user doesn't want
218 for (it = tree[lc].pGroups.begin(); it != tree[lc].pGroups.end(); it++) {
219 if (inUsersGroups(it->first, g) != true) { tree[lc].pGroups.erase(it->first); }
222 //loop through nodes groups removing the ones the user doesn't want
223 for (it = tree[rc].pGroups.begin(); it != tree[rc].pGroups.end(); it++) {
224 if (inUsersGroups(it->first, g) != true) { tree[rc].pGroups.erase(it->first); }
227 //set parsimony groups to left child
228 map<string,int> parsimony = tree[lc].pGroups;
232 //look at right child groups and update maxPars if right child has something higher for that group.
233 for(it=tree[rc].pGroups.begin();it!=tree[rc].pGroups.end();it++){
234 it2 = parsimony.find(it->first);
235 if (it2 != parsimony.end()) {
236 parsimony[it->first]++;
238 parsimony[it->first] = 1;
241 if(parsimony[it->first] > maxPars){
242 maxPars = parsimony[it->first];
246 // this is true if right child had a greater parsimony for a certain group
248 //erase all the groups that are only 1 because you found something with 2.
249 for(it=parsimony.begin();it!=parsimony.end();it++){
251 parsimony.erase(it->first);
254 for(it=parsimony.begin();it!=parsimony.end();it++){
255 parsimony[it->first] = 1;
261 catch(exception& e) {
262 cout << "Standard Error: " << e.what() << " has occurred in the Tree class Function mergeGroups. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
266 cout << "An unknown error has occurred in the Tree class function mergeGroups. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
272 /**************************************************************************************************/
274 map<string,int> Tree::mergeGcounts(int position) {
276 map<string,int>::iterator pos;
278 int lc = tree[position].getLChild();
279 int rc = tree[position].getRChild();
281 map<string,int> sum = tree[lc].pcount;
283 for(it=tree[rc].pcount.begin();it!=tree[rc].pcount.end();it++){
284 sum[it->first] += it->second;
288 catch(exception& e) {
289 cout << "Standard Error: " << e.what() << " has occurred in the Tree class Function mergeGcounts. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
293 cout << "An unknown error has occurred in the Tree class function mergeGcounts. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
297 /**************************************************************************************************/
299 void Tree::randomLabels(vector<string> g) {
302 for(int i = 0; i < numLeaves; i++){
304 //get random index to switch with
305 z = int((float)(i+1) * (float)(rand()) / ((float)RAND_MAX+1.0));
307 //you only want to randomize the nodes that are from a group the user wants analyzed, so
308 //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.
311 treez = inUsersGroups(tree[z].getGroup(), g);
312 treei = inUsersGroups(tree[i].getGroup(), g);
314 if ((treez == true) && (treei == true)) {
315 //switches node i and node z's info.
316 map<string,int> lib_hold = tree[z].pGroups;
317 tree[z].pGroups = (tree[i].pGroups);
318 tree[i].pGroups = (lib_hold);
320 string zgroup = tree[z].getGroup();
321 tree[z].setGroup(tree[i].getGroup());
322 tree[i].setGroup(zgroup);
324 string zname = tree[z].getName();
325 tree[z].setName(tree[i].getName());
326 tree[i].setName(zname);
328 map<string,int> gcount_hold = tree[z].pcount;
329 tree[z].pcount = (tree[i].pcount);
330 tree[i].pcount = (gcount_hold);
334 catch(exception& e) {
335 cout << "Standard Error: " << e.what() << " has occurred in the Tree class Function randomLabels. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
339 cout << "An unknown error has occurred in the Tree class function randomLabels. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
343 /**************************************************************************************************/
345 void Tree::randomLabels(string groupA, string groupB) {
347 int numSeqsA = globaldata->gTreemap->seqsPerGroup[groupA];
348 int numSeqsB = globaldata->gTreemap->seqsPerGroup[groupB];
350 vector<string> randomGroups(numSeqsA+numSeqsB, groupA);
351 for(int i=numSeqsA;i<randomGroups.size();i++){
352 randomGroups[i] = groupB;
354 random_shuffle(randomGroups.begin(), randomGroups.end());
356 int randomCounter = 0;
357 for(int i=0;i<numLeaves;i++){
358 if(tree[i].getGroup() == groupA || tree[i].getGroup() == groupB){
359 tree[i].setGroup(randomGroups[randomCounter]);
360 tree[i].pcount.clear();
361 tree[i].pcount[randomGroups[randomCounter]] = 1;
362 tree[i].pGroups.clear();
363 tree[i].pGroups[randomGroups[randomCounter]] = 1;
368 catch(exception& e) {
369 cout << "Standard Error: " << e.what() << " has occurred in the Tree class Function randomLabels. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
373 cout << "An unknown error has occurred in the Tree class function randomLabels. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
377 /**************************************************************************************************/
378 void Tree::randomBlengths() {
380 for(int i=numNodes-1;i>=0;i--){
381 int z = int((float)(i+1) * (float)(rand()) / ((float)RAND_MAX+1.0));
383 float bl_hold = tree[z].getBranchLength();
384 tree[z].setBranchLength(tree[i].getBranchLength());
385 tree[i].setBranchLength(bl_hold);
388 catch(exception& e) {
389 cout << "Standard Error: " << e.what() << " has occurred in the Tree class Function randomBlengths. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
393 cout << "An unknown error has occurred in the Tree class function randomBlengths. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
397 /*************************************************************************************************/
398 void Tree::assembleRandomUnifracTree(vector<string> g) {
402 /*************************************************************************************************/
403 void Tree::assembleRandomUnifracTree(string groupA, string groupB) {
404 randomLabels(groupA, groupB);
408 /*************************************************************************************************/
409 //for now it's just random topology but may become random labels as well later that why this is such a simple function now...
410 void Tree::assembleRandomTree() {
414 /**************************************************************************************************/
416 void Tree::randomTopology() {
418 for(int i=0;i<numNodes;i++){
419 tree[i].setParent(-1);
421 for(int i=numLeaves;i<numNodes;i++){
422 tree[i].setChildren(-1, -1);
425 for(int i=numLeaves;i<numNodes;i++){
427 int rnd_index1, rnd_index2;
429 rnd_index1 = (int)(((double)rand() / (double) RAND_MAX)*i);
430 if(tree[rnd_index1].getParent() == -1){escape = 1;}
435 rnd_index2 = (int)(((double)rand() / (double) RAND_MAX)*i);
436 if(rnd_index2 != rnd_index1 && tree[rnd_index2].getParent() == -1){
441 tree[i].setChildren(rnd_index1,rnd_index2);
442 tree[i].setParent(-1);
443 tree[rnd_index1].setParent(i);
444 tree[rnd_index2].setParent(i);
447 catch(exception& e) {
448 cout << "Standard Error: " << e.what() << " has occurred in the Tree class Function randomTopology. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
452 cout << "An unknown error has occurred in the Tree class function randomTopology. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
457 /*****************************************************************/
458 // This prints out the tree in Newick form.
459 void Tree::createNewickFile(string f) {
461 int root = findRoot();
462 //filename = getRootName(globaldata->getTreeFile()) + "newick";
464 openOutputFile(filename, out);
468 // you are at the end of the tree
472 catch(exception& e) {
473 cout << "Standard Error: " << e.what() << " has occurred in the Tree class Function createNewickFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
477 cout << "An unknown error has occurred in the Tree class function createNewickFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
482 /*****************************************************************/
483 //This function finds the index of the root node.
485 int Tree::findRoot() {
487 for (int i = 0; i < numNodes; i++) {
489 if (tree[i].getParent() == -1) { return i; }
493 catch(exception& e) {
494 cout << "Standard Error: " << e.what() << " has occurred in the Tree class Function findRoot. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
498 cout << "An unknown error has occurred in the Tree class function findRoot. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
503 /*****************************************************************/
504 void Tree::printBranch(int node) {
507 // you are not a leaf
508 if (tree[node].getLChild() != -1) {
510 printBranch(tree[node].getLChild());
512 printBranch(tree[node].getRChild());
514 }else { //you are a leaf
515 out << tree[node].getGroup() << ":" << tree[node].getBranchLength();
519 catch(exception& e) {
520 cout << "Standard Error: " << e.what() << " has occurred in the Tree class Function printBranch. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
524 cout << "An unknown error has occurred in the Tree class function printBranch. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
529 /*****************************************************************/
531 void Tree::printTree() {
533 for(int i=0;i<numNodes;i++){
540 /*****************************************************************/