]> git.donarmstrong.com Git - mothur.git/blob - weighted.cpp
cluster.split fix
[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 { 
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); }
110                                 exit(0);
111                         }
112                 }
113         
114                 results = driver(t, namesOfGroupCombos, lines[0].start, lines[0].num);
115         
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];
119                         wait(&temp);
120                 }
121         
122                 if (m->control_pressed) { return results; }
123                 
124                 //get data created by processes
125                 for (int i=0;i<(processors-1);i++) { 
126                         ifstream in;
127                         string s = outputDir + toString(processIDS[i]) + ".weighted.results.temp";
128                         m->openInputFile(s, in);
129                         
130                         //get quantiles
131                         while (!in.eof()) {
132                                 int num;
133                                 in >> num; m->gobble(in);
134                                 
135                                 if (m->control_pressed) { break; }
136
137                                 double w; 
138                                 for (int j = 0; j < num; j++) {
139                                         in >> w;
140                                         results.push_back(w);
141                                 }
142                                 m->gobble(in);
143                         }
144                         in.close();
145                         remove(s.c_str());
146                 }
147                 
148                 m->mothurOut("DONE."); m->mothurOutEndLine(); m->mothurOutEndLine();
149                 
150                 return results;
151 #endif          
152         }
153         catch(exception& e) {
154                 m->errorOut(e, "Weighted", "createProcesses");
155                 exit(1);
156         }
157 }
158 /**************************************************************************************************/
159 EstOutput Weighted::driver(Tree* t, vector< vector<string> > namesOfGroupCombos, int start, int num) { 
160  try {
161                 EstOutput results;
162                 vector<double> D;
163                 
164                 int count = 0;
165                 for (int h = start; h < (start+num); h++) {
166                 
167                         if (m->control_pressed) { return results; }
168                 
169                         //initialize weighted score
170                         string groupA = namesOfGroupCombos[h][0]; 
171                         string groupB = namesOfGroupCombos[h][1];
172                         
173                         set<int> validBranches;
174                         WScore[groupA+groupB] = 0.0;
175                         D.push_back(0.0000); //initialize a spot in D for each combination
176                         
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;
181                                 
182                                 double sum = getLengthToRoot(t, t->groupNodeInfo[groupA][j], groupA, groupB);
183                                 double weightedSum = ((numSeqsInGroupI * sum) / (double)tmap->seqsPerGroup[groupA]);
184                         
185                                 D[count] += weightedSum;
186                         }
187                         
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;
192                                 
193                                 double sum = getLengthToRoot(t, t->groupNodeInfo[groupB][j], groupA, groupB);
194                                 double weightedSum = ((numSeqsInGroupL * sum) / (double)tmap->seqsPerGroup[groupB]);
195                         
196                                 D[count] += weightedSum;
197                         }
198                         count++;
199                 }
200                 
201                 //calculate u for the group comb 
202                 
203                 for (int h = start; h < (start+num); h++) {     
204                         //report progress
205                         m->mothurOut("Processing combo: " + toString(h)); m->mothurOutEndLine();
206                         
207                         int numLeaves = t->getNumLeaves();
208                         map<int, double> tempTotals; //maps node to total Branch Length
209                         map<int, int> nodePcountSize; //maps node to pcountSize
210                         
211                         string groupA = namesOfGroupCombos[h][0]; 
212                         string groupB = namesOfGroupCombos[h][1];
213                         
214                         //calculate u for the group comb 
215                         for(int i=0;i<t->getNumNodes();i++){
216                                 
217                                 if (m->control_pressed) { return data; }
218                                 
219                                 double u;
220                                 int pcountSize = 0;
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];
226                                         pcountSize++;
227                                 }else { u = 0.00; }
228                                 
229                                 
230                                 //does this node have descendants from group l
231                                 it = t->tree[i].pcount.find(groupB);
232                                 //if it does subtract their percentage from u
233                                 if (it != t->tree[i].pcount.end()) {
234                                         u -= (double) t->tree[i].pcount[groupB] / (double) tmap->seqsPerGroup[groupB];
235                                         pcountSize++;
236                                 }
237                                 
238                                 u = abs(u * t->tree[i].getBranchLength());
239                                 
240                                 nodePcountSize[i] = pcountSize;
241                                 
242                                 //if you are a leaf from a users group add to total
243                                 if (i < numLeaves) {
244                                         if ((t->tree[i].getBranchLength() != -1) && pcountSize != 0) { 
245                                                 //cout << "added to total" << endl; 
246                                                 WScore[(groupA+groupB)] += u; 
247                                         }
248                                         tempTotals[i] = 0.0;  //we don't care about you, or we have already added you
249                                 }else{ //if you are not a leaf 
250                                         //do both your chidren have have descendants from the users groups? 
251                                         int lc = t->tree[i].getLChild();
252                                         int rc = t->tree[i].getRChild();
253                                         
254                                         //if yes, add your childrens tempTotals
255                                         if ((nodePcountSize[lc] != 0) && (nodePcountSize[rc] != 0)) {
256                                                 WScore[(groupA+groupB)] += tempTotals[lc] + tempTotals[rc]; 
257                                                 //cout << "added to total " << tempTotals[lc] << '\t' << tempTotals[rc] << endl;
258                                                 if (t->tree[i].getBranchLength() != -1) {
259                                                         tempTotals[i] = u;
260                                                 }else {
261                                                         tempTotals[i] = 0.0;
262                                                 }
263                                         }else if ((nodePcountSize[lc] == 0) && (nodePcountSize[rc] == 0)) { tempTotals[i] = 0.0;  //we don't care about you
264                                         }else { //if no, your tempTotal is your childrens temp totals + your branch length
265                                                 tempTotals[i] = tempTotals[lc] + tempTotals[rc] + u; 
266                                         }
267                                         //cout << "temptotal = "<< tempTotals[i] << endl;
268                                 }
269                         }
270                 }
271                 
272                 /********************************************************/
273                 //calculate weighted score for the group combination
274                 double UN;      
275                 count = 0;
276                 for (int h = start; h < (start+num); h++) {
277                         UN = (WScore[namesOfGroupCombos[h][0]+namesOfGroupCombos[h][1]] / D[count]);
278                 
279                         if (isnan(UN) || isinf(UN)) { UN = 0; } 
280                         results.push_back(UN);
281                         count++;
282                 }
283                                 
284                 return results; 
285         }
286         catch(exception& e) {
287                 m->errorOut(e, "Weighted", "driver");
288                 exit(1);
289         }
290 }
291 /**************************************************************************************************/
292 EstOutput Weighted::getValues(Tree* t, string groupA, string groupB) { 
293  try {
294                 globaldata = GlobalData::getInstance();
295                 
296                 data.clear(); //clear out old values
297                 
298                 if (m->control_pressed) { return data; }
299                 
300                 //initialize weighted score
301                 WScore[(groupA+groupB)] = 0.0;
302                 double D = 0.0;
303                 set<int> validBranches;
304                 
305                 vector<string> groups; groups.push_back(groupA); groups.push_back(groupB);
306                 
307                 //adding the wieghted sums from group i
308                 for (int j = 0; j < t->groupNodeInfo[groups[0]].size(); j++) { //the leaf nodes that have seqs from group i
309                         map<string, int>::iterator it = t->tree[t->groupNodeInfo[groups[0]][j]].pcount.find(groups[0]);
310                         int numSeqsInGroupI = it->second;
311                         
312                         double sum = getLengthToRoot(t, t->groupNodeInfo[groups[0]][j], groups[0], groups[1]);
313                         double weightedSum = ((numSeqsInGroupI * sum) / (double)tmap->seqsPerGroup[groups[0]]);
314                 
315                         D += weightedSum;
316                 }
317                 
318                 //adding the wieghted sums from group l
319                 for (int j = 0; j < t->groupNodeInfo[groups[1]].size(); j++) { //the leaf nodes that have seqs from group l
320                         map<string, int>::iterator it = t->tree[t->groupNodeInfo[groups[1]][j]].pcount.find(groups[1]);
321                         int numSeqsInGroupL = it->second;
322                         
323                         double sum = getLengthToRoot(t, t->groupNodeInfo[groups[1]][j], groups[0], groups[1]);
324                         double weightedSum = ((numSeqsInGroupL * sum) / (double)tmap->seqsPerGroup[groups[1]]);
325                 
326                         D += weightedSum;
327                 }
328                 
329                 int numLeaves = t->getNumLeaves();
330                 map<int, double> tempTotals; //maps node to total Branch Length
331                 map<int, int> nodePcountSize; //maps node to pcountSize
332                 
333                 //calculate u for the group comb 
334                 for(int i=0;i<t->getNumNodes();i++){
335                 
336                         if (m->control_pressed) { return data; }
337                         
338                         double u;
339                         int pcountSize = 0;
340                         //does this node have descendants from groupA
341                         it = t->tree[i].pcount.find(groupA);
342                         //if it does u = # of its descendants with a certain group / total number in tree with a certain group
343                         if (it != t->tree[i].pcount.end()) {
344                                 u = (double) t->tree[i].pcount[groupA] / (double) tmap->seqsPerGroup[groupA];
345                                 pcountSize++;
346                         }else { u = 0.00; }
347                 
348                                                 
349                         //does this node have descendants from group l
350                         it = t->tree[i].pcount.find(groupB);
351                         //if it does subtract their percentage from u
352                         if (it != t->tree[i].pcount.end()) {
353                                 u -= (double) t->tree[i].pcount[groupB] / (double) tmap->seqsPerGroup[groupB];
354                                 pcountSize++;
355                         }
356                                                 
357                         u = abs(u * t->tree[i].getBranchLength());
358                         
359                         nodePcountSize[i] = pcountSize;
360                                 
361                         //if you are a leaf from a users group add to total
362                         if (i < numLeaves) {
363                                 if ((t->tree[i].getBranchLength() != -1) && pcountSize != 0) { 
364                                 //cout << "added to total" << endl; 
365                                         WScore[(groupA+groupB)] += u; 
366                                 }
367                                 tempTotals[i] = 0.0;  //we don't care about you, or we have already added you
368                         }else{ //if you are not a leaf 
369                                 //do both your chidren have have descendants from the users groups? 
370                                 int lc = t->tree[i].getLChild();
371                                 int rc = t->tree[i].getRChild();
372                                 
373                                 //if yes, add your childrens tempTotals
374                                 if ((nodePcountSize[lc] != 0) && (nodePcountSize[rc] != 0)) {
375                                         WScore[(groupA+groupB)] += tempTotals[lc] + tempTotals[rc]; 
376                                         //cout << "added to total " << tempTotals[lc] << '\t' << tempTotals[rc] << endl;
377                                         if (t->tree[i].getBranchLength() != -1) {
378                                                 tempTotals[i] = u;
379                                         }else {
380                                                 tempTotals[i] = 0.0;
381                                         }
382                                 }else if ((nodePcountSize[lc] == 0) && (nodePcountSize[rc] == 0)) { tempTotals[i] = 0.0;  //we don't care about you
383                                 }else { //if no, your tempTotal is your childrens temp totals + your branch length
384                                         tempTotals[i] = tempTotals[lc] + tempTotals[rc] + u; 
385                                 }
386                                 //cout << "temptotal = "<< tempTotals[i] << endl;
387                         }
388                 }
389                 
390                 /********************************************************/
391                 
392                 //calculate weighted score for the group combination
393                 double UN;      
394                 UN = (WScore[(groupA+groupB)] / D);
395                 
396                 if (isnan(UN) || isinf(UN)) { UN = 0; } 
397                 data.push_back(UN);
398                                 
399                 return data; 
400         }
401         catch(exception& e) {
402                 m->errorOut(e, "Weighted", "getValues");
403                 exit(1);
404         }
405 }
406 /**************************************************************************************************/
407 double Weighted::getLengthToRoot(Tree* t, int v, string groupA, string groupB) { 
408         try {
409                 
410                 double sum = 0.0;
411                 map<int, double> tempTotals; //maps node to total Branch Length
412                 map<int, int> nodePcountSize; //maps node to pcountSize
413                 map<int, int>::iterator itCount;
414
415                 int index = v;
416         
417                 //you are a leaf
418                 if(t->tree[index].getBranchLength() != -1){     sum += abs(t->tree[index].getBranchLength());   }
419                 tempTotals[index] = 0.0;
420                 index = t->tree[index].getParent();     
421                         
422                 //while you aren't at root
423                 while(t->tree[index].getParent() != -1){
424
425                         if (m->control_pressed) {  return sum; }
426                                 
427                         int pcountSize = 0;
428                         map<string, int>::iterator itGroup = t->tree[index].pcount.find(groupA);
429                         if (itGroup != t->tree[index].pcount.end()) { pcountSize++;  } 
430                         itGroup = t->tree[index].pcount.find(groupB);
431                         if (itGroup != t->tree[index].pcount.end()) { pcountSize++;  } 
432                 
433                         nodePcountSize[index] = pcountSize;
434                         
435                         //do both your chidren have have descendants from the users groups? 
436                         int lc = t->tree[index].getLChild();
437                         int rc = t->tree[index].getRChild();
438                         
439                         itCount = nodePcountSize.find(lc); 
440                         if (itCount == nodePcountSize.end()) { 
441                                 int LpcountSize = 0;
442                                 itGroup = t->tree[lc].pcount.find(groupA);
443                                 if (itGroup != t->tree[lc].pcount.end()) { LpcountSize++;  } 
444                                 itGroup = t->tree[lc].pcount.find(groupB);
445                                 if (itGroup != t->tree[lc].pcount.end()) { LpcountSize++;  } 
446                                 nodePcountSize[lc] = LpcountSize;
447                         }
448                         
449                         itCount = nodePcountSize.find(rc); 
450                         if (itCount == nodePcountSize.end()) { 
451                                 int RpcountSize = 0;
452                                 itGroup = t->tree[rc].pcount.find(groupA);
453                                 if (itGroup != t->tree[rc].pcount.end()) { RpcountSize++;  } 
454                                 itGroup = t->tree[rc].pcount.find(groupB);
455                                 if (itGroup != t->tree[rc].pcount.end()) { RpcountSize++;  } 
456                                 nodePcountSize[rc] = RpcountSize;
457                         }
458                                 
459                         //if yes, add your childrens tempTotals
460                         if ((nodePcountSize[lc] != 0) && (nodePcountSize[rc] != 0)) {
461                                 sum += tempTotals[lc] + tempTotals[rc]; 
462
463                                 //cout << "added to total " << tempTotals[lc] << '\t' << tempTotals[rc] << endl;
464                                 if (t->tree[index].getBranchLength() != -1) {
465                                         tempTotals[index] = abs(t->tree[index].getBranchLength());
466                                 }else {
467                                         tempTotals[index] = 0.0;
468                                 }
469                         }else { //if no, your tempTotal is your childrens temp totals + your branch length
470                                 tempTotals[index] = tempTotals[lc] + tempTotals[rc] + abs(t->tree[index].getBranchLength()); 
471                         }
472                         //cout << "temptotal = "<< tempTotals[i] << endl;
473                         
474                         index = t->tree[index].getParent();     
475                 }
476
477                 return sum;
478         }
479         catch(exception& e) {
480                 m->errorOut(e, "Weighted", "getBranchLengthSums");
481                 exit(1);
482         }
483 }
484 /**************************************************************************************************/
485
486
487