]> git.donarmstrong.com Git - mothur.git/blob - weighted.cpp
d8a4b857ad19d8057ed4eb11a592aa11e6787721
[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                 data.clear(); //clear out old values
17                 int numGroups;
18                 vector<double> D;
19                 processors = p;
20                 outputDir = o;
21                 
22                 numGroups = m->getNumGroups();
23                 
24                 if (m->control_pressed) { return data; }
25                 
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->getGroups())[i]); groups.push_back((m->getGroups())[l]);
33                                 namesOfGroupCombos.push_back(groups);
34                         }
35                 }
36                 
37                 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
38                         if(processors == 1){
39                                 data = driver(t, namesOfGroupCombos, 0, namesOfGroupCombos.size());
40                         }else{
41                                 int numPairs = namesOfGroupCombos.size();
42                                 
43                                 int numPairsPerProcessor = numPairs / processors;
44                                 
45                                 for (int i = 0; i < processors; i++) {
46                                         int startPos = i * numPairsPerProcessor;
47                                         if(i == processors - 1){
48                                                 numPairsPerProcessor = numPairs - i * numPairsPerProcessor;
49                                         }
50                                         lines.push_back(linePair(startPos, numPairsPerProcessor));
51                                 }
52
53                                 data = createProcesses(t, namesOfGroupCombos);
54                                 
55                                 lines.clear();
56                         }
57                 #else
58                         data = driver(t, namesOfGroupCombos, 0, namesOfGroupCombos.size());
59                 #endif
60                 
61                 return data;
62         }
63         catch(exception& e) {
64                 m->errorOut(e, "Weighted", "getValues");
65                 exit(1);
66         }
67 }
68 /**************************************************************************************************/
69
70 EstOutput Weighted::createProcesses(Tree* t, vector< vector<string> > namesOfGroupCombos) {
71         try {
72 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
73                 int process = 1;
74                 vector<int> processIDS;
75                 
76                 EstOutput results;
77                 
78                 //loop through and create all the processes you want
79                 while (process != processors) {
80                         int pid = fork();
81                         
82                         if (pid > 0) {
83                                 processIDS.push_back(pid);  //create map from line number to pid so you can append files in correct order later
84                                 process++;
85                         }else if (pid == 0){
86         
87                                 EstOutput Myresults;
88                                 Myresults = driver(t, namesOfGroupCombos, lines[process].start, lines[process].num);
89                         
90                                 m->mothurOut("Merging results."); m->mothurOutEndLine();
91                                 
92                                 //pass numSeqs to parent
93                                 ofstream out;
94
95                                 string tempFile = outputDir + toString(getpid()) + ".weighted.results.temp";
96         
97                                 m->openOutputFile(tempFile, out);
98         
99                                 out << Myresults.size() << endl;
100                                 for (int i = 0; i < Myresults.size(); i++) {  out << Myresults[i] << '\t';  } out << endl;
101                                 out.close();
102                                 
103                                 exit(0);
104                         }else { 
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); }
107                                 exit(0);
108                         }
109                 }
110         
111                 results = driver(t, namesOfGroupCombos, lines[0].start, lines[0].num);
112         
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];
116                         wait(&temp);
117                 }
118         
119                 if (m->control_pressed) { return results; }
120                 
121                 //get data created by processes
122                 for (int i=0;i<(processors-1);i++) { 
123                         ifstream in;
124                         string s = outputDir + toString(processIDS[i]) + ".weighted.results.temp";
125                         m->openInputFile(s, in);
126                         
127                         //get quantiles
128                         while (!in.eof()) {
129                                 int num;
130                                 in >> num; m->gobble(in);
131                                 
132                                 if (m->control_pressed) { break; }
133
134                                 double w; 
135                                 for (int j = 0; j < num; j++) {
136                                         in >> w;
137                                         results.push_back(w);
138                                 }
139                                 m->gobble(in);
140                         }
141                         in.close();
142                         m->mothurRemove(s);
143                 }
144                 
145                 m->mothurOut("DONE."); m->mothurOutEndLine(); m->mothurOutEndLine();
146                 
147                 return results;
148 #endif          
149         }
150         catch(exception& e) {
151                 m->errorOut(e, "Weighted", "createProcesses");
152                 exit(1);
153         }
154 }
155 /**************************************************************************************************/
156 EstOutput Weighted::driver(Tree* t, vector< vector<string> > namesOfGroupCombos, int start, int num) { 
157  try {
158                 EstOutput results;
159                 vector<double> D;
160                 
161                 int count = 0;
162                 for (int h = start; h < (start+num); h++) {
163                 
164                         if (m->control_pressed) { return results; }
165                 
166                         //initialize weighted score
167                         string groupA = namesOfGroupCombos[h][0]; 
168                         string groupB = namesOfGroupCombos[h][1];
169                         
170                         set<int> validBranches;
171                         WScore[groupA+groupB] = 0.0;
172                         D.push_back(0.0000); //initialize a spot in D for each combination
173                         
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;
178                                 
179                                 double sum = getLengthToRoot(t, t->groupNodeInfo[groupA][j], groupA, groupB);
180                                 double weightedSum = ((numSeqsInGroupI * sum) / (double)tmap->seqsPerGroup[groupA]);
181                         
182                                 D[count] += weightedSum;
183                         }
184                         
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;
189                                 
190                                 double sum = getLengthToRoot(t, t->groupNodeInfo[groupB][j], groupA, groupB);
191                                 double weightedSum = ((numSeqsInGroupL * sum) / (double)tmap->seqsPerGroup[groupB]);
192                         
193                                 D[count] += weightedSum;
194                         }
195                         count++;
196                 }
197          
198                 //calculate u for the group comb 
199                 for (int h = start; h < (start+num); h++) {     
200                         //report progress
201                         //m->mothurOut("Processing combo: " + toString(h)); m->mothurOutEndLine();
202                                                 
203                         string groupA = namesOfGroupCombos[h][0]; 
204                         string groupB = namesOfGroupCombos[h][1];
205                         
206                         //calculate u for the group comb 
207                         for(int i=0;i<t->getNumNodes();i++){
208                                 
209                                 if (m->control_pressed) { return data; }
210                                 
211                                 double u;
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];
218                                 }else { u = 0.00; }
219                                 
220                                 
221                                 //does this node have descendants from group l
222                                 it = t->tree[i].pcount.find(groupB);
223                                 
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];
227                                 }
228                                 
229                                 if (includeRoot) {
230                                         if (t->tree[i].getBranchLength() != -1) {
231                                                 u = abs(u * t->tree[i].getBranchLength());
232                                                 WScore[(groupA+groupB)] += u; 
233                                         }
234                                 }else {
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; 
240                                                 }
241                                         }
242                                 }
243                         }
244                         
245                 }
246                 
247                 /********************************************************/
248                 //calculate weighted score for the group combination
249                 double UN;      
250                 count = 0;
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);
255                         count++;
256                 }
257                                 
258                 return results; 
259         }
260         catch(exception& e) {
261                 m->errorOut(e, "Weighted", "driver");
262                 exit(1);
263         }
264 }
265 /**************************************************************************************************/
266 EstOutput Weighted::getValues(Tree* t, string groupA, string groupB) { 
267  try {
268                 
269                 data.clear(); //clear out old values
270                 
271                 if (m->control_pressed) { return data; }
272                 
273                 //initialize weighted score
274                 WScore[(groupA+groupB)] = 0.0;
275                 double D = 0.0;
276                 set<int> validBranches;
277                 
278                 vector<string> groups; groups.push_back(groupA); groups.push_back(groupB);
279                 
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;
284                         
285                         double sum = getLengthToRoot(t, t->groupNodeInfo[groups[0]][j], groups[0], groups[1]);
286                         double weightedSum = ((numSeqsInGroupI * sum) / (double)tmap->seqsPerGroup[groups[0]]);
287                 
288                         D += weightedSum;
289                 }
290                 
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;
295                         
296                         double sum = getLengthToRoot(t, t->groupNodeInfo[groups[1]][j], groups[0], groups[1]);
297                         double weightedSum = ((numSeqsInGroupL * sum) / (double)tmap->seqsPerGroup[groups[1]]);
298                 
299                         D += weightedSum;
300                 }
301                                 
302                 //calculate u for the group comb 
303                 for(int i=0;i<t->getNumNodes();i++){
304                  
305                         if (m->control_pressed) { return data; }
306                         
307                         double u;
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];
314                         }else { u = 0.00; }
315                         
316                         
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];
322                         }
323                         
324                         if (includeRoot) {
325                                 if (t->tree[i].getBranchLength() != -1) {
326                                         u = abs(u * t->tree[i].getBranchLength());
327                                         WScore[(groupA+groupB)] += u;
328                                 }
329                         }else{
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;
335                                         }
336                                 }
337                         }
338                 }               
339                 /********************************************************/
340          
341                 //calculate weighted score for the group combination
342                 double UN;      
343                 UN = (WScore[(groupA+groupB)] / D);
344                 
345                 if (isnan(UN) || isinf(UN)) { UN = 0; } 
346                 data.push_back(UN);
347                                 
348                 return data; 
349         }
350         catch(exception& e) {
351                 m->errorOut(e, "Weighted", "getValues");
352                 exit(1);
353         }
354 }
355 /**************************************************************************************************/
356 double Weighted::getLengthToRoot(Tree* t, int v, string groupA, string groupB) { 
357         try {
358                 
359                 double sum = 0.0;
360                 int index = v;
361         
362                 //you are a leaf
363                 if(t->tree[index].getBranchLength() != -1){     sum += abs(t->tree[index].getBranchLength());   }
364                 double tempTotal = 0.0;
365                 index = t->tree[index].getParent();     
366                 
367                 vector<string> grouping; grouping.push_back(groupA); grouping.push_back(groupB);
368                 
369                 rootForGrouping[grouping].insert(index);
370                         
371                 //while you aren't at root
372                 while(t->tree[index].getParent() != -1){
373
374                         if (m->control_pressed) {  return sum; }
375                         
376                         int parent = t->tree[index].getParent();
377                         
378                         if (includeRoot) { //add everyone
379                                 if(t->tree[index].getBranchLength() != -1){     sum += abs(t->tree[index].getBranchLength());   }
380                         }else {
381                                 
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();
386                                 
387                                 int sib = lc;
388                                 if (lc == index) { sib = rc; }
389                                 
390                                 map<string, int>::iterator itGroup;
391                                 int pcountSize = 0;
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++;  } 
396                                 
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;
401                                                 tempTotal = 0.0;
402                                         }else {
403                                                 sum += tempTotal;
404                                                 tempTotal = 0.0;
405                                         }
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()); 
411                                         }
412                                 }
413                         }
414                         
415                         index = parent; 
416                 }
417                 
418                 //get all nodes above the root to add so we don't add their u values above
419                 index = *(rootForGrouping[grouping].begin());
420                 
421                 while(t->tree[index].getParent() != -1){
422                         int parent = t->tree[index].getParent();
423                         rootForGrouping[grouping].insert(parent);
424                         index = parent;
425                 }
426                 return sum;
427         }
428         catch(exception& e) {
429                 m->errorOut(e, "Weighted", "getBranchLengthSums");
430                 exit(1);
431         }
432 }
433 /**************************************************************************************************/
434
435