Skip to content

Commit

Permalink
Cigar tools: fix gaps tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Donaim committed Nov 13, 2023
1 parent 2ea41b8 commit 712ef3b
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 8 deletions.
20 changes: 14 additions & 6 deletions micall/tests/test_cigar_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@
from micall.utils.cigar_tools import Cigar, CigarHit, parse_cigar_operation, CIGAR_OP_MAPPING


cigar_mapping_cases: List[Tuple[Cigar, 'mapping', 'closest_mapping']] = [
cigar_mapping_cases = [
# Simple cases
('3M', {0: 0, 1: 1, 2: 2},
{0: 0, 1: 1, 2: 2}),
('1M1D1M', {0: 0, 2: 1},
{0: 0, 1: 0, 2: 1}),
('3M', {0: 0, 1: 1, 2: 2}, # exact mapping
{0: 0, 1: 1, 2: 2}), # closest mapping
('1M1D1M', {0: 0, 2: 1}, # exact mapping
{0: 0, 1: 0, 2: 1}), # closest mapping
('1M1I1M', {0: 0, 1: 2},
{0: 0, 1: 2}),
('2M2D2M', {0: 0, 1: 1, 4: 2, 5: 3},
Expand Down Expand Up @@ -221,6 +221,14 @@ def test_invalid_cigar_string():
[CigarHit('9M1I', r_st=1, r_ei=9, q_st=1, q_ei=10),
CigarHit('8I9M', r_st=10, r_ei=18, q_st=11, q_ei=27)]),

(CigarHit('9M9D9I9M', r_st=1, r_ei=27, q_st=1, q_ei=27), 13.5 or 27/2,
[CigarHit('9M4D', r_st=1, r_ei=13, q_st=1, q_ei=9),
CigarHit('5D9I9M', r_st=14, r_ei=27, q_st=10, q_ei=27)]),

(CigarHit('9M9I9D9M', r_st=1, r_ei=27, q_st=1, q_ei=27), 13.5 or 27/2,
[CigarHit('9M9I4D', r_st=1, r_ei=13, q_st=1, q_ei=18),
CigarHit('5D9M', r_st=14, r_ei=27, q_st=19, q_ei=27)]),

# Edge cases
(CigarHit('9M9I9M', r_st=1, r_ei=18, q_st=1, q_ei=27), 9.5, # no middlepoint
[CigarHit('9M5I', r_st=1, r_ei=9, q_st=1, q_ei=14),
Expand Down Expand Up @@ -331,7 +339,7 @@ def test_cigar_hit_strip_combines_with_add(hit, cut_point):
left = left.rstrip_query()
right = right.lstrip_query()

assert left + right == hit
assert (left + right).coordinate_mapping == hit.coordinate_mapping


@pytest.mark.parametrize('hit, cut_point', [(x[0], x[1]) for x in cigar_hit_ref_cut_cases
Expand Down
9 changes: 7 additions & 2 deletions micall/utils/cigar_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ def translate(self, domain_delta: int, codomain_delta: int) -> 'PartialDict':
return ret


@dataclass
class CoordinateMapping:
def __init__(self):
self.query_to_ref = PartialDict()
Expand Down Expand Up @@ -125,6 +126,10 @@ def translate(self, reference_delta: int, query_delta: int) -> 'CoordinateMappin
return ret


def __repr__(self):
return f'CoordinateMapping({self.ref_to_op},{self.query_to_op})'


class Cigar(list):
"""
A CIGAR string represents a read alignment against a reference sequence.
Expand Down Expand Up @@ -379,8 +384,8 @@ def gaps(self) -> Iterable['CigarHit']:

def make_gap(r_st, r_en):
r_ei = r_en - 1
left, midright = self.cut_reference(r_st - 0.5)
middle, right = midright.cut_reference(r_ei + 0.5)
left, midright = self.cut_reference(r_st - self.epsilon)
middle, right = midright.cut_reference(r_ei + self.epsilon)
return middle

gap_start = None
Expand Down

0 comments on commit 712ef3b

Please sign in to comment.