[413088]: / tests / testthat / test-plots.R

Download this file

228 lines (179 with data), 11.1 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
# Testing plotting functions
test_that("non-spatial plots", {
# get data
data("xenium_data")
# get custom colors
# colors <- scales::hue_pal()(length(unique(xenium_data$clusters)))
colors <- hue_pal(length(unique(xenium_data$clusters)))
names(colors) <- unique(xenium_data$clusters)
# embedding plot
vrEmbeddingPlot(xenium_data, group.by = "clusters", embedding = "umap", label = T)
vrEmbeddingPlot(xenium_data, group.by = "clusters", embedding = "umap", group.ids = c(1,3,4), label = T)
vrEmbeddingPlot(xenium_data, group.by = "clusters", embedding = "umap", colors = colors, label = T)
vrEmbeddingPlot(xenium_data, group.by = "clusters", embedding = "umap", group.ids = c(1,3,4), colors = colors[c(1,3,4)], label = T)
vrEmbeddingPlot(xenium_data, group.by = "clusters", ncol = 3, split.by = "clusters")
vrEmbeddingPlot(xenium_data, group.by = "clusters", ncol = 3, split.by = "Sample")
expect_error(vrEmbeddingPlot(xenium_data, group.by = "clusters", ncol = 3, split.by = "art"))
# embedding feature plot
vrEmbeddingFeaturePlot(xenium_data, features = c("ACTA2", "TACSTD2"), embedding = "umap", combine.features = TRUE)
# scatterplot
vrScatterPlot(xenium_data, feature.1 = "NKG7", feature.2 = "TRAC")
xenium_data <- normalizeData(xenium_data)
vrScatterPlot(xenium_data, feature.1 = "NKG7", feature.2 = "TRAC", norm = TRUE)
})
# Testing plotting functions
test_that("spatial plots", {
# get data
data("xenium_data")
# get custom colors
# colors <- scales::hue_pal()(length(unique(xenium_data$clusters)))
colors <- hue_pal(length(unique(xenium_data$clusters)))
names(colors) <- unique(xenium_data$clusters)
# spatial plot, groups and colors
vrSpatialPlot(xenium_data, group.by = "clusters", plot.segments = TRUE)
vrSpatialPlot(xenium_data, group.by = "clusters", group.ids = c(1,3,4), plot.segments = TRUE)
vrSpatialPlot(xenium_data, group.by = "clusters", colors = colors, plot.segments = TRUE)
vrSpatialPlot(xenium_data, group.by = "clusters", group.ids = c(1,3,4), colors = colors[c(1,3,4)], plot.segments = TRUE)
# spatial plot, background color
vrSpatialPlot(xenium_data, group.by = "clusters", background.color = "black")
vrSpatialPlot(xenium_data, group.by = "clusters", background.color = "yellow")
vrSpatialPlot(xenium_data, group.by = "clusters", background.color = "white")
# spatial plot with spatial and channel arguments
vrSpatialPlot(xenium_data, group.by = "clusters", spatial = "main")
vrSpatialPlot(xenium_data, group.by = "clusters", spatial = "main", background.color = "yellow")
vrSpatialPlot(xenium_data, group.by = "clusters", spatial = "main", channel = "DAPI")
vrSpatialPlot(xenium_data, group.by = "clusters", channel = "DAPI")
vrSpatialPlot(xenium_data, group.by = "clusters", channel = "DAPI2")
vrSpatialPlot(xenium_data, group.by = "clusters", spatial = "main", channel = "DAPI2")
expect_error(vrSpatialPlot(xenium_data, group.by = "clusters", spatial = "main2"))
expect_error(vrSpatialPlot(xenium_data, group.by = "clusters", spatial = "main2", channel = "DAPI2"))
# spatial plot, old background argument
expect_warning(
expect_warning(vrSpatialPlot(xenium_data, group.by = "clusters", background = "main")))
expect_warning(
expect_warning(vrSpatialPlot(xenium_data, group.by = "clusters", background = c("main", "DAPI2"))))
expect_warning(
expect_warning(
expect_error(vrSpatialPlot(xenium_data, group.by = "clusters", background = "main2"))))
# spatial plot without segmentation
vrSpatialPlot(xenium_data, group.by = "clusters", plot.segments = FALSE)
# spatial plot of visium
vrSpatialPlot(visium_data)
# spatial plot of melc data
vrSpatialPlot(melc_data, group.by = "Clusters")
expect_error(vrSpatialPlot(melc_data, group.by = "Clusters_new"))
# feature plots
vrSpatialFeaturePlot(visium_data, features = "Count")
vrSpatialFeaturePlot(visium_data, features = "Stat1", norm = TRUE, log = TRUE)
expect_error(vrSpatialFeaturePlot(visium_data, features = "Count_new"))
vrSpatialFeaturePlot(xenium_data, features = c("ACTA2", "TACSTD2"), norm = TRUE, combine.features = TRUE)
# return
expect_equal(1,1L)
})
# Testing plotting functions
test_that("missing_embedding_values", {
# get data
data("visium_data")
data("xenium_data")
# change embeddings
vrEmbeddings(xenium_data, type = "new_umap") <- vrEmbeddings(xenium_data, type = "umap")[sample(1:length(vrSpatialPoints(xenium_data)), 500),]
vrEmbeddingPlot(xenium_data, embedding = "new_umap")
expect_error(vrEmbeddingPlot(xenium_data, embedding = "new_umap", group.by = "cluster"))
vrEmbeddingFeaturePlot(xenium_data, embedding = "new_umap", features = "Count")
expect_error(vrEmbeddingFeaturePlot(xenium_data, embedding = "new_umap", features = "Counts"))
vrEmbeddingFeaturePlot(xenium_data, embedding = "new_umap", features = "REXO4")
expect_error(vrEmbeddingFeaturePlot(xenium_data, embedding = "new_umap", features = "REXO4s"))
# return
expect_equal(1,1L)
})
# Testing plotting functions
test_that("rasterization", {
# get data
data("xenium_data")
# spatial plot
vrSpatialPlot(xenium_data, group.by = "clusters", background.color = "black", n.tile = 100)
vrSpatialPlot(xenium_data, group.by = "clusters", background.color = "black", n.tile = 1)
vrSpatialPlot(xenium_data, group.by = "clusters", background.color = "black", n.tile = 10)
# feature plots
vrSpatialFeaturePlot(xenium_data, features = "Count", n.tile = 20)
vrSpatialFeaturePlot(xenium_data, features = "KRT14", norm = TRUE, log = TRUE, n.tile = 10)
expect_error(vrSpatialFeaturePlot(xenium_data, features = "Count_new"))
vrSpatialFeaturePlot(xenium_data, features = c("ACTA2", "TACSTD2"), norm = TRUE, n.tile = 100, combine.features = TRUE)
vrSpatialFeaturePlot(xenium_data, features = c("ACTA2", "TACSTD2"), norm = TRUE, n.tile = 2, combine.features = TRUE)
# embedding plots
vrEmbeddingPlot(xenium_data, n.tile = 1200, group.by = "clusters")
vrEmbeddingFeaturePlot(xenium_data, n.tile = 1200, features = c("ACTA2", "TACSTD2"))
vrEmbeddingFeaturePlot(xenium_data, n.tile = 100, features = c("ACTA2", "TACSTD2"), embedding = "umap", combine.features = TRUE)
vrEmbeddingFeaturePlot(xenium_data, n.tile = 2, features = c("ACTA2", "TACSTD2"), embedding = "umap", combine.features = TRUE)
vrEmbeddingPlot(xenium_data, n.tile = 2, group.by = "clusters")
vrEmbeddingFeaturePlot(xenium_data, n.tile = 10, features = c("ACTA2"))
# return
expect_equal(1,1L)
})
# testing multilayer plots
test_that("multilayer", {
skip_if_not_installed("ggnewscale")
data("merged_object")
# single
vrSpatialPlot(merged_object)
# cell vs ROI (without segments)
vrSpatialPlot(merged_object, plot.segments = FALSE) |>
addSpatialLayer(merged_object, assay = "Assay3", group.by = "Sample", alpha = 0.3)
# cell vs ROI (with segments)
vrSpatialPlot(merged_object, plot.segments = TRUE) |>
addSpatialLayer(merged_object, assay = "Assay3", group.by = "Sample", alpha = 0.4, colors = list(Block = "blue"))
# ROI vs cell
vrSpatialPlot(merged_object, assay = "Assay3", group.by = "Sample", alpha = 0.4, colors = list(Block = "blue")) |>
addSpatialLayer(merged_object, assay = "Assay1")
vrSpatialPlot(merged_object, assay = "Assay3", group.by = "Sample", alpha = 1, colors = list(Block = "blue")) |>
addSpatialLayer(merged_object, assay = "Assay1", plot.segments = TRUE, alpha = 0.4)
vrSpatialPlot(merged_object, assay = "Assay3", group.by = "Sample", alpha = 0.4, colors = list(Block = "blue")) |>
addSpatialLayer(merged_object, assay = "Assay1", n.tile = 100)
# cell vs molecule (without segments)
vrSpatialPlot(merged_object, plot.segments = FALSE) |>
addSpatialLayer(merged_object, assay = "Assay2", group.by = "gene", alpha = 1, colors = list(KRT15 = "blue", KRT14 = "green"))
# cell vs molecule
vrSpatialPlot(merged_object, plot.segments = TRUE) |>
addSpatialLayer(merged_object, assay = "Assay2", group.by = "gene", alpha = 1, colors = list(KRT15 = "blue", KRT14 = "green"))
# molecule vs cell (with segments)
vrSpatialPlot(merged_object, assay = "Assay2", group.by = "gene", alpha = 1, colors = list(KRT15 = "blue", KRT14 = "green")) |>
addSpatialLayer(merged_object, assay = "Assay1")
vrSpatialPlot(merged_object, assay = "Assay2", group.by = "gene", alpha = 1, colors = list(KRT15 = "blue", KRT14 = "green")) |>
addSpatialLayer(merged_object, assay = "Assay1", plot.segments = TRUE, alpha = 0.4)
# cells, ROIs and molecules together
vrSpatialPlot(merged_object, plot.segments = TRUE) |>
addSpatialLayer(merged_object, assay = "Assay2", group.by = "gene", alpha = 1, colors = list(KRT15 = "blue", KRT14 = "green")) |>
addSpatialLayer(merged_object, assay = "Assay3", group.by = "Layer", alpha = 0.4, colors = list(Section3 = "blue"))
expect_equal(1,1)
})
# testing multilayer plots
# TODO: tiling multilayer visualization behavior is not ideal right now
test_that("multilayer (with tiling)", {
skip_if_not_installed("ggnewscale")
data("merged_object")
# single
vrSpatialPlot(merged_object)
# cell vs ROI (without segments)
vrSpatialPlot(merged_object, plot.segments = FALSE, n.tile = 100) |>
addSpatialLayer(merged_object, assay = "Assay3", group.by = "Sample", alpha = 0.4, colors = list(Block = "blue"))
# ROI vs cell
vrSpatialPlot(merged_object, assay = "Assay3", group.by = "Sample", alpha = 0.4, colors = list(Block = "blue")) |>
addSpatialLayer(merged_object, assay = "Assay1", n.tile = 100)
# cell vs molecule (without segments)
vrSpatialPlot(merged_object, plot.segments = FALSE) |>
addSpatialLayer(merged_object, assay = "Assay2", group.by = "gene", alpha = 1, colors = list(KRT15 = "blue", KRT14 = "green"), n.tile = 100)
vrSpatialPlot(merged_object, plot.segments = FALSE, n.tile = 100) |>
addSpatialLayer(merged_object, assay = "Assay2", group.by = "gene", alpha = 1, colors = list(KRT15 = "blue", KRT14 = "green"), n.tile = 100)
# cell vs molecule
vrSpatialPlot(merged_object, plot.segments = TRUE) |>
addSpatialLayer(merged_object, assay = "Assay2", group.by = "gene", alpha = 1, colors = list(KRT15 = "blue", KRT14 = "green"), n.tile = 100)
# molecule vs cell (with segments)
vrSpatialPlot(merged_object, assay = "Assay2", group.by = "gene", alpha = 1, colors = list(KRT15 = "blue", KRT14 = "green")) |>
addSpatialLayer(merged_object, assay = "Assay1", n.tile = 100)
vrSpatialPlot(merged_object, assay = "Assay2", group.by = "gene", alpha = 1, colors = list(KRT15 = "blue", KRT14 = "green")) |>
addSpatialLayer(merged_object, assay = "Assay1", plot.segments = TRUE)
# molecule vs ROI
vrSpatialPlot(merged_object, assay = "Assay2", group.by = "gene", alpha = 1, colors = list(KRT15 = "blue", KRT14 = "green"), n.tile = 100) |>
addSpatialLayer(merged_object, assay = "Assay3", group.by = "Sample", alpha = 0.4, colors = list(Block = "blue"))
expect_equal(1,1)
})