Skip to content

Commit

Permalink
Cigar tools: prevent floating point errors in cut_reference
Browse files Browse the repository at this point in the history
  • Loading branch information
Donaim committed Nov 11, 2023
1 parent 8888bb7 commit c1318f6
Showing 1 changed file with 4 additions and 2 deletions.
6 changes: 4 additions & 2 deletions micall/utils/cigar_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from dataclasses import dataclass
from functools import cached_property
from itertools import chain, dropwhile
from fractions import Fraction

from micall.utils.consensus_aligner import CigarActions

Expand Down Expand Up @@ -429,7 +430,7 @@ def __add__(self, other):

@property
def epsilon(self):
return 1 / (self.query_length + self.ref_length + 100)
return Fraction(1, self.query_length + self.ref_length + 100)


def _slice(self, r_st, r_ei, q_st, q_ei) -> 'CigarHit':
Expand Down Expand Up @@ -479,7 +480,8 @@ def cut_reference(self, cut_point: float) -> 'CigarHit':
and that no element is lost.
"""

if float(cut_point).is_integer():
cut_point = Fraction(cut_point)
if cut_point.denominator == 1:
raise ValueError("Cut accepts fractions, not integers")

if self.ref_length == 0 or \
Expand Down

0 comments on commit c1318f6

Please sign in to comment.