Skip to content

Commit

Permalink
issue biocommons#606 - move seq update block out of method to reduce …
Browse files Browse the repository at this point in the history
…complexity
  • Loading branch information
kayleeyuhas committed Dec 23, 2020
1 parent a84ba97 commit 17e8ed6
Showing 1 changed file with 21 additions and 18 deletions.
39 changes: 21 additions & 18 deletions hgvs/variantmapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -538,6 +538,26 @@ def _convert_edit_check_strand(strand, edit_in):
raise NotImplementedError("Only NARefAlt/Dup/Inv types are currently implemented")
return edit_out

def update_sequence(self, seq, pos_start, pos_end, edit):
if edit.type == 'sub':
seq[pos_start] = edit.alt
elif edit.type == 'del':
del seq[pos_start:pos_end]
elif edit.type == 'ins':
seq.insert(pos_start + 1, edit.alt)
elif edit.type == 'delins':
del seq[pos_start:pos_end]
seq.insert(pos_start, edit.alt)
elif edit.type == 'dup':
seq.insert(pos_end, ''.join(seq[pos_start:pos_end]))
elif edit.type == 'inv':
seq[pos_start:pos_end] = list(reverse_complement(''.join(seq[pos_start:pos_end])))
elif edit.type == 'identity':
pass
else:
raise HGVSUnsupportedOperationError(
"Getting altered sequence for {type} is unsupported".format(type=edit.type))

def _get_altered_sequence(self, strand, interval, var):
seq = list(self.hdp.get_seq(var.ac, interval.start.base - 1, interval.end.base))
# positions are 0-based and half-open
Expand All @@ -546,24 +566,7 @@ def _get_altered_sequence(self, strand, interval, var):
edit = var.posedit.edit

try:
if edit.type == 'sub':
seq[pos_start] = edit.alt
elif edit.type == 'del':
del seq[pos_start:pos_end]
elif edit.type == 'ins':
seq.insert(pos_start + 1, edit.alt)
elif edit.type == 'delins':
del seq[pos_start:pos_end]
seq.insert(pos_start, edit.alt)
elif edit.type == 'dup':
seq.insert(pos_end, ''.join(seq[pos_start:pos_end]))
elif edit.type == 'inv':
seq[pos_start:pos_end] = list(reverse_complement(''.join(seq[pos_start:pos_end])))
elif edit.type == 'identity':
pass
else:
raise HGVSUnsupportedOperationError(
"Getting altered sequence for {type} is unsupported".format(type=edit.type))
self.update_sequence(seq, pos_start, pos_end, edit)
except IndexError:
raise HGVSInvalidIntervalError("Specified index does not exist within sequence.")

Expand Down

0 comments on commit 17e8ed6

Please sign in to comment.