diff --git a/src/file.cpp b/src/file.cpp index e199955..8606de7 100644 --- a/src/file.cpp +++ b/src/file.cpp @@ -190,7 +190,7 @@ static nb::dict get_hic_attrs(const hictk::hic::File &hf) { py_attrs["bin_size"] = hf.resolution(); py_attrs["format"] = "HIC"; - py_attrs["format_version"] = hf.version(); + py_attrs["format-version"] = hf.version(); py_attrs["assembly"] = hf.assembly(); py_attrs["format-url"] = "https://github.com/aidenlab/hic-format"; py_attrs["nbins"] = hf.bins().size(); @@ -301,7 +301,8 @@ void declare_file_class(nb::module_ &m) { file.def("resolution", &hictk::File::resolution, "Get the bin size in bp."); file.def("nbins", &hictk::File::nbins, "Get the total number of bins."); - file.def("nchroms", &hictk::File::nchroms, "Get the total number of chromosomes."); + file.def("nchroms", &hictk::File::nchroms, nb::arg("include_ALL") = false, + "Get the total number of chromosomes."); file.def("attributes", &file::attributes, "Get file attributes as a dictionary.", nb::rv_policy::take_ownership); diff --git a/test/test_bin_table.py b/test/test_bin_table.py index 62ba0bc..8e05425 100644 --- a/test/test_bin_table.py +++ b/test/test_bin_table.py @@ -57,17 +57,16 @@ def test_getters(self): bins.get_id("abc", 100) @pytest.mark.skipif( - not numpy_avail() or not pandas_avail() or not pyarrow_avail(), - reason="numpy, pandas, or pyarrow are not available", + not pandas_avail() or not pyarrow_avail(), + reason="pandas or pyarrow are not available", ) def test_vectorized_getters(self): - import numpy as np chroms = {"chr1": 1000, "chr2": 500} bins = hictkpy.BinTable(chroms, 100) - assert len(bins.get(np.array([1, 1]))) == 2 - assert len(bins.get_ids(np.array(["chr1", "chr1"]), np.array([1, 1]))) == 2 + assert len(bins.get([1, 1])) == 2 + assert len(bins.get_ids(["chr1", "chr1"], [1, 1])) == 2 @pytest.mark.skipif(not pandas_avail() or not pyarrow_avail(), reason="pandas is not available") def test_merge(self): diff --git a/test/test_file_accessors.py b/test/test_file_accessors.py index 923ea16..ee621ce 100644 --- a/test/test_file_accessors.py +++ b/test/test_file_accessors.py @@ -27,7 +27,7 @@ class TestClass: def test_attributes(self, file, resolution): f = hictkpy.File(file, resolution) assert f.resolution() == 100_000 - # assert f.nchroms() == 8 # TODO enable after merging https://github.com/paulsengroup/hictk/pull/294 + assert f.nchroms() == 8 assert f.nbins() == 1380 assert "chr2L" in f.chromosomes() diff --git a/test/test_file_creation_cool.py b/test/test_file_creation_cool.py index 5d2ebec..172df5b 100644 --- a/test/test_file_creation_cool.py +++ b/test/test_file_creation_cool.py @@ -31,6 +31,20 @@ def setup_method(): logging.basicConfig(level="INFO", force=True) logging.getLogger().setLevel("INFO") + def test_accessors(self, file, resolution, tmpdir): + bins = hictkpy.File(file, resolution).bins() + + path = tmpdir / "test.cool" + w = hictkpy.cooler.FileWriter(path, bins) + + assert str(w).startswith("CoolFileWriter(") + assert w.path() == path + if resolution is None: + assert w.resolution() == 0 + else: + assert w.resolution() == resolution + assert w.chromosomes() == bins.chromosomes() + def test_file_creation_thin_pixel(self, file, resolution, tmpdir): f = hictkpy.File(file, resolution) if f.bins().type() != "fixed": @@ -39,7 +53,7 @@ def test_file_creation_thin_pixel(self, file, resolution, tmpdir): df = f.fetch(join=False).to_df() expected_sum = df["count"].sum() - path = tmpdir / "test1.cool" + path = tmpdir / "test.cool" w = hictkpy.cooler.FileWriter(path, f.chromosomes(), f.resolution()) chunk_size = 1000 @@ -67,7 +81,7 @@ def test_file_creation(self, file, resolution, tmpdir): df = f.fetch(join=True).to_df() expected_sum = df["count"].sum() - path = tmpdir / "test2.cool" + path = tmpdir / "test.cool" w = hictkpy.cooler.FileWriter(path, f.chromosomes(), f.resolution()) chunk_size = 1000 @@ -93,7 +107,7 @@ def test_file_creation_bin_table(self, file, resolution, tmpdir): df = f.fetch(join=True).to_df() expected_sum = df["count"].sum() - path = tmpdir / "test2.cool" + path = tmpdir / "test.cool" w = hictkpy.cooler.FileWriter(path, f.bins()) chunk_size = 1000 @@ -122,7 +136,7 @@ def test_file_creation_float_counts(self, file, resolution, tmpdir): df["count"] += 0.12345 expected_sum = df["count"].sum() - path = tmpdir / "test3.cool" + path = tmpdir / "test.cool" w = hictkpy.cooler.FileWriter(path, f.chromosomes(), f.resolution()) chunk_size = 1000 diff --git a/test/test_file_validators.py b/test/test_file_validators.py index a1ad54e..aa6a091 100644 --- a/test/test_file_validators.py +++ b/test/test_file_validators.py @@ -4,8 +4,6 @@ import pathlib -import pytest - import hictkpy testdir = pathlib.Path(__file__).resolve().parent @@ -17,7 +15,7 @@ class TestClass: - def test_validators(self): + def test_valid_formats(self): assert hictkpy.is_cooler(cool_file) assert not hictkpy.is_cooler(hic_file) @@ -26,3 +24,31 @@ def test_validators(self): assert hictkpy.is_scool_file(scool_file) assert not hictkpy.is_scool_file(cool_file) + + assert hictkpy.is_hic(hic_file) + assert not hictkpy.is_hic(cool_file) + + def test_invalid_formats(self): + path = pathlib.Path(__file__).resolve() + + assert not hictkpy.is_cooler(path) + assert not hictkpy.is_mcool_file(path) + assert not hictkpy.is_scool_file(path) + assert not hictkpy.is_hic(path) + + def test_invalid_files(self): + non_existing_file = testdir / "foobar.123" + assert not non_existing_file.exists() + + assert not hictkpy.is_cooler(non_existing_file) + assert not hictkpy.is_mcool_file(non_existing_file) + assert not hictkpy.is_scool_file(non_existing_file) + assert not hictkpy.is_hic(non_existing_file) + + folder = testdir + assert folder.is_dir() + + assert not hictkpy.is_cooler(folder) + assert not hictkpy.is_mcool_file(folder) + assert not hictkpy.is_scool_file(folder) + assert not hictkpy.is_hic(folder) diff --git a/test/test_singlecell_file_accessors.py b/test/test_singlecell_file_accessors.py index f48f1c4..aa7a1d9 100644 --- a/test/test_singlecell_file_accessors.py +++ b/test/test_singlecell_file_accessors.py @@ -19,13 +19,19 @@ class TestClass: - def test_attributes(self, file): + def test_accessors(self, file): f = hictkpy.cooler.SingleCellFile(file) + assert str(f).startswith("SingleCellFile(") + assert f.path() == file assert f.resolution() == 100_000 assert len(f.chromosomes()) == 20 + assert len(f.bins()) == 26398 assert len(f.cells()) == 5 assert f.attributes()["format"] == "HDF5::SCOOL" assert f["GSM2687248_41669_ACAGTG-R1-DpnII.100000.cool"].resolution() == 100_000 + + with pytest.raises(Exception): + f["ABC"] # noqa