5 * Created by Sarah Westcott on 2/9/09.
6 * Copyright 2009 Schloss Lab UMASS Amherst. All rights reserved.
12 /**************************************************************************************************/
14 EstOutput Weighted::getValues(Tree* t) {
16 globaldata = GlobalData::getInstance();
20 numGroups = globaldata->Groups.size();
22 vector<double> sums = getBranchLengthSums(t);
24 if (m->control_pressed) { return data; }
26 //calculate number of comparisons i.e. with groups A,B,C = AB, AC, BC = 3;
28 for (int i=0; i<numGroups; i++) {
29 for (int l = i+1; l < numGroups; l++) {
30 //initialize weighted scores
31 WScore[globaldata->Groups[i]+globaldata->Groups[l]] = 0.0;
33 vector<string> groups; groups.push_back(globaldata->Groups[i]); groups.push_back(globaldata->Groups[l]);
35 D.push_back(0.0000); //initialize a spot in D for each combination
37 //adding the wieghted sums from group i
38 for (int j = 0; j < t->groupNodeInfo[groups[0]].size(); j++) { //the leaf nodes that have seqs from group i
39 map<string, int>::iterator it = t->tree[t->groupNodeInfo[groups[0]][j]].pcount.find(groups[0]);
40 int numSeqsInGroupI = it->second;
42 double weightedSum = ((numSeqsInGroupI * sums[t->groupNodeInfo[groups[0]][j]]) / (double)tmap->seqsPerGroup[groups[0]]);
44 D[count] += weightedSum;
47 //adding the wieghted sums from group l
48 for (int j = 0; j < t->groupNodeInfo[groups[1]].size(); j++) { //the leaf nodes that have seqs from group l
49 map<string, int>::iterator it = t->tree[t->groupNodeInfo[groups[1]][j]].pcount.find(groups[1]);
50 int numSeqsInGroupL = it->second;
52 double weightedSum = ((numSeqsInGroupL * sums[t->groupNodeInfo[groups[1]][j]]) / (double)tmap->seqsPerGroup[groups[1]]);
54 D[count] += weightedSum;
57 /********************************************************
58 //calculate a D value for each group combo
59 for(int v=0;v<t->getNumLeaves();v++){
61 if (m->control_pressed) { return data; }
63 //is this sum from a sequence which is in one of the users groups
64 if (m->inUsersGroups(t->tree[v].getGroup(), globaldata->Groups) == true) {
65 //is this sum from a sequence which is in this groupCombo
66 if (m->inUsersGroups(t->tree[v].getGroup(), groups)) {
67 int numSeqsInGroupI, numSeqsInGroupL;
69 map<string, int>::iterator it;
70 it = t->tree[v].pcount.find(groups[0]);
71 if (it != t->tree[v].pcount.end()) { //this leaf node contains seqs from group i
72 numSeqsInGroupI = it->second;
73 }else{ numSeqsInGroupI = 0; }
75 it = t->tree[v].pcount.find(groups[1]);
76 if (it != t->tree[v].pcount.end()) { //this leaf node contains seqs from group l
77 numSeqsInGroupL = it->second;
78 }else{ numSeqsInGroupL = 0; }
80 double weightedSum = ((numSeqsInGroupI * sum) / (double)tmap->seqsPerGroup[groups[0]]) + ((numSeqsInGroupL * sum) / (double)tmap->seqsPerGroup[groups[1]]);
82 //sum /= (double)tmap->seqsPerGroup[t->tree[v].getGroup()];
84 D[count] += weightedSum;
88 /*********************************************************/
93 data.clear(); //clear out old values
95 for(int i=0;i<t->getNumNodes();i++){
96 //calculate weighted score for each of the group comb i.e. with groups A,B,C = AB, AC, BC.
97 for (int b=0; b<numGroups; b++) {
98 for (int l = b+1; l < numGroups; l++) {
100 if (m->control_pressed) { return data; }
102 //calculate a u value for each combo
104 //does this node have descendants from group b-1
105 it = t->tree[i].pcount.find(globaldata->Groups[b]);
106 //if it does u = # of its descendants with a certain group / total number in tree with a certain group
107 if (it != t->tree[i].pcount.end()) {
108 u = (double) t->tree[i].pcount[globaldata->Groups[b]] / (double) tmap->seqsPerGroup[globaldata->Groups[b]];
111 //does this node have descendants from group l
112 it = t->tree[i].pcount.find(globaldata->Groups[l]);
113 //if it does subtract their percentage from u
114 if (it != t->tree[i].pcount.end()) {
115 u -= (double) t->tree[i].pcount[globaldata->Groups[l]] / (double) tmap->seqsPerGroup[globaldata->Groups[l]];
118 u = abs(u * t->tree[i].getBranchLength());
120 //save groupcombs u value
121 WScore[globaldata->Groups[b]+globaldata->Groups[l]] += u;
122 /*********************************************************/
127 //calculate weighted score for each group combination
130 for (int i=0; i<numGroups; i++) {
131 for (int l = i+1; l < numGroups; l++) {
132 UN = (WScore[globaldata->Groups[i]+globaldata->Groups[l]] / D[count]);
134 if (isnan(UN) || isinf(UN)) { UN = 0; }
141 catch(exception& e) {
142 m->errorOut(e, "Weighted", "getValues");
147 /**************************************************************************************************/
148 EstOutput Weighted::getValues(Tree* t, string groupA, string groupB) {
150 globaldata = GlobalData::getInstance();
152 data.clear(); //clear out old values
154 vector<double> sums = getBranchLengthSums(t);
156 if (m->control_pressed) { return data; }
158 //initialize weighted score
159 WScore[(groupA+groupB)] = 0.0;
162 vector<string> groups; groups.push_back(groupA); groups.push_back(groupB);
164 //adding the wieghted sums from groupA
165 for (int j = 0; j < t->groupNodeInfo[groupA].size(); j++) { //the leaf nodes that have seqs from group i
166 map<string, int>::iterator it = t->tree[j].pcount.find(groupA);
167 int numSeqsInGroupA = it->second;
169 double weightedSum = ((numSeqsInGroupA * sums[j]) / (double)tmap->seqsPerGroup[groupA]);
174 //adding the wieghted sums from groupB
175 for (int j = 0; j < t->groupNodeInfo[groupB].size(); j++) { //the leaf nodes that have seqs from group l
176 map<string, int>::iterator it = t->tree[j].pcount.find(groupB);
177 int numSeqsInGroupB = it->second;
179 double weightedSum = ((numSeqsInGroupB * sums[j]) / (double)tmap->seqsPerGroup[groupB]);
185 /********************************************************
186 //calculate a D value for the group combo
187 for(int v=0;v<t->getNumLeaves();v++){
188 if (m->control_pressed) { return data; }
193 //while you aren't at root
194 while(t->tree[index].getParent() != -1){
197 if(t->tree[index].getBranchLength() != -1){
198 sum += abs(t->tree[index].getBranchLength());
200 index = t->tree[index].getParent();
203 //get last breanch length added
204 if(t->tree[index].getBranchLength() != -1){
205 sum += abs(t->tree[index].getBranchLength());
208 if (m->inUsersGroups(t->tree[v].getGroup(), groups)) {
209 int numSeqsInGroupI, numSeqsInGroupL;
211 map<string, int>::iterator it;
212 it = t->tree[v].pcount.find(groups[0]);
213 if (it != t->tree[v].pcount.end()) { //this leaf node contains seqs from group i
214 numSeqsInGroupI = it->second;
215 }else{ numSeqsInGroupI = 0; }
217 it = t->tree[v].pcount.find(groups[1]);
218 if (it != t->tree[v].pcount.end()) { //this leaf node contains seqs from group l
219 numSeqsInGroupL = it->second;
220 }else{ numSeqsInGroupL = 0; }
222 double weightedSum = ((numSeqsInGroupI * sum) / (double)tmap->seqsPerGroup[groups[0]]) + ((numSeqsInGroupL * sum) / (double)tmap->seqsPerGroup[groups[1]]);
224 //sum /= (double)tmap->seqsPerGroup[t->tree[v].getGroup()];
229 /********************************************************/
231 //calculate u for the group comb
232 for(int i=0;i<t->getNumNodes();i++){
234 if (m->control_pressed) { return data; }
237 //does this node have descendants from groupA
238 it = t->tree[i].pcount.find(groupA);
239 //if it does u = # of its descendants with a certain group / total number in tree with a certain group
240 if (it != t->tree[i].pcount.end()) {
241 u = (double) t->tree[i].pcount[groupA] / (double) tmap->seqsPerGroup[groupA];
245 //does this node have descendants from group l
246 it = t->tree[i].pcount.find(groupB);
247 //if it does subtract their percentage from u
248 if (it != t->tree[i].pcount.end()) {
249 u -= (double) t->tree[i].pcount[groupB] / (double) tmap->seqsPerGroup[groupB];
252 u = abs(u * t->tree[i].getBranchLength());
254 //save groupcombs u value
255 WScore[(groupA+groupB)] += u;
258 /********************************************************/
260 //calculate weighted score for the group combination
262 UN = (WScore[(groupA+groupB)] / D);
264 if (isnan(UN) || isinf(UN)) { UN = 0; }
269 catch(exception& e) {
270 m->errorOut(e, "Weighted", "getValues");
274 /**************************************************************************************************/
275 vector<double> Weighted::getBranchLengthSums(Tree* t) {
280 for(int v=0;v<t->getNumLeaves();v++){
282 if (m->control_pressed) { return sums; }
287 //while you aren't at root
288 while(t->tree[index].getParent() != -1){
291 if(t->tree[index].getBranchLength() != -1){
292 sum += abs(t->tree[index].getBranchLength());
294 index = t->tree[index].getParent();
297 //get last breanch length added
298 if(t->tree[index].getBranchLength() != -1){
299 sum += abs(t->tree[index].getBranchLength());
307 catch(exception& e) {
308 m->errorOut(e, "Weighted", "getBranchLengthSums");
312 /**************************************************************************************************/