Module 5 · Population structure · step 6 of 7
5.6Define an ancestry-matched cohort
Commands tested on Windows
In short
We compare each study sample with a European reference group on the first four principal components, numbers that summarise patterns of genetic variation. We remove a sample if any of them lies more than six standard deviations (a measure of spread) from the reference mean; this rule defines the group for our teaching analysis.
Do this
- Paste in the RStudio ConsoleThe R command in the Command section below
You should see: 33 remaining outlier records are excluded. 1,811 samples and 238,513 variants 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.
1ref <- guided_table(paste0(guided_path(cfg, "5.4", "ref_scores"), ".sscore"))study <- guided_table(paste0(guided_path(cfg, "5.4", "study_scores"), ".sscore"))metadata <- guided_table(file.path(cfg$data_dir, "pca_reference", "ref1000g_grch38.psam"))2eur <- metadata$SuperPop[match(ref$IID, metadata$IID)] == "EUR"columns <- paste0("PC", 1:4, "_AVG")r <- sapply(ref[eur, columns, drop = FALSE], as.numeric)s <- sapply(study[columns], as.numeric)3centre <- colMeans(r)spread <- sqrt(colMeans(sweep(r, 2, centre, "-")^2))z <- sweep(sweep(s, 2, centre, "-"), 2, spread, "/")4bad <- study$IID[apply(abs(z), 1, max) > 6]remove <- guided_list(bad, guided_path(cfg, "5.6", "remove.txt"), TRUE)5run_plink(cfg, c("--pfile", guided_path(cfg, "4.6"), "--remove", remove, "--make-pgen", "--out", guided_path(cfg, "5.6")))# Course bookkeeping: records the counts for the next step. Do not change.ops <- guided_replay_lists(cfg, "5.6", "4.6")guided_register(cfg, "5.6", "5.4", 1811, 238513, operations = ops)- Note 1: Read the reference and study scores and reference group labels.
- Note 2: Select the European reference samples and the first four component scores.
- Note 3: Measure each study score's distance from the reference mean using its spread.
- Note 4: List samples more than six standard deviations away on any selected component.
- Note 5: Remove the listed samples and save the retained study group.
You need
The reference and study scores from step 5.4, and the reference labels used in step 5.5.
What we do
Apply a rule, fixed in advance, to the first four projected PCs. The rule flags samples that are more than six standard deviations from the European reference.
Why
In this teaching design, the main analysis uses a cohort that broadly matches the European reference. The rule has three parts. First, take the European reference samples. For each of PC1 to PC4, calculate their mean and their standard deviation (opens in a new tab) (a measure of how spread out the values are). Second, convert each study score into a distance from that mean, counted in standard deviations. Third, flag a sample as an outlier (opens in a new tab) if its largest distance, ignoring the sign, is above six.
This is a rule for this study only. It is not a general way to classify ancestry. Apply it before you look at any association results, and report who was excluded.
What each option does
| Option or modifier | What it does |
|---|---|
| --pfile | Reads the cleaned study genotype files s4-6_state.pgen, .pvar and .psam from the results folder. |
| --remove | Removes the samples listed in s5-6_remove.txt because at least one of their first four component scores is more than six standard deviations from the European reference mean. |
| --make-pgen | Saves the retained 1,811 samples and 238,513 variants as PGEN, PVAR and PSAM files. |
| --out | Starts the retained study output file names with s5-6_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
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
33 remaining outlier records are excluded. 1,811 samples and 238,513 variants remain. Some of the simulated outliers were already removed in earlier QC steps. So 33 is not the total number of outliers in the original data.
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 message. Check the input file named in the message and the previous step. Keep the command and the console log. Do not use files from a different dataset version. See troubleshooting (opens in a new tab).
What this check can reveal
Large differences in ancestry compared with the reference, and possible technical outliers. A mismatch needs a review and a written study design. It does not mean that the excluded people are unsuitable in general.
Quick check
Why does the rule use four PCs and not only the two shown in the plot?
Show the answer
A difference that is weak or invisible on the first two axes can appear on later axes.
Files created
s5-6_remove.txt, s5-6_state.* and replay metadata.