VariantStore
quantnado.dataset.store_variants.VariantStore
¶
VariantStore(
store_path: Path | str,
sample_names: list[str],
*,
overwrite: bool = True,
resume: bool = False,
read_only: bool = False,
)
Bases: BaseStore
Zarr-backed SNP/variant store from per-sample VCF.gz files.
Data is stored sparsely - only variant positions are retained.
Variant positions are unioned across all samples; positions not called
in a sample are filled with genotype -1 (missing) and depths 0.
Per-chromosome zarr layout::
<chrom>/
positions int64[n_variants] 1-based genomic positions
genotype int8[n_samples, n_variants] -1 missing, 0 hom_ref, 1 het, 2 hom_alt
allele_depth_ref int32[n_samples, n_variants]
allele_depth_alt int32[n_samples, n_variants]
qual float32[n_samples, n_variants]
Alleles (ref/alt) are stored as lists in each chromosome group's attributes,
indexed by position order. Retrieve with :meth:get_alleles.
Example
store = VariantStore.from_vcf_files( ... vcf_files=["sample1.vcf.gz", "sample2.vcf.gz"], ... store_path="variants.zarr", ... ) xr_dict = store.to_xarray(variable="genotype") region = store.extract_region("chr21:5000000-6000000")
Source code in quantnado/dataset/store_variants.py
open
classmethod
¶
Open an existing VariantStore for reading (default) or writing.
Source code in quantnado/dataset/store_variants.py
from_vcf_files
classmethod
¶
from_vcf_files(
vcf_files: list[str | Path],
store_path: Path | str,
sample_names: list[str] | None = None,
metadata: DataFrame | Path | str | None = None,
*,
filter_chromosomes: bool = True,
overwrite: bool = True,
resume: bool = False,
sample_column: str = "sample_id",
) -> "VariantStore"
Create a VariantStore from per-sample VCF.gz files.
Each file is treated as a single-sample VCF; the first sample in each
file is used. Variant positions are unioned across all samples per
chromosome; positions not called in a sample receive genotype -1
(missing) and allele depths of 0.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
vcf_files
|
list of str or Path
|
Paths to VCF/VCF.gz files (one per sample). |
required |
store_path
|
Path or str
|
Output Zarr store path. |
required |
sample_names
|
list of str
|
Sample names aligned with |
None
|
metadata
|
DataFrame, Path, or str
|
Sample metadata CSV to attach. |
None
|
filter_chromosomes
|
bool
|
Keep only canonical chromosomes (chr* without underscores). |
True
|
overwrite
|
bool
|
Overwrite existing store. |
True
|
resume
|
bool
|
Resume an existing store. |
False
|
sample_column
|
str
|
Column in metadata matching sample names. |
"sample_id"
|
Source code in quantnado/dataset/store_variants.py
293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 | |
set_metadata
¶
Store metadata columns from a DataFrame.
Source code in quantnado/dataset/store_variants.py
get_positions
¶
get_alleles
¶
Return (ref, alt) allele lists for a chromosome.
Each list is aligned with :meth:get_positions.
Source code in quantnado/dataset/store_variants.py
to_xarray
¶
to_xarray(
chromosomes: list[str] | None = None,
variable: str = "genotype",
) -> dict[str, xr.DataArray]
Extract variant data as per-chromosome Xarray DataArrays (lazy dask-backed).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
chromosomes
|
list[str]
|
Chromosomes to extract. Defaults to all. |
None
|
variable
|
str
|
Which array to extract: |
"genotype"
|
Returns:
| Type | Description |
|---|---|
dict[str, DataArray]
|
Each DataArray has dims |
Source code in quantnado/dataset/store_variants.py
extract_region
¶
extract_region(
region: str | None = None,
chrom: str | None = None,
start: int | None = None,
end: int | None = None,
variable: str = "genotype",
samples: list[str] | list[int] | None = None,
as_xarray: bool = True,
) -> xr.DataArray | np.ndarray
Extract variant data for a genomic region.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
region
|
str
|
Region string, e.g. |
None
|
chrom
|
optional
|
Alternative to |
None
|
start
|
optional
|
Alternative to |
None
|
end
|
optional
|
Alternative to |
None
|
variable
|
str
|
Which variable to return. |
"genotype"
|
samples
|
list
|
Sample names or integer indices. Defaults to all. |
None
|
as_xarray
|
bool
|
Return an xr.DataArray; if False return np.ndarray. |
True
|
Source code in quantnado/dataset/store_variants.py
497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 | |