]> git.donarmstrong.com Git - mothur.git/blob - unweighted.cpp
fixed bug in unifrac commands with unrooted trees
[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                                 data = createProcesses(t, namesOfGroupCombos);
70                                 lines.clear();
71                         }
72                 #else
73                         data = driver(t, namesOfGroupCombos, 0, namesOfGroupCombos.size());
74                 #endif
75                 
76                 return data;
77         }
78         catch(exception& e) {
79                 m->errorOut(e, "Unweighted", "getValues");
80                 exit(1);
81         }
82 }
83 /**************************************************************************************************/
84
85 EstOutput Unweighted::createProcesses(Tree* t, vector< vector<string> > namesOfGroupCombos) {
86         try {
87 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
88                 int process = 1;
89                 vector<int> processIDS;
90                 
91                 EstOutput results;
92                 
93                 //loop through and create all the processes you want
94                 while (process != processors) {
95                         int pid = fork();
96                         
97                         if (pid > 0) {
98                                 processIDS.push_back(pid);  //create map from line number to pid so you can append files in correct order later
99                                 process++;
100                         }else if (pid == 0){
101                                 EstOutput myresults;
102                                 myresults = driver(t, namesOfGroupCombos, lines[process].start, lines[process].num);
103                                 
104                                 if (m->control_pressed) { exit(0); }
105                                 
106                                 m->mothurOut("Merging results."); m->mothurOutEndLine();
107                                 
108                                 //pass numSeqs to parent
109                                 ofstream out;
110                                 string tempFile = outputDir + toString(getpid()) + ".unweighted.results.temp";
111                                 m->openOutputFile(tempFile, out);
112                                 out << myresults.size() << endl;
113                                 for (int i = 0; i < myresults.size(); i++) {  out << myresults[i] << '\t';  } out << endl;
114                                 out.close();
115                                 
116                                 exit(0);
117                         }else { 
118                                 m->mothurOut("[ERROR]: unable to spawn the necessary processes."); m->mothurOutEndLine(); 
119                                 for (int i = 0; i < processIDS.size(); i++) { kill (processIDS[i], SIGINT); }
120                                 exit(0); 
121                         }
122                 }
123                 
124                 results = driver(t, namesOfGroupCombos, lines[0].start, lines[0].num);
125                 
126                 //force parent to wait until all the processes are done
127                 for (int i=0;i<(processors-1);i++) { 
128                         int temp = processIDS[i];
129                         wait(&temp);
130                 }
131                 
132                 if (m->control_pressed) { return results; }
133                 
134                 //get data created by processes
135                 for (int i=0;i<(processors-1);i++) { 
136                         ifstream in;
137                         string s = outputDir + toString(processIDS[i]) + ".unweighted.results.temp";
138                         m->openInputFile(s, in);
139                         
140                         //get quantiles
141                         if (!in.eof()) {
142                                 int num;
143                                 in >> num; m->gobble(in);
144                                 
145                                 if (m->control_pressed) { break; }
146                                 
147                                 double w; 
148                                 for (int j = 0; j < num; j++) {
149                                         in >> w;
150                                         results.push_back(w);
151                                 }
152                                 m->gobble(in);
153                         }
154                         in.close();
155                         remove(s.c_str());
156                 }
157                 
158                 m->mothurOut("DONE."); m->mothurOutEndLine(); m->mothurOutEndLine();
159                 
160                 return results;
161 #endif          
162         }
163         catch(exception& e) {
164                 m->errorOut(e, "Unweighted", "createProcesses");
165                 exit(1);
166         }
167 }
168 /**************************************************************************************************/
169 EstOutput Unweighted::driver(Tree* t, vector< vector<string> > namesOfGroupCombos, int start, int num) { 
170  try {
171                 
172                 EstOutput results; results.resize(num);
173                 
174                 int count = 0;
175                 int total = num;
176                 int twentyPercent = (total * 0.20);
177                 if (twentyPercent == 0) { twentyPercent = 1; }
178          
179                         
180                 for (int h = start; h < (start+num); h++) {
181         //cout << namesOfGroupCombos[h][0] << '\t' << namesOfGroupCombos[h][1] << endl;         
182                         if (m->control_pressed) { return results; }
183                 
184                         double UniqueBL=0.0000;  //a branch length is unique if it's chidren are from the same group
185                         double totalBL = 0.00;  //all branch lengths
186                         double UW = 0.00;               //Unweighted Value = UniqueBL / totalBL;
187                                                 
188                         //find a node that belongs to one of the groups in this combo
189                         int nodeBelonging = -1;
190                         for (int g = 0; g < namesOfGroupCombos[h].size(); g++) {
191                                 if (t->groupNodeInfo[namesOfGroupCombos[h][g]].size() != 0) { nodeBelonging = t->groupNodeInfo[namesOfGroupCombos[h][g]][0]; break; }
192                         }
193                         
194                         //sanity check
195                         if (nodeBelonging == -1) {
196                                 m->mothurOut("[WARNING]: cannot find a nodes in the tree from grouping "); 
197                                 for (int g = 0; g < namesOfGroupCombos[h].size()-1; g++) { m->mothurOut(namesOfGroupCombos[h][g] + "-"); }
198                                 m->mothurOut(namesOfGroupCombos[h][namesOfGroupCombos[h].size()-1]);
199                                 m->mothurOut(", skipping."); m->mothurOutEndLine(); results[count] = UW;
200                         }else{
201                         
202                                 getRoot(t, nodeBelonging, namesOfGroupCombos[h]);
203                                 
204                                 for(int i=0;i<t->getNumNodes();i++){
205                                         
206                                         if (m->control_pressed) {  return data; }
207                                         
208                                         //pcountSize = 0, they are from a branch that is entirely from a group the user doesn't want
209                                         //pcountSize = 2, not unique to one group
210                                         //pcountSize = 1, unique to one group
211                                         
212                                         int pcountSize = 0;
213                                         for (int j = 0; j < namesOfGroupCombos[h].size(); j++) {
214                                                 map<string, int>::iterator itGroup = t->tree[i].pcount.find(namesOfGroupCombos[h][j]);
215                                                 if (itGroup != t->tree[i].pcount.end()) { pcountSize++; if (pcountSize > 1) { break; } } 
216                                         }
217                                         //
218                                         //unique calc
219                                         if (pcountSize == 0) { }
220                                         else if ((t->tree[i].getBranchLength() != -1) && (pcountSize == 1) && (rootForGrouping[namesOfGroupCombos[h]].count(i) == 0)) { //you have a unique branch length and you are not the root 
221                                                 UniqueBL += abs(t->tree[i].getBranchLength()); 
222                                         }
223                                         
224                                         //total calc
225                                         if (pcountSize == 0) { }
226                                         else if ((t->tree[i].getBranchLength() != -1) && (pcountSize != 0) && (rootForGrouping[namesOfGroupCombos[h]].count(i) == 0)) { //you have a branch length and you are not the root 
227                                                 totalBL += abs(t->tree[i].getBranchLength()); 
228                                         }
229                                         
230                                 }
231         cout << UniqueBL << '\t' << totalBL << endl;            
232                                 UW = (UniqueBL / totalBL);  
233         
234                                 if (isnan(UW) || isinf(UW)) { UW = 0; }
235         
236                                 results[count] = UW;
237                         }
238                         count++;
239
240                         //report progress
241                         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();       }
242                 }
243                 
244                 //report progress
245                 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();       }
246                 
247                 return results; 
248         }
249         catch(exception& e) {
250                 m->errorOut(e, "Unweighted", "driver");
251                 exit(1);
252         }
253 }
254 /**************************************************************************************************/
255
256 EstOutput Unweighted::getValues(Tree* t, string groupA, string groupB, int p, string o) { 
257  try {
258                 globaldata = GlobalData::getInstance();
259                 processors = p;
260                 outputDir = o;
261                 
262                 
263                 //if the users enters no groups then give them the score of all groups
264                 int numGroups = globaldata->Groups.size();
265                 
266                 //calculate number of comparsions
267                 int numComp = 0;
268                 vector< vector<string> > namesOfGroupCombos;
269                 for (int r=0; r<numGroups; r++) { 
270                         for (int l = 0; l < r; l++) {
271                                 numComp++;
272                                 vector<string> groups; groups.push_back(globaldata->Groups[r]); groups.push_back(globaldata->Groups[l]);
273                                 namesOfGroupCombos.push_back(groups);
274                         }
275                 }
276                 
277                 if (numComp != 1) {
278                         vector<string> groups;
279                         if (numGroups == 0) {
280                                 //get score for all users groups
281                                 for (int i = 0; i < tmap->namesOfGroups.size(); i++) {
282                                         if (tmap->namesOfGroups[i] != "xxx") {
283                                                 groups.push_back(tmap->namesOfGroups[i]);
284                                         }
285                                 }
286                                 namesOfGroupCombos.push_back(groups);
287                         }else {
288                                 for (int i = 0; i < globaldata->Groups.size(); i++) {
289                                         groups.push_back(globaldata->Groups[i]);
290                                 }
291                                 namesOfGroupCombos.push_back(groups);
292                         }
293                 }
294
295                 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
296                         if(processors == 1){
297                                 data = driver(t, namesOfGroupCombos, 0, namesOfGroupCombos.size(), true);
298                         }else{
299                                 int numPairs = namesOfGroupCombos.size();
300                                 
301                                 int numPairsPerProcessor = numPairs / processors;
302                                 
303                                 for (int i = 0; i < processors; i++) {
304                                         int startPos = i * numPairsPerProcessor;
305                                         if(i == processors - 1){
306                                                 numPairsPerProcessor = numPairs - i * numPairsPerProcessor;
307                                         }
308                                         lines.push_back(linePair(startPos, numPairsPerProcessor));
309                                 }
310                                         
311                                 data = createProcesses(t, namesOfGroupCombos, true);
312                                 
313                                 lines.clear();
314                         }
315                 #else
316                         data = driver(t, namesOfGroupCombos, 0, namesOfGroupCombos.size(), true);
317                 #endif
318         
319                 return data;
320         }
321         catch(exception& e) {
322                 m->errorOut(e, "Unweighted", "getValues");
323                 exit(1);
324         }
325 }
326 /**************************************************************************************************/
327
328 EstOutput Unweighted::createProcesses(Tree* t, vector< vector<string> > namesOfGroupCombos, bool usingGroups) {
329         try {
330 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
331                 int process = 1;
332                 vector<int> processIDS;
333                 
334                 EstOutput results;
335                 
336                 //loop through and create all the processes you want
337                 while (process != processors) {
338                         int pid = fork();
339                         
340                         if (pid > 0) {
341                                 processIDS.push_back(pid);  //create map from line number to pid so you can append files in correct order later
342                                 process++;
343                         }else if (pid == 0){
344                                 EstOutput myresults;
345                                 myresults = driver(t, namesOfGroupCombos, lines[process].start, lines[process].num, usingGroups);
346                                 
347                                 if (m->control_pressed) { exit(0); }
348                                 
349                                 //pass numSeqs to parent
350                                 ofstream out;
351                                 string tempFile = outputDir + toString(getpid()) + ".unweighted.results.temp";
352                                 m->openOutputFile(tempFile, out);
353                                 out << myresults.size() << endl;
354                                 for (int i = 0; i < myresults.size(); i++) {  out << myresults[i] << '\t';  } out << endl;
355                                 out.close();
356                                 
357                                 exit(0);
358                         }else { 
359                                 m->mothurOut("[ERROR]: unable to spawn the necessary processes."); m->mothurOutEndLine(); 
360                                 for (int i = 0; i < processIDS.size(); i++) { kill (processIDS[i], SIGINT); }
361                                 exit(0); 
362                         }
363                 }
364                 
365                 results = driver(t, namesOfGroupCombos, lines[0].start, lines[0].num, usingGroups);
366                 
367                 //force parent to wait until all the processes are done
368                 for (int i=0;i<(processors-1);i++) { 
369                         int temp = processIDS[i];
370                         wait(&temp);
371                 }
372                 
373                 if (m->control_pressed) { return results; }
374                 
375                 //get data created by processes
376                 for (int i=0;i<(processors-1);i++) { 
377                         ifstream in;
378                         string s = outputDir + toString(processIDS[i]) + ".unweighted.results.temp";
379                         m->openInputFile(s, in);
380                         
381                         //get quantiles
382                         if (!in.eof()) {
383                                 int num;
384                                 in >> num; m->gobble(in);
385                                         
386                                 if (m->control_pressed) { break; }
387                                 
388                                 double w; 
389                                 for (int j = 0; j < num; j++) {
390                                         in >> w;
391                                         
392                                         results.push_back(w);
393                                 }
394                                 m->gobble(in);
395                         }
396                         in.close();
397                         remove(s.c_str());
398                 }
399                 
400                 return results;
401 #endif          
402         }
403         catch(exception& e) {
404                 m->errorOut(e, "Unweighted", "createProcesses");
405                 exit(1);
406         }
407 }
408 /**************************************************************************************************/
409 EstOutput Unweighted::driver(Tree* t, vector< vector<string> > namesOfGroupCombos, int start, int num, bool usingGroups) { 
410  try {
411                 
412                 EstOutput results; results.resize(num);
413                 
414                 int count = 0;
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                         //find a node that belongs to one of the groups in this combo
432                         int nodeBelonging = -1;
433                         for (int g = 0; g < namesOfGroupCombos[h].size(); g++) {
434                                 if (copyTree->groupNodeInfo[namesOfGroupCombos[h][g]].size() != 0) { nodeBelonging = copyTree->groupNodeInfo[namesOfGroupCombos[h][g]][0]; break; }
435                         }
436                         
437                         //sanity check
438                         if (nodeBelonging == -1) {
439                                 m->mothurOut("[WARNING]: cannot find a nodes in the tree from grouping "); 
440                                 for (int g = 0; g < namesOfGroupCombos[h].size()-1; g++) { m->mothurOut(namesOfGroupCombos[h][g] + "-"); }
441                                 m->mothurOut(namesOfGroupCombos[h][namesOfGroupCombos[h].size()-1]);
442                                 m->mothurOut(", skipping."); m->mothurOutEndLine(); results[count] = UW;
443                         }else{
444                                 
445                                 getRoot(copyTree, nodeBelonging, namesOfGroupCombos[h]);
446                                 
447                                 for(int i=0;i<copyTree->getNumNodes();i++){
448                                         
449                                         if (m->control_pressed) {  return data; }
450                                         
451                                         //pcountSize = 0, they are from a branch that is entirely from a group the user doesn't want
452                                         //pcountSize = 2, not unique to one group
453                                         //pcountSize = 1, unique to one group
454                                         
455                                         int pcountSize = 0;
456                                         for (int j = 0; j < namesOfGroupCombos[h].size(); j++) {
457                                                 map<string, int>::iterator itGroup = copyTree->tree[i].pcount.find(namesOfGroupCombos[h][j]);
458                                                 if (itGroup != copyTree->tree[i].pcount.end()) { pcountSize++; if (pcountSize > 1) { break; } } 
459                                         }
460                                         
461                                         //unique calc
462                                         if (pcountSize == 0) { }
463                                         else if ((copyTree->tree[i].getBranchLength() != -1) && (pcountSize == 1) && (rootForGrouping[namesOfGroupCombos[h]].count(i) == 0)) { //you have a unique branch length and you are not the root 
464                                                 UniqueBL += abs(copyTree->tree[i].getBranchLength()); 
465                                         }
466                                         
467                                         //total calc
468                                         if (pcountSize == 0) { }
469                                         else if ((copyTree->tree[i].getBranchLength() != -1) && (pcountSize != 0) && (rootForGrouping[namesOfGroupCombos[h]].count(i) == 0)) { //you have a branch length and you are not the root 
470                                                 totalBL += abs(copyTree->tree[i].getBranchLength()); 
471                                         }
472                                         
473                                 }
474                                 //cout << UniqueBL << '\t' << totalBL << endl;          
475                                 UW = (UniqueBL / totalBL);  
476                                 
477                                 if (isnan(UW) || isinf(UW)) { UW = 0; }
478                                 
479                                 results[count] = UW;
480                         }
481                         count++;
482                         
483                 }
484                 
485                 delete copyTree;
486                 
487                 return results; 
488         }
489         catch(exception& e) {
490                 m->errorOut(e, "Unweighted", "driver");
491                 exit(1);
492         }
493 }
494 /**************************************************************************************************/
495 int Unweighted::getRoot(Tree* t, int v, vector<string> grouping) { 
496         try {
497                 //you are a leaf so get your parent
498                 int index = t->tree[index].getParent();
499                 
500                 //my parent is a potential root
501                 rootForGrouping[grouping].insert(index);
502                 
503                 //while you aren't at root
504                 while(t->tree[index].getParent() != -1){
505                         
506                         if (m->control_pressed) {  return 0; }
507                         
508                         //am I the root for this grouping? if so I want to stop "early"
509                         //does my sibling have descendants from the users groups? 
510                         //if so I am not the root
511                         int parent = t->tree[index].getParent();
512                         int lc = t->tree[parent].getLChild();
513                         int rc = t->tree[parent].getRChild();
514                         
515                         int sib = lc;
516                         if (lc == index) { sib = rc; }
517                         
518                         map<string, int>::iterator itGroup;
519                         int pcountSize = 0;
520                         for (int j = 0; j < grouping.size(); j++) {
521                                 map<string, int>::iterator itGroup = t->tree[sib].pcount.find(grouping[j]);
522                                 if (itGroup != t->tree[sib].pcount.end()) { pcountSize++; if (pcountSize > 1) { break; } } 
523                         }
524                         
525                         //if yes, I am not the root
526                         if (pcountSize != 0) {
527                                 rootForGrouping[grouping].clear();
528                                 rootForGrouping[grouping].insert(parent);
529                         }
530                         
531                         index = parent; 
532                 }
533                 
534                 //get all nodes above the root to add so we don't add their u values above
535                 index = *(rootForGrouping[grouping].begin());
536                 while(t->tree[index].getParent() != -1){
537                         int parent = t->tree[index].getParent();
538                         rootForGrouping[grouping].insert(parent);
539                         cout << parent << " in root" << endl;
540                         index = parent;
541                 }
542                 
543                 return 0;
544         }
545         catch(exception& e) {
546                 m->errorOut(e, "Unweighted", "getRoot");
547                 exit(1);
548         }
549 }
550 /**************************************************************************************************/
551
552