-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathFisherExact.java
317 lines (297 loc) · 10.8 KB
/
FisherExact.java
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
/*
* Copyright (c) 2015 Memorial Sloan-Kettering Cancer Center.
*
* This library is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY, WITHOUT EVEN THE IMPLIED WARRANTY OF MERCHANTABILITY OR FITNESS
* FOR A PARTICULAR PURPOSE. The software and documentation provided hereunder
* is on an "as is" basis, and Memorial Sloan-Kettering Cancer Center has no
* obligations to provide maintenance, support, updates, enhancements or
* modifications. In no event shall Memorial Sloan-Kettering Cancer Center be
* liable to any party for direct, indirect, special, incidental or
* consequential damages, including lost profits, arising out of the use of this
* software and its documentation, even if Memorial Sloan-Kettering Cancer
* Center has been advised of the possibility of such damage.
*/
/*
* This file is part of cBioPortal.
*
* cBioPortal is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package tappas;
/**
* This does a Fisher Exact test. The Fisher's Exact test procedure calculates an exact probability value
* for the relationship between two dichotomous variables, as found in a two by two crosstable. The program
* calculates the difference between the data observed and the data expected, considering the given marginal
* and the assumptions of the model of independence. It works in exactly the same way as the Chi-square test
* for independence; however, the Chi-square gives only an estimate of the true probability value, an estimate
* which might not be very accurate if the marginal is very uneven or if there is a small value (less than five)
* in one of the cells.
* <p/>
* It uses an array of factorials initialized at the beginning to provide speed.
* There could be better ways to do this.
*
* @author Ed Buckler
* @version $Id: FisherExact.java,v 1
*/
public class FisherExact {
private static final boolean DEBUG = false;
private double[] f;
int maxSize;
/**
* constructor for FisherExact table
*
* @param maxSize is the maximum sum that will be encountered by the table (a+b+c+d)
*/
public FisherExact(int maxSize) {
this.maxSize = maxSize;
double cf = 1.0;
f = new double[maxSize + 1];
f[0] = 0.0;
for (int i = 1; i <= this.maxSize; i++) {
f[i] = f[i - 1] + Math.log(i);
}
}
/**
* calculates the P-value for this specific state
*
* @param a a, b, c, d are the four cells in a 2x2 matrix
* @param b
* @param c
* @param d
* @return the P-value
*/
public final double getP(int a, int b, int c, int d) {
int n = a + b + c + d;
if (n > maxSize) {
return Double.NaN;
}
double p;
p = (f[a + b] + f[c + d] + f[a + c] + f[b + d]) - (f[a] + f[b] + f[c] + f[d] + f[n]);
return Math.exp(p);
}
/**
* Calculates the one-tail P-value for the Fisher Exact test. Determines whether to calculate the right- or left-
* tail, thereby always returning the smallest p-value.
*
* @param a a, b, c, d are the four cells in a 2x2 matrix
* @param b
* @param c
* @param d
* @return one-tailed P-value (right or left, whichever is smallest)
*/
public final double getCumlativeP(int a, int b, int c, int d) {
int min, i;
int n = a + b + c + d;
if (n > maxSize) {
return Double.NaN;
}
double p = 0;
p += getP(a, b, c, d);
if (DEBUG) {
System.out.println("p = " + p);
}
if ((a * d) >= (b * c)) {
if (DEBUG) {
System.out.println("doing R-tail: a=" + a + " b=" + b + " c=" + c + " d=" + d);
}
min = (c < b) ? c : b;
for (i = 0; i < min; i++) {
if (DEBUG) {
System.out.print("doing round " + i);
}
p += getP(++a, --b, --c, ++d);
if (DEBUG) {
System.out.println("\ta=" + a + " b=" + b + " c=" + c + " d=" + d);
}
}
}
if ((a * d) < (b * c)) {
if (DEBUG) {
System.out.println("doing L-tail: a=" + a + " b=" + b + " c=" + c + " d=" + d);
}
min = (a < d) ? a : d;
for (i = 0; i < min; i++) {
if (DEBUG) {
System.out.print("doing round " + i);
}
double pTemp = getP(--a, ++b, ++c, --d);
if (DEBUG) {
System.out.print("\tpTemp = " + pTemp);
}
p += pTemp;
if (DEBUG) {
System.out.println("\ta=" + a + " b=" + b + " c=" + c + " d=" + d);
}
}
}
return p;
}
/**
* Calculates the right-tail P-value for the Fisher Exact test.
*
* @param a a, b, c, d are the four cells in a 2x2 matrix
* @param b
* @param c
* @param d
* @return one-tailed P-value (right-tail)
*/
public final double getRightTailedP(int a, int b, int c, int d) {
int min, i;
int n = a + b + c + d;
if (n > maxSize) {
return Double.NaN;
}
double p = 0;
p += getP(a, b, c, d);
if (DEBUG) {
System.out.println("p = " + p);
}
if (DEBUG) {
System.out.println("doing R-tail: a=" + a + " b=" + b + " c=" + c + " d=" + d);
}
min = (c < b) ? c : b;
for (i = 0; i < min; i++) {
p += getP(++a, --b, --c, ++d);
}
return p;
}
/**
* Calculates the left-tail P-value for the Fisher Exact test.
*
* @param a a, b, c, d are the four cells in a 2x2 matrix
* @param b
* @param c
* @param d
* @return one-tailed P-value (left-tail)
*/
public final double getLeftTailedP(int a, int b, int c, int d) {
int min, i;
int n = a + b + c + d;
if (n > maxSize) {
return Double.NaN;
}
double p = 0;
p += getP(a, b, c, d);
if (DEBUG) {
System.out.println("p = " + p);
}
if (DEBUG) {
System.out.println("doing L-tail: a=" + a + " b=" + b + " c=" + c + " d=" + d);
}
min = (a < d) ? a : d;
for (i = 0; i < min; i++) {
if (DEBUG) {
System.out.print("doing round " + i);
}
double pTemp = getP(--a, ++b, ++c, --d);
if (DEBUG) {
System.out.print("\tpTemp = " + pTemp);
}
p += pTemp;
if (DEBUG) {
System.out.println("\ta=" + a + " b=" + b + " c=" + c + " d=" + d);
}
}
return p;
}
/**
* Calculates the two-tailed P-value for the Fisher Exact test.
* <p/>
* In order for a table under consideration to have its p-value included
* in the final result, it must have a p-value less than the original table's P-value, i.e.
* Fisher's exact test computes the probability, given the observed marginal
* frequencies, of obtaining exactly the frequencies observed and any configuration more extreme.
* By "more extreme," we mean any configuration (given observed marginals) with a smaller probability of
* occurrence in the same direction (one-tailed) or in both directions (two-tailed).
*
* @param a a, b, c, d are the four cells in a 2x2 matrix
* @param b
* @param c
* @param d
* @return two-tailed P-value
*/
public final double getTwoTailedP(int a, int b, int c, int d) {
int min, i;
int n = a + b + c + d;
if (n > maxSize) {
return Double.NaN;
}
double p = 0;
double baseP = getP(a, b, c, d);
// in order for a table under consideration to have its p-value included
// in the final result, it must have a p-value less than the baseP, i.e.
// Fisher's exact test computes the probability, given the observed marginal
// frequencies, of obtaining exactly the frequencies observed and any configuration more extreme.
// By "more extreme," we mean any configuration (given observed marginals) with a smaller probability of
// occurrence in the same direction (one-tailed) or in both directions (two-tailed).
if (DEBUG) {
System.out.println("baseP = " + baseP);
}
int initialA = a, initialB = b, initialC = c, initialD = d;
p += baseP;
if (DEBUG) {
System.out.println("p = " + p);
}
if (DEBUG) {
System.out.println("Starting with R-tail: a=" + a + " b=" + b + " c=" + c + " d=" + d);
}
min = (c < b) ? c : b;
for (i = 0; i < min; i++) {
if (DEBUG) {
System.out.print("doing round " + i);
}
double tempP = getP(++a, --b, --c, ++d);
if (tempP <= baseP) {
if (DEBUG) {
System.out.print("\ttempP (" + tempP + ") is less than baseP (" + baseP + ")");
}
p += tempP;
}
if (DEBUG) {
System.out.println(" a=" + a + " b=" + b + " c=" + c + " d=" + d);
}
}
// reset the values to their original so we can repeat this process for the other side
a = initialA;
b = initialB;
c = initialC;
d = initialD;
if (DEBUG) {
System.out.println("Now doing L-tail: a=" + a + " b=" + b + " c=" + c + " d=" + d);
}
min = (a < d) ? a : d;
if (DEBUG) {
System.out.println("min = " + min);
}
for (i = 0; i < min; i++) {
if (DEBUG) {
System.out.print("doing round " + i);
}
double pTemp = getP(--a, ++b, ++c, --d);
if (DEBUG) {
System.out.println(" pTemp = " + pTemp);
}
if (pTemp <= baseP) {
if (DEBUG) {
System.out.print("\ttempP (" + pTemp + ") is less than baseP (" + baseP + ")");
}
p += pTemp;
}
if (DEBUG) {
System.out.println(" a=" + a + " b=" + b + " c=" + c + " d=" + d);
}
}
return p;
}
}