Search code examples
python-3.xontologyowlready

Accessing Annotation of an Entity of ontology using owlready


I want to access the annotation properties of an entity from an ontology using owlready library. In the screenshot below I want to access the definition of the selected Entity i.e. impulse control disorder.

from owlready2 import *
onto = get_ontology("./HumanDO.owl").load()
for subclass_of_mental_health in list(onto.search(label ="disease of mental health")[0].subclasses()):
    print(subclass_of_mental_health.id, subclass_of_mental_health.label) # This gives outputs (see below)
    print(subclass_of_mental_health.definition) # This results in error

Above is the code to access the impulse control disorder entity. I was able to access id by simply using dot notation (<entity>.id) but when i try to <entity.definition> getting

['DOID:10937'] ['impulse control disorder']
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
~\AppData\Local\Temp\ipykernel_14348\3127299132.py in <module>
      1 for subclass_of_mental_health in list(onto.search(label ="disease of mental health")[0].subclasses()):
      2     print(subclass_of_mental_health.id, subclass_of_mental_health.label)
----> 3     print(subclass_of_mental_health.definition)

E:\Python\PythonInstall\envs\new\lib\site-packages\owlready2\entity.py in __getattr__(Class, attr)
    592     else:
    593       Prop = Class.namespace.world._props.get(attr)
--> 594       if not Prop: raise AttributeError("'%s' property is not defined." % attr)
    595       if issubclass_python(Prop, AnnotationProperty):
    596         attr = "__%s" % attr # Do NOT cache as such in __dict__, to avoid inheriting annotations

AttributeError: 'definition' property is not defined.

The ontology look like this Disease Ontology and you can download the actual file from here Github link

My final goal is to read some annotations and store them but stuck at just reading them. To make it clear, I am not able to access any property other than id and label


Solution

  • Just after loading the data you need to add sync_reasoner() like the following:

    human_onto = get_ontology("./HumanDO.owl").load()
    sync_reasoner()
    

    Then You can access all the property like the following:

    human_results = human_onto.search(label =name , _case_sensitive = False)
    for res in human_results:
        print("# ",res)
            
        for prop in res.get_properties(res):
            print("Property: ", prop)
            print("Value: ", prop[res])