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

Use AddMetaData #241

Merged
merged 2 commits into from
Nov 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ Suggests:
scran,
scpcaData,
sessioninfo,
Seurat (< 5.0.0),
Seurat,
testthat (>= 3.1.9),
zellkonverter
Remotes:
Expand Down
6 changes: 3 additions & 3 deletions R/sce_to_seurat.R
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ sce_to_seurat <- function(sce,
)

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

# grab names of altExp, if any
Expand All @@ -78,7 +78,7 @@ sce_to_seurat <- function(sce,
# round counts and calculate total counts
alt_counts <- round(counts(altExp(sce, name)))

# subset altExp counts and seurat object to shared cells
# subset altExp counts and Seurat object to shared cells
cells_to_keep <- intersect(seurat_obj_cells, colnames(alt_counts))
alt_counts <- alt_counts[, cells_to_keep]

Expand All @@ -88,7 +88,7 @@ sce_to_seurat <- function(sce,
# add new assay along with associated row data, if any
seurat_obj[[name]] <- Seurat::CreateAssayObject(counts = alt_counts)
rowdata <- as.data.frame(rowData(altExp(sce, name)))
seurat_obj[[name]]@var.features <- rowdata
seurat_obj[[name]] <- Seurat::AddMetaData(seurat_obj[[name]], rowdata)

# check that altExp data is present as a new assay, since Seurat sometimes fails without warning
if (is.null(seurat_obj[[name]])) {
Expand Down
20 changes: 10 additions & 10 deletions tests/testthat/test-sce_to_seurat.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ sce <- sim_sce(n_cells = 100, n_genes = 200, n_empty = 0)
colData(sce) <- data.frame(
"test_column" = sample(0:10, 100, rep = TRUE),
"barcodes" = colnames(sce)
) %>%
tibble::column_to_rownames("barcodes") %>%
) |>
tibble::column_to_rownames("barcodes") |>
DataFrame()
rowData(sce) <- data.frame(
"test_row" = sample(0:10, 200, rep = TRUE),
"gene_id" = rownames(sce)
) %>%
tibble::column_to_rownames("gene_id") %>%
) |>
tibble::column_to_rownames("gene_id") |>
DataFrame()
metadata(sce)$test <- "test"
alt_sce <- sim_sce(n_cells = 100, n_genes = 10, n_empty = 0)
Expand All @@ -22,8 +22,8 @@ sce <- merge_altexp(sce, alt_sce, alt_name)
rowData(altExp(sce, alt_name)) <- data.frame(
"alt_test_row" = sample(0:5, 10, rep = TRUE),
"alt_id" = rownames(altExp(sce, alt_name))
) %>%
tibble::column_to_rownames("alt_id") %>%
) |>
tibble::column_to_rownames("alt_id") |>
DataFrame()

logcounts(sce) <- counts(sce) # dummy logcounts to test the assay_name argument
Expand All @@ -42,13 +42,13 @@ 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([email protected])))
expect_equal(rowdata_sce, seurat_object[["RNA"]]@var.features) # since default "counts" is used, RNA name is expected here
expect_equal(rowdata_sce, seurat_object[["RNA"]][[]]) # 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)
expect_equal(alt_rowdata_sce, seurat_object[[alt_name]][[]])
})


Expand All @@ -66,11 +66,11 @@ test_that("Converting SCE to Seurat objects works as expected for a non-default

# can't directly check that coldata is equal because of added columns in seurat object
expect_true(all(colnames(coldata_sce) %in% colnames([email protected])))
expect_equal(rowdata_sce, seurat_object[["logcounts"]]@var.features)
expect_equal(rowdata_sce, seurat_object[["logcounts"]][[]])
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)
expect_equal(alt_rowdata_sce, seurat_object[[alt_name]][[]])
})
Loading