5 * Created by Sarah Westcott on 2/9/09.
6 * Copyright 2009 Schloss Lab UMASS Amherst. All rights reserved.
10 #include "unweighted.h"
12 /**************************************************************************************************/
14 EstOutput Unweighted::getValues(Tree* t, int p, string o) {
16 globaldata = GlobalData::getInstance();
20 //if the users enters no groups then give them the score of all groups
21 int numGroups = globaldata->Groups.size();
23 //calculate number of comparsions
25 vector< vector<string> > namesOfGroupCombos;
26 for (int r=0; r<numGroups; r++) {
27 for (int l = 0; l < r; l++) {
29 vector<string> groups; groups.push_back(globaldata->Groups[r]); groups.push_back(globaldata->Groups[l]);
30 namesOfGroupCombos.push_back(groups);
35 vector<string> groups;
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]);
43 namesOfGroupCombos.push_back(groups);
45 for (int i = 0; i < globaldata->Groups.size(); i++) {
46 groups.push_back(globaldata->Groups[i]);
48 namesOfGroupCombos.push_back(groups);
52 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
54 data = driver(t, namesOfGroupCombos, 0, namesOfGroupCombos.size());
56 int numPairs = namesOfGroupCombos.size();
58 int numPairsPerProcessor = numPairs / processors;
60 for (int i = 0; i < processors; i++) {
61 int startPos = i * numPairsPerProcessor;
63 if(i == processors - 1){
64 numPairsPerProcessor = numPairs - i * numPairsPerProcessor;
67 lines.push_back(linePair(startPos, numPairsPerProcessor));
70 data = createProcesses(t, namesOfGroupCombos);
74 data = driver(t, namesOfGroupCombos, 0, namesOfGroupCombos.size());
80 m->errorOut(e, "Unweighted", "getValues");
84 /**************************************************************************************************/
86 EstOutput Unweighted::createProcesses(Tree* t, vector< vector<string> > namesOfGroupCombos) {
88 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
91 vector<int> processIDS;
95 //loop through and create all the processes you want
96 while (process != processors) {
100 processIDS.push_back(pid); //create map from line number to pid so you can append files in correct order later
104 myresults = driver(t, namesOfGroupCombos, lines[process].start, lines[process].num);
106 if (m->control_pressed) { exit(0); }
108 m->mothurOut("Merging results."); m->mothurOutEndLine();
110 //pass numSeqs to parent
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;
120 m->mothurOut("[ERROR]: unable to spawn the necessary processes."); m->mothurOutEndLine();
121 for (int i = 0; i < processIDS.size(); i++) { kill (processIDS[i], SIGINT); }
126 results = driver(t, namesOfGroupCombos, lines[0].start, lines[0].num);
128 //force parent to wait until all the processes are done
129 for (int i=0;i<(processors-1);i++) {
130 int temp = processIDS[i];
134 if (m->control_pressed) { return results; }
136 //get data created by processes
137 for (int i=0;i<(processors-1);i++) {
139 string s = outputDir + toString(processIDS[i]) + ".unweighted.results.temp";
140 m->openInputFile(s, in);
145 in >> num; m->gobble(in);
147 if (m->control_pressed) { break; }
150 for (int j = 0; j < num; j++) {
152 results.push_back(w);
160 m->mothurOut("DONE."); m->mothurOutEndLine(); m->mothurOutEndLine();
165 catch(exception& e) {
166 m->errorOut(e, "Unweighted", "createProcesses");
170 /**************************************************************************************************/
171 EstOutput Unweighted::driver(Tree* t, vector< vector<string> > namesOfGroupCombos, int start, int num) {
174 EstOutput results; results.resize(num);
177 int numLeaves = t->getNumLeaves();
179 int twentyPercent = (total * 0.20);
180 if (twentyPercent == 0) { twentyPercent = 1; }
183 for (int h = start; h < (start+num); h++) {
184 //cout << namesOfGroupCombos[h][0] << '\t' << namesOfGroupCombos[h][1] << endl;
185 if (m->control_pressed) { return results; }
187 double UniqueBL=0.0000; //a branch length is unique if it's chidren are from the same group
188 double totalBL = 0.00; //all branch lengths
189 double UW = 0.00; //Unweighted Value = UniqueBL / totalBL;
190 map<int, double> tempTotals; //maps node to total Branch Length
191 map<int, int> nodePcountSize; //maps node to pcountSize
192 map<int, int>::iterator itCount;
194 for(int i=0;i<t->getNumNodes();i++){
196 if (m->control_pressed) { return data; }
198 //pcountSize = 0, they are from a branch that is entirely from a group the user doesn't want
199 //pcountSize = 2, not unique to one group
200 //pcountSize = 1, unique to one group
203 for (int j = 0; j < namesOfGroupCombos[h].size(); j++) {
204 map<string, int>::iterator itGroup = t->tree[i].pcount.find(namesOfGroupCombos[h][j]);
205 if (itGroup != t->tree[i].pcount.end()) { pcountSize++; if (pcountSize > 1) { break; } }
208 nodePcountSize[i] = pcountSize;
210 //cout << i << '\t' << t->tree[i].getName() << " br = " << abs(t->tree[i].getBranchLength()) << '\t';
211 if (pcountSize == 0) { }
212 else if ((t->tree[i].getBranchLength() != -1) && (pcountSize == 1)) { UniqueBL += abs(t->tree[i].getBranchLength()); }
214 //if you are a leaf from a users group add to total
216 if ((t->tree[i].getBranchLength() != -1) && pcountSize != 0) {
217 //cout << "added to total" << endl;
218 totalBL += abs(t->tree[i].getBranchLength());
220 tempTotals[i] = 0.0; //we don't care about you, or we have already added you
221 }else{ //if you are not a leaf
222 //do both your chidren have have descendants from the users groups?
223 int lc = t->tree[i].getLChild();
224 int rc = t->tree[i].getRChild();
226 //if yes, add your childrens tempTotals
227 if ((nodePcountSize[lc] != 0) && (nodePcountSize[rc] != 0)) {
228 totalBL += tempTotals[lc] + tempTotals[rc];
229 //cout << "added to total " << tempTotals[lc] << '\t' << tempTotals[rc] << endl;
230 if (t->tree[i].getBranchLength() != -1) {
231 tempTotals[i] = abs(t->tree[i].getBranchLength());
235 }else if ((nodePcountSize[lc] == 0) && (nodePcountSize[rc] == 0)) { tempTotals[i] = 0.0; //we don't care about you
236 }else { //if no, your tempTotal is your childrens temp totals + your branch length
237 tempTotals[i] = tempTotals[lc] + tempTotals[rc] + abs(t->tree[i].getBranchLength());
239 //cout << "temptotal = "<< tempTotals[i] << endl;
243 //cout << UniqueBL << '\t' << totalBL << endl;
244 UW = (UniqueBL / totalBL);
246 if (isnan(UW) || isinf(UW)) { UW = 0; }
252 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(); }
256 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(); }
260 catch(exception& e) {
261 m->errorOut(e, "Unweighted", "driver");
265 /**************************************************************************************************/
267 EstOutput Unweighted::getValues(Tree* t, string groupA, string groupB, int p, string o) {
269 globaldata = GlobalData::getInstance();
274 //if the users enters no groups then give them the score of all groups
275 int numGroups = globaldata->Groups.size();
277 //calculate number of comparsions
279 vector< vector<string> > namesOfGroupCombos;
280 for (int r=0; r<numGroups; r++) {
281 for (int l = 0; l < r; l++) {
283 vector<string> groups; groups.push_back(globaldata->Groups[r]); groups.push_back(globaldata->Groups[l]);
284 namesOfGroupCombos.push_back(groups);
289 vector<string> groups;
290 if (numGroups == 0) {
291 //get score for all users groups
292 for (int i = 0; i < tmap->namesOfGroups.size(); i++) {
293 if (tmap->namesOfGroups[i] != "xxx") {
294 groups.push_back(tmap->namesOfGroups[i]);
297 namesOfGroupCombos.push_back(groups);
299 for (int i = 0; i < globaldata->Groups.size(); i++) {
300 groups.push_back(globaldata->Groups[i]);
302 namesOfGroupCombos.push_back(groups);
306 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
308 data = driver(t, namesOfGroupCombos, 0, namesOfGroupCombos.size(), true);
310 int numPairs = namesOfGroupCombos.size();
312 int numPairsPerProcessor = numPairs / processors;
314 for (int i = 0; i < processors; i++) {
315 int startPos = i * numPairsPerProcessor;
316 if(i == processors - 1){
317 numPairsPerProcessor = numPairs - i * numPairsPerProcessor;
319 lines.push_back(linePair(startPos, numPairsPerProcessor));
322 data = createProcesses(t, namesOfGroupCombos, true);
327 data = driver(t, namesOfGroupCombos, 0, namesOfGroupCombos.size(), true);
332 catch(exception& e) {
333 m->errorOut(e, "Unweighted", "getValues");
337 /**************************************************************************************************/
339 EstOutput Unweighted::createProcesses(Tree* t, vector< vector<string> > namesOfGroupCombos, bool usingGroups) {
341 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
344 vector<int> processIDS;
348 //loop through and create all the processes you want
349 while (process != processors) {
353 processIDS.push_back(pid); //create map from line number to pid so you can append files in correct order later
357 myresults = driver(t, namesOfGroupCombos, lines[process].start, lines[process].num, usingGroups);
359 if (m->control_pressed) { exit(0); }
361 //pass numSeqs to parent
363 string tempFile = outputDir + toString(getpid()) + ".unweighted.results.temp";
364 m->openOutputFile(tempFile, out);
365 out << myresults.size() << endl;
366 for (int i = 0; i < myresults.size(); i++) { out << myresults[i] << '\t'; } out << endl;
371 m->mothurOut("[ERROR]: unable to spawn the necessary processes."); m->mothurOutEndLine();
372 for (int i = 0; i < processIDS.size(); i++) { kill (processIDS[i], SIGINT); }
377 results = driver(t, namesOfGroupCombos, lines[0].start, lines[0].num, usingGroups);
379 //force parent to wait until all the processes are done
380 for (int i=0;i<(processors-1);i++) {
381 int temp = processIDS[i];
385 if (m->control_pressed) { return results; }
387 //get data created by processes
388 for (int i=0;i<(processors-1);i++) {
390 string s = outputDir + toString(processIDS[i]) + ".unweighted.results.temp";
391 m->openInputFile(s, in);
396 in >> num; m->gobble(in);
398 if (m->control_pressed) { break; }
401 for (int j = 0; j < num; j++) {
403 results.push_back(w);
414 catch(exception& e) {
415 m->errorOut(e, "Unweighted", "createProcesses");
419 /**************************************************************************************************/
420 EstOutput Unweighted::driver(Tree* t, vector< vector<string> > namesOfGroupCombos, int start, int num, bool usingGroups) {
423 EstOutput results; results.resize(num);
427 int twentyPercent = (total * 0.20);
428 int numLeaves = t->getNumLeaves();
430 Tree* copyTree = new Tree;
432 for (int h = start; h < (start+num); h++) {
434 if (m->control_pressed) { return results; }
436 //copy random tree passed in
437 copyTree->getCopy(t);
439 //swap labels in the groups you want to compare
440 copyTree->assembleRandomUnifracTree(namesOfGroupCombos[h]);
442 double UniqueBL=0.0000; //a branch length is unique if it's chidren are from the same group
443 double totalBL = 0.00; //all branch lengths
444 double UW = 0.00; //Unweighted Value = UniqueBL / totalBL;
445 map<int, double> tempTotals; //maps node to total Branch Length
446 map<int, int> nodePcountSize; //maps node to pcountSize
448 for(int i=0;i<copyTree->getNumNodes();i++){
450 if (m->control_pressed) { return data; }
452 //pcountSize = 0, they are from a branch that is entirely from a group the user doesn't want
453 //pcountSize = 2, not unique to one group
454 //pcountSize = 1, unique to one group
457 for (int j = 0; j < namesOfGroupCombos[h].size(); j++) {
458 map<string, int>::iterator itGroup = copyTree->tree[i].pcount.find(namesOfGroupCombos[h][j]);
459 if (itGroup != copyTree->tree[i].pcount.end()) { pcountSize++; if (pcountSize > 1) { break; } }
462 nodePcountSize[i] = pcountSize;
464 if (pcountSize == 0) { }
465 else if ((copyTree->tree[i].getBranchLength() != -1) && (pcountSize == 1)) { UniqueBL += abs(copyTree->tree[i].getBranchLength()); }
467 //if you are a leaf from a users group add to total
469 if ((copyTree->tree[i].getBranchLength() != -1) && pcountSize != 0) {
470 totalBL += abs(copyTree->tree[i].getBranchLength());
472 tempTotals[i] = 0.0; //we don't care about you, or we have already added you
473 }else{ //if you are not a leaf
474 //do both your chidren have have descendants from the users groups?
475 int lc = copyTree->tree[i].getLChild();
476 int rc = copyTree->tree[i].getRChild();
478 //if yes, add your childrens tempTotals
479 if ((nodePcountSize[lc] != 0) && (nodePcountSize[rc] != 0)) {
480 totalBL += tempTotals[lc] + tempTotals[rc];
482 if (copyTree->tree[i].getBranchLength() != -1) {
483 tempTotals[i] = abs(copyTree->tree[i].getBranchLength());
487 }else if ((nodePcountSize[lc] == 0) && (nodePcountSize[rc] == 0)) { tempTotals[i] = 0.0; //we don't care about you
488 }else { //if no, your tempTotal is your childrens temp totals + your branch length
489 tempTotals[i] = tempTotals[lc] + tempTotals[rc] + abs(copyTree->tree[i].getBranchLength());
496 UW = (UniqueBL / totalBL);
498 if (isnan(UW) || isinf(UW)) { UW = 0; }
509 catch(exception& e) {
510 m->errorOut(e, "Unweighted", "driver");
514 /**************************************************************************************************/