MGHome page

GWAS course

Course map

0 of 64 steps done

Start with step 0.1Install R

Saved in this browser only.

Step A.7

Module A · Genotype imputation · step 7 of 10

A.7Prepare the input files

Commands tested on Windows

In short

We write the measured genotypes of the two regions of the module as one compressed VCF file per region, the file format that imputation software and servers read. We check that every reference allele matches the GRCh38 genome and fix two problems of PLINK files on Windows.

Do this

You should see: Two VCF files: 161 sites on chromosome 18 and 277 on chromosome 22, each with 1,811 samples.

Command

Module setup, needed before this step

R: module setup

source("R/guided.R")cfg <- guided_init()

R command

Type or paste into the RStudio console.

1input <- prepare_imputation_input(cfg)2print(input[, c("chr", "sites", "samples", "crlf_lines", "pr_flags")])3stopifnot(identical(input$sites, c(161L, 277L)), all(input$samples == 1811L))
  1. Note 1: Write one VCF per region from your final cohort, then check and fix it.
  2. Note 2: Show the sites and samples of each file, and what was fixed.
  3. Note 3: Check the counts: 161 and 277 array sites, 1,811 samples in each file.

You need

Your final cohort from 5.7 and the array annotation from the course data.

What we do

Write the measured genotypes of the two regions as one VCF file per region, ready for phasing and imputation. Then check every reference allele against the GRCh38 genome.

Why

This is where most imputation jobs fail, so we do the checks in order.

  1. Start from clean data. Imputation copies errors: a wrong genotype at a measured site can spread to the sites around it. We start from the final cohort of step 5.7.
  2. Check the genome build. The course data are on GRCh38 (opens in a new tab), like the module panel. The Michigan and HMIS panels use hg19, so these servers convert your positions. In some stretches of the genome the conversion can give the wrong allele at A/T and C/G sites (Sheng et al. 2023 (opens in a new tab)).
  3. Check strand and alleles. The software compares your alleles with the panel. Sites on the wrong strand are flipped or removed. Step 2.5 removed all the ambiguous A/T and C/G sites of the course data. The REF allele of every site must be the GRCh38 plus-strand (opens in a new tab) allele.
  4. Write one VCF per chromosome, compressed with bgzip and sorted by position. PLINK 2 can write it.
  5. Use the chromosome names of the panel. The module panel uses chr22. TOPMed asks for the chr prefix on GRCh38.

The R helper prepare_imputation_input() runs PLINK once for each region, then checks and fixes the files. PLINK does not know the reference genome, so it marks every REF allele as "provisional" with the flag PR in the INFO column. The helper removes the flag only after it has checked that every REF equals the GRCh38 plus-strand allele of the array annotation (column Ref_Plus). On Windows, PLINK 2 ends each line with a Windows line end (CR LF). The last genotype of every line then reads 0/0\r, which is not valid VCF. The helper writes the file again with plain line ends, compressed with bgzip.

What each option does

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

Reads s5-7_state.pgen, .pvar and .psam, which hold the final study genotypes and sample details.

--extract

Keeps only the variants inside the regions listed in sA-7_regions.bed1.

Modifier of --extract: bed1

Says that the region file has a chromosome, a start and an end on each line, with positions counted from 1.

--chr

Keeps chromosome 22 only. R runs the same command for chromosome 18.

--ref-allele

Sets the reference allele (REF) of each variant from sA-7_ref_alleles_chr22.tsv. The number 2 says that the allele is in column 2, and 1 says that the variant ID is in column 1.

--output-chr

Uses chrM to write chromosome names with the chr prefix, for example chr22, as imputation software expects on GRCh38.

--export

Writes the genotypes in the VCF format, one line per variant and one column per sample.

Modifier of --export: vcf-4.2

Writes version 4.2 of the VCF format, which every imputation server accepts.

Modifier of --export: bgz

Compresses the file with bgzip, the block compression that VCF tools need.

Modifier of --export: id-paste

With iid, writes only the individual ID as the sample name.

--out

Starts output file names with sA-7_chr22, including sA-7_chr22.vcf.gz.

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.

Expected result

Show the expected result

Two VCF files: 161 sites on chromosome 18 and 277 on chromosome 22, each with 1,811 samples. PLINK reports "--ref-allele: No variants changed": the REF alleles of the course data were already right. On Windows the helper reports 168 and 284 lines with a Windows line end (the header lines count too); on macOS and Linux it reports 0. It removed 161 and 277 PR flags.

Validation: executable chunks passed on Windows with PLINK 2.0 b.1.4 on the 1,811 samples of the course cohort (data release 2026.1). The genotypes are identical to the input of the imputation in the module A data. bgzip, tabix and bcftools 1.25 read both files.

If something goes wrong

If something goes wrong

"A REF allele differs from the GRCh38 plus-strand allele": a site is on the wrong strand or has the wrong alleles. Do not upload the file. Check step 2.5 and the annotation. "The selected executable is not a working PLINK 2 binary": see step 0.6. Missing files from step 5.7: restore the checkpoint, as the first chunk of the module explains.

Quick check

Why does the helper check every REF allele before it removes the PR flag?

Show the answer

The flag says that PLINK does not know whether REF is the reference genome allele. Removing it without a check would claim something nobody verified. The imputation compares your alleles with the panel, so a wrong REF would give wrong or missing results at that site.

Files created

sA-7_chr18.vcf.gz and sA-7_chr22.vcf.gz (the input for imputation), sA-7_regions.bed1, sA-7_ref_alleles_chr18.tsv, sA-7_ref_alleles_chr22.tsv and the log files.

Another approach (optional)

Another approach (optional)

Run the imputation yourself. The module A data already hold the imputed output, so this box is optional. Eagle2 and Minimac4 run on Linux and macOS, and on Windows only inside the Windows Subsystem for Linux (WSL). They are not part of the course setup. The commands below were tested on 24 November 2026 with Eagle 2.4.1 and Minimac4 4.1.6 in WSL Ubuntu, on the files of the module A data and the input of this step. Download the programs only from their official pages: Eagle (opens in a new tab) and Minimac4 (opens in a new tab).

Phase the input against the panel (Eagle2). Every input site is also in the panel, so none is lost. It takes about one minute per region.

eagle --vcfRef panel_chr22.vcf.gz --vcfTarget sA-7_chr22.vcf.gz --geneticMapFile genetic_map_hg38_chr18_chr22_eagle.txt.gz --outPrefix phased_chr22 --numThreads 8 --vcfOutFormat z

Impute (Minimac4). The module A data hold the panel in the compact Minimac4 format (.msav).

minimac4 --map minimac4_chr22.map --format GT,DS --threads 8 --empirical-output chr22.empirical_dose.vcf.gz --sites chr22.sites.vcf.gz -o chr22.dose.vcf.gz panel_chr22.msav phased_chr22.vcf.gz

--format GT,DS writes the best-guess genotype and the dosage. Add GP for the genotype probabilities; the files then grow to 150 to 250 MB per region. --empirical-output writes the leave-one-out dosages of the array sites, from which Minimac4 computes the empirical R² (step A.8). It takes about one minute and less than 200 MB of memory per region. The output holds every panel variant: 49,988 on chromosome 18 and 60,224 on chromosome 22.

Expected result. Files like those in data/course/module_a/imputed/. Compare a few R2 values of the INFO column with the files of the module A data.

Reference. Loh et al. 2016 (opens in a new tab) for Eagle2; Das et al. 2016 (opens in a new tab) for Minimac.