fix: handle compound ontology ids in translate()#33
Open
LiudengZhang wants to merge 1 commit into
Open
Conversation
translate() resolved each ontology id with a single `.filter(ontology_id=...).one()` call. CellxGene records multi-ethnic donors with comma-joined ids (e.g. "HANCESTRO:0005,HANCESTRO:0008"), which match no single term, so `.one()` raised ObjectDoesNotExist and aborted the whole run. Add a `_lookup_name` helper that splits comma-joined ids, resolves each part, and rejoins the names. It uses `.one_or_none()` so a genuinely unknown id degrades to None instead of raising. All four branches of translate() now go through the helper; return types are unchanged. Adds tests/test_translate.py covering single, compound, unknown, and mixed ids with a mock registry (no ontology DB needed). Refs jkobject/scPRINT#49
3 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
translate()resolves each ontology id with a single.filter(ontology_id=...).one()call. CellxGene records multi-ethnic donors with comma-joined ids, e.g.self_reported_ethnicity_ontology_term_id = "HANCESTRO:0005,HANCESTRO:0008". That string matches no single ontology term, so.one()raisesObjectDoesNotExistand the whole run aborts.This is what scPRINT users hit in jkobject/scPRINT#49 — a model that predicts ethnicity emits compound labels (they were in the training vocabulary), and
translate()crashes the entire embedding run on them.Fix
Add a
_lookup_name(obj, ontology_id)helper that:"HANCESTRO:0005,HANCESTRO:0008"→"European,African");.one_or_none()so a genuinely unknown id degrades toNoneinstead of raising.All four branches of
translate()(str / dict / set / list) now go through the helper. Return types and shapes are unchanged — only two behaviour changes, both strict improvements: compound ids resolve instead of crashing, and unknown ids returnNoneinstead of raising.Tests
Adds
tests/test_translate.py— covers single, compound, unknown, mixed, and whitespace cases using a mock registry, so it runs without a populated ontology database. (.one_or_none()is confirmed present in the pinnedlamindb==2.1.1.)Refs jkobject/scPRINT#49