Search Shortcut cmd + k | ctrl + k

plinking_duck

Read and write PLINK 2 genomics file formats and run common genetic analyses directly in SQL

Maintainer(s): teaguesterling

Compatibility Matrix

This table covers all DuckDB releases that have not reached end-of-life, including LTS releases, and shows the 2 most recent patch releases of each. Older patch releases are not listed, but the extension may be available for them as well.

Platform 1.5.5 1.5.6 2.0.0 2.0.1
Linux AMD64
Linux ARM64
macOS AMD64
macOS ARM64
Windows AMD64
Windows ARM64
WebAssembly

Installing and Loading

INSTALL plinking_duck FROM community;
LOAD plinking_duck;

Example

-- Read variant metadata from a .pvar file
SELECT CHROM, POS, ID, REF, ALT
FROM read_pvar('cohort.pvar')
WHERE CHROM = '22';

-- Read genotypes in tidy format (one row per variant x sample)
SELECT chrom, pos, iid, genotype
FROM read_pfile('cohort', orient := 'genotype')
LIMIT 10;

-- Compute allele frequencies
SELECT * FROM plink_freq('cohort.pgen')
WHERE ALT_FREQ > 0.01;

-- Run a GWAS association test
SELECT * FROM plink_glm('cohort')
WHERE p_value < 5e-8;

About plinking_duck

PlinkingDuck brings PLINK 2 genotype, variant, and sample data into DuckDB, letting you query genomics datasets with standard SQL instead of format-specific command-line tools.

File readers:

  • read_pvar(path | [paths]) — variant metadata (.pvar/.bim); a list row-concatenates multiple files
  • read_psam(path) — sample metadata (.psam/.fam)
  • read_pgen(path) — binary genotypes (.pgen)
  • read_pfile(prefix | [prefixes]) — unified fileset reader with orient modes (variant/genotype/sample), sample subsetting, region and variant filtering; a list of prefixes reads a variant-sharded fileset (identical samples) as one table, and combine_samples := 'concatenate' | 'disjoint' reads a sample-sharded cohort (one shared .pvar) as one table; and read_pfile(catalog := '<cohort>.pfile.json') reads a version-2 matrix as one 2-D cohort across its variant and sample blocks. read_pfile('<prefix>') now also discovers a <prefix>.pfile.json catalog beside the prefix, catalog := reads version-1 catalogs (a sharded cohort or single fileset) as well, and a WHERE on CHROM/POS or IID prunes catalog cells on both axes before they are read
  • read_plink_vcf(path) — fast biallelic genotype extraction from VCF/VCF.gz (~3x faster than htslib)

Writing & catalogs:

  • COPY (…) TO 'out' (FORMAT pvar | psam | pgen | pfile) — write PLINK 2 filesets from SQL. shard_by '<col>' splits a cohort into one fileset per key (e.g. per chromosome); shard_samples {label: [iids], …} splits it along the sample axis into named groups over one shared .pvar; giving both at once writes a matrix — an M × N grid of filesets described by a "pfile_catalog": 2 catalog. A COPY errors if its output already exists — add force to replace it
  • .pfile.json catalog — a thin JSON catalog over a set of filesets (a shared .pvar/.psam plus per-file partition and genomic-range annotations); the sharded writers emit one as their manifest. The matrix form ("pfile_catalog": 2) tiles a sparse 2-axis variants × samples grid of filesets with catalog-defined typed keys, pruned on both axes
  • ATTACH 'cohort.pfile.json' (TYPE pfile_catalog) — expose any catalog (a single fileset, a sharded cohort, or a matrix) as a read-only database with variants, samples, genotypes, cells, and per-axis block relations; pushdown-aware and DETACH-clean
  • read_pfile_catalog(path) — parse, verify, and prune a catalog (1-D or the v2 matrix), returning one row per surviving shard/cell with resolved companion paths (text or parquet)
  • read_pfile_catalog_metadata(path) / read_pfile_catalog_layout(path) — inspect a catalog without reading data: its declared schema, and its full chunk map (one row per cell, holes included)

Analysis functions:

  • plink_freq — per-variant allele frequencies via fast genotype counting
  • plink_hardy — Hardy-Weinberg equilibrium exact test
  • plink_missing — per-variant or per-sample missingness rates
  • plink_ld — pairwise linkage disequilibrium (r², D, D')
  • plink_score — polygenic risk scoring with mean imputation
  • plink_glm — per-variant GWAS regression (linear, logistic, Firth)
  • plink_pca — principal component analysis via randomized SVD

Genotype output modes:

  • genotypes='struct' — STRUCT with named fields per sample
  • genotypes='counts' — fast genotype counting (no decompression)
  • genotypes='stats' — counts + AF, MAF, missingness, heterozygosity

Flexible inputs:

  • Unified variants parameter: indices, rsids, CPRA strings/structs, ranges
  • Parquet/CSV/table companions for variant and sample metadata
  • af_range / ac_range filter pushdown, and include_genotypes (hardcall category) / genotype_range for fast carrier lookups — in orient := 'sample' only the matching subjects are materialized

Remote / cloud reads:

  • Read .pgen filesets directly from s3://, https://, and other DuckDB filesystems (companions too) — targeted/region queries fetch only the bytes they need via HTTP range reads. plinking_pgen_io := 'localize' downloads once for remote full scans. Split-index (.pgen.pgi) filesets are read transparently.

All functions support projection pushdown (skip genotype decompression for metadata-only queries), parallel scanning, sample subsetting, and region filtering. Legacy PLINK 1 formats (.bim/.fam) are auto-detected.

Built on pgenlib for efficient access to the compressed .pgen binary format without full decompression.

For full documentation, see plinking-duck.readthedocs.io.

Added Functions

function_name function_type description comment examples
pfile_partitions table Compute the ordinal-range partitions of a pfile along the variants dimension (half-open [lo, hi)). NULL [SELECT * FROM pfile_partitions('data/cohort')]
plink_freq table Compute allele frequencies and counts from PLINK files. NULL [SELECT * FROM plink_freq('data/cohort.pgen')]
plink_glm table Fit generalized linear models (linear/logistic regression) across variants. NULL [SELECT * FROM plink_glm('data/cohort.pgen', phenotype := 'trait')]
plink_hardy table Compute Hardy-Weinberg equilibrium exact test statistics. NULL [SELECT * FROM plink_hardy('data/cohort.pgen')]
plink_ld table Compute pairwise linkage disequilibrium metrics. NULL [SELECT * FROM plink_ld('data/cohort.pgen', window_kb := 500, r2_threshold := 0.2)]
plink_missing table Compute missingness rates per variant or per sample. NULL [SELECT * FROM plink_missing('data/cohort.pgen', mode := 'variant')]
plink_pca table Compute principal components analysis from PLINK files. NULL [SELECT * FROM plink_pca('data/cohort.pgen', n_pcs := 10)]
plink_score table Compute polygenic risk scores from variant weights. NULL [SELECT * FROM plink_score('data/cohort.pgen', weights := 'weights.tsv')]
plinking_debug_genotype_where_pushed scalar Test-only: reads whose WHERE on genotype compiled into the scan's genotype filter since the last reset (views PR 2a). NULL [SELECT plinking_debug_genotype_where_pushed()]
plinking_debug_genotype_where_pushed_reset scalar Test-only: reset plinking_debug_genotype_where_pushed to 0. NULL [SELECT plinking_debug_genotype_where_pushed_reset()]
plinking_debug_iid_where_pushed scalar Test-only: reads whose WHERE on IID narrowed the sample set since the last reset. NULL [SELECT plinking_debug_iid_where_pushed()]
plinking_debug_iid_where_pushed_reset scalar Test-only: reset plinking_debug_iid_where_pushed to 0. NULL [SELECT plinking_debug_iid_where_pushed_reset()]
plinking_debug_pgen_header_decodes scalar Test-only: .pgen header decodes (PgfiInitPhase2 runs) by read_pfile since the last reset (cache tier 1). NULL [SELECT plinking_debug_pgen_header_decodes()]
plinking_debug_pgen_header_decodes_reset scalar Test-only: reset plinking_debug_pgen_header_decodes to 0. NULL [SELECT plinking_debug_pgen_header_decodes_reset()]
plinking_debug_psam_iid_loads scalar Test-only: full IID loads of a .psam.parquet since the last reset. NULL [SELECT plinking_debug_psam_iid_loads()]
plinking_debug_psam_iid_loads_reset scalar Test-only: reset plinking_debug_psam_iid_loads to 0. NULL [SELECT plinking_debug_psam_iid_loads_reset()]
plinking_debug_sparse_concat_altmajor scalar Test-only: per-shard ALT/missing-major difflist fallbacks taken by the concat sparse path since the last reset (P13-011 Task 2 branch proof). NULL [SELECT plinking_debug_sparse_concat_altmajor()]
plinking_debug_sparse_concat_dense scalar Test-only: per-shard dense-genovec fallbacks taken by the concat sparse path since the last reset (P13-011 Task 2 branch proof). NULL [SELECT plinking_debug_sparse_concat_dense()]
plinking_debug_sparse_variants scalar Test-only: number of variants decoded via the sparse (pgen difflist) path since the last reset (P13-011 proof-ran control). NULL [SELECT plinking_debug_sparse_variants()]
plinking_debug_sparse_variants_reset scalar Test-only: reset the sparse-difflist proof-ran counter to 0, returns 0 (P13-011). NULL [SELECT plinking_debug_sparse_variants_reset()]
plinking_duck_capabilities scalar List the capability tokens this plinking_duck build supports (for SQL feature detection). NULL [SELECT list_contains(plinking_duck_capabilities(), 'variant_range')]
read_pfile table Read PLINK dataset files into tabular format. NULL [SELECT * FROM read_pfile('data/cohort.pgen')]
read_pfile_catalog table Parse, verify, and prune a .pfile.json shard catalog, returning one row per surviving shard. The parse+verify+prune surface plinking_duck owns for pfile_catalog reading (write side = the sharded pfile writer). NULL [SELECT * FROM read_pfile_catalog('cohort.pfile.json'), SELECT prefix FROM read_pfile_catalog('cohort.pfile.json', region := '2:1-5000000')]
read_pfile_catalog_layout table Interpreted chunk map of a .pfile.json catalog: one row per grid cell INCLUDING declared holes (present=false), with ordinal spans and template-resolved paths. Structural only; opens no data file. NULL [SELECT * FROM read_pfile_catalog_layout('cohort.pfile.json'), SELECT keys, variant_start, sample_start FROM read_pfile_catalog_layout('cohort.pfile.json') WHERE NOT present]
read_pfile_catalog_metadata table Read a .pfile.json catalog LITERALLY: one row per declared partition key (axis, key, type, over, position). The declared schema only; no tiling, no pruning, no data-file I/O. NULL [SELECT * FROM read_pfile_catalog_metadata('cohort.pfile.json'), SELECT axis, key, type FROM read_pfile_catalog_metadata('cohort.pfile.json') WHERE type <> 'enum']
read_pgen table Read PLINK .pgen or .bed genotype files into tabular format. NULL [SELECT * FROM read_pgen('data/cohort.pgen')]
read_plink_vcf table Read VCF files using PLINK engine into tabular format. NULL [SELECT * FROM read_plink_vcf('data/cohort.vcf.gz')]
read_psam table Read PLINK .psam or .fam sample files into tabular format. NULL [SELECT * FROM read_psam('data/cohort.psam')]
read_pvar table Read PLINK .pvar or .bim variant files into tabular format. NULL [SELECT * FROM read_pvar('data/cohort.pvar')]

Overloaded Functions

This extension does not add any function overloads.

Added Types

This extension does not add any types.

Added Settings

name description input_type scope aliases typed_value
plinking_localize_dir Directory (local) for temp copies made by plinking_pgen_io := 'localize'. Empty (default) uses DuckDB's temporary_directory, else the current directory. Created if absent (one level only — parent dirs must already exist). Temps are per-query and removed when the query's bind data is destroyed. VARCHAR GLOBAL []  
plinking_max_matrix_elements Maximum genotype matrix elements for orient := 'sample' pre-read (variants x samples). Default 16 billion (~16 GB of int8). BIGINT GLOBAL [] 17179869184
plinking_max_threads Maximum threads for parallel scan operations. 0 = default (hardcoded cap of 16), >0 = cap at this value. BIGINT GLOBAL [] 0
plinking_pgen_header_cache Cache each decoded .pgen header (its per-variant index, 9 bytes per variant) once per database, shared by every thread and query of read_pfile. Bounded by DuckDB's object cache, which counts against memory_limit. false = decode per thread. BOOLEAN GLOBAL [] true
plinking_pgen_io How .pgen bytes are read: 'auto' (default — remote/VFS paths via DuckDB's VFS, local via native fopen), 'native' (always native fopen; errors on remote), 'vfs' (always via DuckDB's VFS, even local), 'localize' (materialize a local temp copy then read natively — best for remote full scans; always copies, even a local source). VARCHAR GLOBAL [] auto
plinking_sample_counts_sparse orient := 'sample' + genotypes := 'counts'|'stats': when true, use the sparse (pgen difflist) accumulation path — reads only the non-hom_ref carriers of rare variants (auto-falls-back to the dense full-decode path per variant). When false, always use the dense path. Toggle to A/B time both; both paths produce identical counts. BOOLEAN GLOBAL [] false
plinking_use_parquet_companions Auto-discover .pvar.parquet and .psam.parquet companion files. When true, parquet companions are preferred over text formats. BOOLEAN GLOBAL [] true
temp_directory Set the directory to which temporary files are written. VARCHAR GLOBAL [] post.db.tmp
© 2026 DuckDB Foundation, Amsterdam NL
DuckDB Home Code of Conduct Trademark Use Blog