From 22705e25210a93bace2a010307676cc34dbe1ad9 Mon Sep 17 00:00:00 2001 From: Brendan ODonnell Date: Tue, 2 Jan 2024 14:22:03 -0500 Subject: [PATCH] Remove new getattr references --- src/hgvs/assemblymapper.py | 33 +++++++++++++++++---------------- src/hgvs/variantmapper.py | 19 ++++++++++++------- 2 files changed, 29 insertions(+), 23 deletions(-) diff --git a/src/hgvs/assemblymapper.py b/src/hgvs/assemblymapper.py index 438057de..d9896eb8 100644 --- a/src/hgvs/assemblymapper.py +++ b/src/hgvs/assemblymapper.py @@ -244,22 +244,23 @@ def _alt_ac_for_tx_ac(self, tx_ac): return alt_acs[0] def _replace_reference(self, var): - if (getattr(var.posedit.pos.start, 'offset', 0) != 0 or getattr(var.posedit.pos.end, 'offset', 0) != 0): - if var.type != 'g': - alt_ac = self._alt_ac_for_tx_ac(var.ac) - if var.type == 'c': - var_g = super(AssemblyMapper, self).c_to_g(var, alt_ac, alt_aln_method=self.alt_aln_method) - if var.type == 'n': - var_g = super(AssemblyMapper, self).n_to_g(var, alt_ac, alt_aln_method=self.alt_aln_method) - if var.type == 't': - var_g = super(AssemblyMapper, self).t_to_g(var, alt_ac, alt_aln_method=self.alt_aln_method) - super(AssemblyMapper, self)._replace_reference(var_g) - ref = var_g.posedit.edit.ref - if self._fetch_AlignmentMapper(tx_ac=var.ac).strand == -1: - ref = reverse_complement(ref) - var.posedit.edit.ref = ref - _logger.debug("Replaced reference sequence in {var} with {ref}".format(var=var, ref=var_g.posedit.edit.ref)) - return var + if ( + var.type in "cn" + and var.posedit.pos is not None + and (var.posedit.pos.start.offset != 0 or var.posedit.pos.end.offset != 0) + ): + alt_ac = self._alt_ac_for_tx_ac(var.ac) + if var.type == 'c': + var_g = super(AssemblyMapper, self).c_to_g(var, alt_ac, alt_aln_method=self.alt_aln_method) + if var.type == 'n': + var_g = super(AssemblyMapper, self).n_to_g(var, alt_ac, alt_aln_method=self.alt_aln_method) + super(AssemblyMapper, self)._replace_reference(var_g) + ref = var_g.posedit.edit.ref + if self._fetch_AlignmentMapper(tx_ac=var.ac).strand == -1: + ref = reverse_complement(ref) + var.posedit.edit.ref = ref + _logger.debug("Replaced reference sequence in {var} with {ref}".format(var=var, ref=var_g.posedit.edit.ref)) + return var return super(AssemblyMapper, self)._replace_reference(var) diff --git a/src/hgvs/variantmapper.py b/src/hgvs/variantmapper.py index eb748c51..be0c4d12 100644 --- a/src/hgvs/variantmapper.py +++ b/src/hgvs/variantmapper.py @@ -468,11 +468,13 @@ def _replace_reference(self, var): # these types have no reference sequence (zero-width), so return as-is return var - pos = var.posedit.pos - if (getattr(pos.start, 'offset', 0) != 0 or getattr(pos.end, 'offset', 0) != 0): - if var.type != "g": - _logger.info("Can't update reference sequence for intronic variant {}".format(var)) - return var + if ( + var.type in "cnr" + and var.posedit.pos is not None + and (var.posedit.pos.start.offset != 0 or var.posedit.pos.end.offset != 0) + ): + _logger.info("Can't update reference sequence for intronic variant {}".format(var)) + return var # For c. variants, we need coords on underlying sequences if var.type == "c": @@ -483,8 +485,11 @@ def _replace_reference(self, var): else: pos = var.posedit.pos - seq_start = pos.start.base + getattr(pos.start, 'offset', 0) - 1 - seq_end = pos.end.base + getattr(pos.end, 'offset', 0) + seq_start = pos.start.base - 1 + seq_end = pos.end.base + if var.type in "cnr": + seq_start += pos.start.offset + seq_end += pos.end.offset # When strict_bounds is False and an error occurs, return # variant as-is