Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Genotype Data Formatting

This module implements a collection of workflows used to format genotype data.

Overview

Nothing here changes the genotypes; it changes how they are packaged. Tools in the protocol disagree about format - some want PLINK, some want VCF - and the association and fine-mapping steps run per region or per chromosome, so the genotype data has to be split the same way to be processed in parallel. These workflows do those conversions and splits, plus the LD matrix computation per region that summary-statistic fine-mapping needs.

When to run it. Run the relevant workflow after genotype quality control and before association scanning, LD calculation, or fine-mapping that requires the corresponding genotype layout.

This module provides workflows for the formatting of genotype files. This includes the conversion between VCF and PLINK formats, the splitting of data (by specified input, chromosomes or genes) and the merging of data (by specified input, or by chromosomes).

When to run it. After VCF_QC, before PCA and association testing. Which workflows you need depends on the tools you plan to run, not on your data.

Input

  • --genoFile: the genotype data to convert. Its meaning follows the workflow -- one or more PLINK prefixes for plink_to_vcf and the merge_* workflows, one or more VCFs for vcf_to_plink.

  • --name (required): output prefix for this run, e.g. protocol_example.genotype.merged.

  • --cwd (default output): work directory for the analysis.

  • --modular-script-dir (default code/script): directory holding the modular wrapper scripts.

vcf_to_plink -- VCF to PLINK:

  • --add-chr / --no-add-chr (default on): prefix contig names with chr.

  • --remove-duplicates / --no-remove-duplicates (default off): drop duplicated variant IDs.

  • --keep-dosage / --no-keep-dosage (default off): retain dosages rather than hard calls.

  • --remove-samples / --keep-samples (both default .): optional sample lists.

genotype_by_region -- split PLINK genotypes into regions:

  • --region-list (required): regions to cut the genotypes into.

  • --window (default 0): flanking bp added either side of each region.

ld_by_region_plink -- LD matrices per region:

  • --region-list (required): regions to compute LD for.

  • --float-type (default 16): float width of the stored matrix.

genotype_by_chrom -- split by chromosome:

  • --chrom (required): the chromosomes to emit, e.g. --chrom 21 22.

write_data_list -- write a manifest of produced files:

  • --out (required) and --ext (required): manifest path and the file extension to collect.

  • --data-files: the files to list, when they are not discovered from the previous step.

merge_plink -- merge PLINK filesets:

  • --keep-samples (default .): optional sample list.

  • --extra-plink-opts: extra options passed straight to PLINK.

Cluster resources: --numThreads (default 20), --job-size (default 1), --walltime (default 5h), --mem (default 16G).

Output

  • <name>.vcf.gz (+ .tbi) -- from plink_to_vcf: the genotypes as a bgzipped, tabix-indexed VCF.

  • <name>.bed / .bim / .fam, or .pgen / .pvar / .psam -- from vcf_to_plink, and from merge_plink for the merged fileset: a PLINK1 or PLINK2 fileset.

  • <name>.<region>.bed / .bim / .fam -- from genotype_by_region: one fileset per region in --region-list, widened by --window.

  • <name>.<region>.ld.rds -- from ld_by_region_plink: the LD matrix for a region, stored at the width --float-type sets.

  • <name>.<chrom>.bed / .bim / .fam -- from genotype_by_chrom: one fileset per --chrom.

  • the manifest named by --out, a plain .txt or .list file of the produced paths -- from write_data_list. Downstream modules read this rather than globbing.

  • a .stdout / .stderr log and the generated script beside each target.

All paths are relative to --cwd, and <name> is whatever --name was set to.

A .pvar file carries meta_file headers ahead of the variant table, so it must be read with a parser that skips them rather than as a plain TSV.

The merged fileset from Route A, output/genotype_formatting/plink/protocol_example.genotype.merged, holds 371,802 variants across 60 samples. First rows of the .bim:

1	chr1:113886_CTCTT_C	0	113886	C	CTCTT
1	chr1:191223_C_*	0	191223	*	C

and of the .fam, which carries no phenotype (-9):

0 SAMPLE_001 0 0 0 -9
0 SAMPLE_002 0 0 0 -9

Route A step 3 writes one fileset per chromosome plus its manifest:

protocol_example.genotype.merged.10.bed
protocol_example.genotype.merged.10.bim
protocol_example.genotype.merged.10.fam
protocol_example.genotype_by_chrom_files.txt

Routes B and C have no committed example output. Route B (plink_to_vcf, merge_vcf) and the manifest step are runnable but have not been executed here; Route C additionally needs a region list, which the protocol does not yet ship.

Minimal Working Example

Convert the 22 per-chromosome toy VCFs into per-chromosome PLINK bed/bim/fam bundles. Each input VCF becomes one PLINK bundle named after the input stem.

Timing: TBD (on toy dataset)

This step merges all per-chromosome PLINK files into one genome-wide bundle. Note: only plink (v1.9) can perform the merge — plink2 does not support it.

Timing: TBD (on toy dataset)

Step 3. Partition Genotype Data by Chromosome

Split the merged genome-wide bundle back into per-chromosome PLINK files.

Timing: TBD (on toy dataset)

The inverse of Step 1: plink_to_vcf writes one bgzipped VCF per input fileset.

Timing: TBD (on toy dataset)

Step 2. Merge VCF files

merge_vcf concatenates the per-chromosome VCFs into one, the VCF counterpart of Step 2.

Timing: TBD (on toy dataset)

Split by region and compute LD

Both steps read the merged fileset from Route A step 2, so run that first.

genotype_by_region cuts one fileset per region in --region-list, widened by --window. The protocol does not yet ship an example region list, so this command cannot be run against the toy data as it stands; input/genotype/protocol_example.genotype.region_list.txt is the path it expects.

Timing: TBD (on toy dataset)

Step 2. LD matrices per region

ld_by_region_plink computes an LD matrix per region and writes a .ld.list index alongside. The protocol does not yet ship an example region list, so this command cannot be run against the toy data as it stands; input/genotype/protocol_example.genotype.region_list.txt is the path it expects.

Timing: TBD (on toy dataset)

Building a file manifest by hand

write_data_list collects paths into a manifest that downstream modules read instead of globbing. The _1 conversion steps emit their own *_files.txt automatically; this workflow is for building one by hand.

Timing: TBD (on toy dataset)

Command Interface

usage: sos run pipeline/genotype_formatting.ipynb
               [workflow_name | -t targets] [options] [workflow_options]
  workflow_name:        Single or combined workflows defined in this script
  targets:              One or more targets to generate
  options:              Single-hyphen sos parameters (see "sos run -h" for details)
  workflow_options:     Double-hyphen workflow-specific parameters

Workflows:
  plink_to_vcf
  vcf_to_plink
  genotype_by_region
  ld_by_region_plink
  genotype_by_chrom
  write_data_list
  merge_plink
  merge_vcf

Global Workflow Options:
  --modular-script-dir code/script (as path)
  --cwd output (as path)
                        Work directory & output directory
  --job-size 1 (as int)
                        For cluster jobs, number commands to run per job
  --walltime 5h
                        Wall clock time expected
  --mem 16G
                        Memory expected
  --numThreads 20 (as int)
                        Number of threads
  --genoFile  paths

                        the path to a bed file or VCF file, a vector of bed
                        files or VCF files, or a text file listing the bed files
                        or VCF files to process
  --name VAL (as str, required)

Sections
  plink_to_vcf_1:
  vcf_to_plink:
    Workflow Options:
      --[no-]remove-duplicates (default to False)
      --[no-]add-chr (default to True)
      --[no-]keep-dosage (default to False)
      --remove-samples . (as path)
                        The path to the file that contains the list of samples
                        to remove (format FID, IID)
      --keep-samples . (as path)
                        The path to the file that contains the list of samples
                        to keep (format FID, IID)
  genotype_by_region_1:
    Workflow Options:
      --window 0 (as int)
                        cis window size
      --region-list VAL (as path, required)
                        Region definition
  ld_by_region_plink_1:
    Workflow Options:
      --region-list VAL (as path, required)
                        Region definition
      --float-type 16 (as int)
  genotype_by_chrom_1:
    Workflow Options:
      --chrom VAL VAL ... (as type, required)
  genotype_by_chrom_2:
  plink_to_vcf_2:
  genotype_by_region_2:
  ld_by_region_*_2:
    Workflow Options:
      --region-list VAL (as path, required)
  write_data_list:
    Workflow Options:
      --out VAL (as path, required)
      --ext VAL (as str, required)
      --data-files  paths

  merge_plink:
    Workflow Options:
      --keep-samples . (as path)
                        The path to the file that contains the list of samples
                        to keep (format FID, IID)
      --extra-plink-opts  (as list)
  merge_vcf:

Workflow implementation

Export VCF files to PLINK 1.0 format, without keeping allele orders by default. The resulting PLINK will lose ref/alt allele information but will go by major/minor allele, as conventionally used in standard PLINK format. Notice that PLINK 1.0 format does not allow for dosages. PLINK 2.0 format support it, but it is generally not supported by downstreams data analysis.

In the following code block the option --vcf-half-call m treat half-call as missing.

Compute LD matrices for given input region

ldstore2 based implementation

Not implemented. No ldstore2 section exists in this notebook and the tool is not in the container, so nothing here is runnable; ld_by_region_plink above is the only LD workflow. ldstore2 would suit larger cohorts such as UK Biobank, which is not a constraint in the FunGen-xQTL project.

For whoever picks this up, the draft lives outside this repository: LDstore workflow and genotype preparation. It needs a semicolon-separated master file with one dataset per line, Z-file IDs formatted as rsid:chrom:pos:a1:a2 with zero-padded chromosomes, plus a sample list.

One workflow covers both formats: genotype_by_chrom writes {_chrom}.{output_ext}, so --output-ext chooses a PLINK fileset or a VCF per chromosome.

Note that .pvar file have meta_file headers before the variant info dataframe

Merge VCF files