Repository navigation
Conversation
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #455. Based off of #454 so merge that first.
Fixes the
GenotypeMatrix.from_vcf/HaplotypeMatrix.from_vcfAPI asymmetry noted in the linked issue.GenotypeMatrix.from_vcfnow acceptsregion=andsamples=, mirroringHaplotypeMatrix.from_vcf's region-parsing/tabix-rendering andchrom_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 atregion=instead of an external subsetting step. It also gained the "no variants found" guardHaplotypeMatrix.from_vcfalready had for an empty read, which this loader lacked entirely.I considered collapsing the two loaders into one (
GenotypeMatrix.from_vcfdelegating toHaplotypeMatrix.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_matrixreads the already accessible-filtered view and bakes it into new storage, so composing would silently turnGenotypeMatrix.from_vcf(accessible_bed=...)'s accessible mask from non-destructive (reversible viaremove_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.