Skip to content

Commit

Permalink
Update hictk merge to support coolers with variable bin size
Browse files Browse the repository at this point in the history
  • Loading branch information
robomics committed Dec 8, 2023
1 parent a003682 commit 8017d56
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 4 deletions.
9 changes: 7 additions & 2 deletions src/libhictk/bin_table/include/hictk/impl/bin_table_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -172,8 +172,13 @@ inline std::uint64_t BinTable::map_to_bin_id(std::uint32_t chrom_id, std::uint32
inline bool BinTable::operator==(const BinTable &other) const {
return std::visit(
[&](const auto &t1) {
const auto &t2 = std::get<remove_cvref_t<decltype(t1)>>(other._table);
return t1 == t2;
try {
using BinTableT = remove_cvref_t<decltype(t1)>;
const auto &t2 = std::get<BinTableT>(other._table);
return t1 == t2;
} catch (const std::bad_variant_access &) {
return false;
}
},
_table);
}
Expand Down
12 changes: 10 additions & 2 deletions src/libhictk/cooler/include/hictk/cooler/impl/utils_merge_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,21 @@ template <typename N>
inline void validate_bin_size(const std::vector<LightCooler<N>>& coolers) {
assert(coolers.size() > 1);
const auto& clr1 = coolers.front();
const auto bin_table1 = cooler::File(clr1.uri).bins();

for (std::size_t i = 1; i < coolers.size(); ++i) {
const auto& clr2 = coolers[i];
if (clr1.bin_size != clr2.bin_size) {

if (clr1.bin_size == 0 || clr2.bin_size == 0) { // Table(s) with variable bin size
const auto bin_table2 = cooler::File(clr2.uri).bins();
if (bin_table1 != bin_table2) {
throw std::runtime_error(fmt::format(
FMT_STRING("cooler \"{}\" and \"{}\" have different bin tables"), clr1.uri, clr2.uri));

Check warning on line 51 in src/libhictk/cooler/include/hictk/cooler/impl/utils_merge_impl.hpp

View check run for this annotation

Codecov / codecov/patch

src/libhictk/cooler/include/hictk/cooler/impl/utils_merge_impl.hpp#L48-L51

Added lines #L48 - L51 were not covered by tests
}
} else if (clr1.bin_size != clr2.bin_size) {
throw std::runtime_error(fmt::format(
FMT_STRING(
"cooler \"{}\" and \"{}\" have different resolutions ({} and {} respectively)"),
"cooler \"{}\" and \"{}\" have different resolutions ({} and {} respectively)"),
clr1.uri, clr2.uri, clr1.bin_size, clr2.bin_size));
}
}
Expand Down
24 changes: 24 additions & 0 deletions test/units/bin_table/bin_table_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,13 @@ TEST_CASE("BinTable (fixed bins)", "[bin-table][short]") {
CHECK_THROWS(table.get<BinTableVariable<>>());
}

SECTION("operator==") {
CHECK(BinTable(table.chromosomes(), 10) == BinTable(table.chromosomes(), 10));
CHECK(BinTable(table.chromosomes(), 10) != BinTable(table.chromosomes(), 20));
CHECK(BinTable(Reference{table.chromosomes().begin(), table.chromosomes().end() - 1}, 10) !=
BinTable(table.chromosomes(), 10));
}

SECTION("iterators") {
const auto& chr1 = table.chromosomes().at("chr1");
const auto& chr2 = table.chromosomes().at("chr2");
Expand Down Expand Up @@ -325,6 +332,23 @@ TEST_CASE("BinTable (variable bins)", "[bin-table][short]") {
}
}

SECTION("operator==") {
CHECK(BinTable(table.chromosomes(), start_pos, end_pos) ==
BinTable(table.chromosomes(), start_pos, end_pos));

const std::vector<std::uint32_t> start_pos1{0, 0};
const std::vector<std::uint32_t> end_pos1{32, 32};
CHECK(BinTable(table.chromosomes(), start_pos, end_pos) !=
BinTable(table.chromosomes(), start_pos1, end_pos1));

const std::vector<std::uint32_t> start_pos2{0};
const std::vector<std::uint32_t> end_pos2{32};
CHECK(BinTable(Reference{table.chromosomes().begin(), table.chromosomes().end() - 1},
start_pos2, end_pos2) != BinTable(table.chromosomes(), start_pos, end_pos));

CHECK(BinTable(table.chromosomes(), start_pos, end_pos) != BinTable(table.chromosomes(), 10));
}

SECTION("iterators") {
const auto& chr1 = table.chromosomes().at("chr1");
const auto& chr2 = table.chromosomes().at("chr2");
Expand Down

0 comments on commit 8017d56

Please sign in to comment.