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();
263 //if the users enters no groups then give them the score of all groups
264 int numGroups = globaldata->Groups.size();
266 //calculate number of comparsions
268 vector< vector<string> > namesOfGroupCombos;
269 for (int r=0; r<numGroups; r++) {
270 for (int l = 0; l < r; l++) {
272 vector<string> groups; groups.push_back(globaldata->Groups[r]); groups.push_back(globaldata->Groups[l]);
273 namesOfGroupCombos.push_back(groups);
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]);
286 namesOfGroupCombos.push_back(groups);
288 for (int i = 0; i < globaldata->Groups.size(); i++) {
289 groups.push_back(globaldata->Groups[i]);
291 namesOfGroupCombos.push_back(groups);
295 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
297 data = driver(t, namesOfGroupCombos, 0, namesOfGroupCombos.size(), true);
299 int numPairs = namesOfGroupCombos.size();
301 int numPairsPerProcessor = numPairs / processors;
303 for (int i = 0; i < processors; i++) {
304 int startPos = i * numPairsPerProcessor;
305 if(i == processors - 1){
306 numPairsPerProcessor = numPairs - i * numPairsPerProcessor;
308 lines.push_back(linePair(startPos, numPairsPerProcessor));
311 data = createProcesses(t, namesOfGroupCombos, true);
316 data = driver(t, namesOfGroupCombos, 0, namesOfGroupCombos.size(), true);
321 catch(exception& e) {
322 m->errorOut(e, "Unweighted", "getValues");
326 /**************************************************************************************************/
328 EstOutput Unweighted::createProcesses(Tree* t, vector< vector<string> > namesOfGroupCombos, bool usingGroups) {
330 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
332 vector<int> processIDS;
336 //loop through and create all the processes you want
337 while (process != processors) {
341 processIDS.push_back(pid); //create map from line number to pid so you can append files in correct order later
345 myresults = driver(t, namesOfGroupCombos, lines[process].start, lines[process].num, usingGroups);
347 if (m->control_pressed) { exit(0); }
349 //pass numSeqs to parent
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;
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); }
365 results = driver(t, namesOfGroupCombos, lines[0].start, lines[0].num, usingGroups);
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];
373 if (m->control_pressed) { return results; }
375 //get data created by processes
376 for (int i=0;i<(processors-1);i++) {
378 string s = outputDir + toString(processIDS[i]) + ".unweighted.results.temp";
379 m->openInputFile(s, in);
384 in >> num; m->gobble(in);
386 if (m->control_pressed) { break; }
389 for (int j = 0; j < num; j++) {
392 results.push_back(w);
403 catch(exception& e) {
404 m->errorOut(e, "Unweighted", "createProcesses");
408 /**************************************************************************************************/
409 EstOutput Unweighted::driver(Tree* t, vector< vector<string> > namesOfGroupCombos, int start, int num, bool usingGroups) {
412 EstOutput results; results.resize(num);
416 Tree* copyTree = new Tree;
418 for (int h = start; h < (start+num); h++) {
420 if (m->control_pressed) { return results; }
422 //copy random tree passed in
423 copyTree->getCopy(t);
425 //swap labels in the groups you want to compare
426 copyTree->assembleRandomUnifracTree(namesOfGroupCombos[h]);
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; }
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;
445 getRoot(copyTree, nodeBelonging, namesOfGroupCombos[h]);
447 for(int i=0;i<copyTree->getNumNodes();i++){
449 if (m->control_pressed) { return data; }
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
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; } }
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());
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());
474 //cout << UniqueBL << '\t' << totalBL << endl;
475 UW = (UniqueBL / totalBL);
477 if (isnan(UW) || isinf(UW)) { UW = 0; }
489 catch(exception& e) {
490 m->errorOut(e, "Unweighted", "driver");
494 /**************************************************************************************************/
495 int Unweighted::getRoot(Tree* t, int v, vector<string> grouping) {
497 //you are a leaf so get your parent
498 int index = t->tree[index].getParent();
500 //my parent is a potential root
501 rootForGrouping[grouping].insert(index);
503 //while you aren't at root
504 while(t->tree[index].getParent() != -1){
506 if (m->control_pressed) { return 0; }
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();
516 if (lc == index) { sib = rc; }
518 map<string, int>::iterator itGroup;
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; } }
525 //if yes, I am not the root
526 if (pcountSize != 0) {
527 rootForGrouping[grouping].clear();
528 rootForGrouping[grouping].insert(parent);
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;
545 catch(exception& e) {
546 m->errorOut(e, "Unweighted", "getRoot");
550 /**************************************************************************************************/