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, int p, string o) {
16 globaldata = GlobalData::getInstance();
18 data.clear(); //clear out old values
24 numGroups = globaldata->Groups.size();
26 vector<double> sums = getBranchLengthSums(t);
28 if (m->control_pressed) { return data; }
30 //calculate number of comparisons i.e. with groups A,B,C = AB, AC, BC = 3;
31 vector< vector<string> > namesOfGroupCombos;
32 for (int i=0; i<numGroups; i++) {
33 for (int l = i+1; l < numGroups; l++) {
34 //initialize weighted scores
35 //WScore[globaldata->Groups[i]+globaldata->Groups[l]] = 0.0;
36 vector<string> groups; groups.push_back(globaldata->Groups[i]); groups.push_back(globaldata->Groups[l]);
37 namesOfGroupCombos.push_back(groups);
41 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
43 data = driver(t, namesOfGroupCombos, 0, namesOfGroupCombos.size(), sums);
45 int numPairs = namesOfGroupCombos.size();
47 int numPairsPerProcessor = numPairs / processors;
49 for (int i = 0; i < processors; i++) {
50 int startPos = i * numPairsPerProcessor;
51 if(i == processors - 1){
52 numPairsPerProcessor = numPairs - i * numPairsPerProcessor;
54 lines.push_back(new linePair(startPos, numPairsPerProcessor));
57 data = createProcesses(t, namesOfGroupCombos, sums);
59 for (int i = 0; i < lines.size(); i++) { delete lines[i]; } lines.clear();
62 data = driver(t, namesOfGroupCombos, 0, namesOfGroupCombos.size(), sums);
68 m->errorOut(e, "Weighted", "getValues");
72 /**************************************************************************************************/
74 EstOutput Weighted::createProcesses(Tree* t, vector< vector<string> > namesOfGroupCombos, vector<double>& sums) {
76 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
79 vector<int> processIDS;
83 //loop through and create all the processes you want
84 while (process != processors) {
88 processIDS.push_back(pid); //create map from line number to pid so you can append files in correct order later
92 Myresults = driver(t, namesOfGroupCombos, lines[process]->start, lines[process]->num, sums);
94 if (m->control_pressed) { exit(0); }
96 m->mothurOut("Merging results."); m->mothurOutEndLine();
98 //pass numSeqs to parent
100 string tempFile = outputDir + toString(getpid()) + ".weighted.results.temp";
101 m->openOutputFile(tempFile, out);
102 out << Myresults.size() << endl;
103 for (int i = 0; i < Myresults.size(); i++) { out << Myresults[i] << '\t'; } out << endl;
107 }else { m->mothurOut("unable to spawn the necessary processes."); m->mothurOutEndLine(); exit(0); }
110 results = driver(t, namesOfGroupCombos, lines[0]->start, lines[0]->num, sums);
112 //force parent to wait until all the processes are done
113 for (int i=0;i<(processors-1);i++) {
114 int temp = processIDS[i];
118 if (m->control_pressed) { return results; }
120 //get data created by processes
121 for (int i=0;i<(processors-1);i++) {
123 string s = outputDir + toString(processIDS[i]) + ".weighted.results.temp";
124 m->openInputFile(s, in);
129 in >> num; m->gobble(in);
131 if (m->control_pressed) { break; }
134 for (int j = 0; j < num; j++) {
136 results.push_back(w);
144 m->mothurOut("DONE."); m->mothurOutEndLine(); m->mothurOutEndLine();
149 catch(exception& e) {
150 m->errorOut(e, "Weighted", "createProcesses");
154 /**************************************************************************************************/
155 EstOutput Weighted::driver(Tree* t, vector< vector<string> > namesOfGroupCombos, int start, int num, vector<double>& sums) {
157 globaldata = GlobalData::getInstance();
164 int twentyPercent = (total * 0.20);
166 for (int h = start; h < (start+num); h++) {
168 if (m->control_pressed) { return results; }
170 //initialize weighted score
171 string groupA = namesOfGroupCombos[h][0];
172 string groupB = namesOfGroupCombos[h][1];
174 WScore[groupA+groupB] = 0.0;
175 D.push_back(0.0000); //initialize a spot in D for each combination
177 //adding the wieghted sums from group i
178 for (int j = 0; j < t->groupNodeInfo[groupA].size(); j++) { //the leaf nodes that have seqs from group i
179 map<string, int>::iterator it = t->tree[t->groupNodeInfo[groupA][j]].pcount.find(groupA);
180 int numSeqsInGroupI = it->second;
182 double weightedSum = ((numSeqsInGroupI * sums[t->groupNodeInfo[groupA][j]]) / (double)tmap->seqsPerGroup[groupA]);
184 D[count] += weightedSum;
187 //adding the wieghted sums from group l
188 for (int j = 0; j < t->groupNodeInfo[groupB].size(); j++) { //the leaf nodes that have seqs from group l
189 map<string, int>::iterator it = t->tree[t->groupNodeInfo[groupB][j]].pcount.find(groupB);
190 int numSeqsInGroupL = it->second;
192 double weightedSum = ((numSeqsInGroupL * sums[t->groupNodeInfo[groupB][j]]) / (double)tmap->seqsPerGroup[groupB]);
194 D[count] += weightedSum;
199 if((count) % twentyPercent == 0){ m->mothurOut("Percentage complete: " + toString(int((count / (float)total) * 100.0))); m->mothurOutEndLine(); }
202 m->mothurOut("Percentage complete: 100"); m->mothurOutEndLine();
204 //calculate u for the group comb
205 for(int i=0;i<t->getNumNodes();i++){
206 for (int h = start; h < (start+num); h++) {
208 string groupA = namesOfGroupCombos[h][0];
209 string groupB = namesOfGroupCombos[h][1];
211 if (m->control_pressed) { return results; }
214 //does this node have descendants from groupA
215 it = t->tree[i].pcount.find(groupA);
216 //if it does u = # of its descendants with a certain group / total number in tree with a certain group
217 if (it != t->tree[i].pcount.end()) {
218 u = (double) t->tree[i].pcount[groupA] / (double) tmap->seqsPerGroup[groupA];
222 //does this node have descendants from group l
223 it = t->tree[i].pcount.find(groupB);
224 //if it does subtract their percentage from u
225 if (it != t->tree[i].pcount.end()) {
226 u -= (double) t->tree[i].pcount[groupB] / (double) tmap->seqsPerGroup[groupB];
229 u = abs(u * t->tree[i].getBranchLength());
231 //save groupcombs u value
232 WScore[(groupA+groupB)] += u;
236 /********************************************************/
238 //calculate weighted score for the group combination
241 for (int h = start; h < (start+num); h++) {
242 UN = (WScore[namesOfGroupCombos[h][0]+namesOfGroupCombos[h][1]] / D[count]);
244 if (isnan(UN) || isinf(UN)) { UN = 0; }
245 results.push_back(UN);
251 catch(exception& e) {
252 m->errorOut(e, "Weighted", "driver");
256 /**************************************************************************************************/
257 EstOutput Weighted::getValues(Tree* t, string groupA, string groupB, vector<double>& sums) {
259 globaldata = GlobalData::getInstance();
261 data.clear(); //clear out old values
263 if (m->control_pressed) { return data; }
265 //initialize weighted score
266 WScore[(groupA+groupB)] = 0.0;
269 vector<string> groups; groups.push_back(groupA); groups.push_back(groupB);
271 //adding the wieghted sums from group i
272 for (int j = 0; j < t->groupNodeInfo[groups[0]].size(); j++) { //the leaf nodes that have seqs from group i
273 map<string, int>::iterator it = t->tree[t->groupNodeInfo[groups[0]][j]].pcount.find(groups[0]);
274 int numSeqsInGroupI = it->second;
276 double weightedSum = ((numSeqsInGroupI * sums[t->groupNodeInfo[groups[0]][j]]) / (double)tmap->seqsPerGroup[groups[0]]);
281 //adding the wieghted sums from group l
282 for (int j = 0; j < t->groupNodeInfo[groups[1]].size(); j++) { //the leaf nodes that have seqs from group l
283 map<string, int>::iterator it = t->tree[t->groupNodeInfo[groups[1]][j]].pcount.find(groups[1]);
284 int numSeqsInGroupL = it->second;
286 double weightedSum = ((numSeqsInGroupL * sums[t->groupNodeInfo[groups[1]][j]]) / (double)tmap->seqsPerGroup[groups[1]]);
291 //calculate u for the group comb
292 for(int i=0;i<t->getNumNodes();i++){
294 if (m->control_pressed) { return data; }
297 //does this node have descendants from groupA
298 it = t->tree[i].pcount.find(groupA);
299 //if it does u = # of its descendants with a certain group / total number in tree with a certain group
300 if (it != t->tree[i].pcount.end()) {
301 u = (double) t->tree[i].pcount[groupA] / (double) tmap->seqsPerGroup[groupA];
305 //does this node have descendants from group l
306 it = t->tree[i].pcount.find(groupB);
307 //if it does subtract their percentage from u
308 if (it != t->tree[i].pcount.end()) {
309 u -= (double) t->tree[i].pcount[groupB] / (double) tmap->seqsPerGroup[groupB];
312 u = abs(u * t->tree[i].getBranchLength());
314 //save groupcombs u value
315 WScore[(groupA+groupB)] += u;
318 /********************************************************/
320 //calculate weighted score for the group combination
322 UN = (WScore[(groupA+groupB)] / D);
324 if (isnan(UN) || isinf(UN)) { UN = 0; }
329 catch(exception& e) {
330 m->errorOut(e, "Weighted", "getValues");
334 /**************************************************************************************************/
335 vector<double> Weighted::getBranchLengthSums(Tree* t) {
340 for(int v=0;v<t->getNumLeaves();v++){
342 if (m->control_pressed) { return sums; }
347 //while you aren't at root
348 while(t->tree[index].getParent() != -1){
351 if(t->tree[index].getBranchLength() != -1){
352 sum += abs(t->tree[index].getBranchLength());
354 index = t->tree[index].getParent();
357 //get last breanch length added
358 if(t->tree[index].getBranchLength() != -1){
359 sum += abs(t->tree[index].getBranchLength());
367 catch(exception& e) {
368 m->errorOut(e, "Weighted", "getBranchLengthSums");
372 /**************************************************************************************************/