From fab2aa02842145b411d73c1957674b6c759f2b1a Mon Sep 17 00:00:00 2001 From: Roberto Rossini <71787608+robomics@users.noreply.github.com> Date: Mon, 13 Jan 2025 13:37:37 +0100 Subject: [PATCH] Update tests --- test/integration/common.py | 54 ++++++++++++++++++++++++++ test/integration/test_stripepy_call.py | 19 +++++---- 2 files changed, 66 insertions(+), 7 deletions(-) diff --git a/test/integration/common.py b/test/integration/common.py index 4a5baa1..078e82a 100644 --- a/test/integration/common.py +++ b/test/integration/common.py @@ -3,6 +3,14 @@ # SPDX-License-Identifier: MIT +import pathlib +from typing import Sequence + +from pandas.testing import assert_frame_equal + +from stripepy.IO import ResultFile + + def matplotlib_avail() -> bool: try: import matplotlib @@ -10,3 +18,49 @@ def matplotlib_avail() -> bool: return False return True + + +def _compare_attributes(f1: ResultFile, f2: ResultFile): + assert f1.assembly == f2.assembly + assert f1.resolution == f2.resolution + assert f1.format == f2.format + assert f1.normalization == f2.normalization + assert f1.chromosomes == f2.chromosomes + + metadata1 = f1.metadata + metadata2 = f2.metadata + + metadata1.pop("min-chromosome-size") + metadata2.pop("min-chromosome-size") + + assert metadata1 == metadata2 + + +def _compare_field(f1: ResultFile, f2: ResultFile, chrom: str, field: str, location: str): + assert chrom in f1.chromosomes + df1 = f1.get(chrom, field, location) + df2 = f2.get(chrom, field, location) + + assert_frame_equal(df1, df2, check_exact=False) + + +def _compare_result(f1: ResultFile, f2: ResultFile, chrom: str, location: str): + fields = ( + "pseudodistribution", + "all_minimum_points", + "persistence_of_all_minimum_points", + "all_maximum_points", + "persistence_of_all_maximum_points", + "stripes", + ) + + for field in fields: + _compare_field(f1, f2, chrom, field, location) + + +def compare_result_files(reference: pathlib.Path, found: pathlib.Path, chroms: Sequence[str]): + with ResultFile(reference) as f1, ResultFile(found) as f2: + _compare_attributes(f1, f2) + for chrom in chroms: + _compare_result(f1, f2, chrom, "LT") + _compare_result(f1, f2, chrom, "UT") diff --git a/test/integration/test_stripepy_call.py b/test/integration/test_stripepy_call.py index b915d9f..3162c69 100644 --- a/test/integration/test_stripepy_call.py +++ b/test/integration/test_stripepy_call.py @@ -5,13 +5,12 @@ import pathlib import warnings -import h5py import hictkpy import pytest from stripepy import main -from .common import matplotlib_avail +from .common import compare_result_files, matplotlib_avail testdir = pathlib.Path(__file__).resolve().parent.parent @@ -22,6 +21,7 @@ class TestStripePyCall: def setup_class(): test_files = [ testdir / "data" / "4DNFI9GMP2J8.mcool", + testdir / "data" / "results_4DNFI9GMP2J8_v2.hdf5", ] for f in test_files: @@ -33,6 +33,7 @@ def setup_class(): @staticmethod def test_stripepy_call(tmpdir): testfile = testdir / "data" / "4DNFI9GMP2J8.mcool" + result_file = testdir / "data" / "results_4DNFI9GMP2J8_v2.hdf5" resolution = 10_000 chrom_sizes = hictkpy.MultiResFile(testfile).chromosomes() @@ -43,7 +44,7 @@ def test_stripepy_call(tmpdir): str(testfile), str(resolution), "--glob-pers-min", - "0.10", + "0.05", "--loc-pers-min", "0.33", "--loc-trend-min", @@ -58,21 +59,25 @@ def test_stripepy_call(tmpdir): outfile = pathlib.Path(tmpdir) / testfile.stem / str(resolution) / "results.hdf5" assert outfile.is_file() - assert h5py.File(outfile).attrs.get("format", "unknown") == "HDF5::StripePy" + compare_result_files( + result_file, outfile, [chrom for chrom, size in chrom_sizes.items() if size >= chrom_size_cutoff] + ) @staticmethod def test_stripepy_call_with_roi(tmpdir): testfile = testdir / "data" / "4DNFI9GMP2J8.mcool" + result_file = testdir / "data" / "results_4DNFI9GMP2J8_v2.hdf5" resolution = 10_000 - chrom_size_cutoff = max(hictkpy.MultiResFile(testfile).chromosomes().values()) - 1 + chrom_sizes = hictkpy.MultiResFile(testfile).chromosomes() + chrom_size_cutoff = max(chrom_sizes.values()) - 1 args = [ "call", str(testfile), str(resolution), "--glob-pers-min", - "0.10", + "0.05", "--loc-pers-min", "0.33", "--loc-trend-min", @@ -96,4 +101,4 @@ def test_stripepy_call_with_roi(tmpdir): outfile = pathlib.Path(tmpdir) / testfile.stem / str(resolution) / "results.hdf5" assert outfile.is_file() - assert h5py.File(outfile).attrs.get("format", "unknown") == "HDF5::StripePy" + compare_result_files(result_file, outfile, [tuple(chrom_sizes.keys())[0]])