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 = r+1; l < numGroups; 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);
174 int twentyPercent = (total * 0.20);
175 if (twentyPercent == 0) { twentyPercent = 1; }
177 for (int h = start; h < (start+num); h++) {
179 if (m->control_pressed) { return results; }
181 double UniqueBL=0.0000; //a branch length is unique if it's chidren are from the same group
182 double totalBL = 0.00; //all branch lengths
183 double UW = 0.00; //Unweighted Value = UniqueBL / totalBL;
185 for(int i=0;i<t->getNumNodes();i++){
187 if (m->control_pressed) { return data; }
189 //pcountSize = 0, they are from a branch that is entirely from a group the user doesn't want
190 //pcountSize = 2, not unique to one group
191 //pcountSize = 1, unique to one group
194 for (int j = 0; j < namesOfGroupCombos[h].size(); j++) {
195 map<string, int>::iterator itGroup = t->tree[i].pcount.find(namesOfGroupCombos[h][j]);
196 if (itGroup != t->tree[i].pcount.end()) { pcountSize++; if (pcountSize > 1) { break; } }
199 if (pcountSize == 0) { }
200 else if ((t->tree[i].getBranchLength() != -1) && (pcountSize == 1)) { UniqueBL += abs(t->tree[i].getBranchLength()); }
202 if ((t->tree[i].getBranchLength() != -1) && (pcountSize != 0)) {
203 totalBL += abs(t->tree[i].getBranchLength());
207 UW = (UniqueBL / totalBL);
209 if (isnan(UW) || isinf(UW)) { UW = 0; }
215 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(); }
219 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(); }
223 catch(exception& e) {
224 m->errorOut(e, "Unweighted", "driver");
228 /**************************************************************************************************/
230 EstOutput Unweighted::getValues(Tree* t, string groupA, string groupB, int p, string o) {
232 globaldata = GlobalData::getInstance();
237 //if the users enters no groups then give them the score of all groups
238 int numGroups = globaldata->Groups.size();
240 //calculate number of comparsions
242 vector< vector<string> > namesOfGroupCombos;
243 for (int r=0; r<numGroups; r++) {
244 for (int l = r+1; l < numGroups; l++) {
246 vector<string> groups; groups.push_back(globaldata->Groups[r]); groups.push_back(globaldata->Groups[l]);
247 namesOfGroupCombos.push_back(groups);
252 vector<string> groups;
253 if (numGroups == 0) {
254 //get score for all users groups
255 for (int i = 0; i < tmap->namesOfGroups.size(); i++) {
256 if (tmap->namesOfGroups[i] != "xxx") {
257 groups.push_back(tmap->namesOfGroups[i]);
260 namesOfGroupCombos.push_back(groups);
262 for (int i = 0; i < globaldata->Groups.size(); i++) {
263 groups.push_back(globaldata->Groups[i]);
265 namesOfGroupCombos.push_back(groups);
269 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
271 data = driver(t, namesOfGroupCombos, 0, namesOfGroupCombos.size(), true);
273 int numPairs = namesOfGroupCombos.size();
275 int numPairsPerProcessor = numPairs / processors;
277 for (int i = 0; i < processors; i++) {
278 int startPos = i * numPairsPerProcessor;
279 if(i == processors - 1){
280 numPairsPerProcessor = numPairs - i * numPairsPerProcessor;
282 lines.push_back(linePair(startPos, numPairsPerProcessor));
285 data = createProcesses(t, namesOfGroupCombos, true);
290 data = driver(t, namesOfGroupCombos, 0, namesOfGroupCombos.size(), true);
295 catch(exception& e) {
296 m->errorOut(e, "Unweighted", "getValues");
300 /**************************************************************************************************/
302 EstOutput Unweighted::createProcesses(Tree* t, vector< vector<string> > namesOfGroupCombos, bool usingGroups) {
304 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
307 vector<int> processIDS;
311 //loop through and create all the processes you want
312 while (process != processors) {
316 processIDS.push_back(pid); //create map from line number to pid so you can append files in correct order later
320 myresults = driver(t, namesOfGroupCombos, lines[process].start, lines[process].num, usingGroups);
322 if (m->control_pressed) { exit(0); }
324 //pass numSeqs to parent
326 string tempFile = outputDir + toString(getpid()) + ".unweighted.results.temp";
327 m->openOutputFile(tempFile, out);
328 out << myresults.size() << endl;
329 for (int i = 0; i < myresults.size(); i++) { out << myresults[i] << '\t'; } out << endl;
333 }else { m->mothurOut("unable to spawn the necessary processes."); m->mothurOutEndLine(); exit(0); }
336 results = driver(t, namesOfGroupCombos, lines[0].start, lines[0].num, usingGroups);
338 //force parent to wait until all the processes are done
339 for (int i=0;i<(processors-1);i++) {
340 int temp = processIDS[i];
344 if (m->control_pressed) { return results; }
346 //get data created by processes
347 for (int i=0;i<(processors-1);i++) {
349 string s = outputDir + toString(processIDS[i]) + ".unweighted.results.temp";
350 m->openInputFile(s, in);
355 in >> num; m->gobble(in);
357 if (m->control_pressed) { break; }
360 for (int j = 0; j < num; j++) {
362 results.push_back(w);
373 catch(exception& e) {
374 m->errorOut(e, "Unweighted", "createProcesses");
378 /**************************************************************************************************/
379 EstOutput Unweighted::driver(Tree* t, vector< vector<string> > namesOfGroupCombos, int start, int num, bool usingGroups) {
382 EstOutput results; results.resize(num);
386 int twentyPercent = (total * 0.20);
388 Tree* copyTree = new Tree;
390 for (int h = start; h < (start+num); h++) {
392 if (m->control_pressed) { return results; }
394 //copy random tree passed in
395 copyTree->getCopy(t);
397 //swap labels in the groups you want to compare
398 copyTree->assembleRandomUnifracTree(namesOfGroupCombos[h]);
400 double UniqueBL=0.0000; //a branch length is unique if it's chidren are from the same group
401 double totalBL = 0.00; //all branch lengths
402 double UW = 0.00; //Unweighted Value = UniqueBL / totalBL;
404 for(int i=0;i<t->getNumNodes();i++){
406 if (m->control_pressed) { return data; }
408 //pcountSize = 0, they are from a branch that is entirely from a group the user doesn't want
409 //pcountSize = 2, not unique to one group
410 //pcountSize = 1, unique to one group
413 for (int j = 0; j < namesOfGroupCombos[h].size(); j++) {
414 map<string, int>::iterator itGroup = t->tree[i].pcount.find(namesOfGroupCombos[h][j]);
415 if (itGroup != t->tree[i].pcount.end()) { pcountSize++; if (pcountSize > 1) { break; } }
418 if (pcountSize == 0) { }
419 else if ((t->tree[i].getBranchLength() != -1) && (pcountSize == 1)) { UniqueBL += abs(t->tree[i].getBranchLength()); }
421 if ((t->tree[i].getBranchLength() != -1) && (pcountSize != 0)) {
422 totalBL += abs(t->tree[i].getBranchLength());
426 UW = (UniqueBL / totalBL);
428 if (isnan(UW) || isinf(UW)) { UW = 0; }
439 catch(exception& e) {
440 m->errorOut(e, "Unweighted", "driver");
444 /**************************************************************************************************/