Skip to content

mecfs_bio.build_system.task.sldsc.prepared_sldsc_reference

Format-neutral on-disk contract for S-LDSC reference data.

A prepared annotation set is a directory holding: ld_scores.parquet: SNP, CHR, BP, then one float32 column per unique LD-score source, rows sorted by CHR then BP with unique SNPs. m_5_50.parquet: one row per LD column: column id, genome-wide M_5_50, source. models.parquet: one row per analysis label: label, target column id, control column ids.

A prepared baseline holds ld_scores.parquet and m_5_50.parquet for the baseline annotations plus w_ld.parquet (SNP and the regression-weight LD score). Its rows are restricted to SNPs that have a weight LD score (the HapMap3 weights exclude the MHC region), so w_ld and the baseline LD scores share one SNP column.

M_5_50: for each annotation, the sum of its values over reference-panel SNPs with minor allele frequency above 5% (for a binary annotation, the number of common SNPs it contains). In the LDSC model E[chi2_j] = N_j * sum_c tau_c * l(j, c) + N_j * a + 1, with l(j, c) the LD score of SNP j with annotation c and tau_c the per-SNP heritability contributed by annotation c, M_5_50 converts per-SNP effects into the heritability attributed to an annotation (h2_c = tau_c * M_5_50_c).

Adapter Tasks (one per source format) write this contract; the vectorized S-LDSC Task reads it and knows nothing about the source format.

Classes:

Functions:

Attributes:

BP_COL module-attribute

BP_COL = 'BP'

CHR_COL module-attribute

CHR_COL = 'CHR'

LD_SCORES_FILENAME module-attribute

LD_SCORES_FILENAME = 'ld_scores.parquet'

MODELS_FILENAME module-attribute

MODELS_FILENAME = 'models.parquet'

M_5_50_FILENAME module-attribute

M_5_50_FILENAME = 'm_5_50.parquet'

SNP_COL module-attribute

SNP_COL = 'SNP'

W_LD_COL module-attribute

W_LD_COL = 'w_ld'

W_LD_FILENAME module-attribute

W_LD_FILENAME = 'w_ld.parquet'

LdScoreTable

LD scores for a fixed SNP universe.

snps: SNP, CHR, BP in row order (sorted by CHR then BP, SNP unique). column_ids: internal id of each LD column (matrix column order). sources: human-readable provenance of each column (e.g. file prefix and column name). matrix: (n_snps, n_columns) float32 LD scores. m_5_50: (n_columns,) genome-wide M_5_50 per column.

Methods:

Attributes:

column_ids instance-attribute

column_ids: tuple[str, ...]

m_5_50 instance-attribute

m_5_50: ndarray

matrix instance-attribute

matrix: ndarray

snps instance-attribute

snps: DataFrame

sources instance-attribute

sources: tuple[str, ...]

column_index

column_index(column_id: str) -> int
Source code in mecfs_bio/build_system/task/sldsc/prepared_sldsc_reference.py
def column_index(self, column_id: str) -> int:
    return self.column_ids.index(column_id)

take_rows

take_rows(rows: ndarray) -> LdScoreTable

The table restricted to the given row indices, in that order.

Source code in mecfs_bio/build_system/task/sldsc/prepared_sldsc_reference.py
def take_rows(self, rows: np.ndarray) -> "LdScoreTable":
    """The table restricted to the given row indices, in that order."""
    return LdScoreTable(
        snps=self.snps.select(pl.all().gather(rows)),
        column_ids=self.column_ids,
        sources=self.sources,
        matrix=self.matrix[rows],
        m_5_50=self.m_5_50,
    )

PreparedAnnotationSet

Attributes:

ld instance-attribute

ld: LdScoreTable

models instance-attribute

models: tuple[SLDSCAnnotationModel, ...]

PreparedBaseline

Baseline LD scores plus the regression-weight LD score.

w_ld: columns SNP and w_ld, row-aligned with ld.snps (same SNPs in the same order).

Attributes:

ld instance-attribute

ld: LdScoreTable

w_ld instance-attribute

w_ld: DataFrame

SLDSCAnnotationModel

One S-LDSC analysis: the reported target column plus the control columns it is fit with.

Attributes:

control_columns instance-attribute

control_columns: tuple[str, ...]

label instance-attribute

label: str

target_column instance-attribute

target_column: str

read_prepared_annotation_set

read_prepared_annotation_set(
    directory: Path,
) -> PreparedAnnotationSet
Source code in mecfs_bio/build_system/task/sldsc/prepared_sldsc_reference.py
def read_prepared_annotation_set(directory: Path) -> PreparedAnnotationSet:
    models = pl.read_parquet(directory / MODELS_FILENAME)
    return PreparedAnnotationSet(
        ld=_read_ld_score_table(directory),
        models=tuple(
            SLDSCAnnotationModel(
                label=row[_LABEL_COL],
                target_column=row[_TARGET_COL],
                control_columns=tuple(row[_CONTROLS_COL]),
            )
            for row in models.iter_rows(named=True)
        ),
    )

read_prepared_baseline

read_prepared_baseline(directory: Path) -> PreparedBaseline
Source code in mecfs_bio/build_system/task/sldsc/prepared_sldsc_reference.py
def read_prepared_baseline(directory: Path) -> PreparedBaseline:
    return PreparedBaseline(
        ld=_read_ld_score_table(directory),
        w_ld=pl.read_parquet(directory / W_LD_FILENAME),
    )

write_prepared_annotation_set

write_prepared_annotation_set(
    directory: Path, annotation_set: PreparedAnnotationSet
) -> None
Source code in mecfs_bio/build_system/task/sldsc/prepared_sldsc_reference.py
def write_prepared_annotation_set(
    directory: Path, annotation_set: PreparedAnnotationSet
) -> None:
    _write_ld_score_table(directory, annotation_set.ld)
    pl.DataFrame(
        {
            _LABEL_COL: [m.label for m in annotation_set.models],
            _TARGET_COL: [m.target_column for m in annotation_set.models],
            _CONTROLS_COL: [list(m.control_columns) for m in annotation_set.models],
        },
        schema={
            _LABEL_COL: pl.String,
            _TARGET_COL: pl.String,
            _CONTROLS_COL: pl.List(pl.String),
        },
    ).write_parquet(directory / MODELS_FILENAME)

write_prepared_baseline

write_prepared_baseline(
    directory: Path, baseline: PreparedBaseline
) -> None
Source code in mecfs_bio/build_system/task/sldsc/prepared_sldsc_reference.py
def write_prepared_baseline(directory: Path, baseline: PreparedBaseline) -> None:
    _write_ld_score_table(directory, baseline.ld)
    baseline.w_ld.write_parquet(directory / W_LD_FILENAME)