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 data.push_back(percent);
72 }else { data.push_back(0.0); }
78 m->errorOut(e, "PhyloDiversity", "getValues");
82 /**************************************************************************************************/
83 void PhyloDiversity::setTotalGroupBranchLengths(Tree* t) {
88 //initialize group totals
89 for (int i=0; i<globaldata->Groups.size(); i++) { groupTotals[globaldata->Groups[i]] = 0.0; }
92 /********************************************************/
93 //calculate a D value for each group
94 for(int v=0;v<t->getNumLeaves();v++){
96 //is this node from a sequence which is in one of the users groups
97 if (inUsersGroups(t->tree[v].getGroup(), globaldata->Groups) == true) {
99 //calc the branch length
103 while(t->tree[index].getParent() != -1){ //while you aren't at root
106 if(t->tree[index].getBranchLength() != -1){
107 sum += abs(t->tree[index].getBranchLength());
109 index = t->tree[index].getParent();
112 //get last breanch length added
113 if(t->tree[index].getBranchLength() != -1){
114 sum += abs(t->tree[index].getBranchLength());
117 //account for the names file
118 vector<string> groups = t->tree[v].getGroup();
119 for (int j = 0; j < groups.size(); j++) {
120 int numSeqsInGroupJ = 0;
121 map<string, int>::iterator it;
122 it = t->tree[v].pcount.find(groups[j]);
123 if (it != t->tree[v].pcount.end()) { //this leaf node contains seqs from group j
124 numSeqsInGroupJ = it->second;
127 //add branch length to total for group
128 groupTotals[groups[j]] += (numSeqsInGroupJ * sum);
134 catch(exception& e) {
135 m->errorOut(e, "PhyloDiversity", "setTotalGroupBranchLengths");
139 /**************************************************************************************************/