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 vector<double> sums = getBranchLengthSums(t);
28 if (m->control_pressed) { return data; }
30 //calculate number of comparisons i.e. with groups A,B,C = AB, AC, BC = 3;
31 vector< vector<string> > namesOfGroupCombos;
32 for (int i=0; i<numGroups; i++) {
33 for (int l = 0; l < i; l++) {
34 //initialize weighted scores
35 //WScore[globaldata->Groups[i]+globaldata->Groups[l]] = 0.0;
36 vector<string> groups; groups.push_back(globaldata->Groups[i]); groups.push_back(globaldata->Groups[l]);
37 namesOfGroupCombos.push_back(groups);
41 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
43 data = driver(t, namesOfGroupCombos, 0, namesOfGroupCombos.size(), sums);
45 int numPairs = namesOfGroupCombos.size();
47 int numPairsPerProcessor = numPairs / processors;
49 for (int i = 0; i < processors; i++) {
50 int startPos = i * numPairsPerProcessor;
51 if(i == processors - 1){
52 numPairsPerProcessor = numPairs - i * numPairsPerProcessor;
54 lines.push_back(linePair(startPos, numPairsPerProcessor));
57 data = createProcesses(t, namesOfGroupCombos, sums);
62 data = driver(t, namesOfGroupCombos, 0, namesOfGroupCombos.size(), sums);
68 m->errorOut(e, "Weighted", "getValues");
72 /**************************************************************************************************/
74 EstOutput Weighted::createProcesses(Tree* t, vector< vector<string> > namesOfGroupCombos, vector<double>& sums) {
76 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
79 vector<int> processIDS;
83 //loop through and create all the processes you want
84 while (process != processors) {
88 processIDS.push_back(pid); //create map from line number to pid so you can append files in correct order later
93 Myresults = driver(t, namesOfGroupCombos, lines[process].start, lines[process].num, sums);
95 m->mothurOut("Merging results."); m->mothurOutEndLine();
97 //pass numSeqs to parent
100 string tempFile = outputDir + toString(getpid()) + ".weighted.results.temp";
102 m->openOutputFile(tempFile, out);
104 out << Myresults.size() << endl;
105 for (int i = 0; i < Myresults.size(); i++) { out << Myresults[i] << '\t'; } out << endl;
109 }else { m->mothurOut("unable to spawn the necessary processes."); m->mothurOutEndLine(); exit(0); }
112 results = driver(t, namesOfGroupCombos, lines[0].start, lines[0].num, sums);
114 //force parent to wait until all the processes are done
115 for (int i=0;i<(processors-1);i++) {
116 int temp = processIDS[i];
120 if (m->control_pressed) { return results; }
122 //get data created by processes
123 for (int i=0;i<(processors-1);i++) {
125 string s = outputDir + toString(processIDS[i]) + ".weighted.results.temp";
126 m->openInputFile(s, in);
131 in >> num; m->gobble(in);
133 if (m->control_pressed) { break; }
136 for (int j = 0; j < num; j++) {
138 results.push_back(w);
146 m->mothurOut("DONE."); m->mothurOutEndLine(); m->mothurOutEndLine();
151 catch(exception& e) {
152 m->errorOut(e, "Weighted", "createProcesses");
156 /**************************************************************************************************/
157 EstOutput Weighted::driver(Tree* t, vector< vector<string> > namesOfGroupCombos, int start, int num, vector<double>& sums) {
163 for (int h = start; h < (start+num); h++) {
165 if (m->control_pressed) { return results; }
167 //initialize weighted score
168 string groupA = namesOfGroupCombos[h][0];
169 string groupB = namesOfGroupCombos[h][1];
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 weightedSum = ((numSeqsInGroupI * sums[t->groupNodeInfo[groupA][j]]) / (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 weightedSum = ((numSeqsInGroupL * sums[t->groupNodeInfo[groupB][j]]) / (double)tmap->seqsPerGroup[groupB]);
191 D[count] += weightedSum;
196 //calculate u for the group comb
197 int total = t->getNumNodes();
198 int twentyPercent = (total * 0.20);
200 for(int i=0;i<t->getNumNodes();i++){
201 for (int h = start; h < (start+num); h++) {
203 string groupA = namesOfGroupCombos[h][0];
204 string groupB = namesOfGroupCombos[h][1];
206 if (m->control_pressed) { return results; }
209 //does this node have descendants from groupA
210 it = t->tree[i].pcount.find(groupA);
211 //if it does u = # of its descendants with a certain group / total number in tree with a certain group
212 if (it != t->tree[i].pcount.end()) {
213 u = (double) t->tree[i].pcount[groupA] / (double) tmap->seqsPerGroup[groupA];
217 //does this node have descendants from group l
218 it = t->tree[i].pcount.find(groupB);
219 //if it does subtract their percentage from u
220 if (it != t->tree[i].pcount.end()) {
221 u -= (double) t->tree[i].pcount[groupB] / (double) tmap->seqsPerGroup[groupB];
224 u = abs(u * t->tree[i].getBranchLength());
226 //save groupcombs u value
227 WScore[(groupA+groupB)] += u;
230 if((i) % twentyPercent == 0){ m->mothurOut("Percentage complete: " + toString(int((i / (float)total) * 100.0))); m->mothurOutEndLine(); }
233 m->mothurOut("Percentage complete: 100"); m->mothurOutEndLine();
235 /********************************************************/
236 //calculate weighted score for the group combination
239 for (int h = start; h < (start+num); h++) {
240 UN = (WScore[namesOfGroupCombos[h][0]+namesOfGroupCombos[h][1]] / D[count]);
242 if (isnan(UN) || isinf(UN)) { UN = 0; }
243 results.push_back(UN);
249 catch(exception& e) {
250 m->errorOut(e, "Weighted", "driver");
254 /**************************************************************************************************/
255 EstOutput Weighted::getValues(Tree* t, string groupA, string groupB, vector<double>& sums) {
257 globaldata = GlobalData::getInstance();
259 data.clear(); //clear out old values
261 if (m->control_pressed) { return data; }
263 //initialize weighted score
264 WScore[(groupA+groupB)] = 0.0;
267 vector<string> groups; groups.push_back(groupA); groups.push_back(groupB);
269 //adding the wieghted sums from group i
270 for (int j = 0; j < t->groupNodeInfo[groups[0]].size(); j++) { //the leaf nodes that have seqs from group i
271 map<string, int>::iterator it = t->tree[t->groupNodeInfo[groups[0]][j]].pcount.find(groups[0]);
272 int numSeqsInGroupI = it->second;
274 double weightedSum = ((numSeqsInGroupI * sums[t->groupNodeInfo[groups[0]][j]]) / (double)tmap->seqsPerGroup[groups[0]]);
279 //adding the wieghted sums from group l
280 for (int j = 0; j < t->groupNodeInfo[groups[1]].size(); j++) { //the leaf nodes that have seqs from group l
281 map<string, int>::iterator it = t->tree[t->groupNodeInfo[groups[1]][j]].pcount.find(groups[1]);
282 int numSeqsInGroupL = it->second;
284 double weightedSum = ((numSeqsInGroupL * sums[t->groupNodeInfo[groups[1]][j]]) / (double)tmap->seqsPerGroup[groups[1]]);
289 //calculate u for the group comb
290 for(int i=0;i<t->getNumNodes();i++){
292 if (m->control_pressed) { return data; }
295 //does this node have descendants from groupA
296 it = t->tree[i].pcount.find(groupA);
297 //if it does u = # of its descendants with a certain group / total number in tree with a certain group
298 if (it != t->tree[i].pcount.end()) {
299 u = (double) t->tree[i].pcount[groupA] / (double) tmap->seqsPerGroup[groupA];
303 //does this node have descendants from group l
304 it = t->tree[i].pcount.find(groupB);
305 //if it does subtract their percentage from u
306 if (it != t->tree[i].pcount.end()) {
307 u -= (double) t->tree[i].pcount[groupB] / (double) tmap->seqsPerGroup[groupB];
310 u = abs(u * t->tree[i].getBranchLength());
312 //save groupcombs u value
313 WScore[(groupA+groupB)] += u;
316 /********************************************************/
318 //calculate weighted score for the group combination
320 UN = (WScore[(groupA+groupB)] / D);
322 if (isnan(UN) || isinf(UN)) { UN = 0; }
327 catch(exception& e) {
328 m->errorOut(e, "Weighted", "getValues");
332 /**************************************************************************************************/
333 vector<double> Weighted::getBranchLengthSums(Tree* t) {
338 for(int v=0;v<t->getNumLeaves();v++){
340 if (m->control_pressed) { return sums; }
345 //while you aren't at root
346 while(t->tree[index].getParent() != -1){
349 if(t->tree[index].getBranchLength() != -1){
350 sum += abs(t->tree[index].getBranchLength());
352 index = t->tree[index].getParent();
355 //get last breanch length added
356 if(t->tree[index].getBranchLength() != -1){
357 sum += abs(t->tree[index].getBranchLength());
365 catch(exception& e) {
366 m->errorOut(e, "Weighted", "getBranchLengthSums");
370 /**************************************************************************************************/