]> git.donarmstrong.com Git - mothur.git/blob - sharedjackknife.cpp
added logfile feature
[mothur.git] / sharedjackknife.cpp
1 /*
2  *  sharedjackknife.cpp
3  *  Mothur
4  *
5  *  Created by Thomas Ryabin on 3/30/09.
6  *  Copyright 2009 Schloss Lab UMASS Amherst. All rights reserved.
7  *
8  */
9
10 #include "sharedjackknife.h"
11
12 /***************************************************************************************
13
14 ***************************************************************************************/
15 double SharedJackknife::simpson(vector<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         vector<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         vector<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                         if (isnan(data[0]) || isinf(data[0])) { data[0] = 0; }
121                         if (isnan(data[1]) || isinf(data[1])) { data[1] = 0; }
122                         if (isnan(data[2]) || isinf(data[0])) { data[2] = 0; }
123                         
124                         return data;
125                 }
126                 
127                 data.resize(3,0);
128                 data[0] = 0;
129                 data[1] = 0;
130                 data[2] = 0;
131                 
132                 if (isnan(data[0]) || isinf(data[0])) { data[0] = 0; }
133                 if (isnan(data[1]) || isinf(data[1])) { data[1] = 0; }
134                 if (isnan(data[2]) || isinf(data[2])) { data[2] = 0; }
135                 return data;    
136         }
137                 
138         catch(exception& e) {
139                 errorOut(e, "SharedJackknife", "getValues");
140                 exit(1);
141         }
142 }
143
144 /***********************************************************************/
145