From c1318f649663c331bff62be580c5c31afc8db253 Mon Sep 17 00:00:00 2001 From: Vitaliy Mysak Date: Sat, 11 Nov 2023 12:16:40 -0800 Subject: [PATCH] Cigar tools: prevent floating point errors in cut_reference --- micall/utils/cigar_tools.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/micall/utils/cigar_tools.py b/micall/utils/cigar_tools.py index 36b829262..92a45e4d8 100644 --- a/micall/utils/cigar_tools.py +++ b/micall/utils/cigar_tools.py @@ -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 @@ -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': @@ -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 \