pronto Documentation - Read the Docs pronto Documentation, Release dev Pronto is a Python agnostic...

Click here to load reader

  • date post

  • Category


  • view

  • download


Embed Size (px)

Transcript of pronto Documentation - Read the Docs pronto Documentation, Release dev Pronto is a Python agnostic...

  • pronto DocumentationRelease dev

    Martin Larralde

    Nov 22, 2018

  • Contents

    1 Installation: 3

    2 Contents: 5

    3 Indices and tables 19


  • ii

  • pronto Documentation, Release dev

    Pronto is a Python agnostic library designed to work with ontologies. At the moment, it can parse obo and owlXMLformats, open ontologies on the local host or from an network location, and export ontologies to obo format.

    Contents 1

  • pronto Documentation, Release dev

    2 Contents



    Run pip install pronto in a shell to download the latest release and all its dependencies from PyPi, or have alook at the Installation page to find other ways to install pronto.


  • pronto Documentation, Release dev

    4 Chapter 1. Installation:



    2.1 Installation

    2.1.1 PyPi

    Pronto is hosted on GitHub, but the easiest way to install it is to download the latest release from its PyPi repository.It will install all dependencies of Pronto and then install the pronto module:

    pip install pronto

    2.1.2 Conda

    Pronto is also available as a recipe in the bioconda channel. To install, simply use the conda installer:

    conda install -c bioconda pronto

    2.1.3 GitHub + pip

    If, for any reason, you prefer to download the library from GitHub, you can clone the repository and install therepository by running (with the admin rights):

    pip install

    Keep in mind this will install the development version of the library, so not everything may work as expected comparedto a versioned release.

    2.1.4 GitHub + setuptools

    If you do not have pip installed (the Makefile uses pip to install dependencies and then install pronto), you can do thefollowing (after having properly installed all the dependencies):


  • pronto Documentation, Release dev

    git clone prontopython install

    2.2 Examples

    2.2.1 Explore an ontology via relationships

    Pronto can be used to explore an ontology and find children of a specific term. This example was taken from mzml2isa,a program that parses .mzML files (a format of Mass Spectrometry data).

    self.obo = Ontology('')# ... extract metadata and get elements ... #for e in elements:

    for ie in e.iterfind('s:cvParam', self.ns):if ie.attrib['accession'] in self.obo['MS:1000031'].rchildren().id:

    ### ... get instrument info ... ###parents = self.obo[ie.attrib['accession']].rparents()manufacturer = next(parent for parent in parents \

    if parent in self.obo['MS:1000031'].children)### ... get manufacturer info ... ###

    2.2.2 Merge ontologies and export the merge to the Obo Format

    It is really easy to merge two ontologies: for instance, if we want to merge the Amphibian gross anatomy ontologywith the Xenopus anatomy and development ontology:

    First, we import the ontologies:

    >>> from pronto import Ontology>>> aao = Ontology('')>>> xao = Ontology('')>>> print(len(aao), len(xao))1603 1521

    Then, either we merge the ontologies in a new ontology:

    >>> merged = Ontology()>>> merged.merge(aao)>>> merged.merge(xao)>>> print(len(merged))3124

    Or we can also merge the XAO in the AAO to keep the AAO metadata intact:

    >>> aao.merge(xao)>>> print(len(aao))3124

    Then we do the following to export the merged ontology:

    >> with open('merged.obo', 'w') as f:f.write(aao.obo) #or f.write(merged.obo)

    6 Chapter 2. Contents:

  • pronto Documentation, Release dev

    2.3 API Reference

    The following classes are imported when running from pronto import *:

    pronto.Ontology An ontology.pronto.Description A description with optional cross-references.pronto.Relationship A Relationship object.pronto.Synonym A synonym in an ontology.pronto.SynonymType A synonym type in an ontology.pronto.Term A term in an ontology.pronto.TermList A list of Term instances.

    2.3.1 Ontology

    class pronto.OntologyAn ontology.

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

    metadict the metatada contained in the Ontology .

    termsdict the terms of the ontology. Not very useful to access directly, since Ontology provides many usefulshortcuts and features to access them.

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

    pathstr, optional the path to the ontology, if any.


    Import an ontology from a remote location:

    >>> from pronto import Ontology>>> envo = Ontology("")

    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:

    2.3. API Reference 7

  • pronto Documentation, Release dev

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

    __contains__(item)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 clTrue>>> from pronto import Term>>> Term('TST:001', 'tst') in clFalse

    __getitem__(item)Get a term in the Ontology .

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


    >>> cl['CL:0002380']

    >>> cl['CL:0002380'].relations{Relationship('is_a'): []}

    __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 localfilesystem, or a FTP or HTTP URL), a readable file handle containing an ontology, orNone to create a new ontology from scratch.

    imports (bool, optional) if True (the default), embed the ontology importsinto the returned instance.

    import_depth (int, optional) The depth up to which the imports should beresolved. 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 toautodetect.

    __iter__()Return an iterator over the Terms of the Ontology.

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

    8 Chapter 2. Contents:

  • pronto Documentation, Release dev


    >>> for k in uo:... if 'basepair' in print(k)

    __len__()Return the number of terms in the Ontology.

    __repr__()Return repr(self).

    _empty_cache(termlist=None)Empty the cache associated with each Term instance.

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

    _get_parsers(name)Return the appropriate parser asked by the user.

    Todo: Change Ontology._get_parsers behaviour to look for parsers through a setuptools entry-point instead of mere subclasses.

    _include_term(term)Add a single term to the current ontology.

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

    _include_term_list(termlist)Add terms from a TermList to the ontology