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:OBO graphs in JSON format.
-
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
-
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
-
imports
¶ A dictionary mapping references found in the import section of the metadata to resolved
Ontology
instances.
-
classmethod
from_obo_library
(slug: str, import_depth: int = -1, timeout: int = 5, threads: Optional[int] = None) → 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.
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: Union[BinaryIO, str, os.PathLike, None] = None, import_depth: int = -1, timeout: int = 5, threads: Optional[int] = 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 emptyOntology
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__
() → pronto.utils.iter.SizedIterator[str][str][source]¶ Yield the identifiers of all the entities part of the ontology.
-
__getitem__
(id: str) → Union[pronto.term.Term, pronto.relationship.Relationship][source]¶ Get any entity in the ontology graph with the given identifier.
-
__eq__
(other)¶ Return self==value.
-
__ge__
()¶ Return self>=value.
-
__gt__
()¶ Return self>value.
-
__le__
()¶ Return self<=value.
-
__lt__
()¶ Return self<value.
-
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
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
() → pronto.utils.iter.SizedIterator[pronto.synonym.SynonymType][pronto.synonym.SynonymType][source]¶ Iterate over the synonym types of the ontology graph.
-
terms
() → pronto.ontology._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
() → pronto.ontology._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) → 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
andhas_subclass
) can be accessed with this method.- Raises
KeyError – if the provided
id
cannot be found in the relationships of the ontology graph.