Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update sce_to_seurat function #123

Merged
merged 10 commits into from
Sep 1, 2022
8 changes: 6 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,19 @@ Authors@R: c(
person(c("Joshua", "A."), "Shapiro",
email = "josh.shapiro@ccdatalab.org",
comment = list(ORCID = "0000-0002-6224-0347"),
role = c("aut"))
role = c("aut")),
person(c("Stephanie", "J."), "Spielman",
email = "stephanie.spielman@ccdatalab.org",
comment = list(ORCID = "0000-0002-9090-4788"),
role = c("aut"))
)
Maintainer: Ally Hawkins <ally.hawkins@ccdatalab.org>
Description: Tools for processing single cell data associated with the
Alex's Lemonade Stand Foundation Single Cell Pediatric Cancer Atlas.
License: BSD_3_clause + file LICENSE
Encoding: UTF-8
LazyData: true
RoxygenNote: 7.2.0
RoxygenNote: 7.2.1
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Josh and I have been going back and forth on committing and removing this change because for some reason we have different versions but can't figure out why.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Happy to join in the party!

Depends:
R (>= 4.1.0)
Imports:
Expand Down
25 changes: 20 additions & 5 deletions R/sce_to_seurat.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
#' Convert SingleCellExperiment object to Seurat object
#'
#' @param sce SingleCellExperiment object
#' @param assay_name The assay name (default "counts") to include in
#' the Seurat object. This name will be applied as the assay name in
#' the Seurat object. If the default "count" assay is used, then the
#' assay name will instead be "RNA".
#'
#' @return Seurat object
#'
Expand All @@ -13,7 +17,8 @@
#' \dontrun{
#' sce_to_seurat(sce = sce_object)
#' }
sce_to_seurat <- function(sce){
sce_to_seurat <- function(sce,
assay_name = "counts") {

if (!requireNamespace("Seurat", quietly = TRUE)) {
stop("The Seurat package must be installed to create a Seurat object. No output returned.")
Expand All @@ -23,6 +28,10 @@ sce_to_seurat <- function(sce){
stop("Input must be a SingleCellExperiment object.")
}

if (!assay_name %in% assayNames(sce)) {
stop("Provided assay name is not present in the SingleCellExperiment object.")
}

# remove miQC model from metadata
if(!is.null(metadata(sce)$miQC_model)){
metadata(sce)$miQC_model <- NULL
Expand All @@ -31,7 +40,8 @@ sce_to_seurat <- function(sce){


# Seurat counts need to be integers
sce_counts <- round(counts(sce))
# extract the given `assay_name`
sce_counts <- round(assay(sce, assay_name))

# Seurat will not like zero count calls
sce_sum <- Matrix::colSums(sce_counts)
Expand All @@ -43,12 +53,17 @@ sce_to_seurat <- function(sce){
rowdata <- as.data.frame(rowData(sce))


# create seurat object
# create seurat object, with new assay name
# use "RNA" as assay name if `counts` is being used from SCE
if (assay_name == "counts") {
assay_name <- "RNA"
}
seurat_obj <- Seurat::CreateSeuratObject(counts = sce_counts,
meta.data = coldata)
meta.data = coldata,
assay = assay_name)

# add rowdata and metadata separately adding it while creating leaves the slots empty without warning
seurat_obj[["RNA"]]@var.features <- rowdata
seurat_obj[[assay_name]]@var.features <- rowdata
seurat_obj@misc <- metadata(sce)

# grab names of altExp, if any
Expand Down
2 changes: 2 additions & 0 deletions man/sce_to_anndata.Rd

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

5 changes: 4 additions & 1 deletion man/sce_to_seurat.Rd

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

3 changes: 3 additions & 0 deletions tests/testthat/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
_snaps/
test_anndata.h5

29 changes: 28 additions & 1 deletion tests/testthat/test-sce_to_seurat.R
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ rowData(altExp(sce, alt_name)) <- data.frame("alt_test_row" = sample(0:5, 10, re
tibble::column_to_rownames("alt_id") %>%
DataFrame()

logcounts(sce) <- counts(sce) # dummy logcounts to test the assay_name argument

test_that("Converting SCE to Seurat objects works as expected", {

seurat_object <- sce_to_seurat(sce)
Expand All @@ -34,7 +36,32 @@ test_that("Converting SCE to Seurat objects works as expected", {

# can't directly check that coldata is equal because of added columns in seurat object
expect_true(all(colnames(coldata_sce) %in% colnames(seurat_object@meta.data)))
expect_equal(rowdata_sce, seurat_object[["RNA"]]@var.features)
expect_equal(rowdata_sce, seurat_object[["RNA"]]@var.features) # since default "counts" is used, RNA name is expected here
expect_equal(metadata(sce), seurat_object@misc)

# check that altExp data was converted and rowData
expect_s4_class(seurat_object[[alt_name]], 'Assay')
alt_rowdata_sce <- as.data.frame(rowData(altExp(sce, alt_name)))
expect_equal(alt_rowdata_sce, seurat_object[[alt_name]]@var.features)

})


test_that("Converting SCE to Seurat objects works as expected for custom assay name", {

seurat_object <- sce_to_seurat(sce, assay_name = "logcounts")

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I would also include an explicit check for the assay name being logcounts here? I don't know that you need the rest of the checks but they can't hurt to have since they should all pass if it's converted successfully.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in 6dd5790!

# check that column names of Seurat object are derived from SCE object
# they won't necessarily be equal if some cells with 0 counts were removed
expect_true(all(colnames(seurat_object) %in% colnames(sce)))
sjspielman marked this conversation as resolved.
Show resolved Hide resolved

# check that attached metadata/coldata/rowdata in SCE are present in seurat object
coldata_sce <- as.data.frame(colData(sce))
rowdata_sce <- as.data.frame(rowData(sce))

# can't directly check that coldata is equal because of added columns in seurat object
expect_true(all(colnames(coldata_sce) %in% colnames(seurat_object@meta.data)))
expect_equal(rowdata_sce, seurat_object[["logcounts"]]@var.features) # since default "counts" is used, RNA name is expected here
expect_equal(metadata(sce), seurat_object@misc)

# check that altExp data was converted and rowData
Expand Down