Skip to content

Commit

Permalink
FIX: empty SeqView absolute/relative position returns 0
Browse files Browse the repository at this point in the history
  • Loading branch information
GavinHuttley committed Feb 13, 2024
1 parent ee3e458 commit 38ddeeb
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/cogent3/core/sequence.py
Original file line number Diff line number Diff line change
Expand Up @@ -2007,6 +2007,8 @@ def absolute_position(self, rel_index: int, include_boundary=False):
the absolute index with respect to the coordinates of the self
including offset
"""
if not self:
return 0

if rel_index < 0:
raise IndexError("only positive indexing supported!")
Expand All @@ -2029,6 +2031,8 @@ def relative_position(self, abs_index, stop=False):
NOTE: the returned value DOES NOT reflect python indexing. Importantly, negative values represent positions that
precede the current view.
"""
if not self:
return 0

if abs_index < 0:
raise IndexError("Index must be +ve and relative to the + strand")
Expand Down
6 changes: 6 additions & 0 deletions tests/test_core/test_sequence.py
Original file line number Diff line number Diff line change
Expand Up @@ -2714,3 +2714,9 @@ def test_sequences_propogates_seqid():
def test_make_seq_assigns_to_seqview():
seq = cogent3.make_seq("ACGT", name="s1")
assert seq.name == seq._seq.seqid == "s1"


def test_empty_seqview_translate_position():
sv = SeqView("")
assert sv.absolute_position(0) == 0
assert sv.relative_position(0) == 0

0 comments on commit 38ddeeb

Please sign in to comment.