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;
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);
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) {
161 for (int h = start; h < (start+num); h++) {
163 if (m->control_pressed) { return results; }
165 //initialize weighted score
166 string groupA = namesOfGroupCombos[h][0];
167 string groupB = namesOfGroupCombos[h][1];
169 set<int> validBranches;
170 WScore[groupA+groupB] = 0.0;
171 D.push_back(0.0000); //initialize a spot in D for each combination
173 //adding the wieghted sums from group i
174 for (int j = 0; j < t->groupNodeInfo[groupA].size(); j++) { //the leaf nodes that have seqs from group i
175 map<string, int>::iterator it = t->tree[t->groupNodeInfo[groupA][j]].pcount.find(groupA);
176 int numSeqsInGroupI = it->second;
178 double sum = getLengthToRoot(t, t->groupNodeInfo[groupA][j], groupA, groupB);
179 double weightedSum = ((numSeqsInGroupI * sum) / (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 sum = getLengthToRoot(t, t->groupNodeInfo[groupB][j], groupA, groupB);
190 double weightedSum = ((numSeqsInGroupL * sum) / (double)tmap->seqsPerGroup[groupB]);
192 D[count] += weightedSum;
197 //calculate u for the group comb
199 for (int h = start; h < (start+num); h++) {
201 m->mothurOut("Processing combo: " + toString(h)); m->mothurOutEndLine();
203 int numLeaves = t->getNumLeaves();
204 map<int, double> tempTotals; //maps node to total Branch Length
205 map<int, int> nodePcountSize; //maps node to pcountSize
207 string groupA = namesOfGroupCombos[h][0];
208 string groupB = namesOfGroupCombos[h][1];
210 //calculate u for the group comb
211 for(int i=0;i<t->getNumNodes();i++){
213 if (m->control_pressed) { return data; }
217 //does this node have descendants from groupA
218 it = t->tree[i].pcount.find(groupA);
219 //if it does u = # of its descendants with a certain group / total number in tree with a certain group
220 if (it != t->tree[i].pcount.end()) {
221 u = (double) t->tree[i].pcount[groupA] / (double) tmap->seqsPerGroup[groupA];
226 //does this node have descendants from group l
227 it = t->tree[i].pcount.find(groupB);
228 //if it does subtract their percentage from u
229 if (it != t->tree[i].pcount.end()) {
230 u -= (double) t->tree[i].pcount[groupB] / (double) tmap->seqsPerGroup[groupB];
234 u = abs(u * t->tree[i].getBranchLength());
236 nodePcountSize[i] = pcountSize;
238 //if you are a leaf from a users group add to total
240 if ((t->tree[i].getBranchLength() != -1) && pcountSize != 0) {
241 //cout << "added to total" << endl;
242 WScore[(groupA+groupB)] += u;
244 tempTotals[i] = 0.0; //we don't care about you, or we have already added you
245 }else{ //if you are not a leaf
246 //do both your chidren have have descendants from the users groups?
247 int lc = t->tree[i].getLChild();
248 int rc = t->tree[i].getRChild();
250 //if yes, add your childrens tempTotals
251 if ((nodePcountSize[lc] != 0) && (nodePcountSize[rc] != 0)) {
252 WScore[(groupA+groupB)] += tempTotals[lc] + tempTotals[rc];
253 //cout << "added to total " << tempTotals[lc] << '\t' << tempTotals[rc] << endl;
254 if (t->tree[i].getBranchLength() != -1) {
259 }else if ((nodePcountSize[lc] == 0) && (nodePcountSize[rc] == 0)) { tempTotals[i] = 0.0; //we don't care about you
260 }else { //if no, your tempTotal is your childrens temp totals + your branch length
261 tempTotals[i] = tempTotals[lc] + tempTotals[rc] + u;
263 //cout << "temptotal = "<< tempTotals[i] << endl;
268 /********************************************************/
269 //calculate weighted score for the group combination
272 for (int h = start; h < (start+num); h++) {
273 UN = (WScore[namesOfGroupCombos[h][0]+namesOfGroupCombos[h][1]] / D[count]);
275 if (isnan(UN) || isinf(UN)) { UN = 0; }
276 results.push_back(UN);
282 catch(exception& e) {
283 m->errorOut(e, "Weighted", "driver");
287 /**************************************************************************************************/
288 EstOutput Weighted::getValues(Tree* t, string groupA, string groupB) {
290 globaldata = GlobalData::getInstance();
292 data.clear(); //clear out old values
294 if (m->control_pressed) { return data; }
296 //initialize weighted score
297 WScore[(groupA+groupB)] = 0.0;
299 set<int> validBranches;
301 vector<string> groups; groups.push_back(groupA); groups.push_back(groupB);
303 //adding the wieghted sums from group i
304 for (int j = 0; j < t->groupNodeInfo[groups[0]].size(); j++) { //the leaf nodes that have seqs from group i
305 map<string, int>::iterator it = t->tree[t->groupNodeInfo[groups[0]][j]].pcount.find(groups[0]);
306 int numSeqsInGroupI = it->second;
308 double sum = getLengthToRoot(t, t->groupNodeInfo[groups[0]][j], groups[0], groups[1]);
309 double weightedSum = ((numSeqsInGroupI * sum) / (double)tmap->seqsPerGroup[groups[0]]);
314 //adding the wieghted sums from group l
315 for (int j = 0; j < t->groupNodeInfo[groups[1]].size(); j++) { //the leaf nodes that have seqs from group l
316 map<string, int>::iterator it = t->tree[t->groupNodeInfo[groups[1]][j]].pcount.find(groups[1]);
317 int numSeqsInGroupL = it->second;
319 double sum = getLengthToRoot(t, t->groupNodeInfo[groups[1]][j], groups[0], groups[1]);
320 double weightedSum = ((numSeqsInGroupL * sum) / (double)tmap->seqsPerGroup[groups[1]]);
325 int numLeaves = t->getNumLeaves();
326 map<int, double> tempTotals; //maps node to total Branch Length
327 map<int, int> nodePcountSize; //maps node to pcountSize
329 //calculate u for the group comb
330 for(int i=0;i<t->getNumNodes();i++){
332 if (m->control_pressed) { return data; }
336 //does this node have descendants from groupA
337 it = t->tree[i].pcount.find(groupA);
338 //if it does u = # of its descendants with a certain group / total number in tree with a certain group
339 if (it != t->tree[i].pcount.end()) {
340 u = (double) t->tree[i].pcount[groupA] / (double) tmap->seqsPerGroup[groupA];
345 //does this node have descendants from group l
346 it = t->tree[i].pcount.find(groupB);
347 //if it does subtract their percentage from u
348 if (it != t->tree[i].pcount.end()) {
349 u -= (double) t->tree[i].pcount[groupB] / (double) tmap->seqsPerGroup[groupB];
353 u = abs(u * t->tree[i].getBranchLength());
355 nodePcountSize[i] = pcountSize;
357 //if you are a leaf from a users group add to total
359 if ((t->tree[i].getBranchLength() != -1) && pcountSize != 0) {
360 //cout << "added to total" << endl;
361 WScore[(groupA+groupB)] += u;
363 tempTotals[i] = 0.0; //we don't care about you, or we have already added you
364 }else{ //if you are not a leaf
365 //do both your chidren have have descendants from the users groups?
366 int lc = t->tree[i].getLChild();
367 int rc = t->tree[i].getRChild();
369 //if yes, add your childrens tempTotals
370 if ((nodePcountSize[lc] != 0) && (nodePcountSize[rc] != 0)) {
371 WScore[(groupA+groupB)] += tempTotals[lc] + tempTotals[rc];
372 //cout << "added to total " << tempTotals[lc] << '\t' << tempTotals[rc] << endl;
373 if (t->tree[i].getBranchLength() != -1) {
378 }else if ((nodePcountSize[lc] == 0) && (nodePcountSize[rc] == 0)) { tempTotals[i] = 0.0; //we don't care about you
379 }else { //if no, your tempTotal is your childrens temp totals + your branch length
380 tempTotals[i] = tempTotals[lc] + tempTotals[rc] + u;
382 //cout << "temptotal = "<< tempTotals[i] << endl;
386 /********************************************************/
388 //calculate weighted score for the group combination
390 UN = (WScore[(groupA+groupB)] / D);
392 if (isnan(UN) || isinf(UN)) { UN = 0; }
397 catch(exception& e) {
398 m->errorOut(e, "Weighted", "getValues");
402 /**************************************************************************************************/
403 double Weighted::getLengthToRoot(Tree* t, int v, string groupA, string groupB) {
407 map<int, double> tempTotals; //maps node to total Branch Length
408 map<int, int> nodePcountSize; //maps node to pcountSize
409 map<int, int>::iterator itCount;
414 if(t->tree[index].getBranchLength() != -1){ sum += abs(t->tree[index].getBranchLength()); }
415 tempTotals[index] = 0.0;
416 index = t->tree[index].getParent();
418 //while you aren't at root
419 while(t->tree[index].getParent() != -1){
421 if (m->control_pressed) { return sum; }
424 map<string, int>::iterator itGroup = t->tree[index].pcount.find(groupA);
425 if (itGroup != t->tree[index].pcount.end()) { pcountSize++; }
426 itGroup = t->tree[index].pcount.find(groupB);
427 if (itGroup != t->tree[index].pcount.end()) { pcountSize++; }
429 nodePcountSize[index] = pcountSize;
431 //do both your chidren have have descendants from the users groups?
432 int lc = t->tree[index].getLChild();
433 int rc = t->tree[index].getRChild();
435 itCount = nodePcountSize.find(lc);
436 if (itCount == nodePcountSize.end()) {
438 itGroup = t->tree[lc].pcount.find(groupA);
439 if (itGroup != t->tree[lc].pcount.end()) { LpcountSize++; }
440 itGroup = t->tree[lc].pcount.find(groupB);
441 if (itGroup != t->tree[lc].pcount.end()) { LpcountSize++; }
442 nodePcountSize[lc] = LpcountSize;
445 itCount = nodePcountSize.find(rc);
446 if (itCount == nodePcountSize.end()) {
448 itGroup = t->tree[rc].pcount.find(groupA);
449 if (itGroup != t->tree[rc].pcount.end()) { RpcountSize++; }
450 itGroup = t->tree[rc].pcount.find(groupB);
451 if (itGroup != t->tree[rc].pcount.end()) { RpcountSize++; }
452 nodePcountSize[rc] = RpcountSize;
455 //if yes, add your childrens tempTotals
456 if ((nodePcountSize[lc] != 0) && (nodePcountSize[rc] != 0)) {
457 sum += tempTotals[lc] + tempTotals[rc];
459 //cout << "added to total " << tempTotals[lc] << '\t' << tempTotals[rc] << endl;
460 if (t->tree[index].getBranchLength() != -1) {
461 tempTotals[index] = abs(t->tree[index].getBranchLength());
463 tempTotals[index] = 0.0;
465 }else { //if no, your tempTotal is your childrens temp totals + your branch length
466 tempTotals[index] = tempTotals[lc] + tempTotals[rc] + abs(t->tree[index].getBranchLength());
468 //cout << "temptotal = "<< tempTotals[i] << endl;
470 index = t->tree[index].getParent();
475 catch(exception& e) {
476 m->errorOut(e, "Weighted", "getBranchLengthSums");
480 /**************************************************************************************************/