Ontology

class pronto.Ontology[source]

An ontology storing terms and the relationships between them.

Ontologies can be loaded with pronto if they are serialized in any of the following ontology languages and formats at the moment:

metadata

A data structure storing the metadata about the current ontology, either extracted from the owl:Ontology XML element or from the header of the OBO file.

Type:

Metadata

timeout

The timeout in seconds to use when performing network I/O, for instance when connecting to the OBO library to download imports. This is kept for reference, as it is not used after the initialization of the ontology.

Type:

int

imports

A dictionary mapping references found in the import section of the metadata to resolved Ontology instances.

Type:

Dict[str, Ontology]

classmethod from_obo_library(slug: str, import_depth: int = -1, timeout: int = 5, threads: int | None = None) Ontology[source]

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.

Example

>>> apo = pronto.Ontology.from_obo_library("apo.obo")
>>> apo.metadata.ontology
'apo'
>>> apo.path
'http://purl.obolibrary.org/obo/apo.obo'
__init__(handle: BinaryIO | str | PathLike | None = None, import_depth: int = -1, timeout: int = 5, threads: int | None = None)[source]

Create a new Ontology instance.

Parameters:
  • handle (str, BinaryIO, PathLike, or None) – Either the path to a file or a binary file handle that contains a serialized version of the ontology. If None is given, an empty Ontology is returned and can be populated manually.

  • 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.

Raises:
  • TypeError – When the given handle could not be used to parse and ontology.

  • ValueError – When the given handle contains a serialized ontology not supported by any of the builtin parsers.

__len__() int[source]

Return the number of entities in the ontology.

This method takes into accounts the terms and the relationships defined in the current ontology as well as all of its imports. To only count terms or relationships, use len on the iterator returned by the dedicated methods (e.g. len(ontology.terms())).

Example

>>> ms = pronto.Ontology.from_obo_library("ms.obo")
>>> len(ms)
6023
>>> len(ms.terms())
5995
__iter__() SizedIterator[str][source]

Yield the identifiers of all the entities part of the ontology.

__getitem__(id: str) Term | Relationship[source]

Get any entity in the ontology graph with the given identifier.

__repr__()[source]

Return a textual representation of self that should roundtrip.

__eq__(other)

Return self==value.

__hash__ = None
get(k[, d]) D[k] if k in D, else d.  d defaults to None.
items() a set-like object providing a view on D's items
keys() a set-like object providing a view on D's keys
values() an object providing a view on D's values
dump(file: BinaryIO, format: str = 'obo')[source]

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: str = 'obo') str[source]

Get a textual representation of the serialization ontology.

Example

>>> go = pronto.Ontology("go.obo")
>>> print(go.dumps())
format-version: 1.2
data-version: releases/2019-07-01
...
synonym_types() SizedIterator[SynonymType][source]

Iterate over the synonym types of the ontology graph.

terms() _OntologyTerms[source]

Query the terms of an ontology.

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')
...
relationships() _OntologyRelationships[source]

Query the relationships of an ontology.

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', ...)
...
create_term(id: str) Term[source]

Create a new term with the given identifier.

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.

create_relationship(id: str) Relationship[source]

Create a new relationship with the given identifier.

Raises:

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

get_term(id: str) Term[source]

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

Raises:

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

get_relationship(id: str) Relationship[source]

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.

get_synonym_type(id: str) SynonymType[source]

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.