Compute allele frequencies (p, q), genotype frequencies, test for Hardy‑Weinberg equilibrium (HWE) using chi‑square, and visualize observed vs. expected distributions.
The allele frequency (or gene frequency) is the relative proportion of a particular allele among all allele copies in a population. For a biallelic locus with alleles A and a, the frequencies are denoted p (frequency of A) and q (frequency of a), with p + q = 1. The Hardy‑Weinberg principle states that under ideal conditions (random mating, no mutation, no selection, large population size, no migration), genotype frequencies remain constant and follow: p² (AA) + 2pq (Aa) + q² (aa) = 1. Deviation from HWE indicates evolutionary forces at play.
Allele frequency calculation:
p = (2 × count(AA) + count(Aa)) / (2 × total individuals)
q = (2 × count(aa) + count(Aa)) / (2 × total individuals) = 1 − p
Expected genotype counts under HWE:
E(AA) = p² × N, E(Aa) = 2pq × N, E(aa) = q² × N
Chi‑square test (df=1): χ² = Σ (O−E)²/E, p‑value = 1 - CDF(χ², df=1) where CDF is the cumulative distribution function of chi‑square distribution. This implementation uses the complementary error function approximation (Abramowitz & Stegun) for high precision.
Population geneticists use allele frequency calculators to monitor genetic diversity in endangered species, detect natural selection signatures, assess forensic DNA evidence, and understand disease allele distributions (e.g., CFTR mutations in cystic fibrosis). Conservation biologists track loss of rare alleles over time. In agriculture, HWE tests validate breeding populations and detect unintended selection.
In a West African population, observed genotype counts: AA = 340, Aa = 180, aa = 30 (total 550). p = (2*340+180)/1100 = 0.782, q = 0.218. Expected under HWE: AA = p²*550 = 336, Aa = 2pq*550 = 187, aa = q²*550 = 26. χ² = 0.05+0.26+0.62 ≈ 0.93, p=0.33 → not significant, indicating HWE holds, consistent with random mating despite heterozygote advantage (balancing selection may be counterbalanced by other factors). This tool replicates such analyses.