5 * Created by westcott on 4/30/10.
6 * Copyright 2010 Schloss Lab. All rights reserved.
10 #include "phylodiversity.h"
12 /**************************************************************************************************/
13 EstOutput PhyloDiversity::getValues(Tree* t, vector<int> treeNodes) {
16 map<string, float> DScore;
17 float totalLength = 0.0;
21 for (int i=0; i<globaldata->Groups.size(); i++) { DScore[globaldata->Groups[i]] = 0.0; }
23 /********************************************************/
24 //calculate a D value for each group
25 for(int v=0;v<treeNodes.size();v++){
27 if (m->control_pressed) { return data; }
29 //is this node from a sequence which is in one of the users groups
30 if (inUsersGroups(t->tree[treeNodes[v]].getGroup(), globaldata->Groups) == true) {
32 //calc the branch length
33 //while you aren't at root
35 int index = treeNodes[v];
37 while(t->tree[index].getParent() != -1){
40 if(t->tree[index].getBranchLength() != -1){
41 sum += abs(t->tree[index].getBranchLength());
43 index = t->tree[index].getParent();
46 //get last breanch length added
47 if(t->tree[index].getBranchLength() != -1){
48 sum += abs(t->tree[index].getBranchLength());
51 //for each group in the groups update the total branch length accounting for the names file
52 vector<string> groups = t->tree[treeNodes[v]].getGroup();
53 for (int j = 0; j < groups.size(); j++) {
54 int numSeqsInGroupJ = 0;
55 map<string, int>::iterator it;
56 it = t->tree[treeNodes[v]].pcount.find(groups[j]);
57 if (it != t->tree[treeNodes[v]].pcount.end()) { //this leaf node contains seqs from group j
58 numSeqsInGroupJ = it->second;
61 //add branch length to total for group
62 DScore[groups[j]] += (numSeqsInGroupJ * sum);
68 for (int i=0; i<globaldata->Groups.size(); i++) {
69 //if (groupTotals[globaldata->Groups[i]] != 0.0) { //avoid divide by zero error
70 //float percent = DScore[globaldata->Groups[i]] / groupTotals[globaldata->Groups[i]];
71 float percent = DScore[globaldata->Groups[i]];
72 data.push_back(percent);
73 //}else { data.push_back(0.0); }
79 m->errorOut(e, "PhyloDiversity", "getValues");
83 /**************************************************************************************************/
84 void PhyloDiversity::setTotalGroupBranchLengths(Tree* t) {
89 //initialize group totals
90 for (int i=0; i<globaldata->Groups.size(); i++) { groupTotals[globaldata->Groups[i]] = 0.0; }
93 /********************************************************/
94 //calculate a D value for each group
95 for(int v=0;v<t->getNumLeaves();v++){
97 //is this node from a sequence which is in one of the users groups
98 if (inUsersGroups(t->tree[v].getGroup(), globaldata->Groups) == true) {
100 //calc the branch length
104 while(t->tree[index].getParent() != -1){ //while you aren't at root
107 if(t->tree[index].getBranchLength() != -1){
108 sum += abs(t->tree[index].getBranchLength());
110 index = t->tree[index].getParent();
113 //get last breanch length added
114 if(t->tree[index].getBranchLength() != -1){
115 sum += abs(t->tree[index].getBranchLength());
118 //account for the names file
119 vector<string> groups = t->tree[v].getGroup();
120 for (int j = 0; j < groups.size(); j++) {
121 int numSeqsInGroupJ = 0;
122 map<string, int>::iterator it;
123 it = t->tree[v].pcount.find(groups[j]);
124 if (it != t->tree[v].pcount.end()) { //this leaf node contains seqs from group j
125 numSeqsInGroupJ = it->second;
128 //add branch length to total for group
129 groupTotals[groups[j]] += (numSeqsInGroupJ * sum);
135 catch(exception& e) {
136 m->errorOut(e, "PhyloDiversity", "setTotalGroupBranchLengths");
140 /**************************************************************************************************/