]> git.donarmstrong.com Git - mothur.git/blob - sharedjackknife.cpp
*** empty log message ***
[mothur.git] / sharedjackknife.cpp
1 /*
2  *  sharedjackknife.cpp
3  *  Mothur
4  *
5  *  Created by Thomas Ryabin on 3/30/09.
6  *  Copyright 2009 __MyCompanyName__. All rights reserved.
7  *
8  */
9
10 #include "sharedjackknife.h"
11
12 /***************************************************************************************
13
14 ***************************************************************************************/
15 double SharedJackknife::simpson(int abunds[], double numInd, int numBins){
16         double denom = numInd*(numInd-1);
17         double sum = 0;
18         for(int i = 0; i < numBins; i++)
19                 sum += (double)abunds[i]*((double)abunds[i]-1)/denom;
20         
21         return sum;
22 }
23
24 /*****************************************************************************************/
25
26 double* SharedJackknife::jackknife(){           
27         int numBins = groups.at(0)->getNumBins()-1;
28         int cArray[numBins];
29         for(int i = 0; i < numBins; i++)
30                 cArray[i] = 0;
31
32         double numInd = 0;
33         for(int i = 0; i < numGroups; i++)
34                 for(int j = 0; j < numBins; j++) {
35                         int curAbund = groups.at(i)->get(j+1).abundance;
36                         cArray[j] += curAbund;
37                         numInd += (double)curAbund;
38                 }
39
40         double baseD = 1/simpson(cArray, numInd, numBins);
41         
42         double pseudoVals[numBins];
43         double jackknifeEstimate = 0;
44         for(int i = 0; i < numGroups; i++) {
45                 for(int j = 0; j < numBins-1; j++) {
46                         int abundDiff = -groups.at(i)->get(j+1).abundance;
47                         if(i > 0)
48                                 abundDiff += groups.at(i-1)->get(j+1).abundance;
49
50                         cArray[j] += abundDiff;
51                         numInd += abundDiff;    
52                 }
53                 
54                 double curD = 1/simpson(cArray, numInd, numBins);
55                 pseudoVals[i] = (double)numGroups*(baseD - curD) + curD;
56                 jackknifeEstimate += pseudoVals[i];
57         }
58         jackknifeEstimate /= (double)numGroups;
59                 
60         double variance = 0;
61         for(int i = 0; i < numGroups; i++)
62                 variance += pow(pseudoVals[i]-jackknifeEstimate, 2);
63                 
64         variance /= (double)numGroups*((double)numGroups-1);
65         double stErr = sqrt(variance);
66         TDTable table;
67         double confLimit = 0;
68         if(numGroups <= 30)
69                 confLimit = table.getConfLimit(numGroups-1, 1);
70         else
71                 confLimit = 1.645;
72         
73         confLimit *= stErr;
74         
75         double* rdata = new double[3];
76         rdata[0] = baseD;
77         rdata[1] = jackknifeEstimate - confLimit;
78         rdata[2] = jackknifeEstimate + confLimit;
79         
80         return rdata;
81 }
82
83 /************************************************************************************************
84 ************************************************************************************************/
85
86 EstOutput SharedJackknife::getValues(vector<SharedRAbundVector*> vectorShared){ //Fix this for collect, mistake was that it was made with summary in mind.
87         try {
88                 SharedRAbundVector* shared1 = vectorShared[0];
89                 SharedRAbundVector* shared2 = vectorShared[1];
90                 if(numGroups == -1) {
91                         globaldata = GlobalData::getInstance();
92                         numGroups = globaldata->Groups.size();
93                 }
94
95                 if(callCount == numGroups*(numGroups-1)/2) {
96                         currentCallDone = true;
97                         callCount = 0;
98                 }
99                 callCount++;
100
101                 if(currentCallDone) {
102                         groups.clear(); 
103                         currentCallDone = false;
104                 }
105                 
106                 if(groups.size() != numGroups) {        
107                         if(groups.size() == 0)
108                                 groups.push_back(shared1);
109                         groups.push_back(shared2);
110                 }
111                 
112                 if(groups.size() == numGroups && callCount < numGroups) {
113                         data.resize(3,0);
114
115                         double* rdata = jackknife();
116                         data[0] = rdata[0];
117                         data[1] = rdata[1];
118                         data[2] = rdata[2];
119                         
120                         //cout << "sT = " << data[0] << "    lower confLimit = " << data[1] << "     upper confLimit = " << data[2] << "\n";
121                         if (isnan(data[0]) || isinf(data[0])) { data[0] = 0; }
122                         if (isnan(data[1]) || isinf(data[1])) { data[1] = 0; }
123                         if (isnan(data[2]) || isinf(data[0])) { data[2] = 0; }
124                         
125                         /*for(int i = 0; i < groups.size(); i++)
126                                 cout << groups.at(i)->getGroup() << " ";
127                         cout << "\n";
128                         cout << groups.size() << "     " << data[0] << "    " << data[1] << "     " << data[2] << "\n\n";*/
129                         
130                         return data;
131                 }
132                 
133                 data.resize(3,0);
134                 data[0] = 0;
135                 data[1] = 0;
136                 data[2] = 0;
137                 
138                 if (isnan(data[0]) || isinf(data[0])) { data[0] = 0; }
139                 if (isnan(data[1]) || isinf(data[1])) { data[1] = 0; }
140                 if (isnan(data[2]) || isinf(data[2])) { data[2] = 0; }
141                 return data;    
142         }
143                 
144         catch(exception& e) {
145                 cout << "Standard Error: " << e.what() << " has occurred in the SharedJackknife class Function getValues. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
146                 exit(1);
147         }
148         catch(...) {
149                 cout << "An unknown error has occurred in the SharedJackknife class function getValues. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
150                 exit(1);
151         }       
152 }
153
154 /***********************************************************************/
155