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 if (m->control_pressed) { return data; }
28 //calculate number of comparisons i.e. with groups A,B,C = AB, AC, BC = 3;
29 vector< vector<string> > namesOfGroupCombos;
30 for (int i=0; i<numGroups; i++) {
31 for (int l = 0; l < i; l++) {
32 //initialize weighted scores
33 //WScore[globaldata->Groups[i]+globaldata->Groups[l]] = 0.0;
34 vector<string> groups; groups.push_back(globaldata->Groups[i]); groups.push_back(globaldata->Groups[l]);
35 namesOfGroupCombos.push_back(groups);
39 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
41 data = driver(t, namesOfGroupCombos, 0, namesOfGroupCombos.size());
43 int numPairs = namesOfGroupCombos.size();
45 int numPairsPerProcessor = numPairs / processors;
47 for (int i = 0; i < processors; i++) {
48 int startPos = i * numPairsPerProcessor;
49 if(i == processors - 1){
50 numPairsPerProcessor = numPairs - i * numPairsPerProcessor;
52 lines.push_back(linePair(startPos, numPairsPerProcessor));
55 data = createProcesses(t, namesOfGroupCombos);
60 data = driver(t, namesOfGroupCombos, 0, namesOfGroupCombos.size());
66 m->errorOut(e, "Weighted", "getValues");
70 /**************************************************************************************************/
72 EstOutput Weighted::createProcesses(Tree* t, vector< vector<string> > namesOfGroupCombos) {
74 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
77 vector<int> processIDS;
81 //loop through and create all the processes you want
82 while (process != processors) {
86 processIDS.push_back(pid); //create map from line number to pid so you can append files in correct order later
91 Myresults = driver(t, namesOfGroupCombos, lines[process].start, lines[process].num);
93 m->mothurOut("Merging results."); m->mothurOutEndLine();
95 //pass numSeqs to parent
98 string tempFile = outputDir + toString(getpid()) + ".weighted.results.temp";
100 m->openOutputFile(tempFile, out);
102 out << Myresults.size() << endl;
103 for (int i = 0; i < Myresults.size(); i++) { out << Myresults[i] << '\t'; } out << endl;
108 m->mothurOut("[ERROR]: unable to spawn the necessary processes."); m->mothurOutEndLine();
109 for (int i = 0; i < processIDS.size(); i++) { kill (processIDS[i], SIGINT); }
114 results = driver(t, namesOfGroupCombos, lines[0].start, lines[0].num);
116 //force parent to wait until all the processes are done
117 for (int i=0;i<(processors-1);i++) {
118 int temp = processIDS[i];
122 if (m->control_pressed) { return results; }
124 //get data created by processes
125 for (int i=0;i<(processors-1);i++) {
127 string s = outputDir + toString(processIDS[i]) + ".weighted.results.temp";
128 m->openInputFile(s, in);
133 in >> num; m->gobble(in);
135 if (m->control_pressed) { break; }
138 for (int j = 0; j < num; j++) {
140 results.push_back(w);
148 m->mothurOut("DONE."); m->mothurOutEndLine(); m->mothurOutEndLine();
153 catch(exception& e) {
154 m->errorOut(e, "Weighted", "createProcesses");
158 /**************************************************************************************************/
159 EstOutput Weighted::driver(Tree* t, vector< vector<string> > namesOfGroupCombos, int start, int num) {
165 for (int h = start; h < (start+num); h++) {
167 if (m->control_pressed) { return results; }
169 //initialize weighted score
170 string groupA = namesOfGroupCombos[h][0];
171 string groupB = namesOfGroupCombos[h][1];
173 set<int> validBranches;
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 sum = getLengthToRoot(t, t->groupNodeInfo[groupA][j], groupA, groupB);
183 double weightedSum = ((numSeqsInGroupI * sum) / (double)tmap->seqsPerGroup[groupA]);
185 D[count] += weightedSum;
188 //adding the wieghted sums from group l
189 for (int j = 0; j < t->groupNodeInfo[groupB].size(); j++) { //the leaf nodes that have seqs from group l
190 map<string, int>::iterator it = t->tree[t->groupNodeInfo[groupB][j]].pcount.find(groupB);
191 int numSeqsInGroupL = it->second;
193 double sum = getLengthToRoot(t, t->groupNodeInfo[groupB][j], groupA, groupB);
194 double weightedSum = ((numSeqsInGroupL * sum) / (double)tmap->seqsPerGroup[groupB]);
196 D[count] += weightedSum;
201 //calculate u for the group comb
203 for (int h = start; h < (start+num); h++) {
205 m->mothurOut("Processing combo: " + toString(h)); m->mothurOutEndLine();
207 int numLeaves = t->getNumLeaves();
208 map<int, double> tempTotals; //maps node to total Branch Length
209 map<int, int> nodePcountSize; //maps node to pcountSize
211 string groupA = namesOfGroupCombos[h][0];
212 string groupB = namesOfGroupCombos[h][1];
214 //calculate u for the group comb
215 for(int i=0;i<t->getNumNodes();i++){
217 if (m->control_pressed) { return data; }
221 //does this node have descendants from groupA
222 it = t->tree[i].pcount.find(groupA);
223 //if it does u = # of its descendants with a certain group / total number in tree with a certain group
224 if (it != t->tree[i].pcount.end()) {
225 u = (double) t->tree[i].pcount[groupA] / (double) tmap->seqsPerGroup[groupA];
230 //does this node have descendants from group l
231 it = t->tree[i].pcount.find(groupB);
232 //if it does subtract their percentage from u
233 if (it != t->tree[i].pcount.end()) {
234 u -= (double) t->tree[i].pcount[groupB] / (double) tmap->seqsPerGroup[groupB];
238 u = abs(u * t->tree[i].getBranchLength());
240 nodePcountSize[i] = pcountSize;
242 //if you are a leaf from a users group add to total
244 if ((t->tree[i].getBranchLength() != -1) && pcountSize != 0) {
245 //cout << "added to total" << endl;
246 WScore[(groupA+groupB)] += u;
248 tempTotals[i] = 0.0; //we don't care about you, or we have already added you
249 }else{ //if you are not a leaf
250 //do both your chidren have have descendants from the users groups?
251 int lc = t->tree[i].getLChild();
252 int rc = t->tree[i].getRChild();
254 //if yes, add your childrens tempTotals
255 if ((nodePcountSize[lc] != 0) && (nodePcountSize[rc] != 0)) {
256 WScore[(groupA+groupB)] += tempTotals[lc] + tempTotals[rc];
257 //cout << "added to total " << tempTotals[lc] << '\t' << tempTotals[rc] << endl;
258 if (t->tree[i].getBranchLength() != -1) {
263 }else if ((nodePcountSize[lc] == 0) && (nodePcountSize[rc] == 0)) { tempTotals[i] = 0.0; //we don't care about you
264 }else { //if no, your tempTotal is your childrens temp totals + your branch length
265 tempTotals[i] = tempTotals[lc] + tempTotals[rc] + u;
267 //cout << "temptotal = "<< tempTotals[i] << endl;
272 /********************************************************/
273 //calculate weighted score for the group combination
276 for (int h = start; h < (start+num); h++) {
277 UN = (WScore[namesOfGroupCombos[h][0]+namesOfGroupCombos[h][1]] / D[count]);
279 if (isnan(UN) || isinf(UN)) { UN = 0; }
280 results.push_back(UN);
286 catch(exception& e) {
287 m->errorOut(e, "Weighted", "driver");
291 /**************************************************************************************************/
292 EstOutput Weighted::getValues(Tree* t, string groupA, string groupB) {
294 globaldata = GlobalData::getInstance();
296 data.clear(); //clear out old values
298 if (m->control_pressed) { return data; }
300 //initialize weighted score
301 WScore[(groupA+groupB)] = 0.0;
303 set<int> validBranches;
305 vector<string> groups; groups.push_back(groupA); groups.push_back(groupB);
307 //adding the wieghted sums from group i
308 for (int j = 0; j < t->groupNodeInfo[groups[0]].size(); j++) { //the leaf nodes that have seqs from group i
309 map<string, int>::iterator it = t->tree[t->groupNodeInfo[groups[0]][j]].pcount.find(groups[0]);
310 int numSeqsInGroupI = it->second;
312 double sum = getLengthToRoot(t, t->groupNodeInfo[groups[0]][j], groups[0], groups[1]);
313 double weightedSum = ((numSeqsInGroupI * sum) / (double)tmap->seqsPerGroup[groups[0]]);
318 //adding the wieghted sums from group l
319 for (int j = 0; j < t->groupNodeInfo[groups[1]].size(); j++) { //the leaf nodes that have seqs from group l
320 map<string, int>::iterator it = t->tree[t->groupNodeInfo[groups[1]][j]].pcount.find(groups[1]);
321 int numSeqsInGroupL = it->second;
323 double sum = getLengthToRoot(t, t->groupNodeInfo[groups[1]][j], groups[0], groups[1]);
324 double weightedSum = ((numSeqsInGroupL * sum) / (double)tmap->seqsPerGroup[groups[1]]);
329 int numLeaves = t->getNumLeaves();
330 map<int, double> tempTotals; //maps node to total Branch Length
331 map<int, int> nodePcountSize; //maps node to pcountSize
333 //calculate u for the group comb
334 for(int i=0;i<t->getNumNodes();i++){
336 if (m->control_pressed) { return data; }
340 //does this node have descendants from groupA
341 it = t->tree[i].pcount.find(groupA);
342 //if it does u = # of its descendants with a certain group / total number in tree with a certain group
343 if (it != t->tree[i].pcount.end()) {
344 u = (double) t->tree[i].pcount[groupA] / (double) tmap->seqsPerGroup[groupA];
349 //does this node have descendants from group l
350 it = t->tree[i].pcount.find(groupB);
351 //if it does subtract their percentage from u
352 if (it != t->tree[i].pcount.end()) {
353 u -= (double) t->tree[i].pcount[groupB] / (double) tmap->seqsPerGroup[groupB];
357 u = abs(u * t->tree[i].getBranchLength());
359 nodePcountSize[i] = pcountSize;
361 //if you are a leaf from a users group add to total
363 if ((t->tree[i].getBranchLength() != -1) && pcountSize != 0) {
364 //cout << "added to total" << endl;
365 WScore[(groupA+groupB)] += u;
367 tempTotals[i] = 0.0; //we don't care about you, or we have already added you
368 }else{ //if you are not a leaf
369 //do both your chidren have have descendants from the users groups?
370 int lc = t->tree[i].getLChild();
371 int rc = t->tree[i].getRChild();
373 //if yes, add your childrens tempTotals
374 if ((nodePcountSize[lc] != 0) && (nodePcountSize[rc] != 0)) {
375 WScore[(groupA+groupB)] += tempTotals[lc] + tempTotals[rc];
376 //cout << "added to total " << tempTotals[lc] << '\t' << tempTotals[rc] << endl;
377 if (t->tree[i].getBranchLength() != -1) {
382 }else if ((nodePcountSize[lc] == 0) && (nodePcountSize[rc] == 0)) { tempTotals[i] = 0.0; //we don't care about you
383 }else { //if no, your tempTotal is your childrens temp totals + your branch length
384 tempTotals[i] = tempTotals[lc] + tempTotals[rc] + u;
386 //cout << "temptotal = "<< tempTotals[i] << endl;
390 /********************************************************/
392 //calculate weighted score for the group combination
394 UN = (WScore[(groupA+groupB)] / D);
396 if (isnan(UN) || isinf(UN)) { UN = 0; }
401 catch(exception& e) {
402 m->errorOut(e, "Weighted", "getValues");
406 /**************************************************************************************************/
407 double Weighted::getLengthToRoot(Tree* t, int v, string groupA, string groupB) {
411 map<int, double> tempTotals; //maps node to total Branch Length
412 map<int, int> nodePcountSize; //maps node to pcountSize
413 map<int, int>::iterator itCount;
418 if(t->tree[index].getBranchLength() != -1){ sum += abs(t->tree[index].getBranchLength()); }
419 tempTotals[index] = 0.0;
420 index = t->tree[index].getParent();
422 //while you aren't at root
423 while(t->tree[index].getParent() != -1){
425 if (m->control_pressed) { return sum; }
428 map<string, int>::iterator itGroup = t->tree[index].pcount.find(groupA);
429 if (itGroup != t->tree[index].pcount.end()) { pcountSize++; }
430 itGroup = t->tree[index].pcount.find(groupB);
431 if (itGroup != t->tree[index].pcount.end()) { pcountSize++; }
433 nodePcountSize[index] = pcountSize;
435 //do both your chidren have have descendants from the users groups?
436 int lc = t->tree[index].getLChild();
437 int rc = t->tree[index].getRChild();
439 itCount = nodePcountSize.find(lc);
440 if (itCount == nodePcountSize.end()) {
442 itGroup = t->tree[lc].pcount.find(groupA);
443 if (itGroup != t->tree[lc].pcount.end()) { LpcountSize++; }
444 itGroup = t->tree[lc].pcount.find(groupB);
445 if (itGroup != t->tree[lc].pcount.end()) { LpcountSize++; }
446 nodePcountSize[lc] = LpcountSize;
449 itCount = nodePcountSize.find(rc);
450 if (itCount == nodePcountSize.end()) {
452 itGroup = t->tree[rc].pcount.find(groupA);
453 if (itGroup != t->tree[rc].pcount.end()) { RpcountSize++; }
454 itGroup = t->tree[rc].pcount.find(groupB);
455 if (itGroup != t->tree[rc].pcount.end()) { RpcountSize++; }
456 nodePcountSize[rc] = RpcountSize;
459 //if yes, add your childrens tempTotals
460 if ((nodePcountSize[lc] != 0) && (nodePcountSize[rc] != 0)) {
461 sum += tempTotals[lc] + tempTotals[rc];
463 //cout << "added to total " << tempTotals[lc] << '\t' << tempTotals[rc] << endl;
464 if (t->tree[index].getBranchLength() != -1) {
465 tempTotals[index] = abs(t->tree[index].getBranchLength());
467 tempTotals[index] = 0.0;
469 }else { //if no, your tempTotal is your childrens temp totals + your branch length
470 tempTotals[index] = tempTotals[lc] + tempTotals[rc] + abs(t->tree[index].getBranchLength());
472 //cout << "temptotal = "<< tempTotals[i] << endl;
474 index = t->tree[index].getParent();
479 catch(exception& e) {
480 m->errorOut(e, "Weighted", "getBranchLengthSums");
484 /**************************************************************************************************/