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));
69 data = createProcesses(t, namesOfGroupCombos);
73 data = driver(t, namesOfGroupCombos, 0, namesOfGroupCombos.size());
79 m->errorOut(e, "Unweighted", "getValues");
83 /**************************************************************************************************/
85 EstOutput Unweighted::createProcesses(Tree* t, vector< vector<string> > namesOfGroupCombos) {
87 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
89 vector<int> processIDS;
93 //loop through and create all the processes you want
94 while (process != processors) {
98 processIDS.push_back(pid); //create map from line number to pid so you can append files in correct order later
102 myresults = driver(t, namesOfGroupCombos, lines[process].start, lines[process].num);
104 if (m->control_pressed) { exit(0); }
106 m->mothurOut("Merging results."); m->mothurOutEndLine();
108 //pass numSeqs to parent
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;
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); }
124 results = driver(t, namesOfGroupCombos, lines[0].start, lines[0].num);
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];
132 if (m->control_pressed) { return results; }
134 //get data created by processes
135 for (int i=0;i<(processors-1);i++) {
137 string s = outputDir + toString(processIDS[i]) + ".unweighted.results.temp";
138 m->openInputFile(s, in);
143 in >> num; m->gobble(in);
145 if (m->control_pressed) { break; }
148 for (int j = 0; j < num; j++) {
150 results.push_back(w);
158 m->mothurOut("DONE."); m->mothurOutEndLine(); m->mothurOutEndLine();
163 catch(exception& e) {
164 m->errorOut(e, "Unweighted", "createProcesses");
168 /**************************************************************************************************/
169 EstOutput Unweighted::driver(Tree* t, vector< vector<string> > namesOfGroupCombos, int start, int num) {
173 EstOutput results; results.resize(num);
177 int twentyPercent = (total * 0.20);
178 if (twentyPercent == 0) { twentyPercent = 1; }
181 for (int h = start; h < (start+num); h++) {
183 if (m->control_pressed) { return results; }
185 double UniqueBL=0.0000; //a branch length is unique if it's chidren are from the same group
186 double totalBL = 0.00; //all branch lengths
187 double UW = 0.00; //Unweighted Value = UniqueBL / totalBL;
189 //find a node that belongs to one of the groups in this combo
190 int nodeBelonging = -1;
191 for (int g = 0; g < namesOfGroupCombos[h].size(); g++) {
192 if (t->groupNodeInfo[namesOfGroupCombos[h][g]].size() != 0) { nodeBelonging = t->groupNodeInfo[namesOfGroupCombos[h][g]][0]; break; }
196 if (nodeBelonging == -1) {
197 m->mothurOut("[WARNING]: cannot find a nodes in the tree from grouping ");
198 for (int g = 0; g < namesOfGroupCombos[h].size()-1; g++) { m->mothurOut(namesOfGroupCombos[h][g] + "-"); }
199 m->mothurOut(namesOfGroupCombos[h][namesOfGroupCombos[h].size()-1]);
200 m->mothurOut(", skipping."); m->mothurOutEndLine(); results[count] = UW;
202 //cout << "trying to get root" << endl;
203 //if including the root this clears rootForGrouping[namesOfGroupCombos[h]]
204 getRoot(t, nodeBelonging, namesOfGroupCombos[h]);
205 //cout << "here" << endl;
206 for(int i=0;i<t->getNumNodes();i++){
208 if (m->control_pressed) { return data; }
210 //pcountSize = 0, they are from a branch that is entirely from a group the user doesn't want
211 //pcountSize = 2, not unique to one group
212 //pcountSize = 1, unique to one group
215 for (int j = 0; j < namesOfGroupCombos[h].size(); j++) {
216 map<string, int>::iterator itGroup = t->tree[i].pcount.find(namesOfGroupCombos[h][j]);
217 if (itGroup != t->tree[i].pcount.end()) { pcountSize++; if (pcountSize > 1) { break; } }
222 if (pcountSize == 0) { }
223 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
224 UniqueBL += abs(t->tree[i].getBranchLength());
228 if (pcountSize == 0) { }
229 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
230 totalBL += abs(t->tree[i].getBranchLength());
233 //cout << UniqueBL << '\t' << totalBL << endl;
234 UW = (UniqueBL / totalBL);
236 if (isnan(UW) || isinf(UW)) { UW = 0; }
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(); }
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(); }
251 catch(exception& e) {
252 m->errorOut(e, "Unweighted", "driver");
256 /**************************************************************************************************/
258 EstOutput Unweighted::getValues(Tree* t, string groupA, string groupB, int p, string o) {
260 globaldata = GlobalData::getInstance();
264 //if the users enters no groups then give them the score of all groups
265 int numGroups = globaldata->Groups.size();
267 //calculate number of comparsions
269 vector< vector<string> > namesOfGroupCombos;
270 for (int r=0; r<numGroups; r++) {
271 for (int l = 0; l < r; l++) {
273 vector<string> groups; groups.push_back(globaldata->Groups[r]); groups.push_back(globaldata->Groups[l]);
274 namesOfGroupCombos.push_back(groups);
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]);
287 namesOfGroupCombos.push_back(groups);
289 for (int i = 0; i < globaldata->Groups.size(); i++) {
290 groups.push_back(globaldata->Groups[i]);
292 namesOfGroupCombos.push_back(groups);
296 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
298 data = driver(t, namesOfGroupCombos, 0, namesOfGroupCombos.size(), true);
300 int numPairs = namesOfGroupCombos.size();
302 int numPairsPerProcessor = numPairs / processors;
304 for (int i = 0; i < processors; i++) {
305 int startPos = i * numPairsPerProcessor;
306 if(i == processors - 1){
307 numPairsPerProcessor = numPairs - i * numPairsPerProcessor;
309 lines.push_back(linePair(startPos, numPairsPerProcessor));
312 data = createProcesses(t, namesOfGroupCombos, true);
317 data = driver(t, namesOfGroupCombos, 0, namesOfGroupCombos.size(), true);
322 catch(exception& e) {
323 m->errorOut(e, "Unweighted", "getValues");
327 /**************************************************************************************************/
329 EstOutput Unweighted::createProcesses(Tree* t, vector< vector<string> > namesOfGroupCombos, bool usingGroups) {
331 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
333 vector<int> processIDS;
337 //loop through and create all the processes you want
338 while (process != processors) {
342 processIDS.push_back(pid); //create map from line number to pid so you can append files in correct order later
346 myresults = driver(t, namesOfGroupCombos, lines[process].start, lines[process].num, usingGroups);
348 if (m->control_pressed) { exit(0); }
350 //pass numSeqs to parent
352 string tempFile = outputDir + toString(getpid()) + ".unweighted.results.temp";
353 m->openOutputFile(tempFile, out);
354 out << myresults.size() << endl;
355 for (int i = 0; i < myresults.size(); i++) { out << myresults[i] << '\t'; } out << endl;
360 m->mothurOut("[ERROR]: unable to spawn the necessary processes."); m->mothurOutEndLine();
361 for (int i = 0; i < processIDS.size(); i++) { kill (processIDS[i], SIGINT); }
366 results = driver(t, namesOfGroupCombos, lines[0].start, lines[0].num, usingGroups);
368 //force parent to wait until all the processes are done
369 for (int i=0;i<(processors-1);i++) {
370 int temp = processIDS[i];
374 if (m->control_pressed) { return results; }
376 //get data created by processes
377 for (int i=0;i<(processors-1);i++) {
379 string s = outputDir + toString(processIDS[i]) + ".unweighted.results.temp";
380 m->openInputFile(s, in);
385 in >> num; m->gobble(in);
387 if (m->control_pressed) { break; }
390 for (int j = 0; j < num; j++) {
393 results.push_back(w);
404 catch(exception& e) {
405 m->errorOut(e, "Unweighted", "createProcesses");
409 /**************************************************************************************************/
410 EstOutput Unweighted::driver(Tree* t, vector< vector<string> > namesOfGroupCombos, int start, int num, bool usingGroups) {
413 EstOutput results; results.resize(num);
417 Tree* copyTree = new Tree;
419 for (int h = start; h < (start+num); h++) {
421 if (m->control_pressed) { return results; }
423 //copy random tree passed in
424 copyTree->getCopy(t);
426 //swap labels in the groups you want to compare
427 copyTree->assembleRandomUnifracTree(namesOfGroupCombos[h]);
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 //find a node that belongs to one of the groups in this combo
433 int nodeBelonging = -1;
434 for (int g = 0; g < namesOfGroupCombos[h].size(); g++) {
435 if (copyTree->groupNodeInfo[namesOfGroupCombos[h][g]].size() != 0) { nodeBelonging = copyTree->groupNodeInfo[namesOfGroupCombos[h][g]][0]; break; }
439 if (nodeBelonging == -1) {
440 m->mothurOut("[WARNING]: cannot find a nodes in the tree from grouping ");
441 for (int g = 0; g < namesOfGroupCombos[h].size()-1; g++) { m->mothurOut(namesOfGroupCombos[h][g] + "-"); }
442 m->mothurOut(namesOfGroupCombos[h][namesOfGroupCombos[h].size()-1]);
443 m->mothurOut(", skipping."); m->mothurOutEndLine(); results[count] = UW;
446 //if including the root this clears rootForGrouping[namesOfGroupCombos[h]]
447 getRoot(copyTree, nodeBelonging, namesOfGroupCombos[h]);
449 for(int i=0;i<copyTree->getNumNodes();i++){
451 if (m->control_pressed) { return data; }
453 //pcountSize = 0, they are from a branch that is entirely from a group the user doesn't want
454 //pcountSize = 2, not unique to one group
455 //pcountSize = 1, unique to one group
458 for (int j = 0; j < namesOfGroupCombos[h].size(); j++) {
459 map<string, int>::iterator itGroup = copyTree->tree[i].pcount.find(namesOfGroupCombos[h][j]);
460 if (itGroup != copyTree->tree[i].pcount.end()) { pcountSize++; if (pcountSize > 1) { break; } }
464 if (pcountSize == 0) { }
465 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
466 UniqueBL += abs(copyTree->tree[i].getBranchLength());
470 if (pcountSize == 0) { }
471 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
472 totalBL += abs(copyTree->tree[i].getBranchLength());
476 //cout << UniqueBL << '\t' << totalBL << endl;
477 UW = (UniqueBL / totalBL);
479 if (isnan(UW) || isinf(UW)) { UW = 0; }
491 catch(exception& e) {
492 m->errorOut(e, "Unweighted", "driver");
496 /**************************************************************************************************/
497 int Unweighted::getRoot(Tree* t, int v, vector<string> grouping) {
499 //you are a leaf so get your parent
500 int index = t->tree[v].getParent();
503 rootForGrouping[grouping].clear();
506 //my parent is a potential root
507 rootForGrouping[grouping].insert(index);
509 //while you aren't at root
510 while(t->tree[index].getParent() != -1){
511 //cout << index << endl;
512 if (m->control_pressed) { return 0; }
514 //am I the root for this grouping? if so I want to stop "early"
515 //does my sibling have descendants from the users groups?
516 //if so I am not the root
517 int parent = t->tree[index].getParent();
518 int lc = t->tree[parent].getLChild();
519 int rc = t->tree[parent].getRChild();
522 if (lc == index) { sib = rc; }
524 map<string, int>::iterator itGroup;
526 for (int j = 0; j < grouping.size(); j++) {
527 map<string, int>::iterator itGroup = t->tree[sib].pcount.find(grouping[j]);
528 if (itGroup != t->tree[sib].pcount.end()) { pcountSize++; if (pcountSize > 1) { break; } }
531 //if yes, I am not the root
532 if (pcountSize != 0) {
533 rootForGrouping[grouping].clear();
534 rootForGrouping[grouping].insert(parent);
540 //get all nodes above the root to add so we don't add their u values above
541 index = *(rootForGrouping[grouping].begin());
542 while(t->tree[index].getParent() != -1){
543 int parent = t->tree[index].getParent();
544 rootForGrouping[grouping].insert(parent);
545 //cout << parent << " in root" << endl;
552 catch(exception& e) {
553 m->errorOut(e, "Unweighted", "getRoot");
557 /**************************************************************************************************/