]> git.donarmstrong.com Git - mothur.git/blobdiff - weighted.cpp
moved utilities out of mothur.h and into mothurOut class.
[mothur.git] / weighted.cpp
index bcdee111a4763db3dae42d5750066e7ab47417cd..faa1ff76697e88a4f7e0bb06c1e2e44df7c53a2d 100644 (file)
@@ -15,146 +15,237 @@ EstOutput Weighted::getValues(Tree* t) {
     try {
                globaldata = GlobalData::getInstance();
                int numGroups;
+               vector<double> D;
                
-               //if the user has not entered specific groups to analyze then do them all
-               if (globaldata->Groups.size() == 0) {
-                       numGroups = tmap->getNumGroups();
-               }else {
-                       numGroups = globaldata->Groups.size();
-               }
+               numGroups = globaldata->Groups.size();
                
                //calculate number of comparisons i.e. with groups A,B,C = AB, AC, BC = 3;
-               int n = 1;
-               for (int i=1; i<numGroups; i++) { 
-                       for (int l = n; l < numGroups; l++) {
+               int count = 0;
+               for (int i=0; i<numGroups; i++) { 
+                       for (int l = i+1; l < numGroups; l++) { 
                                //initialize weighted scores
-                               if (globaldata->Groups.size() == 0) {
-                                       WScore[tmap->namesOfGroups[i-1]+tmap->namesOfGroups[l]] = 0.0;
-                               }else {
-                                       WScore[globaldata->Groups[i-1]+globaldata->Groups[l]] = 0.0;
-                               }
-                       }
-               }
-
-               data.clear(); //clear out old values
-       
-               double D = 0.0000;
-       
-               for(int i=0;i<t->getNumLeaves();i++){
-                       int index = i;
-                       double sum = 0.0000;
-               
-                       //while you aren't at root
-                       while(t->tree[index].getParent() != -1){
-               
-                               if(t->tree[index].pGroups.size() != 0){
-                                       sum += t->tree[index].getBranchLength();
-                               }
-                       
-                               //old_index = you
-                               int old_index = index; 
-                               //index = your parent   
-                               index = t->tree[index].getParent();
-                       
-                               //if you grandparent is the root 
-                               if(t->tree[index].getParent() == -1 && t->tree[old_index].pGroups.size() != 0){
-                                       int lc = t->tree[t->tree[index].getLChild()].pGroups.size();
-                                       int rc = t->tree[t->tree[index].getRChild()].pGroups.size();
+                               WScore[globaldata->Groups[i]+globaldata->Groups[l]] = 0.0;
+                               
+                               vector<string> groups; groups.push_back(globaldata->Groups[i]); groups.push_back(globaldata->Groups[l]);
+                               
+                               D.push_back(0.0000); //initialize a spot in D for each combination
                                
+                               /********************************************************/
+                               //calculate a D value for each group combo
+                               for(int v=0;v<t->getNumLeaves();v++){
                                
-                                       if(lc == 0 || rc == 0){
-                                               sum -= t->tree[old_index].getBranchLength();
+                                       if (m->control_pressed) { return data; }
+                                       
+                                       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());
+                                               }
+                                               index = t->tree[index].getParent();
+                                       }
+                                               
+                                       //get last breanch length added
+                                       if(t->tree[index].getBranchLength() != -1){
+                                               sum += abs(t->tree[index].getBranchLength());
+                                       }
+                                               
+                                       //is this sum from a sequence which is in one of the users groups
+                                       if (m->inUsersGroups(t->tree[v].getGroup(), globaldata->Groups) == true) {
+                                               //is this sum from a sequence which is in this groupCombo
+                                               if (m->inUsersGroups(t->tree[v].getGroup(), groups)) {
+                                                       int numSeqsInGroupI, numSeqsInGroupL;
+                                                       
+                                                       map<string, int>::iterator it;
+                                                       it = t->tree[v].pcount.find(groups[0]);
+                                                       if (it != t->tree[v].pcount.end()) { //this leaf node contains seqs from group i
+                                                               numSeqsInGroupI = it->second;
+                                                       }else{ numSeqsInGroupI = 0; }
+                                                       
+                                                       it = t->tree[v].pcount.find(groups[1]);
+                                                       if (it != t->tree[v].pcount.end()) { //this leaf node contains seqs from group l
+                                                               numSeqsInGroupL = it->second;
+                                                       }else{ numSeqsInGroupL = 0; }
+                                                       
+                                                       double weightedSum = ((numSeqsInGroupI * sum) / (double)tmap->seqsPerGroup[groups[0]]) + ((numSeqsInGroupL * sum) / (double)tmap->seqsPerGroup[groups[1]]);
+
+                                                       //sum /= (double)tmap->seqsPerGroup[t->tree[v].getGroup()];
+                                                       
+                                                       D[count] += weightedSum; 
+                                               }
                                        }
                                }
-                       }
-       
-                       if(t->tree[i].getGroup() != ""){
-                               sum /= (double)tmap->seqsPerGroup[t->tree[i].getGroup()];
-                               D += sum; 
+                               /*********************************************************/
+                               count++;
                        }
                }
-       
+               
+               data.clear(); //clear out old values
        
                for(int i=0;i<t->getNumNodes();i++){
                        //calculate weighted score for each of the group comb i.e. with groups A,B,C = AB, AC, BC.
-                       n = 1;
-                       for (int b=1; b<numGroups; b++) { 
-                               for (int l = n; l < numGroups; l++) {
-                                       double u;
-                                       //the user has not entered specific groups
-                                       if (globaldata->Groups.size() == 0) {
-                                               //does this node have descendants from group b-1
-                                               it = t->tree[i].pcount.find(tmap->namesOfGroups[b-1]);
-                                               //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[tmap->namesOfGroups[b-1]] / (double) tmap->seqsPerGroup[tmap->namesOfGroups[b-1]];
-                                               }else { u = 0.00; }
-               
-                                               //does this node have descendants from group l
-                                               it = t->tree[i].pcount.find(tmap->namesOfGroups[l]);
-                                               //if it does subtract their percentage from u
-                                               if (it != t->tree[i].pcount.end()) {
-                                                       u -= (double) t->tree[i].pcount[tmap->namesOfGroups[l]] / (double) tmap->seqsPerGroup[tmap->namesOfGroups[l]];
-                                               }
-                                               
-                                               u = abs(u) * t->tree[i].getBranchLength();
+                       for (int b=0; b<numGroups; b++) { 
+                               for (int l = b+1; l < numGroups; l++) {
+                               
+                                       if (m->control_pressed) { return data; }
                                        
-                                               //save groupcombs u value
-                                               WScore[tmap->namesOfGroups[b-1]+tmap->namesOfGroups[l]] += u;
-                                               
-                                       //the user has entered specific groups  
-                                       }else {
-                                               //does this node have descendants from group b-1
-                                               it = t->tree[i].pcount.find(globaldata->Groups[b-1]);
-                                               //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[globaldata->Groups[b-1]] / (double) tmap->seqsPerGroup[globaldata->Groups[b-1]];
-                                               }else { u = 0.00; }
-               
-                                               //does this node have descendants from group l
-                                               it = t->tree[i].pcount.find(globaldata->Groups[l]);
-                                               //if it does subtract their percentage from u
-                                               if (it != t->tree[i].pcount.end()) {
-                                                       u -= (double) t->tree[i].pcount[globaldata->Groups[l]] / (double) tmap->seqsPerGroup[globaldata->Groups[l]];
-                                               }
+                                       //calculate a u value for each combo
+                                       double u;
+                                       //does this node have descendants from group b-1
+                                       it = t->tree[i].pcount.find(globaldata->Groups[b]);
+                                       //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[globaldata->Groups[b]] / (double) tmap->seqsPerGroup[globaldata->Groups[b]];
+                                       }else { u = 0.00; }
+               
+                                       //does this node have descendants from group l
+                                       it = t->tree[i].pcount.find(globaldata->Groups[l]);
+                                       //if it does subtract their percentage from u
+                                       if (it != t->tree[i].pcount.end()) {
+                                               u -= (double) t->tree[i].pcount[globaldata->Groups[l]] / (double) tmap->seqsPerGroup[globaldata->Groups[l]];
+                                       }
                                                
-                                               u = abs(u) * t->tree[i].getBranchLength();
+                                       u = abs(u * t->tree[i].getBranchLength());
                                        
-                                               //save groupcombs u value
-                                               WScore[globaldata->Groups[b-1]+globaldata->Groups[l]] += u;
-                                       }
+                                       //save groupcombs u value
+                                       WScore[globaldata->Groups[b]+globaldata->Groups[l]] += u;
+                               /*********************************************************/
                                }
-                               n++;
                        }
                }
   
                //calculate weighted score for each group combination
                double UN;      
-               n = 1;
-               for (int i=1; i<numGroups; i++) { 
-                       for (int l = n; l < numGroups; l++) {
-                               //the user has not entered specific groups
-                               if (globaldata->Groups.size() == 0) {
-                                       UN = (WScore[tmap->namesOfGroups[i-1]+tmap->namesOfGroups[l]] / D);
-                               }else {//they have entered specific groups
-                                       UN = (WScore[globaldata->Groups[i-1]+globaldata->Groups[l]] / D);
-                               }
+               count = 0;
+               for (int i=0; i<numGroups; i++) { 
+                       for (int l = i+1; l < numGroups; l++) {
+                               UN = (WScore[globaldata->Groups[i]+globaldata->Groups[l]] / D[count]);
+               
                                if (isnan(UN) || isinf(UN)) { UN = 0; } 
                                data.push_back(UN);
+                               count++;
                        }
                }
-
                return data;
        }
        catch(exception& e) {
-               cout << "Standard Error: " << e.what() << " has occurred in the Weighted class Function getValues. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
+               m->errorOut(e, "Weighted", "getValues");
                exit(1);
        }
-       catch(...) {
-               cout << "An unknown error has occurred in the Weighted class function getValues. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
+}
+
+/**************************************************************************************************/
+EstOutput Weighted::getValues(Tree* t, string groupA, string groupB) { 
+ try {
+               globaldata = GlobalData::getInstance();
+               
+               data.clear(); //clear out old values
+               
+               //initialize weighted score
+               WScore[(groupA+groupB)] = 0.0;
+               float D = 0.0;
+               
+               vector<string> groups; groups.push_back(groupA); groups.push_back(groupB);
+                                               
+               /********************************************************/
+               //calculate a D value for the group combo
+               for(int v=0;v<t->getNumLeaves();v++){
+                       if (m->control_pressed) { return data; }
+                       
+                       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());
+                               }
+                               index = t->tree[index].getParent();
+                       }
+                                               
+                       //get last breanch length added
+                       if(t->tree[index].getBranchLength() != -1){
+                               sum += abs(t->tree[index].getBranchLength());
+                       }
+                                               
+                       if (m->inUsersGroups(t->tree[v].getGroup(), groups)) {
+                               int numSeqsInGroupI, numSeqsInGroupL;
+                                                       
+                               map<string, int>::iterator it;
+                               it = t->tree[v].pcount.find(groups[0]);
+                               if (it != t->tree[v].pcount.end()) { //this leaf node contains seqs from group i
+                                       numSeqsInGroupI = it->second;
+                               }else{ numSeqsInGroupI = 0; }
+                               
+                               it = t->tree[v].pcount.find(groups[1]);
+                               if (it != t->tree[v].pcount.end()) { //this leaf node contains seqs from group l
+                                       numSeqsInGroupL = it->second;
+                               }else{ numSeqsInGroupL = 0; }
+                               
+                               double weightedSum = ((numSeqsInGroupI * sum) / (double)tmap->seqsPerGroup[groups[0]]) + ((numSeqsInGroupL * sum) / (double)tmap->seqsPerGroup[groups[1]]);
+                               
+                               //sum /= (double)tmap->seqsPerGroup[t->tree[v].getGroup()];
+                               
+                               D += weightedSum; 
+                       }
+               }
+               /********************************************************/                              
+               
+               //calculate u for the group comb 
+               for(int i=0;i<t->getNumNodes();i++){
+               
+                       if (m->control_pressed) { return data; }
+                       
+                       double u;
+                       //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];
+                       }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 = abs(u * t->tree[i].getBranchLength());
+                                       
+                       //save groupcombs u value
+                       WScore[(groupA+groupB)] += u;
+               }
+               
+               /********************************************************/
+               
+               //calculate weighted score for the group combination
+               double UN;      
+               UN = (WScore[(groupA+groupB)] / D);
+               
+               if (isnan(UN) || isinf(UN)) { UN = 0; } 
+               data.push_back(UN);
+                               
+               return data; 
+       }
+       catch(exception& e) {
+               m->errorOut(e, "Weighted", "getValues");
                exit(1);
        }
-
 }
 
-/**************************************************************************************************/
\ No newline at end of file
+
+
+
+
+
+
+
+