Repository navigation
fix: validate reference allele stated in HGVS expressions #655
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: main
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 |
|---|---|---|
|
|
@@ -17,6 +17,22 @@ | |
| _logger = logging.getLogger(__name__) | ||
|
|
||
|
|
||
| def _reference_bases(ref: object) -> str | None: | ||
| """Return stated reference bases, ignoring a length stored in ``edit.ref``. | ||
|
|
||
| HGVS keeps a deletion length there for expressions such as ``del1`` and | ||
| ``del1insAT`` (``ref`` is ``"1"`` and ``ref_s`` is unset). Digits are not a | ||
| sequence. The hgvs validator skips them the same way. | ||
| """ | ||
| if not isinstance(ref, str) or not ref: | ||
| return None | ||
| try: | ||
| int(ref) | ||
| except ValueError: | ||
| return ref | ||
| return None | ||
|
|
||
|
|
||
| class HgvsTools: | ||
| """A convenience class that exposes only the tools needed by vrs-python for working with HGVS (Human Genome Variation Society) notation. | ||
|
|
||
|
|
@@ -40,6 +56,13 @@ def __init__(self, data_proxy: _DataProxy | None = None) -> None: | |
| self.uta_conn = hgvs.dataproviders.uta.connect() | ||
| self.normalizer = hgvs.normalizer.Normalizer(self.uta_conn, validate=True) | ||
| self.variant_mapper = hgvs.variantmapper.VariantMapper(self.uta_conn) | ||
| # c. to n. projection for translation. Reference checking happens | ||
| # afterwards and has to see the allele the expression stated. | ||
| self._c_to_n_mapper = hgvs.variantmapper.VariantMapper( | ||
| self.uta_conn, | ||
| replace_reference=False, | ||
| prevalidation_level="INTRINSIC", | ||
|
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. I think For example, on NM_006087.3 (CDS length 1335), Since the goal is to stop emitting Alleles from invalid HGVS, could we keep those bounds checks? For example: read the stated ref before projecting and keep using the default (validating) mapper, or call the extrinsic bounds checks explicitly. A test with an out-of-CDS c. position would lock this in. |
||
| ) | ||
| self.data_proxy = data_proxy | ||
|
|
||
| def close(self) -> None: | ||
|
|
@@ -49,6 +72,7 @@ def close(self) -> None: | |
| """ | ||
| self.normalizer = None | ||
| self.variant_mapper = None | ||
| self._c_to_n_mapper = None | ||
| self.data_proxy = None | ||
| if self.uta_conn is not None: | ||
| self.uta_conn.close() | ||
|
|
@@ -139,9 +163,19 @@ def get_position_and_state(self, sv: HgvsSequenceVariant) -> tuple[int, int, str | |
|
|
||
| return start, end, state | ||
|
|
||
| def extract_allele_values(self, hgvs_expr: str) -> dict | None: | ||
| def extract_allele_values( | ||
| self, hgvs_expr: str, require_validation: bool = True | ||
| ) -> dict | None: | ||
| """Parse hgvs into a VRS Allele Object | ||
|
|
||
| :param hgvs_expr: HGVS expression to parse | ||
| :param require_validation: If `True`, a reference allele stated as | ||
| nucleotides (the `C` in `c.900C>A`, or `delC`) must match the | ||
| reference sequence, otherwise a `DataProxyValidationError` is | ||
| raised. A length (`del1`, `del1insAT`) is not a sequence and is not | ||
| compared. If `False`, a mismatch is only logged and the allele is | ||
| still returned. Defaults to `True`. | ||
|
|
||
| kwargs: | ||
| rle_seq_limit Optional(int): If RLE is set as the new state after | ||
| normalization, this sets the limit for the length of the `sequence`. | ||
|
|
@@ -185,10 +219,27 @@ def extract_allele_values(self, hgvs_expr: str) -> dict | None: | |
|
|
||
| # translate coding coordinates to positional coordinates, if necessary | ||
| if sv.type == "c": | ||
| sv = self.c_to_n(sv) | ||
| sv = self._project_c_to_n(sv) | ||
|
|
||
| (start, end, state) = self.get_position_and_state(sv) | ||
|
|
||
| # The HGVS expression may state the expected reference allele (e.g. the | ||
| # `C` in `c.900C>A`, or the deleted bases in `g.44908822delC`). A | ||
| # mismatch means the input itself is invalid: silently emitting a VRS | ||
| # Allele would produce a plausible-but-wrong variant object | ||
| # (ga4gh/vrs-python#364), so validate it against the reference | ||
| # sequence just like the gnomAD translator does. A length-only ref | ||
| # (`del1`) is not a sequence and is left alone. | ||
| ref_allele = _reference_bases(getattr(sv.posedit.edit, "ref", None)) | ||
|
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. A stated ref using the ambiguity code |
||
| if ref_allele: | ||
| self.data_proxy.validate_ref_seq( | ||
| sv.ac, | ||
| start, | ||
| end, | ||
| ref_allele.upper(), | ||
|
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.
Mapping |
||
| require_validation=require_validation, | ||
| ) | ||
|
|
||
| return { | ||
| "sequence_id": sv.ac, | ||
| "refget_accession": refget_accession, | ||
|
|
@@ -331,6 +382,23 @@ def c_to_n(self, hgvs: HgvsSequenceVariant) -> HgvsSequenceVariant: | |
| """Perform hgvs library `c_to_n` method""" | ||
| return self.variant_mapper.c_to_n(hgvs) | ||
|
|
||
| def _project_c_to_n(self, sv: HgvsSequenceVariant) -> HgvsSequenceVariant: | ||
| """Project a c. variant to n. coordinates, keeping its stated reference. | ||
|
|
||
| ``VariantMapper.c_to_n`` validates that reference extrinsically and then | ||
| replaces it. A mismatch raises ``HGVSInvalidVariantError`` before | ||
| ``require_validation`` applies, and a replacement makes the stated | ||
| allele compare equal to the sequence it was overwritten with. A | ||
| length-only deletion is rewritten too: the length lives in ``edit.ref`` | ||
| while ``ref_s`` is unset, so ``fill_ref`` treats it as missing. | ||
| """ | ||
| stated_ref = getattr(sv.posedit.edit, "ref", None) | ||
| mapped = self._c_to_n_mapper.c_to_n(sv) | ||
| edit = mapped.posedit.edit | ||
| if hasattr(edit, "ref"): | ||
| edit.ref = stated_ref | ||
|
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 simplification: rather than a second |
||
| return mapped | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| import os | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -224,6 +224,15 @@ | |
| ], | ||
| "value": "ATGAACTCAATGTTCAGGATT" | ||
| }, | ||
| { | ||
| "func": "get_seq", | ||
| "args": [ | ||
| "NM_006087.3", | ||
|
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. Minor: I don't think this |
||
| 1271, | ||
| 1272 | ||
| ], | ||
| "value": "G" | ||
| }, | ||
| { | ||
| "func": "get_seq", | ||
| "args": [ | ||
|
|
@@ -1318,6 +1327,28 @@ | |
| } | ||
| } | ||
| }, | ||
| { | ||
| "func": "get_tx_identity_info", | ||
| "args": [ | ||
| "NM_006087.3" | ||
| ], | ||
| "value": { | ||
| "psycopg2_dictrow": { | ||
| "tx_ac": "NM_006087.3", | ||
| "alt_ac": "NM_006087.3", | ||
| "alt_aln_method": "transcript", | ||
| "cds_start_i": 372, | ||
| "cds_end_i": 1707, | ||
| "lengths": [ | ||
| 429, | ||
| 109, | ||
| 111, | ||
| 1903 | ||
| ], | ||
| "hgnc": "TUBB4A" | ||
| } | ||
| } | ||
| }, | ||
| { | ||
| "func": "get_tx_identity_info", | ||
| "args": [ | ||
|
|
||
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.
Optional: hgvs already has
edit.ref_s("returns ref if it smells like a sequence"), which treats digit-only refs the same way. Reusing it (with whateverU/Nhandling we settle on) would keep a single definition of "stated reference" and less code to maintain. As it stands,_reference_basesandref_sdisagree on lowercase and RNA refs, which is part of what causes ther.mismatch noted above.