Skip to content

Commit

Permalink
[r] Fix Seurat ingestion with missing command logs collection (#2154)
Browse files Browse the repository at this point in the history
* [r] Fix Seurat ingestion with missing command logs collection
Fix a bug in Seurat ingestion where `exp/uns` is present but
`exp/uns/seurat_commands` is not

fixes #2153

* Add tests

* Expand tests

* Bump develop version
  • Loading branch information
mojaveazure committed Feb 20, 2024
1 parent db548eb commit 6c737ae
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 7 deletions.
2 changes: 1 addition & 1 deletion apis/r/DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ Description: Interface for working with 'TileDB'-based Stack of Matrices,
like those commonly used for single cell data analysis. It is documented at
<https://github.com/single-cell-data>; a formal specification available is at
<https://github.com/single-cell-data/SOMA/blob/main/abstract_specification.md>.
Version: 1.7.99.2
Version: 1.7.99.3
Authors@R: c(
person(given = "Aaron", family = "Wolen",
role = c("cre", "aut"), email = "[email protected]",
Expand Down
6 changes: 2 additions & 4 deletions apis/r/R/SOMAExperimentAxisQuery.R
Original file line number Diff line number Diff line change
Expand Up @@ -546,10 +546,8 @@ SOMAExperimentAxisQuery <- R6::R6Class(
if (inherits(uns, 'SOMACollection')) {
cmds <- tryCatch(
.load_seurat_command(uns, ms_names = private$.measurement_name),
packageCheckError = function(err) {
warning(conditionMessage(err), call. = FALSE, immediate. = TRUE)
return(NULL)
}
packageCheckError = .err_to_warn,
missingCollectionError = .err_to_warn
)
for (i in names(cmds)) {
object[[i]] <- cmds[[i]]
Expand Down
8 changes: 6 additions & 2 deletions apis/r/R/utils-seurat.R
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,15 @@
check_package('SeuratObject', version = .MINIMUM_SEURAT_VERSION())
stopifnot(
"'uns' must be a SOMACollection" = inherits(uns, what = 'SOMACollection'),
"Cannot find a SOMACollection with command logs in 'uns'" = key %in% uns$names() &&
inherits(logs <- uns$get(key), what = 'SOMACollection'),
"'ms_names' must be a character vector with no empty strings" = is.character(ms_names) &&
all(nzchar(ms_names))
)
if (!(key %in% uns$names() && inherits(logs <- uns$get(key), what = 'SOMACollection'))) {
stop(errorCondition(
"Cannot find a SOMACollection with command logs in 'uns'",
class = c("noCommandLogsError", "missingCollectionError")
))
}
slots <- slotNames(getClassDef('SeuratCommand', package = 'SeuratObject'))
hint <- uns_hint('1d')
lognames <- logs$names()
Expand Down
4 changes: 4 additions & 0 deletions apis/r/R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,10 @@ uns_hint <- function(type = c('1d', '2d')) {
))
}

.err_to_warn <- function(err, immediate. = TRUE) {
warning(conditionMessage(err), call. = FALSE, immediate. = immediate.)
}

.decode_from_char <- function(x) {
stopifnot(is.character(x))
double <- paste0(
Expand Down
31 changes: 31 additions & 0 deletions apis/r/tests/testthat/test-SeuratOutgest-command.R
Original file line number Diff line number Diff line change
Expand Up @@ -103,3 +103,34 @@ test_that("Loading SeuratCommands works from experiment queries", {
sort(SeuratObject::Command(pbmc_small))
)
})

test_that("Load SeuratCommand with missing commands", {
skip_if(!extended_tests())
skip_if_not_installed('SeuratObject', .MINIMUM_SEURAT_VERSION('c'))
skip_if_not_installed('jsonlite')


pbmc_small <- get_data('pbmc_small', package = 'SeuratObject')
slot(pbmc_small, "commands") <- list()
expect_true(validObject(pbmc_small))
uri <- write_soma(pbmc_small, uri = withr::local_tempdir('missing-commands'))

expect_no_condition(exp <- SOMAExperimentOpen(uri))
on.exit(exp$close(), add = TRUE)
expect_true('uns' %in% exp$names())
expect_s3_class(uns <- exp$get('uns'), 'SOMACollection')
expect_false('seurat_commands' %in% uns$names())

expect_s3_class(
query <- SOMAExperimentAxisQuery$new(exp, SeuratObject::DefaultAssay(pbmc_small)),
'SOMAExperimentAxisQuery'
)

expect_warning(
obj <- query$to_seurat(X_layers = c('data' = 'data')),
regexp = "^Cannot find a SOMACollection with command logs in 'uns'$"
)
expect_s4_class(obj, 'Seurat')
expect_true(validObject(obj))
expect_length(SeuratObject::Command(obj), 0L)
})

0 comments on commit 6c737ae

Please sign in to comment.