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