OBO Ontology (ontology)

This module provides an interface for parsing, creating and manipulating of OBO ontologies.

Construct an ontology from scratch with custom terms

>>> term = OBOObject("Term", id="foo:bar", name="Foo bar")
>>> print(term)
[Term]
id: foo:bar
name: Foo bar

>>> ontology = OBOOntology()
>>> ontology.add_object(term)
>>> ontology.add_header_tag("created-by", "ales") # add a header tag
>>> from six import StringIO
>>> buffer = StringIO()
>>> ontology.write(buffer) # Save the ontology to a file like object
>>> print(buffer.getvalue()) # Print the contents of the buffer
created-by: ales

[Term]
id: foo:bar
name: Foo bar

To load an ontology from a file, pass the file or filename to the OBOOntology constructor or call its load method

>>> _ = buffer.seek(0) # rewind
>>> ontology = OBOOntology(buffer)
>>> # Or equivalently
>>> _ = buffer.seek(0) # rewind
>>> ontology = OBOOntology()
>>> ontology.load(buffer)

See the definition of the .obo file format.

class orangecontrib.bio.ontology.OBOOntology(file=None)

Bases: object

An class representing an OBO ontology.

Parameters:file (file-like) – A optional file like object describing the ontology in obo format.
add_object(obj)

Add an OBOObject instance to this ontology.

add_header_tag(tag, value)

Add header tag, value pair to this ontology.

load(file, progress_callback=None)

Load terms from a file.

Parameters:
  • file (file-like) – A file-like like object describing the ontology in obo format.
  • progress_callback (function) – An optional function callback to report on the progress.
write(stream)

Write the contents of the ontology to a file in .obo format.

Parameters:file (file-like) – A file like object.
update(other)

Update this ontology with the terms from other.

term(id)

Return the OBOObject associated with this id.

Parameters:id (str) – Term id string.
terms()

Return all Term instances in the ontology.

term_by_name(name)

Return the term with name name.

typedefs()

Return all Typedef instances in the ontology.

instances()

Return all Instance instances in the ontology.

root_terms()

Return all root terms (terms without any parents).

related_terms(term)

Return a list of (rel_type, term_id) tuples where rel_type is relationship type (e.g. ‘is_a’, ‘has_part’, ...) and term_id is the id of the term in the relationship.

edge_types()

Return a list of all edge types in the ontology.

parent_edges(term)

Return a list of (rel_type, parent_term) tuples.

child_edges(term)

Return a list of (rel_type, source_term) tuples.

super_terms(term)

Return a set of all super terms of term up to the most general one.

sub_terms(term)

Return a set of all sub terms for term.

child_terms(term)

Return a set of all child terms for this term.

parent_terms(term)

Return a set of all parent terms for this term.

relations()

Return a list of all relations in the ontology.

to_network(terms=None)

Return an Orange.network.Network instance constructed from this ontology.

to_networkx(terms=None)

Return a NetworkX graph of this ontology.

to_graphviz(terms=None)

Return an pygraphviz.AGraph representation of the ontology. If terms is not None it must be a list of terms in the ontology. The graph will in this case contain only the super graph of those terms.

class orangecontrib.bio.ontology.OBOObject(stanza_type='Term', **kwargs)

Bases: object

A generic OBO object (e.g. Term, Typedef, Instance, ...). Example:

>>> term = OBOObject(stanza_type="Term", id="FOO:001", name="bar")

>>> term = OBOObject(
...     stanza_type="Term",
...     id="FOO:001",
...     name="bar",
...     def_="Example definition { modifier=frob } ! Comment"
... )
...

An alternative way to specify tags in the constructor:

>>> term = OBOObject(stanza_type="Term", id="FOO:001", name="bar",
...                  def_=("Example definition",
...                        [("modifier", "frob")],
...                        "Comment"))
...

Note

Note the use of def_ to define the ‘def’ tag. This is to avoid the name clash with the python’s def keyword.

See also

Term Typedef Instance

is_annonymous

Is this object anonymous.

id

The id of this object.

name

Name of this object

add_tag(tag, value, modifiers=None, comment=None)

Add tag, value pair to the object with optional modifiers and comment.

Example:

>>> term = OBOObject("Term")
>>> term.add_tag("id", "FOO:002", comment="This is an id")
>>> print(term)
[Term]
id: FOO:002 ! This is an id
update(other)

Update the term with tag value pairs from other (OBOObject). The tag value pairs are appended to the end except for the id tag.

tag_count()

Return the number of tags in this object.

tags()

Return an list of all (tag, value, modifiers, comment) tuples.

format_stanza()

Return a string stanza representation of this object.

classmethod parse_stanza(stanza)

Parse and return an OBOObject instance from a stanza string.

>>> term = OBOObject.parse_stanza("""\
... [Term]
... id: FOO:001
... name: bar
... """)
>>> print(term.id, term.name)
FOO:001 bar
related_objects()

Return a list of tuple pairs where the first element is relationship (typedef id) and the second object id whom the relationship applies to.

class orangecontrib.bio.ontology.Term(*args, **kwargs)

Bases: orangecontrib.bio.ontology.OBOObject

A ‘Term’ object in the ontology.

class orangecontrib.bio.ontology.Typedef(*args, **kwargs)

Bases: orangecontrib.bio.ontology.OBOObject

A ‘Typedef’ object in the ontology.

class orangecontrib.bio.ontology.Instance(*args, **kwargs)

Bases: orangecontrib.bio.ontology.OBOObject

An ‘Instance’ object in the ontology

class orangecontrib.bio.ontology.OBOParser(file)

A simple parser for .obo files (inspired by xml.dom.pulldom)

>>> from six import StringIO
>>> file = StringIO("""\
... header_tag: header_value
... [Term]
... id: FOO:001 { modifier=bar } ! comment
... """)
>>> parser = OBOParser(file)
>>> for event, value in parser:
...     print(event, value)
...
HEADER_TAG ['header_tag', 'header_value']
START_STANZA Term
TAG_VALUE ('id', 'FOO:001', 'modifier=bar', 'comment')
CLOSE_STANZA None
parse(progress_callback=None)

Parse the file and yield parse events.