score_test¶
Enrichment score test module for fast annotation testing.
Submodules¶
score_test.score_test: Core score test implementationscore_test.score_test_io: I/O functions for score test filesscore_test.meta_analysis: Meta-analysis across traitsscore_test.convert_scores: Convert between variant and gene-level scoresscore_test.genesets: Gene set utilities
score_test
¶
GraphLD Score Test - Fast heritability enrichment testing for genomic annotations.
This module provides a fast score test for testing genomic or gene annotations for heritability enrichment conditional upon a null model. The test produces Z scores where positive values indicate enrichment and negative values indicate depletion.
Main Functions
run_score_test: Run the enrichment score test on annotations load_variant_data: Load precomputed row-level score statistics load_trait_data: Load trait-specific score statistics load_annotations: Load annotation data from LDSC format files load_gene_table: Load gene position table load_gene_sets_from_gmt: Load gene sets from GMT format files gene_variant_matrix: Build variant-to-gene nearest-gene weight matrices convert_gene_to_variant_annotations: Convert gene-level to variant-level annotations
Example::
from score_test import load_trait_data, load_variant_data, run_score_test
from score_test.score_test_io import load_variant_annotations
row_data = load_variant_data("path/to/scores.h5")
trait_data = load_trait_data("path/to/scores.h5", "height", row_data)
annot = load_variant_annotations("path/to/annot_dir/", variant_table=row_data)
results = run_score_test(trait_data, annot)
run_score_test
¶
Run approximate score test for hypothesis testing of new annotation or functional category.
This function tests annotations against precomputed gradient statistics. It does not adjust for uncertainty in fitted model parameters.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
trait_data
|
TraitData
|
TraitData object containing variant data with gradients |
required |
annot
|
Annot
|
Annot object (VariantAnnot or GeneAnnot) containing annotations to test |
required |
Returns:
| Type | Description |
|---|---|
Tuple[ndarray, ndarray]
|
Tuple of (point_estimates, jackknife_estimates) |
Source code in src/score_test/score_test.py
load_trait_data
¶
Load trait data and combine with variant data into a TraitData object.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
hdf5_path
|
str
|
Path to HDF5 file containing variant and trait data |
required |
trait_name
|
str
|
Name of the trait to load |
required |
variant_table
|
DataFrame
|
Pre-loaded variant table DataFrame |
required |
Returns:
| Type | Description |
|---|---|
TraitData
|
TraitData object containing variant dataframe with gradients/corrections and parameters |
Source code in src/score_test/score_test_io.py
load_annotations
¶
load_annotations(annot_path: str, chromosome: Optional[int] = None, infer_schema_length: int = 100000, add_positions: bool = True, variant_table: DataFrame | None = None, exclude_bed: bool = False) -> pl.DataFrame
Load annotation data for specified chromosome(s).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
annot_path
|
str
|
Path to directory containing .annot and/or .bed files |
required |
chromosome
|
Optional[int]
|
Specific chromosome number, or None for all chromosomes |
None
|
infer_schema_length
|
int
|
Number of rows to infer schema from |
100000
|
add_positions
|
bool
|
If True, rename BP to POS |
True
|
variant_table
|
DataFrame | None
|
Variant table to use as the coordinate universe for BED-only annotation directories |
None
|
exclude_bed
|
bool
|
If True, skip loading .bed files from the annotations directory |
False
|
Returns:
| Type | Description |
|---|---|
DataFrame
|
DataFrame containing annotations |
Raises:
| Type | Description |
|---|---|
ValueError
|
If no matching annotation files are found |
Source code in src/score_test/score_test_io.py
223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 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 | |
convert_gene_to_variant_annotations
¶
convert_gene_to_variant_annotations(gene_annot: object, variant_table: DataFrame, gene_table: DataFrame, nearest_weights: ndarray) -> object
Convert gene annotations to variant-level annotations.
Can accept either: - GeneAnnot object with gene_sets and annot_names attributes - dict[str, list[str]] mapping gene set names to gene lists
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
gene_annot
|
object
|
GeneAnnot object or dict mapping set names to lists of genes (symbols or IDs) |
required |
variant_table
|
DataFrame
|
Variant table DataFrame with CHR, POS, RSID columns |
required |
gene_table
|
DataFrame
|
Gene table DataFrame |
required |
nearest_weights
|
ndarray
|
Weights for k-nearest genes |
required |
Returns:
| Type | Description |
|---|---|
object
|
VariantAnnot object with variant-level annotations, if gene_annot is GeneAnnot. |
object
|
DataFrame with variant-level annotations in LDSC format, if gene_annot is dict. |
Source code in src/score_test/genesets.py
gene_variant_matrix
¶
gene_variant_matrix(variant_table: DataFrame, gene_table: DataFrame, nearest_weights: ndarray, dtype: dtype = np.float64) -> csr_matrix
Compute a chromosome-aware variants x genes weighted matrix.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
variant_table
|
DataFrame
|
DataFrame with CHR, POS columns |
required |
gene_table
|
DataFrame
|
DataFrame with CHR, POS columns (POS is midpoint for genes) |
required |
nearest_weights
|
ndarray
|
Weights for k-nearest genes |
required |
dtype
|
dtype
|
Data type of the sparse matrix values |
float64
|
Returns:
| Type | Description |
|---|---|
csr_matrix
|
Sparse matrix mapping variants to genes with shape (nvariants, ngenes) |
Source code in src/score_test/genesets.py
score_test
¶
TraitData
dataclass
¶
TraitData(df: DataFrame, params: ndarray | None = None, jk_params: ndarray | None = None, annot_names: list[str] | None = None, keys: list[str] | None = None)
exclude_cols
property
¶
Columns to exclude when determining annotation names.
Annot
¶
Base class for annotations that can be merged with TraitData.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
annot_names
|
List[str]
|
List of annotation column names to test |
required |
other_key
|
str | list[str]
|
Column name(s) to use for merging (e.g., 'RSID', 'gene_id', or ['CHR', 'POS']) |
required |
Source code in src/score_test/score_test.py
merge
¶
merge(trait_data: TraitData) -> tuple[np.ndarray, np.ndarray | None, np.ndarray | None, np.ndarray, np.ndarray]
Merge with TraitData and return extracted arrays.
Returns:
| Type | Description |
|---|---|
ndarray
|
Tuple of (grad, hessian, model_annot, test_annot, block_boundaries). |
ndarray | None
|
hessian and model_annot can be None when unavailable. |
Source code in src/score_test/score_test.py
VariantAnnot
¶
Bases: Annot
Variant-level annotations.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
df
|
DataFrame
|
DataFrame with variant annotations |
required |
annot_names
|
List[str]
|
List of annotation column names to test |
required |
other_key
|
str | list[str]
|
Column name(s) to use for merging with trait data |
'RSID'
|
Source code in src/score_test/score_test.py
perturb
¶
Perturb binary annotations.
Source code in src/score_test/score_test.py
merge
¶
merge(trait_data: TraitData) -> tuple[np.ndarray, np.ndarray | None, np.ndarray | None, np.ndarray, np.ndarray]
Merge variant annotations with TraitData.
Returns:
| Type | Description |
|---|---|
ndarray
|
Tuple of (grad, hessian, model_annot, test_annot, block_boundaries). |
ndarray | None
|
hessian is None if not available; model_annot is None if the |
ndarray | None
|
fitted model has no conditioning annotations. |
Source code in src/score_test/score_test.py
GeneAnnot
¶
Bases: Annot
Gene-level annotations.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
gene_sets
|
dict[str, list[str]]
|
Dictionary mapping set names to lists of genes |
required |
Source code in src/score_test/score_test.py
merge
¶
Merge gene annotations with gene-level TraitData.
Creates one-hot encodings for each gene set and returns unmodified gradients from the TraitData.
Returns:
| Type | Description |
|---|---|
ndarray
|
Tuple of (grad, hessian, model_annot, test_annot, block_boundaries). |
None
|
hessian and model_annot are currently None for gene-set tests. |
Source code in src/score_test/score_test.py
run_score_test
¶
Run approximate score test for hypothesis testing of new annotation or functional category.
This function tests annotations against precomputed gradient statistics. It does not adjust for uncertainty in fitted model parameters.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
trait_data
|
TraitData
|
TraitData object containing variant data with gradients |
required |
annot
|
Annot
|
Annot object (VariantAnnot or GeneAnnot) containing annotations to test |
required |
Returns:
| Type | Description |
|---|---|
Tuple[ndarray, ndarray]
|
Tuple of (point_estimates, jackknife_estimates) |
Source code in src/score_test/score_test.py
main
¶
main(variant_stats_hdf5, output_fp, variant_annot_dir, gene_annot_dir, random_genes, random_variants, gene_table, nearest_weights, annotations, trait_name, verbose, seed, perturb_annot)
Run score test for annotation enrichment.
Source code in src/score_test/score_test.py
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 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 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 | |
score_test_io
¶
I/O operations for score test.
is_gene_level_hdf5
¶
Check if HDF5 file contains gene-level data.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
hdf5_path
|
str
|
Path to HDF5 file |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if file contains gene-level data, False for variant-level |
Source code in src/score_test/score_test_io.py
load_row_data
¶
Load row data table from HDF5 file format.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
hdf5_path
|
str
|
Path to the HDF5 file |
required |
Returns:
| Type | Description |
|---|---|
DataFrame
|
Polars DataFrame containing row data (variants or genes) |
Source code in src/score_test/score_test_io.py
get_trait_names
¶
Get list of trait names from HDF5 file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
hdf5_path
|
str
|
Path to the HDF5 file containing trait data |
required |
trait_name
|
Optional[str]
|
Optional specific trait name or meta-analysis name. If provided, returns list with just this trait, or expands meta-analysis to its traits. If None, returns all trait names in the file. |
None
|
Returns:
| Type | Description |
|---|---|
List[str]
|
List of trait names |
Source code in src/score_test/score_test_io.py
load_trait_hdf5
¶
Load trait data from HDF5 file format.
Returns:
| Type | Description |
|---|---|
dict
|
Dictionary with keys: gradient (required), and optionally parameters, jackknife_parameters, other datasets |
Source code in src/score_test/score_test_io.py
load_trait_data
¶
Load trait data and combine with variant data into a TraitData object.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
hdf5_path
|
str
|
Path to HDF5 file containing variant and trait data |
required |
trait_name
|
str
|
Name of the trait to load |
required |
variant_table
|
DataFrame
|
Pre-loaded variant table DataFrame |
required |
Returns:
| Type | Description |
|---|---|
TraitData
|
TraitData object containing variant dataframe with gradients/corrections and parameters |
Source code in src/score_test/score_test_io.py
load_annotations
¶
load_annotations(annot_path: str, chromosome: Optional[int] = None, infer_schema_length: int = 100000, add_positions: bool = True, variant_table: DataFrame | None = None, exclude_bed: bool = False) -> pl.DataFrame
Load annotation data for specified chromosome(s).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
annot_path
|
str
|
Path to directory containing .annot and/or .bed files |
required |
chromosome
|
Optional[int]
|
Specific chromosome number, or None for all chromosomes |
None
|
infer_schema_length
|
int
|
Number of rows to infer schema from |
100000
|
add_positions
|
bool
|
If True, rename BP to POS |
True
|
variant_table
|
DataFrame | None
|
Variant table to use as the coordinate universe for BED-only annotation directories |
None
|
exclude_bed
|
bool
|
If True, skip loading .bed files from the annotations directory |
False
|
Returns:
| Type | Description |
|---|---|
DataFrame
|
DataFrame containing annotations |
Raises:
| Type | Description |
|---|---|
ValueError
|
If no matching annotation files are found |
Source code in src/score_test/score_test_io.py
223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 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 | |
load_variant_annotations
¶
load_variant_annotations(annot_dir: str, annot_names: list[str] | None = None, variant_table: DataFrame | None = None) -> VariantAnnot
Load variant-level annotations from directory.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
annot_dir
|
str
|
Directory containing .annot and/or .bed annotation files |
required |
annot_names
|
list[str] | None
|
Optional list of specific annotation names to load |
None
|
variant_table
|
DataFrame | None
|
Variant table to use as the coordinate universe for BED-only annotation directories |
None
|
Returns:
| Type | Description |
|---|---|
VariantAnnot
|
VariantAnnot object |
Source code in src/score_test/score_test_io.py
load_gene_annotations
¶
Load GMT gene-set files from a directory into a GeneAnnot object.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
gene_annot_dir
|
str
|
Directory containing GMT files with gene sets |
required |
annot_names
|
list[str] | None
|
Optional list of specific annotation names to load |
None
|
Returns:
| Type | Description |
|---|---|
GeneAnnot
|
GeneAnnot object |
Source code in src/score_test/score_test_io.py
create_random_gene_annotations
¶
create_random_gene_annotations(data_table: DataFrame, gene_table_path: str, probs: List[float]) -> GeneAnnot
Create random gene-level annotations as a GeneAnnot object.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data_table
|
DataFrame
|
Data table DataFrame (variants or genes) |
required |
gene_table_path
|
str
|
Path to gene table TSV file |
required |
probs
|
List[float]
|
List of probabilities for random gene selection |
required |
Returns:
| Type | Description |
|---|---|
GeneAnnot
|
GeneAnnot object with random gene sets |
Source code in src/score_test/score_test_io.py
create_random_variant_annotations
¶
Create random variant-level annotations as a VariantAnnot object.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
variant_table
|
DataFrame
|
Variant table DataFrame |
required |
probs
|
List[float]
|
List of probabilities for random annotation |
required |
Returns:
| Type | Description |
|---|---|
VariantAnnot
|
VariantAnnot object with random variant annotations |
Source code in src/score_test/score_test_io.py
get_trait_groups
¶
Get trait groups for meta-analysis from HDF5 file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
hdf5_path
|
str
|
Path to HDF5 file containing trait groups |
required |
Returns:
| Type | Description |
|---|---|
dict[str, list[str]]
|
Dictionary mapping group names to lists of trait names. |
dict[str, list[str]]
|
Returns empty dict if no groups are defined. |
Source code in src/score_test/score_test_io.py
save_trait_groups
¶
Save trait groups for meta-analysis to HDF5 file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
hdf5_path
|
str
|
Path to HDF5 file to create/update |
required |
groups
|
dict[str, list[str]]
|
Dictionary mapping group names to lists of trait names |
required |
Source code in src/score_test/score_test_io.py
save_trait_data
¶
Save trait data to HDF5 file in new format.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
trait_data
|
TraitData
|
TraitData object to save |
required |
hdf5_path
|
str
|
Path to HDF5 file to create/update |
required |
trait_name
|
str
|
Name of the trait |
required |
Source code in src/score_test/score_test_io.py
574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 | |