Skip to content

Commit

Permalink
update one-sided fisher test to two-sided
Browse files Browse the repository at this point in the history
  • Loading branch information
Bryan Lai authored and dippindots committed Jul 20, 2023
1 parent 7339191 commit fae6d16
Show file tree
Hide file tree
Showing 10 changed files with 62 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,13 @@
import org.mskcc.cbio.portal.stats.FisherExact;

/**
* Calculate the cumulative (one-tail) p-value out of fisher exact test
* Calculate the cumulative (two-tail) p-value out of 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 or left, whichever is smallest)
* @return two-tailed P-value (right or left, whichever is smallest)
*
*/
public class CalcFisherExactTest extends HttpServlet {
Expand Down Expand Up @@ -86,7 +86,7 @@ protected void doPost(HttpServletRequest httpServletRequest,
int c = Integer.parseInt(dataSet.split(" ")[2]);
int d = Integer.parseInt(dataSet.split(" ")[3]);
FisherExact fisher = new FisherExact(a + b + c + d);
double pValue = fisher.getCumlativeP(a, b, c, d);
double pValue = fisher.getTwoTailedP(a, b, c, d);
result = result.concat(String.valueOf(pValue) + " ");
}
}
Expand Down
4 changes: 2 additions & 2 deletions core/src/main/java/org/mskcc/cbio/portal/stats/OddsRatio.java
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public OddsRatio(ProfileDataSummary pDataSummary, String geneA, String geneB) {

oddsRatio = ((double) (a * d)) / ((double) (b * c));
FisherExact fisher = new FisherExact(a + b + c + d);
pValue = fisher.getCumlativeP(a, b, c, d);
pValue = fisher.getTwoTailedP(a, b, c, d);
lowerConfidenceInterval = Math.exp(Math.log(oddsRatio) - 1.96 * (Math.sqrt(1 / (double) a
+ 1 / (double) b + 1 / (double) c + 1 / (double) d)));
upperConfidenceInterval = Math.exp(Math.log(oddsRatio) + 1.96 * (Math.sqrt(1 / (double) a
Expand All @@ -103,7 +103,7 @@ public double getOddsRatio() {
return oddsRatio;
}

public double getCumulativeP() {
public double getTwoTailedP() {
return pValue;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -652,7 +652,7 @@ private double runFisherExactTest(HashMap<Integer, String> singleGeneCaseValueMa
}
}
FisherExact fisher = new FisherExact(a + b + c + d);
return fisher.getCumlativeP(a, b, c, d);
return fisher.getTwoTailedP(a, b, c, d);
}

private String getCytoband(int geneticEntityId, String geneticProfileStableId) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public class TestFisherExact extends TestCase {

public void testFisherExact() {
FisherExact fisher = new FisherExact(43 + 2 + 17 + 7);
double p = fisher.getCumlativeP(43, 2, 17, 7);
double p = fisher.getTwoTailedP(43, 2, 17, 7);
assertEquals(0.006653, p, 0.00001);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,9 @@ public void testFisherExact() throws IOException {

OddsRatio oddsRatio = new OddsRatio(pDataSummary, "BRCA1", "BRCA2");
double oddsRatioValue = oddsRatio.getOddsRatio();
double p = oddsRatio.getCumulativeP();
double p = oddsRatio.getTwoTailedP();
assertEquals(0.16666, oddsRatioValue, 0.0001);
assertEquals(0.0849, p, 0.0001);
assertEquals(0.1698, p, 0.0001);
//System.out.println (oddsRatio.getRCommand());
}
}
2 changes: 1 addition & 1 deletion docs/user-guide/faq.md
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ Group Comparison is a suite of analysis features which allows a user to compare
- Other tabs
- 2 groups
- Continuous data: one-sided t-test
- Categorical data: one-sided Fisher's exact test
- Categorical data: two-sided Fisher's exact test
- 3 or more groups
- Continuous data: one-way ANOVA
- Categorical data: Chi-squared test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1034,7 +1034,7 @@ public void getPatientStructuralVariantCountIncludeCustomDriverAnnotationsInclud
alterationFilter.setStructuralVariants(true);
alterationFilter.setMutationTypeSelect(Select.none());
alterationFilter.setCnaTypeSelect(Select.none());
alterationFilter.setSelectedTiers(Select.byValues(List.of("Class 2")));
alterationFilter.setSelectedTiers(Select.byValues(Arrays.asList("Class 2")));
alterationFilter.setIncludeUnknownTier(true);
List<AlterationCountByGene> result = alterationMyBatisRepository.getPatientAlterationGeneCounts(
svPatientIdToProfileId,
Expand All @@ -1049,7 +1049,7 @@ public void getPatientStructuralVariantCountIncludeCustomDriverAnnotationsExclud
alterationFilter.setStructuralVariants(true);
alterationFilter.setMutationTypeSelect(Select.none());
alterationFilter.setCnaTypeSelect(Select.none());
alterationFilter.setSelectedTiers(Select.byValues(List.of("Class 2")));
alterationFilter.setSelectedTiers(Select.byValues(Arrays.asList("Class 2")));
alterationFilter.setIncludeUnknownTier(false);
List<AlterationCountByGene> result = alterationMyBatisRepository.getPatientAlterationGeneCounts(
svPatientIdToProfileId,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ public List<AlterationEnrichment> createAlterationEnrichments(Map<String, Pair<L
int alteredOnlyInQueryGenesCount = counts.get(0).getProfiledCount()
- counts.get(0).getAlteredCount();

pValue = fisherExactTestCalculator.getCumulativePValue(alteredInNoneCount,
pValue = fisherExactTestCalculator.getTwoTailedPValue(alteredInNoneCount,
counts.get(1).getAlteredCount(), alteredOnlyInQueryGenesCount,
counts.get(0).getAlteredCount());
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,51 @@ public double getCumulativePValue(int a, int b, int c, int d) {
}
return p;
}

public double getTwoTailedPValue(int a, int b, int c, int d) {

int min, i;
int n = a + b + c + d;
double p = 0;
double[] f = new double[n + 1];
f[0] = 0.0;

for (int j = 1; j <= n; j++) {
f[j] = f[j - 1] + Math.log(j);
}

double baseP = getPValue(a, b, c, d, f);
// 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).

int initialA = a, initialB = b, initialC = c, initialD = d;
p += baseP;

min = (c < b) ? c : b;
for (i = 0; i < min; i++) {
double tempP = getPValue(++a, --b, --c, ++d, f);
if (tempP <= baseP) {
p += tempP;
}
}

// reset the values to their original so we can repeat this process for the other side
a = initialA;
b = initialB;
c = initialC;
d = initialD;

min = (a < d) ? a : d;
for (i = 0; i < min; i++) {
double pTemp = getPValue(--a, ++b, ++c, --d, f);
if (pTemp <= baseP) {
p += pTemp;
}
}
return p;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,8 @@ public void createAlterationEnrichments() throws Exception {

// START: for 2 groups

Mockito.when(fisherExactTestCalculator.getCumulativePValue(1, 1, 2, 0)).thenReturn(1.0);
Mockito.when(fisherExactTestCalculator.getCumulativePValue(2, 0, 0, 2)).thenReturn(0.3);
Mockito.when(fisherExactTestCalculator.getTwoTailedPValue(1, 1, 2, 0)).thenReturn(1.0);
Mockito.when(fisherExactTestCalculator.getTwoTailedPValue(2, 0, 0, 2)).thenReturn(0.3);

List<AlterationEnrichment> result = alterationEnrichmentUtil.createAlterationEnrichments(
mutationCountsbyEntrezGeneIdAndGroup);
Expand Down

0 comments on commit fae6d16

Please sign in to comment.