From 103d7627e87d81e5614ee527f4b967a95bceee03 Mon Sep 17 00:00:00 2001 From: ppaxisa Date: Fri, 26 Jan 2024 11:42:40 +0100 Subject: [PATCH] roll back expand methods and test to use S4vectors List methods --- R/ranges-expand.R | 6 +++--- man/ranges-expand.Rd | 4 ++-- tests/testthat/test-unnest.R | 14 ++++++++------ 3 files changed, 13 insertions(+), 11 deletions(-) diff --git a/R/ranges-expand.R b/R/ranges-expand.R index 6eeff0f..59b75d3 100644 --- a/R/ranges-expand.R +++ b/R/ranges-expand.R @@ -23,7 +23,7 @@ #' @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) @@ -31,7 +31,7 @@ #' #' # 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) @@ -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) diff --git a/man/ranges-expand.Rd b/man/ranges-expand.Rd index 177328a..3307636 100644 --- a/man/ranges-expand.Rd +++ b/man/ranges-expand.Rd @@ -42,7 +42,7 @@ Expand list-columns in a Ranges object \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) @@ -50,7 +50,7 @@ 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) diff --git a/tests/testthat/test-unnest.R b/tests/testthat/test-unnest.R index 84f38ee..6329465 100644 --- a/tests/testthat/test-unnest.R +++ b/tests/testthat/test-unnest.R @@ -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", @@ -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) @@ -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) @@ -76,7 +78,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)), id1 = c("a", "b", "b", "c", "c")) test_gr <- expand_ranges(gr, col1, .id = "id1", .recursive = TRUE) @@ -84,7 +86,7 @@ test_that("expanding makes sense", { # 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) @@ -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")