]> git.donarmstrong.com Git - mothur.git/blobdiff - unifracunweightedcommand.cpp
fixed unweighted calculator
[mothur.git] / unifracunweightedcommand.cpp
index 77069443c2fd26a0cebbabf4dd661e1d37288030..e2b085f76a8e951886d668915306667a7de1b5f2 100644 (file)
@@ -22,24 +22,7 @@ UnifracUnweightedCommand::UnifracUnweightedCommand() {
                openOutputFile(sumFile, outSum);
                distFile = globaldata->getTreeFile() + ".uwdistrib";
                openOutputFile(distFile, outDist);
-
-               //if the user has not entered specific groups to analyze then do them all
-               if (globaldata->Groups.size() != 0) {
-                       //check that groups are valid
-                       for (int i = 0; i < globaldata->Groups.size(); i++) {
-                               if (tmap->isValidGroup(globaldata->Groups[i]) != true) {
-                                       cout << globaldata->Groups[i] << " is not a valid group, and will be disregarded." << endl;
-                                       // erase the invalid group from globaldata->Groups
-                                       globaldata->Groups.erase (globaldata->Groups.begin()+i);
-                               }
-                       }
-                       
-                       //if the user only entered invalid groups
-                       if (globaldata->Groups.size() == 0) { 
-                               cout << "When using the groups parameter you must have at least 1 valid group. I will run the command using all the groups in your groupfile." << endl; 
-                       }               
-               }
-
+               setGroups(); //sets users groups to analyze
                convert(globaldata->getIters(), iters);  //how many random trees to generate
                unweighted = new Unweighted(tmap);
 
@@ -63,11 +46,18 @@ int UnifracUnweightedCommand::execute() {
                
                //format output
                outDist.setf(ios::fixed, ios::floatfield); outDist.setf(ios::showpoint);
+               
+               outDist << "Groups Used ";
+               for (int m = 0; m < globaldata->Groups.size(); m++) {
+                       outDist << globaldata->Groups[m] << " ";
+               }
+               outDist << endl;
+               
                outDist << "Tree#" << '\t' << "Iter" << '\t' << "UWScore" << endl;
                
                //create new tree with same num nodes and leaves as users
                randT = new Tree();
-                       
+                               
                //get pscores for users trees
                for (int i = 0; i < T.size(); i++) {
                        cout << "Processing tree " << i+1 << endl;
@@ -113,12 +103,13 @@ int UnifracUnweightedCommand::execute() {
                        saveRandomScores(); //save all random scores for unweighted file
                        
                        //find the signifigance of the score
-                       float rcumul = 0.0000;
+                       float rcumul = 1.0000;
                        for (it = rscoreFreq.begin(); it != rscoreFreq.end(); it++) { 
+                               rCumul[it->first] = rcumul;
                                //get percentage of random trees with that info
                                rscoreFreq[it->first] /= iters; 
-                               rcumul+= it->second;  
-                               rCumul[it->first] = rcumul;
+                               rcumul-= it->second;  
+                               
                        }
                        
                        //save the signifigance of the users score for printing later
@@ -130,30 +121,31 @@ int UnifracUnweightedCommand::execute() {
                        rCumul.clear();
                }
                
-               float ucumul = 0.0000;
-               float rcumul = 0.0000;
+               float ucumul = 1.0000;
+               float rcumul = 1.0000;
                //this loop fills the cumulative maps and put 0.0000 in the score freq map to make it easier to print.
                for (it = validScores.begin(); it != validScores.end(); it++) { 
                        it2 = uscoreFreq.find(it->first);
-                       //user data has that score 
-                       if (it2 != uscoreFreq.end()) { uscoreFreq[it->first] /= T.size(); ucumul+= it2->second;  }
-                       else { uscoreFreq[it->first] = 0.0000; } //no user trees with that score
                        //make uCumul map
                        uCumul[it->first] = ucumul;
-                       
+                       //user data has that score 
+                       if (it2 != uscoreFreq.end()) { uscoreFreq[it->first] /= T.size(); ucumul-= it2->second;  }
+                       else { uscoreFreq[it->first] = 0.0000; } //no user trees with that score
+                                               
                        //make rscoreFreq map and rCumul
                        it2 = totalrscoreFreq.find(it->first);
+                       rCumul[it->first] = rcumul;
                        //get percentage of random trees with that info
-                       if (it2 != totalrscoreFreq.end()) {  totalrscoreFreq[it->first] /= (iters*T.size()); rcumul+= it2->second;  }
+                       if (it2 != totalrscoreFreq.end()) {  totalrscoreFreq[it->first] /= (iters*T.size()); rcumul-= it2->second;  }
                        else { totalrscoreFreq[it->first] = 0.0000; } //no random trees with that score
-                       rCumul[it->first] = rcumul;
+                       
                }
                
                printUnweightedFile();
                printUWSummaryFile();
                
-               //reset randomTree parameter to 0
-               globaldata->setRandomTree("0");
+               //reset groups parameter
+               globaldata->Groups.clear();
                
                delete randT;
                
@@ -174,6 +166,12 @@ void UnifracUnweightedCommand::printUnweightedFile() {
        try {
                //column headers
                
+               out << "Groups Used ";
+               for (int m = 0; m < globaldata->Groups.size(); m++) {
+                       out << globaldata->Groups[m] << " ";
+               }
+               out << endl;
+
                out << "Score" << '\t' << "UserFreq" << '\t' << "UserCumul" << '\t' << "RandFreq" << '\t' << "RandCumul" << endl;
                                
                //format output
@@ -201,6 +199,13 @@ void UnifracUnweightedCommand::printUnweightedFile() {
 void UnifracUnweightedCommand::printUWSummaryFile() {
        try {
                //column headers
+               
+               outSum << "Groups Used ";
+               for (int m = 0; m < globaldata->Groups.size(); m++) {
+                       outSum << globaldata->Groups[m] << " ";
+               }
+               outSum << endl;
+
                outSum << "Tree#" << '\t'  <<  "UWScore" << '\t' << '\t' << "UWSig" <<  endl;
                
                //format output
@@ -246,4 +251,44 @@ void UnifracUnweightedCommand::saveRandomScores() {
        }
 }
 
-/***********************************************************/
\ No newline at end of file
+/***********************************************************/
+
+void UnifracUnweightedCommand::setGroups() {
+       try {
+               //if the user has not entered specific groups to analyze then do them all
+               if (globaldata->Groups.size() != 0) {
+                       //check that groups are valid
+                       for (int i = 0; i < globaldata->Groups.size(); i++) {
+                               if (tmap->isValidGroup(globaldata->Groups[i]) != true) {
+                                       cout << globaldata->Groups[i] << " is not a valid group, and will be disregarded." << endl;
+                                       // erase the invalid group from globaldata->Groups
+                                       globaldata->Groups.erase (globaldata->Groups.begin()+i);
+                               }
+                       }
+                       
+                       //if the user only entered invalid groups
+                       if (globaldata->Groups.size() == 0) { 
+                               cout << "When using the groups parameter you must have at least 1 valid group. I will run the command using all the groups in your groupfile." << endl; 
+                               for (int i = 0; i < tmap->namesOfGroups.size(); i++) {
+                                       globaldata->Groups.push_back(tmap->namesOfGroups[i]);
+                               }
+                       }
+                                       
+               }else {
+                       for (int i = 0; i < tmap->namesOfGroups.size(); i++) {
+                               globaldata->Groups.push_back(tmap->namesOfGroups[i]);
+                       }
+               }
+       }
+       catch(exception& e) {
+               cout << "Standard Error: " << e.what() << " has occurred in the UnifracUnweightedCommand class Function setGroups. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
+               exit(1);
+       }
+       catch(...) {
+               cout << "An unknown error has occurred in the UnifracUnweightedCommand class function setGroups. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
+               exit(1);
+       }               
+
+}
+/*****************************************************************/
+