hpotk.ontology package

The hpotk.ontology package defines what an ontology is and provides methods for working with ontology data.

class hpotk.ontology.MinimalOntology[source]

Bases: Generic[ID, MINIMAL_TERM], GraphAware[ID], Versioned

MinimalOntology is a data structure for representing the ontology terms and the ontology hierarchy.

The typical way to load the ontology is by parsing Obographs JSON file using hpotk.util.store.OntologyStore, see Load ontology section for more info.

Here we will load a toy HPO shipped with the documentation:

>>> import os
>>> import hpotk
>>> fpath_hpo = os.path.join('docs', 'data', 'hp.toy.json')
>>> hpo = hpotk.load_minimal_ontology(fpath_hpo)

The ontology includes the following:

The ontology acts as a Python container of term IDs, we can check if a term is in the ontology as:

>>> seizure_curie = 'HP:0001250'
>>> seizure_curie in hpo
True

This works for term IDs too:

>>> seizure_id = hpotk.TermId.from_curie(seizure_curie)
>>> seizure_id in hpo
True

The ontology has length - the number of primary terms:

>>> len(hpo)
393

Note

The toy HPO has only 393 terms. Real-life HPO has much more terms.

The terms of MinimalOntology are instances of hpotk.model.MinimalTerm.

abstract property term_ids: Iterator[ID]

Get an iterator over term IDs of the primary AND obsolete ontology terms.

abstract property terms: Iterator[MINIMAL_TERM]

Get an iterator over current terms (not obsolete terms).

abstract get_term(term_id: str | TermId | Identified) MINIMAL_TERM | None[source]

Get the current term for a term_id.

>>> seizure = hpo.get_term('HP:0001250')
>>> seizure.name
'Seizure'
Parameters:

term_id – a CURIE str (e.g. ‘HP:1234567’), a hpotk.model.TermId or an hpotk.model.Identified entity that represents a current or an obsolete term.

Returns:

the current term or None if the ontology does not contain the term ID.

get_term_name(term_id: str | TermId | Identified) str | None[source]

Get the name of the term with a term_id.

>>> seizure_name = hpo.get_term_name('HP:0001250')
>>> seizure_name
'Seizure'
Parameters:

term_id – a CURIE str (e.g. ‘HP:1234567’), a hpotk.model.TermId or an hpotk.model.Identified entity that represents a current or an obsolete term.

Returns:

name of the term if the term is in ontology or None otherwise.

class hpotk.ontology.Ontology[source]

Bases: MinimalOntology[ID, TERM]

An ontology with all information available for terms.

The terms Ontology are instances of hpotk.model.Term.

hpotk.ontology.create_minimal_ontology(graph: OntologyGraph[ID], terms: Sequence[MINIMAL_TERM], version: str | None = None) MinimalOntology[ID, MINIMAL_TERM][source]

Create minimal ontology from the components.

Parameters:
  • graph – the ontology graph.

  • terms – ALL ontology terms (both obsolete and primary).

  • version – ontology version or None if unknown.

Returns:

the ontology

hpotk.ontology.create_ontology(graph: OntologyGraph[ID], terms: Sequence[TERM], version: str | None = None) Ontology[ID, TERM][source]

Create ontology from the components.

Parameters:
  • graph – the ontology graph.

  • terms – ALL ontology terms (both obsolete and primary).

  • version – ontology version or None if unknown.

Returns:

the ontology.

Subpackages