Skip to content

Commit

Permalink
[r] get_seurat: allow selection of Seurat counts or data slot (#607)
Browse files Browse the repository at this point in the history
Closes #596
  • Loading branch information
mlin authored Jul 9, 2023
1 parent 5ac97a0 commit e6c32ed
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 4 deletions.
9 changes: 5 additions & 4 deletions api/r/cellxgene.census/R/get_helpers.R
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ get_presence_matrix <- function(census, organism, measurement_name = "RNA") {
#' @param census The census object, usually returned by `cellxgene.census::open_soma()`.
#' @param organism The organism to query, usually one of `Homo sapiens` or `Mus musculus`
#' @param measurement_name The measurement object to query. Defaults to `RNA`.
#' @param X_name The `X` layer to query. Defaults to `raw`.
#' @param X_layers A named character of `X` layers to add to the Seurat assay, where the names are
#' the names of Seurat slots (`counts` or `data`) and the values are the names of layers
#' within `X`.
#' @param obs_value_filter A SOMA `value_filter` across columns in the `obs` dataframe, expressed as string.
#' @param obs_coords A set of coordinates on the obs dataframe index, expressed in any type or format supported by SOMADataFrame's read() method.
#' @param obs_column_names Columns to fetch for the `obs` data frame.
Expand All @@ -39,7 +41,7 @@ get_seurat <- function(
census,
organism,
measurement_name = "RNA",
X_name = "raw",
X_layers = c(counts = "raw", data = NULL),
obs_value_filter = NULL,
obs_coords = NULL,
obs_column_names = NULL,
Expand All @@ -53,8 +55,7 @@ get_seurat <- function(
var_query = tiledbsoma::SOMAAxisQuery$new(value_filter = var_value_filter, coords = var_coords)
)
return(expt_query$to_seurat(
# TODO: should we allow selection of the seurat 'counts' or 'data' slot?
c(counts = X_name),
X_layers = X_layers,
obs_column_names = obs_column_names,
var_column_names = var_column_names
))
Expand Down
30 changes: 30 additions & 0 deletions api/r/cellxgene.census/tests/testthat/test-get_helpers.R
Original file line number Diff line number Diff line change
Expand Up @@ -103,3 +103,33 @@ test_that("get_seurat allows missing obs or var filter", {
expect_equal(ncol(seurat[["RNA"]]), 10001)
expect_equal(nrow(seurat[["RNA"]]), 1)
})

test_that("get_seurat X_layers allows slot selection", {
census <- open_soma_latest_for_test()
on.exit(census$close(), add = TRUE)

# default: raw X layer gets loaded into counts (which Seurat implicitly copies into data)
seurat <- get_seurat(
census,
"Mus musculus",
obs_value_filter = "tissue_general == 'vasculature'",
obs_column_names = c("soma_joinid", "cell_type", "tissue", "tissue_general", "assay"),
var_value_filter = "feature_name %in% c('Gm53058', '0610010K14Rik')",
var_column_names = c("soma_joinid", "feature_id", "feature_name", "feature_length")
)
expect_equal(nrow(seurat$RNA@counts), 2)
expect_equal(nrow(seurat$RNA@data), 2)

# expressly load raw X data into data; then counts is empty
seurat <- get_seurat(
census,
"Mus musculus",
obs_value_filter = "tissue_general == 'vasculature'",
obs_column_names = c("soma_joinid", "cell_type", "tissue", "tissue_general", "assay"),
var_value_filter = "feature_name %in% c('Gm53058', '0610010K14Rik')",
var_column_names = c("soma_joinid", "feature_id", "feature_name", "feature_length"),
X_layers = c(counts = NULL, data = "raw")
)
expect_equal(nrow(seurat$RNA@counts), 0)
expect_equal(nrow(seurat$RNA@data), 2)
})

0 comments on commit e6c32ed

Please sign in to comment.