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 remainingPairs = namesOfGroupCombos.size();
41 for (int remainingProcessors = processors; remainingProcessors > 0; remainingProcessors--) {
42 int numPairs = remainingPairs; //case for last processor
43 if (remainingProcessors != 1) { numPairs = ceil(remainingPairs / remainingProcessors); }
44 lines.push_back(linePair(startIndex, numPairs)); //startIndex, numPairs
45 startIndex = startIndex + numPairs;
46 remainingPairs = remainingPairs - numPairs;
49 data = createProcesses(t, namesOfGroupCombos, ct);
56 m->errorOut(e, "Weighted", "getValues");
60 /**************************************************************************************************/
62 EstOutput Weighted::createProcesses(Tree* t, vector< vector<string> > namesOfGroupCombos, CountTable* ct) {
64 vector<int> processIDS;
66 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux) || (__linux__) || (__unix__) || (__unix)
69 //loop through and create all the processes you want
70 while (process != processors) {
74 processIDS.push_back(pid); //create map from line number to pid so you can append files in correct order later
78 Myresults = driver(t, namesOfGroupCombos, lines[process].start, lines[process].num, ct);
80 //pass numSeqs to parent
82 string tempFile = outputDir + toString(getpid()) + ".weighted.results.temp";
83 m->openOutputFile(tempFile, out);
85 out << Myresults.size() << endl;
86 for (int i = 0; i < Myresults.size(); i++) { out << Myresults[i] << '\t'; } out << endl;
91 m->mothurOut("[ERROR]: unable to spawn the necessary processes."); m->mothurOutEndLine();
92 for (int i = 0; i < processIDS.size(); i++) { kill (processIDS[i], SIGINT); }
97 results = driver(t, namesOfGroupCombos, lines[0].start, lines[0].num, ct);
99 //force parent to wait until all the processes are done
100 for (int i=0;i<(processors-1);i++) {
101 int temp = processIDS[i];
105 if (m->control_pressed) { return results; }
107 //get data created by processes
108 for (int i=0;i<(processors-1);i++) {
110 string s = outputDir + toString(processIDS[i]) + ".weighted.results.temp";
111 m->openInputFile(s, in);
116 in >> num; m->gobble(in);
118 if (m->control_pressed) { break; }
121 for (int j = 0; j < num; j++) {
123 results.push_back(w);
133 vector<weightedData*> pDataArray;
134 DWORD dwThreadIdArray[processors-1];
135 HANDLE hThreadArray[processors-1];
136 vector<CountTable*> cts;
139 //Create processor worker threads.
140 for( int i=1; i<processors; i++ ){
141 CountTable* copyCount = new CountTable();
143 Tree* copyTree = new Tree(copyCount);
144 copyTree->getCopy(t);
146 cts.push_back(copyCount);
147 trees.push_back(copyTree);
149 weightedData* tempweighted = new weightedData(m, lines[i].start, lines[i].num, namesOfGroupCombos, copyTree, copyCount, includeRoot);
150 pDataArray.push_back(tempweighted);
151 processIDS.push_back(i);
153 hThreadArray[i-1] = CreateThread(NULL, 0, MyWeightedThreadFunction, pDataArray[i-1], 0, &dwThreadIdArray[i-1]);
156 results = driver(t, namesOfGroupCombos, lines[0].start, lines[0].num, ct);
158 //Wait until all threads have terminated.
159 WaitForMultipleObjects(processors-1, hThreadArray, TRUE, INFINITE);
161 //Close all thread handles and free memory allocations.
162 for(int i=0; i < pDataArray.size(); i++){
163 for (int j = 0; j < pDataArray[i]->results.size(); j++) { results.push_back(pDataArray[i]->results[j]); }
166 CloseHandle(hThreadArray[i]);
167 delete pDataArray[i];
173 catch(exception& e) {
174 m->errorOut(e, "Weighted", "createProcesses");
178 /**************************************************************************************************/
179 EstOutput Weighted::driver(Tree* t, vector< vector<string> > namesOfGroupCombos, int start, int num, CountTable* ct) {
185 for (int h = start; h < (start+num); h++) {
187 if (m->control_pressed) { return results; }
189 //initialize weighted score
190 string groupA = namesOfGroupCombos[h][0];
191 string groupB = namesOfGroupCombos[h][1];
193 set<int> validBranches;
194 WScore[groupA+groupB] = 0.0;
195 D.push_back(0.0000); //initialize a spot in D for each combination
197 //adding the wieghted sums from group i
198 for (int j = 0; j < t->groupNodeInfo[groupA].size(); j++) { //the leaf nodes that have seqs from group i
199 map<string, int>::iterator it = t->tree[t->groupNodeInfo[groupA][j]].pcount.find(groupA);
200 int numSeqsInGroupI = it->second;
202 double sum = getLengthToRoot(t, t->groupNodeInfo[groupA][j], groupA, groupB);
203 double weightedSum = ((numSeqsInGroupI * sum) / (double)ct->getGroupCount(groupA));
205 D[count] += weightedSum;
208 //adding the wieghted sums from group l
209 for (int j = 0; j < t->groupNodeInfo[groupB].size(); j++) { //the leaf nodes that have seqs from group l
210 map<string, int>::iterator it = t->tree[t->groupNodeInfo[groupB][j]].pcount.find(groupB);
211 int numSeqsInGroupL = it->second;
213 double sum = getLengthToRoot(t, t->groupNodeInfo[groupB][j], groupA, groupB);
214 double weightedSum = ((numSeqsInGroupL * sum) / (double)ct->getGroupCount(groupB));
216 D[count] += weightedSum;
221 //calculate u for the group comb
222 for (int h = start; h < (start+num); h++) {
224 //m->mothurOut("Processing combo: " + toString(h)); m->mothurOutEndLine();
226 string groupA = namesOfGroupCombos[h][0];
227 string groupB = namesOfGroupCombos[h][1];
229 //calculate u for the group comb
230 for(int i=0;i<t->getNumNodes();i++){
232 if (m->control_pressed) { return data; }
235 //int pcountSize = 0;
236 //does this node have descendants from groupA
237 it = t->tree[i].pcount.find(groupA);
238 //if it does u = # of its descendants with a certain group / total number in tree with a certain group
239 if (it != t->tree[i].pcount.end()) {
240 u = (double) t->tree[i].pcount[groupA] / (double) ct->getGroupCount(groupA);
244 //does this node have descendants from group l
245 it = t->tree[i].pcount.find(groupB);
247 //if it does subtract their percentage from u
248 if (it != t->tree[i].pcount.end()) {
249 u -= (double) t->tree[i].pcount[groupB] / (double) ct->getGroupCount(groupB);
253 if (t->tree[i].getBranchLength() != -1) {
254 u = abs(u * t->tree[i].getBranchLength());
255 WScore[(groupA+groupB)] += u;
258 //if this is not the root then add it
259 if (rootForGrouping[namesOfGroupCombos[h]].count(i) == 0) {
260 if (t->tree[i].getBranchLength() != -1) {
261 u = abs(u * t->tree[i].getBranchLength());
262 WScore[(groupA+groupB)] += u;
270 /********************************************************/
271 //calculate weighted score for the group combination
274 for (int h = start; h < (start+num); h++) {
275 UN = (WScore[namesOfGroupCombos[h][0]+namesOfGroupCombos[h][1]] / D[count]);
276 if (isnan(UN) || isinf(UN)) { UN = 0; }
277 results.push_back(UN);
283 catch(exception& e) {
284 m->errorOut(e, "Weighted", "driver");
288 /**************************************************************************************************/
289 EstOutput Weighted::getValues(Tree* t, string groupA, string groupB) {
292 data.clear(); //clear out old values
294 CountTable* ct = t->getCountTable();
296 if (m->control_pressed) { return data; }
298 //initialize weighted score
299 WScore[(groupA+groupB)] = 0.0;
301 set<int> validBranches;
303 vector<string> groups; groups.push_back(groupA); groups.push_back(groupB);
305 //adding the wieghted sums from group i
306 for (int j = 0; j < t->groupNodeInfo[groups[0]].size(); j++) { //the leaf nodes that have seqs from group i
307 map<string, int>::iterator it = t->tree[t->groupNodeInfo[groups[0]][j]].pcount.find(groups[0]);
308 int numSeqsInGroupI = it->second;
310 double sum = getLengthToRoot(t, t->groupNodeInfo[groups[0]][j], groups[0], groups[1]);
311 double weightedSum = ((numSeqsInGroupI * sum) / (double)ct->getGroupCount(groups[0]));
316 //adding the wieghted sums from group l
317 for (int j = 0; j < t->groupNodeInfo[groups[1]].size(); j++) { //the leaf nodes that have seqs from group l
318 map<string, int>::iterator it = t->tree[t->groupNodeInfo[groups[1]][j]].pcount.find(groups[1]);
319 int numSeqsInGroupL = it->second;
321 double sum = getLengthToRoot(t, t->groupNodeInfo[groups[1]][j], groups[0], groups[1]);
322 double weightedSum = ((numSeqsInGroupL * sum) / (double)ct->getGroupCount(groups[1]));
327 //calculate u for the group comb
328 for(int i=0;i<t->getNumNodes();i++){
330 if (m->control_pressed) { return data; }
333 //int pcountSize = 0;
334 //does this node have descendants from groupA
335 it = t->tree[i].pcount.find(groupA);
336 //if it does u = # of its descendants with a certain group / total number in tree with a certain group
337 if (it != t->tree[i].pcount.end()) {
338 u = (double) t->tree[i].pcount[groupA] / (double) ct->getGroupCount(groupA);
342 //does this node have descendants from group l
343 it = t->tree[i].pcount.find(groupB);
344 //if it does subtract their percentage from u
345 if (it != t->tree[i].pcount.end()) {
346 u -= (double) t->tree[i].pcount[groupB] / (double) ct->getGroupCount(groupB);
350 if (t->tree[i].getBranchLength() != -1) {
351 u = abs(u * t->tree[i].getBranchLength());
352 WScore[(groupA+groupB)] += u;
355 //if this is not the root then add it
356 if (rootForGrouping[groups].count(i) == 0) {
357 if (t->tree[i].getBranchLength() != -1) {
358 u = abs(u * t->tree[i].getBranchLength());
359 WScore[(groupA+groupB)] += u;
364 /********************************************************/
366 //calculate weighted score for the group combination
368 UN = (WScore[(groupA+groupB)] / D);
370 if (isnan(UN) || isinf(UN)) { UN = 0; }
375 catch(exception& e) {
376 m->errorOut(e, "Weighted", "getValues");
380 /**************************************************************************************************/
381 double Weighted::getLengthToRoot(Tree* t, int v, string groupA, string groupB) {
388 if(t->tree[index].getBranchLength() != -1){ sum += abs(t->tree[index].getBranchLength()); }
389 double tempTotal = 0.0;
390 index = t->tree[index].getParent();
392 vector<string> grouping; grouping.push_back(groupA); grouping.push_back(groupB);
394 rootForGrouping[grouping].insert(index);
396 //while you aren't at root
397 while(t->tree[index].getParent() != -1){
399 if (m->control_pressed) { return sum; }
401 int parent = t->tree[index].getParent();
403 if (includeRoot) { //add everyone
404 if(t->tree[index].getBranchLength() != -1){ sum += abs(t->tree[index].getBranchLength()); }
407 //am I the root for this grouping? if so I want to stop "early"
408 //does my sibling have descendants from the users groups?
409 int lc = t->tree[parent].getLChild();
410 int rc = t->tree[parent].getRChild();
413 if (lc == index) { sib = rc; }
415 map<string, int>::iterator itGroup;
417 itGroup = t->tree[sib].pcount.find(groupA);
418 if (itGroup != t->tree[sib].pcount.end()) { pcountSize++; }
419 itGroup = t->tree[sib].pcount.find(groupB);
420 if (itGroup != t->tree[sib].pcount.end()) { pcountSize++; }
422 //if yes, I am not the root so add me
423 if (pcountSize != 0) {
424 if (t->tree[index].getBranchLength() != -1) {
425 sum += abs(t->tree[index].getBranchLength()) + tempTotal;
431 rootForGrouping[grouping].clear();
432 rootForGrouping[grouping].insert(parent);
433 }else { //if no, I may be the root so add my br to tempTotal until I am proven innocent
434 if (t->tree[index].getBranchLength() != -1) {
435 tempTotal += abs(t->tree[index].getBranchLength());
443 //get all nodes above the root to add so we don't add their u values above
444 index = *(rootForGrouping[grouping].begin());
446 while(t->tree[index].getParent() != -1){
447 int parent = t->tree[index].getParent();
448 rootForGrouping[grouping].insert(parent);
453 catch(exception& e) {
454 m->errorOut(e, "Weighted", "getBranchLengthSums");
458 /**************************************************************************************************/