Module 4 · Variant quality control · step 4 of 6
4.4Hardy-Weinberg and sample size
Commands tested on Windows
In short
We test whether DNA letter combinations on chromosomes 1 to 22 follow expected proportions in people without the disease. We remove variants that fail a rule based on the number of people tested, to reduce possible measurement errors.
Do this
- Paste in the RStudio ConsoleThe R command in the Command section below
You should see: 167 variants are excluded, and 238,852 remain.
Command
Module setup, needed before this step
R: module setup
source("R/guided.R")cfg <- guided_init()Blue: PLINK option · Orange: value you can change · Green: file name
R command
Type or paste into the RStudio console.
1psam <- guided_table(paste0(guided_path(cfg, "4.3"), ".psam"))controls <- guided_list(psam$IID[psam$Disease == "1"], guided_path(cfg, "4.4", "controls.txt"), TRUE)2report <- guided_path(cfg, "4.4", "hardy")run_plink(cfg, c("--pfile", guided_path(cfg, "4.3"), "--keep", controls, "--chr", "1-22", "--hardy", "midp", "--out", report))3hwe <- guided_table(paste0(report, ".hardy"))n_control <- as.numeric(hwe$HOM_A1_CT) + as.numeric(hwe$HET_A1_CT) + as.numeric(hwe$TWO_AX_CT)cutoff <- 10^(-6 - 0.001 * pmax(0, n_control - 1000))4p <- as.numeric(hwe$MIDP)exclude <- guided_list(hwe$ID[!is.na(p) & p < cutoff], guided_path(cfg, "4.4", "exclude.txt"))5run_plink(cfg, c("--pfile", guided_path(cfg, "4.3"), "--exclude", exclude, "--make-pgen", "--out", guided_path(cfg, "4.4")))6retained <- guided_table(paste0(guided_path(cfg, "4.4"), ".pvar"))$IDstopifnot(!any(c("rs4988235", "rs12913832", "rs1426654") %in% retained))# Course bookkeeping: records the counts for the next step. Do not change.ops <- guided_replay_lists(cfg, "4.4", "4.3")guided_register(cfg, "4.4", "4.3", 1844, 238852, operations = ops)- Note 1: Read the sample table and list people without the disease.
- Note 2: Test expected DNA letter combinations on chromosomes 1 to 22.
- Note 3: Calculate a cutoff for each variant from the number of people tested.
- Note 4: List variants with test p-values below their cutoff.
- Note 5: Remove the listed variants from all people and save the remaining data.
- Note 6: Check that the three named variants are absent from the saved data.
You need
The dataset from step 4.3, which we call state 4.3, and its PSAM file. The PSAM file lists the samples and their phenotype. We do not use any association results to choose the cutoff.
What we do
We run an exact Hardy–Weinberg test in the controls only, using the mid-p version. Then we apply a threshold that depends on the sample size.
Why
Hardy–Weinberg equilibrium (HWE) predicts how common each genotype should be. Take one allele with frequency a. Then the three genotypes should have the proportions a^2, 2a(1-a) and (1-a)^2. This holds if people mate at random and a few other simple conditions are true. Here a is an allele frequency. It is not the p-value of the test.
A lack of heterozygotes (opens in a new tab) (people with two different alleles) can have three causes. It can come from genotype calling errors. It can come from parents who are relatives. It can come from a mix of populations in one sample (population stratification (opens in a new tab)).
We test the autosomes (opens in a new tab) only (chromosomes 1 to 22) and use only the controls. In this way a real disease effect in the cases is less likely to cause a removal.
The test is exact, which means it counts every possible outcome instead of using an approximation. An exact test on discrete counts is often too strict. The mid-p (opens in a new tab) version corrects part of this.
A larger sample can detect smaller departures from HWE. So one fixed cutoff does not suit every study. Our rule is set before the analysis: 10^(-6 - 0.001 * max(0, n_control - 1000)). Here n_control is the number of controls with a call at that variant. With 1,000 controls or fewer, the cutoff is 0.000001. With more controls, the cutoff becomes stricter.
This rule is a clear teaching choice. It is not a universal recommended threshold. Before you use it elsewhere, compare the distributions and look at the excess or deficit of heterozygotes.
What each option does
Command 1: test expected DNA letter combinations
| Option or modifier | What it does |
|---|---|
| --pfile | Reads the PGEN, PVAR and PSAM files named results/guided/s4-3_state. |
| --keep | Uses only people without the disease, listed in results/guided/s4-4_controls.txt, for this test. |
| --chr | Limits the test to chromosomes 1 to 22. |
| --hardy | Tests whether the shares of the three DNA letter combinations match those predicted from the allele frequencies when people mate at random. |
| Modifier of --hardy: midp | Uses the mid-p version of the exact test, which adjusts for the limited possible outcomes of whole-number counts. |
| --out | Starts the test report file names with results/guided/s4-4_hardy. |
Command 2: remove variants that fail the test
| Option or modifier | What it does |
|---|---|
| --pfile | Reads the PGEN, PVAR and PSAM files named results/guided/s4-3_state, including people with and without the disease. |
| --exclude | Removes variants listed in results/guided/s4-4_exclude.txt after R applies the cutoff 10^(-6 - 0.001 * max(0, n_control - 1000)), where n_control counts people without the disease who have a result at that variant. |
| --make-pgen | Saves the remaining data as PGEN, PVAR and PSAM files. |
| --out | Starts the output file names with results/guided/s4-4_state. |
Every command also uses --threads 2 (PLINK runs at most two tasks at the same time) and --memory 2048 (PLINK uses at most about 2 GB of memory).
PLINK terminal equivalent. Optional: you do not need it for the course.
Blue: PLINK option · Orange: value you can change · Green: file name
Terminal equivalent: plain PLINK 2
Select any option to see what it does. Do not paste terminal commands into the R console.
bin/plink2
bin/plink2
No option selected. Each underlined part of the command is an option you can open.
Use bin\plink2.exe on Windows.
Expected result
Show the expected result
167 variants are excluded, and 238,852 remain. The strong planted errors are not the only failures. Eight unplanted variants and seven altered duplicate copies also fail. Some mild errors still pass. rs4988235, rs12913832 and rs1426654 leave here. They cannot be used as surviving markers in module 5.
The tested PLINK version is defined in config.R and recorded in
software_versions.txt. Validation: executable chunks and the expected counts passed on Windows.
If something goes wrong
If something goes wrong
Stop at the first error. Check the input file named in the error message, and check the previous step. Keep your command and the console messages. Do not use files from a different version of the dataset. See troubleshooting (opens in a new tab).
What this check can reveal
It can reveal genotype calling errors and a mix of populations. The unplanted failures show that a HWE failure is not proof of a technical error. They also show that we must not rescue association loci after we have seen their p-values.
Quick check
Two groups each follow HWE but have different allele frequencies. If we pool them, can we see a deficit of heterozygotes?
Show the answer
Yes. Pooling can create a deficit even when there is no genotyping error.
Files created
The list of control IDs, the .hardy file, the exclusion list and s4-4_state.*.
Try other thresholds (optional)
Try other thresholds (optional)
Try it
Hardy-Weinberg equilibrium: HWE p-value cutoff
Each point starts from the same input (1,844 samples, 239,019 variants) and changes one threshold.
HWE p-value cutoff 1e-10: 127 variants removed.
| Threshold | Removed | Remaining |
|---|---|---|
| 1e-03 | 700 | 238,319 |
| 1e-04 | 273 | 238,746 |
| 1e-06 | 167 | 238,852 |
| 1e-08 | 150 | 238,869 |
| 1e-10 | 127 | 238,892 |
| course rule(course value) | 167 | 238,852 |
This box is not part of the main path. No later step uses it. It does not change your results.
The Hardy-Weinberg test gives a p-value for each variant in the controls. A smaller cutoff removes fewer variants. A larger cutoff removes more variants, including some that are only unusual. The course does not use one fixed cutoff. It uses the rule 10^(-6 - 0.001 * max(0, n_control - 1000)), where n_control is the number of controls tested at that variant. With 1,000 controls or fewer, the cutoff is 0.000001. Here there are 920 controls, so the course rule is the same as the fixed cutoff 1e-6. The trials below compare fixed cutoffs and the course rule.
# Optional. The trials use the table of tests that the step wrote. No PLINK run is needed.
cutoffs <- c(1e-3, 1e-4, 1e-6, 1e-8, 1e-10)
trial <- hwe_counts(guided_table(paste0(guided_path(cfg, "4.4", "hardy"), ".hardy")), cutoffs)
# The last row is the course rule. Its threshold is NA, so it is the course value.
trial$course_value <- is.na(trial$threshold)
trial
# Plot the removed variants for the fixed cutoffs. The course rule equals 1e-6 here.
plot_threshold_trials(trial, "Variants removed")
# Optional: add engine = "ggplot2" to plot_threshold_trials() to draw the plot with ggplot2 (install.packages("ggplot2") once first).
Plain PLINK 2 command for one trial. It writes only small files with the name that you give to --out, never a new dataset:
bin/plink2 --pfile results/guided/s4-3_state --keep results/guided/s4-4_controls.txt --chr 1-22 --hwe 1e-6 midp --make-just-pvar --out results/trials/s4-4_trial --threads 2 --memory 2048
The course does not use --hwe. It filters the table from --hardy midp in R, because its cutoff depends on the number of controls. The terminal command above uses a fixed cutoff. PLINK 2 can stop with an error when a cutoff is very strict for the sample size, unless you add a value k or the modifier keep-fewhet (read the --hwe entry of the PLINK 2 manual). PLINK 2 tests only founders by default.
The expected result of the trials:
| HWE p-value cutoff | Variants removed | Variants remaining |
|---|---|---|
| 1e-3 | 700 | 238,319 |
| 1e-4 | 273 | 238,746 |
| 1e-6 | 167 | 238,852 |
| 1e-8 | 150 | 238,869 |
| 1e-10 | 127 | 238,892 |
| course rule (course value) | 167 | 238,852 |
How to read it. At 1e-3 the number removed is 700, and at 1e-4 it is 273. At 1e-6 it is 167, and at 1e-10 it is 127. The number falls slowly after 1e-6. The variants that fail even at 1e-10 show the strongest departures. The variants removed only at the looser cutoffs are less clear, and some of them may be sound.
Expected plot. For HWE p-value cutoff, the horizontal axis shows the threshold and the vertical axis shows the number of variants removed. A red diamond marks the course value (course rule), where 167 variants are removed. The line joins the points in the order of the thresholds, so you can see how fast the count changes. The same plot with ggplot2 looks the same and has a light grid.