Repository navigation
feat!: select smallest RLE repeat subunit length (VRS 2.1) - #690
theferrit32 wants to merge 3 commits into
Conversation
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.
518165d to
83550c8
Compare
korikuzma
left a comment
There was a problem hiding this comment.
My only thing is making sure that ga4gh/vrs validation tests are updated and ensuring we're testing it in here
larrybabb
left a comment
There was a problem hiding this comment.
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
LiteralSequenceExpressionstate) assertingas_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
locationis 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
ValueErrorpath for an inconsistentrepeatSubunitLength
| location = ( | ||
| self.location.compute_digest(as_version=PrevVrsVersion.V2_0) | ||
| if isinstance(self.location, SequenceLocation) | ||
| else self.location.ga4gh_serialize() |
There was a problem hiding this comment.
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.
| """ | ||
| state = self.state | ||
| if isinstance(state, ReferenceLengthExpression): | ||
| if not isinstance(self.location, SequenceLocation) or not isinstance( |
There was a problem hiding this comment.
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?
| else self.location.ga4gh_serialize() | ||
| ) | ||
| return encode_canonical_json( | ||
| {"location": location, "state": state.ga4gh_serialize(), "type": self.type} |
There was a problem hiding this comment.
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
left a comment
There was a problem hiding this comment.
A few additional optional / lower-priority notes. None of these block the PR.
| priorPrefix = {PrevVrsVersion.V1_3.value: "VA"} # noqa: N815 | ||
| priorPrefix = { # noqa: N815 | ||
| PrevVrsVersion.V1_3.value: "VA", | ||
| PrevVrsVersion.V2_0.value: "VA", |
There was a problem hiding this comment.
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.
| insert_length = state.length - ref_length | ||
| if insert_length > 0: | ||
| p = state.repeatSubunitLength | ||
| multiples = range(p, ref_length + 1, p) if p > 0 else [] |
There was a problem hiding this comment.
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.
| ): | ||
| 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( |
There was a problem hiding this comment.
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.
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_gennow yields factors in ascending order, so the first valid cycle found is the smallest repeat subunit.lengthgreater than the location span) get a smallerrepeatSubunitLength, and so a new identifier, whenever a smaller subunit is valid. For example,AAinserted 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 ofrepeatSubunitLengththat 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_0methods.Tests
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.