bionty.base.Ontology

class bionty.base.Ontology(handle=None, import_depth=-1, timeout=100, threads=None, prefix='')

Bases: Ontology

Interface with ontologies via pronto.

Also see: https://pronto.readthedocs.io/en/stable/api/pronto.Ontology.html

Parameters:
  • handle (str | Path | BinaryIO | None, default: None) – Path to an ontology source file.

  • import_depth (int, default: -1) – The maximum depth of imports to resolve in the ontology tree.

  • timeout (int, default: 100) – The timeout in seconds to use when performing network I/O.

  • threads (int | None, default: None) – The number of threads to use when parsing.

  • url – The url of the ontology.

  • prefix (str, default: '') – Dev only -> prefix for get_term.

Class methods

classmethod from_obo_library(slug, import_depth=-1, timeout=5, threads=None)

Create an Ontology from a file in the OBO Library.

This is basically just a shortcut constructor to avoid typing the full OBO Library URL each time.

Parameters:
  • slug (str) – The filename of the ontology release to download from the OBO Library, including the file extension (should be one of .obo, .owl or .json).

  • import_depth (int) – The maximum depth of imports to resolve in the ontology tree. Note that the library may not behave correctly when not importing the complete dependency tree, so you should probably use the default value and import everything.

  • timeout (int) – The timeout in seconds to use when performing network I/O, for instance when connecting to the OBO library to download imports.

  • threads (int) – The number of threads to use when parsing, for parsers that support multithreading. Give None to autodetect the number of CPUs on the host machine.

Return type:

Ontology

Example

>>> apo = pronto.Ontology.from_obo_library("apo.obo")
>>> apo.metadata.ontology
'apo'
>>> apo.path
'http://purl.obolibrary.org/obo/apo.obo'

Methods

create_relationship(id)

Create a new relationship with the given identifier.

Raises:

ValueError – if the provided id already identifies an entity in the ontology graph.

Return type:

Relationship

create_term(id)

Create a new term with the given identifier.

Return type:

Term

Returns:

Term – the newly created term view, which attributes can the be modified directly.

Raises:

ValueError – if the provided id already identifies an entity in the ontology graph, or if it is not a valid OBO identifier.

dump(file, format='obo')

Serialize the ontology to a given file-handle.

Parameters:
  • file (BinaryIO) – A binary file handle open in reading mode to write the serialized ontology into.

  • format (str) – The serialization format to use. Currently supported formats are: obo, json.

Example

>>> ms = pronto.Ontology.from_obo_library("ms.obo")
>>> with open("ms.json", "wb") as f:
...     ms.dump(f, format="json")
dumps(format='obo')

Get a textual representation of the serialization ontology.

Return type:

str

Example

>>> go = pronto.Ontology("go.obo")
>>> print(go.dumps())
format-version: 1.2
data-version: releases/2019-07-01
...
get(k[, d]) D[k] if k in D, else d.  d defaults to None.
get_relationship(id)

Get a relationship in the ontology graph from the given identifier.

Builtin ontologies (is_a and has_subclass) can be accessed with this method.

Raises:

KeyError – if the provided id cannot be found in the relationships of the ontology graph.

Return type:

Relationship

get_synonym_type(id)

Get a synonym type in the ontology graph from the given identifier.

Raises:

KeyError – if the provided id does not resolve to a synonym type declared in this Ontology or one of its imports.

Return type:

SynonymType

get_term(term)

Search an ontology by its id.

items() a set-like object providing a view on D's items
keys() a set-like object providing a view on D's keys
relationships()

Query the relationships of an ontology.

Return type:

_OntologyRelationships

Example

>>> pato = pronto.Ontology.from_obo_library("pato.obo")
>>> len(pato.relationships())
24
>>> "reciprocal_of" in pato.relationships()
True
>>> for relationship in pato.relationships():
...     print(relationship)
Relationship('correlates_with', ...)
Relationship('decreased_in_magnitude_relative_to', ...)
...
synonym_types()

Iterate over the synonym types of the ontology graph.

Return type:

SizedIterator[SynonymType]

terms()

Query the terms of an ontology.

Return type:

_OntologyTerms

Example

>>> pato = pronto.Ontology.from_obo_library("pato.obo")
>>> len(pato.terms())
2661
>>> "PATO:0000186" in pato.terms()
True
>>> for term in sorted(pato.terms()):
...     print(term)
Term('PATO:0000000', name='obsolete pato')
Term('PATO:0000001', name='quality')
...
to_df(source=None, include_rel=None, include_id_prefixes=None)

Convert pronto.Ontology to a DataFrame with columns id, name, parents, definition, and synonyms.

Parameters:
  • source (str | None, default: None) – The source of the ontology terms to include. If None, all terms are included.

  • include_rel (str | None, default: None) – The relationship ID to include when gathering parent relationships. If provided, it extends the superclasses with the specified relationship.

  • include_id_prefixes (dict[str, list[str]] | None, default: None) – A dictionary mapping sources to lists of ID prefixes. Only terms with IDs starting with these prefixes will be included for the specified source.

Returns:

A DataFrame containing the filtered ontology terms with columns

  • ontology_id (index): The unique identifier for each term.

  • name: The name of the term.

  • definition: The definition of the term (None if not available).

  • synonyms: A pipe-separated string of exact synonyms (None if no synonyms).

  • parents: A list of parent term IDs, including superclasses and optionally the specified relationship.

values() an object providing a view on D's values