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 numPairs = namesOfGroupCombos.size();
55 int numPairsPerProcessor = numPairs / processors;
57 for (int i = 0; i < processors; i++) {
58 int startPos = i * numPairsPerProcessor;
59 if(i == processors - 1){ numPairsPerProcessor = numPairs - i * numPairsPerProcessor; }
60 lines.push_back(linePair(startPos, numPairsPerProcessor));
63 data = createProcesses(t, namesOfGroupCombos, ct);
70 m->errorOut(e, "Unweighted", "getValues");
74 /**************************************************************************************************/
76 EstOutput Unweighted::createProcesses(Tree* t, vector< vector<string> > namesOfGroupCombos, CountTable* ct) {
79 vector<int> processIDS;
82 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux) || (__linux__) || (__unix__) || (__unix)
85 //loop through and create all the processes you want
86 while (process != processors) {
90 processIDS.push_back(pid); //create map from line number to pid so you can append files in correct order later
94 myresults = driver(t, namesOfGroupCombos, lines[process].start, lines[process].num, ct);
96 if (m->control_pressed) { exit(0); }
98 //m->mothurOut("Merging results."); m->mothurOutEndLine();
100 //pass numSeqs to parent
102 string tempFile = outputDir + toString(getpid()) + ".unweighted.results.temp";
103 m->openOutputFile(tempFile, out);
104 out << myresults.size() << endl;
105 for (int i = 0; i < myresults.size(); i++) { out << myresults[i] << '\t'; } out << endl;
110 m->mothurOut("[ERROR]: unable to spawn the necessary processes."); m->mothurOutEndLine();
111 for (int i = 0; i < processIDS.size(); i++) { kill (processIDS[i], SIGINT); }
116 results = driver(t, namesOfGroupCombos, lines[0].start, lines[0].num, ct);
118 //force parent to wait until all the processes are done
119 for (int i=0;i<(processors-1);i++) {
120 int temp = processIDS[i];
124 if (m->control_pressed) { return results; }
126 //get data created by processes
127 for (int i=0;i<(processors-1);i++) {
129 string s = outputDir + toString(processIDS[i]) + ".unweighted.results.temp";
130 m->openInputFile(s, in);
135 in >> num; m->gobble(in);
137 if (m->control_pressed) { break; }
140 for (int j = 0; j < num; j++) {
142 results.push_back(w);
151 vector<unweightedData*> pDataArray;
152 DWORD dwThreadIdArray[processors-1];
153 HANDLE hThreadArray[processors-1];
154 vector<CountTable*> cts;
157 //Create processor worker threads.
158 for( int i=1; i<processors; i++ ){
159 CountTable* copyCount = new CountTable();
161 Tree* copyTree = new Tree(copyCount);
162 copyTree->getCopy(t);
164 cts.push_back(copyCount);
165 trees.push_back(copyTree);
167 unweightedData* tempweighted = new unweightedData(m, lines[i].start, lines[i].num, namesOfGroupCombos, copyTree, copyCount, includeRoot);
168 pDataArray.push_back(tempweighted);
169 processIDS.push_back(i);
171 hThreadArray[i-1] = CreateThread(NULL, 0, MyUnWeightedThreadFunction, pDataArray[i-1], 0, &dwThreadIdArray[i-1]);
174 results = driver(t, namesOfGroupCombos, lines[0].start, lines[0].num, ct);
176 //Wait until all threads have terminated.
177 WaitForMultipleObjects(processors-1, hThreadArray, TRUE, INFINITE);
179 //Close all thread handles and free memory allocations.
180 for(int i=0; i < pDataArray.size(); i++){
181 for (int j = 0; j < pDataArray[i]->results.size(); j++) { results.push_back(pDataArray[i]->results[j]); }
184 CloseHandle(hThreadArray[i]);
185 delete pDataArray[i];
191 catch(exception& e) {
192 m->errorOut(e, "Unweighted", "createProcesses");
196 /**************************************************************************************************/
197 EstOutput Unweighted::driver(Tree* t, vector< vector<string> > namesOfGroupCombos, int start, int num, CountTable* ct) {
201 EstOutput results; results.resize(num);
206 for (int h = start; h < (start+num); h++) {
208 if (m->control_pressed) { return results; }
210 double UniqueBL=0.0000; //a branch length is unique if it's chidren are from the same group
211 double totalBL = 0.00; //all branch lengths
212 double UW = 0.00; //Unweighted Value = UniqueBL / totalBL;
214 //find a node that belongs to one of the groups in this combo
215 int nodeBelonging = -1;
216 for (int g = 0; g < namesOfGroupCombos[h].size(); g++) {
217 if (t->groupNodeInfo[namesOfGroupCombos[h][g]].size() != 0) { nodeBelonging = t->groupNodeInfo[namesOfGroupCombos[h][g]][0]; break; }
221 if (nodeBelonging == -1) {
222 m->mothurOut("[WARNING]: cannot find a nodes in the tree from grouping ");
223 for (int g = 0; g < namesOfGroupCombos[h].size()-1; g++) { m->mothurOut(namesOfGroupCombos[h][g] + "-"); }
224 m->mothurOut(namesOfGroupCombos[h][namesOfGroupCombos[h].size()-1]);
225 m->mothurOut(", skipping."); m->mothurOutEndLine(); results[count] = UW;
227 //cout << "trying to get root" << endl;
228 //if including the root this clears rootForGrouping[namesOfGroupCombos[h]]
229 getRoot(t, nodeBelonging, namesOfGroupCombos[h]);
230 //cout << "here" << endl;
231 for(int i=0;i<t->getNumNodes();i++){
233 if (m->control_pressed) { return data; }
235 //pcountSize = 0, they are from a branch that is entirely from a group the user doesn't want
236 //pcountSize = 2, not unique to one group
237 //pcountSize = 1, unique to one group
240 for (int j = 0; j < namesOfGroupCombos[h].size(); j++) {
241 map<string, int>::iterator itGroup = t->tree[i].pcount.find(namesOfGroupCombos[h][j]);
242 if (itGroup != t->tree[i].pcount.end()) { pcountSize++; if (pcountSize > 1) { break; } }
247 if (pcountSize == 0) { }
248 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
249 UniqueBL += abs(t->tree[i].getBranchLength());
253 if (pcountSize == 0) { }
254 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
255 totalBL += abs(t->tree[i].getBranchLength());
258 //cout << UniqueBL << '\t' << totalBL << endl;
259 UW = (UniqueBL / totalBL);
261 if (isnan(UW) || isinf(UW)) { UW = 0; }
271 catch(exception& e) {
272 m->errorOut(e, "Unweighted", "driver");
276 /**************************************************************************************************/
278 EstOutput Unweighted::getValues(Tree* t, string groupA, string groupB, int p, string o) {
283 CountTable* ct = t->getCountTable();
285 //if the users enters no groups then give them the score of all groups
286 int numGroups = m->getNumGroups();
288 //calculate number of comparsions
290 vector< vector<string> > namesOfGroupCombos;
291 for (int r=0; r<numGroups; r++) {
292 for (int l = 0; l < r; l++) {
294 vector<string> groups; groups.push_back((m->getGroups())[r]); groups.push_back((m->getGroups())[l]);
295 namesOfGroupCombos.push_back(groups);
300 vector<string> groups;
301 if (numGroups == 0) {
302 //get score for all users groups
303 for (int i = 0; i < (ct->getNamesOfGroups()).size(); i++) {
304 if ((ct->getNamesOfGroups())[i] != "xxx") {
305 groups.push_back((ct->getNamesOfGroups())[i]);
308 namesOfGroupCombos.push_back(groups);
310 for (int i = 0; i < m->getNumGroups(); i++) {
311 groups.push_back((m->getGroups())[i]);
313 namesOfGroupCombos.push_back(groups);
318 int numPairs = namesOfGroupCombos.size();
319 int numPairsPerProcessor = numPairs / processors;
321 for (int i = 0; i < processors; i++) {
322 int startPos = i * numPairsPerProcessor;
323 if(i == processors - 1){ numPairsPerProcessor = numPairs - i * numPairsPerProcessor; }
324 lines.push_back(linePair(startPos, numPairsPerProcessor));
327 data = createProcesses(t, namesOfGroupCombos, true, ct);
332 catch(exception& e) {
333 m->errorOut(e, "Unweighted", "getValues");
337 /**************************************************************************************************/
339 EstOutput Unweighted::createProcesses(Tree* t, vector< vector<string> > namesOfGroupCombos, bool usingGroups, CountTable* ct) {
342 vector<int> processIDS;
345 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux) || (__linux__) || (__unix__) || (__unix)
347 //loop through and create all the processes you want
348 while (process != processors) {
352 processIDS.push_back(pid); //create map from line number to pid so you can append files in correct order later
356 myresults = driver(t, namesOfGroupCombos, lines[process].start, lines[process].num, usingGroups, ct);
358 if (m->control_pressed) { exit(0); }
360 //pass numSeqs to parent
362 string tempFile = outputDir + toString(getpid()) + ".unweighted.results.temp";
363 m->openOutputFile(tempFile, out);
364 out << myresults.size() << endl;
365 for (int i = 0; i < myresults.size(); i++) { out << myresults[i] << '\t'; } out << endl;
370 m->mothurOut("[ERROR]: unable to spawn the necessary processes."); m->mothurOutEndLine();
371 for (int i = 0; i < processIDS.size(); i++) { kill (processIDS[i], SIGINT); }
376 results = driver(t, namesOfGroupCombos, lines[0].start, lines[0].num, usingGroups, ct);
378 //force parent to wait until all the processes are done
379 for (int i=0;i<(processors-1);i++) {
380 int temp = processIDS[i];
384 if (m->control_pressed) { return results; }
386 //get data created by processes
387 for (int i=0;i<(processors-1);i++) {
389 string s = outputDir + toString(processIDS[i]) + ".unweighted.results.temp";
390 m->openInputFile(s, in);
395 in >> num; m->gobble(in);
397 if (m->control_pressed) { break; }
400 for (int j = 0; j < num; j++) {
403 results.push_back(w);
411 //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.
415 vector<unweightedData*> pDataArray;
416 DWORD dwThreadIdArray[processors-1];
417 HANDLE hThreadArray[processors-1];
418 vector<CountTable*> cts;
421 //Create processor worker threads.
422 for( int i=1; i<processors; i++ ){
423 CountTable* copyCount = new CountTable();
425 Tree* copyTree = new Tree(copyCount);
426 copyTree->getCopy(t);
428 cts.push_back(copyCount);
429 trees.push_back(copyTree);
431 unweightedData* tempweighted = new unweightedData(m, lines[i].start, lines[i].num, namesOfGroupCombos, copyTree, copyCount, includeRoot);
432 pDataArray.push_back(tempweighted);
433 processIDS.push_back(i);
435 hThreadArray[i-1] = CreateThread(NULL, 0, MyUnWeightedRandomThreadFunction, pDataArray[i-1], 0, &dwThreadIdArray[i-1]);
438 results = driver(t, namesOfGroupCombos, lines[0].start, lines[0].num, usingGroups, ct);
440 //Wait until all threads have terminated.
441 WaitForMultipleObjects(processors-1, hThreadArray, TRUE, INFINITE);
443 //Close all thread handles and free memory allocations.
444 for(int i=0; i < pDataArray.size(); i++){
445 for (int j = 0; j < pDataArray[i]->results.size(); j++) { results.push_back(pDataArray[i]->results[j]); }
448 CloseHandle(hThreadArray[i]);
449 delete pDataArray[i];
452 results = driver(t, namesOfGroupCombos, 0, namesOfGroupCombos.size(), usingGroups, ct);
456 catch(exception& e) {
457 m->errorOut(e, "Unweighted", "createProcesses");
461 /**************************************************************************************************/
462 EstOutput Unweighted::driver(Tree* t, vector< vector<string> > namesOfGroupCombos, int start, int num, bool usingGroups, CountTable* ct) {
465 EstOutput results; results.resize(num);
469 Tree* copyTree = new Tree(ct);
471 for (int h = start; h < (start+num); h++) {
473 if (m->control_pressed) { return results; }
475 //copy random tree passed in
476 copyTree->getCopy(t);
478 //swap labels in the groups you want to compare
479 copyTree->assembleRandomUnifracTree(namesOfGroupCombos[h]);
481 double UniqueBL=0.0000; //a branch length is unique if it's chidren are from the same group
482 double totalBL = 0.00; //all branch lengths
483 double UW = 0.00; //Unweighted Value = UniqueBL / totalBL;
484 //find a node that belongs to one of the groups in this combo
485 int nodeBelonging = -1;
486 for (int g = 0; g < namesOfGroupCombos[h].size(); g++) {
487 if (copyTree->groupNodeInfo[namesOfGroupCombos[h][g]].size() != 0) { nodeBelonging = copyTree->groupNodeInfo[namesOfGroupCombos[h][g]][0]; break; }
491 if (nodeBelonging == -1) {
492 m->mothurOut("[WARNING]: cannot find a nodes in the tree from grouping ");
493 for (int g = 0; g < namesOfGroupCombos[h].size()-1; g++) { m->mothurOut(namesOfGroupCombos[h][g] + "-"); }
494 m->mothurOut(namesOfGroupCombos[h][namesOfGroupCombos[h].size()-1]);
495 m->mothurOut(", skipping."); m->mothurOutEndLine(); results[count] = UW;
498 //if including the root this clears rootForGrouping[namesOfGroupCombos[h]]
499 getRoot(copyTree, nodeBelonging, namesOfGroupCombos[h]);
501 for(int i=0;i<copyTree->getNumNodes();i++){
503 if (m->control_pressed) { return data; }
505 //pcountSize = 0, they are from a branch that is entirely from a group the user doesn't want
506 //pcountSize = 2, not unique to one group
507 //pcountSize = 1, unique to one group
510 for (int j = 0; j < namesOfGroupCombos[h].size(); j++) {
511 map<string, int>::iterator itGroup = copyTree->tree[i].pcount.find(namesOfGroupCombos[h][j]);
512 if (itGroup != copyTree->tree[i].pcount.end()) { pcountSize++; if (pcountSize > 1) { break; } }
516 if (pcountSize == 0) { }
517 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
518 UniqueBL += abs(copyTree->tree[i].getBranchLength());
522 if (pcountSize == 0) { }
523 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
524 totalBL += abs(copyTree->tree[i].getBranchLength());
528 //cout << UniqueBL << '\t' << totalBL << endl;
529 UW = (UniqueBL / totalBL);
531 if (isnan(UW) || isinf(UW)) { UW = 0; }
543 catch(exception& e) {
544 m->errorOut(e, "Unweighted", "driver");
548 /**************************************************************************************************/
549 int Unweighted::getRoot(Tree* t, int v, vector<string> grouping) {
551 //you are a leaf so get your parent
552 int index = t->tree[v].getParent();
555 rootForGrouping[grouping].clear();
558 //my parent is a potential root
559 rootForGrouping[grouping].insert(index);
561 //while you aren't at root
562 while(t->tree[index].getParent() != -1){
563 //cout << index << endl;
564 if (m->control_pressed) { return 0; }
566 //am I the root for this grouping? if so I want to stop "early"
567 //does my sibling have descendants from the users groups?
568 //if so I am not the root
569 int parent = t->tree[index].getParent();
570 int lc = t->tree[parent].getLChild();
571 int rc = t->tree[parent].getRChild();
574 if (lc == index) { sib = rc; }
576 map<string, int>::iterator itGroup;
578 for (int j = 0; j < grouping.size(); j++) {
579 map<string, int>::iterator itGroup = t->tree[sib].pcount.find(grouping[j]);
580 if (itGroup != t->tree[sib].pcount.end()) { pcountSize++; if (pcountSize > 1) { break; } }
583 //if yes, I am not the root
584 if (pcountSize != 0) {
585 rootForGrouping[grouping].clear();
586 rootForGrouping[grouping].insert(parent);
592 //get all nodes above the root to add so we don't add their u values above
593 index = *(rootForGrouping[grouping].begin());
594 while(t->tree[index].getParent() != -1){
595 int parent = t->tree[index].getParent();
596 rootForGrouping[grouping].insert(parent);
597 //cout << parent << " in root" << endl;
604 catch(exception& e) {
605 m->errorOut(e, "Unweighted", "getRoot");
609 /**************************************************************************************************/