From 3a013b0d3adcf889aaff772f80065afa2e91e7b1 Mon Sep 17 00:00:00 2001 From: Vitaliy Mysak Date: Fri, 11 Oct 2024 14:56:17 -0700 Subject: [PATCH] Add tests for CigarHit.parse --- src/aligntools/cigar_hit.py | 18 +++++++----------- src/aligntools/exceptions.py | 2 +- tests/test_main.py | 32 ++++++++++++++++++++++++++++++++ 3 files changed, 40 insertions(+), 12 deletions(-) diff --git a/src/aligntools/cigar_hit.py b/src/aligntools/cigar_hit.py index 1577870..370db93 100644 --- a/src/aligntools/cigar_hit.py +++ b/src/aligntools/cigar_hit.py @@ -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. @@ -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. diff --git a/src/aligntools/exceptions.py b/src/aligntools/exceptions.py index dad3443..90a975e 100644 --- a/src/aligntools/exceptions.py +++ b/src/aligntools/exceptions.py @@ -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 diff --git a/tests/test_main.py b/tests/test_main.py index 8359d7d..5cb3717 100644 --- a/tests/test_main.py +++ b/tests/test_main.py @@ -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)