Skip to content

Commit

Permalink
Merge pull request #121 from paulsengroup/fix/bin-table-iters
Browse files Browse the repository at this point in the history
Fix bug in BinTable::make_iterable()
  • Loading branch information
robomics authored Nov 11, 2024
2 parents 6cc6b29 + 73e4cb4 commit 6b2f865
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/bin_table.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -360,7 +360,7 @@ nb::object BinTable::merge_coords(nb::object df) const {
nb::iterator BinTable::make_iterable() const {
return std::visit(
[](const auto& bins) {
return nb::make_iterator(nb::type<hictk::BinTable>(), "BinTableIterator", bins.begin(),
return nb::make_iterator(nb::type<hictkpy::BinTable>(), "BinTableIterator", bins.begin(),
bins.end());
},
_bins->get());
Expand Down
25 changes: 25 additions & 0 deletions test/test_bin_table.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
# SPDX-License-Identifier: MIT


import itertools

import pytest

import hictkpy
Expand Down Expand Up @@ -42,6 +44,8 @@ def test_accessors(self):
assert bins.type() == "fixed"
assert len(bins) == 15

assert str(bins).startswith("BinTable(")

def test_getters(self):
chroms = {"chr1": 1000, "chr2": 500}
bins = hictkpy.BinTable(chroms, 100)
Expand Down Expand Up @@ -102,3 +106,24 @@ def test_to_df(self):
assert len(bins.to_df("chr2\t0\t200", "BED")) == 2
with pytest.raises(RuntimeError):
bins.to_df("chr0")

def test_iters(self):
chroms = {"chr1": 1000, "chr2": 500}
bins = hictkpy.BinTable(chroms, 100)

expected_chroms = []
expected_starts = []
expected_ends = []

for chrom, size in chroms.items():
num_bins = (size + bins.resolution() - 1) // bins.resolution()
expected_chroms.extend([chrom] * num_bins)
starts = list(range(0, size, bins.resolution()))
ends = [min(pos + bins.resolution(), size) for pos in starts]
expected_starts.extend(starts)
expected_ends.extend(ends)

for chrom, start, end, bin in itertools.zip_longest(expected_chroms, expected_starts, expected_ends, bins):
assert bin.chrom == chrom
assert bin.start == start
assert bin.end == end

0 comments on commit 6b2f865

Please sign in to comment.