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