Per-trait LDSC heritability and diagnostics for the traits of a fast CT-LDSC study.
Each trait's heritability is the same own-kept-set fit the all-pairs task uses
(estimate_trait_context), so the two tables agree. Columns: the alias p; observed-scale h2_obs and
its delete-block jackknife SE; for binary traits the liability-scale h2_liab and SE (the
observed-to-liability conversion is linear in h2_obs, so both are scaled by
liability_h2(1.0, info)), null for quantitative traits; the free LDSC Intercept and its SE;
lambda_gc (median kept chi-square / 0.4549); mean_chi2; n_snps (kept SNPs); n_bar (mean kept N);
and the alignment diagnostics coverage, allele_mismatch_fraction and position_concordance.
Classes:
Functions:
FastCtLdscHeritabilityTask
Bases: Task
One row of heritability and alignment diagnostics per aligned trait.
Methods:
Attributes:
aligned_tasks
instance-attribute
aligned_tasks: tuple[FastCtLdscAlignedTraitTask, ...]
index_task
instance-attribute
index_task: FastCtLdscVariantIndexTask
n_blocks
class-attribute
instance-attribute
n_blocks: int = DEFAULT_N_BLOCKS
create
classmethod
create(
asset_id: str,
*,
index_task: FastCtLdscVariantIndexTask,
aligned_tasks: Sequence[FastCtLdscAlignedTraitTask],
n_blocks: int = DEFAULT_N_BLOCKS,
) -> Self
Source code in mecfs_bio/build_system/task/fast_ct_ldsc/heritability_task.py
| @classmethod
def create(
cls,
asset_id: str,
*,
index_task: FastCtLdscVariantIndexTask,
aligned_tasks: Sequence[FastCtLdscAlignedTraitTask],
n_blocks: int = DEFAULT_N_BLOCKS,
) -> Self:
return cls(
meta=multi_trait_table_meta(asset_id),
index_task=index_task,
aligned_tasks=tuple(aligned_tasks),
n_blocks=n_blocks,
)
|
execute
execute(scratch_dir: Path, fetch: Fetch, wf: WF) -> Asset
Source code in mecfs_bio/build_system/task/fast_ct_ldsc/heritability_task.py
| def execute(self, scratch_dir: Path, fetch: Fetch, wf: WF) -> Asset:
matrix = load_trait_matrix(self.index_task, self.aligned_tasks, fetch)
contexts = estimate_trait_contexts(matrix, n_blocks=self.n_blocks)
out_path = scratch_dir / "fast_ct_ldsc_heritability.parquet"
heritability_table(matrix, contexts).write_parquet(out_path)
return FileAsset(out_path)
|
heritability_table
heritability_table(
matrix: TraitMatrix,
contexts: Sequence[TraitLdscContext],
) -> pl.DataFrame
Source code in mecfs_bio/build_system/task/fast_ct_ldsc/heritability_task.py
| def heritability_table(
matrix: TraitMatrix, contexts: Sequence[TraitLdscContext]
) -> pl.DataFrame:
rows = []
for alias, ctx, info, diag in zip(
matrix.aliases, contexts, matrix.sample_infos, matrix.diagnostics
):
factor = liability_h2(1.0, info)
rows.append(
{
FAST_CT_LDSC_TRAIT_COL: alias,
FAST_CT_LDSC_H2_OBS_COL: ctx.h2,
FAST_CT_LDSC_H2_OBS_SE_COL: ctx.h2_se,
FAST_CT_LDSC_H2_LIAB_COL: None if factor is None else factor * ctx.h2,
FAST_CT_LDSC_H2_LIAB_SE_COL: None
if factor is None
else factor * ctx.h2_se,
FAST_CT_LDSC_INTERCEPT_COL: ctx.intercept,
FAST_CT_LDSC_INTERCEPT_SE_COL: ctx.intercept_se,
FAST_CT_LDSC_LAMBDA_GC_COL: ctx.lambda_gc,
FAST_CT_LDSC_MEAN_CHI2_COL: ctx.mean_chi2,
FAST_CT_LDSC_N_SNPS_COL: ctx.n_snps,
FAST_CT_LDSC_N_BAR_COL: ctx.n_bar,
FAST_CT_LDSC_COVERAGE_COL: diag.coverage,
FAST_CT_LDSC_ALLELE_MISMATCH_FRACTION_COL: diag.allele_mismatch_fraction,
FAST_CT_LDSC_POSITION_CONCORDANCE_COL: diag.position_concordance,
}
)
schema_overrides = {
FAST_CT_LDSC_H2_LIAB_COL: pl.Float64,
FAST_CT_LDSC_H2_LIAB_SE_COL: pl.Float64,
FAST_CT_LDSC_POSITION_CONCORDANCE_COL: pl.Float64,
}
return pl.DataFrame(rows, schema_overrides=schema_overrides)
|