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 data.clear(); //clear out old values
22 CountTable* ct = t->getCountTable();
24 numGroups = m->getNumGroups();
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((m->getGroups())[i]); groups.push_back((m->getGroups())[l]);
35 namesOfGroupCombos.push_back(groups);
39 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux) || (__linux__) || (__unix__) || (__unix)
41 data = driver(t, namesOfGroupCombos, 0, namesOfGroupCombos.size(), ct);
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, ct);
60 data = driver(t, namesOfGroupCombos, 0, namesOfGroupCombos.size(), ct);
66 m->errorOut(e, "Weighted", "getValues");
70 /**************************************************************************************************/
72 EstOutput Weighted::createProcesses(Tree* t, vector< vector<string> > namesOfGroupCombos, CountTable* ct) {
74 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux) || (__linux__) || (__unix__) || (__unix)
76 vector<int> processIDS;
80 //loop through and create all the processes you want
81 while (process != processors) {
85 processIDS.push_back(pid); //create map from line number to pid so you can append files in correct order later
90 Myresults = driver(t, namesOfGroupCombos, lines[process].start, lines[process].num, ct);
92 //m->mothurOut("Merging results."); m->mothurOutEndLine();
94 //pass numSeqs to parent
97 string tempFile = outputDir + toString(getpid()) + ".weighted.results.temp";
99 m->openOutputFile(tempFile, out);
101 out << Myresults.size() << endl;
102 for (int i = 0; i < Myresults.size(); i++) { out << Myresults[i] << '\t'; } out << endl;
107 m->mothurOut("[ERROR]: unable to spawn the necessary processes."); m->mothurOutEndLine();
108 for (int i = 0; i < processIDS.size(); i++) { kill (processIDS[i], SIGINT); }
113 results = driver(t, namesOfGroupCombos, lines[0].start, lines[0].num, ct);
115 //force parent to wait until all the processes are done
116 for (int i=0;i<(processors-1);i++) {
117 int temp = processIDS[i];
121 if (m->control_pressed) { return results; }
123 //get data created by processes
124 for (int i=0;i<(processors-1);i++) {
126 string s = outputDir + toString(processIDS[i]) + ".weighted.results.temp";
127 m->openInputFile(s, in);
132 in >> num; m->gobble(in);
134 if (m->control_pressed) { break; }
137 for (int j = 0; j < num; j++) {
139 results.push_back(w);
147 //m->mothurOut("DONE."); m->mothurOutEndLine(); m->mothurOutEndLine();
152 catch(exception& e) {
153 m->errorOut(e, "Weighted", "createProcesses");
157 /**************************************************************************************************/
158 EstOutput Weighted::driver(Tree* t, vector< vector<string> > namesOfGroupCombos, int start, int num, CountTable* ct) {
164 for (int h = start; h < (start+num); h++) {
166 if (m->control_pressed) { return results; }
168 //initialize weighted score
169 string groupA = namesOfGroupCombos[h][0];
170 string groupB = namesOfGroupCombos[h][1];
172 set<int> validBranches;
173 WScore[groupA+groupB] = 0.0;
174 D.push_back(0.0000); //initialize a spot in D for each combination
176 //adding the wieghted sums from group i
177 for (int j = 0; j < t->groupNodeInfo[groupA].size(); j++) { //the leaf nodes that have seqs from group i
178 map<string, int>::iterator it = t->tree[t->groupNodeInfo[groupA][j]].pcount.find(groupA);
179 int numSeqsInGroupI = it->second;
181 double sum = getLengthToRoot(t, t->groupNodeInfo[groupA][j], groupA, groupB);
182 double weightedSum = ((numSeqsInGroupI * sum) / (double)ct->getGroupCount(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 sum = getLengthToRoot(t, t->groupNodeInfo[groupB][j], groupA, groupB);
193 double weightedSum = ((numSeqsInGroupL * sum) / (double)ct->getGroupCount(groupB));
195 D[count] += weightedSum;
200 //calculate u for the group comb
201 for (int h = start; h < (start+num); h++) {
203 //m->mothurOut("Processing combo: " + toString(h)); m->mothurOutEndLine();
205 string groupA = namesOfGroupCombos[h][0];
206 string groupB = namesOfGroupCombos[h][1];
208 //calculate u for the group comb
209 for(int i=0;i<t->getNumNodes();i++){
211 if (m->control_pressed) { return data; }
214 //int pcountSize = 0;
215 //does this node have descendants from groupA
216 it = t->tree[i].pcount.find(groupA);
217 //if it does u = # of its descendants with a certain group / total number in tree with a certain group
218 if (it != t->tree[i].pcount.end()) {
219 u = (double) t->tree[i].pcount[groupA] / (double) ct->getGroupCount(groupA);
223 //does this node have descendants from group l
224 it = t->tree[i].pcount.find(groupB);
226 //if it does subtract their percentage from u
227 if (it != t->tree[i].pcount.end()) {
228 u -= (double) t->tree[i].pcount[groupB] / (double) ct->getGroupCount(groupB);
232 if (t->tree[i].getBranchLength() != -1) {
233 u = abs(u * t->tree[i].getBranchLength());
234 WScore[(groupA+groupB)] += u;
237 //if this is not the root then add it
238 if (rootForGrouping[namesOfGroupCombos[h]].count(i) == 0) {
239 if (t->tree[i].getBranchLength() != -1) {
240 u = abs(u * t->tree[i].getBranchLength());
241 WScore[(groupA+groupB)] += u;
249 /********************************************************/
250 //calculate weighted score for the group combination
253 for (int h = start; h < (start+num); h++) {
254 UN = (WScore[namesOfGroupCombos[h][0]+namesOfGroupCombos[h][1]] / D[count]);
255 if (isnan(UN) || isinf(UN)) { UN = 0; }
256 results.push_back(UN);
262 catch(exception& e) {
263 m->errorOut(e, "Weighted", "driver");
267 /**************************************************************************************************/
268 EstOutput Weighted::getValues(Tree* t, string groupA, string groupB) {
271 data.clear(); //clear out old values
273 CountTable* ct = t->getCountTable();
275 if (m->control_pressed) { return data; }
277 //initialize weighted score
278 WScore[(groupA+groupB)] = 0.0;
280 set<int> validBranches;
282 vector<string> groups; groups.push_back(groupA); groups.push_back(groupB);
284 //adding the wieghted sums from group i
285 for (int j = 0; j < t->groupNodeInfo[groups[0]].size(); j++) { //the leaf nodes that have seqs from group i
286 map<string, int>::iterator it = t->tree[t->groupNodeInfo[groups[0]][j]].pcount.find(groups[0]);
287 int numSeqsInGroupI = it->second;
289 double sum = getLengthToRoot(t, t->groupNodeInfo[groups[0]][j], groups[0], groups[1]);
290 double weightedSum = ((numSeqsInGroupI * sum) / (double)ct->getGroupCount(groups[0]));
295 //adding the wieghted sums from group l
296 for (int j = 0; j < t->groupNodeInfo[groups[1]].size(); j++) { //the leaf nodes that have seqs from group l
297 map<string, int>::iterator it = t->tree[t->groupNodeInfo[groups[1]][j]].pcount.find(groups[1]);
298 int numSeqsInGroupL = it->second;
300 double sum = getLengthToRoot(t, t->groupNodeInfo[groups[1]][j], groups[0], groups[1]);
301 double weightedSum = ((numSeqsInGroupL * sum) / (double)ct->getGroupCount(groups[1]));
306 //calculate u for the group comb
307 for(int i=0;i<t->getNumNodes();i++){
309 if (m->control_pressed) { return data; }
312 //int pcountSize = 0;
313 //does this node have descendants from groupA
314 it = t->tree[i].pcount.find(groupA);
315 //if it does u = # of its descendants with a certain group / total number in tree with a certain group
316 if (it != t->tree[i].pcount.end()) {
317 u = (double) t->tree[i].pcount[groupA] / (double) ct->getGroupCount(groupA);
321 //does this node have descendants from group l
322 it = t->tree[i].pcount.find(groupB);
323 //if it does subtract their percentage from u
324 if (it != t->tree[i].pcount.end()) {
325 u -= (double) t->tree[i].pcount[groupB] / (double) ct->getGroupCount(groupB);
329 if (t->tree[i].getBranchLength() != -1) {
330 u = abs(u * t->tree[i].getBranchLength());
331 WScore[(groupA+groupB)] += u;
334 //if this is not the root then add it
335 if (rootForGrouping[groups].count(i) == 0) {
336 if (t->tree[i].getBranchLength() != -1) {
337 u = abs(u * t->tree[i].getBranchLength());
338 WScore[(groupA+groupB)] += u;
343 /********************************************************/
345 //calculate weighted score for the group combination
347 UN = (WScore[(groupA+groupB)] / D);
349 if (isnan(UN) || isinf(UN)) { UN = 0; }
354 catch(exception& e) {
355 m->errorOut(e, "Weighted", "getValues");
359 /**************************************************************************************************/
360 double Weighted::getLengthToRoot(Tree* t, int v, string groupA, string groupB) {
367 if(t->tree[index].getBranchLength() != -1){ sum += abs(t->tree[index].getBranchLength()); }
368 double tempTotal = 0.0;
369 index = t->tree[index].getParent();
371 vector<string> grouping; grouping.push_back(groupA); grouping.push_back(groupB);
373 rootForGrouping[grouping].insert(index);
375 //while you aren't at root
376 while(t->tree[index].getParent() != -1){
378 if (m->control_pressed) { return sum; }
380 int parent = t->tree[index].getParent();
382 if (includeRoot) { //add everyone
383 if(t->tree[index].getBranchLength() != -1){ sum += abs(t->tree[index].getBranchLength()); }
386 //am I the root for this grouping? if so I want to stop "early"
387 //does my sibling have descendants from the users groups?
388 int lc = t->tree[parent].getLChild();
389 int rc = t->tree[parent].getRChild();
392 if (lc == index) { sib = rc; }
394 map<string, int>::iterator itGroup;
396 itGroup = t->tree[sib].pcount.find(groupA);
397 if (itGroup != t->tree[sib].pcount.end()) { pcountSize++; }
398 itGroup = t->tree[sib].pcount.find(groupB);
399 if (itGroup != t->tree[sib].pcount.end()) { pcountSize++; }
401 //if yes, I am not the root so add me
402 if (pcountSize != 0) {
403 if (t->tree[index].getBranchLength() != -1) {
404 sum += abs(t->tree[index].getBranchLength()) + tempTotal;
410 rootForGrouping[grouping].clear();
411 rootForGrouping[grouping].insert(parent);
412 }else { //if no, I may be the root so add my br to tempTotal until I am proven innocent
413 if (t->tree[index].getBranchLength() != -1) {
414 tempTotal += abs(t->tree[index].getBranchLength());
422 //get all nodes above the root to add so we don't add their u values above
423 index = *(rootForGrouping[grouping].begin());
425 while(t->tree[index].getParent() != -1){
426 int parent = t->tree[index].getParent();
427 rootForGrouping[grouping].insert(parent);
432 catch(exception& e) {
433 m->errorOut(e, "Weighted", "getBranchLengthSums");
437 /**************************************************************************************************/