MGHome page

GWAS course

Course map

0 of 64 steps done

Start with step 0.1Install R

Saved in this browser only.

Step 3.5

Module 3 · Sample quality control · step 5 of 8

3.5Heterozygosity

Commands tested on Windows

In short

We check whether each sample has unusually many or few sites with two different DNA letters. We remove samples with unusually many such sites and flag those with unusually few for review, because an unusual value alone does not prove an error.

Do this

You should see: 13 wells with low F are removed.

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.

1report <- guided_path(cfg, "3.5", "het")2run_plink(cfg, c("--pfile", guided_path(cfg, "3.3"), "--extract", paste0(guided_path(cfg, "3.4", "prune"), ".prune.in"), "--het", "--out", report))3het <- guided_table(paste0(report, ".het"))f <- as.numeric(het$F)mean_f <- mean(f); sd_f <- sqrt(mean((f - mean_f)^2))4low <- het$IID[f < mean_f - 3 * sd_f]high <- het$IID[f > mean_f + 3 * sd_f]5guided_list(high, guided_path(cfg, "3.5", "flag_high.txt"), TRUE)remove_file <- guided_list(low, guided_path(cfg, "3.5", "remove.txt"), TRUE)6run_plink(cfg, c("--pfile", guided_path(cfg, "3.3"), "--remove", remove_file, "--make-pgen", "--out", guided_path(cfg, "3.5")))7print(c(mean_F = mean_f, SD_F = sd_f))print(c(low_removed = length(low), high_flagged = length(high)))# Course bookkeeping: records the counts for the next step. Do not change.ops <- guided_replay_lists(cfg, "3.5", "3.3")guided_register(cfg, "3.5", "3.4", 1950, 296360, operations = ops)
  1. Note 1: Choose the name for the report of matching DNA letters.
  2. Note 2: Measure matching DNA letters using the selected variants.
  3. Note 3: Read the report and calculate the average and spread of F.
  4. Note 4: Find samples with unusually low or high F values.
  5. Note 5: Save the high F review list and the low F removal list.
  6. Note 6: Save the dataset without the low F samples.
  7. Note 7: Print the average and spread of F, then the numbers of flagged samples as whole numbers.

You need

State 3.3 and the diagnostic subset (the pruned list) from step 3.4.

What we do

We look for samples with too many or too few heterozygous calls. We use only the pruned subset of sites for this.

Why

A heterozygote carries two different alleles at a site. PLINK's F statistic compares the homozygosity that we observe with the homozygosity that we expect from the allele frequencies.

A negative F can mean too much heterozygosity. One possible cause is contamination (opens in a new tab), which means that the sample is mixed with DNA from another person. A high positive F can come from parents who are related to each other. It can also come from population structure (opens in a new tab) (groups with different ancestry) or from technical effects.

We remove samples with an F below the mean minus three population standard deviations (opens in a new tab). The standard deviation (SD) shows how spread out the values are. We flag samples with an F above the mean plus three standard deviations for review.

In this module, a high F is not an automatic reason to exclude a sample.

What each option does

Command 1: measure matching DNA letters

Command 1: measure matching DNA letters. Each option, with the modifiers under the option they belong to.
Option or modifierWhat it does
--pfile

Reads the PGEN, PVAR and PSAM input files that share the given name prefix.

--extract

Uses only the variants named in the supplied variant list.

--het

Reports, for each sample, the observed and expected numbers of genotypes with two matching DNA letters, and calculates F from them. R reads the F values and calculates the review thresholds.

--out

Sets the shared beginning of the output file names.

Command 2: remove samples with unusually high heterozygosity

Command 2: remove samples with unusually high heterozygosity. Each option, with the modifiers under the option they belong to.
Option or modifierWhat it does
--pfile

Reads the PGEN, PVAR and PSAM input files that share the given name prefix.

--remove

Removes the samples named in the supplied sample list.

--make-pgen

Saves a new dataset as PGEN, PVAR and PSAM files.

--out

Sets the shared beginning of the output file names.

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.

Command 1 of 2

bin/plink2

Command 2 of 2

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

13 wells with low F are removed. Eight wells with high F are flagged and kept. The mean F is approximately 0.0078. The population SD is approximately 0.0298. 1,950 samples remain.

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. Your numbers should be the same.

If something goes wrong

If something goes wrong

Stop at the first error. Check the input file named in the message and the previous step. Keep the 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

Too much or too little heterozygosity. These are ideas to check, not proof of contamination or of related parents.

Quick check

Why does this course keep wells with high F for review, and not remove both ends automatically?

Show the answer

A high value can have a biological cause or an ancestry cause. A symmetric numerical threshold does not prove an error.

Files created

The .het report, the low-F removal list, the high-F review list and s3-5_state.*.

Try other thresholds (optional)

Try other thresholds (optional)

Try it

Heterozygosity: F cutoff (standard deviations)

Each point starts from the same input (1,963 samples, 296,360 variants) and changes one threshold.

What the chart shows
Heterozygosity: samples removed for F cutoff (standard deviations) from 2 to 4071422.5course value33.54F cutoff (standard deviations)

F cutoff (standard deviations) 3: 13 samples removed (course value).

Counts for every threshold tried. The course value is marked.
ThresholdRemovedRemainingFlagged
2131,95013
2.5131,95011
3(course value)131,9508
3.5131,9505
4131,9500

This box is not part of the main path. No later step uses it. It does not change your results.

The statistic F compares the homozygosity that we see in a sample with the homozygosity that we expect from the allele frequencies. A very low F means too many heterozygous calls, which can come from contamination. A very high F means too few, which can come from related parents or a different ancestry. The course removes samples with F more than 3 standard deviations (SD) below the mean. It only flags, and keeps, samples with F more than 3 SD above the mean. The trials change the number of SD from 2 to 4. They use the same F values.

# Optional. The trials use the table of F values that the step wrote. No PLINK run is needed.
multipliers <- c(2, 2.5, 3, 3.5, 4)
trial <- het_counts(guided_table(paste0(guided_path(cfg, "3.5", "het"), ".het")), multipliers)
trial$course_value <- trial$threshold == 3
trial
# Plot the removed samples. The course value is the large diamond.
plot_threshold_trials(trial, "Samples removed", log_x = FALSE)
# 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/s3-3_state --extract results/guided/s3-4_prune.prune.in --het --out results/trials/s3-5_trial --threads 2 --memory 2048

This command writes the F table (results/trials/s3-5_trial.het). The limit itself is not a PLINK option: the course calculates it in R from the table. To repeat a trial in R, change the number 3 in the rule for low and high at the step.

The expected result of the trials:

SD from the meanSamples removedSamples remainingSamples flagged
2131,95013
2.5131,95011
3 (course value)131,9508
3.5131,9505
4131,9500

How to read the two numbers. "Removed" counts wells with an F value below the mean by more than the chosen number of SD. They have too many heterozygous calls, so the course takes them out of the data. At the course value of 3 SD, 13 wells are removed. "Flagged" counts wells with an F value above the mean by more than the chosen number of SD. They have too few heterozygous calls. The course keeps them in the data and only lists them for review. At the course value, 8 wells are flagged. Now the table. The same 13 wells are removed at every limit from 2 to 4 SD. This group is clearly separate from the rest. The number of flagged wells changes from 13 to 0, so it depends more on the rule than on the data. Flagged wells are kept, so this choice does not change the cohort.

Expected plot. For F cutoff (standard deviations), the horizontal axis shows the threshold and the vertical axis shows the number of samples removed. A red diamond marks the course value (3), where 13 samples 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.

Under the hood & literature

Under the hood & literature

What the tool computes. For each sample, --het reports the observed number of homozygous genotypes (O(HOM)), the number expected from the allele frequencies (E(HOM)), the number of genotypes read (OBS_CT) and F = (O(HOM) - E(HOM)) / (OBS_CT - E(HOM)). F is near 0 when homozygosity matches expectation. It is positive with excess homozygosity and negative with excess heterozygosity. The expected count uses allele frequencies estimated from the samples in the file.

Why these values. The mean minus or plus three standard deviations is a convention. Here it gives limits of about -0.082 and +0.097. Fixed F limits and other estimators (fhat1 to fhat3, from GCTA) exist. The course removes only the low end and flags the high end for review, because a high F can have a biological or ancestry cause.

Pitfalls. Pooled allele frequencies mislead when ancestry is mixed. A sample from a small ancestry group tends to look too homozygous, and an admixed sample too heterozygous. Check ancestry (module 5) before you trust either tail. Contamination raises heterozygosity, but a low F alone does not prove it. The result also depends on the pruned set, because correlated variants distort the expectation.

References

  1. Purcell S et al. (2007) PLINK: a tool set for whole-genome association and population-based linkage analyses. Am J Hum Genet 81:559-575. PMID 17701901.
  2. Anderson CA et al. (2010) Data quality control in genetic case-control association studies. Nat Protoc 5:1564-1573. PMID 21085122.
  3. Yang J et al. (2011) GCTA: a tool for genome-wide complex trait analysis. Am J Hum Genet 88:76-82. PMID 21167468.
  4. Jun G et al. (2012) Detecting and estimating contamination of human DNA samples in sequencing and array-based genotype data. Am J Hum Genet 91:839-848. PMID 23103226.