Skip to content

Commit

Permalink
updated localOutliers example and unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
MicTott committed Apr 3, 2024
1 parent aa51ad1 commit cad1a34
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 16 deletions.
30 changes: 25 additions & 5 deletions R/localOutliers.R
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
61 changes: 50 additions & 11 deletions tests/testthat/test-localOutliers.R
Original file line number Diff line number Diff line change
@@ -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)
})

0 comments on commit cad1a34

Please sign in to comment.