Hardy-Weinberg Equilibrium Calculation
When to Use
- Calculate expected genotype frequencies under HWE
- Compute expected heterozygosity
- Analyze population genetics data
Input Format
File /root/input.txt:
- AA: Number of homozygous reference
- Aa: Number of heterozygous
- aa: Number of homozygous alternate
Before calculating, read /root/input.txt and extract the provided AA, Aa, and aa genotype counts exactly as written. Parse them into numeric variables before doing any calculations; do not assume values or ordering without checking the file contents.
Output Format
To /root/output.txt:
Expected AA: X
Expected Aa: X
Expected aa: X
Expected heterozygosity: X.XXXX
Observed heterozygosity: X.XXXX
Round to 4 decimal places.
Write the completed result block to /root/output.txt using the exact five labels shown above, in that order, with all numeric values rounded to 4 decimal places.
Calculation Workflow
Calculation Workflow
- Compute total individuals:
N = AA + Aa + aa. - Verify
N > 0before dividing. - Compute allele frequencies from genotype counts:
p = (2*AA + Aa) / (2*N)q = (2*aa + Aa) / (2*N)(equivalently,q = 1 - p)
- Compute expected genotype counts under HWE:
Expected AA = p^2 * NExpected Aa = 2*p*q * NExpected aa = q^2 * N
- Compute heterozygosity values:
Expected heterozygosity = 2*p*qObserved heterozygosity = Aa / N
- Write results to
/root/output.txtusing the exact labels and order shown above.
Tips
- Verify
N = AA + Aa + aabefore calculating, and handle the edge caseN = 0before dividing - Compute heterozygosity separately from genotype counts: expected heterozygosity
= 2*p*q, observed heterozygosity= Aa / N - Do not confuse expected heterozygosity (a proportion) with expected heterozygote count
2*p*q*N - Preserve the input mapping exactly:
AA,Aa, andaaare genotype counts, not allele counts - Sanity-check that
p + q = 1within small floating-point error, and that expected counts sum toNup to rounding - Prefer a short script to parse
/root/input.txt, perform all calculations, and write/root/output.txt - Match the output labels, line order, and numeric formatting exactly as shown above; format every reported value to 4 decimal places
- After writing
/root/output.txt, read it back once to verify all five lines, labels, order, values, and formatting