#!/usr/bin/env python # coding: utf-8 # ## 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) # In[4]: ## Extract labels for results ["{} '{}'".format(x, ont.label(x)) for x in ont.ancestors(nucleus)] # In[5]: ## Filter ancestors based on relationship type ont.ancestors(nucleus, relations='subClassOf') # ### Node objects # In[42]: ## Fetch the nucleus node ont.node(nucleus) # 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'] # ### 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') # ### 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) # In[41]: graph.node[nucleus] # In[45]: import networkx import matplotlib.pyplot as plt # In[47]: networkx.draw(graph) # In[ ]: plt.savefig('foo.png')