5 * Created by Sarah Westcott on 2/9/09.
6 * Copyright 2009 Schloss Lab UMASS Amherst. All rights reserved.
12 /**************************************************************************************************/
14 EstOutput Weighted::getValues(Tree* t, int p, string o) {
16 data.clear(); //clear out old values
22 CountTable* ct = t->getCountTable();
24 numGroups = m->getNumGroups();
26 if (m->control_pressed) { return data; }
28 //calculate number of comparisons i.e. with groups A,B,C = AB, AC, BC = 3;
29 vector< vector<string> > namesOfGroupCombos;
30 for (int i=0; i<numGroups; i++) {
31 for (int l = 0; l < i; l++) {
32 //initialize weighted scores
33 //WScore[globaldata->Groups[i]+globaldata->Groups[l]] = 0.0;
34 vector<string> groups; groups.push_back((m->getGroups())[i]); groups.push_back((m->getGroups())[l]);
35 namesOfGroupCombos.push_back(groups);
39 int numPairs = namesOfGroupCombos.size();
40 int numPairsPerProcessor = numPairs / processors;
42 for (int i = 0; i < processors; i++) {
43 int startPos = i * numPairsPerProcessor;
44 if(i == processors - 1){ numPairsPerProcessor = numPairs - i * numPairsPerProcessor; }
45 lines.push_back(linePair(startPos, numPairsPerProcessor));
48 data = createProcesses(t, namesOfGroupCombos, ct);
55 m->errorOut(e, "Weighted", "getValues");
59 /**************************************************************************************************/
61 EstOutput Weighted::createProcesses(Tree* t, vector< vector<string> > namesOfGroupCombos, CountTable* ct) {
63 vector<int> processIDS;
65 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux) || (__linux__) || (__unix__) || (__unix)
68 //loop through and create all the processes you want
69 while (process != processors) {
73 processIDS.push_back(pid); //create map from line number to pid so you can append files in correct order later
77 Myresults = driver(t, namesOfGroupCombos, lines[process].start, lines[process].num, ct);
79 //pass numSeqs to parent
81 string tempFile = outputDir + toString(getpid()) + ".weighted.results.temp";
82 m->openOutputFile(tempFile, out);
84 out << Myresults.size() << endl;
85 for (int i = 0; i < Myresults.size(); i++) { out << Myresults[i] << '\t'; } out << endl;
90 m->mothurOut("[ERROR]: unable to spawn the necessary processes."); m->mothurOutEndLine();
91 for (int i = 0; i < processIDS.size(); i++) { kill (processIDS[i], SIGINT); }
96 results = driver(t, namesOfGroupCombos, lines[0].start, lines[0].num, ct);
98 //force parent to wait until all the processes are done
99 for (int i=0;i<(processors-1);i++) {
100 int temp = processIDS[i];
104 if (m->control_pressed) { return results; }
106 //get data created by processes
107 for (int i=0;i<(processors-1);i++) {
109 string s = outputDir + toString(processIDS[i]) + ".weighted.results.temp";
110 m->openInputFile(s, in);
115 in >> num; m->gobble(in);
117 if (m->control_pressed) { break; }
120 for (int j = 0; j < num; j++) {
122 results.push_back(w);
132 vector<weightedData*> pDataArray;
133 DWORD dwThreadIdArray[processors-1];
134 HANDLE hThreadArray[processors-1];
135 vector<CountTable*> cts;
138 //Create processor worker threads.
139 for( int i=1; i<processors; i++ ){
140 CountTable* copyCount = new CountTable();
142 Tree* copyTree = new Tree(copyCount);
143 copyTree->getCopy(t);
145 cts.push_back(copyCount);
146 trees.push_back(copyTree);
148 weightedData* tempweighted = new weightedData(m, lines[i].start, lines[i].num, namesOfGroupCombos, copyTree, copyCount, includeRoot);
149 pDataArray.push_back(tempweighted);
150 processIDS.push_back(i);
152 hThreadArray[i-1] = CreateThread(NULL, 0, MyWeightedThreadFunction, pDataArray[i-1], 0, &dwThreadIdArray[i-1]);
155 results = driver(t, namesOfGroupCombos, lines[0].start, lines[0].num, ct);
157 //Wait until all threads have terminated.
158 WaitForMultipleObjects(processors-1, hThreadArray, TRUE, INFINITE);
160 //Close all thread handles and free memory allocations.
161 for(int i=0; i < pDataArray.size(); i++){
162 for (int j = 0; j < pDataArray[i]->results.size(); j++) { results.push_back(pDataArray[i]->results[j]); }
165 CloseHandle(hThreadArray[i]);
166 delete pDataArray[i];
172 catch(exception& e) {
173 m->errorOut(e, "Weighted", "createProcesses");
177 /**************************************************************************************************/
178 EstOutput Weighted::driver(Tree* t, vector< vector<string> > namesOfGroupCombos, int start, int num, CountTable* ct) {
184 for (int h = start; h < (start+num); h++) {
186 if (m->control_pressed) { return results; }
188 //initialize weighted score
189 string groupA = namesOfGroupCombos[h][0];
190 string groupB = namesOfGroupCombos[h][1];
192 set<int> validBranches;
193 WScore[groupA+groupB] = 0.0;
194 D.push_back(0.0000); //initialize a spot in D for each combination
196 //adding the wieghted sums from group i
197 for (int j = 0; j < t->groupNodeInfo[groupA].size(); j++) { //the leaf nodes that have seqs from group i
198 map<string, int>::iterator it = t->tree[t->groupNodeInfo[groupA][j]].pcount.find(groupA);
199 int numSeqsInGroupI = it->second;
201 double sum = getLengthToRoot(t, t->groupNodeInfo[groupA][j], groupA, groupB);
202 double weightedSum = ((numSeqsInGroupI * sum) / (double)ct->getGroupCount(groupA));
204 D[count] += weightedSum;
207 //adding the wieghted sums from group l
208 for (int j = 0; j < t->groupNodeInfo[groupB].size(); j++) { //the leaf nodes that have seqs from group l
209 map<string, int>::iterator it = t->tree[t->groupNodeInfo[groupB][j]].pcount.find(groupB);
210 int numSeqsInGroupL = it->second;
212 double sum = getLengthToRoot(t, t->groupNodeInfo[groupB][j], groupA, groupB);
213 double weightedSum = ((numSeqsInGroupL * sum) / (double)ct->getGroupCount(groupB));
215 D[count] += weightedSum;
220 //calculate u for the group comb
221 for (int h = start; h < (start+num); h++) {
223 //m->mothurOut("Processing combo: " + toString(h)); m->mothurOutEndLine();
225 string groupA = namesOfGroupCombos[h][0];
226 string groupB = namesOfGroupCombos[h][1];
228 //calculate u for the group comb
229 for(int i=0;i<t->getNumNodes();i++){
231 if (m->control_pressed) { return data; }
234 //int pcountSize = 0;
235 //does this node have descendants from groupA
236 it = t->tree[i].pcount.find(groupA);
237 //if it does u = # of its descendants with a certain group / total number in tree with a certain group
238 if (it != t->tree[i].pcount.end()) {
239 u = (double) t->tree[i].pcount[groupA] / (double) ct->getGroupCount(groupA);
243 //does this node have descendants from group l
244 it = t->tree[i].pcount.find(groupB);
246 //if it does subtract their percentage from u
247 if (it != t->tree[i].pcount.end()) {
248 u -= (double) t->tree[i].pcount[groupB] / (double) ct->getGroupCount(groupB);
252 if (t->tree[i].getBranchLength() != -1) {
253 u = abs(u * t->tree[i].getBranchLength());
254 WScore[(groupA+groupB)] += u;
257 //if this is not the root then add it
258 if (rootForGrouping[namesOfGroupCombos[h]].count(i) == 0) {
259 if (t->tree[i].getBranchLength() != -1) {
260 u = abs(u * t->tree[i].getBranchLength());
261 WScore[(groupA+groupB)] += u;
269 /********************************************************/
270 //calculate weighted score for the group combination
273 for (int h = start; h < (start+num); h++) {
274 UN = (WScore[namesOfGroupCombos[h][0]+namesOfGroupCombos[h][1]] / D[count]);
275 if (isnan(UN) || isinf(UN)) { UN = 0; }
276 results.push_back(UN);
282 catch(exception& e) {
283 m->errorOut(e, "Weighted", "driver");
287 /**************************************************************************************************/
288 EstOutput Weighted::getValues(Tree* t, string groupA, string groupB) {
291 data.clear(); //clear out old values
293 CountTable* ct = t->getCountTable();
295 if (m->control_pressed) { return data; }
297 //initialize weighted score
298 WScore[(groupA+groupB)] = 0.0;
300 set<int> validBranches;
302 vector<string> groups; groups.push_back(groupA); groups.push_back(groupB);
304 //adding the wieghted sums from group i
305 for (int j = 0; j < t->groupNodeInfo[groups[0]].size(); j++) { //the leaf nodes that have seqs from group i
306 map<string, int>::iterator it = t->tree[t->groupNodeInfo[groups[0]][j]].pcount.find(groups[0]);
307 int numSeqsInGroupI = it->second;
309 double sum = getLengthToRoot(t, t->groupNodeInfo[groups[0]][j], groups[0], groups[1]);
310 double weightedSum = ((numSeqsInGroupI * sum) / (double)ct->getGroupCount(groups[0]));
315 //adding the wieghted sums from group l
316 for (int j = 0; j < t->groupNodeInfo[groups[1]].size(); j++) { //the leaf nodes that have seqs from group l
317 map<string, int>::iterator it = t->tree[t->groupNodeInfo[groups[1]][j]].pcount.find(groups[1]);
318 int numSeqsInGroupL = it->second;
320 double sum = getLengthToRoot(t, t->groupNodeInfo[groups[1]][j], groups[0], groups[1]);
321 double weightedSum = ((numSeqsInGroupL * sum) / (double)ct->getGroupCount(groups[1]));
326 //calculate u for the group comb
327 for(int i=0;i<t->getNumNodes();i++){
329 if (m->control_pressed) { return data; }
332 //int pcountSize = 0;
333 //does this node have descendants from groupA
334 it = t->tree[i].pcount.find(groupA);
335 //if it does u = # of its descendants with a certain group / total number in tree with a certain group
336 if (it != t->tree[i].pcount.end()) {
337 u = (double) t->tree[i].pcount[groupA] / (double) ct->getGroupCount(groupA);
341 //does this node have descendants from group l
342 it = t->tree[i].pcount.find(groupB);
343 //if it does subtract their percentage from u
344 if (it != t->tree[i].pcount.end()) {
345 u -= (double) t->tree[i].pcount[groupB] / (double) ct->getGroupCount(groupB);
349 if (t->tree[i].getBranchLength() != -1) {
350 u = abs(u * t->tree[i].getBranchLength());
351 WScore[(groupA+groupB)] += u;
354 //if this is not the root then add it
355 if (rootForGrouping[groups].count(i) == 0) {
356 if (t->tree[i].getBranchLength() != -1) {
357 u = abs(u * t->tree[i].getBranchLength());
358 WScore[(groupA+groupB)] += u;
363 /********************************************************/
365 //calculate weighted score for the group combination
367 UN = (WScore[(groupA+groupB)] / D);
369 if (isnan(UN) || isinf(UN)) { UN = 0; }
374 catch(exception& e) {
375 m->errorOut(e, "Weighted", "getValues");
379 /**************************************************************************************************/
380 double Weighted::getLengthToRoot(Tree* t, int v, string groupA, string groupB) {
387 if(t->tree[index].getBranchLength() != -1){ sum += abs(t->tree[index].getBranchLength()); }
388 double tempTotal = 0.0;
389 index = t->tree[index].getParent();
391 vector<string> grouping; grouping.push_back(groupA); grouping.push_back(groupB);
393 rootForGrouping[grouping].insert(index);
395 //while you aren't at root
396 while(t->tree[index].getParent() != -1){
398 if (m->control_pressed) { return sum; }
400 int parent = t->tree[index].getParent();
402 if (includeRoot) { //add everyone
403 if(t->tree[index].getBranchLength() != -1){ sum += abs(t->tree[index].getBranchLength()); }
406 //am I the root for this grouping? if so I want to stop "early"
407 //does my sibling have descendants from the users groups?
408 int lc = t->tree[parent].getLChild();
409 int rc = t->tree[parent].getRChild();
412 if (lc == index) { sib = rc; }
414 map<string, int>::iterator itGroup;
416 itGroup = t->tree[sib].pcount.find(groupA);
417 if (itGroup != t->tree[sib].pcount.end()) { pcountSize++; }
418 itGroup = t->tree[sib].pcount.find(groupB);
419 if (itGroup != t->tree[sib].pcount.end()) { pcountSize++; }
421 //if yes, I am not the root so add me
422 if (pcountSize != 0) {
423 if (t->tree[index].getBranchLength() != -1) {
424 sum += abs(t->tree[index].getBranchLength()) + tempTotal;
430 rootForGrouping[grouping].clear();
431 rootForGrouping[grouping].insert(parent);
432 }else { //if no, I may be the root so add my br to tempTotal until I am proven innocent
433 if (t->tree[index].getBranchLength() != -1) {
434 tempTotal += abs(t->tree[index].getBranchLength());
442 //get all nodes above the root to add so we don't add their u values above
443 index = *(rootForGrouping[grouping].begin());
445 while(t->tree[index].getParent() != -1){
446 int parent = t->tree[index].getParent();
447 rootForGrouping[grouping].insert(parent);
452 catch(exception& e) {
453 m->errorOut(e, "Weighted", "getBranchLengthSums");
457 /**************************************************************************************************/