Skip to content

Commit

Permalink
Merge pull request #97 from snystrom/#96
Browse files Browse the repository at this point in the history
Implements strandedness for filter_by_overlap() family. Closes #96.
  • Loading branch information
mikelove authored Apr 4, 2024
2 parents 9fd8070 + ff197eb commit 2f0ab1d
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 3 deletions.
4 changes: 4 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,11 @@ S3method(filter,GroupedIntegerRanges)
S3method(filter,Ranges)
S3method(filter_by_non_overlaps,GenomicRanges)
S3method(filter_by_non_overlaps,IntegerRanges)
S3method(filter_by_non_overlaps_directed,GenomicRanges)
S3method(filter_by_overlaps,DeferredGenomicRanges)
S3method(filter_by_overlaps,GenomicRanges)
S3method(filter_by_overlaps,IntegerRanges)
S3method(filter_by_overlaps_directed,GenomicRanges)
S3method(find_overlaps,GenomicRanges)
S3method(find_overlaps,IntegerRanges)
S3method(find_overlaps_directed,GenomicRanges)
Expand Down Expand Up @@ -210,7 +212,9 @@ export(disjoin_ranges_directed)
export(expand_ranges)
export(filter)
export(filter_by_non_overlaps)
export(filter_by_non_overlaps_directed)
export(filter_by_overlaps)
export(filter_by_overlaps_directed)
export(find_overlaps)
export(find_overlaps_directed)
export(find_overlaps_within)
Expand Down
32 changes: 30 additions & 2 deletions R/ranges-overlap-filter.R
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#'
#' @details By default, `filter_by_overlaps` and
#' `filter_by_non_overlaps` ignore strandedness for [GRanges()]
#' objects. The argument `maxgap` is the maximum number of positions
#' objects. To perform stranded operations use `filter_by_overlaps_directed` and `filter_by_non_overlaps_directed`. The argument `maxgap` is the maximum number of positions
#' between two ranges for them to be considered overlapping. Here the default
#' is set to be -1 as that is the the gap between two ranges that
#' has its start or end strictly inside the other. The argugment
Expand Down Expand Up @@ -39,8 +39,12 @@
#'
#' filter_by_overlaps(query, subject)
#'
#' filter_by_overlaps_directed(query, subject)
#'
#' filter_by_non_overlaps(query, subject)
#'
#' filter_by_non_overlaps_directed(query, subject)
#'
#' @rdname ranges-filter-overlaps
filter_by_overlaps <- function(x,y, maxgap = -1L, minoverlap = 0L) {
UseMethod("filter_by_overlaps")
Expand Down Expand Up @@ -74,4 +78,28 @@ filter_by_non_overlaps.GenomicRanges <- function(x,y, maxgap = -1L, minoverlap =
ignore.strand = TRUE)
}

# TODO -- add in more variants here?
#' @export
#' @rdname ranges-filter-overlaps
filter_by_overlaps_directed <- function(x,y, maxgap = -1L, minoverlap = 0L) {
UseMethod("filter_by_overlaps_directed")
}

#' @export
filter_by_overlaps_directed.GenomicRanges <- function(x,y, maxgap = -1L, minoverlap = 0L) {
subsetByOverlaps(x,y, maxgap, minoverlap, ignore.strand = FALSE)
}

#' @export
#' @rdname ranges-filter-overlaps
filter_by_non_overlaps_directed <- function(x,y, maxgap, minoverlap) {
UseMethod("filter_by_non_overlaps_directed")
}

#' @export
filter_by_non_overlaps_directed.GenomicRanges <- function(x,y, maxgap = -1L, minoverlap = 0L) {
subsetByOverlaps(x,y, maxgap, minoverlap,
invert = TRUE,
ignore.strand = FALSE)
}

# TODO -- add in more variants here?
12 changes: 11 additions & 1 deletion man/ranges-filter-overlaps.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions man/tidyverse-reexports.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

36 changes: 36 additions & 0 deletions tests/testthat/test-filter-by-overlap.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# test-filter-by-overlap.R

test_that("filters by overlap & non-overlap work", {

subject_gr <- as_granges(data.frame(seqnames = rep("chr1", 3),
start = c(1,1, 100),
width = rep(100, 3),
strand = c("+", "-", "+")))

query_gr <- as_granges(data.frame(seqnames = c("chr1", "chr2", "chr1"),
start = c(20, 20, 150),
width = c(10, 10, 10),
strand = c("+", "-", "+")))

filter_directed_expected_gr <- as_granges(data.frame(seqnames = rep("chr1", 2),
start = c(1, 100),
width = rep(100, 2),
strand = c("+", "+")))

filter_non_overlap_expected_gr <- GRanges(seqinfo = Seqinfo(seqnames = "chr1"))

filter_non_overlap_directed_expected_gr <- as_granges(
data.frame(seqnames = "chr1",
start = c(1),
width = rep(100),
strand = c("-")
)
)

expect_identical(subject_gr, filter_by_overlaps(subject_gr, query_gr))
expect_identical(filter_directed_expected_gr, filter_by_overlaps_directed(subject_gr, query_gr))

expect_identical(filter_non_overlap_expected_gr, filter_by_non_overlaps(subject_gr, query_gr))
expect_identical(filter_non_overlap_directed_expected_gr, filter_by_non_overlaps_directed(subject_gr, query_gr))

})

0 comments on commit 2f0ab1d

Please sign in to comment.