]> git.donarmstrong.com Git - mothur.git/commitdiff
added root parameter to the unifrac commands so you can choose to include the entire...
authorwestcott <westcott>
Fri, 25 Feb 2011 11:13:46 +0000 (11:13 +0000)
committerwestcott <westcott>
Fri, 25 Feb 2011 11:13:46 +0000 (11:13 +0000)
unifracunweightedcommand.cpp
unifracunweightedcommand.h
unifracweightedcommand.cpp
unifracweightedcommand.h
unweighted.cpp
unweighted.h
weighted.cpp
weighted.h

index 63487e05cfc77ecfd6baa705b3a6a756a009cac4..2e90626585b2c86e31f34770b7f7868eb2926ba7 100644 (file)
@@ -12,7 +12,7 @@
 //**********************************************************************************************************************
 vector<string> UnifracUnweightedCommand::getValidParameters(){ 
        try {
-               string Array[] =  {"groups","iters","distance","random", "processors","outputdir","inputdir"};
+               string Array[] =  {"groups","iters","distance","random","root", "processors","outputdir","inputdir"};
                vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
                return myArray;
        }
@@ -73,7 +73,7 @@ UnifracUnweightedCommand::UnifracUnweightedCommand(string option)  {
                
                else {
                        //valid paramters for this command
-                       string Array[] =  {"groups","iters","distance","random", "processors","outputdir","inputdir"};
+                       string Array[] =  {"groups","iters","distance","random","root", "processors","outputdir","inputdir"};
                        vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
                        
                        OptionParser parser(option);
@@ -124,6 +124,9 @@ UnifracUnweightedCommand::UnifracUnweightedCommand(string option)  {
                        temp = validParameter.validFile(parameters, "random", false);                                   if (temp == "not found") { temp = "f"; }
                        random = m->isTrue(temp);
                        
+                       temp = validParameter.validFile(parameters, "root", false);                                     if (temp == "not found") { temp = "F"; }
+                       includeRoot = m->isTrue(temp);
+                       
                        temp = validParameter.validFile(parameters, "processors", false);       if (temp == "not found"){       temp = "1";                             }
                        convert(temp, processors); 
                        
@@ -149,7 +152,7 @@ UnifracUnweightedCommand::UnifracUnweightedCommand(string option)  {
                                
                                if (numGroups == 1) { numComp++; groupComb.push_back(allGroups); }
                                
-                               unweighted = new Unweighted(tmap);
+                               unweighted = new Unweighted(tmap, includeRoot);
                                
                        }
                        
@@ -167,11 +170,12 @@ UnifracUnweightedCommand::UnifracUnweightedCommand(string option)  {
 void UnifracUnweightedCommand::help(){
        try {
                m->mothurOut("The unifrac.unweighted command can only be executed after a successful read.tree command.\n");
-               m->mothurOut("The unifrac.unweighted command parameters are groups, iters, distance, processors and random.  No parameters are required.\n");
+               m->mothurOut("The unifrac.unweighted command parameters are groups, iters, distance, processors, root and random.  No parameters are required.\n");
                m->mothurOut("The groups parameter allows you to specify which of the groups in your groupfile you would like analyzed.  You must enter at least 1 valid group.\n");
                m->mothurOut("The group names are separated by dashes.  The iters parameter allows you to specify how many random trees you would like compared to your tree.\n");
                m->mothurOut("The distance parameter allows you to create a distance file from the results. The default is false. You may set distance to lt, square or column.\n");
                m->mothurOut("The random parameter allows you to shut off the comparison to random trees. The default is false, meaning compare don't your trees with randomly generated trees.\n");
+               m->mothurOut("The root parameter allows you to include the entire root in your calculations. The default is false, meaning stop at the root for this comparision instead of the root of the entire tree.\n");
                m->mothurOut("The processors parameter allows you to specify the number of processors to use. The default is 1.\n");
                m->mothurOut("The unifrac.unweighted command should be in the following format: unifrac.unweighted(groups=yourGroups, iters=yourIters).\n");
                m->mothurOut("Example unifrac.unweighted(groups=A-B-C, iters=500).\n");
index a1f2bf37480acc446b94e2f96ab6647b5c9e07a8..cd46b62b3462a4db2baa57865743d4bb37c198a7 100644 (file)
@@ -50,7 +50,7 @@ class UnifracUnweightedCommand : public Command {
                vector< map<float, float> > rscoreFreq;  //map <unweighted score, number of random trees with that score.> -vector entry for each combination.
                vector< map<float, float> > rCumul;  //map <unweighted score, cumulative percentage of number of random trees with that score or higher.> -vector entry for each combination.
                
-               bool abort, phylip, random;
+               bool abort, phylip, random, includeRoot;
                string groups, itersString, outputDir, outputForm;
                vector<string> Groups, outputNames; //holds groups to be used
                map<string, vector<string> > outputTypes;
index 1fa1a2833c1fa7604a9e2cbb07bf255ace9dcd94..0ccdf2f5b58888741328346b230b47cdf874a7d2 100644 (file)
@@ -12,7 +12,7 @@
 //**********************************************************************************************************************
 vector<string> UnifracWeightedCommand::getValidParameters(){   
        try {
-               string Array[] =  {"groups","iters","distance","random","processors","outputdir","inputdir"};
+               string Array[] =  {"groups","iters","distance","random","processors","root","outputdir","inputdir"};
                vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
                return myArray;
        }
@@ -72,7 +72,7 @@ UnifracWeightedCommand::UnifracWeightedCommand(string option) {
                
                else {
                        //valid paramters for this command
-                       string Array[] =  {"groups","iters","distance","random","processors","outputdir","inputdir"};
+                       string Array[] =  {"groups","iters","distance","random","processors","root","outputdir","inputdir"};
                        vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
                        
                        OptionParser parser(option);
@@ -120,9 +120,12 @@ UnifracWeightedCommand::UnifracWeightedCommand(string option) {
                                else { m->mothurOut("Options for distance are: lt, square, or column. Using lt."); m->mothurOutEndLine(); phylip = true; outputForm = "lt"; }
                        }
                        
-                       temp = validParameter.validFile(parameters, "random", false);                                   if (temp == "not found") { temp = "F"; }
+                       temp = validParameter.validFile(parameters, "random", false);                           if (temp == "not found") { temp = "F"; }
                        random = m->isTrue(temp);
                        
+                       temp = validParameter.validFile(parameters, "root", false);                                     if (temp == "not found") { temp = "F"; }
+                       includeRoot = m->isTrue(temp);
+                       
                        temp = validParameter.validFile(parameters, "processors", false);       if (temp == "not found"){       temp = "1";                             }
                        convert(temp, processors); 
                        
@@ -141,7 +144,7 @@ UnifracWeightedCommand::UnifracWeightedCommand(string option) {
                                util->setGroups(globaldata->Groups, tmap->namesOfGroups, s, numGroups, "weighted");     //sets the groups the user wants to analyze
                                util->getCombos(groupComb, globaldata->Groups, numComp);
                                
-                               weighted = new Weighted(tmap);
+                               weighted = new Weighted(tmap, includeRoot);
                                
                        }
                }
@@ -158,11 +161,12 @@ UnifracWeightedCommand::UnifracWeightedCommand(string option) {
 void UnifracWeightedCommand::help(){
        try {
                m->mothurOut("The unifrac.weighted command can only be executed after a successful read.tree command.\n");
-               m->mothurOut("The unifrac.weighted command parameters are groups, iters, distance, processors and random.  No parameters are required.\n");
+               m->mothurOut("The unifrac.weighted command parameters are groups, iters, distance, processors, root and random.  No parameters are required.\n");
                m->mothurOut("The groups parameter allows you to specify which of the groups in your groupfile you would like analyzed.  You must enter at least 2 valid groups.\n");
                m->mothurOut("The group names are separated by dashes.  The iters parameter allows you to specify how many random trees you would like compared to your tree.\n");
                m->mothurOut("The distance parameter allows you to create a distance file from the results. The default is false.\n");
                m->mothurOut("The random parameter allows you to shut off the comparison to random trees. The default is false, meaning don't compare your trees with randomly generated trees.\n");
+               m->mothurOut("The root parameter allows you to include the entire root in your calculations. The default is false, meaning stop at the root for this comparision instead of the root of the entire tree.\n");
                m->mothurOut("The processors parameter allows you to specify the number of processors to use. The default is 1.\n");
                m->mothurOut("The unifrac.weighted command should be in the following format: unifrac.weighted(groups=yourGroups, iters=yourIters).\n");
                m->mothurOut("Example unifrac.weighted(groups=A-B-C, iters=500).\n");
index 5bb690a854ed5c13f62a18fedc51cd39eee93f3a..2eee7c44e674073420c217f1b4d49aa4dc769497 100644 (file)
@@ -60,7 +60,7 @@ class UnifracWeightedCommand : public Command {
                vector< map<float, float> > rCumul;  //map <weighted score, cumulative percentage of number of random trees with that score or higher.> -vector entry for each c                                                                
                map<float, float>  validScores;  //map contains scores from random
                
-               bool abort, phylip, random;
+               bool abort, phylip, random, includeRoot;
                string groups, itersString, outputForm;
                vector<string> Groups, outputNames; //holds groups to be used
                map<string, vector<string> > outputTypes;
index f0c7c4051a7f7d83214cb921ae40570e7f4f8dd7..08e83ec5ba4ccbbbefe36163c25d1075fb05a883 100644 (file)
@@ -199,7 +199,8 @@ EstOutput Unweighted::driver(Tree* t, vector< vector<string> > namesOfGroupCombo
                                m->mothurOut(namesOfGroupCombos[h][namesOfGroupCombos[h].size()-1]);
                                m->mothurOut(", skipping."); m->mothurOutEndLine(); results[count] = UW;
                        }else{
-                       
+                               
+                               //if including the root this clears rootForGrouping[namesOfGroupCombos[h]]
                                getRoot(t, nodeBelonging, namesOfGroupCombos[h]);
                                
                                for(int i=0;i<t->getNumNodes();i++){
@@ -215,19 +216,19 @@ EstOutput Unweighted::driver(Tree* t, vector< vector<string> > namesOfGroupCombo
                                                map<string, int>::iterator itGroup = t->tree[i].pcount.find(namesOfGroupCombos[h][j]);
                                                if (itGroup != t->tree[i].pcount.end()) { pcountSize++; if (pcountSize > 1) { break; } } 
                                        }
-                                                                               
+                                       
+                                       
                                        //unique calc
                                        if (pcountSize == 0) { }
                                        else if ((t->tree[i].getBranchLength() != -1) && (pcountSize == 1) && (rootForGrouping[namesOfGroupCombos[h]].count(i) == 0)) { //you have a unique branch length and you are not the root 
                                                UniqueBL += abs(t->tree[i].getBranchLength()); 
                                        }
-                                       
+                                               
                                        //total calc
                                        if (pcountSize == 0) { }
                                        else if ((t->tree[i].getBranchLength() != -1) && (pcountSize != 0) && (rootForGrouping[namesOfGroupCombos[h]].count(i) == 0)) { //you have a branch length and you are not the root 
                                                totalBL += abs(t->tree[i].getBranchLength()); 
                                        }
-                                       
                                }
        //cout << UniqueBL << '\t' << totalBL << endl;          
                                UW = (UniqueBL / totalBL);  
@@ -442,6 +443,7 @@ EstOutput Unweighted::driver(Tree* t, vector< vector<string> > namesOfGroupCombo
                                m->mothurOut(", skipping."); m->mothurOutEndLine(); results[count] = UW;
                        }else{
                                
+                               //if including the root this clears rootForGrouping[namesOfGroupCombos[h]]
                                getRoot(copyTree, nodeBelonging, namesOfGroupCombos[h]);
                                
                                for(int i=0;i<copyTree->getNumNodes();i++){
@@ -497,47 +499,51 @@ int Unweighted::getRoot(Tree* t, int v, vector<string> grouping) {
                //you are a leaf so get your parent
                int index = t->tree[index].getParent();
                
-               //my parent is a potential root
-               rootForGrouping[grouping].insert(index);
-               
-               //while you aren't at root
-               while(t->tree[index].getParent() != -1){
-                       
-                       if (m->control_pressed) {  return 0; }
-                       
-                       //am I the root for this grouping? if so I want to stop "early"
-                       //does my sibling have descendants from the users groups? 
-                       //if so I am not the root
-                       int parent = t->tree[index].getParent();
-                       int lc = t->tree[parent].getLChild();
-                       int rc = t->tree[parent].getRChild();
-                       
-                       int sib = lc;
-                       if (lc == index) { sib = rc; }
+               if (includeRoot) { 
+                       rootForGrouping[grouping].clear();
+               }else {
+                       //my parent is a potential root
+                       rootForGrouping[grouping].insert(index);
                        
-                       map<string, int>::iterator itGroup;
-                       int pcountSize = 0;
-                       for (int j = 0; j < grouping.size(); j++) {
-                               map<string, int>::iterator itGroup = t->tree[sib].pcount.find(grouping[j]);
-                               if (itGroup != t->tree[sib].pcount.end()) { pcountSize++; if (pcountSize > 1) { break; } } 
+                       //while you aren't at root
+                       while(t->tree[index].getParent() != -1){
+                               
+                               if (m->control_pressed) {  return 0; }
+                               
+                               //am I the root for this grouping? if so I want to stop "early"
+                               //does my sibling have descendants from the users groups? 
+                               //if so I am not the root
+                               int parent = t->tree[index].getParent();
+                               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;
+                               for (int j = 0; j < grouping.size(); j++) {
+                                       map<string, int>::iterator itGroup = t->tree[sib].pcount.find(grouping[j]);
+                                       if (itGroup != t->tree[sib].pcount.end()) { pcountSize++; if (pcountSize > 1) { break; } } 
+                               }
+                               
+                               //if yes, I am not the root
+                               if (pcountSize != 0) {
+                                       rootForGrouping[grouping].clear();
+                                       rootForGrouping[grouping].insert(parent);
+                               }
+                               
+                               index = parent; 
                        }
                        
-                       //if yes, I am not the root
-                       if (pcountSize != 0) {
-                               rootForGrouping[grouping].clear();
+                       //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);
+                               //cout << parent << " in root" << endl;
+                               index = parent;
                        }
-                       
-                       index = parent; 
-               }
-               
-               //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);
-                       //cout << parent << " in root" << endl;
-                       index = parent;
                }
                
                return 0;
index 598af1a9a561fb51a5861c9e26a4c1994b1d2ad3..0ccfab55ad9500f46f33d74e1c1c19f95ec525ba 100644 (file)
@@ -19,7 +19,7 @@
 class Unweighted : public TreeCalculator  {
        
        public:
-               Unweighted(TreeMap* t) : tmap(t) {};
+               Unweighted(TreeMap* t, bool r) : tmap(t), includeRoot(r) {};
                ~Unweighted() {};
                EstOutput getValues(Tree*, int, string);
                EstOutput getValues(Tree*, string, string, int, string);
@@ -38,6 +38,7 @@ class Unweighted : public TreeCalculator  {
                int processors;
                string outputDir;
                map< vector<string>, set<int> > rootForGrouping;  //maps a grouping combo to the roots for that combo
+               bool includeRoot;
                
                EstOutput driver(Tree*, vector< vector<string> >, int, int); 
                EstOutput createProcesses(Tree*, vector< vector<string> >);
index b158815f90f266e07d64d4efa173fb7c4edaa0a0..cae2b92885632b697852babccd84d463b7a78564 100644 (file)
@@ -228,12 +228,19 @@ EstOutput Weighted::driver(Tree* t, vector< vector<string> > namesOfGroupCombos,
                                        u -= (double) t->tree[i].pcount[groupB] / (double) tmap->seqsPerGroup[groupB];
                                }
                                
-                               //if this is not the root then add it
-                               if (rootForGrouping[namesOfGroupCombos[h]].count(i) == 0) {
+                               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; 
+                                               }
+                                       }
                                }
                        }
                        
@@ -317,12 +324,19 @@ EstOutput Weighted::getValues(Tree* t, string groupA, string groupB) {
                                u -= (double) t->tree[i].pcount[groupB] / (double) tmap->seqsPerGroup[groupB];
                        }
                        
-                       //if this is not the root then add it
-                       if (rootForGrouping[groups].count(i) == 0) {
+                       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;
+                                       }
+                               }
                        }
                }               
                /********************************************************/
@@ -361,37 +375,43 @@ double Weighted::getLengthToRoot(Tree* t, int v, string groupA, string groupB) {
                while(t->tree[index].getParent() != -1){
 
                        if (m->control_pressed) {  return sum; }
-                               
-                       //am I the root for this grouping? if so I want to stop "early"
-                       //does my sibling have descendants from the users groups? 
+                       
                        int parent = t->tree[index].getParent();
-                       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()); 
+                       if (includeRoot) { //add everyone
+                               if(t->tree[index].getBranchLength() != -1){     sum += abs(t->tree[index].getBranchLength());   }
+                       }else {
+                               
+                               //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 11cd26b2d9642cb2679a0f3dfc06d930c15127f0..d78edf4d49ffd17e24f8eb559747a43952ad522a 100644 (file)
@@ -19,7 +19,7 @@
 class Weighted : public TreeCalculator  {
        
        public:
-               Weighted(TreeMap* t) : tmap(t) {};
+               Weighted(TreeMap* t, bool r) : tmap(t), includeRoot(r) {};
                ~Weighted() {};
                
                EstOutput getValues(Tree*, string, string);
@@ -41,6 +41,7 @@ class Weighted : public TreeCalculator  {
                int processors;
                string outputDir;
                map< vector<string>, set<int> > rootForGrouping;  //maps a grouping combo to the root for that combo
+               bool includeRoot;
                
                EstOutput driver(Tree*, vector< vector<string> >, int, int); 
                EstOutput createProcesses(Tree*, vector< vector<string> >);