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)
90 vector<int> processIDS;
94 //loop through and create all the processes you want
95 while (process != processors) {
99 processIDS.push_back(pid); //create map from line number to pid so you can append files in correct order later
103 myresults = driver(t, namesOfGroupCombos, lines[process].start, lines[process].num);
105 if (m->control_pressed) { exit(0); }
107 m->mothurOut("Merging results."); m->mothurOutEndLine();
109 //pass numSeqs to parent
111 string tempFile = outputDir + toString(getpid()) + ".unweighted.results.temp";
112 m->openOutputFile(tempFile, out);
113 out << myresults.size() << endl;
114 for (int i = 0; i < myresults.size(); i++) { out << myresults[i] << '\t'; } out << endl;
119 m->mothurOut("[ERROR]: unable to spawn the necessary processes."); m->mothurOutEndLine();
120 for (int i = 0; i < processIDS.size(); i++) { kill (processIDS[i], SIGINT); }
125 results = driver(t, namesOfGroupCombos, lines[0].start, lines[0].num);
127 //force parent to wait until all the processes are done
128 for (int i=0;i<(processors-1);i++) {
129 int temp = processIDS[i];
133 if (m->control_pressed) { return results; }
135 //get data created by processes
136 for (int i=0;i<(processors-1);i++) {
138 string s = outputDir + toString(processIDS[i]) + ".unweighted.results.temp";
139 m->openInputFile(s, in);
144 in >> num; m->gobble(in);
146 if (m->control_pressed) { break; }
149 for (int j = 0; j < num; j++) {
151 results.push_back(w);
159 m->mothurOut("DONE."); m->mothurOutEndLine(); m->mothurOutEndLine();
164 catch(exception& e) {
165 m->errorOut(e, "Unweighted", "createProcesses");
169 /**************************************************************************************************/
170 EstOutput Unweighted::driver(Tree* t, vector< vector<string> > namesOfGroupCombos, int start, int num) {
173 EstOutput results; results.resize(num);
176 int numLeaves = t->getNumLeaves();
178 int twentyPercent = (total * 0.20);
179 if (twentyPercent == 0) { twentyPercent = 1; }
182 for (int h = start; h < (start+num); h++) {
183 //cout << namesOfGroupCombos[h][0] << '\t' << namesOfGroupCombos[h][1] << endl;
184 if (m->control_pressed) { return results; }
186 double UniqueBL=0.0000; //a branch length is unique if it's chidren are from the same group
187 double totalBL = 0.00; //all branch lengths
188 double UW = 0.00; //Unweighted Value = UniqueBL / totalBL;
189 map<int, double> tempTotals; //maps node to total Branch Length
190 map<int, int> nodePcountSize; //maps node to pcountSize
191 map<int, int>::iterator itCount;
193 for(int i=0;i<t->getNumNodes();i++){
195 if (m->control_pressed) { return data; }
197 //pcountSize = 0, they are from a branch that is entirely from a group the user doesn't want
198 //pcountSize = 2, not unique to one group
199 //pcountSize = 1, unique to one group
202 for (int j = 0; j < namesOfGroupCombos[h].size(); j++) {
203 map<string, int>::iterator itGroup = t->tree[i].pcount.find(namesOfGroupCombos[h][j]);
204 if (itGroup != t->tree[i].pcount.end()) { pcountSize++; if (pcountSize > 1) { break; } }
207 nodePcountSize[i] = pcountSize;
209 //cout << i << '\t' << t->tree[i].getName() << " br = " << abs(t->tree[i].getBranchLength()) << '\t';
210 if (pcountSize == 0) { }
211 else if ((t->tree[i].getBranchLength() != -1) && (pcountSize == 1)) { UniqueBL += abs(t->tree[i].getBranchLength()); }
213 //if you are a leaf from a users group add to total
215 if ((t->tree[i].getBranchLength() != -1) && pcountSize != 0) {
216 //cout << "added to total" << endl;
217 totalBL += abs(t->tree[i].getBranchLength());
219 tempTotals[i] = 0.0; //we don't care about you, or we have already added you
220 }else{ //if you are not a leaf
221 //do both your chidren have have descendants from the users groups?
222 int lc = t->tree[i].getLChild();
223 int rc = t->tree[i].getRChild();
225 //if yes, add your childrens tempTotals
226 if ((nodePcountSize[lc] != 0) && (nodePcountSize[rc] != 0)) {
227 totalBL += tempTotals[lc] + tempTotals[rc];
228 //cout << "added to total " << tempTotals[lc] << '\t' << tempTotals[rc] << endl;
229 if (t->tree[i].getBranchLength() != -1) {
230 tempTotals[i] = abs(t->tree[i].getBranchLength());
234 }else if ((nodePcountSize[lc] == 0) && (nodePcountSize[rc] == 0)) { tempTotals[i] = 0.0; //we don't care about you
235 }else { //if no, your tempTotal is your childrens temp totals + your branch length
236 tempTotals[i] = tempTotals[lc] + tempTotals[rc] + abs(t->tree[i].getBranchLength());
238 //cout << "temptotal = "<< tempTotals[i] << endl;
242 //cout << UniqueBL << '\t' << totalBL << endl;
243 UW = (UniqueBL / totalBL);
245 if (isnan(UW) || isinf(UW)) { UW = 0; }
251 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(); }
255 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(); }
259 catch(exception& e) {
260 m->errorOut(e, "Unweighted", "driver");
264 /**************************************************************************************************/
266 EstOutput Unweighted::getValues(Tree* t, string groupA, string groupB, int p, string o) {
268 globaldata = GlobalData::getInstance();
273 //if the users enters no groups then give them the score of all groups
274 int numGroups = globaldata->Groups.size();
276 //calculate number of comparsions
278 vector< vector<string> > namesOfGroupCombos;
279 for (int r=0; r<numGroups; r++) {
280 for (int l = 0; l < r; l++) {
282 vector<string> groups; groups.push_back(globaldata->Groups[r]); groups.push_back(globaldata->Groups[l]);
283 namesOfGroupCombos.push_back(groups);
288 vector<string> groups;
289 if (numGroups == 0) {
290 //get score for all users groups
291 for (int i = 0; i < tmap->namesOfGroups.size(); i++) {
292 if (tmap->namesOfGroups[i] != "xxx") {
293 groups.push_back(tmap->namesOfGroups[i]);
296 namesOfGroupCombos.push_back(groups);
298 for (int i = 0; i < globaldata->Groups.size(); i++) {
299 groups.push_back(globaldata->Groups[i]);
301 namesOfGroupCombos.push_back(groups);
305 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
307 data = driver(t, namesOfGroupCombos, 0, namesOfGroupCombos.size(), true);
309 int numPairs = namesOfGroupCombos.size();
311 int numPairsPerProcessor = numPairs / processors;
313 for (int i = 0; i < processors; i++) {
314 int startPos = i * numPairsPerProcessor;
315 if(i == processors - 1){
316 numPairsPerProcessor = numPairs - i * numPairsPerProcessor;
318 lines.push_back(linePair(startPos, numPairsPerProcessor));
321 data = createProcesses(t, namesOfGroupCombos, true);
326 data = driver(t, namesOfGroupCombos, 0, namesOfGroupCombos.size(), true);
331 catch(exception& e) {
332 m->errorOut(e, "Unweighted", "getValues");
336 /**************************************************************************************************/
338 EstOutput Unweighted::createProcesses(Tree* t, vector< vector<string> > namesOfGroupCombos, bool usingGroups) {
340 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
342 vector<int> processIDS;
346 //loop through and create all the processes you want
347 while (process != processors) {
351 processIDS.push_back(pid); //create map from line number to pid so you can append files in correct order later
355 myresults = driver(t, namesOfGroupCombos, lines[process].start, lines[process].num, usingGroups);
357 if (m->control_pressed) { exit(0); }
359 //pass numSeqs to parent
361 string tempFile = outputDir + toString(getpid()) + ".unweighted.results.temp";
362 m->openOutputFile(tempFile, out);
363 out << myresults.size() << endl;
364 for (int i = 0; i < myresults.size(); i++) { out << myresults[i] << '\t'; } out << endl;
369 m->mothurOut("[ERROR]: unable to spawn the necessary processes."); m->mothurOutEndLine();
370 for (int i = 0; i < processIDS.size(); i++) { kill (processIDS[i], SIGINT); }
375 results = driver(t, namesOfGroupCombos, lines[0].start, lines[0].num, usingGroups);
377 //force parent to wait until all the processes are done
378 for (int i=0;i<(processors-1);i++) {
379 int temp = processIDS[i];
383 if (m->control_pressed) { return results; }
385 //get data created by processes
386 for (int i=0;i<(processors-1);i++) {
388 string s = outputDir + toString(processIDS[i]) + ".unweighted.results.temp";
389 m->openInputFile(s, in);
394 in >> num; m->gobble(in);
396 if (m->control_pressed) { break; }
399 for (int j = 0; j < num; j++) {
401 results.push_back(w);
412 catch(exception& e) {
413 m->errorOut(e, "Unweighted", "createProcesses");
417 /**************************************************************************************************/
418 EstOutput Unweighted::driver(Tree* t, vector< vector<string> > namesOfGroupCombos, int start, int num, bool usingGroups) {
421 EstOutput results; results.resize(num);
424 int numLeaves = t->getNumLeaves();
426 Tree* copyTree = new Tree;
428 for (int h = start; h < (start+num); h++) {
430 if (m->control_pressed) { return results; }
432 //copy random tree passed in
433 copyTree->getCopy(t);
435 //swap labels in the groups you want to compare
436 copyTree->assembleRandomUnifracTree(namesOfGroupCombos[h]);
438 double UniqueBL=0.0000; //a branch length is unique if it's chidren are from the same group
439 double totalBL = 0.00; //all branch lengths
440 double UW = 0.00; //Unweighted Value = UniqueBL / totalBL;
441 map<int, double> tempTotals; //maps node to total Branch Length
442 map<int, int> nodePcountSize; //maps node to pcountSize
444 for(int i=0;i<copyTree->getNumNodes();i++){
446 if (m->control_pressed) { return data; }
448 //pcountSize = 0, they are from a branch that is entirely from a group the user doesn't want
449 //pcountSize = 2, not unique to one group
450 //pcountSize = 1, unique to one group
453 for (int j = 0; j < namesOfGroupCombos[h].size(); j++) {
454 map<string, int>::iterator itGroup = copyTree->tree[i].pcount.find(namesOfGroupCombos[h][j]);
455 if (itGroup != copyTree->tree[i].pcount.end()) { pcountSize++; if (pcountSize > 1) { break; } }
458 nodePcountSize[i] = pcountSize;
460 if (pcountSize == 0) { }
461 else if ((copyTree->tree[i].getBranchLength() != -1) && (pcountSize == 1)) { UniqueBL += abs(copyTree->tree[i].getBranchLength()); }
463 //if you are a leaf from a users group add to total
465 if ((copyTree->tree[i].getBranchLength() != -1) && pcountSize != 0) {
466 totalBL += abs(copyTree->tree[i].getBranchLength());
468 tempTotals[i] = 0.0; //we don't care about you, or we have already added you
469 }else{ //if you are not a leaf
470 //do both your chidren have have descendants from the users groups?
471 int lc = copyTree->tree[i].getLChild();
472 int rc = copyTree->tree[i].getRChild();
474 //if yes, add your childrens tempTotals
475 if ((nodePcountSize[lc] != 0) && (nodePcountSize[rc] != 0)) {
476 totalBL += tempTotals[lc] + tempTotals[rc];
478 if (copyTree->tree[i].getBranchLength() != -1) {
479 tempTotals[i] = abs(copyTree->tree[i].getBranchLength());
483 }else if ((nodePcountSize[lc] == 0) && (nodePcountSize[rc] == 0)) { tempTotals[i] = 0.0; //we don't care about you
484 }else { //if no, your tempTotal is your childrens temp totals + your branch length
485 tempTotals[i] = tempTotals[lc] + tempTotals[rc] + abs(copyTree->tree[i].getBranchLength());
492 UW = (UniqueBL / totalBL);
494 if (isnan(UW) || isinf(UW)) { UW = 0; }
505 catch(exception& e) {
506 m->errorOut(e, "Unweighted", "driver");
510 /**************************************************************************************************/