Repository navigation
feat!: select smallest RLE repeat subunit length (VRS 2.1) #690
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: v3
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -771,6 +771,9 @@ def ga4gh_serialize_as_version(self, as_version: PrevVrsVersion) -> str: | |
| raise ValueError(msg) | ||
| out.append(result) | ||
| return f'{{"interval":{{"end":{out[1]},"start":{out[0]},"type":"SequenceInterval"}},"sequence_id":"{self.sequenceReference.refgetAccession.split(".")[1]}","type":"SequenceLocation"}}' | ||
| if as_version == PrevVrsVersion.V2_0: | ||
| # SequenceLocation serialization is unchanged between VRS 2.0 and 2.1 | ||
| return encode_canonical_json(self.ga4gh_serialize()).decode("utf-8") | ||
| msg = f"Received an unexpected value for `as_version`: {as_version}. MUST be an instance of `PrevVrsVersion`." | ||
| raise TypeError(msg) | ||
|
|
||
|
|
@@ -789,7 +792,10 @@ class ga4gh(Ga4ghIdentifiableObject.ga4gh): # noqa: N801 | |
| """Define GA4GH serialization metadata for this model.""" | ||
|
|
||
| prefix = "SL" | ||
| priorPrefix = {PrevVrsVersion.V1_3.value: "VSL"} # noqa: N815 | ||
| priorPrefix = { # noqa: N815 | ||
| PrevVrsVersion.V1_3.value: "VSL", | ||
| PrevVrsVersion.V2_0.value: "SL", | ||
| } | ||
| inherent = ["end", "sequenceReference", "start", "type"] | ||
|
|
||
|
|
||
|
|
@@ -877,6 +883,17 @@ class _VariationBase(Ga4ghIdentifiableObject, ABC): | |
| expressions: list[Expression] | None = None | ||
|
|
||
|
|
||
| def _interval_value(pos: Range | int) -> int: | ||
| """Return the coordinate of an int or indefinite Range ([None, v] / [v, None])""" | ||
| value: int | list[int | None] | None = get_pydantic_root(pos) | ||
| if isinstance(value, int): | ||
| return value | ||
| if not isinstance(value, list) or None not in value: | ||
| msg = f"Expected an int or indefinite Range coordinate, but got {value}" | ||
| raise ValueError(msg) | ||
| return next(v for v in value if v is not None) | ||
|
|
||
|
|
||
| class Allele(_VariationBase, BaseModelForbidExtra): | ||
| """The state of a molecule at a `Location`.""" | ||
|
|
||
|
|
@@ -895,14 +912,27 @@ class Allele(_VariationBase, BaseModelForbidExtra): | |
| def ga4gh_serialize_as_version(self, as_version: PrevVrsVersion) -> str: | ||
| """Return a serialized string following the conventions for Allele serialization as defined in the VRS version specified by ``as_version``. | ||
|
|
||
| :raises ValueError: If the Allele cannot be serialized for ``as_version``. See | ||
| ``_ga4gh_serialize_as_1_3`` and ``_ga4gh_serialize_as_2_0``. | ||
| """ | ||
| if as_version == PrevVrsVersion.V1_3: | ||
| return self._ga4gh_serialize_as_1_3() | ||
| if as_version == PrevVrsVersion.V2_0: | ||
| return self._ga4gh_serialize_as_2_0() | ||
| msg = f"Received an unexpected value for `as_version`: {as_version}. MUST be an instance of `PrevVrsVersion`." | ||
| raise TypeError(msg) | ||
|
|
||
| def _ga4gh_serialize_as_1_3(self) -> str: | ||
| """Return a serialized string following VRS 1.3 Allele serialization. | ||
|
|
||
| :raises ValueError: If ``state`` is not a ``LiteralSequenceExpression`` or | ||
| ``ReferenceLengthExpression``; ``state.sequence`` is null. | ||
|
|
||
| :param as_version: Prior VRS version to use. | ||
| :raises TypeError: If the requested version is unsupported. | ||
| :return: Serialization for the requested prior version. | ||
| """ | ||
| location_digest = self.location.compute_digest(as_version=as_version) | ||
| location_digest = self.location.compute_digest(as_version=PrevVrsVersion.V1_3) | ||
|
|
||
| if not isinstance( | ||
| self.state, LiteralSequenceExpression | ReferenceLengthExpression | ||
|
|
@@ -915,16 +945,69 @@ def ga4gh_serialize_as_version(self, as_version: PrevVrsVersion) -> str: | |
| if sequence is None: | ||
| raise ValueError("State sequence attribute must be defined.") | ||
|
|
||
| if as_version == PrevVrsVersion.V1_3: | ||
| return f'{{"location":"{location_digest}","state":{{"sequence":"{sequence}","type":"LiteralSequenceExpression"}},"type":"Allele"}}' | ||
| msg = f"Received an unexpected value for `as_version`: {as_version}. MUST be an instance of `PrevVrsVersion`." | ||
| raise TypeError(msg) | ||
| return f'{{"location":"{location_digest}","state":{{"sequence":"{sequence}","type":"LiteralSequenceExpression"}},"type":"Allele"}}' | ||
|
|
||
| def _ga4gh_serialize_as_2_0(self) -> str: | ||
| """Return a serialized string following VRS 2.0 Allele serialization. | ||
|
|
||
| VRS 2.0 normalization selected the largest valid repeat subunit length for | ||
| reference-derived insertions (``ReferenceLengthExpression`` ``length`` greater | ||
| than the location span); VRS 2.1 selects the smallest. The valid lengths are the | ||
| multiples of the smallest that divide the insertion length and fit within the | ||
| span. All other states serialize the same in VRS 2.0 and 2.1. | ||
|
|
||
| This assumes ``repeatSubunitLength`` is the smallest valid subunit, as produced | ||
| by VRS 2.1 normalization. Verifying that requires the reference sequence, so only | ||
| inconsistencies detectable from the Allele alone raise an error. | ||
|
|
||
| Nested digests are recomputed rather than read from a cached ``digest``, and are | ||
| not stored on this Allele or its location. | ||
|
|
||
| :raises ValueError: If a ``ReferenceLengthExpression`` ``state`` is not on a | ||
| ``SequenceLocation`` with int or indefinite Range coordinates, has a Range | ||
| ``length``, or has a ``repeatSubunitLength`` that is not a factor of the | ||
| insertion length within the span | ||
| """ | ||
| state = self.state | ||
| if isinstance(state, ReferenceLengthExpression): | ||
| if not isinstance(self.location, SequenceLocation) or not isinstance( | ||
| state.length, int | ||
| ): | ||
| 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( | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Optional: for a circular location that wraps the origin ( |
||
| self.location.start | ||
| ) | ||
| insert_length = state.length - ref_length | ||
| if insert_length > 0: | ||
| p = state.repeatSubunitLength | ||
| multiples = range(p, ref_length + 1, p) if p > 0 else [] | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Optional: no multiple larger than |
||
| valid = [c for c in multiples if insert_length % c == 0] | ||
| if not valid: | ||
| msg = ( | ||
| f"repeatSubunitLength {p} is inconsistent with a normalized " | ||
| f"insertion of {insert_length} residues into a {ref_length} " | ||
| "residue span" | ||
| ) | ||
| raise ValueError(msg) | ||
| state = state.model_copy(update={"repeatSubunitLength": valid[-1]}) | ||
| location = ( | ||
| self.location.compute_digest(as_version=PrevVrsVersion.V2_0) | ||
| if isinstance(self.location, SequenceLocation) | ||
| else self.location.ga4gh_serialize() | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. When For example, |
||
| ) | ||
| return encode_canonical_json( | ||
| {"location": location, "state": state.ga4gh_serialize(), "type": self.type} | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This builds the Allele serialization by hand rather than going through |
||
| ).decode("utf-8") | ||
|
|
||
| class ga4gh(Ga4ghIdentifiableObject.ga4gh): # noqa: N801 | ||
| """Define GA4GH serialization metadata for this model.""" | ||
|
|
||
| prefix = "VA" | ||
| priorPrefix = {PrevVrsVersion.V1_3.value: "VA"} # noqa: N815 | ||
| priorPrefix = { # noqa: N815 | ||
| PrevVrsVersion.V1_3.value: "VA", | ||
| PrevVrsVersion.V2_0.value: "VA", | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Lower priority since |
||
| } | ||
| inherent = ["location", "state", "type"] | ||
|
|
||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,170 @@ | ||
| interactions: | ||
| - request: | ||
| body: null | ||
| headers: {} | ||
| method: GET | ||
| uri: http://localhost:5000/seqrepo/1/sequence/ga4gh:SQ.Ya6Rs7DHhDeg7YaOSg1EoNi3U_nQ9SvO?start=1007183&end=1007183 | ||
| response: | ||
| body: | ||
| string: '' | ||
| headers: {} | ||
| status: | ||
| code: 200 | ||
| message: OK | ||
| - request: | ||
| body: null | ||
| headers: {} | ||
| method: GET | ||
| uri: http://localhost:5000/seqrepo/1/sequence/ga4gh:SQ.Ya6Rs7DHhDeg7YaOSg1EoNi3U_nQ9SvO?start=1007182&end=1007183 | ||
| response: | ||
| body: | ||
| string: T | ||
| headers: {} | ||
| status: | ||
| code: 200 | ||
| message: OK | ||
| - request: | ||
| body: null | ||
| headers: {} | ||
| method: GET | ||
| uri: http://localhost:5000/seqrepo/1/sequence/ga4gh:SQ.Ya6Rs7DHhDeg7YaOSg1EoNi3U_nQ9SvO?start=1007181&end=1007182 | ||
| response: | ||
| body: | ||
| string: G | ||
| headers: {} | ||
| status: | ||
| code: 200 | ||
| message: OK | ||
| - request: | ||
| body: null | ||
| headers: {} | ||
| method: GET | ||
| uri: http://localhost:5000/seqrepo/1/sequence/ga4gh:SQ.Ya6Rs7DHhDeg7YaOSg1EoNi3U_nQ9SvO?start=1007180&end=1007181 | ||
| response: | ||
| body: | ||
| string: T | ||
| headers: {} | ||
| status: | ||
| code: 200 | ||
| message: OK | ||
| - request: | ||
| body: null | ||
| headers: {} | ||
| method: GET | ||
| uri: http://localhost:5000/seqrepo/1/sequence/ga4gh:SQ.Ya6Rs7DHhDeg7YaOSg1EoNi3U_nQ9SvO?start=1007179&end=1007180 | ||
| response: | ||
| body: | ||
| string: G | ||
| headers: {} | ||
| status: | ||
| code: 200 | ||
| message: OK | ||
| - request: | ||
| body: null | ||
| headers: {} | ||
| method: GET | ||
| uri: http://localhost:5000/seqrepo/1/sequence/ga4gh:SQ.Ya6Rs7DHhDeg7YaOSg1EoNi3U_nQ9SvO?start=1007178&end=1007179 | ||
| response: | ||
| body: | ||
| string: T | ||
| headers: {} | ||
| status: | ||
| code: 200 | ||
| message: OK | ||
| - request: | ||
| body: null | ||
| headers: {} | ||
| method: GET | ||
| uri: http://localhost:5000/seqrepo/1/sequence/ga4gh:SQ.Ya6Rs7DHhDeg7YaOSg1EoNi3U_nQ9SvO?start=1007177&end=1007178 | ||
| response: | ||
| body: | ||
| string: G | ||
| headers: {} | ||
| status: | ||
| code: 200 | ||
| message: OK | ||
| - request: | ||
| body: null | ||
| headers: {} | ||
| method: GET | ||
| uri: http://localhost:5000/seqrepo/1/sequence/ga4gh:SQ.Ya6Rs7DHhDeg7YaOSg1EoNi3U_nQ9SvO?start=1007176&end=1007177 | ||
| response: | ||
| body: | ||
| string: T | ||
| headers: {} | ||
| status: | ||
| code: 200 | ||
| message: OK | ||
| - request: | ||
| body: null | ||
| headers: {} | ||
| method: GET | ||
| uri: http://localhost:5000/seqrepo/1/sequence/ga4gh:SQ.Ya6Rs7DHhDeg7YaOSg1EoNi3U_nQ9SvO?start=1007175&end=1007176 | ||
| response: | ||
| body: | ||
| string: G | ||
| headers: {} | ||
| status: | ||
| code: 200 | ||
| message: OK | ||
| - request: | ||
| body: null | ||
| headers: {} | ||
| method: GET | ||
| uri: http://localhost:5000/seqrepo/1/sequence/ga4gh:SQ.Ya6Rs7DHhDeg7YaOSg1EoNi3U_nQ9SvO?start=1007174&end=1007175 | ||
| response: | ||
| body: | ||
| string: T | ||
| headers: {} | ||
| status: | ||
| code: 200 | ||
| message: OK | ||
| - request: | ||
| body: null | ||
| headers: {} | ||
| method: GET | ||
| uri: http://localhost:5000/seqrepo/1/sequence/ga4gh:SQ.Ya6Rs7DHhDeg7YaOSg1EoNi3U_nQ9SvO?start=1007173&end=1007174 | ||
| response: | ||
| body: | ||
| string: G | ||
| headers: {} | ||
| status: | ||
| code: 200 | ||
| message: OK | ||
| - request: | ||
| body: null | ||
| headers: {} | ||
| method: GET | ||
| uri: http://localhost:5000/seqrepo/1/sequence/ga4gh:SQ.Ya6Rs7DHhDeg7YaOSg1EoNi3U_nQ9SvO?start=1007172&end=1007173 | ||
| response: | ||
| body: | ||
| string: C | ||
| headers: {} | ||
| status: | ||
| code: 200 | ||
| message: OK | ||
| - request: | ||
| body: null | ||
| headers: {} | ||
| method: GET | ||
| uri: http://localhost:5000/seqrepo/1/sequence/ga4gh:SQ.Ya6Rs7DHhDeg7YaOSg1EoNi3U_nQ9SvO?start=1007183&end=1007184 | ||
| response: | ||
| body: | ||
| string: C | ||
| headers: {} | ||
| status: | ||
| code: 200 | ||
| message: OK | ||
| - request: | ||
| body: null | ||
| headers: {} | ||
| method: GET | ||
| uri: http://localhost:5000/seqrepo/1/sequence/ga4gh:SQ.Ya6Rs7DHhDeg7YaOSg1EoNi3U_nQ9SvO?start=1007173&end=1007183 | ||
| response: | ||
| body: | ||
| string: GTGTGTGTGT | ||
| headers: {} | ||
| status: | ||
| code: 200 | ||
| message: OK | ||
| version: 1 |
There was a problem hiding this comment.
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_valuecalls below) run before theinsert_length > 0test, so deletion and reference RLEs, which serialize the same in 2.0 and 2.1, can raise here. For example, an RLE withstart=[5, 10],end=[12, 15],length=2,repeatSubunitLength=2(normalize passes definite ranges through unchanged) raisesExpected 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?