Skip to content

Commit

Permalink
Introduce the concept of relaxation
Browse files Browse the repository at this point in the history
  • Loading branch information
Donaim committed Oct 25, 2024
1 parent 95e8361 commit 28c6d87
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 3 deletions.
13 changes: 11 additions & 2 deletions src/aligntools/cigar.py
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,15 @@ def coordinate_mapping(self) -> CoordinateMapping:

return mapping

def relax(self) -> 'Cigar':
"""
Turn `CigarActions.SEQ_MATCH` and `CigarActions.MISMATCH`
into `CigarActions.MATCH`.
"""

new = [(count, action.relax()) for (count, action) in self._data]
return Cigar(new)

def to_msa(self, reference_seq: str, query_seq: str) -> Tuple[str, str]:
"""
Constructs a multiple sequence alignment (MSA) representation
Expand Down Expand Up @@ -283,9 +292,9 @@ def from_msa(reference: str, query: str) -> 'Cigar':
elif query_base == '-':
op = CigarActions.DELETE
elif ref_base == query_base:
op = CigarActions.MATCH
op = CigarActions.SEQ_MATCH
else:
op = CigarActions.MATCH
op = CigarActions.MISMATCH

operations.append((1, op))

Expand Down
8 changes: 8 additions & 0 deletions src/aligntools/cigar_actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,14 @@ def parse(value: str) -> 'CigarActions':
def __str__(self) -> str:
return OP_MAPPING_REV[self]

def relax(self) -> 'CigarActions':
if self == CigarActions.SEQ_MATCH:
return CigarActions.MATCH
elif self == CigarActions.MISMATCH:
return CigarActions.MATCH
else:
return self


OP_MAPPING = {
'M': CigarActions.MATCH,
Expand Down
2 changes: 1 addition & 1 deletion tests/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -757,7 +757,7 @@ def test_illigal_cigar_to_msa(cigar, reference_seq, query_seq):
]
)
def test_from_msa(reference, query, expected_cigar_str):
cigar = Cigar.from_msa(reference, query)
cigar = Cigar.from_msa(reference, query).relax()
assert str(cigar) == expected_cigar_str


Expand Down

0 comments on commit 28c6d87

Please sign in to comment.