MGHome page

GWAS course

Course map

0 of 64 steps done

Start with step 0.1Install R

Saved in this browser only.

Step 3.6

Module 3 · Sample quality control · step 6 of 8

3.6Relatedness and duplicates

Commands tested on Windows

In short

We compare pairs of samples to find repeated measurements and close relatives. We apply the stated removal rules so that repeated or closely related people do not distort the later association analysis.

Do this

You should see: There are 19 duplicate-class pairs and ten first-degree pairs.

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.6", "king")2run_plink(cfg, c("--pfile", guided_path(cfg, "3.5"), "--extract", paste0(guided_path(cfg, "3.4", "prune"), ".prune.in"),  "--make-king-table", "--king-table-filter", "0.0442", "--out", report))3kin <- guided_table(paste0(report, ".kin0"))kin <- kin[order(-as.numeric(kin$KINSHIP)), ]4metadata <- guided_metadata(cfg)remove <- character()5for (i in seq_len(nrow(kin))) {  a <- kin$IID1[i]; b <- kin$IID2[i]; value <- as.numeric(kin$KINSHIP[i])6  if (value > 0.354) {7    if (endsWith(a, "_R2") || endsWith(b, "_R2")) {      remove <- union(remove, if (endsWith(a, "_R2")) a else b)    } else {8      ai <- match(a, metadata$IID); bi <- match(b, metadata$IID)      same <- identical(unname(unlist(metadata[ai, c("SEX", "Age", "Disease")])),                        unname(unlist(metadata[bi, c("SEX", "Age", "Disease")])))9      remove <- union(remove, if (same) max(a, b) else c(a, b))    }10  } else if (value > 0.177 && !a %in% remove && !b %in% remove) remove <- union(remove, max(a, b))}11remove_file <- guided_list(remove, guided_path(cfg, "3.6", "remove.txt"), TRUE)12run_plink(cfg, c("--pfile", guided_path(cfg, "3.5"), "--remove", remove_file, "--make-pgen", "--out", guided_path(cfg, "3.6")))# Course bookkeeping: records the counts for the next step. Do not change.ops <- guided_replay_lists(cfg, "3.6", "3.5")guided_register(cfg, "3.6", "3.5", 1920, 296360, operations = ops)
  1. Note 1: Choose the name for the report comparing pairs of samples.
  2. Note 2: Compare pairs using the selected variants.
  3. Note 3: Read the pairs and put the closest matches first.
  4. Note 4: Read the sample details and start an empty removal list.
  5. Note 5: Review each pair to decide which samples to remove.
  6. Note 6: Treat the closest matches as repeated samples.
  7. Note 7: Remove the labelled repeat when its name ends in _R2.
  8. Note 8: Compare sex, age and disease status for unlabelled repeats.
  9. Note 9: Keep one matching record or remove both conflicting records.
  10. Note 10: Keep one person from each remaining closely related pair.
  11. Note 11: Write the removal list.
  12. Note 12: Save the dataset without the listed samples.

You need

State 3.5, the pruning list from step 3.4 and the source metadata from step 2.7.

What we do

We estimate the kinship of every pair of samples. We review the duplicates. We keep a set of unrelated samples for the main analysis.

Why

Kinship (opens in a new tab) measures how much DNA two people share, beyond what is normal in the population. Repeated measurements of one person violate an assumption of the simple logistic model that we use for association. Close relatives violate it too. The assumption is that the samples are independent, which means unrelated.

We use approximate KING (opens in a new tab) kinship values to sort the pairs. A value above 0.354 means a duplicate. A value above 0.177 means a first-degree (opens in a new tab) pair, such as a parent and a child, or full siblings. These are screening thresholds, not exact rules.

We apply these rules:

  • For a labelled technical repeat, we remove the repeat well.
  • For an unlabelled duplicate with conflicting metadata (opens in a new tab), we remove both records. Metadata are the facts recorded about a sample, such as sex, age and disease status.
  • For an unlabelled duplicate with matching metadata, we remove one record.
  • For a first-degree pair, we keep one person. We choose by a reproducible rule based on the ID, never by disease status.

What each option does

Command 1: compare pairs of samples

Command 1: compare pairs of samples. 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.

--make-king-table

Estimates how closely related each pair of samples is and writes a table.

--king-table-filter

Writes only pairs whose kinship score is above 0.0442. The kinship score measures how closely two people are related. This value is an approximate boundary for third-degree relatives such as first cousins.

--out

Sets the shared beginning of the output file names.

Command 2: remove repeated or closely related samples

Command 2: remove repeated or closely related samples. 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

There are 19 duplicate-class pairs and ten first-degree pairs. In total, 30 records are removed. 1,920 samples remain. Pairs can overlap, because one person can be in more than one pair. For this reason, the pair counts are not counts of unique people.

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

Technical repeats, unlabelled duplicates, conflicts in the metadata and close relatives. In real research, decide the retention policy before you look at the association results. The policy should consider consent, completeness of the phenotype and the study design.

Quick check

If we remove one record for every related pair, do we always get the right number of removals?

Show the answer

No. One person can be part of several pairs. Review the whole network of relationships and the records that are already removed.

Files created

The .kin0 report, the reviewed removal list and s3-6_state.*.

Try other thresholds (optional)

Try other thresholds (optional)

Try it

Relatedness: KING kinship cutoff

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

What the chart shows
Relatedness: samples removed for KING kinship cutoff from 0.0442 to 0.354017330.04420.0884course value0.1770.354KING kinship cutoff (log scale)

KING kinship cutoff 0.177: 30 samples removed (course value).

Counts for every threshold tried. The course value is marked.
ThresholdRemovedRemaining
0.0442301,920
0.0884301,920
0.177(course value)301,920
0.354201,930

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

The kinship coefficient estimates how much DNA two samples share. The usual KING cutoffs are 0.354 for repeated samples and identical twins, 0.177 for first-degree relatives, 0.0884 for second-degree relatives and 0.0442 for third-degree relatives (Manichaikul et al. 2010, doi:10.1093/bioinformatics/btq559). This step does not use --king-cutoff. It writes the table of pairs with --make-king-table and --king-table-filter 0.0442, and a rule in R decides who to remove. The trials change the cutoff for relatives. Pairs above 0.354 are always treated as repeats.

# Optional. The trials use the table of pairs that the step wrote. No PLINK run is needed.
cutoffs <- c(0.0442, 0.0884, 0.177, 0.354)
kin <- guided_table(paste0(guided_path(cfg, "3.6", "king"), ".kin0"))
trial <- data.frame(threshold = cutoffs, removed = kinship_counts(kin, guided_metadata(cfg), cutoffs))
trial$course_value <- trial$threshold == 0.177
trial
# Plot the removed samples. The course value is the large diamond.
plot_threshold_trials(trial, "Samples 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/s3-5_state --extract results/guided/s3-4_prune.prune.in --king-cutoff 0.177 --out results/trials/s3-6_trial --threads 2 --memory 2048

--king-cutoff removes one member of each pair above the cutoff with a greedy rule, and it writes two lists of sample IDs (.king.cutoff.in.id and .king.cutoff.out.id). Its count can differ a little from the course rule, which also treats repeats separately. To repeat the trial, change the number after --king-cutoff.

The expected result of the trials:

Kinship cutoffSamples removedSamples remaining
0.0442301,920
0.0884301,920
0.177 (course value)301,920
0.354201,930

How to read it. The cutoffs 0.0442, 0.0884 and 0.177 give the same 30 removed samples. In this simulated cohort no extra pair lies between 0.0442 and 0.177. At 0.354 only repeated samples are removed (20), and the ten first-degree pairs stay in the data. Real cohorts usually have many more distant relatives, so the counts would then differ.

Expected plot. For KING kinship cutoff, the horizontal axis shows the threshold and the vertical axis shows the number of samples removed. A red diamond marks the course value (0.177), where 30 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. --make-king-table applies the KING-robust estimator to every pair of samples, using autosomal hard calls. It compares how often the two people carry opposite homozygous genotypes (IBS0) and how often both are heterozygous (HETHET), and it does not need allele frequencies from the sample. KINSHIP is the kinship coefficient, the chance that an allele drawn from each person is identical by descent. Expected values are 0.5 for a duplicate, 0.25 for a parent and child or full siblings, 0.125 for second-degree and 0.0625 for third-degree pairs. NSNP is the number of variants compared.

Why these values. The cut-offs 0.354, 0.177 and 0.0442 sit between these expected values on a scale of powers of two. They are screening conventions. A parent and child cannot be told from full siblings by KINSHIP alone, but IBS0 is close to zero for a parent and child.

Pitfalls. The older PI_HAT takes allele frequencies from the whole sample, so mixed ancestry biases it. KING-robust is far less sensitive to this, although admixed samples and pairs of different ancestry can still bias it. Relatives also shape principal components. Remove them, or use methods built for related samples, before a PCA. Keep one person per pair by a rule that ignores disease status.

References

  1. Manichaikul A et al. (2010) Robust relationship inference in genome-wide association studies. Bioinformatics 26:2867-2873. PMID 20926424.
  2. Anderson CA et al. (2010) Data quality control in genetic case-control association studies. Nat Protoc 5:1564-1573. PMID 21085122.
  3. Conomos MP et al. (2015) Robust inference of population structure for ancestry prediction and correction of stratification in the presence of relatedness. Genet Epidemiol 39:276-293. PMID 25810074.
  4. 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.