]> git.donarmstrong.com Git - mothur.git/blob - weighted.cpp
6cbeae8572010f36d943121e0aceb689a460e444
[mothur.git] / weighted.cpp
1 /*
2  *  weighted.cpp
3  *  Mothur
4  *
5  *  Created by Sarah Westcott on 2/9/09.
6  *  Copyright 2009 Schloss Lab UMASS Amherst. All rights reserved.
7  *
8  */
9
10 #include "weighted.h"
11
12 /**************************************************************************************************/
13
14 EstOutput Weighted::getValues(Tree* t, int p, string o) {
15     try {
16                 globaldata = GlobalData::getInstance();
17                 
18                 data.clear(); //clear out old values
19                 int numGroups;
20                 vector<double> D;
21                 processors = p;
22                 outputDir = o;
23                 
24                 numGroups = globaldata->Groups.size();
25                 
26                 if (m->control_pressed) { return data; }
27                 
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);
36                         }
37                 }
38                 
39                 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
40                         if(processors == 1){
41                                 data = driver(t, namesOfGroupCombos, 0, namesOfGroupCombos.size());
42                         }else{
43                                 int numPairs = namesOfGroupCombos.size();
44                                 
45                                 int numPairsPerProcessor = numPairs / processors;
46                                 
47                                 for (int i = 0; i < processors; i++) {
48                                         int startPos = i * numPairsPerProcessor;
49                                         if(i == processors - 1){
50                                                 numPairsPerProcessor = numPairs - i * numPairsPerProcessor;
51                                         }
52                                         lines.push_back(linePair(startPos, numPairsPerProcessor));
53                                 }
54
55                                 data = createProcesses(t, namesOfGroupCombos);
56                                 
57                                 lines.clear();
58                         }
59                 #else
60                         data = driver(t, namesOfGroupCombos, 0, namesOfGroupCombos.size());
61                 #endif
62                 
63                 return data;
64         }
65         catch(exception& e) {
66                 m->errorOut(e, "Weighted", "getValues");
67                 exit(1);
68         }
69 }
70 /**************************************************************************************************/
71
72 EstOutput Weighted::createProcesses(Tree* t, vector< vector<string> > namesOfGroupCombos) {
73         try {
74 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
75                 int process = 1;
76                 int num = 0;
77                 vector<int> processIDS;
78                 
79                 EstOutput results;
80                 
81                 //loop through and create all the processes you want
82                 while (process != processors) {
83                         int pid = fork();
84                         
85                         if (pid > 0) {
86                                 processIDS.push_back(pid);  //create map from line number to pid so you can append files in correct order later
87                                 process++;
88                         }else if (pid == 0){
89         
90                                 EstOutput Myresults;
91                                 Myresults = driver(t, namesOfGroupCombos, lines[process].start, lines[process].num);
92                         
93                                 m->mothurOut("Merging results."); m->mothurOutEndLine();
94                                 
95                                 //pass numSeqs to parent
96                                 ofstream out;
97
98                                 string tempFile = outputDir + toString(getpid()) + ".weighted.results.temp";
99         
100                                 m->openOutputFile(tempFile, out);
101         
102                                 out << Myresults.size() << endl;
103                                 for (int i = 0; i < Myresults.size(); i++) {  out << Myresults[i] << '\t';  } out << endl;
104                                 out.close();
105                                 
106                                 exit(0);
107                         }else { m->mothurOut("unable to spawn the necessary processes."); m->mothurOutEndLine(); exit(0); }
108                 }
109         
110                 results = driver(t, namesOfGroupCombos, lines[0].start, lines[0].num);
111         
112                 //force parent to wait until all the processes are done
113                 for (int i=0;i<(processors-1);i++) { 
114                         int temp = processIDS[i];
115                         wait(&temp);
116                 }
117         
118                 if (m->control_pressed) { return results; }
119                 
120                 //get data created by processes
121                 for (int i=0;i<(processors-1);i++) { 
122                         ifstream in;
123                         string s = outputDir + toString(processIDS[i]) + ".weighted.results.temp";
124                         m->openInputFile(s, in);
125                         
126                         //get quantiles
127                         while (!in.eof()) {
128                                 int num;
129                                 in >> num; m->gobble(in);
130                                 
131                                 if (m->control_pressed) { break; }
132
133                                 double w; 
134                                 for (int j = 0; j < num; j++) {
135                                         in >> w;
136                                         results.push_back(w);
137                                 }
138                                 m->gobble(in);
139                         }
140                         in.close();
141                         remove(s.c_str());
142                 }
143                 
144                 m->mothurOut("DONE."); m->mothurOutEndLine(); m->mothurOutEndLine();
145                 
146                 return results;
147 #endif          
148         }
149         catch(exception& e) {
150                 m->errorOut(e, "Weighted", "createProcesses");
151                 exit(1);
152         }
153 }
154 /**************************************************************************************************/
155 EstOutput Weighted::driver(Tree* t, vector< vector<string> > namesOfGroupCombos, int start, int num) { 
156  try {
157                 EstOutput results;
158                 vector<double> D;
159                 
160                 int count = 0;
161                 for (int h = start; h < (start+num); h++) {
162                 
163                         if (m->control_pressed) { return results; }
164                 
165                         //initialize weighted score
166                         string groupA = namesOfGroupCombos[h][0]; 
167                         string groupB = namesOfGroupCombos[h][1];
168                         
169                         set<int> validBranches;
170                         WScore[groupA+groupB] = 0.0;
171                         D.push_back(0.0000); //initialize a spot in D for each combination
172                         
173                         //adding the wieghted sums from group i
174                         for (int j = 0; j < t->groupNodeInfo[groupA].size(); j++) { //the leaf nodes that have seqs from group i
175                                 map<string, int>::iterator it = t->tree[t->groupNodeInfo[groupA][j]].pcount.find(groupA);
176                                 int numSeqsInGroupI = it->second;
177                                 
178                                 double sum = getLengthToRoot(t, t->groupNodeInfo[groupA][j], groupA, groupB);
179                                 double weightedSum = ((numSeqsInGroupI * sum) / (double)tmap->seqsPerGroup[groupA]);
180                         
181                                 D[count] += weightedSum;
182                         }
183                         
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;
188                                 
189                                 double sum = getLengthToRoot(t, t->groupNodeInfo[groupB][j], groupA, groupB);
190                                 double weightedSum = ((numSeqsInGroupL * sum) / (double)tmap->seqsPerGroup[groupB]);
191                         
192                                 D[count] += weightedSum;
193                         }
194                         count++;
195                 }
196                 
197                 //calculate u for the group comb 
198                 
199                 for (int h = start; h < (start+num); h++) {     
200                         //report progress
201                         m->mothurOut("Processing combo: " + toString(h)); m->mothurOutEndLine();
202                         
203                         int numLeaves = t->getNumLeaves();
204                         map<int, double> tempTotals; //maps node to total Branch Length
205                         map<int, int> nodePcountSize; //maps node to pcountSize
206                         
207                         string groupA = namesOfGroupCombos[h][0]; 
208                         string groupB = namesOfGroupCombos[h][1];
209                         
210                         //calculate u for the group comb 
211                         for(int i=0;i<t->getNumNodes();i++){
212                                 
213                                 if (m->control_pressed) { return data; }
214                                 
215                                 double u;
216                                 int pcountSize = 0;
217                                 //does this node have descendants from groupA
218                                 it = t->tree[i].pcount.find(groupA);
219                                 //if it does u = # of its descendants with a certain group / total number in tree with a certain group
220                                 if (it != t->tree[i].pcount.end()) {
221                                         u = (double) t->tree[i].pcount[groupA] / (double) tmap->seqsPerGroup[groupA];
222                                         pcountSize++;
223                                 }else { u = 0.00; }
224                                 
225                                 
226                                 //does this node have descendants from group l
227                                 it = t->tree[i].pcount.find(groupB);
228                                 //if it does subtract their percentage from u
229                                 if (it != t->tree[i].pcount.end()) {
230                                         u -= (double) t->tree[i].pcount[groupB] / (double) tmap->seqsPerGroup[groupB];
231                                         pcountSize++;
232                                 }
233                                 
234                                 u = abs(u * t->tree[i].getBranchLength());
235                                 
236                                 nodePcountSize[i] = pcountSize;
237                                 
238                                 //if you are a leaf from a users group add to total
239                                 if (i < numLeaves) {
240                                         if ((t->tree[i].getBranchLength() != -1) && pcountSize != 0) { 
241                                                 //cout << "added to total" << endl; 
242                                                 WScore[(groupA+groupB)] += u; 
243                                         }
244                                         tempTotals[i] = 0.0;  //we don't care about you, or we have already added you
245                                 }else{ //if you are not a leaf 
246                                         //do both your chidren have have descendants from the users groups? 
247                                         int lc = t->tree[i].getLChild();
248                                         int rc = t->tree[i].getRChild();
249                                         
250                                         //if yes, add your childrens tempTotals
251                                         if ((nodePcountSize[lc] != 0) && (nodePcountSize[rc] != 0)) {
252                                                 WScore[(groupA+groupB)] += tempTotals[lc] + tempTotals[rc]; 
253                                                 //cout << "added to total " << tempTotals[lc] << '\t' << tempTotals[rc] << endl;
254                                                 if (t->tree[i].getBranchLength() != -1) {
255                                                         tempTotals[i] = u;
256                                                 }else {
257                                                         tempTotals[i] = 0.0;
258                                                 }
259                                         }else if ((nodePcountSize[lc] == 0) && (nodePcountSize[rc] == 0)) { tempTotals[i] = 0.0;  //we don't care about you
260                                         }else { //if no, your tempTotal is your childrens temp totals + your branch length
261                                                 tempTotals[i] = tempTotals[lc] + tempTotals[rc] + u; 
262                                         }
263                                         //cout << "temptotal = "<< tempTotals[i] << endl;
264                                 }
265                         }
266                 }
267                 
268                 /********************************************************/
269                 //calculate weighted score for the group combination
270                 double UN;      
271                 count = 0;
272                 for (int h = start; h < (start+num); h++) {
273                         UN = (WScore[namesOfGroupCombos[h][0]+namesOfGroupCombos[h][1]] / D[count]);
274                 
275                         if (isnan(UN) || isinf(UN)) { UN = 0; } 
276                         results.push_back(UN);
277                         count++;
278                 }
279                                 
280                 return results; 
281         }
282         catch(exception& e) {
283                 m->errorOut(e, "Weighted", "driver");
284                 exit(1);
285         }
286 }
287 /**************************************************************************************************/
288 EstOutput Weighted::getValues(Tree* t, string groupA, string groupB) { 
289  try {
290                 globaldata = GlobalData::getInstance();
291                 
292                 data.clear(); //clear out old values
293                 
294                 if (m->control_pressed) { return data; }
295                 
296                 //initialize weighted score
297                 WScore[(groupA+groupB)] = 0.0;
298                 double D = 0.0;
299                 set<int> validBranches;
300                 
301                 vector<string> groups; groups.push_back(groupA); groups.push_back(groupB);
302                 
303                 //adding the wieghted sums from group i
304                 for (int j = 0; j < t->groupNodeInfo[groups[0]].size(); j++) { //the leaf nodes that have seqs from group i
305                         map<string, int>::iterator it = t->tree[t->groupNodeInfo[groups[0]][j]].pcount.find(groups[0]);
306                         int numSeqsInGroupI = it->second;
307                         
308                         double sum = getLengthToRoot(t, t->groupNodeInfo[groups[0]][j], groups[0], groups[1]);
309                         double weightedSum = ((numSeqsInGroupI * sum) / (double)tmap->seqsPerGroup[groups[0]]);
310                 
311                         D += weightedSum;
312                 }
313                 
314                 //adding the wieghted sums from group l
315                 for (int j = 0; j < t->groupNodeInfo[groups[1]].size(); j++) { //the leaf nodes that have seqs from group l
316                         map<string, int>::iterator it = t->tree[t->groupNodeInfo[groups[1]][j]].pcount.find(groups[1]);
317                         int numSeqsInGroupL = it->second;
318                         
319                         double sum = getLengthToRoot(t, t->groupNodeInfo[groups[1]][j], groups[0], groups[1]);
320                         double weightedSum = ((numSeqsInGroupL * sum) / (double)tmap->seqsPerGroup[groups[1]]);
321                 
322                         D += weightedSum;
323                 }
324                 
325                 int numLeaves = t->getNumLeaves();
326                 map<int, double> tempTotals; //maps node to total Branch Length
327                 map<int, int> nodePcountSize; //maps node to pcountSize
328                 
329                 //calculate u for the group comb 
330                 for(int i=0;i<t->getNumNodes();i++){
331                 
332                         if (m->control_pressed) { return data; }
333                         
334                         double u;
335                         int pcountSize = 0;
336                         //does this node have descendants from groupA
337                         it = t->tree[i].pcount.find(groupA);
338                         //if it does u = # of its descendants with a certain group / total number in tree with a certain group
339                         if (it != t->tree[i].pcount.end()) {
340                                 u = (double) t->tree[i].pcount[groupA] / (double) tmap->seqsPerGroup[groupA];
341                                 pcountSize++;
342                         }else { u = 0.00; }
343                 
344                                                 
345                         //does this node have descendants from group l
346                         it = t->tree[i].pcount.find(groupB);
347                         //if it does subtract their percentage from u
348                         if (it != t->tree[i].pcount.end()) {
349                                 u -= (double) t->tree[i].pcount[groupB] / (double) tmap->seqsPerGroup[groupB];
350                                 pcountSize++;
351                         }
352                                                 
353                         u = abs(u * t->tree[i].getBranchLength());
354                         
355                         nodePcountSize[i] = pcountSize;
356                                 
357                         //if you are a leaf from a users group add to total
358                         if (i < numLeaves) {
359                                 if ((t->tree[i].getBranchLength() != -1) && pcountSize != 0) { 
360                                 //cout << "added to total" << endl; 
361                                         WScore[(groupA+groupB)] += u; 
362                                 }
363                                 tempTotals[i] = 0.0;  //we don't care about you, or we have already added you
364                         }else{ //if you are not a leaf 
365                                 //do both your chidren have have descendants from the users groups? 
366                                 int lc = t->tree[i].getLChild();
367                                 int rc = t->tree[i].getRChild();
368                                 
369                                 //if yes, add your childrens tempTotals
370                                 if ((nodePcountSize[lc] != 0) && (nodePcountSize[rc] != 0)) {
371                                         WScore[(groupA+groupB)] += tempTotals[lc] + tempTotals[rc]; 
372                                         //cout << "added to total " << tempTotals[lc] << '\t' << tempTotals[rc] << endl;
373                                         if (t->tree[i].getBranchLength() != -1) {
374                                                 tempTotals[i] = u;
375                                         }else {
376                                                 tempTotals[i] = 0.0;
377                                         }
378                                 }else if ((nodePcountSize[lc] == 0) && (nodePcountSize[rc] == 0)) { tempTotals[i] = 0.0;  //we don't care about you
379                                 }else { //if no, your tempTotal is your childrens temp totals + your branch length
380                                         tempTotals[i] = tempTotals[lc] + tempTotals[rc] + u; 
381                                 }
382                                 //cout << "temptotal = "<< tempTotals[i] << endl;
383                         }
384                 }
385                 
386                 /********************************************************/
387                 
388                 //calculate weighted score for the group combination
389                 double UN;      
390                 UN = (WScore[(groupA+groupB)] / D);
391                 
392                 if (isnan(UN) || isinf(UN)) { UN = 0; } 
393                 data.push_back(UN);
394                                 
395                 return data; 
396         }
397         catch(exception& e) {
398                 m->errorOut(e, "Weighted", "getValues");
399                 exit(1);
400         }
401 }
402 /**************************************************************************************************/
403 double Weighted::getLengthToRoot(Tree* t, int v, string groupA, string groupB) { 
404         try {
405                 
406                 double sum = 0.0;
407                 map<int, double> tempTotals; //maps node to total Branch Length
408                 map<int, int> nodePcountSize; //maps node to pcountSize
409                 map<int, int>::iterator itCount;
410
411                 int index = v;
412         
413                 //you are a leaf
414                 if(t->tree[index].getBranchLength() != -1){     sum += abs(t->tree[index].getBranchLength());   }
415                 tempTotals[index] = 0.0;
416                 index = t->tree[index].getParent();     
417                         
418                 //while you aren't at root
419                 while(t->tree[index].getParent() != -1){
420
421                         if (m->control_pressed) {  return sum; }
422                                 
423                         int pcountSize = 0;
424                         map<string, int>::iterator itGroup = t->tree[index].pcount.find(groupA);
425                         if (itGroup != t->tree[index].pcount.end()) { pcountSize++;  } 
426                         itGroup = t->tree[index].pcount.find(groupB);
427                         if (itGroup != t->tree[index].pcount.end()) { pcountSize++;  } 
428                 
429                         nodePcountSize[index] = pcountSize;
430                         
431                         //do both your chidren have have descendants from the users groups? 
432                         int lc = t->tree[index].getLChild();
433                         int rc = t->tree[index].getRChild();
434                         
435                         itCount = nodePcountSize.find(lc); 
436                         if (itCount == nodePcountSize.end()) { 
437                                 int LpcountSize = 0;
438                                 itGroup = t->tree[lc].pcount.find(groupA);
439                                 if (itGroup != t->tree[lc].pcount.end()) { LpcountSize++;  } 
440                                 itGroup = t->tree[lc].pcount.find(groupB);
441                                 if (itGroup != t->tree[lc].pcount.end()) { LpcountSize++;  } 
442                                 nodePcountSize[lc] = LpcountSize;
443                         }
444                         
445                         itCount = nodePcountSize.find(rc); 
446                         if (itCount == nodePcountSize.end()) { 
447                                 int RpcountSize = 0;
448                                 itGroup = t->tree[rc].pcount.find(groupA);
449                                 if (itGroup != t->tree[rc].pcount.end()) { RpcountSize++;  } 
450                                 itGroup = t->tree[rc].pcount.find(groupB);
451                                 if (itGroup != t->tree[rc].pcount.end()) { RpcountSize++;  } 
452                                 nodePcountSize[rc] = RpcountSize;
453                         }
454                                 
455                         //if yes, add your childrens tempTotals
456                         if ((nodePcountSize[lc] != 0) && (nodePcountSize[rc] != 0)) {
457                                 sum += tempTotals[lc] + tempTotals[rc]; 
458
459                                 //cout << "added to total " << tempTotals[lc] << '\t' << tempTotals[rc] << endl;
460                                 if (t->tree[index].getBranchLength() != -1) {
461                                         tempTotals[index] = abs(t->tree[index].getBranchLength());
462                                 }else {
463                                         tempTotals[index] = 0.0;
464                                 }
465                         }else { //if no, your tempTotal is your childrens temp totals + your branch length
466                                 tempTotals[index] = tempTotals[lc] + tempTotals[rc] + abs(t->tree[index].getBranchLength()); 
467                         }
468                         //cout << "temptotal = "<< tempTotals[i] << endl;
469                         
470                         index = t->tree[index].getParent();     
471                 }
472
473                 return sum;
474         }
475         catch(exception& e) {
476                 m->errorOut(e, "Weighted", "getBranchLengthSums");
477                 exit(1);
478         }
479 }
480 /**************************************************************************************************/
481
482
483