]> git.donarmstrong.com Git - mothur.git/blob - chao1.cpp
added modify names parameter to set.dir
[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                 //this is a modification do to a vector fill error that occurs when an empty sharedRabund creates a sabund
20                 //in that case there is no 1 0r 2.
21                 double singles;
22                 if (rank->size() > 1) {
23                         singles = (double)rank->get(1);
24                 }else{ singles = 0.0;  }
25
26                 double doubles;
27                 if (rank->size() > 2) {
28                          doubles = (double)rank->get(2);
29                 }else{ doubles = 0.0;  }
30
31                 double chaovar = 0.0000;
32 //cout << "singles = " << singles << " doubles = " << doubles << " sobs = " << sobs << endl;    
33                 double chao = sobs + singles*(singles-1)/(2*(doubles+1));
34         
35                 if(singles > 0 && doubles > 0){
36                         chaovar = singles*(singles-1)/(2*(doubles+1))
37                                         + singles*pow(2*singles-1, 2)/(4*pow(doubles+1,2))
38                                         + pow(singles, 2)*doubles*pow(singles-1, 2)/(4*pow(doubles+1,4));
39                 }
40                 else if(singles > 0 && doubles == 0){
41                         chaovar = singles*(singles-1)/2 + singles*pow(2*singles-1, 2)/4 - pow(singles, 4)/(4*chao); 
42                 }
43                 else if(singles == 0){
44                         chaovar = sobs*exp(-1*rank->getNumSeqs()/sobs)*(1-exp(-1*rank->getNumSeqs()/sobs));
45                 }
46                         
47                 double chaohci, chaolci;
48         
49                 if(singles>0){
50                         double denom = pow(chao-sobs,2);
51                         double c = exp(1.96*pow((log(1+chaovar/denom)),0.5));
52                         chaolci = sobs+(chao-sobs)/c;//chao lci
53                         chaohci = sobs+(chao-sobs)*c;//chao hci
54                 }
55                 else{
56                         double p = exp(-1*rank->getNumSeqs()/sobs);
57                         chaolci = sobs/(1-p)-1.96*pow(sobs*p/(1-p), 0.5);
58                         chaohci = sobs/(1-p)+1.96*pow(sobs*p/(1-p), 0.5);
59                         if(chaolci < sobs){     chaolci = sobs; }
60                 }
61                 
62                 data[0] = chao;
63                 data[1] = chaolci;
64                 data[2] = chaohci;
65
66             if (isnan(data[0]) || isinf(data[0])) { data[0] = 0; }
67                 if (isnan(data[1]) || isinf(data[1])) { data[1] = 0; }
68                 if (isnan(data[2]) || isinf(data[2])) { data[2] = 0; }
69                 
70                 return data;
71         }
72         catch(exception& e) {
73                 m->errorOut(e, "Chao1", "getValues");
74                 exit(1);
75         }
76 }
77
78 /***********************************************************************/