#!/usr/bin/env python # coding: utf-8 # # General Information # This notebook assembles some personal experiences in accessing the [Basic Formal Ontology](https://basic-formal-ontology.org/users.html)(BFO) with [Owlready2](https://owlready2.readthedocs.io/en/latest/index.html). # In[1]: import time print(time.ctime()) get_ipython().run_line_magic('load_ext', 'ipydex.displaytools') # In[2]: import owlready2 as owl2 # In[3]: original_iris = dict(owl2.default_world.graph._abbreviate_d) ##:i # In[4]: # bfo = owl2.get_ontology("external_data/bfo.owl").load() bfo = owl2.get_ontology("http://purl.obolibrary.org/obo/bfo.owl").load() # In[5]: all_iris = dict(owl2.default_world.graph._abbreviate_d) ##:i # In[6]: new_iris = [key for key in all_iris.keys() if key not in original_iris] ##:i new_iris[:20] # In[7]: classes = list(bfo.classes()) c0 = classes[0] classes[:10] # In[8]: c0.name ##: c0.label ##: c0.iri ##: bfo.base_iri ##: # ## How to access a class by its `name` (i.e. not by its `label`)? # # ### Manually building the iri # # This requires to know the internal iri-prefix which is not the same as `bfo.base_iri` # In[9]: klass = bfo.world["http://purl.obolibrary.org/obo/BFO_0000001"] ##: klass == c0 # ### Fixing (hacking) `.base_iri` # # In[10]: # Inherently, `bfo.BFO_0000001` cannot be resolved due to iri mismatch. bfo.BFO_0000001 is None # In[11]: # changing the base_iri to the string wich is used as prefix in `bfo.world.graph._abbreviate_d` bfo.base_iri = 'http://purl.obolibrary.org/obo/' # In[12]: # now this works as intended bfo.BFO_0000001 is None ##: bfo.BFO_0000001 # ### Access via `.search(...)` # In[13]: # this does not require to alter the base_iri bfo.search(iri="*BFO_0000001") # In[14]: bfo.search(label="entity") # In[15]: bfo.search(label="entity").first().iri # ## General overview of the entities in BFO # In[16]: for e in classes: print(e.name, e.label) # In[17]: annotation_properties = list(bfo.annotation_properties()) ##:i # In[18]: tab = [] for e in annotation_properties: try: label = e.label except TypeError: label = "" tab.append((e.name, label)) # In[19]: import tabulate # In[20]: print(tabulate.tabulate(tab)) # In[21]: annotation_properties[2].label # --- # # isolated problem reproduction (after kernel restart) # In[1]: import owlready2 as owl2 bfo = owl2.get_ontology("http://purl.obolibrary.org/obo/bfo.owl").load() annotation_properties = list(bfo.annotation_properties()) # works -> List[str] print(annotation_properties[2].label) # works -> empty list print(annotation_properties[3].label) # works not -> TypeError: print(annotation_properties[0].label)