MGHome page

GWAS course

Course map

0 of 64 steps done

Start with step 0.1Install R

Saved in this browser only.

Step 2.6

Module 2 · Files and formats · step 6 of 8

2.6Genome builds and lift-over

Commands tested on Windows

In short

We convert the DNA positions in a separate exercise from the GRCh37 genome reference to GRCh38. We keep sites that map to the same chromosome, correct opposite-strand letters and check the new positions without changing the main dataset.

Do this

You should see: The exercise has 10,000 sites. 9,970 of them map to the same chromosome, and eight of these map to the reverse strand in this chain file.

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.

1legacy <- file.path(cfg$data_dir, "grch37_exercise", "legacy_grch37")bim37 <- read_ws(paste0(legacy, ".bim"), header = FALSE)2chain <- file.path(cfg$data_dir, "grch37_exercise", "hg19ToHg38.over.chain.gz")require_files(chain)3lifted <- guided_liftover(paste0("chr", bim37[[1]]), bim37[[4]], chain)keep <- !is.na(lifted$position) & lifted$chromosome == paste0("chr", bim37[[1]])4map <- data.frame(ID = bim37[[2]][keep], POS = lifted$position[keep])map_file <- guided_path(cfg, "2.6", "update_map.tsv")write.table(map, map_file, sep = "\t", quote = FALSE, row.names = FALSE, col.names = FALSE)keep_file <- guided_list(map$ID, guided_path(cfg, "2.6", "mapped.txt"))5args <- c("--bfile", legacy, "--extract", keep_file, "--update-map", map_file)reverse <- bim37[[2]][keep & lifted$strand == "-"]if (length(reverse)) args <- c(args, "--flip", guided_list(reverse, guided_path(cfg, "2.6", "reverse.txt")))6run_plink(cfg, c(args, "--make-pgen", "--out", guided_path(cfg, "2.6", "grch38")))7ann <- guided_table(file.path(cfg$data_dir, "array", "sim2026_array_annotation.tsv"))k <- match(map$ID, ann$Name)stopifnot(all(map$POS[!is.na(k)] == as.numeric(ann$Position_GRCh38[k[!is.na(k)]])))8print(c(input_sites = nrow(bim37), mapped_same_chromosome = sum(keep), reverse_strand = length(reverse)))
  1. Note 1: Read the variant positions in the separate GRCh37 exercise.
  2. Note 2: Find the supplied file that links GRCh37 positions to GRCh38 positions.
  3. Note 3: Convert the positions and keep sites that stay on the same chromosome.
  4. Note 4: Save the new positions and the list of sites to keep.
  5. Note 5: Prepare the PLINK options and add a correction for opposite-strand sites if needed.
  6. Note 6: Save the converted exercise separately from the main dataset.
  7. Note 7: Check the new positions against matching sites in the supplied reference table.
  8. Note 8: Show how many sites were read, kept and mapped to the opposite strand.

You need

You need the small GRCh37 exercise supplied at 0.5. You also need the official hg19-to-hg38 chain file. It is part of the course data, in the folder data/course/grch37_exercise/. You do not need to download it. The main array is already GRCh38, so we do not lift it again.

What we do

We convert the separate GRCh37 exercise to GRCh38 in R. We use a chain file for this one direction. We keep the exercise apart from the main analysis.

Why

GRCh37 and GRCh38 are two different reference assemblies of the human genome. A coordinate is a chromosome and a position in one named assembly. Changing the build label or the rsID does not change the coordinates.

A lift-over (opens in a new tab) converts coordinates from one assembly to the other. It uses a chain file (opens in a new tab), which records which intervals of the two genomes are aligned. After a lift-over, a site can be unmapped (it has no match), it can move to another chromosome, or it can map to the reverse strand.

UCSC intervals are zero-based and half-open, which means they start counting at 0 and exclude the end point. The course positions are one-based, which means they start counting at 1. Review the unmapped sites and the reverse-strand sites. Do not accept a conversion that nobody documented.

What each option does

Each option, with the modifiers under the option they belong to.
Option or modifierWhat it does
--bfile

Reads the BED, BIM and FAM files named data/course/grch37_exercise/legacy_grch37, containing 10,000 exercise sites.

--extract

Keeps the 9,970 sites listed in s2-6_mapped.txt that map to the same chromosome in GRCh38.

--update-map

Reads site identifiers and their new GRCh38 positions from the two columns of s2-6_update_map.tsv.

--flip

R adds this option when sites map to the opposite DNA strand, using s2-6_reverse.txt to correct the DNA letters at eight sites in this exercise.

--make-pgen

Saves the converted exercise as PGEN, PVAR and PSAM files.

--out

Starts the converted exercise file names with results/guided/s2-6_grch38.

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

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

The exercise has 10,000 sites. 9,970 of them map to the same chromosome, and eight of these map to the reverse strand in this chain file. PLINK prints a warning that the base-pair positions are now unsorted. This warning is expected: the new GRCh38 positions are not in the old order. The resulting exercise variants are not sorted by position. Sort them explicitly before any operation that needs sorted input. Mapped positions whose identifier also appears in the course annotation agree with GRCh38. The main 296,510-site state from 2.5 is unchanged. A lift-over can lose sites, so record the mapping counts that are reported.

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. Check that the previous step finished. Keep the command and the console output (the text printed in R). 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 build mismatches, gaps and reverse mappings. A chain file does not check alleles and does not impute genotypes.

Quick check

Does a successful position lift-over guarantee that REF and ALT now match the destination reference?

Show the answer

No. You must still check the alleles and the reverse-strand mappings.

Files created

s2-6_update_map.tsv, the mapped and reverse lists, and the separate s2-6_grch38 files.