From ffbde6b015ce7bc5a997dbbae2570446d608520c Mon Sep 17 00:00:00 2001 From: Anton Goloborodko Date: Mon, 25 Mar 2024 16:58:18 +0100 Subject: [PATCH] stats.py: fix recently introduced bugs --- pairtools/lib/stats.py | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/pairtools/lib/stats.py b/pairtools/lib/stats.py index 24ceb2d..e008314 100644 --- a/pairtools/lib/stats.py +++ b/pairtools/lib/stats.py @@ -614,6 +614,7 @@ def from_yaml(cls, file_handle, n_dist_bins_decade=N_DIST_BINS_DECADE_DEFAULT): stat_from_file._stat = stat return stat_from_file + def add_pair( self, chrom1, @@ -933,11 +934,6 @@ def flatten(self, filter="no_filter"): for dirs, freqs in v.items(): dist = self._dist_bins[i] - # in some previous versions of stats, last bin was not reported, so we need to skip it now: - if (i == len(self._dist_bins) - 1) and dist not in freqs: - flat_stat[formatted_key] = 0 - continue - # last bin is treated differently: "100000+" vs "1200-3000": if i < len(self._dist_bins) - 1: dist_next = self._dist_bins[i + 1] @@ -950,11 +946,16 @@ def flatten(self, filter="no_filter"): ).format(k, dist, dirs) else: raise ValueError("There is a mismatch between dist_freq bins in the instance") + # store key,value pair: try: flat_stat[formatted_key] = freqs[dist] except: - raise ValueError(f"Error in {k} {dirs} {dist} {dist_next} {freqs}: source and destination bins do not match") + # in some previous versions of stats, last bin was not reported, so we need to skip it now: + if (dist not in freqs) and (i == len(self._dist_bins) - 1): + flat_stat[formatted_key] = 0 + else: + raise ValueError(f"Error in {k} {dirs} {dist} {dist_next} {freqs}: source and destination bins do not match") elif (k in ["pair_types", "dedup", "chromsizes", 'summary']) and v: # 'pair_types' and 'dedup' are simple dicts inside,