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;
62 if(i == processors - 1){
63 numPairsPerProcessor = numPairs - i * numPairsPerProcessor;
65 lines.push_back(new linePair(startPos, numPairsPerProcessor));
68 data = createProcesses(t, namesOfGroupCombos);
70 for (int i = 0; i < lines.size(); i++) { delete lines[i]; } lines.clear();
73 data = driver(t, namesOfGroupCombos, 0, namesOfGroupCombos.size());
79 m->errorOut(e, "Unweighted", "getValues");
83 /**************************************************************************************************/
85 EstOutput Unweighted::createProcesses(Tree* t, vector< vector<string> > namesOfGroupCombos) {
87 #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;
118 }else { m->mothurOut("unable to spawn the necessary processes."); m->mothurOutEndLine(); exit(0); }
121 results = driver(t, namesOfGroupCombos, lines[0]->start, lines[0]->num);
123 //force parent to wait until all the processes are done
124 for (int i=0;i<(processors-1);i++) {
125 int temp = processIDS[i];
129 if (m->control_pressed) { return results; }
131 //get data created by processes
132 for (int i=0;i<(processors-1);i++) {
134 string s = outputDir + toString(processIDS[i]) + ".unweighted.results.temp";
135 m->openInputFile(s, in);
140 in >> num; m->gobble(in);
142 if (m->control_pressed) { break; }
145 for (int j = 0; j < num; j++) {
147 results.push_back(w);
155 m->mothurOut("DONE."); m->mothurOutEndLine(); m->mothurOutEndLine();
160 catch(exception& e) {
161 m->errorOut(e, "Unweighted", "createProcesses");
165 /**************************************************************************************************/
166 EstOutput Unweighted::driver(Tree* t, vector< vector<string> > namesOfGroupCombos, int start, int num) {
169 EstOutput results; results.resize(num);
173 int twentyPercent = (total * 0.20);
175 for (int h = start; h < (start+num); h++) {
177 if (m->control_pressed) { return results; }
179 double UniqueBL=0.0000; //a branch length is unique if it's chidren are from the same group
180 double totalBL = 0.00; //all branch lengths
181 double UW = 0.00; //Unweighted Value = UniqueBL / totalBL;
183 for(int i=0;i<t->getNumNodes();i++){
185 if (m->control_pressed) { return data; }
187 //pcountSize = 0, they are from a branch that is entirely from a group the user doesn't want
188 //pcountSize = 2, not unique to one group
189 //pcountSize = 1, unique to one group
192 for (int j = 0; j < namesOfGroupCombos[h].size(); j++) {
193 map<string, int>::iterator itGroup = t->tree[i].pcount.find(namesOfGroupCombos[h][j]);
194 if (itGroup != t->tree[i].pcount.end()) { pcountSize++; if (pcountSize > 1) { break; } }
197 if (pcountSize == 0) { }
198 else if ((t->tree[i].getBranchLength() != -1) && (pcountSize == 1)) { UniqueBL += abs(t->tree[i].getBranchLength()); }
200 if ((t->tree[i].getBranchLength() != -1) && (pcountSize != 0)) {
201 totalBL += abs(t->tree[i].getBranchLength());
205 UW = (UniqueBL / totalBL);
207 if (isnan(UW) || isinf(UW)) { UW = 0; }
213 if((count) % twentyPercent == 0){ m->mothurOut("Percentage complete: " + toString(int((count / (float)total) * 100.0))); m->mothurOutEndLine(); }
216 m->mothurOut("Percentage complete: 100"); m->mothurOutEndLine();
220 catch(exception& e) {
221 m->errorOut(e, "Unweighted", "driver");
225 /**************************************************************************************************/
227 EstOutput Unweighted::getValues(Tree* t, string groupA, string groupB) {
229 globaldata = GlobalData::getInstance();
231 vector<string> groups;
232 double UniqueBL; //a branch length is unique if it's chidren are from the same group
233 double totalBL; //all branch lengths
234 double UW; //Unweighted Value = UniqueBL / totalBL;
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 for (int r=0; r<numGroups; r++) {
243 for (int l = r+1; l < numGroups; l++) {
248 //numComp+1 for AB, AC, BC, ABC
249 data.resize(numComp+1,0);
252 for (int a=0; a<numGroups; a++) {
253 for (int l = a+1; l < numGroups; l++) {
254 UniqueBL=0.0000; //a branch length is unique if it's chidren are from the same group
255 totalBL = 0.00; //all branch lengths
256 UW = 0.00; //Unweighted Value = UniqueBL / totalBL;
258 //copy random tree passed in
259 copyTree->getCopy(t);
261 //groups in this combo
262 groups.push_back(globaldata->Groups[a]); groups.push_back(globaldata->Groups[l]);
264 //swap labels in the groups you want to compare
265 copyTree->assembleRandomUnifracTree(groups[0], groups[1]);
267 if (m->control_pressed) { delete copyTree; return data; }
269 for(int i=0;i<copyTree->getNumNodes();i++){
271 if (m->control_pressed) { return data; }
273 //pcountSize = 0, they are from a branch that is entirely from a group the user doesn't want
274 //pcountSize = 2, not unique to one group
275 //pcountSize = 1, unique to one group
278 for (int j = 0; j < groups.size(); j++) {
279 map<string, int>::iterator itGroup = copyTree->tree[i].pcount.find(groups[j]);
280 if (itGroup != copyTree->tree[i].pcount.end()) { pcountSize++; }
283 if (pcountSize == 0) { }
284 else if ((copyTree->tree[i].getBranchLength() != -1) && (pcountSize == 1)) { UniqueBL += abs(copyTree->tree[i].getBranchLength()); }
286 if ((copyTree->tree[i].getBranchLength() != -1) && (pcountSize != 0)) {
287 totalBL += abs(copyTree->tree[i].getBranchLength());
292 UW = (UniqueBL / totalBL);
294 if (isnan(UW) || isinf(UW)) { UW = 0; }
304 if (numGroups == 0) {
305 //get score for all users groups
306 for (int i = 0; i < tmap->namesOfGroups.size(); i++) {
307 if (tmap->namesOfGroups[i] != "xxx") {
308 groups.push_back(tmap->namesOfGroups[i]);
312 for (int i = 0; i < globaldata->Groups.size(); i++) {
313 groups.push_back(globaldata->Groups[i]);
317 UniqueBL=0.0000; //a branch length is unique if it's chidren are from the same group
318 totalBL = 0.00; //all branch lengths
319 UW = 0.00; //Unweighted Value = UniqueBL / totalBL;
321 //copy random tree passed in
322 copyTree->getCopy(t);
324 //swap labels in all the groups you want to compare
325 copyTree->assembleRandomUnifracTree(groups);
327 if (m->control_pressed) { delete copyTree; return data; }
329 for(int i=0;i<copyTree->getNumNodes();i++){
331 if (m->control_pressed) { return data; }
333 //pcountSize = 0, they are from a branch that is entirely from a group the user doesn't want
334 //pcountSize = 2, not unique to one group
335 //pcountSize = 1, unique to one group
338 for (int j = 0; j < groups.size(); j++) {
339 map<string, int>::iterator itGroup = copyTree->tree[i].pcount.find(groups[j]);
340 if (itGroup != copyTree->tree[i].pcount.end()) { pcountSize++; if (pcountSize > 1) { break; } }
343 if (pcountSize == 0) { }
344 else if ((copyTree->tree[i].getBranchLength() != -1) && (pcountSize == 1)) { UniqueBL += abs(copyTree->tree[i].getBranchLength()); }
346 if ((copyTree->tree[i].getBranchLength() != -1) && (pcountSize != 0)) {
347 totalBL += abs(copyTree->tree[i].getBranchLength());
351 UW = (UniqueBL / totalBL);
353 if (isnan(UW) || isinf(UW)) { UW = 0; }
363 catch(exception& e) {
364 m->errorOut(e, "Unweighted", "getValues");
369 /**************************************************************************************************/