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;
203 getRoot(t, nodeBelonging, namesOfGroupCombos[h]);
205 for(int i=0;i<t->getNumNodes();i++){
207 if (m->control_pressed) { return data; }
209 //pcountSize = 0, they are from a branch that is entirely from a group the user doesn't want
210 //pcountSize = 2, not unique to one group
211 //pcountSize = 1, unique to one group
214 for (int j = 0; j < namesOfGroupCombos[h].size(); j++) {
215 map<string, int>::iterator itGroup = t->tree[i].pcount.find(namesOfGroupCombos[h][j]);
216 if (itGroup != t->tree[i].pcount.end()) { pcountSize++; if (pcountSize > 1) { break; } }
220 if (pcountSize == 0) { }
221 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
222 UniqueBL += abs(t->tree[i].getBranchLength());
226 if (pcountSize == 0) { }
227 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
228 totalBL += abs(t->tree[i].getBranchLength());
232 //cout << UniqueBL << '\t' << totalBL << endl;
233 UW = (UniqueBL / totalBL);
235 if (isnan(UW) || isinf(UW)) { UW = 0; }
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(); }
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(); }
250 catch(exception& e) {
251 m->errorOut(e, "Unweighted", "driver");
255 /**************************************************************************************************/
257 EstOutput Unweighted::getValues(Tree* t, string groupA, string groupB, int p, string o) {
259 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 getRoot(copyTree, nodeBelonging, namesOfGroupCombos[h]);
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; } }
463 if (pcountSize == 0) { }
464 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
465 UniqueBL += abs(copyTree->tree[i].getBranchLength());
469 if (pcountSize == 0) { }
470 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
471 totalBL += abs(copyTree->tree[i].getBranchLength());
475 //cout << UniqueBL << '\t' << totalBL << endl;
476 UW = (UniqueBL / totalBL);
478 if (isnan(UW) || isinf(UW)) { UW = 0; }
490 catch(exception& e) {
491 m->errorOut(e, "Unweighted", "driver");
495 /**************************************************************************************************/
496 int Unweighted::getRoot(Tree* t, int v, vector<string> grouping) {
498 //you are a leaf so get your parent
499 int index = t->tree[index].getParent();
501 //my parent is a potential root
502 rootForGrouping[grouping].insert(index);
504 //while you aren't at root
505 while(t->tree[index].getParent() != -1){
507 if (m->control_pressed) { return 0; }
509 //am I the root for this grouping? if so I want to stop "early"
510 //does my sibling have descendants from the users groups?
511 //if so I am not the root
512 int parent = t->tree[index].getParent();
513 int lc = t->tree[parent].getLChild();
514 int rc = t->tree[parent].getRChild();
517 if (lc == index) { sib = rc; }
519 map<string, int>::iterator itGroup;
521 for (int j = 0; j < grouping.size(); j++) {
522 map<string, int>::iterator itGroup = t->tree[sib].pcount.find(grouping[j]);
523 if (itGroup != t->tree[sib].pcount.end()) { pcountSize++; if (pcountSize > 1) { break; } }
526 //if yes, I am not the root
527 if (pcountSize != 0) {
528 rootForGrouping[grouping].clear();
529 rootForGrouping[grouping].insert(parent);
535 //get all nodes above the root to add so we don't add their u values above
536 index = *(rootForGrouping[grouping].begin());
537 while(t->tree[index].getParent() != -1){
538 int parent = t->tree[index].getParent();
539 rootForGrouping[grouping].insert(parent);
540 //cout << parent << " in root" << endl;
546 catch(exception& e) {
547 m->errorOut(e, "Unweighted", "getRoot");
551 /**************************************************************************************************/