.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "examples/syntactic/plot_1_onto2vec.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code. .. rst-class:: sphx-glr-example-title .. _sphx_glr_examples_syntactic_plot_1_onto2vec.py: Onto2Vec =========== This example corresponds to the paper `Onto2Vec: joint vector-based representation of biological entities and their ontology-based annotations `_. This method is an approach to learn numerical representations (embeddings) of (biomedical) ontologies by representing ontology axioms as text sequences and applying an unsupervised learning algorithm such as Word2Vec. Onto2Vec uses an ontology reasoner to infer new axioms as a preprocessing step. The algorithm is tested on the protein-protein interaction task. .. GENERATED FROM PYTHON SOURCE LINES 15-20 For this algorithm, we need three components: - The reasoner - The corpus generator - The Word2Vec model .. GENERATED FROM PYTHON SOURCE LINES 20-38 .. code-block:: Python import mowl mowl.init_jvm("20g") from mowl.datasets.builtin import PPIYeastSlimDataset from mowl.corpus import extract_and_save_axiom_corpus from mowl.owlapi import OWLAPIAdapter from mowl.reasoning import MOWLReasoner from org.semanticweb.elk.owlapi import ElkReasonerFactory from java.util import HashSet from gensim.models.word2vec import LineSentence from gensim.models import Word2Vec import os .. GENERATED FROM PYTHON SOURCE LINES 39-44 Inferring new axioms -------------------- Onto2Vec uses an ontology reasoner to infer new axioms as a preprocessing step. In the original paper, the authors used the HermiT reasoner. For this example, we use the ELK reasoner. .. GENERATED FROM PYTHON SOURCE LINES 44-51 .. code-block:: Python dataset = PPIYeastSlimDataset() reasoner_factory = ElkReasonerFactory() reasoner = reasoner_factory.createReasoner(dataset.ontology) mowl_reasoner = MOWLReasoner(reasoner) .. GENERATED FROM PYTHON SOURCE LINES 52-59 We wrap the reasoner into the :class:`MOWLReasoner ` class \ in order to use some shortcuts the mOWL provides such as: - inferring subclass axioms - inferring equivalent class axioms - inferring disjoint axioms (not applicable for this example since we use ELK reasoner) .. GENERATED FROM PYTHON SOURCE LINES 59-64 .. code-block:: Python classes = dataset.ontology.getClassesInSignature() subclass_axioms = mowl_reasoner.infer_subclass_axioms(classes) equivalent_class_axioms = mowl_reasoner.infer_equivalent_class_axioms(classes) .. GENERATED FROM PYTHON SOURCE LINES 65-66 We can now add the inferred axioms to the ontology. .. GENERATED FROM PYTHON SOURCE LINES 66-77 .. code-block:: Python adapter = OWLAPIAdapter() manager = adapter.owl_manager axioms = HashSet() axioms.addAll(subclass_axioms) axioms.addAll(equivalent_class_axioms) manager.addAxioms(dataset.ontology, axioms) .. rst-class:: sphx-glr-script-out .. code-block:: none .. GENERATED FROM PYTHON SOURCE LINES 78-83 Generating the corpus and training the model ----------------------------------------------- Now that we have an extended ontology, we can generate the corpus out of it. After that, we can train the Word2Vec model. .. GENERATED FROM PYTHON SOURCE LINES 83-89 .. code-block:: Python extract_and_save_axiom_corpus(dataset.ontology, "onto2vec_corpus.txt") sentences = LineSentence("onto2vec_corpus.txt") model = Word2Vec(sentences, vector_size=5, window=2, min_count=1, workers=4, epochs=2) .. GENERATED FROM PYTHON SOURCE LINES 90-91 Cleaning up memory .. GENERATED FROM PYTHON SOURCE LINES 91-93 .. code-block:: Python os.remove("onto2vec_corpus.txt") .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 15.920 seconds) **Estimated memory usage:** 499 MB .. _sphx_glr_download_examples_syntactic_plot_1_onto2vec.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_1_onto2vec.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_1_onto2vec.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_1_onto2vec.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_