Skip to content

Commit 65c8bfd

Browse files
authored
Merge branch 'master' into update-documentation
2 parents cd1dba6 + 638183d commit 65c8bfd

File tree

4 files changed

+170
-14
lines changed

4 files changed

+170
-14
lines changed

DESCRIPTION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
Type: Package
22
Package: tidySummarizedExperiment
33
Title: Brings SummarizedExperiment to the Tidyverse
4-
Version: 1.11.3
4+
Version: 1.11.4
55
Authors@R: c(person("Stefano", "Mangiola", email = "mangiolastefano@gmail.com",
66
role = c("aut", "cre")) )
77
Description: The tidySummarizedExperiment package provides a set of tools for creating and

R/print_method.R

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -64,16 +64,30 @@ tbl_format_header.tidySummarizedExperiment <- function(x, setup, ...) {
6464
print.SummarizedExperiment <- function(x, ..., n=NULL,
6565
width=NULL, n_extra=NULL) {
6666

67-
# Fix NOTEs
68-
. <- NULL
6967

70-
# Stop if column names of assays do not overlap
71-
if (check_if_assays_are_NOT_overlapped(x))
72-
stop(
73-
"tidySummarizedExperiment says:",
74-
" the assays in your SummarizedExperiment have column names, ",
75-
"but their order is not the same, and they not completely overlap."
76-
)
68+
# Fix NOTEs
69+
. <- NULL
70+
71+
# Stop if any column or row names are duplicated
72+
if (check_if_any_dimnames_duplicated(x, dim = "cols")) {
73+
stop("tidySummarizedExperiment says: some column names are duplicated")
74+
}
75+
if (check_if_any_dimnames_duplicated(x, dim = "rows")) {
76+
stop("tidySummarizedExperiment says: some row names are duplicated")
77+
}
78+
# Stop if column names of assays do not overlap
79+
if (check_if_assays_are_NOT_overlapped(x, dim = "cols")) {
80+
stop(
81+
"tidySummarizedExperiment says: the assays in your SummarizedExperiment have column names,
82+
but they do not completely overlap."
83+
)
84+
}
85+
if (check_if_assays_are_NOT_overlapped(x, dim = "rows")) {
86+
stop(
87+
"tidySummarizedExperiment says: the assays in your SummarizedExperiment have row names,
88+
but they do not completely overlap."
89+
)
90+
}
7791

7892
# reorder assay colnames before printing
7993
# Rearrange if assays has colnames and rownames

R/utilities.R

Lines changed: 61 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -623,6 +623,14 @@ get_special_datasets <- function(se) {
623623
}
624624

625625
check_se_dimnames <- function(se) {
626+
# Stop if any column or row names are duplicated
627+
if (check_if_any_dimnames_duplicated(se, dim = "cols")) {
628+
stop("tidySummarizedExperiment says: some column names are duplicated")
629+
}
630+
if (check_if_any_dimnames_duplicated(se, dim = "rows")) {
631+
stop("tidySummarizedExperiment says: some row names are duplicated")
632+
}
633+
626634
# Stop if column names of assays do not overlap, or if some assays have
627635
# column names and others don't
628636
if (check_if_assays_are_NOT_overlapped(se, dim = "cols")) {
@@ -1275,6 +1283,51 @@ check_if_assays_are_NOT_consistently_ordered <- function(se) {
12751283
not()
12761284
}
12771285

1286+
check_if_any_dimnames_duplicated <- function(se, dim = "cols") {
1287+
stopifnot(dim %in% c("rows", "cols"))
1288+
if (dim == "rows") {
1289+
dimnames_function <- rownames
1290+
nbr_unique_dimnames_function <- function(x) length(unique(rownames(x)))
1291+
length_function <- nrow
1292+
} else {
1293+
dimnames_function <- colnames
1294+
nbr_unique_dimnames_function <- function(x) length(unique(colnames(x)))
1295+
length_function <- ncol
1296+
}
1297+
1298+
# Check assays
1299+
# If I have any assay at all
1300+
assays_check <- assays(se) |> length() |> gt(0) &&
1301+
1302+
# If I have at least one assay with dimnames
1303+
Filter(
1304+
Negate(is.null),
1305+
assays(se, withDimnames = FALSE) |>
1306+
as.list() |>
1307+
map(dimnames_function)
1308+
) |>
1309+
length() |>
1310+
gt(0) &&
1311+
1312+
# If any named assay have fewer unique names than expected
1313+
assays(se, withDimnames = FALSE) |>
1314+
as.list() |>
1315+
map(dimnames_function) |>
1316+
Filter(Negate(is.null), x = _) |>
1317+
map(unique) |>
1318+
map(length) |>
1319+
reduce(min) |>
1320+
equals(length_function(se)) |>
1321+
not()
1322+
1323+
# Check SE object
1324+
se_check <- !is.null(dimnames_function(se)) &&
1325+
nbr_unique_dimnames_function(se) != length_function(se)
1326+
1327+
# Return TRUE if either of the two checks return TRUE
1328+
assays_check || se_check
1329+
}
1330+
12781331
check_if_assays_are_NOT_overlapped <- function(se, dim = "cols") {
12791332
stopifnot(dim %in% c("rows", "cols"))
12801333
if (dim == "rows") {
@@ -1284,6 +1337,7 @@ check_if_assays_are_NOT_overlapped <- function(se, dim = "cols") {
12841337
dimnames_function <- colnames
12851338
length_function <- ncol
12861339
}
1340+
is_identical_for_reduce <- function(x,y) if (identical(x,y)) x else FALSE
12871341

12881342
# If I have any assay at all
12891343
assays(se) |> length() |> gt(0) &&
@@ -1300,13 +1354,16 @@ check_if_assays_are_NOT_overlapped <- function(se, dim = "cols") {
13001354

13011355
# If I have lack of consistency
13021356
# This will be TRUE also if some assays have dimnames and other don't
1357+
# For each assay, sort the dimnames, then check that they are all the
1358+
# same. Can't check for unique length, since some names may be repeated
1359+
# If they're not all the same, the reduce() step will return FALSE;
1360+
# otherwise, returns the (shared) dimnames
13031361
assays(se, withDimnames = FALSE) |>
13041362
as.list() |>
13051363
map(dimnames_function) |>
1306-
reduce(intersect) |>
1307-
length() |>
1308-
equals(length_function(se)) |>
1309-
not()
1364+
map(sort) |>
1365+
reduce(is_identical_for_reduce) |>
1366+
is.logical()
13101367
}
13111368

13121369

tests/testthat/test-utilities.R

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,91 @@ test_that("get_count_datasets works", {
247247
expect_equal(cds$mat2, seq(10, 18))
248248
expect_equal(cds$mat3, seq(19, 27))
249249

250+
# SE does not have dimnames, one assay has duplicated colnames, one has no colnames
251+
se1 <- se
252+
rownames(se1) <- colnames(se1) <- NULL
253+
colnames(assay(se1, "mat1", withDimnames = FALSE))[2] <-
254+
colnames(assay(se1, "mat1", withDimnames = FALSE))[1]
255+
colnames(assay(se1, "mat2", withDimnames = FALSE)) <- NULL
256+
expect_equal(colnames(assay(se1, "mat1", withDimnames = FALSE)), paste0("S", c(1, 1, 3)))
257+
expect_equal(rownames(assay(se1, "mat1", withDimnames = FALSE)), paste0("G", seq_len(3)))
258+
expect_null(colnames(assay(se1, "mat2", withDimnames = FALSE)))
259+
expect_equal(colnames(assay(se1, "mat3", withDimnames = FALSE)), paste0("S", seq_len(3)))
260+
expect_null(colnames(se1))
261+
expect_null(rownames(se1))
262+
expect_error(cds <- get_count_datasets(se1), "some column names are duplicated")
263+
264+
# SE does not have dimnames, one assay has duplicated rownames, one has no rownames
265+
se1 <- se
266+
rownames(se1) <- colnames(se1) <- NULL
267+
rownames(assay(se1, "mat1", withDimnames = FALSE))[2:3] <-
268+
rownames(assay(se1, "mat1", withDimnames = FALSE))[1]
269+
rownames(assay(se1, "mat2", withDimnames = FALSE)) <- NULL
270+
expect_equal(rownames(assay(se1, "mat1", withDimnames = FALSE)), paste0("G", c(1, 1, 1)))
271+
expect_equal(colnames(assay(se1, "mat1", withDimnames = FALSE)), paste0("S", seq_len(3)))
272+
expect_null(rownames(assay(se1, "mat2", withDimnames = FALSE)))
273+
expect_equal(rownames(assay(se1, "mat3", withDimnames = FALSE)), paste0("G", seq_len(3)))
274+
expect_null(colnames(se1))
275+
expect_null(rownames(se1))
276+
expect_error(cds <- get_count_datasets(se1), "some row names are duplicated")
277+
278+
# SE has duplicated colnames
279+
se1 <- se
280+
colnames(se1) <- paste0("S", c(1, 1, 1))
281+
expect_error(cds <- get_count_datasets(se1), "some column names are duplicated")
282+
expect_true(check_if_any_dimnames_duplicated(se1, dim = "cols"))
283+
expect_false(check_if_any_dimnames_duplicated(se1, dim = "rows"))
284+
285+
# SE has duplicated rownames
286+
se1 <- se
287+
rownames(se1) <- paste0("G", c(1, 2, 1))
288+
expect_error(cds <- get_count_datasets(se1), "some row names are duplicated")
289+
expect_false(check_if_any_dimnames_duplicated(se1, dim = "cols"))
290+
expect_true(check_if_any_dimnames_duplicated(se1, dim = "rows"))
291+
292+
# All assays + SE have duplicated colnames
293+
se1 <- se
294+
colnames(se1)[2] <-
295+
colnames(assay(se1, "mat1", withDimnames = FALSE))[2] <-
296+
colnames(assay(se1, "mat2", withDimnames = FALSE))[2] <-
297+
colnames(assay(se1, "mat3", withDimnames = FALSE))[2] <- "S1"
298+
expect_true(check_if_any_dimnames_duplicated(se1, dim = "cols"))
299+
expect_false(check_if_any_dimnames_duplicated(se1, dim = "rows"))
300+
expect_false(check_if_assays_are_NOT_overlapped(se1, dim = "cols"))
301+
expect_false(check_if_assays_are_NOT_overlapped(se1, dim = "rows"))
302+
303+
# Two assays + SE have duplicated colnames
304+
se1 <- se
305+
colnames(se1)[2] <-
306+
colnames(assay(se1, "mat1", withDimnames = FALSE))[2] <-
307+
colnames(assay(se1, "mat3", withDimnames = FALSE))[2] <- "S1"
308+
expect_true(check_if_any_dimnames_duplicated(se1, dim = "cols"))
309+
expect_false(check_if_any_dimnames_duplicated(se1, dim = "rows"))
310+
expect_true(check_if_assays_are_NOT_overlapped(se1, dim = "cols"))
311+
expect_false(check_if_assays_are_NOT_overlapped(se1, dim = "rows"))
312+
313+
# Assays have duplicated colnames in different ways
314+
se1 <- se
315+
assay(se1, "mat2") <- NULL
316+
colnames(assay(se1, "mat1", withDimnames = FALSE)) <- c("S1", "S1", "S2")
317+
colnames(assay(se1, "mat3", withDimnames = FALSE)) <- c("S1", "S2", "S2")
318+
expect_true(check_if_any_dimnames_duplicated(se1, dim = "cols"))
319+
expect_false(check_if_any_dimnames_duplicated(se1, dim = "rows"))
320+
expect_true(check_if_assays_are_NOT_overlapped(se1, dim = "cols"))
321+
expect_false(check_if_assays_are_NOT_overlapped(se1, dim = "rows"))
322+
323+
# All dimnames are NULL - not duplicated
324+
se1 <- se
325+
rownames(se1) <- colnames(se1) <- NULL
326+
rownames(assay(se1, "mat1", withDimnames = FALSE)) <-
327+
colnames(assay(se1, "mat1", withDimnames = FALSE)) <-
328+
rownames(assay(se1, "mat2", withDimnames = FALSE)) <-
329+
colnames(assay(se1, "mat2", withDimnames = FALSE)) <-
330+
rownames(assay(se1, "mat3", withDimnames = FALSE)) <-
331+
colnames(assay(se1, "mat3", withDimnames = FALSE)) <- NULL
332+
expect_false(check_if_any_dimnames_duplicated(se1, dim = "cols"))
333+
expect_false(check_if_any_dimnames_duplicated(se1, dim = "rows"))
334+
250335
# Unnamed assay(s)
251336
# se1 <- SummarizedExperiment::SummarizedExperiment(
252337
# assays = list(

0 commit comments

Comments
 (0)