Extend scispaCy with external databases using pyobo
mainYou can load arbitrary databases and ontologies into scispaCy by integrating with pyobo. This allows you to use the EntityLinker logic with external identifiers like HGNC.
Requirements:
pip install "pyobo>=0.12.9"
Use pyobo.get_scispacy_entity_linker(database_name, ...) to create a linker instance that can be used directly as a spaCy component.
import pyobo
import spacy
from scispacy.linking import EntityLinker
from tabulate import tabulate
# Create a linker for the HGNC database
linker: EntityLinker = pyobo.get_scispacy_entity_linker("hgnc", filter_for_definitions=False)
# Use it with a standard spaCy model
nlp = spacy.load("en_core_web_sm")
text = "RAC(Rho family)-alpha serine/threonine-protein kinase is an enzyme that in humans is encoded by the AKT1 gene."
# Apply the linker to the doc
doc = linker(nlp(text))
# Process results
rows = [
(
span,
span.start_char,
span.end_char,
f"[{identifier}](https://bioregistry.io/{identifier})",
score,
)
for span in doc.ents
for identifier, score in span._.kb_ents
]
print(tabulate(rows, headers=["text", "start", "end", "prefix", "identifier"], tablefmt="github"))