X-Git-Url: https://git.donarmstrong.com/?a=blobdiff_plain;f=phylodiversitycommand.cpp;h=80d909016103f3b579f9096f342cf3a8acbb677b;hb=5694c92fbf646fe01abc87bde2af59e14a9a56b6;hp=6198f139ccb117b157dea30fb31a5ef2330e1aae;hpb=8bc3e5b38c2317a1715f53be22fa96455868c281;p=mothur.git diff --git a/phylodiversitycommand.cpp b/phylodiversitycommand.cpp index 6198f13..80d9090 100644 --- a/phylodiversitycommand.cpp +++ b/phylodiversitycommand.cpp @@ -24,6 +24,7 @@ vector PhyloDiversityCommand::getValidParameters(){ //********************************************************************************************************************** PhyloDiversityCommand::PhyloDiversityCommand(){ try { + abort = true; //initialize outputTypes vector tempOutNames; outputTypes["phylodiv"] = tempOutNames; @@ -289,7 +290,7 @@ int PhyloDiversityCommand::createProcesses(vector& procIters, Tree* t, map< try { #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux) int process = 1; - int num = 0; + vector processIDS; map< string, vector >::iterator itSum; @@ -321,7 +322,11 @@ int PhyloDiversityCommand::createProcesses(vector& procIters, Tree* t, map< out.close(); exit(0); - }else { m->mothurOut("unable to spawn the necessary processes."); m->mothurOutEndLine(); exit(0); } + }else { + m->mothurOut("[ERROR]: unable to spawn the necessary processes."); m->mothurOutEndLine(); + for (int i = 0; i < processIDS.size(); i++) { kill (processIDS[i], SIGINT); } + exit(0); + } } driver(t, div, sumDiv, procIters[0], increment, randomLeaf, numSampledList, outCollect, outSum, true); @@ -388,7 +393,7 @@ int PhyloDiversityCommand::driver(Tree* t, map< string, vector >& div, ma if (m->control_pressed) { return 0; } //calc branch length of randomLeaf k - float br = calcBranchLength(t, randomLeaf[k], countedBranch); + vector br = calcBranchLength(t, randomLeaf[k], countedBranch); //for each group in the groups update the total branch length accounting for the names file vector groups = t->tree[randomLeaf[k]].getGroup(); @@ -401,7 +406,7 @@ int PhyloDiversityCommand::driver(Tree* t, map< string, vector >& div, ma numSeqsInGroupJ = it->second; } - if (numSeqsInGroupJ != 0) { div[groups[j]][(counts[groups[j]]+1)] = div[groups[j]][counts[groups[j]]] + br; } + if (numSeqsInGroupJ != 0) { div[groups[j]][(counts[groups[j]]+1)] = div[groups[j]][counts[groups[j]]] + br[j]; } for (int s = (counts[groups[j]]+2); s <= (counts[groups[j]]+numSeqsInGroupJ); s++) { div[groups[j]][s] = div[groups[j]][s-1]; //update counts, but don't add in redundant branch lengths @@ -498,37 +503,92 @@ void PhyloDiversityCommand::printData(set& num, map< string, vector } } //********************************************************************************************************************** -float PhyloDiversityCommand::calcBranchLength(Tree* t, int leaf, map< string, set >& counted){ +//need a vector of floats one branch length for every group the node represents. +vector PhyloDiversityCommand::calcBranchLength(Tree* t, int leaf, map< string, set >& counted){ try { //calc the branch length //while you aren't at root - float sum = 0.0; + vector sums; int index = leaf; vector groups = t->tree[leaf].getGroup(); - - while(t->tree[index].getParent() != -1){ - - //if you have a BL - if(t->tree[index].getBranchLength() != -1){ - if (counted[groups[0]].count(index) == 0) { //you have not already counted this branch - sum += abs(t->tree[index].getBranchLength()); - for (int j = 0; j < groups.size(); j++) { counted[groups[j]].insert(index); } - } + sums.resize(groups.size(), 0.0); + + map > tempTotals; //maps node to total Branch Length + map< string, set > tempCounted; + set::iterator it; + + //you are a leaf + if(t->tree[index].getBranchLength() != -1){ + for (int k = 0; k < groups.size(); k++) { + sums[k] += abs(t->tree[index].getBranchLength()); + counted[groups[k]].insert(index); } - index = t->tree[index].getParent(); } + + for (int k = 0; k < groups.size(); k++) { + tempTotals[groups[k]][index] = 0.0; + } + + index = t->tree[index].getParent(); - //get last breanch length added - if(t->tree[index].getBranchLength() != -1){ - if (counted[groups[0]].count(index) == 0) { //you have not already counted this branch - sum += abs(t->tree[index].getBranchLength()); - for (int j = 0; j < groups.size(); j++) { counted[groups[j]].insert(index); } + //while you aren't at root + while(t->tree[index].getParent() != -1){ + + if (m->control_pressed) { return sums; } + + int pcountSize = 0; + for (int k = 0; k < groups.size(); k++) { + map::iterator itGroup = t->tree[index].pcount.find(groups[k]); + if (itGroup != t->tree[index].pcount.end()) { pcountSize++; } + + //do both your chidren have have descendants from the users groups? + int lc = t->tree[index].getLChild(); + int rc = t->tree[index].getRChild(); + + int LpcountSize = 0; + itGroup = t->tree[lc].pcount.find(groups[k]); + if (itGroup != t->tree[lc].pcount.end()) { LpcountSize++; } + + int RpcountSize = 0; + itGroup = t->tree[rc].pcount.find(groups[k]); + if (itGroup != t->tree[rc].pcount.end()) { RpcountSize++; } + + //if yes, add your childrens tempTotals + if ((LpcountSize != 0) && (RpcountSize != 0)) { + sums[k] += tempTotals[groups[k]][lc] + tempTotals[groups[k]][rc]; + + for (it = tempCounted[groups[k]].begin(); it != tempCounted[groups[k]].end(); it++) { counted[groups[k]].insert(*it); } + + //cout << "added to total " << tempTotals[lc] << '\t' << tempTotals[rc] << endl; + if (t->tree[index].getBranchLength() != -1) { + if (counted[groups[k]].count(index) == 0) { + tempTotals[groups[k]][index] = abs(t->tree[index].getBranchLength()); + tempCounted[groups[k]].insert(index); + }else{ + tempTotals[groups[k]][index] = 0.0; + } + }else { + tempTotals[groups[k]][index] = 0.0; + } + }else { //if no, your tempTotal is your childrens temp totals + your branch length + tempTotals[groups[k]][index] = tempTotals[groups[k]][lc] + tempTotals[groups[k]][rc]; + + if (counted[groups[k]].count(index) == 0) { + tempTotals[groups[k]][index] += abs(t->tree[index].getBranchLength()); + tempCounted[groups[k]].insert(index); + } + + } + //cout << "temptotal = "<< tempTotals[i] << endl; } + + index = t->tree[index].getParent(); } - - return sum; + + return sums; + } catch(exception& e) { m->errorOut(e, "PhyloDiversityCommand", "calcBranchLength");