From 32eaf3e8cb3ca350ffce578413992cc68268f404 Mon Sep 17 00:00:00 2001 From: Ariel Even Date: Thu, 26 Nov 2026 08:09:33 +0200 Subject: [PATCH] fix: serialize enref'd references by digest so ids don't change ga4gh_enref replaces each referable attribute with the referenced object's CURIE as a plain string, then identifies the parent. _recurse_ga4gh_serialize returned that string unchanged, so the parent's digest was computed over "ga4gh:SL." instead of "". An Allele enref'd without a precomputed id therefore got a different VA id than the same Allele inlined. Serialize plain GA4GH CURIE strings to their digest, the same way iriReference.ga4gh_serialize already does. test_enref2's fixture carried the id produced by this bug (VA.LDzK...); it now uses the id ga4gh_identify gives for that Allele (VA.0AeP..., as in #440). --- src/ga4gh/vrs/models.py | 5 ++++- tests/test_vrs.py | 35 +++++++++++++++++++++++++++++++---- 2 files changed, 35 insertions(+), 5 deletions(-) diff --git a/src/ga4gh/vrs/models.py b/src/ga4gh/vrs/models.py index ad2978d5..6dca6dc5 100644 --- a/src/ga4gh/vrs/models.py +++ b/src/ga4gh/vrs/models.py @@ -237,7 +237,10 @@ def _recurse_ga4gh_serialize(obj): if isinstance(obj, RootModel): return _recurse_ga4gh_serialize(obj.model_dump()) if isinstance(obj, str): - return obj + # A referable attribute set to a plain GA4GH CURIE (as ga4gh_enref does) + # serializes to its digest, the same as an iriReference or inlined object. + m = GA4GH_IR_REGEXP.match(obj) + return m["digest"] if m else obj if isinstance(obj, list): return [_recurse_ga4gh_serialize(x) for x in obj] return obj diff --git a/tests/test_vrs.py b/tests/test_vrs.py index 975854ab..dfe2d331 100644 --- a/tests/test_vrs.py +++ b/tests/test_vrs.py @@ -247,8 +247,8 @@ def test_enref2(): object_store = {} a = { "type": "Allele", - "id": "ga4gh:VA.LDzK5JahEZG2Ua_5itDtVV8v3O1ptTgI", - "digest": "LDzK5JahEZG2Ua_5itDtVV8v3O1ptTgI", + "id": "ga4gh:VA.0AePZIWZUNsUlQTamyLrjm2HWUw2opLt", + "digest": "0AePZIWZUNsUlQTamyLrjm2HWUw2opLt", "location": { "id": "ga4gh:SL.wIlaGykfwHIpPY2Fcxtbx4TINbbODFVz", "digest": "wIlaGykfwHIpPY2Fcxtbx4TINbbODFVz", @@ -273,14 +273,41 @@ def test_enref2(): ) assert a_enreffed.location == "ga4gh:SL.wIlaGykfwHIpPY2Fcxtbx4TINbbODFVz" assert a_enreffed.model_dump(exclude_none=True) == { - "id": "ga4gh:VA.LDzK5JahEZG2Ua_5itDtVV8v3O1ptTgI", - "digest": "LDzK5JahEZG2Ua_5itDtVV8v3O1ptTgI", + "id": "ga4gh:VA.0AePZIWZUNsUlQTamyLrjm2HWUw2opLt", + "digest": "0AePZIWZUNsUlQTamyLrjm2HWUw2opLt", "type": "Allele", "location": "ga4gh:SL.wIlaGykfwHIpPY2Fcxtbx4TINbbODFVz", "state": {"type": "LiteralSequenceExpression", "sequence": "T"}, } +def test_enref_without_precomputed_ids(): + """Enref must not change the identifier of an object whose ids are not set yet""" + allele_dict = { + "type": "Allele", + "location": { + "type": "SequenceLocation", + "sequenceReference": { + "type": "SequenceReference", + "refgetAccession": "SQ.IIB53T8CNeJJdUqzn9V_JnRtQadwWCbl", + }, + "start": 44908821, + "end": 44908822, + }, + "state": {"type": "LiteralSequenceExpression", "sequence": "T"}, + } + expected_id = ga4gh_identify(models.Allele(**allele_dict), in_place="never") + + object_store = {} + enreffed = vrs_enref(models.Allele(**allele_dict), object_store=object_store) + assert enreffed.location == "ga4gh:SL.wIlaGykfwHIpPY2Fcxtbx4TINbbODFVz" + assert enreffed.id == expected_id + + dereffed = vrs_deref(enreffed, object_store=object_store) + assert dereffed.id == expected_id + assert dereffed.location.id == "ga4gh:SL.wIlaGykfwHIpPY2Fcxtbx4TINbbODFVz" + + def test_class_refatt_map(): class_refatt_map_expected = { "Allele": ["location"],