Skip to content

Add region and samples arguments to GenotypeMatrix.from_vcf - #456

Open
nspope wants to merge 3 commits into
mainfrom
feature/genotype-matrix-vcf-region-samples
Open

nspope wants to merge 3 commits into
mainfrom
feature/genotype-matrix-vcf-region-samples

Conversation

@nspope

@nspope nspope commented Nov 20, 2026

Copy link
Copy Markdown
Collaborator

Closes #455. Based off of #454 so merge that first.

Fixes the GenotypeMatrix.from_vcf/HaplotypeMatrix.from_vcf API asymmetry noted in the linked issue. GenotypeMatrix.from_vcf now accepts region= and samples=, mirroring HaplotypeMatrix.from_vcf's region-parsing/tabix-rendering and chrom_start/chrom_end-from-region logic, and its multi-contig guard (added in #398) now only fires when no region is given, with the remedy pointing at region= instead of an external subsetting step. It also gained the "no variants found" guard HaplotypeMatrix.from_vcf already had for an empty read, which this loader lacked entirely.

I considered collapsing the two loaders into one (GenotypeMatrix.from_vcf delegating to HaplotypeMatrix.from_vcf + GenotypeMatrix.from_haplotype_matrix) to eliminate the now-duplicated region/bounds logic, and verified empirically that the core genotype/dosage math is identical between the two path. However, from_haplotype_matrix reads the already accessible-filtered view and bakes it into new storage, so composing would silently turn GenotypeMatrix.from_vcf(accessible_bed=...)'s accessible mask from non-destructive (reversible via remove_accessible_mask(), the established convention throughout this codebase) into destructive (the masked-out sites would be gone for good). So, I'm keeping the two loaders independent and accepting the code duplication.

from_vcf loaded every record unconditionally when no region= was given,
concatenating positions from every contig into one coordinate axis with
no record of where one contig ends and the next begins. Any statistic
that depends on inter-site distance or span (pairwise LD, span-normalized
rates, include_invariant's accessible mask, windowed analysis) could then
pair or window sites from two different chromosomes as if they were
adjacent and linked.

The multi-contig guard already existed (_data_contig, shared with
from_zarr's accessible-mask path) but only ran when accessible_bed was
also given. It now runs whenever region is None, mirroring from_zarr's
unconditional guard. _data_contig's error message and docstring no
longer frame this as an accessible-mask-specific rule, since every call
site now reflects the same general constraint: the matrix has one
chrom_start/chrom_end, so it assumes one contig.
Same fix as HaplotypeMatrix.from_vcf: the multi-contig guard only ran
when accessible_bed was given, so a multi-contig VCF loaded silently
otherwise. GenotypeMatrix.from_vcf has no region= to select one contig
with, so the remedy is to subset the VCF first (e.g. bcftools view -r).

Also moved the check before the biallelic filtering and dosage
conversion, which previously ran (and built the GenotypeMatrix) before
the accessible_bed-gated check could reject the result; a rejected VCF
no longer pays for either.

The loader-level tests confirm HaplotypeMatrix.from_vcf and
GenotypeMatrix.from_vcf reject a multi-contig VCF directly, but neither
exercised the public entry point the original bug report reproduced
against. Added a test through compute_ld_statistics(vcf_file=...) on
both the genotype and haplotype paths, confirmed to fail (silently
returning a bogus LD value) against the pre-fix loaders.
GenotypeMatrix.from_vcf had neither, unlike HaplotypeMatrix.from_vcf,
so selecting one contig or a sample subset meant pre-filtering the VCF
with an external tool first. Ports HaplotypeMatrix.from_vcf's own
region-parsing/tabix-rendering and chrom_start/chrom_end-from-region
logic; the multi-contig guard now only fires when no region is given,
since a given region already narrows to one contig, and its remedy
points at region= instead of an external subsetting step. Also added
the same "no variants found" guard HaplotypeMatrix.from_vcf already has
for an empty read (e.g. a region matching nothing), which GenotypeMatrix.from_vcf
lacked entirely; without region= this was nearly unreachable, but an
empty region makes it an easy way to hit a raw TypeError instead of a
clear error.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

GenotypeMatrix.from_vcf has no region= or samples=, unlike HaplotypeMatrix.from_vcf

1 participant