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++) {
180 //cout << namesOfGroupCombos[h][0] << '\t' << namesOfGroupCombos[h][1] << endl;
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 //cout << i << '\t' << t->tree[i].getName() << " br = " << abs(t->tree[i].getBranchLength()) << '\t';
207 if (pcountSize == 0) { }
208 else if ((t->tree[i].getBranchLength() != -1) && (pcountSize == 1)) { UniqueBL += abs(t->tree[i].getBranchLength()); }
210 //if you are a leaf from a users group add to total
212 if ((t->tree[i].getBranchLength() != -1) && pcountSize != 0) {
213 //cout << "added to total" << endl;
214 totalBL += abs(t->tree[i].getBranchLength());
216 tempTotals[i] = 0.0; //we don't care about you, or we have already added you
217 }else{ //if you are not a leaf
218 //do both your chidren have have descendants from the users groups?
219 int lc = t->tree[i].getLChild();
220 int rc = t->tree[i].getRChild();
222 //if yes, add your childrens tempTotals
223 if ((nodePcountSize[lc] != 0) && (nodePcountSize[rc] != 0)) {
224 totalBL += tempTotals[lc] + tempTotals[rc];
225 //cout << "added to total " << tempTotals[lc] << '\t' << tempTotals[rc] << endl;
226 if (t->tree[i].getBranchLength() != -1) {
227 tempTotals[i] = abs(t->tree[i].getBranchLength());
231 }else if ((nodePcountSize[lc] == 0) && (nodePcountSize[rc] == 0)) { tempTotals[i] = 0.0; //we don't care about you
232 }else { //if no, your tempTotal is your childrens temp totals + your branch length
233 tempTotals[i] = tempTotals[lc] + tempTotals[rc] + abs(t->tree[i].getBranchLength());
235 //cout << "temptotal = "<< tempTotals[i] << endl;
239 //cout << UniqueBL << '\t' << totalBL << endl;
240 UW = (UniqueBL / totalBL);
242 if (isnan(UW) || isinf(UW)) { UW = 0; }
248 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(); }
252 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(); }
256 catch(exception& e) {
257 m->errorOut(e, "Unweighted", "driver");
261 /**************************************************************************************************/
263 EstOutput Unweighted::getValues(Tree* t, string groupA, string groupB, int p, string o) {
265 globaldata = GlobalData::getInstance();
270 //if the users enters no groups then give them the score of all groups
271 int numGroups = globaldata->Groups.size();
273 //calculate number of comparsions
275 vector< vector<string> > namesOfGroupCombos;
276 for (int r=0; r<numGroups; r++) {
277 for (int l = 0; l < r; l++) {
279 vector<string> groups; groups.push_back(globaldata->Groups[r]); groups.push_back(globaldata->Groups[l]);
280 namesOfGroupCombos.push_back(groups);
285 vector<string> groups;
286 if (numGroups == 0) {
287 //get score for all users groups
288 for (int i = 0; i < tmap->namesOfGroups.size(); i++) {
289 if (tmap->namesOfGroups[i] != "xxx") {
290 groups.push_back(tmap->namesOfGroups[i]);
293 namesOfGroupCombos.push_back(groups);
295 for (int i = 0; i < globaldata->Groups.size(); i++) {
296 groups.push_back(globaldata->Groups[i]);
298 namesOfGroupCombos.push_back(groups);
302 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
304 data = driver(t, namesOfGroupCombos, 0, namesOfGroupCombos.size(), true);
306 int numPairs = namesOfGroupCombos.size();
308 int numPairsPerProcessor = numPairs / processors;
310 for (int i = 0; i < processors; i++) {
311 int startPos = i * numPairsPerProcessor;
312 if(i == processors - 1){
313 numPairsPerProcessor = numPairs - i * numPairsPerProcessor;
315 lines.push_back(linePair(startPos, numPairsPerProcessor));
318 data = createProcesses(t, namesOfGroupCombos, true);
323 data = driver(t, namesOfGroupCombos, 0, namesOfGroupCombos.size(), true);
328 catch(exception& e) {
329 m->errorOut(e, "Unweighted", "getValues");
333 /**************************************************************************************************/
335 EstOutput Unweighted::createProcesses(Tree* t, vector< vector<string> > namesOfGroupCombos, bool usingGroups) {
337 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
340 vector<int> processIDS;
344 //loop through and create all the processes you want
345 while (process != processors) {
349 processIDS.push_back(pid); //create map from line number to pid so you can append files in correct order later
353 myresults = driver(t, namesOfGroupCombos, lines[process].start, lines[process].num, usingGroups);
355 if (m->control_pressed) { exit(0); }
357 //pass numSeqs to parent
359 string tempFile = outputDir + toString(getpid()) + ".unweighted.results.temp";
360 m->openOutputFile(tempFile, out);
361 out << myresults.size() << endl;
362 for (int i = 0; i < myresults.size(); i++) { out << myresults[i] << '\t'; } out << endl;
366 }else { m->mothurOut("unable to spawn the necessary processes."); m->mothurOutEndLine(); exit(0); }
369 results = driver(t, namesOfGroupCombos, lines[0].start, lines[0].num, usingGroups);
371 //force parent to wait until all the processes are done
372 for (int i=0;i<(processors-1);i++) {
373 int temp = processIDS[i];
377 if (m->control_pressed) { return results; }
379 //get data created by processes
380 for (int i=0;i<(processors-1);i++) {
382 string s = outputDir + toString(processIDS[i]) + ".unweighted.results.temp";
383 m->openInputFile(s, in);
388 in >> num; m->gobble(in);
390 if (m->control_pressed) { break; }
393 for (int j = 0; j < num; j++) {
395 results.push_back(w);
406 catch(exception& e) {
407 m->errorOut(e, "Unweighted", "createProcesses");
411 /**************************************************************************************************/
412 EstOutput Unweighted::driver(Tree* t, vector< vector<string> > namesOfGroupCombos, int start, int num, bool usingGroups) {
415 EstOutput results; results.resize(num);
419 int twentyPercent = (total * 0.20);
420 int numLeaves = t->getNumLeaves();
422 Tree* copyTree = new Tree;
424 for (int h = start; h < (start+num); h++) {
426 if (m->control_pressed) { return results; }
428 //copy random tree passed in
429 copyTree->getCopy(t);
431 //swap labels in the groups you want to compare
432 copyTree->assembleRandomUnifracTree(namesOfGroupCombos[h]);
434 double UniqueBL=0.0000; //a branch length is unique if it's chidren are from the same group
435 double totalBL = 0.00; //all branch lengths
436 double UW = 0.00; //Unweighted Value = UniqueBL / totalBL;
437 map<int, double> tempTotals; //maps node to total Branch Length
438 map<int, int> nodePcountSize; //maps node to pcountSize
440 for(int i=0;i<copyTree->getNumNodes();i++){
442 if (m->control_pressed) { return data; }
444 //pcountSize = 0, they are from a branch that is entirely from a group the user doesn't want
445 //pcountSize = 2, not unique to one group
446 //pcountSize = 1, unique to one group
449 for (int j = 0; j < namesOfGroupCombos[h].size(); j++) {
450 map<string, int>::iterator itGroup = copyTree->tree[i].pcount.find(namesOfGroupCombos[h][j]);
451 if (itGroup != copyTree->tree[i].pcount.end()) { pcountSize++; if (pcountSize > 1) { break; } }
454 nodePcountSize[i] = pcountSize;
456 if (pcountSize == 0) { }
457 else if ((copyTree->tree[i].getBranchLength() != -1) && (pcountSize == 1)) { UniqueBL += abs(copyTree->tree[i].getBranchLength()); }
459 //if you are a leaf from a users group add to total
461 if ((copyTree->tree[i].getBranchLength() != -1) && pcountSize != 0) {
462 totalBL += abs(copyTree->tree[i].getBranchLength());
464 tempTotals[i] = 0.0; //we don't care about you, or we have already added you
465 }else{ //if you are not a leaf
466 //do both your chidren have have descendants from the users groups?
467 int lc = copyTree->tree[i].getLChild();
468 int rc = copyTree->tree[i].getRChild();
470 //if yes, add your childrens tempTotals
471 if ((nodePcountSize[lc] != 0) && (nodePcountSize[rc] != 0)) {
472 totalBL += tempTotals[lc] + tempTotals[rc];
474 if (copyTree->tree[i].getBranchLength() != -1) {
475 tempTotals[i] = abs(copyTree->tree[i].getBranchLength());
479 }else if ((nodePcountSize[lc] == 0) && (nodePcountSize[rc] == 0)) { tempTotals[i] = 0.0; //we don't care about you
480 }else { //if no, your tempTotal is your childrens temp totals + your branch length
481 tempTotals[i] = tempTotals[lc] + tempTotals[rc] + abs(copyTree->tree[i].getBranchLength());
488 UW = (UniqueBL / totalBL);
490 if (isnan(UW) || isinf(UW)) { UW = 0; }
501 catch(exception& e) {
502 m->errorOut(e, "Unweighted", "driver");
506 /**************************************************************************************************/