]> git.donarmstrong.com Git - mothur.git/blob - geom.cpp
1a7c2bfd601340c85cf5b73b06b280550b8f1f60
[mothur.git] / geom.cpp
1 /*
2  *  geom.cpp
3  *  Mothur
4  *
5  *  Created by Thomas Ryabin on 2/23/09.
6  *  Copyright 2009 __MyCompanyName__. All rights reserved.
7  *
8  */
9
10 #include "geom.h"
11
12 /***********************************************************************/
13
14 double Geom::kEq(double k, double spec){
15         return k/(1-k)*pow(1-k, spec)/(1-pow(1-k, spec));
16 }
17
18 RAbundVector Geom::getRAbundVector(SAbundVector* rank){
19                 vector <int> rData;
20                 int mr = 1;
21                 int nb = 0;
22                 int ns = 0;
23                 
24                 for(int i = rank->size()-1; i > 0; i--)
25                 {
26                         int cur = rank->get(i);
27                         if(mr == 1 && cur > 0)
28                                 mr = i;
29                         nb += cur;
30                         ns += i*cur;
31                         for(int j = 0; j < cur; j++)
32                                 rData.push_back(i);
33                 }
34                 
35                 RAbundVector rav = RAbundVector(rData, mr, nb, ns);
36                 rav.setLabel(rank->getLabel());
37                 return rav;
38 }
39
40 /***********************************************************************************/
41
42 /***********************************************************************************/
43 EstOutput Geom::getValues(SAbundVector* rank){
44         try {
45                 data.resize(2,0);
46                 
47                 rdata = getRAbundVector(rank);
48                 int numInd = rdata.getNumSeqs();
49                 int numSpec = rdata.getNumBins();
50                 int min = rdata.get(rdata.size()-1);
51                 double k = .5;
52                 double step = .49999;
53                 
54                 while(fabs(min - numInd*kEq(k, (double)numSpec)) > .0001) //This uses a binary search to find the value of k.
55                 {
56                         if(numInd*kEq(k, numSpec) > min)
57                                 k += step;
58                         else
59                                 k -= step;
60                         step /= 2;
61                 }
62                 
63                 double cK = 1/(1-pow(1-k, numSpec));
64                 double sumExp = 0;
65                 double sumObs = 0;
66                 double maxDiff = 0;
67
68                 for(int i = 0; i < rdata.size(); i++)
69                 {
70                         sumObs += rdata.get(i);
71                         sumExp += numInd*cK*k*pow(1-k, i);
72                         double diff = fabs(sumObs-sumExp);
73                         if(diff > maxDiff)
74                                 maxDiff = diff;
75                 }
76
77                 double DStatistic = maxDiff/numInd;
78                 double critVal = 0;
79                 /*cout << "Geom:\n";
80                 cout << "D-Statistic = " << DStatistic << "\n";
81                 cout << "Critical value for 95% confidence interval = ";*/
82                 if(rdata.size() > 20)
83                 {
84                         critVal = .886/sqrt(rdata.size());
85                 }
86                 else
87                 {
88                         KOSTable table;
89                         critVal = table.getConfLimit(numSpec);
90                 }
91                 /*cout << critVal << "\n";
92                 cout << "If D-Statistic is less than the critical value then the data fits the Geometric Series model w/ 95% confidence.\n\n";*/
93                 
94                 data[0] = DStatistic;
95                 data[1] = critVal;
96                 
97                 if (isnan(data[0]) || isinf(data[0])) { data[0] = 0; }
98                 if (isnan(data[1]) || isinf(data[1])) { data[1] = 0; }
99                 
100                 return data;
101         }
102         catch(exception& e) {
103                 cout << "Standard Error: " << e.what() << " has occurred in the NPShannon class Function getValues. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
104                 exit(1);
105         }
106         catch(...) {
107                 cout << "An unknown error has occurred in the NPShannon class function getValues. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
108                 exit(1);
109         }       
110 }
111
112 /***********************************************************************/
113
114
115
116
117
118
119