Skip to content

Basic Usage of Ontology

\(\textsf{DeepOnto}\) extends from the OWLAPI and implements many useful methods for ontology processing and reasoning, integrated in the base class Ontology.

This page gives typical examples of how to use Ontology. There are other more specific usages, please refer to the documentation by clicking Ontology.

Loading Ontology

Ontology can be easily loaded from a local ontology file by its path:

from deeponto.onto import Ontology

Importing Ontology will require the setting of JAVA_HOME environment variable if it does not find the JVM, and JVM memory allocation (defaults to 8g; if nohup is used to run the program in the backend, use nohup echo "8g" | python command):

Please enter the maximum memory located to JVM: [8g]: 16g

16g maximum memory allocated to JVM.
JVM started successfully.

Loading an ontology from a local file:

onto = Ontology("path_to_ontology.owl")

It also possible to choose a reasoner to be used:

onto = Ontology("path_to_ontology.owl", "hermit")

Tip

For faster (but incomplete) reasoning over larger ontologies, choose a reasoner like "elk".

Acessing Ontology Entities

The most fundamental feature of Ontology is to access entities in the ontology such as classes (or concepts) and properties (object, data, and annotation properties). To get an entity by its IRI, do the following:

from deeponto.onto import Ontology
# e.g., load the disease ontology
doid = Ontology("doid.owl")
# class or property IRI as input
doid.get_owl_object("http://purl.obolibrary.org/obo/DOID_9969")

To get the asserted parents or children of a given class or property, do the following:

doid.get_asserted_parents(doid.get_owl_object("http://purl.obolibrary.org/obo/DOID_9969"))
doid.get_asserted_children(doid.get_owl_object("http://purl.obolibrary.org/obo/DOID_9969"))

To obtain the literal values (as Set[str]) of an annotation property (such as \(\texttt{rdfs:label}\)) for an entity:

# note that annotations with no language tags are deemed as in English ("en")
doid.get_annotations(
    doid.get_owl_object("http://purl.obolibrary.org/obo/DOID_9969"),
    annotation_property_iri='http://www.w3.org/2000/01/rdf-schema#label',
    annotation_language_tag=None,
    apply_lowercasing=False,
    normalise_identifiers=False
)
Output:
{'carotenemia'}

To get the special entities related to top (\(\top\)) and bottom (\(\bot\)), for example, to get \(\texttt{owl:Thing}\):

doid.OWLThing

Ontology Reasoning

Ontology has an important attribute .reasoner for conducting reasoning activities. Currently, two types of reasoners are supported, i.e., HermitT and ELK.

Inferring Super- and Sub-Entities

To get the super-entities (a super-class, or a super-propety) of an entity, do the following:

doid_class = doid.get_owl_object("http://purl.obolibrary.org/obo/DOID_9969")
doid.reasoner.get_inferred_super_entities(doid_class, direct=False) 
Output:
['http://purl.obolibrary.org/obo/DOID_0014667',
'http://purl.obolibrary.org/obo/DOID_0060158',
'http://purl.obolibrary.org/obo/DOID_4']

The outputs are IRIs of the corresponding super-entities. direct is a boolean value indicating whether the returned entities are parents (direct=True) or ancestors (direct=False).

To get the sub-entities, simply replace the method name with sub_entities_of.

Inferring Class Instances

To retrieve the entailed instances of a class:

doid.reasoner.get_instances(doid_class)

Checking Entailment

The implemented reasoner also supports several entailment checks for subsumption, disjointness, and so on. For example:

doid.reasoner.check_subsumption(doid_potential_sub_entity, doid_potential_super_entity)

Feature Requests

Should you have any feature requests (such as those commonly used in the OWLAPI), please raise a ticket in the \(\textsf{DeepOnto}\) GitHub repository.


Last update: March 10, 2024
Created: November 21, 2021
GitHub: @Lawhy   Personal Page: yuanhe.wiki