Skip to content

Commit 157c3a7

Browse files
committed
check_if_assays_are_NOT_overlapped not to raise error if names are duplicated but identical
1 parent d2c49ac commit 157c3a7

File tree

2 files changed

+44
-5
lines changed

2 files changed

+44
-5
lines changed

R/utilities.R

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1315,7 +1315,7 @@ check_if_any_dimnames_duplicated <- function(se, dim = "cols") {
13151315
not()
13161316

13171317
# Check SE object
1318-
se_check <- !is.null(dimnames_function(se)) &&
1318+
se_check <- !is.null(dimnames_function(se)) &&
13191319
nbr_unique_dimnames_function(se) != length_function(se)
13201320

13211321
# Return TRUE if either of the two checks return TRUE
@@ -1331,6 +1331,7 @@ check_if_assays_are_NOT_overlapped <- function(se, dim = "cols") {
13311331
dimnames_function <- colnames
13321332
length_function <- ncol
13331333
}
1334+
is_identical_for_reduce <- function(x,y) if (identical(x,y)) x else FALSE
13341335

13351336
# If I have any assay at all
13361337
assays(se) |> length() |> gt(0) &&
@@ -1347,11 +1348,14 @@ check_if_assays_are_NOT_overlapped <- function(se, dim = "cols") {
13471348

13481349
# If I have lack of consistency
13491350
# This will be TRUE also if some assays have dimnames and other don't
1351+
# For each assay, sort the dimnames, then check that they are all the
1352+
# same. Can't check for unique length, since some names may be repeated
13501353
assays(se, withDimnames = FALSE) |>
13511354
as.list() |>
13521355
map(dimnames_function) |>
1353-
reduce(intersect) |>
1354-
length() |>
1356+
map(sort) |>
1357+
reduce(is_identical_for_reduce) |>
1358+
length() |>
13551359
equals(length_function(se)) |>
13561360
not()
13571361
}

tests/testthat/test-utilities.R

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -275,15 +275,50 @@ test_that("get_count_datasets works", {
275275
expect_null(rownames(se1))
276276
expect_error(cds <- get_count_datasets(se1), "some row names are duplicated")
277277

278-
# SE has duplicated colname
278+
# SE has duplicated colnames
279279
se1 <- se
280280
colnames(se1) <- paste0("S", c(1, 1, 1))
281281
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"))
282284

283-
# SE has duplicated rowname
285+
# SE has duplicated rownames
284286
se1 <- se
285287
rownames(se1) <- paste0("G", c(1, 2, 1))
286288
expect_error(cds <- get_count_datasets(se1), "some row names are duplicated")
289+
expect_true(check_if_any_dimnames_duplicated(se1, dim = "cols"))
290+
expect_false(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"))
287322

288323
# All dimnames are NULL - not duplicated
289324
se1 <- se

0 commit comments

Comments
 (0)