Module 3 · Sample quality control · step 1 of 8
3.1Rough cleaning
Commands tested on Windows
In short
We remove samples and variants with very many missing calls. This first clean prevents the worst failures from distorting the checks that follow.
Do this
- Paste in the RStudio ConsoleThe R command in the Command section below
You should see: Three wells and 150 variants are removed. 2,013 samples and 296,360 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.
1input <- guided_path(cfg, "2.8")rough <- guided_path(cfg, "3.1", "samples")2run_plink(cfg, c("--pfile", input, "--mind", "0.10", "--make-pgen", "--out", rough))3run_plink(cfg, c("--pfile", rough, "--geno", "0.10", "--make-pgen", "--out", guided_path(cfg, "3.1")))# Course bookkeeping: records the counts for the next step. Do not change.ops <- guided_replay_lists(cfg, "3.1", "2.8")guided_register(cfg, "3.1", "2.8", 2013, 296360, operations = ops)- Note 1: Choose the input files and the names for the saved files.
- Note 2: Remove samples with more than 10% of calls missing.
- Note 3: Remove variants with more than 10% of calls missing.
You need
The full 2.8 checkpoint.
What we do
First we remove wells (opens in a new tab) that were read very badly. A well is a small hole in a plate that holds the DNA of one sample. Then we remove variants (opens in a new tab) (also called sites) that were read very badly. A variant is a place in the DNA where people differ.
Why
Some wells fail in the laboratory. For these wells, many results are missing. Missingness (opens in a new tab) is the share of genotypes (opens in a new tab) that could not be read. A genotype is the pair of DNA letters that a person has at one site.
Gross failures distort later summaries of missingness and of heterozygosity (opens in a new tab). Heterozygosity means that a person carries two different alleles (opens in a new tab) (versions of the DNA letter) at a site.
First, we remove samples with more than 10% missing genotypes. Second, we recompute the missingness of each variant on the samples that remain. Third, we remove variants with more than 10% missingness.
The order matters. Removing poor wells can improve the apparent call rate (opens in a new tab) of a site. The call rate is the share of genotypes that were read successfully. This step is only a first screen. It is not the final 98% call-rate rule.
What each option does
Command 1: remove poor samples
| Option or modifier | What it does |
|---|---|
| --pfile | Reads the PGEN, PVAR and PSAM input files that share the given name prefix. |
| --mind | Removes samples with more than 10% missing calls. |
| --make-pgen | Saves a new dataset as PGEN, PVAR and PSAM files. |
| --out | Sets the shared beginning of the output file names. |
Command 2: remove poor variants
| Option or modifier | What it does |
|---|---|
| --pfile | Reads the PGEN, PVAR and PSAM input files that share the given name prefix. |
| --geno | Removes variants with more than 10% missing calls in the input dataset. |
| --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.
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
Three wells and 150 variants are removed. 2,013 samples and 296,360 variants 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
Failed wells and sites that failed badly. This check cannot tell you the biological or technical cause by itself.
Quick check
Why do we recalculate variant missingness after we remove the very poor wells?
Show the answer
Poor wells can make good sites look bad.
Files created
s3-1_samples.*, s3-1_state.*, the lists of removed samples and variants, and the replay metadata (notes that let the course repeat this step).
Try other thresholds (optional)
Try other thresholds (optional)
Try it
Rough clean: variants: --geno
Each point starts from the same input (2,013 samples, 296,510 variants) and changes one threshold.
--geno 0.1: 150 variants removed (course value).
| Threshold | Removed | Remaining |
|---|---|---|
| 0.01 | 3,705 | 292,805 |
| 0.02 | 958 | 295,552 |
| 0.05 | 735 | 295,775 |
| 0.1(course value) | 150 | 296,360 |
| 0.2 | 130 | 296,380 |
Try it
Rough clean: samples: --mind
Each point starts from the same input (2,016 samples, 296,510 variants) and changes one threshold.
--mind 0.1: 3 samples removed (course value).
| Threshold | Removed | Remaining |
|---|---|---|
| 0.01 | 121 | 1,895 |
| 0.02 | 45 | 1,971 |
| 0.05 | 3 | 2,013 |
| 0.1(course value) | 3 | 2,013 |
| 0.2 | 3 | 2,013 |
This box is not part of the main path. No later step uses it. It does not change your results.
The step makes a rough first clean. --mind is the largest share of missing calls that a sample may have. --geno is the same limit for a variant. A low value is strict and removes more. A high value is generous. At 0.10 only wells and sites that are clearly broken are removed. The finer checks come later, at steps 3.2 and 4.1, so this first clean can be generous. Each trial below starts from the same input as the step and changes one threshold only.
# Optional. Each trial starts from the same input and changes one threshold only. Your results folder is not changed.
thresholds <- c(0.01, 0.02, 0.05, 0.10, 0.20)
# 1. Samples: --mind on the cohort saved at 2.8.
samples <- threshold_counts(cfg, guided_path(cfg, "2.8"), "--mind", thresholds)
samples$course_value <- samples$threshold == 0.10
samples
# 2. Variants: --geno on the samples that remain after --mind 0.10.
variants <- threshold_counts(cfg, guided_path(cfg, "3.1", "samples"), "--geno", thresholds)
variants$course_value <- variants$threshold == 0.10
variants
# 3. Plot the removed numbers. The course value is the large diamond.
par(mfrow = c(1, 2)); plot_threshold_trials(samples, "Samples removed"); plot_threshold_trials(variants, "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/s2-8_state --mind 0.05 --make-just-psam --out results/trials/s3-1_trial --threads 2 --memory 2048
The log file results/trials/s3-1_trial.log states how many samples were removed. To repeat the trial, change the number after --mind and use a new --out name. For variants, use --geno and --make-just-pvar, and start from results/guided/s3-1_samples. PLINK 2 treats a dosage without a hard call as missing by default (the dosage modifier changes this). The course data have hard calls only, so this makes no difference here.
The expected result of the trials:
Samples removed by --mind:
| Threshold | Samples removed | Samples remaining |
|---|---|---|
| 0.01 | 121 | 1,895 |
| 0.02 | 45 | 1,971 |
| 0.05 | 3 | 2,013 |
| 0.1 (course value) | 3 | 2,013 |
| 0.2 | 3 | 2,013 |
Variants removed by --geno, after --mind 0.10:
| Threshold | Variants removed | Variants remaining |
|---|---|---|
| 0.01 | 3,705 | 292,805 |
| 0.02 | 958 | 295,552 |
| 0.05 | 735 | 295,775 |
| 0.1 (course value) | 150 | 296,360 |
| 0.2 | 130 | 296,380 |
How to read it. Between 0.05 and 0.20 the same three wells are removed. Those wells are so poor that no limit in this range keeps them. Below 0.05 the limit starts to remove good wells too (45 at 0.02 and 121 at 0.01). For variants, a stricter limit removes many more sites. The course value of 0.10 keeps the rough clean generous on purpose.
Expected plot. For --geno, the horizontal axis shows the threshold and the vertical axis shows the number of variants removed. A red diamond marks the course value (0.1), where 150 variants are removed. For --mind, the horizontal axis shows the threshold and the vertical axis shows the number of samples removed. A red diamond marks the course value (0.1), where 3 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.