fix(science): stop scoring evidence that was never looked up
A review of the ranking's arithmetic found four things wrong, all of which made the score look better informed than it was. Measurements below are from this repo, not estimates. **Components now abstain instead of inventing a number.** A run without a VEP cache returns no allele frequencies, and rarity_score(None) read that as "absent from gnomAD, therefore maximally rare" and awarded every variant a free 0.25. jobs.has_frequencies / has_effect_scores record what the run actually produced, absent components are dropped from the weighted mean, and the remaining weights are renormalised so the score keeps its meaning. The UI shows "not looked up" rather than a bar, and the funnel stops calling a step "rare" when nothing was filtered. **Allele frequency is no longer a model feature.** It dominated: the same missense variant scored 0.887 at AF 0 and 0.0003 at AF 0.01. That double- counted, because the ranking already scores frequency explicitly, putting ~45% of every rank on one measurement; and it was circular, because ACMG assigns ClinVar's benign labels using frequency (BA1/BS1). Retraining without it moves missense AUROC from 0.872 to 0.500 — exactly random. The old figure was allele frequency, not variant-effect knowledge. The model therefore abstains unless CADD or AlphaMissense is present, since otherwise it only restates the consequence class. **Phenotype matching is weighted by information content** and HPO annotations are propagated up the ontology. Counting terms alike let "global developmental delay" (IC 0.93) count as much as "dilated left subclavian artery" (IC 7.88). **A real bug in the propagation, found by checking it.** The ancestor walk read a pre-order DFS backwards, which on a DAG lets a term resolve before one of its parents and inherit that parent alone instead of its lineage. It dropped 399 terms out of the phenotype branch, Camptodactyly and Chiari malformation among them. Now a true post-order, tested against a reference transitive closure. The ontology arithmetic moved to rarelens_ml.hpo so it is covered by tests, and rarelens_ml.benchmark measures the whole thing: across 10,178 published cases the causal gene ranks first 45.9-81.0% of the time against 5,269 genes, versus 0.02% for chance. docs/data.md reports that with its contamination (HPO's annotations come from these same case reports), and includes the measurement showing information-content weighting earns its place while propagation does not - kept anyway, for a reason the docs argue rather than assume.
This commit is contained in:
+41
-5
@@ -1,8 +1,19 @@
|
||||
import uuid
|
||||
from typing import Any
|
||||
|
||||
from sqlalchemy.dialects.postgresql import insert
|
||||
|
||||
from app.db import SessionLocal
|
||||
from app.models import Case, CasePhenotype, GenePhenotype, Job, JobStatus, Prediction, Variant
|
||||
from app.models import (
|
||||
Case,
|
||||
CasePhenotype,
|
||||
GenePhenotype,
|
||||
HpoTerm,
|
||||
Job,
|
||||
JobStatus,
|
||||
Prediction,
|
||||
Variant,
|
||||
)
|
||||
|
||||
VARIANT_DEFAULTS: dict[str, Any] = {
|
||||
"chrom": "22", "pos": 1, "ref": "A", "alt": "G", "gene": "NF2",
|
||||
@@ -17,10 +28,16 @@ async def seed_case(
|
||||
gene_terms: dict[str, list[tuple[str, str]]] | None = None,
|
||||
status: JobStatus = JobStatus.succeeded,
|
||||
name: str | None = None,
|
||||
has_frequencies: bool = True,
|
||||
has_effect_scores: bool = True,
|
||||
) -> tuple[uuid.UUID, uuid.UUID]:
|
||||
"""Insert a case, its phenotypes, a job and its variants. Returns (case_id, job_id).
|
||||
|
||||
`variants` entries override VARIANT_DEFAULTS; a "score" key becomes a Prediction.
|
||||
|
||||
The job defaults to a run that looked everything up, so a test says so explicitly when it
|
||||
wants the opposite. Every term gets information content 1.0, which makes the phenotype score
|
||||
plain term counting unless a test seeds its own weights.
|
||||
"""
|
||||
async with SessionLocal() as s:
|
||||
case = Case(
|
||||
@@ -29,11 +46,30 @@ async def seed_case(
|
||||
assembly="GRCh38",
|
||||
phenotypes=[CasePhenotype(hpo_id=hpo, label=label) for hpo, label in (phenotypes or [])],
|
||||
)
|
||||
job = Job(case=case, status=status, vep_version="113.0")
|
||||
job = Job(
|
||||
case=case,
|
||||
status=status,
|
||||
vep_version="113.0",
|
||||
has_frequencies=has_frequencies,
|
||||
has_effect_scores=has_effect_scores,
|
||||
)
|
||||
s.add_all([case, job])
|
||||
for gene, terms in (gene_terms or {}).items():
|
||||
s.add_all(
|
||||
GenePhenotype(gene_symbol=gene, hpo_id=hpo, hpo_name=label) for hpo, label in terms
|
||||
# HPO rows are shared reference data, so two seeds in one test may name the same term.
|
||||
terms_seen: dict[str, str] = dict(phenotypes or [])
|
||||
annotations = [
|
||||
{"gene_symbol": gene, "hpo_id": hpo, "hpo_name": label}
|
||||
for gene, terms in (gene_terms or {}).items()
|
||||
for hpo, label in terms
|
||||
]
|
||||
for terms in (gene_terms or {}).values():
|
||||
terms_seen.update(terms)
|
||||
if annotations:
|
||||
await s.execute(insert(GenePhenotype).values(annotations).on_conflict_do_nothing())
|
||||
if terms_seen:
|
||||
await s.execute(
|
||||
insert(HpoTerm)
|
||||
.values([{"hpo_id": h, "name": lab, "ic": 1.0} for h, lab in terms_seen.items()])
|
||||
.on_conflict_do_nothing()
|
||||
)
|
||||
for spec in variants or []:
|
||||
fields = VARIANT_DEFAULTS | spec
|
||||
|
||||
Reference in New Issue
Block a user