Skip to content

Commit

Permalink
Closes #591: don't replace reference on out-of-bounds variation
Browse files Browse the repository at this point in the history
  • Loading branch information
reece committed Mar 25, 2020
1 parent e35a4ca commit 25a96d6
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 7 deletions.
25 changes: 18 additions & 7 deletions hgvs/variantmapper.py
Expand Up @@ -23,7 +23,7 @@
import hgvs.sequencevariant
import hgvs.validator

from hgvs.exceptions import HGVSUnsupportedOperationError, HGVSInvalidVariantError
from hgvs.exceptions import HGVSDataNotAvailableError, HGVSUnsupportedOperationError, HGVSInvalidVariantError
from hgvs.decorators.lru_cache import lru_cache
from hgvs.enums import PrevalidationLevel
from hgvs.utils.reftranscriptdata import RefTranscriptData
Expand Down Expand Up @@ -423,11 +423,8 @@ def _replace_reference(self, var):
if var.type not in "cgmnr":
raise HGVSUnsupportedOperationError("Can only update references for type c, g, m, n, r")

if var.posedit.edit.type == "ins":
# insertions have no reference sequence (zero-width), so return as-is
return var
if var.posedit.edit.type == "con":
# conversions have no reference sequence (zero-width), so return as-is
if var.posedit.edit.type in ("ins", "con"):
# these types have no reference sequence (zero-width), so return as-is
return var

pos = var.posedit.pos
Expand All @@ -443,7 +440,21 @@ def _replace_reference(self, var):
pos = mapper.c_to_n(var.posedit.pos)
else:
pos = var.posedit.pos
seq = self.hdp.get_seq(var.ac, pos.start.base - 1, pos.end.base)

seq_start = pos.start.base - 1
seq_end = pos.end.base

# When strict_bounds is False and an error occurs, return
# variant as-is

try:
seq = self.hdp.get_seq(var.ac, seq_start, seq_end)
except HGVSDataNotAvailableError as e:
if (seq_start < 0 or len(seq) != seq_end - seq_start):
assert not hgvs.global_config.mapping.strict_bounds, f"{var}: Got out of bounds variant with strict_bounds enabled"
assert var.type in "cnr", f"Should not see out of bounds variant on type {var.type}"
_logger.info(f"{var}: variant outside sequence bounds; reference sequence can't be validated")
return var

edit = var.posedit.edit
if edit.ref != seq:
Expand Down
17 changes: 17 additions & 0 deletions tests/issues/test_437.py
Expand Up @@ -117,6 +117,23 @@ def test_enforce_strict_bounds(self):
with pytest.raises(HGVSInvalidVariantError):
e2_g = self.am37.n_to_g(self.e2_n)



def test_invitae_examples(parser, am37):
tests = [
# ("NC_000009.11:g.35657741A>G", "NR_003051.3:n.*7T>C"), # Grammar doesn't support n.*
("NC_000009.11:g.35658020C>T", "NR_003051.3:n.-5G>A"),
]

hgvs.global_config.mapping.strict_bounds = False

for hgvs_g, hgvs_t in tests:
var_g = parser.parse(hgvs_g)
var_t = parser.parse(hgvs_t)
assert hgvs_t == str(am37.g_to_t(var_g, var_t.ac))

hgvs.global_config.mapping.strict_bounds = True



def test_oob_dup(parser, am37):
Expand Down

0 comments on commit 25a96d6

Please sign in to comment.