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) {
19 //if the users enters no groups then give them the score of all groups
20 int numGroups = m->getNumGroups();
22 //calculate number of comparsions
24 vector< vector<string> > namesOfGroupCombos;
25 for (int r=0; r<numGroups; r++) {
26 for (int l = 0; l < r; l++) {
28 vector<string> groups; groups.push_back((m->getGroups())[r]); groups.push_back((m->getGroups())[l]);
29 namesOfGroupCombos.push_back(groups);
34 vector<string> groups;
36 //get score for all users groups
37 for (int i = 0; i < (tmap->getNamesOfGroups()).size(); i++) {
38 if ((tmap->getNamesOfGroups())[i] != "xxx") {
39 groups.push_back((tmap->getNamesOfGroups())[i]);
42 namesOfGroupCombos.push_back(groups);
44 for (int i = 0; i < m->getNumGroups(); i++) {
45 groups.push_back((m->getGroups())[i]);
47 namesOfGroupCombos.push_back(groups);
51 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux) || (__linux__) || (__unix__) || (__unix)
53 data = driver(t, namesOfGroupCombos, 0, namesOfGroupCombos.size());
55 int numPairs = namesOfGroupCombos.size();
57 int numPairsPerProcessor = numPairs / processors;
59 for (int i = 0; i < processors; i++) {
60 int startPos = i * numPairsPerProcessor;
62 if(i == processors - 1){
63 numPairsPerProcessor = numPairs - i * numPairsPerProcessor;
66 lines.push_back(linePair(startPos, numPairsPerProcessor));
68 data = createProcesses(t, namesOfGroupCombos);
72 data = driver(t, namesOfGroupCombos, 0, namesOfGroupCombos.size());
78 m->errorOut(e, "Unweighted", "getValues");
82 /**************************************************************************************************/
84 EstOutput Unweighted::createProcesses(Tree* t, vector< vector<string> > namesOfGroupCombos) {
86 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux) || (__linux__) || (__unix__) || (__unix)
88 vector<int> processIDS;
92 //loop through and create all the processes you want
93 while (process != processors) {
97 processIDS.push_back(pid); //create map from line number to pid so you can append files in correct order later
101 myresults = driver(t, namesOfGroupCombos, lines[process].start, lines[process].num);
103 if (m->control_pressed) { exit(0); }
105 m->mothurOut("Merging results."); m->mothurOutEndLine();
107 //pass numSeqs to parent
109 string tempFile = outputDir + toString(getpid()) + ".unweighted.results.temp";
110 m->openOutputFile(tempFile, out);
111 out << myresults.size() << endl;
112 for (int i = 0; i < myresults.size(); i++) { out << myresults[i] << '\t'; } out << endl;
117 m->mothurOut("[ERROR]: unable to spawn the necessary processes."); m->mothurOutEndLine();
118 for (int i = 0; i < processIDS.size(); i++) { kill (processIDS[i], SIGINT); }
123 results = driver(t, namesOfGroupCombos, lines[0].start, lines[0].num);
125 //force parent to wait until all the processes are done
126 for (int i=0;i<(processors-1);i++) {
127 int temp = processIDS[i];
131 if (m->control_pressed) { return results; }
133 //get data created by processes
134 for (int i=0;i<(processors-1);i++) {
136 string s = outputDir + toString(processIDS[i]) + ".unweighted.results.temp";
137 m->openInputFile(s, in);
142 in >> num; m->gobble(in);
144 if (m->control_pressed) { break; }
147 for (int j = 0; j < num; j++) {
149 results.push_back(w);
157 m->mothurOut("DONE."); m->mothurOutEndLine(); m->mothurOutEndLine();
162 catch(exception& e) {
163 m->errorOut(e, "Unweighted", "createProcesses");
167 /**************************************************************************************************/
168 EstOutput Unweighted::driver(Tree* t, vector< vector<string> > namesOfGroupCombos, int start, int num) {
172 EstOutput results; results.resize(num);
176 int twentyPercent = (total * 0.20);
177 if (twentyPercent == 0) { twentyPercent = 1; }
180 for (int h = start; h < (start+num); h++) {
182 if (m->control_pressed) { return results; }
184 double UniqueBL=0.0000; //a branch length is unique if it's chidren are from the same group
185 double totalBL = 0.00; //all branch lengths
186 double UW = 0.00; //Unweighted Value = UniqueBL / totalBL;
188 //find a node that belongs to one of the groups in this combo
189 int nodeBelonging = -1;
190 for (int g = 0; g < namesOfGroupCombos[h].size(); g++) {
191 if (t->groupNodeInfo[namesOfGroupCombos[h][g]].size() != 0) { nodeBelonging = t->groupNodeInfo[namesOfGroupCombos[h][g]][0]; break; }
195 if (nodeBelonging == -1) {
196 m->mothurOut("[WARNING]: cannot find a nodes in the tree from grouping ");
197 for (int g = 0; g < namesOfGroupCombos[h].size()-1; g++) { m->mothurOut(namesOfGroupCombos[h][g] + "-"); }
198 m->mothurOut(namesOfGroupCombos[h][namesOfGroupCombos[h].size()-1]);
199 m->mothurOut(", skipping."); m->mothurOutEndLine(); results[count] = UW;
201 //cout << "trying to get root" << endl;
202 //if including the root this clears rootForGrouping[namesOfGroupCombos[h]]
203 getRoot(t, nodeBelonging, namesOfGroupCombos[h]);
204 //cout << "here" << endl;
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; } }
221 if (pcountSize == 0) { }
222 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
223 UniqueBL += abs(t->tree[i].getBranchLength());
227 if (pcountSize == 0) { }
228 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
229 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) {
262 //if the users enters no groups then give them the score of all groups
263 int numGroups = m->getNumGroups();
265 //calculate number of comparsions
267 vector< vector<string> > namesOfGroupCombos;
268 for (int r=0; r<numGroups; r++) {
269 for (int l = 0; l < r; l++) {
271 vector<string> groups; groups.push_back((m->getGroups())[r]); groups.push_back((m->getGroups())[l]);
272 namesOfGroupCombos.push_back(groups);
277 vector<string> groups;
278 if (numGroups == 0) {
279 //get score for all users groups
280 for (int i = 0; i < (tmap->getNamesOfGroups()).size(); i++) {
281 if ((tmap->getNamesOfGroups())[i] != "xxx") {
282 groups.push_back((tmap->getNamesOfGroups())[i]);
285 namesOfGroupCombos.push_back(groups);
287 for (int i = 0; i < m->getNumGroups(); i++) {
288 groups.push_back((m->getGroups())[i]);
290 namesOfGroupCombos.push_back(groups);
294 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux) || (__linux__) || (__unix__) || (__unix)
296 data = driver(t, namesOfGroupCombos, 0, namesOfGroupCombos.size(), true);
298 int numPairs = namesOfGroupCombos.size();
300 int numPairsPerProcessor = numPairs / processors;
302 for (int i = 0; i < processors; i++) {
303 int startPos = i * numPairsPerProcessor;
304 if(i == processors - 1){
305 numPairsPerProcessor = numPairs - i * numPairsPerProcessor;
307 lines.push_back(linePair(startPos, numPairsPerProcessor));
310 data = createProcesses(t, namesOfGroupCombos, true);
315 data = driver(t, namesOfGroupCombos, 0, namesOfGroupCombos.size(), true);
320 catch(exception& e) {
321 m->errorOut(e, "Unweighted", "getValues");
325 /**************************************************************************************************/
327 EstOutput Unweighted::createProcesses(Tree* t, vector< vector<string> > namesOfGroupCombos, bool usingGroups) {
329 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux) || (__linux__) || (__unix__) || (__unix)
331 vector<int> processIDS;
335 //loop through and create all the processes you want
336 while (process != processors) {
340 processIDS.push_back(pid); //create map from line number to pid so you can append files in correct order later
344 myresults = driver(t, namesOfGroupCombos, lines[process].start, lines[process].num, usingGroups);
346 if (m->control_pressed) { exit(0); }
348 //pass numSeqs to parent
350 string tempFile = outputDir + toString(getpid()) + ".unweighted.results.temp";
351 m->openOutputFile(tempFile, out);
352 out << myresults.size() << endl;
353 for (int i = 0; i < myresults.size(); i++) { out << myresults[i] << '\t'; } out << endl;
358 m->mothurOut("[ERROR]: unable to spawn the necessary processes."); m->mothurOutEndLine();
359 for (int i = 0; i < processIDS.size(); i++) { kill (processIDS[i], SIGINT); }
364 results = driver(t, namesOfGroupCombos, lines[0].start, lines[0].num, usingGroups);
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];
372 if (m->control_pressed) { return results; }
374 //get data created by processes
375 for (int i=0;i<(processors-1);i++) {
377 string s = outputDir + toString(processIDS[i]) + ".unweighted.results.temp";
378 m->openInputFile(s, in);
383 in >> num; m->gobble(in);
385 if (m->control_pressed) { break; }
388 for (int j = 0; j < num; j++) {
391 results.push_back(w);
402 catch(exception& e) {
403 m->errorOut(e, "Unweighted", "createProcesses");
407 /**************************************************************************************************/
408 EstOutput Unweighted::driver(Tree* t, vector< vector<string> > namesOfGroupCombos, int start, int num, bool usingGroups) {
411 EstOutput results; results.resize(num);
415 Tree* copyTree = new Tree(tmap);
417 for (int h = start; h < (start+num); h++) {
419 if (m->control_pressed) { return results; }
421 //copy random tree passed in
422 copyTree->getCopy(t);
424 //swap labels in the groups you want to compare
425 copyTree->assembleRandomUnifracTree(namesOfGroupCombos[h]);
427 double UniqueBL=0.0000; //a branch length is unique if it's chidren are from the same group
428 double totalBL = 0.00; //all branch lengths
429 double UW = 0.00; //Unweighted Value = UniqueBL / totalBL;
430 //find a node that belongs to one of the groups in this combo
431 int nodeBelonging = -1;
432 for (int g = 0; g < namesOfGroupCombos[h].size(); g++) {
433 if (copyTree->groupNodeInfo[namesOfGroupCombos[h][g]].size() != 0) { nodeBelonging = copyTree->groupNodeInfo[namesOfGroupCombos[h][g]][0]; break; }
437 if (nodeBelonging == -1) {
438 m->mothurOut("[WARNING]: cannot find a nodes in the tree from grouping ");
439 for (int g = 0; g < namesOfGroupCombos[h].size()-1; g++) { m->mothurOut(namesOfGroupCombos[h][g] + "-"); }
440 m->mothurOut(namesOfGroupCombos[h][namesOfGroupCombos[h].size()-1]);
441 m->mothurOut(", skipping."); m->mothurOutEndLine(); results[count] = UW;
444 //if including the root this clears rootForGrouping[namesOfGroupCombos[h]]
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[v].getParent();
501 rootForGrouping[grouping].clear();
504 //my parent is a potential root
505 rootForGrouping[grouping].insert(index);
507 //while you aren't at root
508 while(t->tree[index].getParent() != -1){
509 //cout << index << endl;
510 if (m->control_pressed) { return 0; }
512 //am I the root for this grouping? if so I want to stop "early"
513 //does my sibling have descendants from the users groups?
514 //if so I am not the root
515 int parent = t->tree[index].getParent();
516 int lc = t->tree[parent].getLChild();
517 int rc = t->tree[parent].getRChild();
520 if (lc == index) { sib = rc; }
522 map<string, int>::iterator itGroup;
524 for (int j = 0; j < grouping.size(); j++) {
525 map<string, int>::iterator itGroup = t->tree[sib].pcount.find(grouping[j]);
526 if (itGroup != t->tree[sib].pcount.end()) { pcountSize++; if (pcountSize > 1) { break; } }
529 //if yes, I am not the root
530 if (pcountSize != 0) {
531 rootForGrouping[grouping].clear();
532 rootForGrouping[grouping].insert(parent);
538 //get all nodes above the root to add so we don't add their u values above
539 index = *(rootForGrouping[grouping].begin());
540 while(t->tree[index].getParent() != -1){
541 int parent = t->tree[index].getParent();
542 rootForGrouping[grouping].insert(parent);
543 //cout << parent << " in root" << endl;
550 catch(exception& e) {
551 m->errorOut(e, "Unweighted", "getRoot");
555 /**************************************************************************************************/