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 CountTable* ct = t->getCountTable();
21 //if the users enters no groups then give them the score of all groups
22 int numGroups = m->getNumGroups();
24 //calculate number of comparsions
26 vector< vector<string> > namesOfGroupCombos;
27 for (int r=0; r<numGroups; r++) {
28 for (int l = 0; l < r; l++) {
30 vector<string> groups; groups.push_back((m->getGroups())[r]); groups.push_back((m->getGroups())[l]);
31 namesOfGroupCombos.push_back(groups);
36 vector<string> groups;
38 //get score for all users groups
39 for (int i = 0; i < (ct->getNamesOfGroups()).size(); i++) {
40 if ((ct->getNamesOfGroups())[i] != "xxx") {
41 groups.push_back((ct->getNamesOfGroups())[i]);
44 namesOfGroupCombos.push_back(groups);
46 for (int i = 0; i < m->getNumGroups(); i++) {
47 groups.push_back((m->getGroups())[i]);
49 namesOfGroupCombos.push_back(groups);
54 int remainingPairs = namesOfGroupCombos.size();
56 for (int remainingProcessors = processors; remainingProcessors > 0; remainingProcessors--) {
57 int numPairs = remainingPairs; //case for last processor
58 if (remainingProcessors != 1) { numPairs = ceil(remainingPairs / remainingProcessors); }
59 lines.push_back(linePair(startIndex, numPairs)); //startIndex, numPairs
60 startIndex = startIndex + numPairs;
61 remainingPairs = remainingPairs - numPairs;
64 data = createProcesses(t, namesOfGroupCombos, ct);
71 m->errorOut(e, "Unweighted", "getValues");
75 /**************************************************************************************************/
77 EstOutput Unweighted::createProcesses(Tree* t, vector< vector<string> > namesOfGroupCombos, CountTable* ct) {
80 vector<int> processIDS;
83 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux) || (__linux__) || (__unix__) || (__unix)
86 //loop through and create all the processes you want
87 while (process != processors) {
91 processIDS.push_back(pid); //create map from line number to pid so you can append files in correct order later
95 myresults = driver(t, namesOfGroupCombos, lines[process].start, lines[process].num, ct);
97 if (m->control_pressed) { exit(0); }
99 //m->mothurOut("Merging results."); m->mothurOutEndLine();
101 //pass numSeqs to parent
103 string tempFile = outputDir + toString(getpid()) + ".unweighted.results.temp";
104 m->openOutputFile(tempFile, out);
105 out << myresults.size() << endl;
106 for (int i = 0; i < myresults.size(); i++) { out << myresults[i] << '\t'; } out << endl;
111 m->mothurOut("[ERROR]: unable to spawn the necessary processes."); m->mothurOutEndLine();
112 for (int i = 0; i < processIDS.size(); i++) { kill (processIDS[i], SIGINT); }
117 results = driver(t, namesOfGroupCombos, lines[0].start, lines[0].num, ct);
119 //force parent to wait until all the processes are done
120 for (int i=0;i<(processors-1);i++) {
121 int temp = processIDS[i];
125 if (m->control_pressed) { return results; }
127 //get data created by processes
128 for (int i=0;i<(processors-1);i++) {
130 string s = outputDir + toString(processIDS[i]) + ".unweighted.results.temp";
131 m->openInputFile(s, in);
136 in >> num; m->gobble(in);
138 if (m->control_pressed) { break; }
141 for (int j = 0; j < num; j++) {
143 results.push_back(w);
152 vector<unweightedData*> pDataArray;
153 DWORD dwThreadIdArray[processors-1];
154 HANDLE hThreadArray[processors-1];
155 vector<CountTable*> cts;
158 //Create processor worker threads.
159 for( int i=1; i<processors; i++ ){
160 CountTable* copyCount = new CountTable();
162 Tree* copyTree = new Tree(copyCount);
163 copyTree->getCopy(t);
165 cts.push_back(copyCount);
166 trees.push_back(copyTree);
168 unweightedData* tempweighted = new unweightedData(m, lines[i].start, lines[i].num, namesOfGroupCombos, copyTree, copyCount, includeRoot);
169 pDataArray.push_back(tempweighted);
170 processIDS.push_back(i);
172 hThreadArray[i-1] = CreateThread(NULL, 0, MyUnWeightedThreadFunction, pDataArray[i-1], 0, &dwThreadIdArray[i-1]);
175 results = driver(t, namesOfGroupCombos, lines[0].start, lines[0].num, ct);
177 //Wait until all threads have terminated.
178 WaitForMultipleObjects(processors-1, hThreadArray, TRUE, INFINITE);
180 //Close all thread handles and free memory allocations.
181 for(int i=0; i < pDataArray.size(); i++){
182 for (int j = 0; j < pDataArray[i]->results.size(); j++) { results.push_back(pDataArray[i]->results[j]); }
185 CloseHandle(hThreadArray[i]);
186 delete pDataArray[i];
192 catch(exception& e) {
193 m->errorOut(e, "Unweighted", "createProcesses");
197 /**************************************************************************************************/
198 EstOutput Unweighted::driver(Tree* t, vector< vector<string> > namesOfGroupCombos, int start, int num, CountTable* ct) {
202 EstOutput results; results.resize(num);
207 for (int h = start; h < (start+num); h++) {
209 if (m->control_pressed) { return results; }
211 double UniqueBL=0.0000; //a branch length is unique if it's chidren are from the same group
212 double totalBL = 0.00; //all branch lengths
213 double UW = 0.00; //Unweighted Value = UniqueBL / totalBL;
215 //find a node that belongs to one of the groups in this combo
216 int nodeBelonging = -1;
217 for (int g = 0; g < namesOfGroupCombos[h].size(); g++) {
218 if (t->groupNodeInfo[namesOfGroupCombos[h][g]].size() != 0) { nodeBelonging = t->groupNodeInfo[namesOfGroupCombos[h][g]][0]; break; }
222 if (nodeBelonging == -1) {
223 m->mothurOut("[WARNING]: cannot find a nodes in the tree from grouping ");
224 for (int g = 0; g < namesOfGroupCombos[h].size()-1; g++) { m->mothurOut(namesOfGroupCombos[h][g] + "-"); }
225 m->mothurOut(namesOfGroupCombos[h][namesOfGroupCombos[h].size()-1]);
226 m->mothurOut(", skipping."); m->mothurOutEndLine(); results[count] = UW;
228 //cout << "trying to get root" << endl;
229 //if including the root this clears rootForGrouping[namesOfGroupCombos[h]]
230 getRoot(t, nodeBelonging, namesOfGroupCombos[h]);
231 //cout << "here" << endl;
232 for(int i=0;i<t->getNumNodes();i++){
234 if (m->control_pressed) { return data; }
236 //pcountSize = 0, they are from a branch that is entirely from a group the user doesn't want
237 //pcountSize = 2, not unique to one group
238 //pcountSize = 1, unique to one group
241 for (int j = 0; j < namesOfGroupCombos[h].size(); j++) {
242 map<string, int>::iterator itGroup = t->tree[i].pcount.find(namesOfGroupCombos[h][j]);
243 if (itGroup != t->tree[i].pcount.end()) { pcountSize++; if (pcountSize > 1) { break; } }
248 if (pcountSize == 0) { }
249 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
250 UniqueBL += abs(t->tree[i].getBranchLength());
254 if (pcountSize == 0) { }
255 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
256 totalBL += abs(t->tree[i].getBranchLength());
259 //cout << UniqueBL << '\t' << totalBL << endl;
260 UW = (UniqueBL / totalBL);
262 if (isnan(UW) || isinf(UW)) { UW = 0; }
272 catch(exception& e) {
273 m->errorOut(e, "Unweighted", "driver");
277 /**************************************************************************************************/
279 EstOutput Unweighted::getValues(Tree* t, string groupA, string groupB, int p, string o) {
284 CountTable* ct = t->getCountTable();
286 //if the users enters no groups then give them the score of all groups
287 int numGroups = m->getNumGroups();
289 //calculate number of comparsions
291 vector< vector<string> > namesOfGroupCombos;
292 for (int r=0; r<numGroups; r++) {
293 for (int l = 0; l < r; l++) {
295 vector<string> groups; groups.push_back((m->getGroups())[r]); groups.push_back((m->getGroups())[l]);
296 namesOfGroupCombos.push_back(groups);
301 vector<string> groups;
302 if (numGroups == 0) {
303 //get score for all users groups
304 for (int i = 0; i < (ct->getNamesOfGroups()).size(); i++) {
305 if ((ct->getNamesOfGroups())[i] != "xxx") {
306 groups.push_back((ct->getNamesOfGroups())[i]);
309 namesOfGroupCombos.push_back(groups);
311 for (int i = 0; i < m->getNumGroups(); i++) {
312 groups.push_back((m->getGroups())[i]);
314 namesOfGroupCombos.push_back(groups);
319 int numPairs = namesOfGroupCombos.size();
320 int numPairsPerProcessor = ceil(numPairs / processors);
322 for (int i = 0; i < processors; i++) {
323 int startPos = i * numPairsPerProcessor;
324 if(i == processors - 1){ numPairsPerProcessor = numPairs - i * numPairsPerProcessor; }
325 lines.push_back(linePair(startPos, numPairsPerProcessor));
328 data = createProcesses(t, namesOfGroupCombos, true, ct);
333 catch(exception& e) {
334 m->errorOut(e, "Unweighted", "getValues");
338 /**************************************************************************************************/
340 EstOutput Unweighted::createProcesses(Tree* t, vector< vector<string> > namesOfGroupCombos, bool usingGroups, CountTable* ct) {
343 vector<int> processIDS;
346 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux) || (__linux__) || (__unix__) || (__unix)
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, ct);
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, ct);
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++) {
404 results.push_back(w);
412 //for some reason it doesn't seem to be calculating hte random trees scores. all scores are the same even though copytree appears to be randomized.
416 vector<unweightedData*> pDataArray;
417 DWORD dwThreadIdArray[processors-1];
418 HANDLE hThreadArray[processors-1];
419 vector<CountTable*> cts;
422 //Create processor worker threads.
423 for( int i=1; i<processors; i++ ){
424 CountTable* copyCount = new CountTable();
426 Tree* copyTree = new Tree(copyCount);
427 copyTree->getCopy(t);
429 cts.push_back(copyCount);
430 trees.push_back(copyTree);
432 unweightedData* tempweighted = new unweightedData(m, lines[i].start, lines[i].num, namesOfGroupCombos, copyTree, copyCount, includeRoot);
433 pDataArray.push_back(tempweighted);
434 processIDS.push_back(i);
436 hThreadArray[i-1] = CreateThread(NULL, 0, MyUnWeightedRandomThreadFunction, pDataArray[i-1], 0, &dwThreadIdArray[i-1]);
439 results = driver(t, namesOfGroupCombos, lines[0].start, lines[0].num, usingGroups, ct);
441 //Wait until all threads have terminated.
442 WaitForMultipleObjects(processors-1, hThreadArray, TRUE, INFINITE);
444 //Close all thread handles and free memory allocations.
445 for(int i=0; i < pDataArray.size(); i++){
446 for (int j = 0; j < pDataArray[i]->results.size(); j++) { results.push_back(pDataArray[i]->results[j]); }
449 CloseHandle(hThreadArray[i]);
450 delete pDataArray[i];
453 results = driver(t, namesOfGroupCombos, 0, namesOfGroupCombos.size(), usingGroups, ct);
457 catch(exception& e) {
458 m->errorOut(e, "Unweighted", "createProcesses");
462 /**************************************************************************************************/
463 EstOutput Unweighted::driver(Tree* t, vector< vector<string> > namesOfGroupCombos, int start, int num, bool usingGroups, CountTable* ct) {
466 EstOutput results; results.resize(num);
470 Tree* copyTree = new Tree(ct);
472 for (int h = start; h < (start+num); h++) {
474 if (m->control_pressed) { return results; }
476 //copy random tree passed in
477 copyTree->getCopy(t);
479 //swap labels in the groups you want to compare
480 copyTree->assembleRandomUnifracTree(namesOfGroupCombos[h]);
482 double UniqueBL=0.0000; //a branch length is unique if it's chidren are from the same group
483 double totalBL = 0.00; //all branch lengths
484 double UW = 0.00; //Unweighted Value = UniqueBL / totalBL;
485 //find a node that belongs to one of the groups in this combo
486 int nodeBelonging = -1;
487 for (int g = 0; g < namesOfGroupCombos[h].size(); g++) {
488 if (copyTree->groupNodeInfo[namesOfGroupCombos[h][g]].size() != 0) { nodeBelonging = copyTree->groupNodeInfo[namesOfGroupCombos[h][g]][0]; break; }
492 if (nodeBelonging == -1) {
493 m->mothurOut("[WARNING]: cannot find a nodes in the tree from grouping ");
494 for (int g = 0; g < namesOfGroupCombos[h].size()-1; g++) { m->mothurOut(namesOfGroupCombos[h][g] + "-"); }
495 m->mothurOut(namesOfGroupCombos[h][namesOfGroupCombos[h].size()-1]);
496 m->mothurOut(", skipping."); m->mothurOutEndLine(); results[count] = UW;
499 //if including the root this clears rootForGrouping[namesOfGroupCombos[h]]
500 getRoot(copyTree, nodeBelonging, namesOfGroupCombos[h]);
502 for(int i=0;i<copyTree->getNumNodes();i++){
504 if (m->control_pressed) { return data; }
506 //pcountSize = 0, they are from a branch that is entirely from a group the user doesn't want
507 //pcountSize = 2, not unique to one group
508 //pcountSize = 1, unique to one group
511 for (int j = 0; j < namesOfGroupCombos[h].size(); j++) {
512 map<string, int>::iterator itGroup = copyTree->tree[i].pcount.find(namesOfGroupCombos[h][j]);
513 if (itGroup != copyTree->tree[i].pcount.end()) { pcountSize++; if (pcountSize > 1) { break; } }
517 if (pcountSize == 0) { }
518 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
519 UniqueBL += abs(copyTree->tree[i].getBranchLength());
523 if (pcountSize == 0) { }
524 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
525 totalBL += abs(copyTree->tree[i].getBranchLength());
529 //cout << UniqueBL << '\t' << totalBL << endl;
530 UW = (UniqueBL / totalBL);
532 if (isnan(UW) || isinf(UW)) { UW = 0; }
544 catch(exception& e) {
545 m->errorOut(e, "Unweighted", "driver");
549 /**************************************************************************************************/
550 int Unweighted::getRoot(Tree* t, int v, vector<string> grouping) {
552 //you are a leaf so get your parent
553 int index = t->tree[v].getParent();
556 rootForGrouping[grouping].clear();
559 //my parent is a potential root
560 rootForGrouping[grouping].insert(index);
562 //while you aren't at root
563 while(t->tree[index].getParent() != -1){
564 //cout << index << endl;
565 if (m->control_pressed) { return 0; }
567 //am I the root for this grouping? if so I want to stop "early"
568 //does my sibling have descendants from the users groups?
569 //if so I am not the root
570 int parent = t->tree[index].getParent();
571 int lc = t->tree[parent].getLChild();
572 int rc = t->tree[parent].getRChild();
575 if (lc == index) { sib = rc; }
577 map<string, int>::iterator itGroup;
579 for (int j = 0; j < grouping.size(); j++) {
580 map<string, int>::iterator itGroup = t->tree[sib].pcount.find(grouping[j]);
581 if (itGroup != t->tree[sib].pcount.end()) { pcountSize++; if (pcountSize > 1) { break; } }
584 //if yes, I am not the root
585 if (pcountSize != 0) {
586 rootForGrouping[grouping].clear();
587 rootForGrouping[grouping].insert(parent);
593 //get all nodes above the root to add so we don't add their u values above
594 index = *(rootForGrouping[grouping].begin());
595 while(t->tree[index].getParent() != -1){
596 int parent = t->tree[index].getParent();
597 rootForGrouping[grouping].insert(parent);
598 //cout << parent << " in root" << endl;
605 catch(exception& e) {
606 m->errorOut(e, "Unweighted", "getRoot");
610 /**************************************************************************************************/