Ontology#
- class pronto.Ontology[source]#
Bases:
Mapping[str,Term|Relationship]An ontology storing terms and the relationships between them.
Ontologies can be loaded with
prontoif 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:OntologyXML 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
Ontologyinstances.
- classmethod from_obo_library(slug: str, import_depth: int = -1, timeout: int = 5, threads: int | None = None) Ontology[source]#
Create an
Ontologyfrom 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,.owlor.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
Noneto 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, encoding: str | None = None)[source]#
Create a new
Ontologyinstance.- 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
Noneis given, an emptyOntologyis 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
Noneto autodetect the number of CPUs on the host machine.encoding (str) – The encoding to use for decoding the file if needed. If
Nonegiven (the default), useschardetto detect the encoding when needed.
- Raises:
TypeError – When the given
handlecould not be used to parse and ontology.ValueError – When the given
handlecontains a serialized ontology not supported by any of the builtin parsers.
Added in version 2.7.0: The
encodingkeyword argument.
- __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
lenon 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.
- __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:
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
idalready 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
idalready 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
idcannot 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_aandhas_subclass) can be accessed with this method.- Raises:
KeyError – if the provided
idcannot be found in the relationships of the ontology graph.