Skip to content

fix: handle compound ontology ids in translate()#33

Open
LiudengZhang wants to merge 1 commit into
jkobject:mainfrom
LiudengZhang:fix-translate-compound-ontology-ids
Open

fix: handle compound ontology ids in translate()#33
LiudengZhang wants to merge 1 commit into
jkobject:mainfrom
LiudengZhang:fix-translate-compound-ontology-ids

Conversation

@LiudengZhang
Copy link
Copy Markdown

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() raises ObjectDoesNotExist and 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:

  • splits comma-joined ids, resolves each part, and rejoins the names ("HANCESTRO:0005,HANCESTRO:0008""European,African");
  • uses .one_or_none() so a genuinely unknown id degrades to None instead 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 return None instead 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 pinned lamindb==2.1.1.)

Refs jkobject/scPRINT#49

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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant