Skip to content

Commit

Permalink
Deduplicate CigarActions code
Browse files Browse the repository at this point in the history
  • Loading branch information
Donaim committed Sep 27, 2024
1 parent e9ba36a commit 1673d7d
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 39 deletions.
2 changes: 1 addition & 1 deletion src/aligntools/cigar_actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class CigarActions(IntEnum):
@staticmethod
def parse(value: str) -> 'CigarActions':
ret = OP_MAPPING.get(value)
if not ret:
if ret is None:
raise ex.ParseError(f"Invalid action: {value}")
return ret

Expand Down
42 changes: 4 additions & 38 deletions src/aligntools/lib.py
Original file line number Diff line number Diff line change
Expand Up @@ -325,47 +325,14 @@ def ref_length(self):
# Boring boilerplate code below #
# #

OP_MAPPING = {
# Alignment match (can be a sequence match or mismatch)
'M': CigarActions.MATCH,

# Insertion to the reference
'I': CigarActions.INSERT,

# Deletion from the reference
'D': CigarActions.DELETE,

# Skipped region from the reference
'N': CigarActions.SKIPPED,

# Soft clip on the read (not aligned but present in the read)
'S': CigarActions.SOFT_CLIPPED,

# Hard clip on the read (not present in the read)
'H': CigarActions.HARD_CLIPPED,

# Padding (silent deletion from padded reference)
'P': CigarActions.PADDING,

# Sequence match
'=': CigarActions.SEQ_MATCH,

# Sequence mismatch
'X': CigarActions.MISMATCH,
}

@staticmethod
def parse_operation(operation: str) -> CigarActions:
if operation in Cigar.OP_MAPPING:
return Cigar.OP_MAPPING[operation]
else:
try:
return CigarActions.parse(operation)
except ex.ParseError:
raise ex.InvalidOperationError(
f"Unexpected CIGAR action: {operation!r}.")

@staticmethod
def operation_to_str(op: CigarActions) -> str:
return [k for (k, v) in Cigar.OP_MAPPING.items() if v == op][0]

@staticmethod
def parse(string: str) -> 'Cigar':
"""
Expand Down Expand Up @@ -450,8 +417,7 @@ def __repr__(self):

def __str__(self):
""" Inverse of `Cigar.parse` """
return ''.join('{}{}'.format(num, Cigar.operation_to_str(op))
for num, op in self._data)
return ''.join('{}{}'.format(num, op) for num, op in self._data)


def intervals_overlap(x: Tuple[int, int], y: Tuple[int, int]) -> bool:
Expand Down

0 comments on commit 1673d7d

Please sign in to comment.