diff --git a/NAMESPACE b/NAMESPACE index 4677547..4f63b48 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -15,6 +15,7 @@ S3method(arrange,GInteractions) S3method(as_ginteractions,data.frame) S3method(as_ginteractions,default) S3method(count,GInteractions) +S3method(count,GroupedGInteractions) S3method(count_overlaps,GInteractions) S3method(count_overlaps,PinnedGInteractions) S3method(count_overlaps_directed,GInteractions) diff --git a/tests/testthat/test-classes.R b/tests/testthat/test-classes.R index ab0226f..c919b88 100644 --- a/tests/testthat/test-classes.R +++ b/tests/testthat/test-classes.R @@ -204,4 +204,150 @@ test_that("classes work", { gi |> ungroup() |> expect_error("GInteractions") show(ggi) |> expect_no_error() + ## DelegatingGInteractions + expect_identical( + anchors1(apgi), + new("GRanges", seqnames = new("Rle", values = structure(1L, levels = c("chr1", + "chr2"), class = "factor"), lengths = 4L, elementMetadata = NULL, + metadata = list()), ranges = new("IRanges", start = c(11L, + 11L, 11L, 11L), width = c(10L, 10L, 20L, 20L), NAMES = NULL, + elementType = "ANY", elementMetadata = NULL, metadata = list()), + strand = new("Rle", values = structure(1:2, levels = c("+", + "-", "*"), class = "factor"), lengths = c(2L, 2L), elementMetadata = NULL, + metadata = list()), seqinfo = new("Seqinfo", seqnames = c("chr1", + "chr2"), seqlengths = c(NA_integer_, NA_integer_), is_circular = c(NA, + NA), genome = c(NA_character_, NA_character_)), elementMetadata = new("DFrame", + rownames = NULL, nrows = 4L, elementType = "ANY", elementMetadata = NULL, + metadata = list(), listData = structure(list(), names = character(0))), + elementType = "ANY", metadata = list()) + ) + expect_identical( + ranges1(apgi), + new("IRanges", start = c(11L, 11L, 11L, 11L), width = c(10L, + 10L, 20L, 20L), NAMES = NULL, elementType = "ANY", elementMetadata = NULL, + metadata = list()) + ) + expect_identical( + seqnames1(apgi), + new("Rle", values = structure(1L, levels = c("chr1", "chr2"), class = "factor"), + lengths = 4L, elementMetadata = NULL, metadata = list()) + ) + expect_identical( + start1(apgi), + c(11L, 11L, 11L, 11L) + ) + expect_identical( + end1(apgi), + c(20L, 20L, 30L, 30L) + ) + expect_identical( + width1(apgi), + c(10L, 10L, 20L, 20L) + ) + expect_identical( + strand1(apgi), + new("Rle", values = structure(1:2, levels = c("+", "-", "*"), class = "factor"), + lengths = c(2L, 2L), elementMetadata = NULL, metadata = list()) + ) + expect_identical( + anchors2(apgi), + new("GRanges", seqnames = new("Rle", values = structure(1:2, levels = c("chr1", + "chr2"), class = "factor"), lengths = c(3L, 1L), elementMetadata = NULL, + metadata = list()), ranges = new("IRanges", start = c(21L, + 51L, 51L, 51L), width = c(10L, 5L, 5L, 10L), NAMES = NULL, elementType = "ANY", + elementMetadata = NULL, metadata = list()), strand = new("Rle", + values = structure(1:2, levels = c("+", "-", "*"), class = "factor"), + lengths = c(2L, 2L), elementMetadata = NULL, metadata = list()), + seqinfo = new("Seqinfo", seqnames = c("chr1", "chr2"), seqlengths = c(NA_integer_, + NA_integer_), is_circular = c(NA, NA), genome = c(NA_character_, + NA_character_)), elementMetadata = new("DFrame", rownames = NULL, + nrows = 4L, elementType = "ANY", elementMetadata = NULL, + metadata = list(), listData = structure(list(), names = character(0))), + elementType = "ANY", metadata = list()) + ) + expect_identical( + ranges2(apgi), + new("IRanges", start = c(21L, 51L, 51L, 51L), width = c(10L, + 5L, 5L, 10L), NAMES = NULL, elementType = "ANY", elementMetadata = NULL, + metadata = list()) + ) + expect_identical( + seqnames2(apgi), + new("Rle", values = structure(1:2, levels = c("chr1", "chr2"), class = "factor"), + lengths = c(3L, 1L), elementMetadata = NULL, metadata = list()) + ) + expect_identical( + start2(apgi), + c(21L, 51L, 51L, 51L) + ) + expect_identical( + end2(apgi), + c(30L, 55L, 55L, 60L) + ) + expect_identical( + width2(apgi), + c(10L, 5L, 5L, 10L) + ) + expect_identical( + strand2(apgi), + new("Rle", values = structure(1:2, levels = c("+", "-", "*"), class = "factor"), + lengths = c(2L, 2L), elementMetadata = NULL, metadata = list()) + ) + expect_identical( + anchors(apgi), + list(first = new("GRanges", seqnames = new("Rle", values = structure(1L, levels = c("chr1", + "chr2"), class = "factor"), lengths = 4L, elementMetadata = NULL, + metadata = list()), ranges = new("IRanges", start = c(11L, + 11L, 11L, 11L), width = c(10L, 10L, 20L, 20L), NAMES = NULL, + elementType = "ANY", elementMetadata = NULL, metadata = list()), + strand = new("Rle", values = structure(1:2, levels = c("+", + "-", "*"), class = "factor"), lengths = c(2L, 2L), elementMetadata = NULL, + metadata = list()), seqinfo = new("Seqinfo", seqnames = c("chr1", + "chr2"), seqlengths = c(NA_integer_, NA_integer_), is_circular = c(NA, + NA), genome = c(NA_character_, NA_character_)), elementMetadata = new("DFrame", + rownames = NULL, nrows = 4L, elementType = "ANY", elementMetadata = NULL, + metadata = list(), listData = structure(list(), names = character(0))), + elementType = "ANY", metadata = list()), second = new("GRanges", + seqnames = new("Rle", values = structure(1:2, levels = c("chr1", + "chr2"), class = "factor"), lengths = c(3L, 1L), elementMetadata = NULL, + metadata = list()), ranges = new("IRanges", start = c(21L, + 51L, 51L, 51L), width = c(10L, 5L, 5L, 10L), NAMES = NULL, + elementType = "ANY", elementMetadata = NULL, metadata = list()), + strand = new("Rle", values = structure(1:2, levels = c("+", + "-", "*"), class = "factor"), lengths = c(2L, 2L), elementMetadata = NULL, + metadata = list()), seqinfo = new("Seqinfo", seqnames = c("chr1", + "chr2"), seqlengths = c(NA_integer_, NA_integer_), is_circular = c(NA, + NA), genome = c(NA_character_, NA_character_)), elementMetadata = new("DFrame", + rownames = NULL, nrows = 4L, elementType = "ANY", elementMetadata = NULL, + metadata = list(), listData = structure(list(), names = character(0))), + elementType = "ANY", metadata = list())) + ) + expect_identical( + regions(apgi), + new("GRanges", seqnames = new("Rle", values = structure(1:2, levels = c("chr1", + "chr2"), class = "factor"), lengths = c(5L, 1L), elementMetadata = NULL, + metadata = list()), ranges = new("IRanges", start = c(11L, + 21L, 51L, 11L, 51L, 51L), width = c(10L, 10L, 5L, 20L, 5L, 10L + ), NAMES = NULL, elementType = "ANY", elementMetadata = NULL, + metadata = list()), strand = new("Rle", values = structure(1:2, levels = c("+", + "-", "*"), class = "factor"), lengths = c(3L, 3L), elementMetadata = NULL, + metadata = list()), seqinfo = new("Seqinfo", seqnames = c("chr1", + "chr2"), seqlengths = c(NA_integer_, NA_integer_), is_circular = c(NA, + NA), genome = c(NA_character_, NA_character_)), elementMetadata = new("DFrame", + rownames = NULL, nrows = 6L, elementType = "ANY", elementMetadata = NULL, + metadata = list(), listData = structure(list(), names = character(0))), + elementType = "ANY", metadata = list()) + ) + expect_identical( + seqinfo(apgi), + new("Seqinfo", seqnames = c("chr1", "chr2"), seqlengths = c(NA_integer_, + NA_integer_), is_circular = c(NA, NA), genome = c(NA_character_, + NA_character_)) + ) + expect_identical( + mcols(apgi)[,2], + c("cis", "cis", "cis", "trans") + ) + show(apgi) |> expect_no_error() + }) diff --git a/tests/testthat/test-dplyr.R b/tests/testthat/test-dplyr.R index c0e2473..3559d19 100644 --- a/tests/testthat/test-dplyr.R +++ b/tests/testthat/test-dplyr.R @@ -12,19 +12,110 @@ test_that("dplyr functions work", { ## tally/count ggi |> count(strand1) |> expect_no_error() - ggi |> tally() |> expect_no_error() - gi |> count(strand1) |> expect_s4_class("DataFrame") - gi |> tally() |> expect_error() + expect_identical( + ggi |> count(group), + new("DFrame", rownames = NULL, nrows = 2L, elementType = "ANY", + elementMetadata = NULL, metadata = list(), listData = list( + group = c(1, 2), n = c(2L, 2L))) + ) + expect_identical( + ggi |> count(type), + new("DFrame", rownames = NULL, nrows = 3L, elementType = "ANY", + elementMetadata = NULL, metadata = list(), listData = list( + group = c(1, 2, 2), type = c("cis", "cis", "trans"), + n = c(2L, 1L, 1L))) + ) + expect_identical( + ggi |> tally(), + new("DFrame", rownames = NULL, nrows = 2L, elementType = "ANY", + elementMetadata = NULL, metadata = list(), listData = list( + group = c(1, 2), n = c(2L, 2L))) + ) + expect_identical( + ggi |> count(strand1), + new("DFrame", rownames = NULL, nrows = 2L, elementType = "ANY", + elementMetadata = NULL, metadata = list(), listData = list( + group = c(1, 2), strand1 = new("Rle", values = structure(1:2, levels = c("+", + "-", "*"), class = "factor"), lengths = c(1L, 1L), elementMetadata = NULL, + metadata = list()), n = c(2L, 2L))) + ) + expect_identical( + gi |> count(strand1), + new("DFrame", rownames = NULL, nrows = 2L, elementType = "ANY", + elementMetadata = NULL, metadata = list(), listData = list( + strand1 = new("Rle", values = structure(1:2, levels = c("+", + "-", "*"), class = "factor"), lengths = c(1L, 1L), elementMetadata = NULL, + metadata = list()), n = c(2L, 2L))) + ) + expect_identical( + ggi |> count(strand1, wt = score), + new("DFrame", rownames = NULL, nrows = 2L, elementType = "ANY", + elementMetadata = NULL, metadata = list(), listData = list( + group = c(1, 2), strand1 = new("Rle", values = structure(1:2, levels = c("+", + "-", "*"), class = "factor"), lengths = c(1L, 1L), elementMetadata = NULL, + metadata = list()), n = c(0.736002816120163, 1.23265417455696 + ))), + tolerance = 1e-4 + ) + expect_identical( + gi |> count(strand1, wt = score), + new("DFrame", rownames = NULL, nrows = 2L, elementType = "ANY", + elementMetadata = NULL, metadata = list(), listData = list( + strand1 = new("Rle", values = structure(1:2, levels = c("+", + "-", "*"), class = "factor"), lengths = c(1L, 1L), elementMetadata = NULL, + metadata = list()), n = c(0.736002816120163, 1.23265417455696 + ))), + tolerance = 1e-4 + ) + expect_identical( + ggi |> count(strand1, wt = score, sort = TRUE), + new("DFrame", rownames = NULL, nrows = 2L, elementType = "ANY", + elementMetadata = NULL, metadata = list(), listData = list( + group = c(2, 1), strand1 = new("Rle", values = structure(2:1, levels = c("+", + "-", "*"), class = "factor"), lengths = c(1L, 1L), elementMetadata = NULL, + metadata = list()), n = c(1.23265417455696, 0.736002816120163 + ))), + tolerance = 1e-4 + ) + expect_identical( + gi |> count(strand1, wt = score, sort = TRUE), + new("DFrame", rownames = NULL, nrows = 2L, elementType = "ANY", + elementMetadata = NULL, metadata = list(), listData = list( + strand1 = new("Rle", values = structure(2:1, levels = c("+", + "-", "*"), class = "factor"), lengths = c(1L, 1L), elementMetadata = NULL, + metadata = list()), n = c(1.23265417455696, 0.736002816120163 + ))), + tolerance = 1e-4 + ) ## filter - expect_identical(gi |> filter(strand1 == '-') |> length(), 2L) + expect_identical( + gi |> filter(strand1 == '-') |> ranges1(), + new("IRanges", start = c(11L, 11L), width = c(20L, 20L), NAMES = NULL, + elementType = "ANY", elementMetadata = NULL, metadata = list()) + ) + expect_identical( + gi |> filter(strand1 == '+') |> ranges1(), + new("IRanges", start = c(11L, 11L), width = c(10L, 10L), NAMES = NULL, + elementType = "ANY", elementMetadata = NULL, metadata = list()) + ) ggi |> filter(strand1 == '+') |> expect_error() ## mutate - gi |> mutate(strand1 = '-') |> expect_s4_class("GInteractions") - gi |> mutate(xxx = 1) |> expect_s4_class("GInteractions") + expect_identical( + gi |> mutate(strand1 = '-') |> strand1(), + new("Rle", values = structure(2L, levels = c("+", "-", "*"), class = "factor"), + lengths = 4L, elementMetadata = NULL, metadata = list()) + ) + expect_identical( + gi |> mutate(xxx = 1) |> mcols() |> subset(, c(2, 3)), + new("DFrame", rownames = NULL, nrows = 4L, elementType = "ANY", + elementMetadata = NULL, metadata = list(), listData = list( + type = c("cis", "cis", "cis", "trans"), xxx = c(1, 1, + 1, 1))) + ) expect_identical( mutate(gi, xxx = IRanges::RleList(c(1, 2), c(3, 4)))$xxx, IRanges::RleList(c(1, 2), c(3, 4), c(1, 2), c(3, 4)) @@ -58,24 +149,87 @@ test_that("dplyr functions work", { ) ## slice - gi |> slice(1:3) |> expect_s4_class("GInteractions") + expect_identical( + gi |> slice(1:3) |> start1(), + c(11L, 11L, 11L) + ) gi |> slice(1:5) |> expect_error() gi |> slice('error') |> expect_error() ## summarize - ggi |> summarize(m = mean(score)) - ggi |> summarise(m = mean(score)) + expect_identical( + ggi |> summarize(m = mean(score)), + new("DFrame", rownames = NULL, nrows = 2L, elementType = "ANY", + elementMetadata = NULL, metadata = list(), listData = list( + group = c(1, 2), m = c(0.368001408060081, 0.616327087278478 + ))), + tolerance = 1e-4 + ) + expect_identical( + ggi |> summarize(m = table(type)), + new("DFrame", rownames = NULL, nrows = 2L, elementType = "ANY", + elementMetadata = NULL, metadata = list(), listData = list( + group = c(1, 2), m = new("CompressedIntegerList", elementType = "integer", + elementMetadata = NULL, metadata = list(), unlistData = c(cis = 2L, + trans = 0L, cis = 1L, trans = 1L), partitioning = new("PartitioningByEnd", + end = c(2L, 4L), NAMES = c("1", "2"), elementType = "ANY", + elementMetadata = NULL, metadata = list())))) + ) + expect_identical( + ggi |> summarize(m = table(type), n = table(group)), + new("DFrame", rownames = NULL, nrows = 2L, elementType = "ANY", + elementMetadata = NULL, metadata = list(), listData = list( + group = c(1, 2), m = new("CompressedIntegerList", elementType = "integer", + elementMetadata = NULL, metadata = list(), unlistData = c(cis = 2L, + trans = 0L, cis = 1L, trans = 1L), partitioning = new("PartitioningByEnd", + end = c(2L, 4L), NAMES = c("1", "2"), elementType = "ANY", + elementMetadata = NULL, metadata = list())), + n = new("CompressedIntegerList", elementType = "integer", + elementMetadata = NULL, metadata = list(), unlistData = c(`1` = 2L, + `2` = 0L, `1` = 0L, `2` = 2L), partitioning = new("PartitioningByEnd", + end = c(2L, 4L), NAMES = c("1", "2"), elementType = "ANY", + elementMetadata = NULL, metadata = list())))) + ) }) test_that("dplyr group functions work", { - group_data(ggi) |> expect_s4_class("DataFrame") - group_keys(ggi) |> expect_s4_class("DataFrame") - expect_identical(group_indices(ggi), S4Vectors::Rle(c(1L, 1L, 2L, 2L))) - expect_identical(group_vars(ggi), "group") - expect_identical(groups(ggi), list(rlang::sym("group"))) - expect_identical(group_size(ggi), c(2L, 2L)) - expect_identical(n_groups(ggi), 2L) + expect_identical( + group_data(ggi), + new("DFrame", rownames = NULL, nrows = 2L, elementType = "ANY", + elementMetadata = NULL, metadata = list(), listData = list( + group = c(1, 2), .rows = new("CompressedIntegerList", + elementType = "integer", elementMetadata = NULL, + metadata = list(), unlistData = 1:4, partitioning = new("PartitioningByEnd", + end = c(2L, 4L), NAMES = NULL, elementType = "ANY", + elementMetadata = NULL, metadata = list())))) + ) + expect_identical( + group_keys(ggi), + new("DFrame", rownames = NULL, nrows = 2L, elementType = "ANY", + elementMetadata = NULL, metadata = list(), listData = list( + group = c(1, 2))) + ) + expect_identical( + group_indices(ggi), + S4Vectors::Rle(c(1L, 1L, 2L, 2L)) + ) + expect_identical( + group_vars(ggi), + "group" + ) + expect_identical( + groups(ggi), + list(rlang::sym("group")) + ) + expect_identical( + group_size(ggi), + c(2L, 2L) + ) + expect_identical( + n_groups(ggi), + 2L + ) }) diff --git a/tests/testthat/test-setters.R b/tests/testthat/test-setters.R index c8e6d6c..05766a4 100644 --- a/tests/testthat/test-setters.R +++ b/tests/testthat/test-setters.R @@ -1,14 +1,49 @@ test_that("setter functions work", { - set_seqnames1(gi, factor("chr2", c("chr1", "chr2"))) |> expect_no_error() - set_seqnames2(gi, factor("chr2", c("chr1", "chr2"))) |> expect_no_error() - set_start1(gi, 1) |> expect_no_error() - set_start2(gi, 2) |> expect_no_error() - set_end1(gi, 100) |> expect_no_error() - set_end2(gi, 100) |> expect_no_error() - set_width1(gi, 10) |> expect_no_error() - set_width2(gi, 10) |> expect_no_error() - set_strand1(gi, '*') |> expect_no_error() - set_strand2(gi, c('-', '+', '-', '*')) |> expect_no_error() + expect_identical( + set_seqnames1(gi, factor("chr2", c("chr1", "chr2"))) |> seqnames1(), + new("Rle", values = structure(2L, levels = c("chr1", "chr2"), class = "factor"), + lengths = 4L, elementMetadata = NULL, metadata = list()) + ) + expect_identical( + set_seqnames2(gi, factor("chr2", c("chr1", "chr2"))) |> seqnames2(), + new("Rle", values = structure(2L, levels = c("chr1", "chr2"), class = "factor"), + lengths = 4L, elementMetadata = NULL, metadata = list()) + ) + expect_identical( + set_start1(gi, 1) |> start1(), + c(1L, 1L, 1L, 1L) + ) + expect_identical( + set_start2(gi, 2) |> start2(), + c(2L, 2L, 2L, 2L) + ) + expect_identical( + set_end1(gi, 10) |> end1(), + c(10L, 10L, 10L, 10L) + ) + expect_identical( + set_end2(gi, 200) |> end2(), + c(200L, 200L, 200L, 200L) + ) + expect_identical( + set_width1(gi, 10) |> width1(), + c(10L, 10L, 10L, 10L) + ) + expect_identical( + set_width2(gi, 200) |> width2(), + c(200L, 200L, 200L, 200L) + ) + expect_identical( + set_strand1(gi, c('-', '+', '-', '*')) |> strand1(), + new("Rle", values = structure(c(2L, 1L, 2L, 3L), levels = c("+", + "-", "*"), class = "factor"), lengths = c(1L, 1L, 1L, 1L), elementMetadata = NULL, + metadata = list()) + ) + expect_identical( + set_strand2(gi, "*") |> strand2(), + new("Rle", values = structure(3L, levels = c("+", "-", "*"), class = "factor"), + lengths = 4L, elementMetadata = NULL, metadata = list()) + ) })