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 = i+1; l < numGroups; 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(new linePair(startPos, numPairsPerProcessor));
57 data = createProcesses(t, namesOfGroupCombos, sums);
59 for (int i = 0; i < lines.size(); i++) { delete lines[i]; } lines.clear();
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
91 results = driver(t, namesOfGroupCombos, lines[process]->start, lines[process]->num, sums);
93 if (m->control_pressed) { exit(0); }
95 m->mothurOut("Merging results."); m->mothurOutEndLine();
97 //pass numSeqs to parent
99 string tempFile = outputDir + toString(getpid()) + ".results.temp";
100 m->openOutputFile(tempFile, out);
101 out << results.size() << endl;
102 for (int i = 0; i < results.size(); i++) { out << results[i] << '\t'; } out << endl;
106 }else { m->mothurOut("unable to spawn the necessary processes."); m->mothurOutEndLine(); exit(0); }
109 //force parent to wait until all the processes are done
110 for (int i=0;i<processors;i++) {
111 int temp = processIDS[i];
115 if (m->control_pressed) { return results; }
117 //get data created by processes
118 for (int i=0;i<processors;i++) {
120 string s = outputDir + toString(processIDS[i]) + ".results.temp";
121 m->openInputFile(s, in);
130 if (m->control_pressed) { break; }
135 for (int j = 0; j < num; j++) {
144 //save quan in quantiles
145 for (int j = 0; j < r.size(); j++) {
146 //put all values of r into results
147 results.push_back(r[j]);
151 m->mothurOut("DONE."); m->mothurOutEndLine(); m->mothurOutEndLine();
156 catch(exception& e) {
157 m->errorOut(e, "Weighted", "createProcesses");
161 /**************************************************************************************************/
162 EstOutput Weighted::driver(Tree* t, vector< vector<string> > namesOfGroupCombos, int start, int num, vector<double>& sums) {
164 globaldata = GlobalData::getInstance();
170 int total = start+num;
171 int twentyPercent = (total * 0.20);
173 for (int h = start; h < (start+num); h++) {
175 if (m->control_pressed) { return results; }
177 //initialize weighted score
178 string groupA = namesOfGroupCombos[h][0];
179 string groupB = namesOfGroupCombos[h][1];
181 WScore[groupA+groupB] = 0.0;
182 D.push_back(0.0000); //initialize a spot in D for each combination
184 //adding the wieghted sums from group i
185 for (int j = 0; j < t->groupNodeInfo[groupA].size(); j++) { //the leaf nodes that have seqs from group i
186 map<string, int>::iterator it = t->tree[t->groupNodeInfo[groupA][j]].pcount.find(groupA);
187 int numSeqsInGroupI = it->second;
189 double weightedSum = ((numSeqsInGroupI * sums[t->groupNodeInfo[groupA][j]]) / (double)tmap->seqsPerGroup[groupA]);
191 D[count] += weightedSum;
194 //adding the wieghted sums from group l
195 for (int j = 0; j < t->groupNodeInfo[groupB].size(); j++) { //the leaf nodes that have seqs from group l
196 map<string, int>::iterator it = t->tree[t->groupNodeInfo[groupB][j]].pcount.find(groupB);
197 int numSeqsInGroupL = it->second;
199 double weightedSum = ((numSeqsInGroupL * sums[t->groupNodeInfo[groupB][j]]) / (double)tmap->seqsPerGroup[groupB]);
201 D[count] += weightedSum;
206 if((h) % twentyPercent == 0){ m->mothurOut("Percentage complete: " + toString(int((h / (float)total) * 100.0))); m->mothurOutEndLine(); }
209 m->mothurOut("Percentage complete: 100"); m->mothurOutEndLine();
211 //calculate u for the group comb
212 for(int i=0;i<t->getNumNodes();i++){
213 for (int h = start; h < (start+num); h++) {
215 string groupA = namesOfGroupCombos[h][0];
216 string groupB = namesOfGroupCombos[h][1];
218 if (m->control_pressed) { return results; }
221 //does this node have descendants from groupA
222 it = t->tree[i].pcount.find(groupA);
223 //if it does u = # of its descendants with a certain group / total number in tree with a certain group
224 if (it != t->tree[i].pcount.end()) {
225 u = (double) t->tree[i].pcount[groupA] / (double) tmap->seqsPerGroup[groupA];
229 //does this node have descendants from group l
230 it = t->tree[i].pcount.find(groupB);
231 //if it does subtract their percentage from u
232 if (it != t->tree[i].pcount.end()) {
233 u -= (double) t->tree[i].pcount[groupB] / (double) tmap->seqsPerGroup[groupB];
236 u = abs(u * t->tree[i].getBranchLength());
238 //save groupcombs u value
239 WScore[(groupA+groupB)] += u;
243 /********************************************************/
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, vector<double>& sums) {
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;
276 vector<string> groups; groups.push_back(groupA); groups.push_back(groupB);
278 //adding the wieghted sums from group i
279 for (int j = 0; j < t->groupNodeInfo[groups[0]].size(); j++) { //the leaf nodes that have seqs from group i
280 map<string, int>::iterator it = t->tree[t->groupNodeInfo[groups[0]][j]].pcount.find(groups[0]);
281 int numSeqsInGroupI = it->second;
283 double weightedSum = ((numSeqsInGroupI * sums[t->groupNodeInfo[groups[0]][j]]) / (double)tmap->seqsPerGroup[groups[0]]);
288 //adding the wieghted sums from group l
289 for (int j = 0; j < t->groupNodeInfo[groups[1]].size(); j++) { //the leaf nodes that have seqs from group l
290 map<string, int>::iterator it = t->tree[t->groupNodeInfo[groups[1]][j]].pcount.find(groups[1]);
291 int numSeqsInGroupL = it->second;
293 double weightedSum = ((numSeqsInGroupL * sums[t->groupNodeInfo[groups[1]][j]]) / (double)tmap->seqsPerGroup[groups[1]]);
298 //calculate u for the group comb
299 for(int i=0;i<t->getNumNodes();i++){
301 if (m->control_pressed) { return data; }
304 //does this node have descendants from groupA
305 it = t->tree[i].pcount.find(groupA);
306 //if it does u = # of its descendants with a certain group / total number in tree with a certain group
307 if (it != t->tree[i].pcount.end()) {
308 u = (double) t->tree[i].pcount[groupA] / (double) tmap->seqsPerGroup[groupA];
312 //does this node have descendants from group l
313 it = t->tree[i].pcount.find(groupB);
314 //if it does subtract their percentage from u
315 if (it != t->tree[i].pcount.end()) {
316 u -= (double) t->tree[i].pcount[groupB] / (double) tmap->seqsPerGroup[groupB];
319 u = abs(u * t->tree[i].getBranchLength());
321 //save groupcombs u value
322 WScore[(groupA+groupB)] += u;
325 /********************************************************/
327 //calculate weighted score for the group combination
329 UN = (WScore[(groupA+groupB)] / D);
331 if (isnan(UN) || isinf(UN)) { UN = 0; }
336 catch(exception& e) {
337 m->errorOut(e, "Weighted", "getValues");
341 /**************************************************************************************************/
342 vector<double> Weighted::getBranchLengthSums(Tree* t) {
347 for(int v=0;v<t->getNumLeaves();v++){
349 if (m->control_pressed) { return sums; }
354 //while you aren't at root
355 while(t->tree[index].getParent() != -1){
358 if(t->tree[index].getBranchLength() != -1){
359 sum += abs(t->tree[index].getBranchLength());
361 index = t->tree[index].getParent();
364 //get last breanch length added
365 if(t->tree[index].getBranchLength() != -1){
366 sum += abs(t->tree[index].getBranchLength());
374 catch(exception& e) {
375 m->errorOut(e, "Weighted", "getBranchLengthSums");
379 /**************************************************************************************************/