{ "cells": [ { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "# Resolving Strategies\n", "\n", "* Example on how to use resolving strategies\n" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "ExecuteTime": { "end_time": "2019-09-23T18:50:20.068658Z", "start_time": "2019-09-23T18:50:19.054054Z" } }, "outputs": [], "source": [ "import os\n", "import json\n", "\n", "from kgforge.core import KnowledgeGraphForge\n", "from kgforge.specializations.resources import Dataset" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stdin", "output_type": "stream", "text": [ " ········\n" ] } ], "source": [ "import getpass\n", "TOKEN = getpass.getpass()" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "BUCKET = \"dke/kgforge\"\n", "forge = KnowledgeGraphForge(\"../use-cases/prod-forge-nexus.yml\",\n", " bucket=BUCKET,\n", " token=TOKEN\n", " )" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "### List available resolvers" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'agent': {'agents': {'bucket': 'bbp/agents', 'filters': None}},\n", " 'ontology': {'terms': {'bucket': 'neurosciencegraph/datamodels',\n", " 'filters': None},\n", " 'CellType': {'bucket': 'neurosciencegraph/datamodels',\n", " 'filters': {'subClassOf*.id': 'BrainCellType'}},\n", " 'BrainRegion': {'bucket': 'neurosciencegraph/datamodels',\n", " 'filters': {'subClassOf*.id': 'BrainRegion'}},\n", " 'Species': {'bucket': 'neurosciencegraph/datamodels',\n", " 'filters': {'has_rank.id': 'http://purl.obolibrary.org/obo/NCBITaxon_species'}},\n", " 'Strain': {'bucket': 'neurosciencegraph/datamodels',\n", " 'filters': {'has_rank.id': 'http://purl.obolibrary.org/obo/NCBITaxon_strain'}}}}" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "forge.resolvers(output='dict')" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [], "source": [ "case_insensitive = forge.resolve('Pv+', scope='ontology', strategy='EXACT_CASE_INSENSITIVE_MATCH')" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [], "source": [ "exact = forge.resolve('PV+', scope='ontology', strategy='EXACT_MATCH')" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [], "source": [ "assert case_insensitive == exact" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "### Try with a similar string, but other characters" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [], "source": [ "similar = forge.resolve('pv-', scope='ontology', strategy='EXACT_CASE_INSENSITIVE_MATCH')" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [], "source": [ "similar1 = forge.resolve('pv:', scope='ontology', strategy='EXACT_CASE_INSENSITIVE_MATCH')" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [], "source": [ "assert similar is None\n", "assert similar1 is None" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "### Combine spaces and other characters" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "{\n", " id: https://bbp.epfl.ch/ontologies/core/ttypes/229_L6_IT_CTX\n", " type: Class\n", " label: 229_L6 IT CTX\n", " subClassOf:\n", " [\n", " https://bbp.epfl.ch/ontologies/core/ttypes/L4_5_6_IT_Car3\n", " https://bbp.epfl.ch/ontologies/core/celltypes/NeuronTranscriptomicType\n", " ]\n", "}\n" ] } ], "source": [ "print(forge.resolve('229_l6 it ctx', scope='ontology', strategy='EXACT_CASE_INSENSITIVE_MATCH'))" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "{\n", " id: https://bbp.epfl.ch/ontologies/core/ttypes/264_L5_6_NP_CTX\n", " type: Class\n", " label: 264_L5/6 NP CTX\n", " subClassOf:\n", " [\n", " https://bbp.epfl.ch/ontologies/core/ttypes/L5_6_NP_CTX\n", " https://bbp.epfl.ch/ontologies/core/celltypes/NeuronTranscriptomicType\n", " ]\n", "}\n" ] } ], "source": [ "print(forge.resolve('264_L5/6 np CTX', scope='ontology', strategy='EXACT_CASE_INSENSITIVE_MATCH'))" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "## Resolve from a specific ontology\n", "This is particularly useful when two entities have a similar acronym like the example below, where there is a brain region called BAC, and a electrical cell type labelled bAC.\n", "\n", "Without any specification, it will return the first result in the list of matches (no priority or ranking)." ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "### List available ontologies to resolve from" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['terms', 'CellType', 'BrainRegion', 'Species', 'Strain']" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "list(forge.resolvers(output='dict')['ontology'].keys()) # terms is for resolving accross all ontologies" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "{\n", " id: http://uri.interlex.org/base/ilx_0738199\n", " type: Class\n", " label: bAC\n", " notation: bAC\n", " prefLabel: Burst accommodating electrical type\n", " subClassOf:\n", " [\n", " nsg:EType\n", " bmo:NeuronElectricalType\n", " ]\n", "}\n" ] } ], "source": [ "from kgforge.core.commons.strategies import ResolvingStrategy\n", "\n", "print(forge.resolve(\"bAC\", scope=\"ontology\", target=\"terms\", \n", " strategy=ResolvingStrategy.EXACT_CASE_INSENSITIVE_MATCH))" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "{\n", " id: http://uri.interlex.org/base/ilx_0738199\n", " type: Class\n", " label: bAC\n", " notation: bAC\n", " prefLabel: Burst accommodating electrical type\n", " subClassOf:\n", " [\n", " nsg:EType\n", " bmo:NeuronElectricalType\n", " ]\n", "}\n" ] } ], "source": [ "print(forge.resolve(\"bAC\", scope=\"ontology\", target=\"CellType\", strategy=\"EXACT_CASE_INSENSITIVE_MATCH\"))" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "{\n", " id: http://api.brain-map.org/api/v2/data/Structure/287\n", " type: Class\n", " label: Bed nucleus of the anterior commissure\n", " isDefinedBy: http://bbp.epfl.ch/neurosciencegraph/ontologies/core/brainregion\n", " notation: BAC\n", " prefLabel: Bed nucleus of the anterior commissure\n", " subClassOf: nsg:BrainRegion\n", "}\n" ] } ], "source": [ "print(forge.resolve(\"bAC\", scope=\"ontology\", target=\"BrainRegion\", strategy=\"EXACT_CASE_INSENSITIVE_MATCH\"))" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "{\n", " id: http://purl.obolibrary.org/obo/NCBITaxon_10116\n", " type: Class\n", " label: Rattus norvegicus\n", " altLabel: rat\n", " atlasRelease:\n", " {\n", " id: https://bbp.epfl.ch/neurosciencegraph/data/4906ab85-694f-469d-962f-c0174e901885\n", " }\n", " subClassOf:\n", " [\n", " nsg:Species\n", " NCBITaxon:10114\n", " ]\n", "}\n" ] } ], "source": [ "print(forge.resolve(\"rat\", scope=\"ontology\", target=\"Species\", strategy=\"EXACT_CASE_INSENSITIVE_MATCH\"))" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "{\n", " id: http://www.ebi.ac.uk/efo/EFO_0001342\n", " type: Class\n", " label: Wistar\n", " altLabel:\n", " [\n", " Rats, Wistar\n", " Wistar rat\n", " Wistar rats\n", " ]\n", " atlasRelease:\n", " {\n", " id: https://bbp.epfl.ch/neurosciencegraph/data/4906ab85-694f-469d-962f-c0174e901885\n", " }\n", " subClassOf: NCBITaxon:10116\n", "}\n" ] } ], "source": [ "print(forge.resolve(\"Wistar\", scope=\"ontology\", target=\"Strain\", strategy=\"EXACT_CASE_INSENSITIVE_MATCH\"))" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "### Ensuring one label" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [], "source": [ "resource = forge.resolve(\"AOB_MC\", scope=\"ontology\", target=\"terms\", strategy=\"EXACT_MATCH\")\n", "assert isinstance(resource.label, str)" ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [], "source": [ "excitatory = forge.resolve(\"exc\", scope='ontology', strategy='EXACT_CASE_INSENSITIVE_MATCH')\n", "assert isinstance(excitatory.label, str)" ] }, { "cell_type": "code", "execution_count": 20, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "{\n", " id: https://neuroshapes.org/ExcitatoryNeuron\n", " type: Class\n", " label: Excitatory Neuron\n", " notation: Exc\n", " prefLabel: Excitatory Neuron\n", " subClassOf: bmo:NeurotransmitterType\n", "}\n" ] } ], "source": [ "print(excitatory)" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.11.5" }, "vscode": { "interpreter": { "hash": "9ac393a5ddd595f2c78ea58b15bf8d269850a4413729cbea5c5fae9013762763" } } }, "nbformat": 4, "nbformat_minor": 4 }