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
93 Myresults = driver(t, namesOfGroupCombos, lines[process]->start, lines[process]->num, sums);
95 m->mothurOut("Merging results."); m->mothurOutEndLine();
97 //pass numSeqs to parent
100 string tempFile = outputDir + toString(getpid()) + ".weighted.results.temp";
102 m->openOutputFile(tempFile, out);
104 out << Myresults.size() << endl;
105 for (int i = 0; i < Myresults.size(); i++) { out << Myresults[i] << '\t'; } out << endl;
109 }else { m->mothurOut("unable to spawn the necessary processes."); m->mothurOutEndLine(); exit(0); }
112 results = driver(t, namesOfGroupCombos, lines[0]->start, lines[0]->num, sums);
114 //force parent to wait until all the processes are done
115 for (int i=0;i<(processors-1);i++) {
116 int temp = processIDS[i];
120 if (m->control_pressed) { return results; }
122 //get data created by processes
123 for (int i=0;i<(processors-1);i++) {
125 string s = outputDir + toString(processIDS[i]) + ".weighted.results.temp";
126 m->openInputFile(s, in);
131 in >> num; m->gobble(in);
133 if (m->control_pressed) { break; }
136 for (int j = 0; j < num; j++) {
138 results.push_back(w);
146 m->mothurOut("DONE."); m->mothurOutEndLine(); m->mothurOutEndLine();
151 catch(exception& e) {
152 m->errorOut(e, "Weighted", "createProcesses");
156 /**************************************************************************************************/
157 EstOutput Weighted::driver(Tree* t, vector< vector<string> > namesOfGroupCombos, int start, int num, vector<double>& sums) {
163 for (int h = start; h < (start+num); h++) {
165 if (m->control_pressed) { return results; }
167 //initialize weighted score
168 string groupA = namesOfGroupCombos[h][0];
169 string groupB = namesOfGroupCombos[h][1];
171 WScore[groupA+groupB] = 0.0;
172 D.push_back(0.0000); //initialize a spot in D for each combination
174 //adding the wieghted sums from group i
175 for (int j = 0; j < t->groupNodeInfo[groupA].size(); j++) { //the leaf nodes that have seqs from group i
176 map<string, int>::iterator it = t->tree[t->groupNodeInfo[groupA][j]].pcount.find(groupA);
177 int numSeqsInGroupI = it->second;
179 double weightedSum = ((numSeqsInGroupI * sums[t->groupNodeInfo[groupA][j]]) / (double)tmap->seqsPerGroup[groupA]);
181 D[count] += weightedSum;
184 //adding the wieghted sums from group l
185 for (int j = 0; j < t->groupNodeInfo[groupB].size(); j++) { //the leaf nodes that have seqs from group l
186 map<string, int>::iterator it = t->tree[t->groupNodeInfo[groupB][j]].pcount.find(groupB);
187 int numSeqsInGroupL = it->second;
189 double weightedSum = ((numSeqsInGroupL * sums[t->groupNodeInfo[groupB][j]]) / (double)tmap->seqsPerGroup[groupB]);
191 D[count] += weightedSum;
196 //calculate u for the group comb
197 int total = t->getNumNodes();
198 int twentyPercent = (total * 0.20);
199 for(int i=0;i<t->getNumNodes();i++){
200 for (int h = start; h < (start+num); h++) {
202 string groupA = namesOfGroupCombos[h][0];
203 string groupB = namesOfGroupCombos[h][1];
205 if (m->control_pressed) { return results; }
208 //does this node have descendants from groupA
209 it = t->tree[i].pcount.find(groupA);
210 //if it does u = # of its descendants with a certain group / total number in tree with a certain group
211 if (it != t->tree[i].pcount.end()) {
212 u = (double) t->tree[i].pcount[groupA] / (double) tmap->seqsPerGroup[groupA];
216 //does this node have descendants from group l
217 it = t->tree[i].pcount.find(groupB);
218 //if it does subtract their percentage from u
219 if (it != t->tree[i].pcount.end()) {
220 u -= (double) t->tree[i].pcount[groupB] / (double) tmap->seqsPerGroup[groupB];
223 u = abs(u * t->tree[i].getBranchLength());
225 //save groupcombs u value
226 WScore[(groupA+groupB)] += u;
229 if((i) % twentyPercent == 0){ m->mothurOut("Percentage complete: " + toString(int((i / (float)total) * 100.0))); m->mothurOutEndLine(); }
232 m->mothurOut("Percentage complete: 100"); m->mothurOutEndLine();
234 /********************************************************/
235 //calculate weighted score for the group combination
238 for (int h = start; h < (start+num); h++) {
239 UN = (WScore[namesOfGroupCombos[h][0]+namesOfGroupCombos[h][1]] / D[count]);
241 if (isnan(UN) || isinf(UN)) { UN = 0; }
242 results.push_back(UN);
248 catch(exception& e) {
249 m->errorOut(e, "Weighted", "driver");
253 /**************************************************************************************************/
254 EstOutput Weighted::getValues(Tree* t, string groupA, string groupB, vector<double>& sums) {
256 globaldata = GlobalData::getInstance();
258 data.clear(); //clear out old values
260 if (m->control_pressed) { return data; }
262 //initialize weighted score
263 WScore[(groupA+groupB)] = 0.0;
266 vector<string> groups; groups.push_back(groupA); groups.push_back(groupB);
268 //adding the wieghted sums from group i
269 for (int j = 0; j < t->groupNodeInfo[groups[0]].size(); j++) { //the leaf nodes that have seqs from group i
270 map<string, int>::iterator it = t->tree[t->groupNodeInfo[groups[0]][j]].pcount.find(groups[0]);
271 int numSeqsInGroupI = it->second;
273 double weightedSum = ((numSeqsInGroupI * sums[t->groupNodeInfo[groups[0]][j]]) / (double)tmap->seqsPerGroup[groups[0]]);
278 //adding the wieghted sums from group l
279 for (int j = 0; j < t->groupNodeInfo[groups[1]].size(); j++) { //the leaf nodes that have seqs from group l
280 map<string, int>::iterator it = t->tree[t->groupNodeInfo[groups[1]][j]].pcount.find(groups[1]);
281 int numSeqsInGroupL = it->second;
283 double weightedSum = ((numSeqsInGroupL * sums[t->groupNodeInfo[groups[1]][j]]) / (double)tmap->seqsPerGroup[groups[1]]);
288 //calculate u for the group comb
289 for(int i=0;i<t->getNumNodes();i++){
291 if (m->control_pressed) { return data; }
294 //does this node have descendants from groupA
295 it = t->tree[i].pcount.find(groupA);
296 //if it does u = # of its descendants with a certain group / total number in tree with a certain group
297 if (it != t->tree[i].pcount.end()) {
298 u = (double) t->tree[i].pcount[groupA] / (double) tmap->seqsPerGroup[groupA];
302 //does this node have descendants from group l
303 it = t->tree[i].pcount.find(groupB);
304 //if it does subtract their percentage from u
305 if (it != t->tree[i].pcount.end()) {
306 u -= (double) t->tree[i].pcount[groupB] / (double) tmap->seqsPerGroup[groupB];
309 u = abs(u * t->tree[i].getBranchLength());
311 //save groupcombs u value
312 WScore[(groupA+groupB)] += u;
315 /********************************************************/
317 //calculate weighted score for the group combination
319 UN = (WScore[(groupA+groupB)] / D);
321 if (isnan(UN) || isinf(UN)) { UN = 0; }
326 catch(exception& e) {
327 m->errorOut(e, "Weighted", "getValues");
331 /**************************************************************************************************/
332 vector<double> Weighted::getBranchLengthSums(Tree* t) {
337 for(int v=0;v<t->getNumLeaves();v++){
339 if (m->control_pressed) { return sums; }
344 //while you aren't at root
345 while(t->tree[index].getParent() != -1){
348 if(t->tree[index].getBranchLength() != -1){
349 sum += abs(t->tree[index].getBranchLength());
351 index = t->tree[index].getParent();
354 //get last breanch length added
355 if(t->tree[index].getBranchLength() != -1){
356 sum += abs(t->tree[index].getBranchLength());
364 catch(exception& e) {
365 m->errorOut(e, "Weighted", "getBranchLengthSums");
369 /**************************************************************************************************/