Skip to content
This repository was archived by the owner on May 22, 2026. It is now read-only.

Commit b217c6d

Browse files
committed
Added integration test for fisher option (compare with epsilon value). Fixed scientific notation of numbers in expected VCF.
1 parent 6fbdcef commit b217c6d

6 files changed

Lines changed: 155 additions & 84 deletions

File tree

src/main/java/com/astrazeneca/vardict/data/fishertest/FisherExact.java

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,11 @@ public class FisherExact {
3333
private double PvalueLess;
3434
private double PvalueGreater;
3535
private double PvalueTwoSided;
36-
3736
private List<Integer> support;
3837

38+
// Seems that Java and R have differences with round half even (JDK-8227248 example, it will round value in memory)
39+
public static double RESULT_ROUND_R = 1E5;
40+
3941
public FisherExact(int refFwd, int refRev, int altFwd, int altRev) {
4042
m = refFwd + refRev;
4143
n = altFwd + altRev;
@@ -47,8 +49,8 @@ public FisherExact(int refFwd, int refRev, int altFwd, int altRev) {
4749
for (int j = lo; j <= hi; j++) {
4850
support.add(j);
4951
}
50-
5152
logdc = logdcDhyper(m, n, k);
53+
5254
calculatePValue();
5355
}
5456

@@ -66,7 +68,7 @@ private List<Double> logdcDhyper(int m, int n, int k) {
6668
if (value.isNaN()) {
6769
value = 0.0;
6870
}
69-
logdc.add(value);
71+
logdc.add(roundHalfEven("0.0000000", value));
7072
}
7173
return logdc;
7274
}
@@ -129,14 +131,12 @@ public String getOddRatio() {
129131
} else if (oddRatio == Math.round(oddRatio)) {
130132
return new DecimalFormat("0").format(oddRatio);
131133
} else {
132-
return String.valueOf(roundHalfEven("0.00000", oddRatio));
134+
return String.valueOf(round_as_r(oddRatio));
133135
}
134136
}
135137

136138
public double getPValue() {
137-
double pValue = PvalueTwoSided;
138-
double pValueRound = roundHalfEven("0.00000", pValue);
139-
return pValueRound == 0.0 ? 0 : (pValueRound == 1.0 ? 1 : pValueRound);
139+
return round_as_r(PvalueTwoSided);
140140
}
141141

142142
public List<Double> getLogdc() {
@@ -145,15 +145,18 @@ public List<Double> getLogdc() {
145145
}
146146

147147
public double getPValueGreater() {
148-
double pValueGreater = PvalueGreater;
149-
double pValueRound = roundHalfEven("0.00000", pValueGreater);
150-
return pValueRound == 0.0 ? 0 : (pValueRound == 1.0 ? 1 : pValueRound);
148+
return round_as_r(PvalueGreater);
151149
}
152150

153151
public double getPValueLess() {
154-
double pValueLess = PvalueLess;
155-
double pValueRound = roundHalfEven("0.00000", pValueLess);
156-
return pValueRound == 0.0 ? 0 : (pValueRound == 1.0 ? 1 : pValueRound);
152+
return round_as_r(PvalueLess);
153+
}
154+
155+
private double round_as_r(double value) {
156+
value = roundHalfEven("0", value * RESULT_ROUND_R);
157+
value = value/RESULT_ROUND_R;
158+
value = value == 0.0 ? 0 : (value == 1.0 ? 1 : value);
159+
return value;
157160
}
158161

159162
private void calculatePValue() {

src/test/java/com/astrazeneca/vardict/data/fishertest/FisherExact_Test.java

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,21 @@
77
import java.util.ArrayList;
88
import java.util.List;
99

10+
import static com.astrazeneca.vardict.Utils.roundHalfEven;
11+
1012
public class FisherExact_Test {
1113
@Test
1214
public void test_logdc() {
1315
FisherExact fisher_test = new FisherExact(11, 12, 1, 2);
1416
List<Double> logdc = fisher_test.getLogdc();
1517
List<Double> expected = new ArrayList<Double>() {{
16-
add(-2.469639177657212);
17-
add(-1.0345546523678895);
18-
add(-0.867500567704723);
19-
add(-1.9661128563728325);
18+
add(-2.4696392);
19+
add(-1.0345547);
20+
add(-0.8675006);
21+
add(-1.9661129);
2022
}};
2123
Assert.assertTrue(logdc.equals(expected));
22-
// -2.4696392 -1.0345547 -0.8675006 -1.966112
24+
// -2.4696392 -1.0345547 -0.8675006 -1.9661129
2325
}
2426

2527
@DataProvider(name = "counts")
@@ -37,26 +39,29 @@ public Object[][] counts() {
3739
{ new FisherExact(10, 10, 10, 1), 0.04722, 0.02599, 0.99802, 0.10703 },
3840
{ new FisherExact(10, 10, 10, 0), 0.01099, 0.00615, 1.0, 0.0 },
3941
{ new FisherExact(69,1, 74, 95), 0.0, 1.0, 0.0, 87.68597 },
42+
{ new FisherExact(41, 86, 1, 1), 0.54688, 0.54687, 0.89571, 0.47973 },
43+
{ new FisherExact(130, 189, 1, 0), 0.40937, 0.40937, 1.0, 0.0 }, // R: 0.409375 round to 0.40938 in both
44+
{ new FisherExact(83, 40, 1, 2), 0.25746, 0.96473, 0.25746, 4.09908 },
45+
{ new FisherExact(74, 117, 1, 0), 0.39062, 0.39063, 1.0, 0.0 },
46+
{ new FisherExact(60, 62, 2, 0), 0.49593, 0.24797, 1.0, 0.0 },
47+
{ new FisherExact(43, 83, 1, 1), 1.0, 0.57111, 0.88361, 0.52091 },
48+
{ new FisherExact(78, 40, 1, 1), 1.0, 0.88515, 0.56849, 1.93844 },
4049
};
4150
}
4251

4352
@Test(dataProvider = "counts")
4453
public void test_fexact(FisherExact fisherExact,
4554
double pvalue, double pvalueLess, double pValueGreater, double oddRatio) {
4655
double p = fisherExact.getPValue();
47-
System.err.println(p);
48-
assert p == pvalue;
56+
assert roundHalfEven("0.00000", p) == pvalue;
4957

5058
p = fisherExact.getPValueLess();
51-
System.err.println(p);
52-
assert p == pvalueLess;
59+
assert roundHalfEven("0.00000", p) == pvalueLess;
5360

5461
p = fisherExact.getPValueGreater();
55-
System.err.println(p);
56-
assert p == pValueGreater;
62+
assert roundHalfEven("0.00000", p) == pValueGreater;
5763

5864
String odd = fisherExact.getOddRatio();
59-
System.err.println(odd);
60-
assert Double.valueOf(odd) == oddRatio;
65+
assert roundHalfEven("0.00000", Double.valueOf(odd)) == oddRatio;
6166
}
6267
}

0 commit comments

Comments
 (0)