From ca5d9d06fdc74ecb996f546a18ec6abc742672d8 Mon Sep 17 00:00:00 2001 From: OfficialBishal Date: Thu, 25 Jun 2026 03:21:38 -0400 Subject: [PATCH] fix: use chromsizes from the pairs header in scaling compute_scaling() read the .pairs header chromsizes via read_pairs() but discarded them, so running scaling without --view left region ends at -1 and produced an empty n_bp2 column. Use the header chromsizes when the caller does not pass any, and add a test. Closes #282 --- pairtools/lib/scaling.py | 6 +++++- tests/test_scaling.py | 13 +++++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/pairtools/lib/scaling.py b/pairtools/lib/scaling.py index 772c163e..7746c43a 100644 --- a/pairtools/lib/scaling.py +++ b/pairtools/lib/scaling.py @@ -389,7 +389,11 @@ def compute_scaling( pairs_df = pairs elif isinstance(pairs, str) or hasattr(pairs, "buffer") or hasattr(pairs, "peek"): - pairs_df, _, _ = pairsio.read_pairs(pairs, nproc=nproc_in, chunksize=chunksize) + pairs_df, _, header_chromsizes = pairsio.read_pairs( + pairs, nproc=nproc_in, chunksize=chunksize + ) + if chromsizes is None and header_chromsizes is not None and len(header_chromsizes): + chromsizes = header_chromsizes else: raise ValueError( "pairs must be either a path to a pairs file or a pd.DataFrame" diff --git a/tests/test_scaling.py b/tests/test_scaling.py index 21046004..225ba832 100644 --- a/tests/test_scaling.py +++ b/tests/test_scaling.py @@ -25,3 +25,16 @@ def test_scaling(): assert ( output["n_pairs"].sum() == 9 ) # double unmapped pairs are currently ignored by lib.scaling + + +def test_scaling_uses_header_chromsizes(): + # Without an explicit view, the implicit per-chromosome view must use the chromsizes + # from the .pairs header (chr* = 100 in mock.pairsam); otherwise region ends stay at + # -1 and n_bp2 is left empty. + from pairtools.lib.scaling import compute_scaling + + mock_pairsam_path = os.path.join(testdir, "data", "mock.pairsam") + cis, _trans = compute_scaling(mock_pairsam_path, regions=None) + + assert (cis["end1"] == 100).all() + assert (cis["n_bp2"] > 0).any()