]> git.donarmstrong.com Git - mothur.git/blob - chao1.cpp
added logfile feature
[mothur.git] / chao1.cpp
1 /*
2  *  chao1.cpp
3  *  Dotur
4  *
5  *  Created by Sarah Westcott on 1/7/09.
6  *  Copyright 2009 Schloss Lab UMASS Amherst. All rights reserved.
7  *
8  */
9
10 #include "chao1.h"
11
12 /***********************************************************************/
13 EstOutput Chao1::getValues(SAbundVector* rank){
14         try {
15                 data.resize(3,0);
16         
17                 double sobs = (double)rank->getNumBins();
18         
19                 double singles = (double)rank->get(1);
20                 double doubles = (double)rank->get(2);
21                 double chaovar = 0.0000;
22         
23                 double chao = sobs + singles*(singles-1)/(2*(doubles+1));
24         
25                 if(singles > 0 && doubles > 0){
26                         chaovar = singles*(singles-1)/(2*(doubles+1))
27                                         + singles*pow(2*singles-1, 2)/(4*pow(doubles+1,2))
28                                         + pow(singles, 2)*doubles*pow(singles-1, 2)/(4*pow(doubles+1,4));
29                 }
30                 else if(singles > 0 && doubles == 0){
31                         chaovar = singles*(singles-1)/2 + singles*pow(2*singles-1, 2)/4 - pow(singles, 4)/(4*chao); 
32                 }
33                 else if(singles == 0){
34                         chaovar = sobs*exp(-1*rank->getNumSeqs()/sobs)*(1-exp(-1*rank->getNumSeqs()/sobs));
35                 }
36                         
37                 double chaohci, chaolci;
38         
39                 if(singles>0){
40                         double denom = pow(chao-sobs,2);
41                         double c = exp(1.96*pow((log(1+chaovar/denom)),0.5));
42                         chaolci = sobs+(chao-sobs)/c;//chao lci
43                         chaohci = sobs+(chao-sobs)*c;//chao hci
44                 }
45                 else{
46                         double p = exp(-1*rank->getNumSeqs()/sobs);
47                         chaolci = sobs/(1-p)-1.96*pow(sobs*p/(1-p), 0.5);
48                         chaohci = sobs/(1-p)+1.96*pow(sobs*p/(1-p), 0.5);
49                         if(chaolci < sobs){     chaolci = sobs; }
50                 }
51                 
52                 data[0] = chao;
53                 data[1] = chaolci;
54                 data[2] = chaohci;
55
56             if (isnan(data[0]) || isinf(data[0])) { data[0] = 0; }
57                 if (isnan(data[1]) || isinf(data[1])) { data[1] = 0; }
58                 if (isnan(data[2]) || isinf(data[2])) { data[2] = 0; }
59                 
60                 return data;
61         }
62         catch(exception& e) {
63                 errorOut(e, "Chao1", "getValues");
64                 exit(1);
65         }
66 }
67
68 /***********************************************************************/