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 numGroups = m->Groups.size();
24 if (m->control_pressed) { return data; }
26 //calculate number of comparisons i.e. with groups A,B,C = AB, AC, BC = 3;
27 vector< vector<string> > namesOfGroupCombos;
28 for (int i=0; i<numGroups; i++) {
29 for (int l = 0; l < i; l++) {
30 //initialize weighted scores
31 //WScore[globaldata->Groups[i]+globaldata->Groups[l]] = 0.0;
32 vector<string> groups; groups.push_back(m->Groups[i]); groups.push_back(m->Groups[l]);
33 namesOfGroupCombos.push_back(groups);
37 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
39 data = driver(t, namesOfGroupCombos, 0, namesOfGroupCombos.size());
41 int numPairs = namesOfGroupCombos.size();
43 int numPairsPerProcessor = numPairs / processors;
45 for (int i = 0; i < processors; i++) {
46 int startPos = i * numPairsPerProcessor;
47 if(i == processors - 1){
48 numPairsPerProcessor = numPairs - i * numPairsPerProcessor;
50 lines.push_back(linePair(startPos, numPairsPerProcessor));
53 data = createProcesses(t, namesOfGroupCombos);
58 data = driver(t, namesOfGroupCombos, 0, namesOfGroupCombos.size());
64 m->errorOut(e, "Weighted", "getValues");
68 /**************************************************************************************************/
70 EstOutput Weighted::createProcesses(Tree* t, vector< vector<string> > namesOfGroupCombos) {
72 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
74 vector<int> processIDS;
78 //loop through and create all the processes you want
79 while (process != processors) {
83 processIDS.push_back(pid); //create map from line number to pid so you can append files in correct order later
88 Myresults = driver(t, namesOfGroupCombos, lines[process].start, lines[process].num);
90 m->mothurOut("Merging results."); m->mothurOutEndLine();
92 //pass numSeqs to parent
95 string tempFile = outputDir + toString(getpid()) + ".weighted.results.temp";
97 m->openOutputFile(tempFile, out);
99 out << Myresults.size() << endl;
100 for (int i = 0; i < Myresults.size(); i++) { out << Myresults[i] << '\t'; } out << endl;
105 m->mothurOut("[ERROR]: unable to spawn the necessary processes."); m->mothurOutEndLine();
106 for (int i = 0; i < processIDS.size(); i++) { kill (processIDS[i], SIGINT); }
111 results = driver(t, namesOfGroupCombos, lines[0].start, lines[0].num);
113 //force parent to wait until all the processes are done
114 for (int i=0;i<(processors-1);i++) {
115 int temp = processIDS[i];
119 if (m->control_pressed) { return results; }
121 //get data created by processes
122 for (int i=0;i<(processors-1);i++) {
124 string s = outputDir + toString(processIDS[i]) + ".weighted.results.temp";
125 m->openInputFile(s, in);
130 in >> num; m->gobble(in);
132 if (m->control_pressed) { break; }
135 for (int j = 0; j < num; j++) {
137 results.push_back(w);
145 m->mothurOut("DONE."); m->mothurOutEndLine(); m->mothurOutEndLine();
150 catch(exception& e) {
151 m->errorOut(e, "Weighted", "createProcesses");
155 /**************************************************************************************************/
156 EstOutput Weighted::driver(Tree* t, vector< vector<string> > namesOfGroupCombos, int start, int num) {
162 for (int h = start; h < (start+num); h++) {
164 if (m->control_pressed) { return results; }
166 //initialize weighted score
167 string groupA = namesOfGroupCombos[h][0];
168 string groupB = namesOfGroupCombos[h][1];
170 set<int> validBranches;
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 sum = getLengthToRoot(t, t->groupNodeInfo[groupA][j], groupA, groupB);
180 double weightedSum = ((numSeqsInGroupI * sum) / (double)tmap->seqsPerGroup[groupA]);
182 D[count] += weightedSum;
185 //adding the wieghted sums from group l
186 for (int j = 0; j < t->groupNodeInfo[groupB].size(); j++) { //the leaf nodes that have seqs from group l
187 map<string, int>::iterator it = t->tree[t->groupNodeInfo[groupB][j]].pcount.find(groupB);
188 int numSeqsInGroupL = it->second;
190 double sum = getLengthToRoot(t, t->groupNodeInfo[groupB][j], groupA, groupB);
191 double weightedSum = ((numSeqsInGroupL * sum) / (double)tmap->seqsPerGroup[groupB]);
193 D[count] += weightedSum;
198 //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 string groupA = namesOfGroupCombos[h][0];
204 string groupB = namesOfGroupCombos[h][1];
206 //calculate u for the group comb
207 for(int i=0;i<t->getNumNodes();i++){
209 if (m->control_pressed) { return data; }
212 //int pcountSize = 0;
213 //does this node have descendants from groupA
214 it = t->tree[i].pcount.find(groupA);
215 //if it does u = # of its descendants with a certain group / total number in tree with a certain group
216 if (it != t->tree[i].pcount.end()) {
217 u = (double) t->tree[i].pcount[groupA] / (double) tmap->seqsPerGroup[groupA];
221 //does this node have descendants from group l
222 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];
230 if (t->tree[i].getBranchLength() != -1) {
231 u = abs(u * t->tree[i].getBranchLength());
232 WScore[(groupA+groupB)] += u;
235 //if this is not the root then add it
236 if (rootForGrouping[namesOfGroupCombos[h]].count(i) == 0) {
237 if (t->tree[i].getBranchLength() != -1) {
238 u = abs(u * t->tree[i].getBranchLength());
239 WScore[(groupA+groupB)] += u;
247 /********************************************************/
248 //calculate weighted score for the group combination
251 for (int h = start; h < (start+num); h++) {
252 UN = (WScore[namesOfGroupCombos[h][0]+namesOfGroupCombos[h][1]] / D[count]);
253 if (isnan(UN) || isinf(UN)) { UN = 0; }
254 results.push_back(UN);
260 catch(exception& e) {
261 m->errorOut(e, "Weighted", "driver");
265 /**************************************************************************************************/
266 EstOutput Weighted::getValues(Tree* t, string groupA, string groupB) {
269 data.clear(); //clear out old values
271 if (m->control_pressed) { return data; }
273 //initialize weighted score
274 WScore[(groupA+groupB)] = 0.0;
276 set<int> validBranches;
278 vector<string> groups; groups.push_back(groupA); groups.push_back(groupB);
280 //adding the wieghted sums from group i
281 for (int j = 0; j < t->groupNodeInfo[groups[0]].size(); j++) { //the leaf nodes that have seqs from group i
282 map<string, int>::iterator it = t->tree[t->groupNodeInfo[groups[0]][j]].pcount.find(groups[0]);
283 int numSeqsInGroupI = it->second;
285 double sum = getLengthToRoot(t, t->groupNodeInfo[groups[0]][j], groups[0], groups[1]);
286 double weightedSum = ((numSeqsInGroupI * sum) / (double)tmap->seqsPerGroup[groups[0]]);
291 //adding the wieghted sums from group l
292 for (int j = 0; j < t->groupNodeInfo[groups[1]].size(); j++) { //the leaf nodes that have seqs from group l
293 map<string, int>::iterator it = t->tree[t->groupNodeInfo[groups[1]][j]].pcount.find(groups[1]);
294 int numSeqsInGroupL = it->second;
296 double sum = getLengthToRoot(t, t->groupNodeInfo[groups[1]][j], groups[0], groups[1]);
297 double weightedSum = ((numSeqsInGroupL * sum) / (double)tmap->seqsPerGroup[groups[1]]);
302 //calculate u for the group comb
303 for(int i=0;i<t->getNumNodes();i++){
305 if (m->control_pressed) { return data; }
308 //int pcountSize = 0;
309 //does this node have descendants from groupA
310 it = t->tree[i].pcount.find(groupA);
311 //if it does u = # of its descendants with a certain group / total number in tree with a certain group
312 if (it != t->tree[i].pcount.end()) {
313 u = (double) t->tree[i].pcount[groupA] / (double) tmap->seqsPerGroup[groupA];
317 //does this node have descendants from group l
318 it = t->tree[i].pcount.find(groupB);
319 //if it does subtract their percentage from u
320 if (it != t->tree[i].pcount.end()) {
321 u -= (double) t->tree[i].pcount[groupB] / (double) tmap->seqsPerGroup[groupB];
325 if (t->tree[i].getBranchLength() != -1) {
326 u = abs(u * t->tree[i].getBranchLength());
327 WScore[(groupA+groupB)] += u;
330 //if this is not the root then add it
331 if (rootForGrouping[groups].count(i) == 0) {
332 if (t->tree[i].getBranchLength() != -1) {
333 u = abs(u * t->tree[i].getBranchLength());
334 WScore[(groupA+groupB)] += u;
339 /********************************************************/
341 //calculate weighted score for the group combination
343 UN = (WScore[(groupA+groupB)] / D);
345 if (isnan(UN) || isinf(UN)) { UN = 0; }
350 catch(exception& e) {
351 m->errorOut(e, "Weighted", "getValues");
355 /**************************************************************************************************/
356 double Weighted::getLengthToRoot(Tree* t, int v, string groupA, string groupB) {
363 if(t->tree[index].getBranchLength() != -1){ sum += abs(t->tree[index].getBranchLength()); }
364 double tempTotal = 0.0;
365 index = t->tree[index].getParent();
367 vector<string> grouping; grouping.push_back(groupA); grouping.push_back(groupB);
369 rootForGrouping[grouping].insert(index);
371 //while you aren't at root
372 while(t->tree[index].getParent() != -1){
374 if (m->control_pressed) { return sum; }
376 int parent = t->tree[index].getParent();
378 if (includeRoot) { //add everyone
379 if(t->tree[index].getBranchLength() != -1){ sum += abs(t->tree[index].getBranchLength()); }
382 //am I the root for this grouping? if so I want to stop "early"
383 //does my sibling have descendants from the users groups?
384 int lc = t->tree[parent].getLChild();
385 int rc = t->tree[parent].getRChild();
388 if (lc == index) { sib = rc; }
390 map<string, int>::iterator itGroup;
392 itGroup = t->tree[sib].pcount.find(groupA);
393 if (itGroup != t->tree[sib].pcount.end()) { pcountSize++; }
394 itGroup = t->tree[sib].pcount.find(groupB);
395 if (itGroup != t->tree[sib].pcount.end()) { pcountSize++; }
397 //if yes, I am not the root so add me
398 if (pcountSize != 0) {
399 if (t->tree[index].getBranchLength() != -1) {
400 sum += abs(t->tree[index].getBranchLength()) + tempTotal;
406 rootForGrouping[grouping].clear();
407 rootForGrouping[grouping].insert(parent);
408 }else { //if no, I may be the root so add my br to tempTotal until I am proven innocent
409 if (t->tree[index].getBranchLength() != -1) {
410 tempTotal += abs(t->tree[index].getBranchLength());
418 //get all nodes above the root to add so we don't add their u values above
419 index = *(rootForGrouping[grouping].begin());
421 while(t->tree[index].getParent() != -1){
422 int parent = t->tree[index].getParent();
423 rootForGrouping[grouping].insert(parent);
428 catch(exception& e) {
429 m->errorOut(e, "Weighted", "getBranchLengthSums");
433 /**************************************************************************************************/