]> git.donarmstrong.com Git - mothur.git/blobdiff - chao1.cpp
changed random forest output filename
[mothur.git] / chao1.cpp
index d938eb4ab71504f66f72eec2c91f9bfbb243a212..ad1faa242b81c1520d29fb6395fa5f5825d179ec 100644 (file)
--- a/chao1.cpp
+++ b/chao1.cpp
@@ -15,33 +15,49 @@ EstOutput Chao1::getValues(SAbundVector* rank){
                data.resize(3,0);
        
                double sobs = (double)rank->getNumBins();
-       
-               double singles = (double)rank->get(1);
-               double doubles = (double)rank->get(2);
+               
+               //this is a modification do to a vector fill error that occurs when an empty sharedRabund creates a sabund
+               //in that case there is no 1 0r 2.
+               double singles;
+               if (rank->size() > 1) {
+                       singles = (double)rank->get(1);
+               }else{ singles = 0.0;  }
+
+               double doubles;
+               if (rank->size() > 2) {
+                        doubles = (double)rank->get(2);
+               }else{ doubles = 0.0;  }
+
                double chaovar = 0.0000;
+//cout << "singles = " << singles << " doubles = " << doubles << " sobs = " << sobs << endl;   
+               double chao = sobs + singles*(singles-1)/(2*(doubles+1));
        
-               double chao = sobs + pow(singles,2)/(2*(doubles+1)) - (singles*doubles/(2*pow(doubles+1,2)));
-       
-               if(doubles==0){chaovar=0;}
-               else{
-                       double g=singles/doubles;
-                       chaovar = doubles*(0.25*pow(g,4)+pow(g,3)+0.5*pow(g,2));
+               if(singles > 0 && doubles > 0){
+                       chaovar = singles*(singles-1)/(2*(doubles+1))
+                                       + singles*pow(2*singles-1, 2)/(4*pow(doubles+1,2))
+                                       + pow(singles, 2)*doubles*pow(singles-1, 2)/(4*pow(doubles+1,4));
                }
-
-       
+               else if(singles > 0 && doubles == 0){
+                       chaovar = singles*(singles-1)/2 + singles*pow(2*singles-1, 2)/4 - pow(singles, 4)/(4*chao); 
+               }
+               else if(singles == 0){
+                       chaovar = sobs*exp(-1*rank->getNumSeqs()/sobs)*(1-exp(-1*rank->getNumSeqs()/sobs));
+               }
+                       
                double chaohci, chaolci;
        
-               if(chao==sobs){
-                       double ci = 1.96*pow(chaovar,0.5);
-                       chaolci = chao-ci;//chao lci
-                       chaohci = chao+ci;//chao hci
-               }
-               else{
+               if(singles>0){
                        double denom = pow(chao-sobs,2);
                        double c = exp(1.96*pow((log(1+chaovar/denom)),0.5));
                        chaolci = sobs+(chao-sobs)/c;//chao lci
                        chaohci = sobs+(chao-sobs)*c;//chao hci
                }
+               else{
+                       double p = exp(-1*rank->getNumSeqs()/sobs);
+                       chaolci = sobs/(1-p)-1.96*pow(sobs*p/(1-p), 0.5);
+                       chaohci = sobs/(1-p)+1.96*pow(sobs*p/(1-p), 0.5);
+                       if(chaolci < sobs){     chaolci = sobs; }
+               }
                
                data[0] = chao;
                data[1] = chaolci;
@@ -54,13 +70,9 @@ EstOutput Chao1::getValues(SAbundVector* rank){
                return data;
        }
        catch(exception& e) {
-               cout << "Standard Error: " << e.what() << " has occurred in the Chao1 class Function getValues. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
+               m->errorOut(e, "Chao1", "getValues");
                exit(1);
        }
-       catch(...) {
-               cout << "An unknown error has occurred in the Chao1 class function getValues. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
-               exit(1);
-       }       
-};
+}
 
 /***********************************************************************/