This notebook assembles some personal experiences in accessing the Basic Formal Ontology(BFO) with Owlready2.
import time
print(time.ctime())
%load_ext ipydex.displaytools
Sun Feb 7 16:11:56 2021
import owlready2 as owl2
original_iris = dict(owl2.default_world.graph._abbreviate_d) ##:i
info(original_iris) := "<class 'dict'> with length: 142"
---
# bfo = owl2.get_ontology("external_data/bfo.owl").load()
bfo = owl2.get_ontology("http://purl.obolibrary.org/obo/bfo.owl").load()
all_iris = dict(owl2.default_world.graph._abbreviate_d) ##:i
info(all_iris) := "<class 'dict'> with length: 282"
---
new_iris = [key for key in all_iris.keys() if key not in original_iris] ##:i
new_iris[:20]
info(new_iris) := "<class 'list'> with length: 140"
---
['http://basic-formal-ontology.org', 'http://creativecommons.org/licenses/by/4.0', 'http://groups.google.com/group/bfo-devel', 'http://groups.google.com/group/bfo-discuss', 'http://groups.google.com/group/bfo-owl-devel', 'http://ifomis.org/bfo', 'http://purl.obolibrary.org/obo/BFO_0000001', 'http://purl.obolibrary.org/obo/BFO_0000002', 'http://purl.obolibrary.org/obo/BFO_0000003', 'http://purl.obolibrary.org/obo/BFO_0000004', 'http://purl.obolibrary.org/obo/BFO_0000006', 'http://purl.obolibrary.org/obo/BFO_0000008', 'http://purl.obolibrary.org/obo/BFO_0000009', 'http://purl.obolibrary.org/obo/BFO_0000011', 'http://purl.obolibrary.org/obo/BFO_0000015', 'http://purl.obolibrary.org/obo/BFO_0000016', 'http://purl.obolibrary.org/obo/BFO_0000017', 'http://purl.obolibrary.org/obo/BFO_0000018', 'http://purl.obolibrary.org/obo/BFO_0000019', 'http://purl.obolibrary.org/obo/BFO_0000020']
classes = list(bfo.classes())
c0 = classes[0]
classes[:10]
[obo.BFO_0000001, obo.BFO_0000002, obo.BFO_0000003, obo.BFO_0000004, obo.BFO_0000020, obo.BFO_0000031, obo.BFO_0000006, obo.BFO_0000141, obo.BFO_0000029, obo.BFO_0000140]
c0.name ##:
c0.label ##:
c0.iri ##:
bfo.base_iri ##:
(c0.name) := 'BFO_0000001'
---
(c0.label) := ['entity']
---
(c0.iri) := 'http://purl.obolibrary.org/obo/BFO_0000001'
---
(bfo.base_iri) := 'http://purl.obolibrary.org/obo/bfo.owl#'
---
klass = bfo.world["http://purl.obolibrary.org/obo/BFO_0000001"] ##:
klass == c0
klass := obo.BFO_0000001
---
True
.base_iri
¶# Inherently, `bfo.BFO_0000001` cannot be resolved due to iri mismatch.
bfo.BFO_0000001 is None
True
# 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/'
# now this works as intended
bfo.BFO_0000001 is None ##:
bfo.BFO_0000001
(bfo.BFO_0000001 is None) := False
---
obo.BFO_0000001
.search(...)
¶# this does not require to alter the base_iri
bfo.search(iri="*BFO_0000001")
[obo.BFO_0000001]
bfo.search(label="entity")
[obo.BFO_0000001]
bfo.search(label="entity").first().iri
'http://purl.obolibrary.org/obo/BFO_0000001'
for e in classes:
print(e.name, e.label)
BFO_0000001 ['entity'] BFO_0000002 ['continuant'] BFO_0000003 ['occurrent'] BFO_0000004 ['independent continuant'] BFO_0000020 ['specifically dependent continuant'] BFO_0000031 ['generically dependent continuant'] BFO_0000006 ['spatial region'] BFO_0000141 ['immaterial entity'] BFO_0000029 ['site'] BFO_0000140 ['continuant fiat boundary'] BFO_0000008 ['temporal region'] BFO_0000011 ['spatiotemporal region'] BFO_0000015 ['process'] BFO_0000035 ['process boundary'] BFO_0000009 ['two-dimensional spatial region'] BFO_0000028 ['three-dimensional spatial region'] BFO_0000016 ['disposition'] BFO_0000017 ['realizable entity'] BFO_0000023 ['role'] BFO_0000019 ['quality'] BFO_0000018 ['zero-dimensional spatial region'] BFO_0000024 ['fiat object part'] BFO_0000040 ['material entity'] BFO_0000026 ['one-dimensional spatial region'] BFO_0000027 ['object aggregate'] BFO_0000030 ['object'] BFO_0000034 ['function'] BFO_0000038 ['one-dimensional temporal region'] BFO_0000148 ['zero-dimensional temporal region'] BFO_0000142 ['one-dimensional continuant fiat boundary'] BFO_0000146 ['two-dimensional continuant fiat boundary'] BFO_0000147 ['zero-dimensional continuant fiat boundary'] BFO_0000144 ['process profile'] BFO_0000182 ['history'] BFO_0000145 ['relational quality']
annotation_properties = list(bfo.annotation_properties()) ##:i
info(annotation_properties) := "<class 'list'> with length: 22"
---
tab = []
for e in annotation_properties:
try:
label = e.label
except TypeError:
label = "<TypeError>"
tab.append((e.name, label))
import tabulate
print(tabulate.tabulate(tab))
----------- -------------------------------- isDefinedBy <TypeError> seeAlso <TypeError> IAO_0000116 ['editor note'] contributor [] license [] homepage [] mbox [] BFO_0000179 ['BFO OWL specification label'] IAO_0000115 ['definition'] IAO_0000232 ['curator note'] BFO_0000180 ['BFO CLIF specification label'] IAO_0000119 ['definition source'] IAO_0000111 ['editor preferred term'] IAO_0000112 ['example of usage'] IAO_0000117 ['term editor'] IAO_0000118 ['alternative term'] IAO_0000412 ['imported from'] IAO_0000600 ['elucidation'] IAO_0000601 ['has associated axiom(nl)'] IAO_0000602 ['has associated axiom(fol)'] IAO_0010000 ['has axiom label'] member [] ----------- --------------------------------
annotation_properties[2].label
['editor note']
isolated problem reproduction (after kernel restart)
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)
['editor note'] []
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-1-bf2b883b90ca> in <module> 11 12 # works not -> TypeError: ---> 13 print(annotation_properties[0].label) /media/workcard/workstickdir/RST/expertise_system/ontologie_experimente/owlready2_cvk_fork/owlready2/prop.py in __getattr__(Prop, attr) 258 259 def __getattr__(Prop, attr): --> 260 if Prop.namespace.world is owl_world: Annot = CURRENT_NAMESPACES.get()[-1].world._props.get(attr) 261 else: Annot = Prop.namespace.world._props.get(attr) 262 #l = CURRENT_NAMESPACES.get() TypeError: 'NoneType' object is not subscriptable