diff --git a/R/localOutliers.R b/R/localOutliers.R index a0d2012..90e1a40 100644 --- a/R/localOutliers.R +++ b/R/localOutliers.R @@ -39,14 +39,34 @@ #' # Identifying the mitochondrial transcripts in our SpatialExperiment. #' is.mito <- rownames(spe)[grepl("^MT-", rownames(spe))] #' -#' # Calculating QC features for each spot using scuttle -#' spe <- scuttle::addPerCellQC(spe, subsets = list(Mito = is.mito)) +#' # Calculating QC metrics for each spot using scuttle +#' spe <- scuttle::addPerCellQCMetrics(spe, subsets = list(Mito = is.mito)) +#' colnames(colData(spe)) #' -# Identifying local outliers suing SpotSweepR +#' # Identifying local outliers using SpotSweeper #' spe <- localOutliers(spe, -#' metric = "detected", -#' direction = "lower" +#' metric = "sum", +#' direction = "lower", +#' log = TRUE #' ) +#' +#' spe <- localOutliers(spe, +#' metric = "detected", +#' direction = "lower", +#' log = TRUE +#' ) +#' +#' spe <- localOutliers(spe, +#' metric = "subsets_Mito_percent", +#' direction = "higher", +#' log = FALSE +#' ) +#' +#' # combine all outliers into "local_outliers" column +#' spe$local_outliers <- as.logical(spe$sum_outliers) | +#' as.logical(spe$detected_outliers) | +#' as.logical(spe$subsets_Mito_percent_outliers) +#' localOutliers <- function( spe, metric = "detected", direction = "lower", n_neighbors = 36, samples = "sample_id", diff --git a/tests/testthat/test-localOutliers.R b/tests/testthat/test-localOutliers.R index 04fd770..4e56d36 100644 --- a/tests/testthat/test-localOutliers.R +++ b/tests/testthat/test-localOutliers.R @@ -1,18 +1,57 @@ # run examples from localOutliers() function documentation -example(localOutliers, echo = FALSE) +library(SpatialExperiment) -test_that("example objects have correct class", { - # matrices?? -}) +# load example data +spe <- STexampleData::Visium_humanDLPFC() -test_that("example objects have correct dimensions", { - expect_equal(dim(spe), c(X, XXXXX)) -}) +# change from gene id to gene names +rownames(spe) <- rowData(spe)$gene_name -test_that("example objects gives correct number of outliers", { - expect_equal(length(colnames(colData(spe))), XY) -}) +# drop out-of-tissue spots +spe <- spe[, spe$in_tissue == 1] +spe <- spe[, !is.na(spe$ground_truth)] + +# Identifying the mitochondrial transcripts in our SpatialExperiment. +is.mito <- rownames(spe)[grepl("^MT-", rownames(spe))] + +# Calculating QC metrics for each spot using scuttle +spe <- scuttle::addPerCellQCMetrics(spe, subsets = list(Mito = is.mito)) +colnames(colData(spe)) + +# Identifying local outliers using SpotSweeper +spe <- localOutliers(spe, + metric = "sum", + direction = "lower", + log = TRUE +) -test_that("combined SPE gives contains correct colData", { +spe <- localOutliers(spe, + metric = "detected", + direction = "lower", + log = TRUE +) + +spe <- localOutliers(spe, + metric = "subsets_Mito_percent", + direction = "higher", + log = FALSE +) + +# combine all outliers into "local_outliers" column +spe$local_outliers <- as.logical(spe$sum_outliers) | + as.logical(spe$detected_outliers) | + as.logical(spe$subsets_Mito_percent_outliers) + + + +# === Tests === +test_that("example objects have correct class", { + expect_s4_class(spe, "SpatialExperiment") +}) +test_that("correct outliers were found", { + expect_equal(sum(as.logical(spe$sum_outliers)), 7) + expect_equal(sum(as.logical(spe$detected_outliers)), 9) + expect_equal(sum(as.logical(spe$subsets_Mito_percent_outliers)), 2) + expect_equal(sum(as.logical(spe$local_outliers)), 11) })