From 38f8833f049683177e895d884210c58722579270 Mon Sep 17 00:00:00 2001 From: Vitaliy Mysak Date: Tue, 14 Nov 2023 11:33:19 -0800 Subject: [PATCH] Cigar tools: fix strips of empty queries --- micall/tests/test_cigar_tools.py | 29 +++++++++++++++++++++++++++++ micall/utils/cigar_tools.py | 4 ++-- 2 files changed, 31 insertions(+), 2 deletions(-) diff --git a/micall/tests/test_cigar_tools.py b/micall/tests/test_cigar_tools.py index 9378cb775..fd817c41c 100644 --- a/micall/tests/test_cigar_tools.py +++ b/micall/tests/test_cigar_tools.py @@ -341,6 +341,35 @@ def test_cigar_hit_strip_combines_with_connect(hit, cut_point): assert left.connect(right).coordinate_mapping == hit.coordinate_mapping +@pytest.mark.parametrize('hit', [x[0] for x in cigar_hit_ref_cut_cases]) +def test_cigar_hit_strip_never_crashes(hit): + hit.rstrip_query().lstrip_query() + hit.lstrip_query().rstrip_query() + hit.lstrip_query().lstrip_query() + hit.rstrip_query().rstrip_query() + + +@pytest.mark.parametrize('hit', [x[0] for x in cigar_hit_ref_cut_cases]) +def test_cigar_hit_strip_is_idempotent(hit): + h1 = hit.rstrip_query() + assert h1 == h1.rstrip_query() == h1.rstrip_query().rstrip_query() + + h1 = hit.lstrip_query() + assert h1 == h1.lstrip_query() == h1.lstrip_query().lstrip_query() + + h1 = hit.lstrip_query().rstrip_query() + assert h1 == h1.lstrip_query() == h1.rstrip_query() + + h1 = hit.rstrip_query().lstrip_query() + assert h1 == h1.rstrip_query() == h1.lstrip_query() + + +@pytest.mark.parametrize('hit', [x[0] for x in cigar_hit_ref_cut_cases]) +def test_cigar_hit_strips_are_commutative(hit): + assert hit.rstrip_query().lstrip_query() \ + == hit.lstrip_query().rstrip_query() + + @pytest.mark.parametrize('hit, cut_point', [(x[0], x[1]) for x in cigar_hit_ref_cut_cases if not isinstance(x[2], Exception) and not 'N' in str(x[0].cigar)]) diff --git a/micall/utils/cigar_tools.py b/micall/utils/cigar_tools.py index 9b44058aa..10527a64e 100644 --- a/micall/utils/cigar_tools.py +++ b/micall/utils/cigar_tools.py @@ -580,7 +580,7 @@ def cut_reference(self, cut_point: float) -> Tuple['CigarHit', 'CigarHit']: def lstrip_query(self) -> 'CigarHit': """ Return a copy of the CigarHit with leading (unmatched) query elements removed. """ - if self.query_length == 0: + if len(self.coordinate_mapping.ref_to_query) == 0: return self closest_ref = self.coordinate_mapping.ref_to_query.closest_key(self.r_st - 1) @@ -591,7 +591,7 @@ def lstrip_query(self) -> 'CigarHit': def rstrip_query(self) -> 'CigarHit': """ Return a copy of the CigarHit with trailing (unmatched) query elements removed. """ - if self.query_length == 0: + if len(self.coordinate_mapping.ref_to_query) == 0: return self closest_ref = self.coordinate_mapping.ref_to_query.closest_key(self.r_ei + 1)