Skip to content

Commit

Permalink
Add tests for CigarHit.parse
Browse files Browse the repository at this point in the history
  • Loading branch information
Donaim committed Oct 11, 2024
1 parent d539187 commit 3a013b0
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 12 deletions.
18 changes: 7 additions & 11 deletions src/aligntools/cigar_hit.py
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,7 @@ def translate(self, reference_delta: int, query_delta: int) -> 'CigarHit':
q_ei=self.q_ei + query_delta)

@staticmethod
def parse_cigar_hit(string: str) -> 'CigarHit':
def parse(string: str) -> 'CigarHit':
"""
Parses a string representation of a CigarHit
and returns a CigarHit object.
Expand All @@ -357,16 +357,12 @@ def parse_cigar_hit(string: str) -> 'CigarHit':
if not match:
raise ex.ParseError(f"Invalid CigarHit string format: {string!r}.")

try:
# Extracting components from the matched regex groups
cigar_str = match.group('cigar')
q_st = int(match.group('q_st'))
q_ei = int(match.group('q_ei'))
r_st = int(match.group('r_st'))
r_ei = int(match.group('r_ei'))
except ValueError as e:
raise ex.ParseError(f"Error parsing indices in: {string!r}.") \
from e
# Extracting components from the matched regex groups
cigar_str = match.group('cigar')
q_st = int(match.group('q_st'))
q_ei = int(match.group('q_ei'))
r_st = int(match.group('r_st'))
r_ei = int(match.group('r_ei'))

# Validating that start indices
# are less than or equal to end indices.
Expand Down
2 changes: 1 addition & 1 deletion src/aligntools/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class MSALengthError(IndexError, CigarError):
pass


class InvalidOperationError(ValueError, CigarError):
class InvalidOperationError(ParseError, CigarError):
"""Exception raised for invalid operations within CIGAR strings."""
pass

Expand Down
32 changes: 32 additions & 0 deletions tests/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -1090,3 +1090,35 @@ def test_cigar_hit_serialization():
hit = parsed_hit("3M2I3D2M@1->1")
assert repr(hit) \
== "CigarHit(Cigar('3M2I3D2M'), r_st=1, r_ei=8, q_st=1, q_ei=7)"


@pytest.mark.parametrize(
"hit_str, expected",
[
("3M@[0,2]->[0,2]",
CigarHit(Cigar.coerce("3M"), 0, 2, 0, 2)),
("3M2I3D2M@[1,7]->[1,8]",
CigarHit(Cigar.coerce("3M2I3D2M"), 1, 8, 1, 7)),
("5M10I5D@[5,19]->[5,14]",
CigarHit(Cigar.coerce("5M10I5D"), 5, 14, 5, 19)),
]
)
def test_parse_cigar_hit_valid(hit_str, expected):
result = CigarHit.parse(hit_str)
assert result == expected


@pytest.mark.parametrize(
"hit_str",
[
"whatever",
"3K@[0,2]->[0,2]",
"3K@[a,b]->[c,d]",
"3K@[30,10]->[1,5]",
"3K@[3,10]->[20,5]",
"3K@[30,10]->[20,5]",
]
)
def test_parse_cigar_hit_invalid(hit_str):
with pytest.raises(ex.ParseError):
CigarHit.parse(hit_str)

0 comments on commit 3a013b0

Please sign in to comment.