Skip to content

Commit

Permalink
Adjusted unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
nilseling committed Dec 12, 2023
1 parent 1bd8483 commit fc75f61
Show file tree
Hide file tree
Showing 4 changed files with 133 additions and 19 deletions.
2 changes: 1 addition & 1 deletion R/binAcrossPixels.R
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ binAcrossPixels <- function(object,
cur_split_tmp <- split(object[[spot_id]], f = object[[spot_id]])
cur_split <- lapply(cur_split_tmp, function(x){ceiling(seq_along(x)/bin_size)})

if (!isTRUE(all.equal(unlist(cur_split_tmp), object[[spot_id]]))) {
if (!isTRUE(all.equal(as.vector(unlist(cur_split_tmp)), object[[spot_id]]))) {
stop("Spot IDs of pixels within 'object' are not ordered alphabetically.")
}

Expand Down
50 changes: 50 additions & 0 deletions tests/testthat/test_binAcrossPixels.R
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,30 @@ test_that("binAcrossPixels function works.", {
expect_equal(counts(out)[,out$sample_id == "Dy164"][,1], rowSums(counts(cur_sce)[,cur_sce$sample_id == "Dy164"][,1:10]))
expect_equal(counts(out)[,out$sample_id == "Dy164"][,2], rowSums(counts(cur_sce)[,cur_sce$sample_id == "Dy164"][,11:20]))

test <- aggregate(t(counts(cur_sce)), by = list(cur_sce$sample_id, rep(rep(1:10, each = 10), 4)), FUN = "sum")
test <- test[order(test$Group.1, test$Group.2),]

test2 <- counts(out)
test <- t(as.matrix(test[,-c(1,2)]))

dimnames(test2) <- NULL
dimnames(test) <- NULL

all.equal(test2, test)

expect_silent(out <- binAcrossPixels(cur_sce, bin_size = 10, statistic = "mean"))

test <- aggregate(t(counts(cur_sce)), by = list(cur_sce$sample_id, rep(rep(1:10, each = 10), 4)), FUN = "mean")
test <- test[order(test$Group.1, test$Group.2),]

test2 <- counts(out)
test <- t(as.matrix(test[,-c(1,2)]))

dimnames(test2) <- NULL
dimnames(test) <- NULL

all.equal(test2, test)

# Works
expect_silent(out <- binAcrossPixels(cur_sce, bin_size = 2))
expect_s4_class(out, "SingleCellExperiment")
Expand Down Expand Up @@ -61,6 +85,26 @@ test_that("binAcrossPixels function works.", {
expect_equal(counts(out)[,out$sample_id == "Dy164"][,1], rowMeans(counts(cur_sce)[,cur_sce$sample_id == "Dy164"][,1:5]))
expect_equal(counts(out)[,out$sample_id == "Dy164"][,2], rowMeans(counts(cur_sce)[,cur_sce$sample_id == "Dy164"][,6:10]))

# Works
expect_silent(out <- binAcrossPixels(cur_sce, bin_size = 3))
expect_s4_class(out, "SingleCellExperiment")

# colData are correct
expect_equal(out$sample_id, rep(c("Dy161", "Dy162", "Dy163", "Dy164"), each = 34))
expect_equal(out$spot_id, rep(c("Dy161", "Dy162", "Dy163", "Dy164"), each = 34))
expect_equal(as.numeric(out$bin), rep(1:34, 4))
expect_equal(as.numeric(out$ncells), rep(c(rep(3, 33), 1), 4))

# rowData are correct
expect_equal(rowData(cur_sce), rowData(out))

# Summarized counts are correct
expect_equal(counts(out)[,1], rowSums(counts(cur_sce)[,1:3]))
expect_equal(counts(out)[,2], rowSums(counts(cur_sce)[,4:6]))
expect_equal(counts(out)[,3], rowSums(counts(cur_sce)[,7:9]))
expect_equal(counts(out)[,4], rowSums(counts(cur_sce)[,10:12]))
expect_equal(counts(out)[,34], counts(cur_sce)[,100])

# Error
expect_error(binAcrossPixels("test"),
regexp = "'object' needs to be a SingleCellExperiment object.",
Expand All @@ -80,4 +124,10 @@ test_that("binAcrossPixels function works.", {
expect_error(binAcrossPixels(cur_sce, bin_size = 10, statistic = "test"),
regexp = "'statistic' must be 'sum', 'mean' or 'median'.",
fixed = TRUE)

cur_sce <- cur_sce[,sample(ncol(cur_sce))]

expect_error(binAcrossPixels(cur_sce, bin_size = 10),
regexp = "Spot IDs of pixels within 'object' are not ordered alphabetically.",
fixed = TRUE)
})
43 changes: 35 additions & 8 deletions tests/testthat/test_read_cpout.R
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ test_that("read_cpout function works.", {
# SpatialExperiment
cur_spe <- read_cpout(path, graph_file = "Object_relationships.csv")

expect_equal(colnames(cur_spe), paste0(cur_spe$sample_id, "_", cur_spe$ObjectNumber))

expect_s4_class(cur_spe, "SpatialExperiment")

expect_equal(rownames(cur_spe), c("Ag107", "Pr141", "Sm147", "Eu153", "Yb172"))
Expand All @@ -25,7 +27,10 @@ test_that("read_cpout function works.", {
image_file <- vroom::vroom(file.path(path, "Image.csv"))
object_file <- dplyr::left_join(object_file, image_file, by = "ImageNumber")

expect_equal(counts(cur_spe), cur_counts * (2^16 - 1))
test1 <- counts(cur_spe)
colnames(test1) <- NULL

expect_equal(test1, cur_counts * (2^16 - 1))
expect_equal(counts(cur_spe)[1:10], c(0.108695652148605, 0.0217391304297211, 0, 0.0434782608594421,
0.739130434610516, 0, 0, 0, 0, 1.1999999997206))

Expand Down Expand Up @@ -68,6 +73,7 @@ test_that("read_cpout function works.", {

# SingleCellExperiment
cur_sce <- read_cpout(path, return_as = "sce", graph_file = "Object_relationships.csv")
expect_equal(colnames(cur_sce), paste0(cur_sce$sample_id, "_", cur_sce$ObjectNumber))

expect_s4_class(cur_sce, "SingleCellExperiment")

Expand All @@ -89,7 +95,10 @@ test_that("read_cpout function works.", {
image_file <- vroom::vroom(file.path(path, "Image.csv"))
object_file <- dplyr::left_join(object_file, image_file, by = "ImageNumber")

expect_equal(counts(cur_sce), cur_counts * (2^16 - 1))
test1 <- counts(cur_sce)
colnames(test1) <- NULL

expect_equal(test1, cur_counts * (2^16 - 1))
expect_equal(counts(cur_sce)[1:10], c(0.108695652148605, 0.0217391304297211, 0, 0.0434782608594421,
0.739130434610516, 0, 0, 0, 0, 1.1999999997206))

Expand Down Expand Up @@ -146,7 +155,10 @@ test_that("read_cpout function works.", {
cur_counts <- t(object_file[,grepl("MeanIntensity_FullStack", colnames(object_file))])
rownames(cur_counts) <- rownames(cur_spe)

expect_equal(counts(cur_spe), cur_counts)
test1 <- counts(cur_spe)
colnames(test1) <- NULL

expect_equal(test1, cur_counts)
expect_equal(counts(cur_spe)[1:10], c(1.65858933621127e-06, 3.31717867242253e-07, 0, 6.63435734484507e-07,
1.12784074862366e-05, 0, 0, 0, 0, 1.83108262717724e-05))

Expand Down Expand Up @@ -200,7 +212,10 @@ test_that("read_cpout function works.", {
cur_counts <- t(object_file[,grepl("MeanIntensity_FullStack", colnames(object_file))])
rownames(cur_counts) <- rownames(cur_sce)

expect_equal(counts(cur_sce), cur_counts)
test1 <- counts(cur_sce)
colnames(test1) <- NULL

expect_equal(test1, cur_counts)
expect_equal(counts(cur_sce)[1:10], c(1.65858933621127e-06, 3.31717867242253e-07, 0, 6.63435734484507e-07,
1.12784074862366e-05, 0, 0, 0, 0, 1.83108262717724e-05))

Expand Down Expand Up @@ -239,7 +254,10 @@ test_that("read_cpout function works.", {
cur_counts <- t(object_file[,grepl("MeanIntensity_FullStack", colnames(object_file))])
rownames(cur_counts) <- rownames(cur_spe)

expect_equal(counts(cur_spe), cur_counts)
test1 <- counts(cur_spe)
colnames(test1) <- NULL

expect_equal(test1, cur_counts)
expect_equal(counts(cur_spe)[1:10], c(1.65858933621127e-06, 3.31717867242253e-07, 0, 6.63435734484507e-07,
1.12784074862366e-05, 0, 0, 0, 0, 1.83108262717724e-05))

Expand Down Expand Up @@ -281,7 +299,10 @@ test_that("read_cpout function works.", {
image_file <- vroom::vroom(file.path(path, "Image.csv"))
object_file <- dplyr::left_join(object_file, image_file, by = "ImageNumber")

expect_equal(counts(cur_spe), cur_counts * (2^16 - 1))
test1 <- counts(cur_spe)
colnames(test1) <- NULL

expect_equal(test1, cur_counts * (2^16 - 1))
expect_equal(counts(cur_spe)[1:10], c(0.108695652148605, 0.0217391304297211, 0, 0.0434782608594421,
0.739130434610516, 0, 0, 0, 0, 1.1999999997206))

Expand Down Expand Up @@ -322,7 +343,10 @@ test_that("read_cpout function works.", {
image_file <- vroom::vroom(file.path(path, "Image.csv"))
object_file <- dplyr::left_join(object_file, image_file, by = "ImageNumber")

expect_equal(counts(cur_spe), cur_counts * (2^16 - 1))
test1 <- counts(cur_spe)
colnames(test1) <- NULL

expect_equal(test1, cur_counts * (2^16 - 1))
expect_equal(counts(cur_spe)[1:10], c(0.108695652148605, 0.0217391304297211, 0, 0.0434782608594421,
0.739130434610516, 0, 0, 0, 0, 1.1999999997206))

Expand Down Expand Up @@ -357,7 +381,10 @@ test_that("read_cpout function works.", {
image_file <- vroom::vroom(file.path(path, "Image.csv"))
object_file <- merge(object_file, image_file, by = "ImageNumber", order = FALSE)

expect_equal(counts(cur_spe), cur_counts * (2^16 - 1))
test1 <- counts(cur_spe)
colnames(test1) <- NULL

expect_equal(test1, cur_counts * (2^16 - 1))
expect_equal(counts(cur_spe)[1:10], c(0, 0, 0, 0, 0,
0, 0, 0, 0, 1))

Expand Down
57 changes: 47 additions & 10 deletions tests/testthat/test_read_steinbock.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ test_that("read_steinbock function works", {
# SpatialExperiment
cur_spe <- read_steinbock(path)

expect_equal(colnames(cur_spe), paste0(cur_spe$sample_id, "_", cur_spe$ObjectNumber))

expect_s4_class(cur_spe, "SpatialExperiment")

expect_equal(rownames(cur_spe), c("Ag107", "Cytokeratin 5", "Laminin",
Expand All @@ -23,7 +25,10 @@ test_that("read_steinbock function works", {
cur_counts <- lapply(cur_files, readr::read_csv, show_col_types = FALSE)
cur_counts <- do.call("rbind", cur_counts)

expect_equal(counts(cur_spe), t(cur_counts[,-1]))
test1 <- counts(cur_spe)
colnames(test1) <- NULL

expect_equal(test1, t(cur_counts[,-1]))
expect_equal(counts(cur_spe)[1:10], c(0.0909090909090909, 0.181818181818182, 0.0909090909090909,
0.0909090909090909, 0.938306353308938, 0.181163804871695, 0,
0.142857142857143, 0.501448290688651, 1.00346943310329))
Expand Down Expand Up @@ -73,6 +78,8 @@ test_that("read_steinbock function works", {
# SingleCellExperiment
cur_sce <- read_steinbock(path, return_as = "sce")

expect_equal(colnames(cur_sce), paste0(cur_sce$sample_id, "_", cur_sce$ObjectNumber))

expect_s4_class(cur_sce, "SingleCellExperiment")

expect_equal(rownames(cur_sce), c("Ag107", "Cytokeratin 5", "Laminin",
Expand All @@ -91,7 +98,10 @@ test_that("read_steinbock function works", {
cur_counts <- lapply(cur_files, readr::read_csv, show_col_types = FALSE)
cur_counts <- do.call("rbind", cur_counts)

expect_equal(counts(cur_sce), t(cur_counts[,-1]))
test1 <- counts(cur_spe)
colnames(test1) <- NULL

expect_equal(test1, t(cur_counts[,-1]))
expect_equal(counts(cur_sce)[1:10], c(0.0909090909090909, 0.181818181818182, 0.0909090909090909,
0.0909090909090909, 0.938306353308938, 0.181163804871695, 0,
0.142857142857143, 0.501448290688651, 1.00346943310329))
Expand Down Expand Up @@ -152,7 +162,10 @@ test_that("read_steinbock function works", {
cur_counts <- lapply(cur_files, readr::read_csv, show_col_types = FALSE)
cur_counts <- do.call("rbind", cur_counts)

expect_equal(counts(cur_spe), t(cur_counts[,-1]))
test1 <- counts(cur_spe)
colnames(test1) <- NULL

expect_equal(test1, t(cur_counts[,-1]))

cur_files <- list.files(file.path(path, "regionprops"), full.names = TRUE)
cur_morph <- lapply(cur_files, readr::read_csv, show_col_types = FALSE)
Expand Down Expand Up @@ -203,7 +216,10 @@ test_that("read_steinbock function works", {
cur_counts <- lapply(cur_files, readr::read_csv, show_col_types = FALSE)
cur_counts <- do.call("rbind", cur_counts)

expect_equal(counts(cur_sce), t(cur_counts[,-1]))
test1 <- counts(cur_sce)
colnames(test1) <- NULL

expect_equal(test1, t(cur_counts[,-1]))

cur_files <- list.files(file.path(path, "regionprops"), full.names = TRUE)
cur_morph <- lapply(cur_files, readr::read_csv, show_col_types = FALSE)
Expand Down Expand Up @@ -239,7 +255,10 @@ test_that("read_steinbock function works", {
cur_counts <- lapply(cur_files, readr::read_csv, show_col_types = FALSE)
cur_counts <- do.call("rbind", cur_counts)

expect_equal(counts(cur_spe), t(cur_counts[,-1]))
test1 <- counts(cur_spe)
colnames(test1) <- NULL

expect_equal(test1, t(cur_counts[,-1]))

cur_files <- list.files(file.path(path, "regionprops"), full.names = TRUE)
cur_morph <- lapply(cur_files, readr::read_csv, show_col_types = FALSE)
Expand Down Expand Up @@ -282,7 +301,10 @@ test_that("read_steinbock function works", {
cur_counts <- lapply(cur_files, readr::read_csv, show_col_types = FALSE)
cur_counts <- do.call("rbind", cur_counts)

expect_equal(counts(cur_sce), t(cur_counts[,-1]))
test1 <- counts(cur_sce)
colnames(test1) <- NULL

expect_equal(test1, t(cur_counts[,-1]))

cur_files <- list.files(file.path(path, "regionprops"), full.names = TRUE)
cur_morph <- lapply(cur_files, readr::read_csv, show_col_types = FALSE)
Expand Down Expand Up @@ -321,7 +343,10 @@ test_that("read_steinbock function works", {
cur_counts <- lapply(cur_files, readr::read_csv, show_col_types = FALSE)
cur_counts <- do.call("rbind", cur_counts)

expect_equal(counts(cur_spe), t(cur_counts[,-1]))
test1 <- counts(cur_spe)
colnames(test1) <- NULL

expect_equal(test1, t(cur_counts[,-1]))

expect_equal(cur_spe$ObjectNumber, cur_counts$Object)

Expand Down Expand Up @@ -359,7 +384,10 @@ test_that("read_steinbock function works", {
cur_counts <- lapply(cur_files, readr::read_csv, show_col_types = FALSE)
cur_counts <- do.call("rbind", cur_counts)

expect_equal(counts(cur_sce), t(cur_counts[,-1]))
test1 <- counts(cur_sce)
colnames(test1) <- NULL

expect_equal(test1, t(cur_counts[,-1]))

cur_panel <- readr::read_csv(file.path(path, "panel.csv"), show_col_types = FALSE)
expect_equal(rowData(cur_sce)$name, cur_panel$name)
Expand All @@ -373,6 +401,8 @@ test_that("read_steinbock function works", {

cur_spe <- read_steinbock(path, pattern = "mockData2")

expect_equal(colnames(cur_spe), paste0(cur_spe$sample_id, "_", cur_spe$ObjectNumber))

expect_s4_class(cur_spe, "SpatialExperiment")

expect_equal(rownames(cur_spe), c("Ag107", "Cytokeratin 5", "Laminin",
Expand All @@ -391,7 +421,11 @@ test_that("read_steinbock function works", {
cur_counts <- lapply(cur_files, readr::read_csv, show_col_types = FALSE)
cur_counts <- do.call("rbind", cur_counts)

expect_equal(counts(cur_spe), t(cur_counts[,-1]))
test1 <- counts(cur_spe)
colnames(test1) <- NULL

expect_equal(test1, t(cur_counts[,-1]))

expect_equal(counts(cur_spe)[1:10], c(0.399655099572807, 0.0344827586206896, 4.64201253035973, 6.50705560733532,
3.57779644892133, 0.428760413080454, 0.0945868939161301, 1.56471130624413,
8.0167475938797, 1.46797196567059))
Expand Down Expand Up @@ -455,7 +489,10 @@ test_that("read_steinbock function works", {
cur_counts <- lapply(cur_files, readr::read_csv)
cur_counts <- do.call("rbind", cur_counts)

expect_equal(counts(cur_sce), t(cur_counts[,-1]))
test1 <- counts(cur_sce)
colnames(test1) <- NULL

expect_equal(test1, t(cur_counts[,-1]))

cur_files <- list.files(file.path(path, "regionprops"), full.names = TRUE, pattern = "mockData2")
cur_morph <- lapply(cur_files, readr::read_csv)
Expand Down

0 comments on commit fc75f61

Please sign in to comment.