Skip to content

Commit

Permalink
roll back expand methods and test to use S4vectors List methods
Browse files Browse the repository at this point in the history
  • Loading branch information
ppaxisa committed Jan 26, 2024
1 parent b3219ec commit 103d762
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 11 deletions.
6 changes: 3 additions & 3 deletions R/ranges-expand.R
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,15 @@
#' @examples
#' grng <- as_granges(data.frame(seqnames = "chr1", start = 20:23, width = 1000))
#' grng <- mutate(grng,
#' exon_id = List(a = 1, b = c(4,5), c = 3, d = c(2,5))
#' exon_id = IntegerList(a = 1, b = c(4,5), c = 3, d = c(2,5))
#' )
#' grng
#' expand_ranges(grng)
#' expand_ranges(grng, .id = "name")
#'
#' # empty list elements are not preserved by default
#' grng <- mutate(grng,
#' exon_id = List(a = NULL, b = c(4,5), c= 3, d = c(2,5))
#' exon_id = IntegerList(a = NULL, b = c(4,5), c= 3, d = c(2,5))
#' )
#' expand_ranges(grng)
#' expand_ranges(grng, .keep_empty = TRUE)
Expand Down Expand Up @@ -101,7 +101,7 @@ expand_ranges <- function(data, ..., .drop = FALSE, .id = NULL, .keep_empty = FA


get_list_cols <- function(data) {
list_cols_pos <- unlist(Map(function(.) is(., "list"), mcols(data)))
list_cols_pos <- unlist(Map(function(.) is(., "List"), mcols(data)))

list_cols <- Filter(isTRUE, list_cols_pos)

Expand Down
4 changes: 2 additions & 2 deletions man/ranges-expand.Rd

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

14 changes: 8 additions & 6 deletions tests/testthat/test-unnest.R
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@ test_that("expanding makes sense", {

# by default expand_ranges does cartesian product of list columns
gr <- as_granges(data.frame(seqnames = "chr1", start = 20:22, width = 1000))

gr <- mutate(gr,
col1 = List(a = 1L, b = c(4L,5L), c = c(2L,3L)),
col2 = List(c(1L,2L), c(3L,4L), c(5L)),
col1 = IntegerList(a = 1L, b = c(4L,5L), c = c(2L,3L)),
col2 = IntegerList(c(1L,2L), c(3L,4L), c(5L)),
score = 1:3)

correct_gr <- GRanges(seqnames = "chr1",
Expand All @@ -23,6 +24,7 @@ test_that("expanding makes sense", {
col1 = c(1L,1L,4L,4L,5L,5L,2L,3L),
col2 = c(1L,2L,3L,4L,3L,4L,5L,5L),
score = c(1L,1L,2L,2L,2L,2L,3L,3L))

test_gr <- expand_ranges(gr, .recursive = TRUE)

expect_identical(correct_gr, test_gr)
Expand All @@ -43,7 +45,7 @@ test_that("expanding makes sense", {
ranges = IRanges(start = c(20,21,21,22,22),
width = 1000),
col1 = as.integer(c(1,4,5,2,3)),
col2 = I(list(c(1L,2L), c(3L,4L), c(3L,4L), 5L,5L)),
col2 = IntegerList(c(1L,2L), c(3L,4L), c(3L,4L), 5L,5L),
score = as.integer(c(1,2,2,3,3)))
test_gr <- expand_ranges(gr, col1)
expect_identical(correct_gr, test_gr)
Expand Down Expand Up @@ -76,15 +78,15 @@ test_that("expanding makes sense", {
ranges = IRanges(start = c(20,21,21,22,22),
width = 1000),
col1 = as.integer(c(1,4,5,2,3)),
col2 = I(list(c(1L,2L), c(3L,4L), c(3L,4L), 5L,5L)),
col2 = IntegerList(c(1L,2L), c(3L,4L), c(3L,4L), 5L,5L),
score = as.integer(c(1,2,2,3,3)),
id1 = c("a", "b", "b", "c", "c"))
test_gr <- expand_ranges(gr, col1, .id = "id1", .recursive = TRUE)
expect_identical(correct_gr, test_gr)

# one column test
gr <- as_granges(data.frame(seqnames = "chr1", start = 20:22, width = 1000))
gr <- mutate(gr, col1 = list(a = 1, b = c(4,5), c = c(2,3)))
gr <- mutate(gr, col1 = IntegerList(a = 1, b = c(4,5), c = c(2,3)))
correct_gr <- S4Vectors::expand(gr)
expect_equal(expand_ranges(gr), correct_gr)

Expand All @@ -93,7 +95,7 @@ test_that("expanding makes sense", {

# drop emptys
gr <- as_granges(data.frame(seqnames = "chr1", start = 20:22, width = 1000))
gr <- mutate(gr, col1 = list(a = integer(), b = c(4,5), c = c(2,3)))
gr <- mutate(gr, col1 = IntegerList(a = integer(), b = c(4,5), c = c(2,3)))
correct_gr <- S4Vectors::expand(gr, keepEmptyRows = TRUE)
expect_equal(expand_ranges(gr, .keep_empty = TRUE), correct_gr)
correct_gr$id1 <- c("a", "b", "b", "c", "c")
Expand Down

0 comments on commit 103d762

Please sign in to comment.