Skip to content

Commit

Permalink
Merge pull request #56 from TileDB-Inc/aaronwolen/sc-17824/support-st…
Browse files Browse the repository at this point in the history
…orage-of-seurat-cell-identities

Support storage of Seurat cell identities
  • Loading branch information
aaronwolen authored May 20, 2022
2 parents d577e50 + ca1ac08 commit d97de36
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 11 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ Description: Store and retrieve single cell data using TileDB and the on-disk
format proposed in the Unified Single Cell Data Model and API. Users can
import from and export to in-memory formats used by popular toolchains like
Seurat and Bioconductor SingleCellExperiment.
Version: 0.1.0.9009
Version: 0.1.0.9010
Authors@R: c(
person(given = "Aaron",
family = "Wolen",
Expand Down
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ importFrom(SeuratObject,CreateSeuratObject)
importFrom(SeuratObject,DefaultAssay)
importFrom(SeuratObject,Embeddings)
importFrom(SeuratObject,GetAssayData)
importFrom(SeuratObject,Idents)
importFrom(SeuratObject,Loadings)
importFrom(SeuratObject,Reductions)
importFrom(SeuratObject,SetAssayData)
Expand Down
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,4 @@ See [TileDB 2.8 release notes](https://github.com/TileDB-Inc/TileDB/releases/tag
* New internal `TileDBURI` class for handling various URI formats
* The `uri` field for all TileDB(Array|Group)-based classes is now an active binding that retrieves the URI from the private `tiledb_uri` field
* Several default parameters have been changed to store the the `X`, `obs`, and `var` arrays more efficiently on disk (#50)
* Seurat cell identities are now stored in the `active_ident` attribute of the `obs` array (#56)
25 changes: 24 additions & 1 deletion R/SCDataset.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#' @description
#' Class for representing a sc_dataset, which may contain of one or more
#' [`SCGroup`]s.
#' @importFrom SeuratObject CreateSeuratObject Reductions
#' @importFrom SeuratObject CreateSeuratObject Reductions Idents
#' @export
SCDataset <- R6::R6Class(
classname = "SCDataset",
Expand Down Expand Up @@ -56,6 +56,12 @@ SCDataset <- R6::R6Class(
#' a nested TileDB group with a URI of `./scgroup_<assay>` where `<assay>`
#' is the name of the Seurat assay.
#'
#' ## Identities
#'
#' Cell identities in the [`SeuratObject::Seurat`] are maintained by
#' creating an `active_ident` attribute in `obs` that stores the factor
#' levels as a character vector.
#
#' ## Dimensionality Reductions
#'
#' Dimensionality reduction results are stored as `obsm` and `varm` arrays
Expand All @@ -68,6 +74,15 @@ SCDataset <- R6::R6Class(
from_seurat = function(object) {
stopifnot(inherits(object, "Seurat"))

idents <- SeuratObject::Idents(object)
if (nlevels(idents) > 1L) {
object <- SeuratObject::AddMetaData(
object = object,
metadata = as.character(idents),
col.name = "active_ident"
)
}

assays <- SeuratObject::Assays(object)
for (assay in assays) {
if (is.null(self$members[[assay]])) {
Expand Down Expand Up @@ -139,12 +154,20 @@ SCDataset <- R6::R6Class(
# just take the first scgroup's obs metadata
obs_df <- self$scgroups[[1]]$obs$to_dataframe()

# retain cell identities before restoring cell-level metadata
idents <- setNames(obs_df$active_ident, rownames(obs_df))
obs_df$active_ident <- NULL

object <- SeuratObject::CreateSeuratObject(
counts = assays[[1]],
project = project,
meta.data = obs_df
)

if (!is.null(idents)) {
SeuratObject::Idents(object) <- idents[SeuratObject::Cells(object)]
}

if (nassays > 1) {
for (i in seq(2, nassays)) {
assay <- names(assays)[i]
Expand Down
7 changes: 7 additions & 0 deletions man/SCDataset.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 9 additions & 9 deletions tests/testthat/test_SCDataset_Seurat.R
Original file line number Diff line number Diff line change
@@ -1,13 +1,5 @@
setup({
tdb_uri <<- file.path(tempdir(), "test-scdata")
})

teardown({
tiledb::tiledb_vfs_remove_dir(tdb_uri)
})


test_that("SCDataset can be created from a Seurat object", {
tdb_uri <- withr::local_tempdir("test-scdataset")
scdataset <- SCDataset$new(uri = tdb_uri, verbose = TRUE)
expect_true(inherits(scdataset, "SCDataset"))

Expand Down Expand Up @@ -67,6 +59,14 @@ test_that("SCDataset can be created from a Seurat object", {
expect_identical(embed2[rownames(embed1), ], embed1)
}

# check for cell identities
# factors are stored in tiledb as character vectors
obs_ids <- SeuratObject::Cells(pbmc_small)
expect_equal(
as.character(SeuratObject::Idents(pbmc_small2)[obs_ids]),
as.character(SeuratObject::Idents(pbmc_small)[obs_ids])
)

expect_identical(
SeuratObject::Graphs(pbmc_small2),
SeuratObject::Graphs(pbmc_small)
Expand Down

0 comments on commit d97de36

Please sign in to comment.