Skip to content

Commit bc47ac6

Browse files
committed
Add unit tests for utilities
1 parent a2a795e commit bc47ac6

File tree

1 file changed

+201
-0
lines changed

1 file changed

+201
-0
lines changed

tests/testthat/test-utilities.R

Lines changed: 201 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,201 @@
1+
test_that("get_count_datasets works", {
2+
# Use testthat edition 3
3+
local_edition(3)
4+
5+
# Generate test SE
6+
se <- SummarizedExperiment::SummarizedExperiment(
7+
assays = list(
8+
mat1 = matrix(seq_len(9), nrow = 3),
9+
mat2 = matrix(seq(10, 18), nrow = 3),
10+
mat3 = matrix(seq(19, 27), nrow = 3)
11+
)
12+
)
13+
rownames(se) <- paste0("G", seq_len(3))
14+
colnames(se) <- paste0("S", seq_len(3))
15+
SummarizedExperiment::assays(se) <- lapply(
16+
SummarizedExperiment::assays(se),
17+
function(x) {
18+
rownames(x) <- rownames(se)
19+
colnames(x) <- colnames(se)
20+
x
21+
}
22+
)
23+
24+
# Check that dimnames are as expected
25+
expect_equal(rownames(se), rownames(assay(se, "mat1", withDimnames = FALSE)))
26+
expect_equal(rownames(se), rownames(assay(se, "mat2", withDimnames = FALSE)))
27+
expect_equal(rownames(se), rownames(assay(se, "mat3", withDimnames = FALSE)))
28+
expect_equal(colnames(se), colnames(assay(se, "mat1", withDimnames = FALSE)))
29+
expect_equal(colnames(se), colnames(assay(se, "mat2", withDimnames = FALSE)))
30+
expect_equal(colnames(se), colnames(assay(se, "mat3", withDimnames = FALSE)))
31+
# Check that matrix values are as expected
32+
expect_equal(assay(se, "mat1")[, 1], c(1, 2, 3), ignore_attr = TRUE)
33+
expect_equal(assay(se, "mat2")[, 2], c(13, 14, 15), ignore_attr = TRUE)
34+
expect_equal(assay(se, "mat3")[, 3], c(25, 26, 27), ignore_attr = TRUE)
35+
36+
# SE and all assays have the same dimnames
37+
cds <- get_count_datasets(se)
38+
expect_s3_class(cds, "tbl_df")
39+
expect_equal(nrow(cds), 9L)
40+
expect_equal(ncol(cds), 5L)
41+
expect_named(cds, c(".feature", ".sample", "mat1", "mat2", "mat3"))
42+
expect_equal(cds$.feature, rep(paste0("G", seq_len(3)), 3))
43+
expect_equal(cds$.sample, rep(paste0("S", seq_len(3)), each = 3))
44+
expect_equal(cds$mat1, seq(1, 9))
45+
expect_equal(cds$mat2, seq(10, 18))
46+
expect_equal(cds$mat3, seq(19, 27))
47+
48+
# SE does not have dimnames, all assays have the same
49+
se1 <- se
50+
rownames(se1) <- colnames(se1) <- NULL
51+
expect_equal(colnames(assay(se1, "mat1", withDimnames = FALSE)), paste0("S", seq_len(3)))
52+
expect_equal(rownames(assay(se1, "mat1", withDimnames = FALSE)), paste0("G", seq_len(3)))
53+
expect_null(colnames(se1))
54+
expect_null(rownames(se1))
55+
expect_warning(expect_warning(cds <- get_count_datasets(se1), "have column names, but the SummarizedExperiment does not"), "have row names, but the SummarizedExperiment does not")
56+
expect_s3_class(cds, "tbl_df")
57+
expect_equal(nrow(cds), 9L)
58+
expect_equal(ncol(cds), 5L)
59+
expect_named(cds, c(".feature", ".sample", "mat1", "mat2", "mat3"))
60+
expect_equal(cds$.feature, rep(paste0("G", seq_len(3)), 3))
61+
expect_equal(cds$.sample, rep(paste0("S", seq_len(3)), each = 3))
62+
expect_equal(cds$mat1, seq(1, 9))
63+
expect_equal(cds$mat2, seq(10, 18))
64+
expect_equal(cds$mat3, seq(19, 27))
65+
66+
# SE does not have dimnames, two assays have the same, third assay does not have -> error
67+
se1 <- se
68+
rownames(se1) <- colnames(se1) <- NULL
69+
rownames(assay(se1, "mat2", withDimnames = FALSE)) <-
70+
colnames(assay(se1, "mat2", withDimnames = FALSE)) <- NULL
71+
expect_error(get_count_datasets(se1), "at least one of the assays in your SummarizedExperiment have column names")
72+
73+
# SE does not have dimnames, assays have the same but in different order
74+
se1 <- se
75+
rownames(se1) <- colnames(se1) <- NULL
76+
colnames(assay(se1, "mat2", withDimnames = FALSE)) <- c("S2", "S3", "S1")
77+
rownames(assay(se1, "mat3", withDimnames = FALSE)) <- c("G2", "G3", "G1")
78+
expect_warning(expect_warning(cds <- get_count_datasets(se1), "have column names, but the SummarizedExperiment does not"), "have row names, but the SummarizedExperiment does not")
79+
expect_s3_class(cds, "tbl_df")
80+
expect_equal(nrow(cds), 9L)
81+
expect_equal(ncol(cds), 5L)
82+
expect_named(cds, c(".feature", ".sample", "mat1", "mat2", "mat3"))
83+
expect_equal(cds$.feature, rep(paste0("G", seq_len(3)), 3))
84+
expect_equal(cds$.sample, rep(paste0("S", seq_len(3)), each = 3))
85+
expect_equal(cds$mat1, seq(1, 9))
86+
expect_equal(cds$mat2, c(16, 17, 18, 10, 11, 12, 13, 14, 15))
87+
expect_equal(cds$mat3, c(21, 19, 20, 24, 22, 23, 27, 25, 26))
88+
89+
# SE does not have dimnames, assays have nonoverlapping dimnames -> error
90+
se1 <- se
91+
rownames(se1) <- colnames(se1) <- NULL
92+
rownames(assay(se1, "mat2", withDimnames = FALSE)) <- paste0("A", seq_len(3))
93+
expect_error(get_count_datasets(se1), "at least one of the assays in your SummarizedExperiment have row names")
94+
95+
se1 <- se
96+
rownames(se1) <- colnames(se1) <- NULL
97+
colnames(assay(se1, "mat2", withDimnames = FALSE)) <- paste0("A", seq_len(3))
98+
expect_error(get_count_datasets(se1), "at least one of the assays in your SummarizedExperiment have column names")
99+
100+
# Neither SE nor assays have column names
101+
se1 <- se
102+
colnames(se1) <- NULL
103+
colnames(assay(se1, "mat1", withDimnames = FALSE)) <- NULL
104+
colnames(assay(se1, "mat2", withDimnames = FALSE)) <- NULL
105+
colnames(assay(se1, "mat3", withDimnames = FALSE)) <- NULL
106+
cds <- get_count_datasets(se1)
107+
expect_s3_class(cds, "tbl_df")
108+
expect_equal(nrow(cds), 9L)
109+
expect_equal(ncol(cds), 5L)
110+
expect_named(cds, c(".feature", ".sample", "mat1", "mat2", "mat3"))
111+
expect_equal(cds$.feature, rep(paste0("G", seq_len(3)), 3))
112+
expect_equal(cds$.sample, rep(as.character(seq_len(3)), each = 3))
113+
expect_equal(cds$mat1, seq(1, 9))
114+
expect_equal(cds$mat2, seq(10, 18))
115+
expect_equal(cds$mat3, seq(19, 27))
116+
117+
# Neither SE nor assays have row names
118+
se1 <- se
119+
rownames(se1) <- NULL
120+
rownames(assay(se1, "mat1", withDimnames = FALSE)) <- NULL
121+
rownames(assay(se1, "mat2", withDimnames = FALSE)) <- NULL
122+
rownames(assay(se1, "mat3", withDimnames = FALSE)) <- NULL
123+
cds <- get_count_datasets(se1)
124+
expect_s3_class(cds, "tbl_df")
125+
expect_equal(nrow(cds), 9L)
126+
expect_equal(ncol(cds), 5L)
127+
expect_named(cds, c(".feature", ".sample", "mat1", "mat2", "mat3"))
128+
expect_equal(cds$.feature, rep(as.character(seq_len(3)), 3))
129+
expect_equal(cds$.sample, rep(paste0("S", seq_len(3)), each = 3))
130+
expect_equal(cds$mat1, seq(1, 9))
131+
expect_equal(cds$mat2, seq(10, 18))
132+
expect_equal(cds$mat3, seq(19, 27))
133+
134+
# Neither SE nor assays have any dimnames
135+
se1 <- se
136+
rownames(se1) <- NULL
137+
colnames(se1) <- NULL
138+
rownames(assay(se1, "mat1", withDimnames = FALSE)) <- NULL
139+
colnames(assay(se1, "mat1", withDimnames = FALSE)) <- NULL
140+
rownames(assay(se1, "mat2", withDimnames = FALSE)) <- NULL
141+
colnames(assay(se1, "mat2", withDimnames = FALSE)) <- NULL
142+
rownames(assay(se1, "mat3", withDimnames = FALSE)) <- NULL
143+
colnames(assay(se1, "mat3", withDimnames = FALSE)) <- NULL
144+
cds <- get_count_datasets(se1)
145+
expect_s3_class(cds, "tbl_df")
146+
expect_equal(nrow(cds), 9L)
147+
expect_equal(ncol(cds), 5L)
148+
expect_named(cds, c(".feature", ".sample", "mat1", "mat2", "mat3"))
149+
expect_equal(cds$.feature, rep(as.character(seq_len(3)), 3))
150+
expect_equal(cds$.sample, rep(as.character(seq_len(3)), each = 3))
151+
expect_equal(cds$mat1, seq(1, 9))
152+
expect_equal(cds$mat2, seq(10, 18))
153+
expect_equal(cds$mat3, seq(19, 27))
154+
155+
# SE has dimnames, assays have the same dimnames but not overlapping with those of the SE
156+
se1 <- se
157+
rownames(se1) <- colnames(se1) <- paste0("A", seq_len(3))
158+
expect_error(get_count_datasets(se1), "don't agree with the column names of the SummarizedExperiment")
159+
160+
se1 <- se
161+
rownames(se1) <- paste0("A", seq_len(3))
162+
expect_error(get_count_datasets(se1), "don't agree with the row names of the SummarizedExperiment")
163+
164+
# SE has dimnames, none of the assays have
165+
se1 <- se
166+
rownames(assay(se1, "mat1", withDimnames = FALSE)) <- NULL
167+
colnames(assay(se1, "mat1", withDimnames = FALSE)) <- NULL
168+
rownames(assay(se1, "mat2", withDimnames = FALSE)) <- NULL
169+
colnames(assay(se1, "mat2", withDimnames = FALSE)) <- NULL
170+
rownames(assay(se1, "mat3", withDimnames = FALSE)) <- NULL
171+
colnames(assay(se1, "mat3", withDimnames = FALSE)) <- NULL
172+
cds <- get_count_datasets(se1)
173+
expect_s3_class(cds, "tbl_df")
174+
expect_equal(nrow(cds), 9L)
175+
expect_equal(ncol(cds), 5L)
176+
expect_named(cds, c(".feature", ".sample", "mat1", "mat2", "mat3"))
177+
expect_equal(cds$.feature, rep(paste0("G", seq_len(3)), 3))
178+
expect_equal(cds$.sample, rep(paste0("S", seq_len(3)), each = 3))
179+
expect_equal(cds$mat1, seq(1, 9))
180+
expect_equal(cds$mat2, seq(10, 18))
181+
expect_equal(cds$mat3, seq(19, 27))
182+
183+
# Unnamed assay(s)
184+
# se1 <- SummarizedExperiment::SummarizedExperiment(
185+
# assays = list(
186+
# matrix(seq_len(9), nrow = 3),
187+
# mat2 = matrix(seq(10, 18), nrow = 3),
188+
# matrix(seq(19, 27), nrow = 3)
189+
# )
190+
# )
191+
# expect_error(cds <- get_count_datasets(se1), "assays must be named")
192+
#
193+
# se1 <- SummarizedExperiment::SummarizedExperiment(
194+
# assays = list(
195+
# matrix(seq_len(9), nrow = 3),
196+
# matrix(seq(10, 18), nrow = 3),
197+
# matrix(seq(19, 27), nrow = 3)
198+
# )
199+
# )
200+
# expect_error(cds <- get_count_datasets(se1), "assays must be named")
201+
})

0 commit comments

Comments
 (0)