]> git.donarmstrong.com Git - mothur.git/blob - unweighted.cpp
added pipeline commands which involved change to command factory and command class...
[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 to total your childrens branchLengths and 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                                                 //tempTotals[i] = tempTotals[lc] + tempTotals[rc]; 
229                                         }else { //if no, your tempTotal is your childrens temp totals
230                                                 tempTotals[i] = tempTotals[lc] + tempTotals[rc] + abs(t->tree[i].getBranchLength()); 
231                                         }
232                                 }
233                         }
234                         
235                         UW = (UniqueBL / totalBL);  
236         
237                         if (isnan(UW) || isinf(UW)) { UW = 0; }
238         
239                         results[count] = UW;
240                         count++;
241
242                         //report progress
243                         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();       }
244                 }
245                 
246                 //report progress
247                 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();       }
248                 
249                 return results; 
250         }
251         catch(exception& e) {
252                 m->errorOut(e, "Unweighted", "driver");
253                 exit(1);
254         }
255 }
256 /**************************************************************************************************/
257
258 EstOutput Unweighted::getValues(Tree* t, string groupA, string groupB, int p, string o) { 
259  try {
260                 globaldata = GlobalData::getInstance();
261                 processors = p;
262                 outputDir = o;
263                 
264                 
265                 //if the users enters no groups then give them the score of all groups
266                 int numGroups = globaldata->Groups.size();
267                 
268                 //calculate number of comparsions
269                 int numComp = 0;
270                 vector< vector<string> > namesOfGroupCombos;
271                 for (int r=0; r<numGroups; r++) { 
272                         for (int l = 0; l < r; l++) {
273                                 numComp++;
274                                 vector<string> groups; groups.push_back(globaldata->Groups[r]); groups.push_back(globaldata->Groups[l]);
275                                 namesOfGroupCombos.push_back(groups);
276                         }
277                 }
278                 
279                 if (numComp != 1) {
280                         vector<string> groups;
281                         if (numGroups == 0) {
282                                 //get score for all users groups
283                                 for (int i = 0; i < tmap->namesOfGroups.size(); i++) {
284                                         if (tmap->namesOfGroups[i] != "xxx") {
285                                                 groups.push_back(tmap->namesOfGroups[i]);
286                                         }
287                                 }
288                                 namesOfGroupCombos.push_back(groups);
289                         }else {
290                                 for (int i = 0; i < globaldata->Groups.size(); i++) {
291                                         groups.push_back(globaldata->Groups[i]);
292                                 }
293                                 namesOfGroupCombos.push_back(groups);
294                         }
295                 }
296
297                 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
298                         if(processors == 1){
299                                 data = driver(t, namesOfGroupCombos, 0, namesOfGroupCombos.size(), true);
300                         }else{
301                                 int numPairs = namesOfGroupCombos.size();
302                                 
303                                 int numPairsPerProcessor = numPairs / processors;
304                                 
305                                 for (int i = 0; i < processors; i++) {
306                                         int startPos = i * numPairsPerProcessor;
307                                         if(i == processors - 1){
308                                                 numPairsPerProcessor = numPairs - i * numPairsPerProcessor;
309                                         }
310                                         lines.push_back(linePair(startPos, numPairsPerProcessor));
311                                 }
312
313                                 data = createProcesses(t, namesOfGroupCombos, true);
314                                 
315                                 lines.clear();
316                         }
317                 #else
318                         data = driver(t, namesOfGroupCombos, 0, namesOfGroupCombos.size(), true);
319                 #endif
320         
321                 return data;
322         }
323         catch(exception& e) {
324                 m->errorOut(e, "Unweighted", "getValues");
325                 exit(1);
326         }
327 }
328 /**************************************************************************************************/
329
330 EstOutput Unweighted::createProcesses(Tree* t, vector< vector<string> > namesOfGroupCombos, bool usingGroups) {
331         try {
332 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
333                 int process = 1;
334                 int num = 0;
335                 vector<int> processIDS;
336                 
337                 EstOutput results;
338                 
339                 //loop through and create all the processes you want
340                 while (process != processors) {
341                         int pid = fork();
342                         
343                         if (pid > 0) {
344                                 processIDS.push_back(pid);  //create map from line number to pid so you can append files in correct order later
345                                 process++;
346                         }else if (pid == 0){
347                                 EstOutput myresults;
348                                 myresults = driver(t, namesOfGroupCombos, lines[process].start, lines[process].num, usingGroups);
349                                 
350                                 if (m->control_pressed) { exit(0); }
351                                 
352                                 //pass numSeqs to parent
353                                 ofstream out;
354                                 string tempFile = outputDir + toString(getpid()) + ".unweighted.results.temp";
355                                 m->openOutputFile(tempFile, out);
356                                 out << myresults.size() << endl;
357                                 for (int i = 0; i < myresults.size(); i++) {  out << myresults[i] << '\t';  } out << endl;
358                                 out.close();
359                                 
360                                 exit(0);
361                         }else { m->mothurOut("unable to spawn the necessary processes."); m->mothurOutEndLine(); exit(0); }
362                 }
363                 
364                 results = driver(t, namesOfGroupCombos, lines[0].start, lines[0].num, usingGroups);
365                 
366                 //force parent to wait until all the processes are done
367                 for (int i=0;i<(processors-1);i++) { 
368                         int temp = processIDS[i];
369                         wait(&temp);
370                 }
371                 
372                 if (m->control_pressed) { return results; }
373                 
374                 //get data created by processes
375                 for (int i=0;i<(processors-1);i++) { 
376                         ifstream in;
377                         string s = outputDir + toString(processIDS[i]) + ".unweighted.results.temp";
378                         m->openInputFile(s, in);
379                         
380                         //get quantiles
381                         if (!in.eof()) {
382                                 int num;
383                                 in >> num; m->gobble(in);
384                                 
385                                 if (m->control_pressed) { break; }
386                                 
387                                 double w; 
388                                 for (int j = 0; j < num; j++) {
389                                         in >> w;
390                                         results.push_back(w);
391                                 }
392                                 m->gobble(in);
393                         }
394                         in.close();
395                         remove(s.c_str());
396                 }
397                 
398                 return results;
399 #endif          
400         }
401         catch(exception& e) {
402                 m->errorOut(e, "Unweighted", "createProcesses");
403                 exit(1);
404         }
405 }
406 /**************************************************************************************************/
407 EstOutput Unweighted::driver(Tree* t, vector< vector<string> > namesOfGroupCombos, int start, int num, bool usingGroups) { 
408  try {
409                 
410                 EstOutput results; results.resize(num);
411                 
412                 int count = 0;
413                 int total = num;
414                 int twentyPercent = (total * 0.20);
415                 int numLeaves = t->getNumLeaves();
416                 
417                 Tree* copyTree = new Tree;
418                 
419                 for (int h = start; h < (start+num); h++) {
420                 
421                         if (m->control_pressed) { return results; }
422                 
423                         //copy random tree passed in
424                         copyTree->getCopy(t);
425                                 
426                         //swap labels in the groups you want to compare
427                         copyTree->assembleRandomUnifracTree(namesOfGroupCombos[h]);
428                         
429                         double UniqueBL=0.0000;  //a branch length is unique if it's chidren are from the same group
430                         double totalBL = 0.00;  //all branch lengths
431                         double UW = 0.00;               //Unweighted Value = UniqueBL / totalBL;
432                         map<int, double> tempTotals; //maps node to total Branch Length
433                         map<int, int> nodePcountSize; //maps node to pcountSize
434                                 
435                         for(int i=0;i<copyTree->getNumNodes();i++){
436                         
437                                 if (m->control_pressed) {  return data; }
438                                 
439                                 //pcountSize = 0, they are from a branch that is entirely from a group the user doesn't want
440                                 //pcountSize = 2, not unique to one group
441                                 //pcountSize = 1, unique to one group
442                                 
443                                 int pcountSize = 0;
444                                 for (int j = 0; j < namesOfGroupCombos[h].size(); j++) {
445                                         map<string, int>::iterator itGroup = copyTree->tree[i].pcount.find(namesOfGroupCombos[h][j]);
446                                         if (itGroup != copyTree->tree[i].pcount.end()) { pcountSize++; if (pcountSize > 1) { break; } } 
447                                 }
448                                 
449                                 nodePcountSize[i] = pcountSize;
450                                 
451                                 if (pcountSize == 0) { }
452                                 else if ((copyTree->tree[i].getBranchLength() != -1) && (pcountSize == 1)) {  UniqueBL += abs(copyTree->tree[i].getBranchLength());     }
453                                 
454                                 //if you are a leaf from a users group add to total
455                                 if (i < numLeaves) {
456                                         if ((copyTree->tree[i].getBranchLength() != -1) && pcountSize != 0) {  
457                                                 totalBL += abs(copyTree->tree[i].getBranchLength()); 
458                                         }
459                                         tempTotals[i] = 0.0;  //we don't care about you, or we have already added you
460                                 }else{ //if you are not a leaf 
461                                         //do both your chidren have have descendants from the users groups? 
462                                         int lc = copyTree->tree[i].getLChild();
463                                         int rc = copyTree->tree[i].getRChild();
464                                         
465                                         //if yes, add to total your childrens branchLengths and your childrens tempTotals
466                                         if ((nodePcountSize[lc] != 0) && (nodePcountSize[rc] != 0)) {
467                                                 totalBL += tempTotals[lc] + tempTotals[rc]; 
468                                                 if (copyTree->tree[i].getBranchLength() != -1) {
469                                                         tempTotals[i] = abs(copyTree->tree[i].getBranchLength());
470                                                 }else {
471                                                         tempTotals[i] = 0.0;
472                                                 }
473                                                 //tempTotals[i] = tempTotals[lc] + tempTotals[rc]; 
474                                         }else { //if no, your tempTotal is your childrens temp totals
475                                                 tempTotals[i] = tempTotals[lc] + tempTotals[rc] + abs(copyTree->tree[i].getBranchLength()); 
476                                         }
477                                 }
478
479                         }
480                 
481                         UW = (UniqueBL / totalBL);  
482         
483                         if (isnan(UW) || isinf(UW)) { UW = 0; }
484         
485                         results[count] = UW;
486                         count++;
487
488                 }
489                 
490                 delete copyTree;
491                 
492                 return results; 
493         }
494         catch(exception& e) {
495                 m->errorOut(e, "Unweighted", "driver");
496                 exit(1);
497         }
498 }
499 /**************************************************************************************************/
500
501