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) → pronto.ontology.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.

Example

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

Create a new Ontology instance.

Parameters
  • handle (str, BinaryIO, 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.

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
>>> len(ms.relationships())
28
__iter__() → pronto.utils.iter.SizedIterator[str][str][source]

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

__repr__()[source]

Return a textual representation of self that should roundtrip.

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, obojson.

Example

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

Return self==value.

__ge__()

Return self>=value.

__gt__()

Return self>value.

__le__()

Return self<=value.

__lt__()

Return self<value.

dumps(format: str = 'obo') → str[source]

Get a textual representation of the serialization ontology.

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
terms() → pronto.utils.iter.SizedIterator[pronto.term.Term][pronto.term.Term][source]

Iterate over the terms of the ontology graph.

relationships() → pronto.utils.iter.SizedIterator[pronto.relationship.Relationship][pronto.relationship.Relationship][source]

Iterate over the relationships of the ontology graph.

Builtin relationships (is_a) are not part of the yielded entities, yet they can still be accessed with the Ontology.get_relationship method.

create_term(id: str) → pronto.term.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) → pronto.relationship.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) → pronto.term.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) → pronto.relationship.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.