Module 4 · Variant quality control · step 5 of 6
4.5Sex chromosomes, MT and duplicated sites
Commands tested on Windows
In short
We remove variants on X, Y and mitochondrial DNA with unexpected results in more than 1% of people checked. We then rename variants by their DNA location and letters, keep one copy of matching duplicates and remove copies that disagree.
Do this
- Paste in the RStudio ConsoleThe R command in the Command section below
You should see: 33 variants with impossible calls on the sex chromosomes or MT 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, "4.5", "sexchr")run_plink(cfg, c("--pfile", guided_path(cfg, "4.4"), "--chr", "X,Y,MT", "--export", "A", "--out", report))2header <- strsplit(readLines(paste0(report, ".raw"), n = 1L), "[[:space:]]+")[[1]]raw <- read.table(paste0(report, ".raw"), header = TRUE, check.names = FALSE, quote = "", comment.char = "", colClasses = c(rep("character", 6), rep("integer", length(header) - 6L)), na.strings = "NA")3genotypes <- as.matrix(raw[, -(1:6), drop = FALSE]); genotypes[is.na(genotypes)] <- -1Lids <- sub("_[^_]+$", "", names(raw)[-(1:6)])pvar <- guided_table(paste0(guided_path(cfg, "4.4"), ".pvar"))chromosome <- pvar$CHROM[match(ids, pvar$ID)]4wrong <- numeric(length(ids))wrong[chromosome == "X"] <- colMeans(genotypes[raw$SEX == "1", chromosome == "X", drop = FALSE] == 1L)wrong[chromosome == "Y"] <- colMeans(genotypes[raw$SEX == "2", chromosome == "Y", drop = FALSE] >= 0L)wrong[chromosome == "MT"] <- colMeans(genotypes[, chromosome == "MT", drop = FALSE] == 1L)5exclude <- guided_list(ids[wrong > 0.01], guided_path(cfg, "4.5", "exclude.txt"))6sex_state <- guided_path(cfg, "4.5", "sex_state")run_plink(cfg, c("--pfile", guided_path(cfg, "4.4"), "--exclude", exclude, "--make-pgen", "--out", sex_state))stopifnot(all(guided_counts(sex_state) == c(1844, 238819)))7run_plink(cfg, c("--pfile", sex_state, "--set-all-var-ids", "@:#:$r:$a", "--rm-dup", "exclude-mismatch", "list", "--make-pgen", "--out", guided_path(cfg, "4.5")))# Course bookkeeping: records the counts for the next step. Do not change.ops <- data.frame(option = c("--exclude", "--set-all-var-ids", "--rm-dup"), value = c(exclude, "@:#:$r:$a", "exclude-mismatch"))guided_register(cfg, "4.5", "4.4", 1844, 238513, operations = ops)8rm(raw, genotypes); invisible(gc())- Note 1: Export X, Y and mitochondrial DNA results as a table.
- Note 2: Read the column names and the exported results.
- Note 3: Arrange the DNA results by person and variant and identify each chromosome.
- Note 4: Calculate the share of unexpected results for each variant.
- Note 5: List variants with unexpected results in more than 1% of people checked.
- Note 6: Remove the listed variants and check the remaining counts.
- Note 7: Rename variants by location and DNA letters and remove duplicate entries.
- Note 8: Free the computer memory used by the large tables.
You need
The dataset from step 4.4, which we call state 4.4. Step 3.3 must already have split the PAR regions.
What we do
We review calls that are impossible for the sex chromosomes (opens in a new tab) X and Y and for mitochondrial DNA (MT). We also merge duplicated entries that describe the same variant.
Why
Men have one X chromosome and one Y chromosome. Women have two X chromosomes. A chromosome with a single copy in a person is called haploid (opens in a new tab). Mitochondrial DNA (MT (opens in a new tab)) is a small circular DNA that comes from the mother only, so it is also treated as haploid. So the expected genotypes differ from those on chromosomes 1 to 22.
The X chromosome has two small regions that behave like autosomes. They are the pseudoautosomal regions (opens in a new tab) (PAR). Step 3.3 already split them from the rest of X. The rest is called non-PAR X.
This step looks for three kinds of impossible calls. The first is a heterozygous call on non-PAR X in men. The second is a heterozygous call on MT. The third is any call on Y in people recorded as women. We use a 1% rule for each variant: we remove it if more than 1% of the relevant samples show an impossible call. A real interpretation needs more laboratory work and a review of the chromosome analysis of each person.
The second task is duplicates. Different array IDs can describe the same chromosome, position and allele pair. A duplicated variant (opens in a new tab) is such a repeated entry. We rename every variant as chromosome:position:REF:ALT. REF is the reference allele (opens in a new tab), the version found in the reference genome. ALT is the other version, the alternative allele. We keep one copy of each duplicate if the genotypes of the copies agree. We remove both copies if the genotypes disagree. The imported REF is only provisional until someone checks it against a reference genome.
What each option does
Command 1: export chromosome results for checking
| Option or modifier | What it does |
|---|---|
| --pfile | Reads the PGEN, PVAR and PSAM files named results/guided/s4-4_state. |
| --chr | Selects only X, Y and MT, the mitochondrial chromosome. |
| --export | Writes genotype data in a text format for R to read. |
| Modifier of --export: A | Writes a table with one row per person and values of 0, 1 or 2 for each variant. For chromosomes present in one copy, such as X in males, PLINK writes 0 or 2, so 2 does not mean two physical copies. |
| --out | Starts the export file names with results/guided/s4-5_sexchr. |
Command 2: remove variants with unexpected chromosome results
| Option or modifier | What it does |
|---|---|
| --pfile | Reads the PGEN, PVAR and PSAM files named results/guided/s4-4_state. |
| --exclude | Removes variants listed in results/guided/s4-5_exclude.txt because more than 1% of the people checked have unexpected results on X, Y or MT. |
| --make-pgen | Saves the remaining data as PGEN, PVAR and PSAM files. |
| --out | Starts the output file names with results/guided/s4-5_sex_state. |
Command 3: rename variants and remove duplicate entries
| Option or modifier | What it does |
|---|---|
| --pfile | Reads the PGEN, PVAR and PSAM files named results/guided/s4-5_sex_state. |
| --set-all-var-ids | Names each variant with @:#:$r:$a: chromosome, position, reference DNA version and alternative DNA version, separated by colons. |
| --rm-dup | Finds entries with the same variant name and keeps one copy when they match. |
| Modifier of --rm-dup: exclude-mismatch | Removes all copies of a duplicated variant when their records disagree. |
| Modifier of --rm-dup: list | Writes a list of the duplicated variant names. |
| --make-pgen | Saves the remaining data as PGEN, PVAR and PSAM files. |
| --out | Starts the output file names with results/guided/s4-5_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
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 variants with impossible calls on the sex chromosomes or MT are removed. The duplicate review removes 306 entries. It keeps one of each of the 280 matching pairs, and it removes both members of the 13 pairs that disagree. The final number of variants is 238,513.
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 impossible haploid calls, repeated entries in the array annotation and duplicates with different genotypes. Changing an ID does not check the reference alleles. It also does not give the variant a new biological meaning.
Quick check
Why do we remove both copies of a duplicate with different genotypes? Why do we not keep the copy with the better association p-value?
Show the answer
The difference between the genotypes is not resolved. Association results must not decide which technical record to trust.
Files created
The chromosome export, the exclusion list, the duplicate reports and s4-5_state.*.