phenocor.ontology package

phenocor.ontology.hierarchy module

class HPOHierarchyEngine(hpo_file: str | IO[Any] | None = None, release: str | None = None)

Bases: object

Apply HPO hierarchy-aware operations to phenotype status matrices.

The engine provides two main operations:

  • propagation of observed and excluded phenotype annotations through the HPO hierarchy;

  • construction of a pairwise mask for excluding ontologically related HPO terms from downstream correlation analysis.

Phenotype status matrices are expected to contain:

1 = observed 0 = excluded NaN = unknown

Before propagation, HPO term IDs are resolved to canonical primary IDs. Invalid HPO terms are removed, and columns resolving to the same canonical HPO term are merged.

Notes

Propagation is restricted to HPO terms represented in the input matrix. Observed annotations propagate to represented ancestors, while excluded annotations propagate to represented descendants.

Explicit annotations are preserved when they conflict with propagated annotations.

build_relationship_mask(hpo_ids: Sequence[str]) → DataFrame

Build a pairwise mask for ontologically related HPO terms.

Self-pairs and ancestor-descendant pairs are marked as NaN. Unrelated HPO term pairs are marked as 0.

The mask is intended to exclude structurally related HPO term pairs from downstream correlation analysis.

This method is intended for internal use after propagate() has prepared the ontology caches for the supplied terms.

Parameters:

hpo_ids (Sequence[str]) – Canonical HPO term IDs, typically corresponding to columns of a propagated phenotype matrix. Input order is preserved.

Returns:

Square matrix indexed and columned by hpo_ids:

  • NaN indicates a self-pair or ancestor-descendant pair;

  • 0 indicates a pair eligible for downstream evaluation with respect to ontology structure.

Return type:

pd.DataFrame

get_labels() → dict[str, str]

Return labels for HPO terms prepared by this engine.

property hpo: MinimalOntology

Return the underlying HPO ontology.

propagate(matrix: DataFrame) → DataFrame

Propagate phenotype annotations through the ontology hierarchy.

Observed annotations (1) propagate upward to represented ancestor terms. Excluded annotations (0) propagate downward to represented descendant terms.

Before propagation, HPO term IDs are resolved to canonical primary IDs. Invalid terms are removed. If multiple input columns resolve to the same canonical HPO term, they are merged. When conflicting values occur during this merge, observed status (1) takes precedence and a warning is emitted.

Propagation only fills previously unknown values (NaN). Therefore, explicit observed or excluded annotations are never overwritten by propagated values. Conflicts between explicit and propagated annotations are reported as warnings.

When both observed and excluded states are inferred for an unknown value, observed status takes precedence and a warning is emitted.

Parameters:

matrix (pd.DataFrame) – Phenotype status matrix with individuals as rows and HPO term IDs as columns. Values must be 1 (observed), 0 (excluded), or NaN (unknown).

Returns:

Phenotype status matrix with canonical HPO term IDs and hierarchy-propagated annotations.

Return type:

pd.DataFrame