Skip to main content

Fast calculation of approximately independent LD blocks

Project description

LDetect-lite

PyPI Tests PyPI Downloads

A modern, fast re-implementation of LDetect, a method for calculating approximately independent linkage disequilibrium (LD) blocks in the human genome. The algorithm is described in Berisa & Pickrell, 2016.

Installation

Install from PyPI via:

pip install ldetect-lite

Or, with uv:

uv add ldetect-lite

This installs three equivalent CLI entry points — ldetect-lite, ldetect, and ldl — so pick whichever is most convenient; examples below use ldetect.

The main ldetect run pipeline also requires htslib. Specifically, tabix is used to stream VCF files to ldetect calc-covariance, and so must be on PATH.

Optional (--generate-heatmap): install matplotlib with pip install "ldetect-lite[heatmap]", or use uv sync --extra heatmap from a source checkout. Generating covariance heatmaps requires a matplotlib install.

Development

Install from source

git clone https://github.com/adamyhe/ldetect-lite.git
cd ldetect-lite
uv sync --extra dev

From a development checkout, run CLI commands through uv run so they use the managed environment.

Usage

End-to-end pipeline

uv run ldetect run \
  --genetic-map chr2.interpolated_genetic_map.gz \
  --reference-panel 1000G.chr2.vcf.gz \
  --individuals eurinds.txt \
  --chromosome chr2 \
  --output-dir results/chr2/

This writes results/chr2/chr2-ld-blocks.bed — a BED file of approximately independent LD blocks.

Global options (before the subcommand):

  • -v / --verbosity {debug,info,warning,error} — logging verbosity (default: info; use warning to silence progress messages, debug for full detail)

Options:

  • --ne FLOAT — effective population size Ne used by the Wen & Stephens shrinkage estimator (default: 11418.0, the CEU/HapMap II value; reproduction configs may override this for non-European populations)
  • --cov-cutoff FLOAT — LD pairs with absolute shrinkage correlation below this threshold are not written to disk, reducing storage (default: 1e-7)
  • --covariance-cache {compact,full} — partition cache schema for ldetect run (default: compact). Compact caches write only canonical position pairs, shrink_ld, diagonals, and lookup indexes, which is enough for restartable matrix-to-vector, metric, and local-search steps. Use full when debugging or when later running full-matrix/heatmap readers.
  • --covariance-compression {lzf,zstd} — HDF5 compression codec for covariance partitions (default: zstd). zstd is smaller and faster to read/write than lzf at equal precision — see docs/optimizations.md.
  • --n-snps-bw-bpoints N — target mean number of SNPs between consecutive breakpoints; controls block granularity (default: 10000, following Berisa & Pickrell 2016). The target breakpoint count is ceil(n_snps / N - 1). Mutually exclusive with --n-bpoints.
  • --n-bpoints N — directly specify the number of breakpoints, bypassing the --n-snps-bw-bpoints formula; useful when replicating a published analysis with a known block count
  • --subset {fourier,fourier_ls,uniform,uniform_ls} — which of the four breakpoint sets to write to the BED file (default: fourier_ls; see docs/pipeline-steps.md step 4)
  • --all-breakpoint-subsets — compute all four breakpoint sets in the JSON output. By default, run computes only the requested --subset and its dependencies to avoid unused local-search work.
  • --workers N — parallel workers for the pipeline (default: 1); set to the number of available cores to speed up covariance calculation (step 2) and, unless overridden below, matrix-to-vector, local search, and metric scoring as well
  • --matrix-workers N — override parallel workers for matrix-to-vector partition processing (default: inherit --workers)
  • --local-search-workers N — override parallel workers for local search (default: inherit --workers). Higher values can multiply RAM use because each worker loads its own covariance window.
  • --metric-workers N — override parallel workers for streaming metric row passes during breakpoint scoring (default: inherit --workers)
  • --high-precision — use 50-digit Decimal arithmetic for local search instead of the default float path (slower; mainly useful for exact reference comparisons)

Each of the five stages (partition, covariance, matrix-to-vector, find-minima, extract-bpoints) can also be run individually, along with a covariance-summary inspection utility — see docs/pipeline-steps.md.

Interpolate genetic maps

Convert a recombination rate map (e.g. the deCODE map or HapMap-interpolated 1000G maps) to per-SNP genetic positions required by steps 1 and 2:

uv run ldetect interpolate-maps \
  --snp-file snps.bed.gz \
  --genetic-map recombination_map.gz \
  --output chr2.interpolated_genetic_map.gz

Arguments:

  • --snp-file PATH — bgzipped BED file of SNP positions (columns: chrom start end rs_id); typically extracted from a filtered VCF with bcftools query -f '%CHROM\t%POS0\t%POS\t%ID\n'
  • --genetic-map PATH — gzipped recombination map; interpolation is used to assign a cM value to each SNP position
  • --output PATH — gzipped output map in the 3-column format expected by steps 1 and 2 (rs_id position cM)
  • --mode {point,interval} (default: point) — interpolation algorithm:
    • point — treats --genetic-map as discrete (position, cM) points and linearly interpolates between the two points bracketing each SNP. Correct for point-sampled maps (e.g. HapMap-interpolated 1000G maps).
    • interval — treats each map row as the start of a genomic interval with its own recombination rate (Begin, rate_cM_Mb, cumulative_cM_at_End), matching MacDonald et al.'s R interpolation scripts (interpolate.R/interpolate_pyhro.R). Required for interval-rate maps such as the deCODE map — feeding those into point mode silently uses the next interval's rate for SNPs in the current interval, an off-by-one bug that produced a ~0.001–0.003 cM error per SNP in earlier testing (see notes/findings/macdonald2022-reproduction.md).

Algorithm

The pipeline detects LD block boundaries by finding local minima in a smoothed diagonal-sum signal derived from the shrinkage LD covariance matrix:

  1. Partition — chromosome split into ~5000-SNP overlapping windows at low-recombination boundaries
  2. Covariance — Wen & Stephens shrinkage estimator applied to phased haplotypes; shrinks sample correlations toward the expected LD decay to reduce finite-sample noise
  3. Matrix → vector — each covariance matrix reduced to a [position, diagonal_sum] signal; troughs correspond to LD block boundaries
  4. Find minima — binary search for optimal Hanning-window filter width; scipy.signal.argrelextrema finds local minima; local search refines each breakpoint using sum of squared inter-block correlations as the quality metric
  5. Extract — chosen breakpoint set written as BED

The available breakpoint sets are fourier and uniform (raw minima from Fourier-filtered and uniformly-spaced candidates), plus fourier_ls and uniform_ls (after local search refinement). fourier_ls is the recommended output.

Known limitations

ldetect-lite reproduces the published Berisa & Pickrell (2016) 1000 Genomes LD blocks exactly for ASN (all 22 autosomes) and AFR (all chromosomes except chr22), and matches EUR block counts and coverage exactly but with shifted internal boundaries on chr8–chr12. These two residual divergences (EUR chr8-12, AFR chr22) are understood to stem from an unidentified upstream input/provenance difference from the original authors' pipeline, not a bug in this implementation — an extensive diagnostic effort ruled out VCF release-version provenance, SNP filtering, genetic map family, Ne assignment, duplicate/cross-partition handling, and reference-BED integrity as causes. See notes/findings/ldetect-original-reproduction.md for the full writeup, and notes/findings/macdonald2022-reproduction.md for the equivalent status reproducing MacDonald et al. (2022)'s GRCh38 blocks.

Pre-computed LD blocks

Pre-computed BED files for 1000 Genomes reference populations are available from in hg19 coordinates from the original LDetect data repository and in hg38 coordinates from a more recent effort by MacDonald et al. (2022).

BED files produced by our work will be released once the code base leaves alpha/finish major breaking updates.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

ldetect_lite-0.1.0.tar.gz (995.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

ldetect_lite-0.1.0-py3-none-any.whl (85.6 kB view details)

Uploaded Python 3

File details

Details for the file ldetect_lite-0.1.0.tar.gz.

File metadata

  • Download URL: ldetect_lite-0.1.0.tar.gz
  • Upload date:
  • Size: 995.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for ldetect_lite-0.1.0.tar.gz
Algorithm Hash digest
SHA256 7db87015cd99cbd1dcc7fde2c8b479e4a203d9a2a397cd62e91b8ad39eb48805
MD5 5c9cd48797f270da7b1d13997ec82191
BLAKE2b-256 d99475a6f439b2a5fec45602bf7d12c7b964ca45a4d49dbde39ceb724fd2290e

See more details on using hashes here.

Provenance

The following attestation bundles were made for ldetect_lite-0.1.0.tar.gz:

Publisher: publish.yml on adamyhe/LDetect-lite

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file ldetect_lite-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: ldetect_lite-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 85.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for ldetect_lite-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 0b173f067d2ea5b825d97dfb4e82985380db254200e08413eee9891aec2813a4
MD5 55d85566d554b3dc47ac2ce6726792c2
BLAKE2b-256 bc5111d1ac9b4d57364098d016fda95d322be5d695a896001b790418755f6680

See more details on using hashes here.

Provenance

The following attestation bundles were made for ldetect_lite-0.1.0-py3-none-any.whl:

Publisher: publish.yml on adamyhe/LDetect-lite

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page