Files
rarelens/api/alembic/versions/b7d4e2f80c31_evidence_availability_and_term_ic.py
T
Kemal Yaylali e76ae847a1 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.
2026-09-12 11:32:46 +01:00

49 lines
1.7 KiB
Python

"""Record what an annotation run produced, and how specific each HPO term is
Two changes, both so the ranking can stop asserting things it has not measured:
- jobs.has_frequencies / has_effect_scores: whether the run looked up allele frequencies and
CADD/AlphaMissense at all. Without this the API cannot tell "absent from gnomAD" (strong
evidence) from "nobody checked gnomAD" (no evidence), and scored both as maximally rare.
- hpo_terms: each term's information content, so a phenotype match is weighted by how specific
the matching term is rather than counting every term alike.
Existing rows default to false, which is correct: every job recorded before this ran used VEP's
database mode, which returns neither frequencies nor plugin scores.
Revision ID: b7d4e2f80c31
Revises: 9a1c2d3e4f50
"""
from collections.abc import Sequence
import sqlalchemy as sa
from alembic import op
revision: str = "b7d4e2f80c31"
down_revision: str | None = "9a1c2d3e4f50"
branch_labels: str | Sequence[str] | None = None
depends_on: str | Sequence[str] | None = None
def upgrade() -> None:
op.add_column(
"jobs",
sa.Column("has_frequencies", sa.Boolean(), nullable=False, server_default=sa.false()),
)
op.add_column(
"jobs",
sa.Column("has_effect_scores", sa.Boolean(), nullable=False, server_default=sa.false()),
)
op.create_table(
"hpo_terms",
sa.Column("hpo_id", sa.String(length=20), primary_key=True),
sa.Column("name", sa.String(length=200), nullable=False),
sa.Column("ic", sa.Float(), nullable=False),
)
def downgrade() -> None:
op.drop_table("hpo_terms")
op.drop_column("jobs", "has_effect_scores")
op.drop_column("jobs", "has_frequencies")