diff --git a/conanfile.txt b/conanfile.txt index 7436f87..51907ba 100644 --- a/conanfile.txt +++ b/conanfile.txt @@ -55,6 +55,7 @@ hdf5*:enable_cxx=False hdf5*:hl=False hdf5*:threadsafe=False hdf5*:parallel=False +hictk*:with_eigen=False highfive*:with_boost=False highfive*:with_eigen=False highfive*:with_opencv=False diff --git a/src/hictkr_file.cpp b/src/hictkr_file.cpp index b7577b7..0fbf136 100644 --- a/src/hictkr_file.cpp +++ b/src/hictkr_file.cpp @@ -255,13 +255,16 @@ static Rcpp::NumericMatrix fetch_as_matrix(const Selector &sel) { matrix.at(i1, i2) = tp.count; if (mirror_matrix) { - // Mirror matrix below diagonal - if (i2 - i1 < num_rows && i1 < num_cols && i2 < num_rows) { + const auto delta = i2 - i1; + if (delta >= 0 && delta < num_rows && i1 < num_cols && i2 < num_rows) { matrix.at(i2, i1) = tp.count; - } else if (i2 - i1 > num_cols && i1 < num_cols && i2 < num_rows) { + } else if ((delta < 0 || delta > num_cols) && i1 < num_cols && i2 < num_rows) { const auto i3 = static_cast(tp.bin2_id - row_offset); const auto i4 = static_cast(tp.bin1_id - col_offset); - matrix.at(i3, i4) = tp.count; + + if (i3 >= 0 && i3 < num_rows && i4 >= 0 && i4 < num_cols) { + matrix.at(i3, i4) = tp.count; + } } } }); diff --git a/tests/testthat/test-fetch-dense.R b/tests/testthat/test-fetch-dense.R index c00e303..9e90cda 100644 --- a/tests/testthat/test-fetch-dense.R +++ b/tests/testthat/test-fetch-dense.R @@ -19,7 +19,7 @@ for (path in test_files) { expect_equal(sum_, 178263235) }) - test_that("HiCFile: fetch (dense) cis", { + test_that("HiCFile: fetch (dense) symmetric cis", { f <- File(path, 100000) m <- fetch(f, "chr2R:10,000,000-15,000,000", type="dense") @@ -31,6 +31,35 @@ for (path in test_files) { expect_equal(sum_, 6029333) }) + test_that("HiCFile: fetch (dense) asymmetric cis", { + f <- File(path, 100000) + + m <- fetch(f, "chr2L:0-10,000,000", "chr2L:5,000,000-20,000,000", type="dense") + + shape <- dim(m) + sum_ <- sum(m) + + expect_equal(shape, c(100, 150)) + expect_equal(sum_, 6287451) + + + m <- fetch(f, "chr2L:0-10,000,000", "chr2L:10,000,000-20,000,000", type="dense") + + shape <- dim(m) + sum_ <- sum(m) + + expect_equal(shape, c(100, 100)) + expect_equal(sum_, 761223) + + m <- fetch(f, "chr2L:0-10,000,000", "chr2L:0-15,000,000", type="dense") + + shape <- dim(m) + sum_ <- sum(m) + + expect_equal(shape, c(100, 150)) + expect_equal(sum_, 12607205) + }) + test_that("HiCFile: fetch (dense) cis BED queries", { f <- File(path, 100000)