Skip to content

EProportions · Guide test 303

McDonald–Kreitman test

Compares synonymous and nonsynonymous polymorphism versus divergence to assess departure from a neutral evolutionary model.

Runs in CodeSplash (Experimental)Python example validatedR example validated 2 corrections in edition 2

Do it in CodeSplash

In the app, set it up in Analyze with this method:

  • McDonald–Kreitman test — Is the ratio of protein-changing to silent changes the same within species as between them?

In McDonald–Kreitman test, choose Test = Fisher's exact test or choose Test = G-test or choose Test = Pearson χ².

Do it in CodeSplash Run this test on your own data, in Python or R.

Bench

Change the design, drag the data, move α. The statistic, its reference distribution and the verdict follow.

Bench · McDonald–Kreitman test

30
40
0.55
0.30
0.050

Alternative

Your data

Edit the counts
SuccessFailureTotal
Row 1
14E = 11.6
16E = 18.4
30
Row 2
13E = 15.4
27E = 24.6
40
Total274370

E is the count expected if rows and columns were unrelated.

If H₀ were true

a = 14
  1. The reference: how a varies across samples when H₀ is true.
  2. Your statistic, from the data above.
  3. The p-value: the total probability of outcomes no more likely than yours (p = .321).
  4. The hatched rejection region at α = .050: statistics landing here reject H₀.

The formula

P(a)=(a+ba)(c+dc)(na+c) (Fisher’s exact test on the 2 × 2 table)P(a) = \dfrac{\binom{a+b}{a}\binom{c+d}{c}}{\binom{n}{a+c}}\ \text{(Fisher's exact test on the 2 × 2 table)}

With your numbers

a=14, b=16, c=13, d=27a = 14,\ b = 16,\ c = 13,\ d = 27
a, ba,\ b
nonsynonymous: fixed, polymorphic
c, dc,\ d
synonymous: fixed, polymorphic

The verdict

a = 14, p = .321, OR = 1.80

p > α = .050: compatible with H₀

Effect size: conditional MLE odds ratio OR = 1.802

An excess of nonsynonymous fixed differences points to adaptive evolution. Rows: nonsynonymous, synonymous; columns: fixed, polymorphic, so this bench's odds ratio is (D_N·P_S)/(P_N·D_S), the reference's orientation. CodeSplash's headline odds ratio is the reciprocal, (P_N·D_S)/(P_S·D_N), oriented like the neutrality index (or_cmle); the reference-oriented ratio is reported as or_reference. Which tail a one-sided Fisher p-value tests depends on this orientation, and the result card says which.

Statistic
14.000
p-value
0.3213
McDonald–Kreitman test on a 2 × 2 table of 70 counts: a = 14, p = .321, OR = 1.80. The p-value is above α = .050, so the data are compatible with H0.

McDonald–Kreitman test on a 2 × 2 table of 70 counts: a = 14, p = .321, OR = 1.80. The p-value is above α = .050, so the data are compatible with H₀.

Question and hypotheses

H0: the nonsynonymous/synonymous ratio is the same for polymorphism and divergence under the neutral reference.

Assumptions

Reliable orthology, polymorphism and divergence classification; a suitable neutral synonymous reference and population model.

Formula

synonymousnonsynonymouspolymorphicPSPNfixedDSDNOR=DNPSDSPN\begin{gathered}\begin{array}{c|cc}&\text{synonymous}&\text{nonsynonymous}\\\text{polymorphic}&P_S&P_N\\\text{fixed}&D_S&D_N\end{array}\\[5pt] OR=\frac{D_NP_S}{D_SP_N}\end{gathered}

Parameters and notation

OR=1 under the contingency-table null; an associated adaptive fraction estimate is α=1−DS PN/(DN PS) when denominators are nonzero.

Structure of the method

Form the 2×2 table crossing polymorphic versus fixed differences with synonymous versus nonsynonymous status and test the ratio equality. Fisher’s exact test conditions on margins; likelihood variants make different approximations. Biological interpretation requires a defensible neutral reference, not merely a statistically associated table.

Estimated and nuisance parameters

Cell proportions, the nonsynonymous/synonymous odds ratio and optional adaptive-fraction summaries are estimated. These estimates depend on table orientation; the defined OR=DN·PS/(DS·PN) is obtained from rows polymorphic/fixed and columns synonymous/nonsynonymous. Nuisance sampling intensities are addressed by the chosen table model.

Settings and design choices

Prespecify orthology, sequence-quality filters, outgroup, functional classification, polymorphism-frequency cutoff and how multiple loci are combined. Low-frequency variant filtering changes the estimand and should not be selected to maximize evidence. Report exact-tail convention and any continuity or zero-cell correction.

Fitting and adaptation

Adaptation means fitting a biologically appropriate polymorphism/divergence model or using a named extension that accounts for demography and weakly deleterious variants. It is not neural fine-tuning. Compare defensible filtering/model choices transparently and retain uncertainty in any adaptive-fraction estimate.

Sensitivity and robustness

Weakly deleterious polymorphisms, selected synonymous sites, population history, linkage and errors in fixed-difference classification can change the result. Zero cells make simple ratios unstable. Rejection is compatible with several departures from the simple neutral table model and does not uniquely establish an exact adaptive substitution fraction.

How to use and calibrate

Use Fisher exact or a suitable contingency likelihood test; model-based extensions adjust frequency spectrum/demography.

Example application

Investigate evidence of adaptive protein evolution.

Interpretation and pitfalls

Watch out

Weakly deleterious polymorphisms, demography and nonneutral synonymous sites can bias interpretation. Table significance alone is not a unique selection diagnosis.

What changed in edition 2

  • New sections: structure of the method, estimated and nuisance parameters, settings and design choices, fitting and adaptation, sensitivity and robustness.
  • Rewritten in the review: Python code, R code.
  • Python codenature.com

    Reverse the original column ordering so the printed odds-ratio orientation matches the explicitly defined MK odds ratio; the two-sided p value is unchanged.

    Corrected Python code
    import numpy as np
    from scipy import stats
    # Rows: polymorphic, fixed. Columns: synonymous, nonsynonymous.
    # This orientation gives OR = D_N * P_S / (D_S * P_N).
    tab = np.array([[30, 12], [40, 8]])
    result = stats.fisher_exact(tab, alternative="two-sided")
    print("MK-oriented sample odds ratio:", result.statistic)
    print("two-sided p value:", result.pvalue)
    
  • R codenature.com

    Align table orientation with the displayed MK ratio and distinguish R conditional-MLE odds ratio from the sample cross-product estimate.

    Corrected R code
    # Rows: polymorphic, fixed. Columns: synonymous, nonsynonymous.
    # This orientation matches OR = D_N * P_S / (D_S * P_N).
    tab <- matrix(c(30,12,40,8),2,byrow=TRUE,
                  dimnames=list(c("polymorphic","fixed"),
                                c("synonymous","nonsynonymous")))
    result <- fisher.test(tab,alternative="two.sided")
    print(result)
    # R reports a conditional-MLE odds ratio, not the sample cross-product ratio.
    print(c(MK_sample_OR=tab[1,1]*tab[2,2]/(tab[1,2]*tab[2,1])))
    

Python and R

McDonald–Kreitman 2×2 comparison of nonsynonymous/synonymous polymorphisms and fixed differences; interpret under the relevant population-genetic assumptions.

Python · native

Validated — Executed successfully in the validation environment · needs numpy, scipy

import numpy as np
from scipy import stats
rng = np.random.default_rng(2026)
tab=np.array([[12,30],[8,40]])
print(stats.fisher_exact(tab))
Output
SignificanceResult(statistic=np.float64(2.0), pvalue=np.float64(0.20938506341940927))
R

Validated — Executed successfully in the validation environment · needs Base/standard library

tab<-matrix(c(12,30,8,40),2,byrow=TRUE,dimnames=list(c("polymorphic","fixed"),c("nonsynonymous","synonymous")));print(fisher.test(tab))
Output
	Fisher's Exact Test for Count Data

data:  tab
p-value = 0.2094
alternative hypothesis: true odds ratio is not equal to 1
95 percent confidence interval:
 0.6510192 6.3634915
sample estimates:
odds ratio 
  1.984466

Sources

Reference's record:
Edition 2 review: Python: executed · R: not executed (Rscript unavailable). Python: Ran in the reference's review, on this exact code. R: Reviewed but not run: R wasn't available to the reference's authors.

From the CodeSplash statistics guide (v4), entry 303, updated from the reference's edition 2 (reviewed 14 November 2026) as STAT303.