From 2b1fe590cae5608b96935aa474171b7535221f10 Mon Sep 17 00:00:00 2001 From: Alex Pickering Date: Wed, 22 May 2024 14:48:37 -0700 Subject: [PATCH] fix v5 import --- R/seurat_to_sce.R | 13 ++- tests/testthat/test_import_scseq.R | 137 +++++++++++++++++++++++++++-- 2 files changed, 139 insertions(+), 11 deletions(-) diff --git a/R/seurat_to_sce.R b/R/seurat_to_sce.R index 86b14ae0..4c965a87 100644 --- a/R/seurat_to_sce.R +++ b/R/seurat_to_sce.R @@ -5,8 +5,13 @@ seurat_to_sce <- function(sdata, dataset_name) { Seurat::DefaultAssay(sdata) <- 'RNA' # join layers if v5 - if (methods::is(sdata[['RNA']], 'Assay5')) { + # use meta.features (v3) or meta.data (v5) + meta_name <- 'meta.features' + is.v5 <- methods::is(sdata[['RNA']], 'Assay5') + + if (is.v5) { sdata[['RNA']] <- SeuratObject::JoinLayers(sdata[['RNA']]) + meta_name <- 'meta.data' } # normalize if need to @@ -20,7 +25,7 @@ seurat_to_sce <- function(sdata, dataset_name) { # meta.features need to have same row.names as assay for (assay_name in Seurat::Assays(sdata)) { assay <- sdata[[assay_name]] - assay@meta.features <- assay@meta.features[row.names(assay), ] + slot(assay, meta_name) <- slot(assay, meta_name)[row.names(assay), ] sdata[[assay_name]] <- assay } @@ -51,14 +56,14 @@ seurat_to_sce <- function(sdata, dataset_name) { sce$cluster <- unname(Seurat::Idents(sdata)) # default HVGs unless integrated assay - hvgs <- sdata[['RNA']]@var.features + hvgs <- SeuratObject::VariableFeatures(sdata, assay = 'RNA') is.integrated <- have.samples | have.integrated if (is.integrated) { # get HVGs and transfer reductions from integrated assay if present if (have.integrated) { - hvgs <- sdata[['integrated']]@var.features + hvgs <- SeuratObject::VariableFeatures(sdata, assay = 'integrated') SingleCellExperiment::reducedDims(sce) <- SingleCellExperiment::reducedDims(SingleCellExperiment::altExp(sce, 'integrated')) } diff --git a/tests/testthat/test_import_scseq.R b/tests/testthat/test_import_scseq.R index a73c92d8..a0ac0f35 100644 --- a/tests/testthat/test_import_scseq.R +++ b/tests/testthat/test_import_scseq.R @@ -89,13 +89,6 @@ test_that("cellranger .h5 files with multiple assays can be imported", { tx2gene_dir <- file.path(tempdir(), 'tx2gene') dir.create(tx2gene_dir) - mock_uploaded_data(dataset_name, - sc_dir, - uploaded_data_dir, - type = 'h5', - version = '3', - gene.type = c(rep('Gene Expression', 199), 'Antibody Capture')) - expect_error( suppressWarnings(import_scseq(dataset_name, uploaded_data_dir, sc_dir, tx2gene_dir)), NA @@ -104,3 +97,133 @@ test_that("cellranger .h5 files with multiple assays can be imported", { # cleanup unlink(c(sc_dir, uploaded_data_dir, tx2gene_dir), recursive = TRUE) }) + + +test_that("Seurat files with RNA Assay5 can be imported", { + # setup + dataset_name <- 'test' + sc_dir <- file.path(tempdir(), 'single-cell') + dir.create(sc_dir) + uploaded_data_dir <- file.path(tempdir(), 'uploads') + dir.create(uploaded_data_dir) + + tx2gene_dir <- file.path(tempdir(), 'tx2gene') + dir.create(tx2gene_dir) + + # create Seurat object + pbmc_raw <- read.table( + file = system.file("extdata", "pbmc_raw.txt", package = "Seurat"), + as.is = TRUE + ) + + # can upload non-split Assay5 + pbmc_raw <- as(as.matrix(pbmc_raw), 'dgCMatrix') + scdata <- Seurat::CreateSeuratObject(counts = pbmc_raw) + expect_true(methods::is(scdata[['RNA']], 'Assay5')) + + + qs::qsave(scdata, file.path(uploaded_data_dir, 'scdata.qs')) + + expect_error( + suppressWarnings(import_scseq( + dataset_name, + species = 'Homo sapiens', + uploaded_data_dir, + sc_dir, + tx2gene_dir, + metrics = NULL)), + NA + ) + + # cleanup + unlink(c(sc_dir, uploaded_data_dir, tx2gene_dir), recursive = TRUE) +}) + +test_that("Seurat files with split RNA Assay5 can be imported", { + # setup + dataset_name <- 'test' + sc_dir <- file.path(tempdir(), 'single-cell') + dir.create(sc_dir) + uploaded_data_dir <- file.path(tempdir(), 'uploads') + dir.create(uploaded_data_dir) + + tx2gene_dir <- file.path(tempdir(), 'tx2gene') + dir.create(tx2gene_dir) + + # create Seurat object + pbmc_raw <- read.table( + file = system.file("extdata", "pbmc_raw.txt", package = "Seurat"), + as.is = TRUE + ) + + # can upload non-split Assay5 + pbmc_raw <- as(as.matrix(pbmc_raw), 'dgCMatrix') + scdata <- Seurat::CreateSeuratObject(counts = pbmc_raw) + expect_true(methods::is(scdata[['RNA']], 'Assay5')) + + scdata$batch <- c('a', 'b', 'c') + scdata[["RNA"]] <- split(scdata[["RNA"]], f = scdata$batch) + + expect_equal(SeuratObject::Layers(scdata), c("counts.a", "counts.b", "counts.c")) + + + qs::qsave(scdata, file.path(uploaded_data_dir, 'scdata.qs')) + + expect_error( + suppressWarnings(import_scseq( + dataset_name, + species = 'Homo sapiens', + uploaded_data_dir, + sc_dir, + tx2gene_dir, + metrics = NULL)), + NA + ) + + # cleanup + unlink(c(sc_dir, uploaded_data_dir, tx2gene_dir), recursive = TRUE) +}) + + +test_that("Seurat object with RNA Assay v3 can be imported", { + + options(Seurat.object.assay.version = "v3") + # setup + dataset_name <- 'test' + sc_dir <- file.path(tempdir(), 'single-cell') + dir.create(sc_dir) + uploaded_data_dir <- file.path(tempdir(), 'uploads') + dir.create(uploaded_data_dir) + + tx2gene_dir <- file.path(tempdir(), 'tx2gene') + dir.create(tx2gene_dir) + + # create Seurat object + pbmc_raw <- read.table( + file = system.file("extdata", "pbmc_raw.txt", package = "Seurat"), + as.is = TRUE + ) + + # can upload Assay + pbmc_raw <- as(as.matrix(pbmc_raw), 'dgCMatrix') + scdata <- Seurat::CreateSeuratObject(counts = pbmc_raw) + expect_true(methods::is(scdata[['RNA']], 'Assay')) + + + qs::qsave(scdata, file.path(uploaded_data_dir, 'scdata.qs')) + + expect_error( + suppressWarnings(import_scseq( + dataset_name, + species = 'Homo sapiens', + uploaded_data_dir, + sc_dir, + tx2gene_dir, + metrics = NULL)), + NA + ) + + # cleanup + options(Seurat.object.assay.version = "v5") + unlink(c(sc_dir, uploaded_data_dir, tx2gene_dir), recursive = TRUE) +})