{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Looking for obsolete terms without replacements" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In this example, we use `fastobo` to create a small validation script which will retrieve obsolete terms without replacement." ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import fastobo\n", "import requests" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "`fastobo.load` takes a file-handle, which can be accessed using the `raw` property of the `Response` object returned by `requests.get`:" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "res = requests.get(\"http://purl.obolibrary.org/obo/go.obo\", stream=True)\n", "doc = fastobo.load(res.raw)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Note that we are using `isinstance` a lot compared to what you may be used to in other Python library: this is because `fastobo` is based on a Rust library which is strongly-typed, so that is reflected in the Python library that wraps it. We could use the strong typing to write the same snippet using type-specific callback wrapped in a `dict`:" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "for frame in doc:\n", " \n", " if isinstance(frame, fastobo.term.TermFrame):\n", " \n", " obsolete = False\n", " replacements = []\n", "\n", " for clause in frame:\n", " if clause.raw_tag == \"is_obsolete\":\n", " obsolete |= clause.obsolete\n", " elif clause.raw_tag in (\"consider\", \"replaced_by\"):\n", " replacements.append(clause.term)\n", "\n", " if obsolete and not replacements:\n", " print(frame.id, \"is obsolete but has no replacement.\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Note that we could use the same kind of logic to retrieve terms with more than one replacement, which can be the case when an obsolete term does not have a strictly equivalent substitute in the newer versions of an ontology" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "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.9.1" } }, "nbformat": 4, "nbformat_minor": 4 }