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));
70 data = createProcesses(t, namesOfGroupCombos);
74 data = driver(t, namesOfGroupCombos, 0, namesOfGroupCombos.size());
80 m->errorOut(e, "Unweighted", "getValues");
84 /**************************************************************************************************/
86 EstOutput Unweighted::createProcesses(Tree* t, vector< vector<string> > namesOfGroupCombos) {
88 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
91 vector<int> processIDS;
95 //loop through and create all the processes you want
96 while (process != processors) {
100 processIDS.push_back(pid); //create map from line number to pid so you can append files in correct order later
104 myresults = driver(t, namesOfGroupCombos, lines[process].start, lines[process].num);
106 if (m->control_pressed) { exit(0); }
108 m->mothurOut("Merging results."); m->mothurOutEndLine();
110 //pass numSeqs to parent
112 string tempFile = outputDir + toString(getpid()) + ".unweighted.results.temp";
113 m->openOutputFile(tempFile, out);
114 out << myresults.size() << endl;
115 for (int i = 0; i < myresults.size(); i++) { out << myresults[i] << '\t'; } out << endl;
119 }else { m->mothurOut("unable to spawn the necessary processes."); m->mothurOutEndLine(); exit(0); }
122 results = driver(t, namesOfGroupCombos, lines[0].start, lines[0].num);
124 //force parent to wait until all the processes are done
125 for (int i=0;i<(processors-1);i++) {
126 int temp = processIDS[i];
130 if (m->control_pressed) { return results; }
132 //get data created by processes
133 for (int i=0;i<(processors-1);i++) {
135 string s = outputDir + toString(processIDS[i]) + ".unweighted.results.temp";
136 m->openInputFile(s, in);
141 in >> num; m->gobble(in);
143 if (m->control_pressed) { break; }
146 for (int j = 0; j < num; j++) {
148 results.push_back(w);
156 m->mothurOut("DONE."); m->mothurOutEndLine(); m->mothurOutEndLine();
161 catch(exception& e) {
162 m->errorOut(e, "Unweighted", "createProcesses");
166 /**************************************************************************************************/
167 EstOutput Unweighted::driver(Tree* t, vector< vector<string> > namesOfGroupCombos, int start, int num) {
170 EstOutput results; results.resize(num);
173 int numLeaves = t->getNumLeaves();
175 int twentyPercent = (total * 0.20);
176 if (twentyPercent == 0) { twentyPercent = 1; }
179 for (int h = start; h < (start+num); h++) {
181 if (m->control_pressed) { return results; }
183 double UniqueBL=0.0000; //a branch length is unique if it's chidren are from the same group
184 double totalBL = 0.00; //all branch lengths
185 double UW = 0.00; //Unweighted Value = UniqueBL / totalBL;
186 map<int, double> tempTotals; //maps node to total Branch Length
187 map<int, int> nodePcountSize; //maps node to pcountSize
188 map<int, int>::iterator itCount;
190 for(int i=0;i<t->getNumNodes();i++){
192 if (m->control_pressed) { return data; }
194 //pcountSize = 0, they are from a branch that is entirely from a group the user doesn't want
195 //pcountSize = 2, not unique to one group
196 //pcountSize = 1, unique to one group
199 for (int j = 0; j < namesOfGroupCombos[h].size(); j++) {
200 map<string, int>::iterator itGroup = t->tree[i].pcount.find(namesOfGroupCombos[h][j]);
201 if (itGroup != t->tree[i].pcount.end()) { pcountSize++; if (pcountSize > 1) { break; } }
204 nodePcountSize[i] = pcountSize;
206 if (pcountSize == 0) { ; }
207 else if ((t->tree[i].getBranchLength() != -1) && (pcountSize == 1)) { UniqueBL += abs(t->tree[i].getBranchLength()); }
209 //if you are a leaf from a users group add to total
211 if ((t->tree[i].getBranchLength() != -1) && pcountSize != 0) {
212 totalBL += abs(t->tree[i].getBranchLength());
214 tempTotals[i] = 0.0; //we don't care about you, or we have already added you
215 }else{ //if you are not a leaf
216 //do both your chidren have have descendants from the users groups?
217 int lc = t->tree[i].getLChild();
218 int rc = t->tree[i].getRChild();
220 //if yes, add your childrens tempTotals
221 if ((nodePcountSize[lc] != 0) && (nodePcountSize[rc] != 0)) {
222 totalBL += tempTotals[lc] + tempTotals[rc];
223 if (t->tree[i].getBranchLength() != -1) {
224 tempTotals[i] = abs(t->tree[i].getBranchLength());
228 }else { //if no, your tempTotal is your childrens temp totals + your branch length
229 tempTotals[i] = tempTotals[lc] + tempTotals[rc] + abs(t->tree[i].getBranchLength());
233 cout << UniqueBL << '\t' << totalBL << endl;
234 UW = (UniqueBL / totalBL);
236 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)
334 vector<int> processIDS;
338 //loop through and create all the processes you want
339 while (process != processors) {
343 processIDS.push_back(pid); //create map from line number to pid so you can append files in correct order later
347 myresults = driver(t, namesOfGroupCombos, lines[process].start, lines[process].num, usingGroups);
349 if (m->control_pressed) { exit(0); }
351 //pass numSeqs to parent
353 string tempFile = outputDir + toString(getpid()) + ".unweighted.results.temp";
354 m->openOutputFile(tempFile, out);
355 out << myresults.size() << endl;
356 for (int i = 0; i < myresults.size(); i++) { out << myresults[i] << '\t'; } out << endl;
360 }else { m->mothurOut("unable to spawn the necessary processes."); m->mothurOutEndLine(); exit(0); }
363 results = driver(t, namesOfGroupCombos, lines[0].start, lines[0].num, usingGroups);
365 //force parent to wait until all the processes are done
366 for (int i=0;i<(processors-1);i++) {
367 int temp = processIDS[i];
371 if (m->control_pressed) { return results; }
373 //get data created by processes
374 for (int i=0;i<(processors-1);i++) {
376 string s = outputDir + toString(processIDS[i]) + ".unweighted.results.temp";
377 m->openInputFile(s, in);
382 in >> num; m->gobble(in);
384 if (m->control_pressed) { break; }
387 for (int j = 0; j < num; j++) {
389 results.push_back(w);
400 catch(exception& e) {
401 m->errorOut(e, "Unweighted", "createProcesses");
405 /**************************************************************************************************/
406 EstOutput Unweighted::driver(Tree* t, vector< vector<string> > namesOfGroupCombos, int start, int num, bool usingGroups) {
409 EstOutput results; results.resize(num);
413 int twentyPercent = (total * 0.20);
414 int numLeaves = t->getNumLeaves();
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 map<int, double> tempTotals; //maps node to total Branch Length
432 map<int, int> nodePcountSize; //maps node to pcountSize
434 for(int i=0;i<copyTree->getNumNodes();i++){
436 if (m->control_pressed) { return data; }
438 //pcountSize = 0, they are from a branch that is entirely from a group the user doesn't want
439 //pcountSize = 2, not unique to one group
440 //pcountSize = 1, unique to one group
443 for (int j = 0; j < namesOfGroupCombos[h].size(); j++) {
444 map<string, int>::iterator itGroup = copyTree->tree[i].pcount.find(namesOfGroupCombos[h][j]);
445 if (itGroup != copyTree->tree[i].pcount.end()) { pcountSize++; if (pcountSize > 1) { break; } }
448 nodePcountSize[i] = pcountSize;
450 if (pcountSize == 0) { }
451 else if ((copyTree->tree[i].getBranchLength() != -1) && (pcountSize == 1)) { UniqueBL += abs(copyTree->tree[i].getBranchLength()); }
453 //if you are a leaf from a users group add to total
455 if ((copyTree->tree[i].getBranchLength() != -1) && pcountSize != 0) {
456 totalBL += abs(copyTree->tree[i].getBranchLength());
458 tempTotals[i] = 0.0; //we don't care about you, or we have already added you
459 }else{ //if you are not a leaf
460 //do both your chidren have have descendants from the users groups?
461 int lc = copyTree->tree[i].getLChild();
462 int rc = copyTree->tree[i].getRChild();
464 //if yes, add your childrens tempTotals
465 if ((nodePcountSize[lc] != 0) && (nodePcountSize[rc] != 0)) {
466 totalBL += tempTotals[lc] + tempTotals[rc];
467 if (copyTree->tree[i].getBranchLength() != -1) {
468 tempTotals[i] = abs(copyTree->tree[i].getBranchLength());
472 }else { //if no, your tempTotal is your childrens temp totals + your branch length
473 tempTotals[i] = tempTotals[lc] + tempTotals[rc] + abs(copyTree->tree[i].getBranchLength());
479 UW = (UniqueBL / totalBL);
481 if (isnan(UW) || isinf(UW)) { UW = 0; }
492 catch(exception& e) {
493 m->errorOut(e, "Unweighted", "driver");
497 /**************************************************************************************************/