Ontologies

Ontology()

Ontology such as the Gene Ontology or the Sequence Ontology: a directed acyclic graph of OntologyTerm objects stored by ID, linked by is_a and part_of relationships.

OntologyTerm(ID, parents, container, **kwargs)

Single term in an Ontology.

class picea.Ontology[source]

Ontology such as the Gene Ontology or the Sequence Ontology: a directed acyclic graph of OntologyTerm objects stored by ID, linked by is_a and part_of relationships.

Examples

>>> obo = '''format-version: 1.2
...
... [Term]
... id: SO:0000001
... name: region
...
... [Term]
... id: SO:0000704
... name: gene
... is_a: SO:0000001 ! region
... '''
>>> so = Ontology.from_obo(string=obo)
>>> so['SO:0000704'].name
['gene']
>>> [term.ID for term in so['SO:0000001'].children]
['SO:0000704']
classmethod from_obo(filename=None, string=None, skip_obsolete=True)[source]

Read an OBO formatted file or string. Exactly one of filename or string must be given.

Only [Term] stanzas are read. Parents are taken from is_a and part_of relationships. Every alternative ID (alt_id) is stored as a separate term with the same parents.

Parameters:
  • filename (str) – OBO filename

  • string (str) – OBO formatted string

  • skip_obsolete (bool) – Skip terms that are marked is_obsolete

Returns:

Ontology

Return type:

Ontology

add(element)

Add an element, stored by its ID

Parameters:

element (DAGElement) – Element to add

property elements: List[DAGElement]

List of all elements

filter(filter_func)

Filter elements in the current collection based on the output of filter_func

Parameters:

filter_func (Callable[[DAGElement], bool]) – Callable[[DagElement], bool] receives as input an element of the current collection and returns a boolean indicating whether the element should be kept (i.e. an element with a ‘True’ return value will be kept)

Returns:

DirectedAcyclicGraph subclass with unwanted elements removed

Return type:

DirectedAcyclicGraph

groupby(group_func)

Group elements in the current collection by calling ‘group_func’ and using the results as a dictionary key

Parameters:

group_func (Callable[[DAGElement], Hashable]) – Callable[[DagElement], Hashable] receives as input an element of the current collection and should return something that can be used as a dictionary key (i.e. it should be hashable)

Returns:

Dictionary with keys for groups and values individual DirectedAcyclicGraph subclasses

Return type:

DefaultDict[Hashable, DirectedAcyclicGraph]

pop(ID)

Remove an element and return it

Parameters:

ID (Hashable) – ID of the element to remove

Returns:

The removed element

Return type:

DAGElement

Raises:

KeyError – If there is no element with this ID

class picea.OntologyTerm(ID, parents, container, **kwargs)[source]

Single term in an Ontology. OBO tags are stored as attributes with list values. name and def are always present (None when missing). Use term["def"], since def is a Python keyword.

Parameters:
  • ID (str) – Term ID, e.g. GO:0008150

  • parents (List[str]) – IDs of parent terms

  • container (Ontology) – Ontology the term belongs to

  • **kwargs – OBO tags

property ID

Unique identifier of the element. Setting a new ID also updates the references of parent and child elements, and the key of the element in its container.

property children: DirectedAcyclicGraph

All descendants of this element (children, their children, etc.), as a new graph of the same type as the container

property parents: DirectedAcyclicGraph

All ancestors of this element (parents, their parents, etc.), as a new graph of the same type as the container