From 80f0703727d78dccdb5e63efa3fb646c4bd067c2 Mon Sep 17 00:00:00 2001 From: Joshua Shapiro Date: Mon, 9 Dec 2024 10:41:43 -0500 Subject: [PATCH] Add Seurat v5 checks --- tests/testthat/test-make-seurat.R | 92 +++++++++++++++++++++++++++---- 1 file changed, 80 insertions(+), 12 deletions(-) diff --git a/tests/testthat/test-make-seurat.R b/tests/testthat/test-make-seurat.R index adfe4e6..242213b 100644 --- a/tests/testthat/test-make-seurat.R +++ b/tests/testthat/test-make-seurat.R @@ -1,6 +1,7 @@ test_that("SCE to Seurat with no id conversion works", { sce <- readRDS(test_path("data", "scpca_sce.rds")) seurat_obj <- sce_to_seurat(sce, use_symbols = FALSE) + expect_s4_class(seurat_obj, "Seurat") expect_equal(dim(seurat_obj), dim(sce)) expect_setequal(rownames(seurat_obj), rownames(sce)) @@ -9,7 +10,11 @@ test_that("SCE to Seurat with no id conversion works", { expect_equal(names(seurat_obj@assays), c("RNA", "spliced")) expect_equal(Seurat::DefaultAssay(seurat_obj), "RNA") - # array contents + # assay types + expect_s4_class(seurat_obj[["RNA"]], "Assay") + expect_s4_class(seurat_obj[["spliced"]], "Assay") + + # assay contents expect_contains( slotNames(seurat_obj[["RNA"]]), c("counts", "data", "scale.data", "var.features", "meta.features") @@ -18,9 +23,10 @@ test_that("SCE to Seurat with no id conversion works", { expect_equal(seurat_obj[["RNA"]]$counts, counts(sce)) expect_equal(seurat_obj[["RNA"]]$data, logcounts(sce)) - expect_setequal( - seurat_obj[["RNA"]]@var.features, - metadata(sce)$highly_variable_genes + expect_equal( + Seurat::VariableFeatures(seurat_obj[["RNA"]]), + metadata(sce)$highly_variable_genes, + ignore_attr = TRUE ) expect_equal(names(seurat_obj@reductions), c("pca", "umap")) @@ -40,6 +46,7 @@ test_that("SCE to Seurat with id conversion works as expected", { expect_warning( seurat_obj <- sce_to_seurat(sce, use_symbols = TRUE, reference = "sce") ) + expect_s4_class(seurat_obj, "Seurat") new_genes <- suppressMessages( ensembl_to_symbol(rownames(sce), unique = TRUE, sce = sce) @@ -58,15 +65,20 @@ test_that("SCE to Seurat with id conversion works as expected", { expect_equal(names(seurat_obj@assays), c("RNA", "spliced")) expect_equal(Seurat::DefaultAssay(seurat_obj), "RNA") - # array contents + # assay types + expect_s4_class(seurat_obj[["RNA"]], "Assay") + expect_s4_class(seurat_obj[["spliced"]], "Assay") + + # assay contents expect_contains( slotNames(seurat_obj[["RNA"]]), c("counts", "data", "scale.data", "var.features", "meta.features") ) - expect_setequal( - seurat_obj[["RNA"]]@var.features, - hv_genes + expect_equal( + Seurat::VariableFeatures(seurat_obj[["RNA"]]), + hv_genes, + ignore_attr = TRUE ) expect_equal(names(seurat_obj@reductions), c("pca", "umap")) expect_equal(names(seurat_obj@reductions), c("pca", "umap")) @@ -83,6 +95,7 @@ test_that("SCE to Seurat with id conversion works as expected", { test_that("SCE to Seurat with id conversion and 10x reference works as expected", { sce <- readRDS(test_path("data", "scpca_sce.rds")) seurat_obj <- sce_to_seurat(sce, use_symbols = TRUE, reference = "10x2020") + expect_s4_class(seurat_obj, "Seurat") new_genes <- suppressMessages( ensembl_to_symbol(rownames(sce), unique = TRUE, reference = "10x2020") @@ -101,15 +114,21 @@ test_that("SCE to Seurat with id conversion and 10x reference works as expected" expect_equal(names(seurat_obj@assays), c("RNA", "spliced")) expect_equal(Seurat::DefaultAssay(seurat_obj), "RNA") - # array contents + + # assay types + expect_s4_class(seurat_obj[["RNA"]], "Assay") + expect_s4_class(seurat_obj[["spliced"]], "Assay") + + # assay contents expect_contains( slotNames(seurat_obj[["RNA"]]), c("counts", "data", "scale.data", "var.features", "meta.features") ) - expect_setequal( - seurat_obj[["RNA"]]@var.features, - hv_genes + expect_equal( + Seurat::VariableFeatures(seurat_obj[["RNA"]]), + hv_genes, + ignore_attr = TRUE ) expect_equal(names(seurat_obj@reductions), c("pca", "umap")) expect_equal(names(seurat_obj@reductions), c("pca", "umap")) @@ -132,6 +151,7 @@ test_that("conversion works with altExps", { ) seurat_obj <- sce_to_seurat(sce, use_symbols = FALSE) + expect_s4_class(seurat_obj, "Seurat") expect_equal(dim(seurat_obj), dim(sce)) expect_setequal(rownames(seurat_obj), rownames(sce)) @@ -139,4 +159,52 @@ test_that("conversion works with altExps", { expect_setequal(names(seurat_obj@assays), c("RNA", "spliced", "alt1", "alt2")) expect_equal(Seurat::DefaultAssay(seurat_obj), "RNA") + + # assay types + expect_s4_class(seurat_obj[["RNA"]], "Assay") + expect_s4_class(seurat_obj[["spliced"]], "Assay") + expect_s4_class(seurat_obj[["alt1"]], "Assay") + expect_s4_class(seurat_obj[["alt2"]], "Assay") +}) + +test_that("Seurat v5 conversion works", { + sce <- readRDS(test_path("data", "scpca_sce.rds")) + seurat_obj <- sce_to_seurat(sce, use_symbols = FALSE, seurat_assay_version = "v5") + expect_s4_class(seurat_obj, "Seurat") + + expect_equal(dim(seurat_obj), dim(sce)) + expect_setequal(rownames(seurat_obj), rownames(sce)) + expect_setequal(colnames(seurat_obj), colnames(sce)) + + expect_equal(names(seurat_obj@assays), c("RNA", "spliced")) + expect_equal(Seurat::DefaultAssay(seurat_obj), "RNA") + + # assay types + expect_s4_class(seurat_obj[["RNA"]], "Assay5") + expect_s4_class(seurat_obj[["spliced"]], "Assay5") + + # expected layers present + expect_contains( + names(seurat_obj[["RNA"]]@layers), + c("counts", "data", "scale.data") + ) + + expect_equal(seurat_obj[["RNA"]]$counts, counts(sce)) + expect_equal(seurat_obj[["RNA"]]$data, logcounts(sce)) + + expect_equal( + Seurat::VariableFeatures(seurat_obj[["RNA"]]), + metadata(sce)$highly_variable_genes, + ignore_attr = TRUE + ) + + expect_equal(names(seurat_obj@reductions), c("pca", "umap")) + expect_equal( + dim(seurat_obj[["pca"]]), + dim(reducedDim(sce, "PCA")) + ) + expect_equal( + dim(seurat_obj[["umap"]]), + dim(reducedDim(sce, "UMAP")) + ) })