Skip to content

Commit

Permalink
Cigar tools: make CigarHit immutable
Browse files Browse the repository at this point in the history
  • Loading branch information
Donaim committed Dec 4, 2023
1 parent d031a5e commit 4fc31e3
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 6 deletions.
2 changes: 1 addition & 1 deletion micall/core/contig_stitcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ def align_to_reference(contig) -> Iterable[GenotypedContig]:
yield contig
return

hits_array = [CigarHit(x.cigar, x.r_st, x.r_en - 1, x.q_st, x.q_en - 1) for x in alignments]
hits_array = [CigarHit(Cigar.coerce(x.cigar), x.r_st, x.r_en - 1, x.q_st, x.q_en - 1) for x in alignments]
connected = connect_cigar_hits(hits_array)

logger.info("Contig %r aligned in %s parts.", contig.name, len(connected),
Expand Down
8 changes: 3 additions & 5 deletions micall/utils/cigar_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

from math import ceil, floor
import re
from typing import Container, Tuple, Iterable, Optional, Set, Dict, List
from typing import Container, Tuple, Iterable, Optional, Set, Dict, List, Union
from dataclasses import dataclass
from functools import cached_property, reduce
from itertools import chain, dropwhile
Expand Down Expand Up @@ -152,7 +152,7 @@ def __init__(self, data) -> None:


@staticmethod
def coerce(obj):
def coerce(obj: Union['Cigar', str, Iterable[Tuple[int, CigarActions]]]):
if isinstance(obj, Cigar):
return obj

Expand Down Expand Up @@ -437,7 +437,7 @@ def __str__(self):
return ''.join('{}{}'.format(num, Cigar.operation_to_str(op)) for num, op in self._data)


@dataclass
@dataclass(frozen=True)
class CigarHit:
"""
This class provides an abstraction over the complex details involved in working with sequence alignments
Expand All @@ -461,8 +461,6 @@ class CigarHit:


def __post_init__(self):
self.cigar = Cigar.coerce(self.cigar)

if self.ref_length != self.cigar.ref_length:
raise ValueError(f"CIGAR string maps {self.cigar.ref_length}"
f" reference positions, but CIGAR hit range is {self.ref_length}")
Expand Down

0 comments on commit 4fc31e3

Please sign in to comment.