class pronto.Ontology

An ontology.

Ontologies inheriting from this class will be able to use the same API as providing they generated the expected structure in the _parse() method.


dict – the metatada contained in the Ontology.


dict – the terms of the ontology. Not very useful to access directly, since Ontology provides many useful shortcuts and features to access them.


list – a list of paths and/or URLs to additional ontologies the ontology depends on.


str, optional – the path to the ontology, if any.


Import an ontology from a remote location:

>>> from pronto import Ontology
>>> envo = Ontology("http://purl.obolibrary.org/obo/bfo.owl")

Merge two local ontologies and export the merge:

>>> uo = Ontology("tests/resources/uo.obo", False)
>>> cl = Ontology("tests/resources/cl.ont.gz", False)
>>> uo.merge(cl)
>>> with open('tests/run/merge.obo', 'w') as f:
...     f.write(uo.obo) 

Export an ontology with its dependencies embedded:

>>> cl = Ontology("tests/resources/cl.ont.gz")
>>> with open('tests/run/cl.obo', 'w') as f:
...     f.write(cl.obo) 

Use the parser argument to force usage a parser:

>>> cl = Ontology("tests/resources/cl.ont.gz",
...               parser='OwlXMLParser')

Check if the ontology contains a term.

It is possible to check if an Ontology contains a Term using an id or a Term instance.

Raises:TypeError – if argument (or left operand) is neither a string nor a Term


>>> 'CL:0002404' in cl
>>> from pronto import Term
>>> Term('TST:001', 'tst') in cl

Get a term in the Ontology.

Method was overloaded to allow accessing to any Term of the Ontology using the Python dictionary syntax.


>>> cl['CL:0002380']
<CL:0002380: oospore>
>>> cl['CL:0002380'].relations
{Relationship('is_a'): [<CL:0000605: fungal asexual spore>]}
__init__(handle=None, imports=True, import_depth=-1, timeout=2, parser=None)

Create an Ontology instance from a file handle or a path.

  • handle (io.IOBase or str) – the location of the file (either a path on the local filesystem, or a FTP or HTTP URL), a readable file handle containing an ontology, or None to create a new ontology from scratch.
  • imports (bool, optional) – if True (the default), embed the ontology imports into the returned instance.
  • import_depth (int, optional) – The depth up to which the imports should be resolved. Setting this to 0 is equivalent to setting imports to False. Leave as default (-1) to handle all the imports.
  • timeout (int, optional) – The timeout in seconds for network operations.
  • parser (BaseParser, optional) – A parser instance to use. Leave to None to autodetect.

Return an iterator over the Terms of the Ontology.

For convenience of implementation, the returned instance is actually a generator that yields each term of the ontology, sorted in the definition order in the ontology file.


>>> for k in uo:
...    if 'basepair' in k.name:
...       print(k)
<UO:0000328: kilobasepair>
<UO:0000329: megabasepair>
<UO:0000330: gigabasepair>

Return the number of terms in the Ontology.


Return repr(self).


Empty the cache associated with each Term instance.

This method is called when merging Ontologies or including new terms in the Ontology to make sure the cache of each term is cleaned and avoid returning wrong memoized values (such as Term.rchildren() TermLists, which get memoized for performance concerns)


Return the appropriate parser asked by the user.


Change Ontology._get_parsers behaviour to look for parsers through a setuptools entrypoint instead of mere subclasses.


Add a single term to the current ontology.

It is needed to dereference any term in the term’s relationship and then to build the reference again to make sure the other terms referenced in the term’s relations are the one contained in the ontology (to make sure changes to one term in the ontology will be applied to every other term related to that term).


Add terms from a TermList to the ontology.


Generate the obo metadata header and updates metadata.

When called, this method will create appropriate values for the auto-generated-by and date fields.


Generated following specs of the unofficial format guide: ftp://ftp.geneontology.org/pub/go/www/GO.format.obo-1_4.shtml


Make terms aware of their children.

This is done automatically when using the merge and include methods as well as the __init__ method, but it should be called in case of manual editing of the parents or children of a Term.


Add new terms to the current ontology.

Raises:TypeError – when the arguments is (are) neither a TermList nor a Term.


This will also recursively include terms in the term’s relations dictionnary, but it is considered bad practice to do so. If you want to create your own ontology, you should only add an ID (such as ‘ONT:001’) to your terms relations, and let the Ontology link terms with each other.


Create a new ontology from scratch

>>> from pronto import Term, Relationship
>>> t1 = Term('ONT:001','my 1st term',
...           'this is my first term')
>>> t2 = Term('ONT:002', 'my 2nd term',
...           'this is my second term',
...           {Relationship('part_of'): ['ONT:001']})
>>> ont = Ontology()
>>> ont.include(t1, t2)
>>> 'ONT:002' in ont
>>> ont['ONT:001'].children
[<ONT:002: my 2nd term>]

Merge another ontology into the current one.

Raises:TypeError – When argument is not an Ontology object.


>>> from pronto import Ontology
>>> nmr = Ontology('tests/resources/nmrCV.owl', False)
>>> po = Ontology('tests/resources/po.obo.gz', False)
>>> 'NMR:1000271' in nmr
>>> 'NMR:1000271' in po
>>> po.merge(nmr)
>>> 'NMR:1000271' in po
parse(stream, parser=None)

Parse the given file using available BaseParser instances.

  • TypeError – when the parser argument is not a string or None.
  • ValueError – when the parser argument is a string that does not name a BaseParser.

Make relations point to ontology terms instead of term ids.

This is done automatically when using the merge and include methods as well as the __init__ method, but it should be called in case of manual changes of the relationships of a Term.

resolve_imports(imports, import_depth, parser=None)

Import required ontologies.


str – the ontology serialized in json format.


str – the ontology serialized in obo format.