]> git.donarmstrong.com Git - mothur.git/blobdiff - weighted.cpp
sffinfo bug with flow grams right index when clipQualRight=0
[mothur.git] / weighted.cpp
index 5f58f2eee439d16a11d2a2b9fbbb86397794b729..cf1291dfa91af84e4d821541b2a8f43a169d64f7 100644 (file)
 
 EstOutput Weighted::getValues(Tree* t, int p, string o) {
     try {
-               globaldata = GlobalData::getInstance();
-               
                data.clear(); //clear out old values
                int numGroups;
                vector<double> D;
                processors = p;
                outputDir = o;
+        
+        CountTable* ct = t->getCountTable();
                
-               numGroups = globaldata->Groups.size();
-       
-               vector<double> sums = getBranchLengthSums(t);
+               numGroups = m->getNumGroups();
                
                if (m->control_pressed) { return data; }
                
                //calculate number of comparisons i.e. with groups A,B,C = AB, AC, BC = 3;
                vector< vector<string> > namesOfGroupCombos;
                for (int i=0; i<numGroups; i++) { 
-                       for (int l = i+1; l < numGroups; l++) { 
+                       for (int l = 0; l < i; l++) {   
                                //initialize weighted scores
                                //WScore[globaldata->Groups[i]+globaldata->Groups[l]] = 0.0;
-                               vector<string> groups; groups.push_back(globaldata->Groups[i]); groups.push_back(globaldata->Groups[l]);
+                               vector<string> groups; groups.push_back((m->getGroups())[i]); groups.push_back((m->getGroups())[l]);
                                namesOfGroupCombos.push_back(groups);
                        }
                }
                
-               #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
+               #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux) || (__linux__) || (__unix__) || (__unix)
                        if(processors == 1){
-                               data = driver(t, namesOfGroupCombos, 0, namesOfGroupCombos.size(), sums);
+                               data = driver(t, namesOfGroupCombos, 0, namesOfGroupCombos.size(), ct);
                        }else{
                                int numPairs = namesOfGroupCombos.size();
                                
@@ -51,15 +49,15 @@ EstOutput Weighted::getValues(Tree* t, int p, string o) {
                                        if(i == processors - 1){
                                                numPairsPerProcessor = numPairs - i * numPairsPerProcessor;
                                        }
-                                       lines.push_back(new linePair(startPos, numPairsPerProcessor));
+                                       lines.push_back(linePair(startPos, numPairsPerProcessor));
                                }
 
-                               data = createProcesses(t, namesOfGroupCombos, sums);
+                               data = createProcesses(t, namesOfGroupCombos, ct);
                                
-                               for (int i = 0; i < lines.size(); i++) {  delete lines[i];  }  lines.clear();
+                               lines.clear();
                        }
                #else
-                       data = driver(t, namesOfGroupCombos, 0, namesOfGroupCombos.size(), sums);
+                       data = driver(t, namesOfGroupCombos, 0, namesOfGroupCombos.size(), ct);
                #endif
                
                return data;
@@ -71,11 +69,10 @@ EstOutput Weighted::getValues(Tree* t, int p, string o) {
 }
 /**************************************************************************************************/
 
-EstOutput Weighted::createProcesses(Tree* t, vector< vector<string> > namesOfGroupCombos, vector<double>& sums) {
+EstOutput Weighted::createProcesses(Tree* t, vector< vector<string> > namesOfGroupCombos, CountTable* ct) {
        try {
-#if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
+#if defined (__APPLE__) || (__MACH__) || (linux) || (__linux) || (__linux__) || (__unix__) || (__unix)
                int process = 1;
-               int num = 0;
                vector<int> processIDS;
                
                EstOutput results;
@@ -90,9 +87,9 @@ EstOutput Weighted::createProcesses(Tree* t, vector< vector<string> > namesOfGro
                        }else if (pid == 0){
        
                                EstOutput Myresults;
-                               Myresults = driver(t, namesOfGroupCombos, lines[process]->start, lines[process]->num, sums);
+                               Myresults = driver(t, namesOfGroupCombos, lines[process].start, lines[process].num, ct);
                        
-                               m->mothurOut("Merging results."); m->mothurOutEndLine();
+                               //m->mothurOut("Merging results."); m->mothurOutEndLine();
                                
                                //pass numSeqs to parent
                                ofstream out;
@@ -106,10 +103,14 @@ EstOutput Weighted::createProcesses(Tree* t, vector< vector<string> > namesOfGro
                                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);
+                       }
                }
        
-               results = driver(t, namesOfGroupCombos, lines[0]->start, lines[0]->num, sums);
+               results = driver(t, namesOfGroupCombos, lines[0].start, lines[0].num, ct);
        
                //force parent to wait until all the processes are done
                for (int i=0;i<(processors-1);i++) { 
@@ -140,10 +141,10 @@ EstOutput Weighted::createProcesses(Tree* t, vector< vector<string> > namesOfGro
                                m->gobble(in);
                        }
                        in.close();
-                       remove(s.c_str());
+                       m->mothurRemove(s);
                }
                
-               m->mothurOut("DONE."); m->mothurOutEndLine(); m->mothurOutEndLine();
+               //m->mothurOut("DONE."); m->mothurOutEndLine(); m->mothurOutEndLine();
                
                return results;
 #endif         
@@ -154,7 +155,7 @@ EstOutput Weighted::createProcesses(Tree* t, vector< vector<string> > namesOfGro
        }
 }
 /**************************************************************************************************/
-EstOutput Weighted::driver(Tree* t, vector< vector<string> > namesOfGroupCombos, int start, int num, vector<double>& sums) { 
+EstOutput Weighted::driver(Tree* t, vector< vector<string> > namesOfGroupCombos, int start, int num, CountTable* ct) { 
  try {
                EstOutput results;
                vector<double> D;
@@ -168,6 +169,7 @@ EstOutput Weighted::driver(Tree* t, vector< vector<string> > namesOfGroupCombos,
                        string groupA = namesOfGroupCombos[h][0]; 
                        string groupB = namesOfGroupCombos[h][1];
                        
+                       set<int> validBranches;
                        WScore[groupA+groupB] = 0.0;
                        D.push_back(0.0000); //initialize a spot in D for each combination
                        
@@ -176,7 +178,8 @@ EstOutput Weighted::driver(Tree* t, vector< vector<string> > namesOfGroupCombos,
                                map<string, int>::iterator it = t->tree[t->groupNodeInfo[groupA][j]].pcount.find(groupA);
                                int numSeqsInGroupI = it->second;
                                
-                               double weightedSum = ((numSeqsInGroupI * sums[t->groupNodeInfo[groupA][j]]) / (double)tmap->seqsPerGroup[groupA]);
+                               double sum = getLengthToRoot(t, t->groupNodeInfo[groupA][j], groupA, groupB);
+                               double weightedSum = ((numSeqsInGroupI * sum) / (double)ct->getGroupCount(groupA));
                        
                                D[count] += weightedSum;
                        }
@@ -186,58 +189,69 @@ EstOutput Weighted::driver(Tree* t, vector< vector<string> > namesOfGroupCombos,
                                map<string, int>::iterator it = t->tree[t->groupNodeInfo[groupB][j]].pcount.find(groupB);
                                int numSeqsInGroupL = it->second;
                                
-                               double weightedSum = ((numSeqsInGroupL * sums[t->groupNodeInfo[groupB][j]]) / (double)tmap->seqsPerGroup[groupB]);
+                               double sum = getLengthToRoot(t, t->groupNodeInfo[groupB][j], groupA, groupB);
+                               double weightedSum = ((numSeqsInGroupL * sum) / (double)ct->getGroupCount(groupB));
                        
                                D[count] += weightedSum;
                        }
                        count++;
                }
-               
+        
                //calculate u for the group comb 
-               int total = t->getNumNodes();
-               int twentyPercent = (total * 0.20);
-               for(int i=0;i<t->getNumNodes();i++){
-                       for (int h = start; h < (start+num); h++) {
+               for (int h = start; h < (start+num); h++) {     
+                       //report progress
+                       //m->mothurOut("Processing combo: " + toString(h)); m->mothurOutEndLine();
+                                               
+                       string groupA = namesOfGroupCombos[h][0]; 
+                       string groupB = namesOfGroupCombos[h][1];
+                       
+                       //calculate u for the group comb 
+                       for(int i=0;i<t->getNumNodes();i++){
                                
-                               string groupA = namesOfGroupCombos[h][0]; 
-                               string groupB = namesOfGroupCombos[h][1];
-
-                               if (m->control_pressed) { return results; }
+                               if (m->control_pressed) { return data; }
                                
                                double u;
+                               //int pcountSize = 0;
                                //does this node have descendants from groupA
                                it = t->tree[i].pcount.find(groupA);
                                //if it does u = # of its descendants with a certain group / total number in tree with a certain group
                                if (it != t->tree[i].pcount.end()) {
-                                       u = (double) t->tree[i].pcount[groupA] / (double) tmap->seqsPerGroup[groupA];
+                                       u = (double) t->tree[i].pcount[groupA] / (double) ct->getGroupCount(groupA);
                                }else { u = 0.00; }
-                       
-                                                       
+                               
+                               
                                //does this node have descendants from group l
                                it = t->tree[i].pcount.find(groupB);
+                               
                                //if it does subtract their percentage from u
                                if (it != t->tree[i].pcount.end()) {
-                                       u -= (double) t->tree[i].pcount[groupB] / (double) tmap->seqsPerGroup[groupB];
+                                       u -= (double) t->tree[i].pcount[groupB] / (double) ct->getGroupCount(groupB);
+                               }
+                               
+                               if (includeRoot) {
+                                       if (t->tree[i].getBranchLength() != -1) {
+                                               u = abs(u * t->tree[i].getBranchLength());
+                                               WScore[(groupA+groupB)] += u; 
+                                       }
+                               }else {
+                                       //if this is not the root then add it
+                                       if (rootForGrouping[namesOfGroupCombos[h]].count(i) == 0) {
+                                               if (t->tree[i].getBranchLength() != -1) {
+                                                       u = abs(u * t->tree[i].getBranchLength());
+                                                       WScore[(groupA+groupB)] += u; 
+                                               }
+                                       }
                                }
-                                                       
-                               u = abs(u * t->tree[i].getBranchLength());
-                                               
-                               //save groupcombs u value
-                               WScore[(groupA+groupB)] += u;
                        }
-                       //report progress
-                       if((i) % twentyPercent == 0){   m->mothurOut("Percentage complete: " + toString(int((i / (float)total) * 100.0))); m->mothurOutEndLine();               }
+                       
                }
                
-               m->mothurOut("Percentage complete: 100"); m->mothurOutEndLine();
-               
                /********************************************************/
                //calculate weighted score for the group combination
                double UN;      
                count = 0;
                for (int h = start; h < (start+num); h++) {
                        UN = (WScore[namesOfGroupCombos[h][0]+namesOfGroupCombos[h][1]] / D[count]);
-               
                        if (isnan(UN) || isinf(UN)) { UN = 0; } 
                        results.push_back(UN);
                        count++;
@@ -251,17 +265,19 @@ EstOutput Weighted::driver(Tree* t, vector< vector<string> > namesOfGroupCombos,
        }
 }
 /**************************************************************************************************/
-EstOutput Weighted::getValues(Tree* t, string groupA, string groupB, vector<double>& sums) { 
+EstOutput Weighted::getValues(Tree* t, string groupA, string groupB) { 
  try {
-               globaldata = GlobalData::getInstance();
                
                data.clear(); //clear out old values
+     
+        CountTable* ct = t->getCountTable();
                
                if (m->control_pressed) { return data; }
                
                //initialize weighted score
                WScore[(groupA+groupB)] = 0.0;
                double D = 0.0;
+               set<int> validBranches;
                
                vector<string> groups; groups.push_back(groupA); groups.push_back(groupB);
                
@@ -270,7 +286,8 @@ EstOutput Weighted::getValues(Tree* t, string groupA, string groupB, vector<doub
                        map<string, int>::iterator it = t->tree[t->groupNodeInfo[groups[0]][j]].pcount.find(groups[0]);
                        int numSeqsInGroupI = it->second;
                        
-                       double weightedSum = ((numSeqsInGroupI * sums[t->groupNodeInfo[groups[0]][j]]) / (double)tmap->seqsPerGroup[groups[0]]);
+                       double sum = getLengthToRoot(t, t->groupNodeInfo[groups[0]][j], groups[0], groups[1]);
+                       double weightedSum = ((numSeqsInGroupI * sum) / (double)ct->getGroupCount(groups[0]));
                
                        D += weightedSum;
                }
@@ -280,40 +297,51 @@ EstOutput Weighted::getValues(Tree* t, string groupA, string groupB, vector<doub
                        map<string, int>::iterator it = t->tree[t->groupNodeInfo[groups[1]][j]].pcount.find(groups[1]);
                        int numSeqsInGroupL = it->second;
                        
-                       double weightedSum = ((numSeqsInGroupL * sums[t->groupNodeInfo[groups[1]][j]]) / (double)tmap->seqsPerGroup[groups[1]]);
+                       double sum = getLengthToRoot(t, t->groupNodeInfo[groups[1]][j], groups[0], groups[1]);
+                       double weightedSum = ((numSeqsInGroupL * sum) / (double)ct->getGroupCount(groups[1]));
                
                        D += weightedSum;
                }
-               
+                               
                //calculate u for the group comb 
                for(int i=0;i<t->getNumNodes();i++){
-               
+                
                        if (m->control_pressed) { return data; }
                        
                        double u;
+                       //int pcountSize = 0;
                        //does this node have descendants from groupA
                        it = t->tree[i].pcount.find(groupA);
                        //if it does u = # of its descendants with a certain group / total number in tree with a certain group
                        if (it != t->tree[i].pcount.end()) {
-                               u = (double) t->tree[i].pcount[groupA] / (double) tmap->seqsPerGroup[groupA];
+                               u = (double) t->tree[i].pcount[groupA] / (double) ct->getGroupCount(groupA);
                        }else { u = 0.00; }
-               
-                                               
+                       
+                       
                        //does this node have descendants from group l
                        it = t->tree[i].pcount.find(groupB);
                        //if it does subtract their percentage from u
                        if (it != t->tree[i].pcount.end()) {
-                               u -= (double) t->tree[i].pcount[groupB] / (double) tmap->seqsPerGroup[groupB];
+                               u -= (double) t->tree[i].pcount[groupB] / (double) ct->getGroupCount(groupB);
                        }
-                                               
-                       u = abs(u * t->tree[i].getBranchLength());
-                                       
-                       //save groupcombs u value
-                       WScore[(groupA+groupB)] += u;
-               }
-               
+                       
+                       if (includeRoot) {
+                               if (t->tree[i].getBranchLength() != -1) {
+                                       u = abs(u * t->tree[i].getBranchLength());
+                                       WScore[(groupA+groupB)] += u;
+                               }
+                       }else{
+                               //if this is not the root then add it
+                               if (rootForGrouping[groups].count(i) == 0) {
+                                       if (t->tree[i].getBranchLength() != -1) {
+                                               u = abs(u * t->tree[i].getBranchLength());
+                                               WScore[(groupA+groupB)] += u;
+                                       }
+                               }
+                       }
+               }               
                /********************************************************/
-               
+        
                //calculate weighted score for the group combination
                double UN;      
                UN = (WScore[(groupA+groupB)] / D);
@@ -329,37 +357,77 @@ EstOutput Weighted::getValues(Tree* t, string groupA, string groupB, vector<doub
        }
 }
 /**************************************************************************************************/
-vector<double> Weighted::getBranchLengthSums(Tree* t) { 
+double Weighted::getLengthToRoot(Tree* t, int v, string groupA, string groupB) { 
        try {
                
-               vector<double> sums;
+               double sum = 0.0;
+               int index = v;
+       
+               //you are a leaf
+               if(t->tree[index].getBranchLength() != -1){     sum += abs(t->tree[index].getBranchLength());   }
+               double tempTotal = 0.0;
+               index = t->tree[index].getParent();     
+               
+               vector<string> grouping; grouping.push_back(groupA); grouping.push_back(groupB);
                
-               for(int v=0;v<t->getNumLeaves();v++){
+               rootForGrouping[grouping].insert(index);
                        
-                       if (m->control_pressed) { return sums; }
+               //while you aren't at root
+               while(t->tree[index].getParent() != -1){
+
+                       if (m->control_pressed) {  return sum; }
+                       
+                       int parent = t->tree[index].getParent();
+                       
+                       if (includeRoot) { //add everyone
+                               if(t->tree[index].getBranchLength() != -1){     sum += abs(t->tree[index].getBranchLength());   }
+                       }else {
                                
-                       int index = v;
-                       double sum = 0.0000;
-       
-                       //while you aren't at root
-                       while(t->tree[index].getParent() != -1){
-                                               
-                               //if you have a BL
-                               if(t->tree[index].getBranchLength() != -1){
-                                       sum += abs(t->tree[index].getBranchLength());
+                               //am I the root for this grouping? if so I want to stop "early"
+                               //does my sibling have descendants from the users groups? 
+                               int lc = t->tree[parent].getLChild();
+                               int rc = t->tree[parent].getRChild();
+                               
+                               int sib = lc;
+                               if (lc == index) { sib = rc; }
+                               
+                               map<string, int>::iterator itGroup;
+                               int pcountSize = 0;
+                               itGroup = t->tree[sib].pcount.find(groupA);
+                               if (itGroup != t->tree[sib].pcount.end()) { pcountSize++;  } 
+                               itGroup = t->tree[sib].pcount.find(groupB);
+                               if (itGroup != t->tree[sib].pcount.end()) { pcountSize++;  } 
+                               
+                               //if yes, I am not the root so add me
+                               if (pcountSize != 0) {
+                                       if (t->tree[index].getBranchLength() != -1) {
+                                               sum += abs(t->tree[index].getBranchLength()) + tempTotal;
+                                               tempTotal = 0.0;
+                                       }else {
+                                               sum += tempTotal;
+                                               tempTotal = 0.0;
+                                       }
+                                       rootForGrouping[grouping].clear();
+                                       rootForGrouping[grouping].insert(parent);
+                               }else { //if no, I may be the root so add my br to tempTotal until I am proven innocent
+                                       if (t->tree[index].getBranchLength() != -1) {
+                                               tempTotal += abs(t->tree[index].getBranchLength()); 
+                                       }
                                }
-                               index = t->tree[index].getParent();
-                       }
-                                       
-                       //get last breanch length added
-                       if(t->tree[index].getBranchLength() != -1){
-                               sum += abs(t->tree[index].getBranchLength());
                        }
                        
-                       sums.push_back(sum);
+                       index = parent; 
                }
                
-               return sums;
+               //get all nodes above the root to add so we don't add their u values above
+               index = *(rootForGrouping[grouping].begin());
+               
+               while(t->tree[index].getParent() != -1){
+                       int parent = t->tree[index].getParent();
+                       rootForGrouping[grouping].insert(parent);
+                       index = parent;
+               }
+               return sum;
        }
        catch(exception& e) {
                m->errorOut(e, "Weighted", "getBranchLengthSums");
@@ -369,4 +437,3 @@ vector<double> Weighted::getBranchLengthSums(Tree* t) {
 /**************************************************************************************************/
 
 
-