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;
202 //calculate u for the group comb
204 for (int h = start; h < (start+num); h++) {
206 m->mothurOut("Processing combo: " + toString(h)); m->mothurOutEndLine();
208 int numLeaves = t->getNumLeaves();
210 string groupA = namesOfGroupCombos[h][0];
211 string groupB = namesOfGroupCombos[h][1];
213 //calculate u for the group comb
214 for(int i=0;i<t->getNumNodes();i++){
216 if (m->control_pressed) { return data; }
219 //int pcountSize = 0;
220 //does this node have descendants from groupA
221 it = t->tree[i].pcount.find(groupA);
222 //if it does u = # of its descendants with a certain group / total number in tree with a certain group
223 if (it != t->tree[i].pcount.end()) {
224 u = (double) t->tree[i].pcount[groupA] / (double) tmap->seqsPerGroup[groupA];
228 //does this node have descendants from group l
229 it = t->tree[i].pcount.find(groupB);
230 //if it does subtract their percentage from u
231 if (it != t->tree[i].pcount.end()) {
232 u -= (double) t->tree[i].pcount[groupB] / (double) tmap->seqsPerGroup[groupB];
235 //if this is not the root then add it
236 if (rootForGrouping[namesOfGroupCombos[h]].count(i) == 0) {
237 u = abs(u * t->tree[i].getBranchLength());
238 WScore[(groupA+groupB)] += u;
244 /********************************************************/
245 //calculate weighted score for the group combination
248 for (int h = start; h < (start+num); h++) {
249 UN = (WScore[namesOfGroupCombos[h][0]+namesOfGroupCombos[h][1]] / D[count]);
251 if (isnan(UN) || isinf(UN)) { UN = 0; }
252 results.push_back(UN);
258 catch(exception& e) {
259 m->errorOut(e, "Weighted", "driver");
263 /**************************************************************************************************/
264 EstOutput Weighted::getValues(Tree* t, string groupA, string groupB) {
266 globaldata = GlobalData::getInstance();
268 data.clear(); //clear out old values
270 if (m->control_pressed) { return data; }
272 //initialize weighted score
273 WScore[(groupA+groupB)] = 0.0;
275 set<int> validBranches;
277 vector<string> groups; groups.push_back(groupA); groups.push_back(groupB);
279 //adding the wieghted sums from group i
280 for (int j = 0; j < t->groupNodeInfo[groups[0]].size(); j++) { //the leaf nodes that have seqs from group i
281 map<string, int>::iterator it = t->tree[t->groupNodeInfo[groups[0]][j]].pcount.find(groups[0]);
282 int numSeqsInGroupI = it->second;
284 double sum = getLengthToRoot(t, t->groupNodeInfo[groups[0]][j], groups[0], groups[1]);
285 double weightedSum = ((numSeqsInGroupI * sum) / (double)tmap->seqsPerGroup[groups[0]]);
290 //adding the wieghted sums from group l
291 for (int j = 0; j < t->groupNodeInfo[groups[1]].size(); j++) { //the leaf nodes that have seqs from group l
292 map<string, int>::iterator it = t->tree[t->groupNodeInfo[groups[1]][j]].pcount.find(groups[1]);
293 int numSeqsInGroupL = it->second;
295 double sum = getLengthToRoot(t, t->groupNodeInfo[groups[1]][j], groups[0], groups[1]);
296 double weightedSum = ((numSeqsInGroupL * sum) / (double)tmap->seqsPerGroup[groups[1]]);
301 int numLeaves = t->getNumLeaves();
303 //calculate u for the group comb
304 for(int i=0;i<t->getNumNodes();i++){
306 if (m->control_pressed) { return data; }
309 //int pcountSize = 0;
310 //does this node have descendants from groupA
311 it = t->tree[i].pcount.find(groupA);
312 //if it does u = # of its descendants with a certain group / total number in tree with a certain group
313 if (it != t->tree[i].pcount.end()) {
314 u = (double) t->tree[i].pcount[groupA] / (double) tmap->seqsPerGroup[groupA];
318 //does this node have descendants from group l
319 it = t->tree[i].pcount.find(groupB);
320 //if it does subtract their percentage from u
321 if (it != t->tree[i].pcount.end()) {
322 u -= (double) t->tree[i].pcount[groupB] / (double) tmap->seqsPerGroup[groupB];
325 //if this is not the root then add it
326 if (rootForGrouping[groups].count(i) == 0) {
327 u = abs(u * t->tree[i].getBranchLength());
328 WScore[(groupA+groupB)] += u;
331 /********************************************************/
333 //calculate weighted score for the group combination
335 UN = (WScore[(groupA+groupB)] / D);
337 if (isnan(UN) || isinf(UN)) { UN = 0; }
342 catch(exception& e) {
343 m->errorOut(e, "Weighted", "getValues");
347 /**************************************************************************************************/
348 double Weighted::getLengthToRoot(Tree* t, int v, string groupA, string groupB) {
355 if(t->tree[index].getBranchLength() != -1){ sum += abs(t->tree[index].getBranchLength()); }
356 double tempTotal = 0.0;
357 index = t->tree[index].getParent();
359 vector<string> grouping; grouping.push_back(groupA); grouping.push_back(groupB);
361 rootForGrouping[grouping].insert(index);
363 //while you aren't at root
364 while(t->tree[index].getParent() != -1){
366 if (m->control_pressed) { return sum; }
368 //am I the root for this grouping? if so I want to stop "early"
369 //does my sibling have descendants from the users groups?
370 int parent = t->tree[index].getParent();
371 int lc = t->tree[parent].getLChild();
372 int rc = t->tree[parent].getRChild();
375 if (lc == index) { sib = rc; }
377 map<string, int>::iterator itGroup;
379 itGroup = t->tree[sib].pcount.find(groupA);
380 if (itGroup != t->tree[sib].pcount.end()) { pcountSize++; }
381 itGroup = t->tree[sib].pcount.find(groupB);
382 if (itGroup != t->tree[sib].pcount.end()) { pcountSize++; }
384 //if yes, I am not the root so add me
385 if (pcountSize != 0) {
386 if (t->tree[index].getBranchLength() != -1) {
387 sum += abs(t->tree[index].getBranchLength()) + tempTotal;
393 rootForGrouping[grouping].clear();
394 rootForGrouping[grouping].insert(parent);
395 }else { //if no, I may be the root so add my br to tempTotal until I am proven innocent
396 if (t->tree[index].getBranchLength() != -1) {
397 tempTotal += abs(t->tree[index].getBranchLength());
404 //get all nodes above the root to add so we don't add their u values above
405 index = *(rootForGrouping[grouping].begin());
406 while(t->tree[index].getParent() != -1){
407 int parent = t->tree[index].getParent();
408 rootForGrouping[grouping].insert(parent);
414 catch(exception& e) {
415 m->errorOut(e, "Weighted", "getBranchLengthSums");
419 /**************************************************************************************************/