Load ontology

First load an ontology

Loading a local ontology

Assume we have an ontology on disk in obographs-json format, e.g. nucleus.json.

First create an ontology factory, and create an ontology by passing the filename as the handle


In [1]:
from ontobio.ontol_factory import OntologyFactory

ofactory = OntologyFactory()
ont = ofactory.create("nucleus.json") ## interpreted as a json local handle

Now we can search the ontology by label; regexes can be used, but here we specify an exact match, so we expect one result:


In [2]:
[nucleus] = ont.search('nucleus')

Ancestor Queries


In [3]:
ont.ancestors(nucleus)


Out[3]:
{'GO:0005575',
 'GO:0005622',
 'GO:0005623',
 'GO:0043226',
 'GO:0043227',
 'GO:0043229',
 'GO:0043231',
 'GO:0044424',
 'GO:0044464'}

In [4]:
## Extract labels for results
["{} '{}'".format(x, ont.label(x)) for x in ont.ancestors(nucleus)]


Out[4]:
["GO:0005575 'cellular_component'",
 "GO:0044464 'cell part'",
 "GO:0043229 'intracellular organelle'",
 "GO:0005622 'intracellular'",
 "GO:0043227 'membrane-bounded organelle'",
 "GO:0044424 'intracellular part'",
 "GO:0043231 'intracellular membrane-bounded organelle'",
 "GO:0043226 'organelle'",
 "GO:0005623 'cell'"]

In [5]:
## Filter ancestors based on relationship type
ont.ancestors(nucleus, relations='subClassOf')


Out[5]:
{'GO:0005575',
 'GO:0043226',
 'GO:0043227',
 'GO:0043229',
 'GO:0043231',
 'GO:0044424',
 'GO:0044464'}

Node objects


In [42]:
## Fetch the nucleus node

ont.node(nucleus)


Out[42]:
{'id': 'http://purl.obolibrary.org/obo/GO_0005634',
 'label': 'nucleus',
 'lbl': 'nucleus',
 'meta': {'basicPropertyValues': [{'pred': 'http://www.geneontology.org/formats/oboInOwl#hasOBONamespace',
    'val': 'cellular_component'}],
  'definition': {'val': "A membrane-bounded organelle of eukaryotic cells in which chromosomes are housed and replicated. In most cells, the nucleus contains all of the cell's chromosomes except the organellar chromosomes, and is the site of RNA synthesis and processing. In some species, or in specialized cell types, RNA metabolism or DNA replication may be absent.",
   'xrefs': ['GOC:go_curators']},
  'subsets': ['http://purl.obolibrary.org/obo/go-test#goslim_aspergillus',
   'http://purl.obolibrary.org/obo/go-test#goslim_pir',
   'http://purl.obolibrary.org/obo/go-test#goslim_candida',
   'http://purl.obolibrary.org/obo/go-test#goslim_metagenomics',
   'http://purl.obolibrary.org/obo/go-test#goslim_yeast',
   'http://purl.obolibrary.org/obo/go-test#goslim_generic',
   'http://purl.obolibrary.org/obo/go-test#goslim_plant',
   'http://purl.obolibrary.org/obo/go-test#goslim_chembl'],
  'synonyms': [{'pred': 'hasExactSynonym',
    'val': 'cell nucleus',
    'xrefs': []}],
  'xrefs': [{'val': 'Wikipedia:Cell_nucleus'},
   {'val': 'NIF_Subcellular:sao1702920020'}]},
 'type': 'CLASS'}

Note that the ontol object provides special convenience methods for accessing information in a node (see Synonyms section below). However, it is always possible to access the dictionary object for a node and to probe this directly, for example:


In [44]:
ont.node(nucleus)['meta']['synonyms']


Out[44]:
[{'pred': 'hasExactSynonym', 'val': 'cell nucleus', 'xrefs': []}]

Synonyms Objects

A synonym object includes the scope, type and any provenance for the synonym. See the notebook on lexical mapping for more details on ontology lexical elements


In [34]:
ont.synonyms('GO:0005575')


Out[34]:
[GO:0005575 "cellular component" hasExactSynonym None [],
 GO:0005575 "subcellular entity" hasRelatedSynonym None ['NIF_Subcellular:nlx_subcell_100315'],
 GO:0005575 "cell or subcellular entity" hasExactSynonym None []]

Networkx Graphs

The underlying representation of the ontology is a networkx graph. You can access a graph directly:


In [37]:
graph = ont.get_graph()

In [38]:
## number of nodes in this test ontology
len(graph)


Out[38]:
28

In [41]:
graph.node[nucleus]


Out[41]:
{'id': 'http://purl.obolibrary.org/obo/GO_0005634',
 'label': 'nucleus',
 'lbl': 'nucleus',
 'meta': {'basicPropertyValues': [{'pred': 'http://www.geneontology.org/formats/oboInOwl#hasOBONamespace',
    'val': 'cellular_component'}],
  'definition': {'val': "A membrane-bounded organelle of eukaryotic cells in which chromosomes are housed and replicated. In most cells, the nucleus contains all of the cell's chromosomes except the organellar chromosomes, and is the site of RNA synthesis and processing. In some species, or in specialized cell types, RNA metabolism or DNA replication may be absent.",
   'xrefs': ['GOC:go_curators']},
  'subsets': ['http://purl.obolibrary.org/obo/go-test#goslim_aspergillus',
   'http://purl.obolibrary.org/obo/go-test#goslim_pir',
   'http://purl.obolibrary.org/obo/go-test#goslim_candida',
   'http://purl.obolibrary.org/obo/go-test#goslim_metagenomics',
   'http://purl.obolibrary.org/obo/go-test#goslim_yeast',
   'http://purl.obolibrary.org/obo/go-test#goslim_generic',
   'http://purl.obolibrary.org/obo/go-test#goslim_plant',
   'http://purl.obolibrary.org/obo/go-test#goslim_chembl'],
  'synonyms': [{'pred': 'hasExactSynonym',
    'val': 'cell nucleus',
    'xrefs': []}],
  'xrefs': [{'val': 'Wikipedia:Cell_nucleus'},
   {'val': 'NIF_Subcellular:sao1702920020'}]},
 'type': 'CLASS'}

In [45]:
import networkx
import matplotlib.pyplot as plt

In [47]:
networkx.draw(graph)


/Users/cjm/repos/ontobio/venv/lib/python3.5/site-packages/networkx/drawing/nx_pylab.py:126: MatplotlibDeprecationWarning: pyplot.hold is deprecated.
    Future behavior will be consistent with the long-time default:
    plot commands add elements without first clearing the
    Axes and/or Figure.
  b = plt.ishold()
/Users/cjm/repos/ontobio/venv/lib/python3.5/site-packages/networkx/drawing/nx_pylab.py:138: MatplotlibDeprecationWarning: pyplot.hold is deprecated.
    Future behavior will be consistent with the long-time default:
    plot commands add elements without first clearing the
    Axes and/or Figure.
  plt.hold(b)
/Users/cjm/repos/ontobio/venv/lib/python3.5/site-packages/matplotlib/__init__.py:917: UserWarning: axes.hold is deprecated. Please remove it from your matplotlibrc and/or style files.
  warnings.warn(self.msg_depr_set % key)
/Users/cjm/repos/ontobio/venv/lib/python3.5/site-packages/matplotlib/rcsetup.py:152: UserWarning: axes.hold is deprecated, will be removed in 3.0
  warnings.warn("axes.hold is deprecated, will be removed in 3.0")

In [ ]:
plt.savefig('foo.png')