Skip to content

feat!: select smallest RLE repeat subunit length (VRS 2.1) - #690

Open
theferrit32 wants to merge 3 commits into
v3from
kf/637-rle-subunit-mode-2.1
Open

theferrit32 wants to merge 3 commits into
v3from
kf/637-rle-subunit-mode-2.1

Conversation

@theferrit32

Copy link
Copy Markdown
Contributor

Closes #637. Supersedes #665.

VRS 2.1 normalization (ga4gh/vrs#700) uses the smallest valid repeat subunit for reference-derived insertions; VRS 2.0 used the largest.

Changes

  • normalize: _factor_gen now yields factors in ascending order, so the first valid cycle found is the smallest repeat subunit.
    • Breaking: insertion RLEs (length greater than the location span) get a smaller repeatSubunitLength, and so a new identifier, whenever a smaller subunit is valid. For example, AA inserted into a poly-A tract now gets a subunit length of 1 instead of 2. Deletions and reference alleles are unchanged.
  • PrevVrsVersion.V2_0: ga4gh_identify(allele, as_version=PrevVrsVersion.V2_0) returns the VRS 2.0 identifier. The VRS 2.0 subunit is computed from the stored state: the largest multiple of repeatSubunitLength that divides the inserted length and fits within the location span. No sequence lookup is needed. Allele serialization is now split into private _ga4gh_serialize_as_1_3 / _ga4gh_serialize_as_2_0 methods.

Tests

  • A normalization test for a microsatellite insertion, checking that the smallest subunit (GT) is selected.
  • test_vrs_2_0_identify: the 2.1 vs 2.0 ids for poly-A, microsatellite and indefinite-range insertions, plus a deletion whose id is the same in both. Also error cases for RLEs that can't be converted to 2.0.
  • Two translator tests now expect a subunit length of 1 instead of 2.

@theferrit32
theferrit32 requested review from a team as code owners November 10, 2026 02:25
@theferrit32
theferrit32 marked this pull request as draft November 10, 2026 02:52
@theferrit32
theferrit32 marked this pull request as ready for review November 10, 2026 16:44
Stop the subunit factor search at the first factor longer than the span, document the smallest-subunit assumption, and add type hints to the new tests.
@theferrit32
theferrit32 force-pushed the kf/637-rle-subunit-mode-2.1 branch from 518165d to 83550c8 Compare November 10, 2026 17:02
@theferrit32
theferrit32 changed the base branch from vrs/2.1 to v3 November 10, 2026 17:02

@korikuzma korikuzma left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My only thing is making sure that ga4gh/vrs validation tests are updated and ensuring we're testing it in here

@larrybabb larrybabb left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice work. The core change looks right: choosing factors in ascending order selects the smallest valid repeat subunit. I also brute-forced the 2.1→2.0 subunit conversion formula against the old largest-first loop, and it agreed on 7,217 random insertions. The new parametrized cases with paired 2.1 / 2.0 ids are a good way to pin this down.

The comments below are all about the new as_version="2.0" serialization, where a few cases produce a wrong legacy id or an unexpected error. I reproduced the first two on the branch.

Tests. To make sure the 2.0 path is covered beyond insertion RLEs on a SequenceLocation, I'd suggest adding:

  • a non-RLE Allele (e.g. a LiteralSequenceExpression state) asserting as_version="2.0" gives the same id as the 2.1 id, since only insertion RLEs should differ
  • the same check for an Allele whose location is an iriReference
  • deletion and reference RLEs (including one with definite-Range coordinates) asserting the 2.0 and 2.1 ids are equal and nothing raises
  • an insertion where the location span, not the insertion length, limits the 2.0 subunit (all current cases have span ≥ insertion length), to catch off-by-one changes to the range(...) bound
  • the ValueError path for an inconsistent repeatSubunitLength

Comment thread src/ga4gh/vrs/models.py
location = (
self.location.compute_digest(as_version=PrevVrsVersion.V2_0)
if isinstance(self.location, SequenceLocation)
else self.location.ga4gh_serialize()

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When location is an iriReference, this branch uses iriReference.ga4gh_serialize(), which reduces it to the bare digest, while the normal 2.1 serialization keeps the full IRI string. So 2.0 and 2.1 ids differ even for alleles that aren't insertion RLEs.

For example, Allele(location="ga4gh:SL.shRADI_uRNo_P7bKGqD7Hrsw6iAkXciO", state=LiteralSequenceExpression(sequence="A")) gives ga4gh:VA.leDC4CJOAEKlExh8jbJgG3RpHWNDG9t0 normally but ga4gh:VA.MIMMIPRiwpVTR_8eU0yRvSG7xkzzMCmr with as_version="2.0". Since only insertion RLEs should change between versions, I think the 2.0 path should serialize the location the same way the regular path does.

Comment thread src/ga4gh/vrs/models.py
"""
state = self.state
if isinstance(state, ReferenceLengthExpression):
if not isinstance(self.location, SequenceLocation) or not isinstance(

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These checks (and the _interval_value calls below) run before the insert_length > 0 test, so deletion and reference RLEs, which serialize the same in 2.0 and 2.1, can raise here. For example, an RLE with start=[5, 10], end=[12, 15], length=2, repeatSubunitLength=2 (normalize passes definite ranges through unchanged) raises Expected an int or indefinite Range coordinate, but got [12, 15] instead of returning the unchanged id.

Could the 2.0 adjustment be skipped (falling back to the regular serialization) whenever the state isn't an insertion, or when the location or length can't be interpreted, and only raise for an insertion that actually needs adjusting?

Comment thread src/ga4gh/vrs/models.py
else self.location.ga4gh_serialize()
)
return encode_canonical_json(
{"location": location, "state": state.ga4gh_serialize(), "type": self.type}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This builds the Allele serialization by hand rather than going through ga4gh_serialize / _recurse_ga4gh_serialize. That duplication is what causes the iriReference mismatch above, and it will drift silently if Allele's inherent fields or IRI handling change later. A simpler and more robust approach: make a copy of the Allele with the adjusted state (and the 2.0 location digest), then serialize it through the shared path. That way only the RLE subunit differs between versions.

@larrybabb larrybabb left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A few additional optional / lower-priority notes. None of these block the PR.

Comment thread src/ga4gh/vrs/models.py
priorPrefix = {PrevVrsVersion.V1_3.value: "VA"} # noqa: N815
priorPrefix = { # noqa: N815
PrevVrsVersion.V1_3.value: "VA",
PrevVrsVersion.V2_0.value: "VA",

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lower priority since CisPhasedBlock is still draft: V2_0 is only added to Allele and SequenceLocation, but a CPB's id is built from its member Allele digests, so it also changes when a member is an insertion RLE. Right now ga4gh_identify(cpb, as_version="2.0") raises AttributeError: This class does not support prior version identifiers. Probably fine to defer, but worth a follow-up issue so 2.0 handling can be made generic (serializing members with their 2.0 digests) before CPB stabilizes.

Comment thread src/ga4gh/vrs/models.py
insert_length = state.length - ref_length
if insert_length > 0:
p = state.repeatSubunitLength
multiples = range(p, ref_length + 1, p) if p > 0 else []

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Optional: no multiple larger than insert_length can divide it, so the upper bound could be min(ref_length, insert_length). As written, a 1-bp insertion into a very long repeat (e.g. a 1 Mb span) builds a list of about 1M ints and runs about 1M modulo checks per 2.0 id. That adds up in VCF-scale migrations. A generator with max(...) instead of building valid as a list would also avoid the allocation.

Comment thread src/ga4gh/vrs/models.py
):
msg = "A VRS 2.0 `ReferenceLengthExpression` requires a `SequenceLocation` and an int `length`"
raise ValueError(msg)
ref_length = _interval_value(self.location.end) - _interval_value(

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Optional: for a circular location that wraps the origin (start > end), ref_length goes negative, multiples is empty, and the error says repeatSubunitLength N is inconsistent with ... a -X residue span, which is misleading. A clear "unsupported location" error, or falling back to the regular serialization, would be friendlier.

This branch has not been deployed

No deployments
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.

3 participants