Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ensure that first interval per group is part of cluster in bed_cluster #389

Merged
merged 1 commit into from
Aug 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# valr (development version)

* Fixed bug in handling the max_dist argument for first intervals in a contig with `bed_cluster()` (#388)

# valr 0.6.4

* Fixed intron score numbering error in `create_introns` (#377 @sheridar)
Expand Down
16 changes: 9 additions & 7 deletions src/merge.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,19 +80,21 @@ DataFrame clusterMergedIntervals(const ValrGroupedDataFrame& gdf,
ListView idx(gdf.indices()) ;

// approach from http://www.geeksforgeeks.org/merging-intervals/

std::vector<ivl_t> s ;

for (int i = 0; i < ng; i++) {

IntegerVector indices ;
indices = idx[i];
int ni = indices.size();

ivl_vector_t intervals = makeIntervalVector(df, indices);
// set dummy first interval to ensure first interval maintained

// store a first interval to ensure first interval maintained
ivl_t last_interval = ivl_t(0, 0, 0) ;
std::vector<ivl_t> s ;
s.push_back(last_interval) ;
for (auto it : intervals) {

for (int j = 0; j < ni ; j++) {
ivl_t it = intervals[j];
// get index to store cluster ids in vector
auto idx = it.value ;

Expand All @@ -102,7 +104,7 @@ DataFrame clusterMergedIntervals(const ValrGroupedDataFrame& gdf,

last_interval = it ; // set interval to compare
auto top = s.back() ; // last good interval
if (top.stop + max_dist < it.start) {
if ( j == 0 || top.stop + max_dist < it.start) {
// no overlap push to end of vector and get new id
s.push_back(it) ;
cluster_id++ ;
Expand All @@ -115,7 +117,7 @@ DataFrame clusterMergedIntervals(const ValrGroupedDataFrame& gdf,
top.stop = it.stop ; // update end position
s.pop_back() ; // remove best end
s.push_back(top) ; // update end interval
ids[idx] = cluster_id ; // assign cluster id at proper inde
ids[idx] = cluster_id ; // assign cluster id at proper index
}

else {
Expand Down
23 changes: 23 additions & 0 deletions tests/testthat/test_cluster.r
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,26 @@ test_that("cluster ids are not repeated per group issue #171", {
shared_ids <- intersect(chr1_ids, chr2_ids)
expect_equal(length(shared_ids), 0)
})


test_that("guard against max_dist argument preventing clustering first interval in contig issue #388", {
x <- tibble::tribble(
~chrom, ~start, ~end,
"a", 1, 10,
"a", 20, 50,
"b", 20, 50,
"c", 100, 100
)

res <- bed_cluster(x, max_dist=0)
expect_equal(res$.id, 1L:4L)

res <- bed_cluster(x, max_dist=100)
expect_equal(res$.id, c(1, 1, 2, 3))

res <- bed_cluster(x, max_dist=10)
expect_equal(res$.id, c(1, 1, 2, 3))

res <- bed_cluster(x, max_dist=9)
expect_equal(res$.id, 1L:4L)
})