File: sharedjackknife.cpp

package info (click to toggle)
mothur 1.33.3%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 11,248 kB
  • ctags: 12,231
  • sloc: cpp: 152,046; fortran: 665; makefile: 74; sh: 34
file content (144 lines) | stat: -rw-r--r-- 3,824 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/*
 *  sharedjackknife.cpp
 *  Mothur
 *
 *  Created by Thomas Ryabin on 3/30/09.
 *  Copyright 2009 Schloss Lab UMASS Amherst. All rights reserved.
 *
 */

#include "sharedjackknife.h"

/***************************************************************************************

***************************************************************************************/
double SharedJackknife::simpson(vector<int> abunds, double numInd, int numBins){
	double denom = numInd*(numInd-1);
	double sum = 0;
	for(int i = 0; i < numBins; i++)
		sum += (double)abunds[i]*((double)abunds[i]-1)/denom;
	
	return sum;
}

/*****************************************************************************************/

double* SharedJackknife::jackknife(){		
	int numBins = groups.at(0)->getNumBins()-1;
	vector<int> cArray(numBins);
	for(int i = 0; i < numBins; i++)
		cArray[i] = 0;

	double numInd = 0;
	for(int i = 0; i < numGroups; i++)
		for(int j = 0; j < numBins; j++) {
			int curAbund = groups.at(i)->get(j+1).abundance;
			cArray[j] += curAbund;
			numInd += (double)curAbund;
		}

	double baseD = 1/simpson(cArray, numInd, numBins);
	
	vector<double> pseudoVals(numBins);
	double jackknifeEstimate = 0;
	for(int i = 0; i < numGroups; i++) {
		for(int j = 0; j < numBins-1; j++) {
			int abundDiff = -groups.at(i)->get(j+1).abundance;
			if(i > 0)
				abundDiff += groups.at(i-1)->get(j+1).abundance;

			cArray[j] += abundDiff;
			numInd += abundDiff;	
		}
		
		double curD = 1/simpson(cArray, numInd, numBins);
		pseudoVals[i] = (double)numGroups*(baseD - curD) + curD;
		jackknifeEstimate += pseudoVals[i];
	}
	jackknifeEstimate /= (double)numGroups;
		
	double variance = 0;
	for(int i = 0; i < numGroups; i++)
		variance += pow(pseudoVals[i]-jackknifeEstimate, 2);
		
	variance /= (double)numGroups*((double)numGroups-1);
	double stErr = sqrt(variance);
	TDTable table;
	double confLimit = 0;
	if(numGroups <= 30)
		confLimit = table.getConfLimit(numGroups-1, 1);
	else
		confLimit = 1.645;
	
	confLimit *= stErr;
	
	double* rdata = new double[3];
	rdata[0] = baseD;
	rdata[1] = jackknifeEstimate - confLimit;
	rdata[2] = jackknifeEstimate + confLimit;
	
	return rdata;
}

/************************************************************************************************
************************************************************************************************/

EstOutput SharedJackknife::getValues(vector<SharedRAbundVector*> vectorShared){ //Fix this for collect, mistake was that it was made with summary in mind.
	try {
		SharedRAbundVector* shared1 = vectorShared[0];
		SharedRAbundVector* shared2 = vectorShared[1];
		if(numGroups == -1) {
			numGroups = m->getNumGroups();
		}

		if(callCount == numGroups*(numGroups-1)/2) {
			currentCallDone = true;
			callCount = 0;
		}
		callCount++;

		if(currentCallDone) {
			groups.clear();	
			currentCallDone = false;
		}
		
		if(groups.size() != numGroups) {	
			if(groups.size() == 0)
				groups.push_back(shared1);
			groups.push_back(shared2);
		}
		
		if(groups.size() == numGroups && callCount < numGroups) {
			data.resize(3,0);

			double* rdata = jackknife();
			data[0] = rdata[0];
			data[1] = rdata[1];
			data[2] = rdata[2];
		
			if (isnan(data[0]) || isinf(data[0])) { data[0] = 0; }
			if (isnan(data[1]) || isinf(data[1])) { data[1] = 0; }
			if (isnan(data[2]) || isinf(data[0])) { data[2] = 0; }
			
			return data;
		}
		
		data.resize(3,0);
		data[0] = 0;
		data[1] = 0;
		data[2] = 0;
		
		if (isnan(data[0]) || isinf(data[0])) { data[0] = 0; }
		if (isnan(data[1]) || isinf(data[1])) { data[1] = 0; }
		if (isnan(data[2]) || isinf(data[2])) { data[2] = 0; }
		return data;	
	}
		
	catch(exception& e) {
		m->errorOut(e, "SharedJackknife", "getValues");
		exit(1);
	}
}

/***********************************************************************/