Skip to content

graphld.genesets

Gene set annotation utilities.

genesets

Gene set annotation utilities for graphREML.

This module keeps the GraphLD-facing LDSC annotation contract while reusing the lightweight score-test implementation for gene-table loading, GMT loading, position encoding, nearest-gene mapping, and gene-to-variant conversion.

convert_gene_sets_to_variant_annotations

convert_gene_sets_to_variant_annotations(gene_sets: dict[str, list[str]], variant_table: DataFrame, gene_table: DataFrame, nearest_weights: ndarray) -> pl.DataFrame

Convert gene sets to LDSC-style variant-level annotations.

Parameters:

Name Type Description Default
gene_sets dict[str, list[str]]

Dictionary mapping gene set names to gene symbols or Ensembl IDs

required
variant_table DataFrame

Variant table DataFrame with CHR, POS, SNP columns

required
gene_table DataFrame

Gene table DataFrame with CHR, POS, gene_id, gene_name columns

required
nearest_weights ndarray

Weights for k-nearest genes

required

Returns:

Type Description
DataFrame

DataFrame with CHR, BP, SNP, CM, and one column per gene set.

Source code in src/graphld/genesets.py
def convert_gene_sets_to_variant_annotations(
    gene_sets: dict[str, list[str]],
    variant_table: pl.DataFrame,
    gene_table: pl.DataFrame,
    nearest_weights: np.ndarray,
) -> pl.DataFrame:
    """Convert gene sets to LDSC-style variant-level annotations.

    Args:
        gene_sets: Dictionary mapping gene set names to gene symbols or Ensembl IDs
        variant_table: Variant table DataFrame with CHR, POS, SNP columns
        gene_table: Gene table DataFrame with CHR, POS, gene_id, gene_name columns
        nearest_weights: Weights for k-nearest genes

    Returns:
        DataFrame with CHR, BP, SNP, CM, and one column per gene set.
    """
    return gene_sets_to_variant_annotation_frame(
        gene_sets,
        variant_table,
        gene_table,
        nearest_weights,
        variant_id_col="SNP",
        output_id_col="SNP",
    )

load_gene_annotations

load_gene_annotations(gene_annot_dir: str, variant_table: DataFrame, gene_table_path: str, nearest_weights: ndarray, annot_names: Optional[list[str]] = None) -> pl.DataFrame

Load GMT gene annotations and convert them to variant-level annotations.

Source code in src/graphld/genesets.py
def load_gene_annotations(
    gene_annot_dir: str,
    variant_table: pl.DataFrame,
    gene_table_path: str,
    nearest_weights: np.ndarray,
    annot_names: Optional[list[str]] = None,
) -> pl.DataFrame:
    """Load GMT gene annotations and convert them to variant-level annotations."""
    chromosomes = variant_table["CHR"].unique().sort().to_list()
    gene_table = load_gene_table(gene_table_path, chromosomes)
    gene_sets = load_gene_sets_from_gmt(gene_annot_dir)

    if annot_names:
        gene_sets = {name: genes for name, genes in gene_sets.items() if name in annot_names}

    return convert_gene_sets_to_variant_annotations(
        gene_sets,
        variant_table,
        gene_table,
        nearest_weights,
    )