|
a |
|
b/tests/testthat/test-spatial.R |
|
|
1 |
test_that("spatial neighbors", { |
|
|
2 |
|
|
|
3 |
# get data |
|
|
4 |
data("xenium_data") |
|
|
5 |
|
|
|
6 |
# spatial neighbors, delaunay |
|
|
7 |
xenium_data <- getSpatialNeighbors(xenium_data, method = "delaunay", verbose = FALSE) |
|
|
8 |
graphs <- vrGraph(xenium_data, graph.type = "delaunay") |
|
|
9 |
expect_true(inherits(graphs,"igraph")) |
|
|
10 |
expect_true(length(igraph::E(graphs)) > 0) |
|
|
11 |
vrSpatialPlot(xenium_data, graph.name = "delaunay", group.by = "clusters") |
|
|
12 |
|
|
|
13 |
# spatial neighbors, spatialkNN |
|
|
14 |
xenium_data <- getSpatialNeighbors(xenium_data, method = "spatialkNN", k = 5, verbose = FALSE) |
|
|
15 |
graphs <- vrGraph(xenium_data, graph.type = "spatialkNN") |
|
|
16 |
expect_true(inherits(graphs,"igraph")) |
|
|
17 |
expect_true(length(igraph::E(graphs)) > 0) |
|
|
18 |
vrSpatialPlot(xenium_data, graph.name = "spatialkNN", group.by = "clusters") |
|
|
19 |
|
|
|
20 |
# spatial neighbors, radius |
|
|
21 |
xenium_data <- getSpatialNeighbors(xenium_data, method = "radius", radius = 10, verbose = FALSE) |
|
|
22 |
graphs <- vrGraph(xenium_data, graph.type = "radius") |
|
|
23 |
expect_true(inherits(graphs,"igraph")) |
|
|
24 |
expect_true(length(igraph::E(graphs)) > 0) |
|
|
25 |
vrSpatialPlot(xenium_data, graph.name = "radius", group.by = "clusters") |
|
|
26 |
|
|
|
27 |
# return |
|
|
28 |
expect_equal(1,1L) |
|
|
29 |
}) |
|
|
30 |
|
|
|
31 |
test_that("spatial neighbors for subsets", { |
|
|
32 |
|
|
|
33 |
# get data |
|
|
34 |
data("xenium_data") |
|
|
35 |
|
|
|
36 |
# merge two of same types |
|
|
37 |
xenium_data2 <- xenium_data |
|
|
38 |
xenium_data2$Sample <- "XeniumR2" |
|
|
39 |
merged_data <- merge(xenium_data, xenium_data2, verbose = FALSE) |
|
|
40 |
|
|
|
41 |
# spatial neighbors, delaunay |
|
|
42 |
merged_data <- getSpatialNeighbors(merged_data, assay = "Assay1", method = "delaunay", verbose = FALSE) |
|
|
43 |
graphs <- vrGraph(merged_data, graph.type = "delaunay") |
|
|
44 |
expect_true(inherits(graphs,"igraph")) |
|
|
45 |
expect_true(length(igraph::E(graphs)) > 0) |
|
|
46 |
vrSpatialPlot(merged_data, graph.name = "delaunay", group.by = "clusters") |
|
|
47 |
|
|
|
48 |
# return |
|
|
49 |
expect_equal(1,1L) |
|
|
50 |
}) |
|
|
51 |
|
|
|
52 |
test_that("spatial tests", { |
|
|
53 |
|
|
|
54 |
# get data |
|
|
55 |
data("xenium_data") |
|
|
56 |
|
|
|
57 |
# spatial neighbors, radius based |
|
|
58 |
xenium_data <- getSpatialNeighbors(xenium_data, method = "radius", radius = 10, verbose = FALSE) |
|
|
59 |
|
|
|
60 |
# getis ord test |
|
|
61 |
xenium_data <- getHotSpotAnalysis(xenium_data, graph.type = "radius", features = c("GNLY"), verbose = FALSE) |
|
|
62 |
expect_true(all(c("GNLY_hotspot_stat", "GNLY_hotspot_pvalue", "GNLY_hotspot_flag") %in% colnames(Metadata(xenium_data)))) |
|
|
63 |
xenium_data <- getHotSpotAnalysis(xenium_data, graph.type = "radius", features = c("GNLY", "Count"), verbose = FALSE) |
|
|
64 |
expect_error(getHotSpotAnalysis(xenium_data, graph.type = "radius", features = c("GNLY", "Count1"), verbose = FALSE)) |
|
|
65 |
expect_error(getHotSpotAnalysis(xenium_data, graph.type = "radius", features = c("GNLY2", "Count1"), verbose = FALSE)) |
|
|
66 |
|
|
|
67 |
# multiple assays |
|
|
68 |
data("xenium_data") |
|
|
69 |
xenium_data2 <- xenium_data |
|
|
70 |
xenium_data2$Sample <- "Sample2" |
|
|
71 |
xenium_data <- merge(xenium_data, xenium_data2, verbose = FALSE) |
|
|
72 |
xenium_data <- getSpatialNeighbors(xenium_data, method = "radius", radius = 10, verbose = FALSE) |
|
|
73 |
xenium_data <- getHotSpotAnalysis(xenium_data, graph.type = "radius", features = c("GNLY", "Count"), verbose = FALSE) |
|
|
74 |
expect_true(all(c("GNLY_hotspot_stat", "GNLY_hotspot_pvalue", "GNLY_hotspot_flag") %in% colnames(Metadata(xenium_data)))) |
|
|
75 |
expect_true(all(c("Count_hotspot_stat", "Count_hotspot_pvalue", "Count_hotspot_flag") %in% colnames(Metadata(xenium_data)))) |
|
|
76 |
}) |
|
|
77 |
|
|
|
78 |
test_that("niche clustering", { |
|
|
79 |
|
|
|
80 |
data("xenium_data") |
|
|
81 |
|
|
|
82 |
#### |
|
|
83 |
# single assay |
|
|
84 |
#### |
|
|
85 |
|
|
|
86 |
# build niche assay |
|
|
87 |
xenium_data2 <- getSpatialNeighbors(xenium_data, radius = 15, method = "radius", verbose = FALSE) |
|
|
88 |
xenium_data2 <- getNicheAssay(xenium_data2, label = "clusters", graph.type = "radius") |
|
|
89 |
expect_equal(vrFeatureTypeNames(xenium_data2), c("RNA", "Niche")) |
|
|
90 |
xenium_data2 <- getNicheAssay(xenium_data2, label = "clusters", graph.type = "radius", new_feature_name = "Niche2") |
|
|
91 |
expect_equal(vrFeatureTypeNames(xenium_data2), c("RNA", "Niche", "Niche2")) |
|
|
92 |
expect_error(getNicheAssay(xenium_data2, label = "clusters1", graph.type = "radius")) |
|
|
93 |
|
|
|
94 |
# cluster niches |
|
|
95 |
vrMainFeatureType(xenium_data2) <- "Niche" |
|
|
96 |
xenium_data2 <- getClusters(xenium_data2, nclus = 3, method = "kmeans", label = "cluster_niches") |
|
|
97 |
expect_true("cluster_niches" %in% colnames(Metadata(xenium_data2))) |
|
|
98 |
expect_error(xenium_data2 <- getClusters(xenium_data2, nclus = 0, method = "kmeans", label = "cluster_niches")) |
|
|
99 |
expect_error(xenium_data2 <- getClusters(xenium_data2, nclus = 1.1, method = "kmeans", label = "cluster_niches")) |
|
|
100 |
expect_error(xenium_data2 <- getClusters(xenium_data2, nclus = -1, method = "kmeans", label = "cluster_niches")) |
|
|
101 |
expect_error(xenium_data2 <- getClusters(xenium_data2, nclus = c(1,2), method = "kmeans", label = "cluster_niches")) |
|
|
102 |
|
|
|
103 |
# clean |
|
|
104 |
rm(xenium_data2) |
|
|
105 |
|
|
|
106 |
#### |
|
|
107 |
# multiple assays |
|
|
108 |
#### |
|
|
109 |
|
|
|
110 |
xenium_data2 <- xenium_data |
|
|
111 |
xenium_data2$Sample <- "Sample2" |
|
|
112 |
xenium_data2 <- merge(xenium_data2, xenium_data, verbose = FALSE) |
|
|
113 |
xenium_data2 <- getSpatialNeighbors(xenium_data2, radius = 15, method = "radius", verbose = FALSE) |
|
|
114 |
xenium_data3 <- getNicheAssay(xenium_data2, label = "clusters", graph.type = "radius") |
|
|
115 |
metadata <- vrFeatureTypeNames(xenium_data3, assay = "all") |
|
|
116 |
expect_equal(metadata$Feature, c("RNA,Niche", "RNA,Niche")) |
|
|
117 |
xenium_data3 <- getNicheAssay(xenium_data2, assay = "Assay1", label = "clusters", graph.type = "radius") |
|
|
118 |
metadata <- vrFeatureTypeNames(xenium_data3, assay = "all") |
|
|
119 |
expect_equal(metadata$Feature, c("RNA,Niche", "RNA")) |
|
|
120 |
|
|
|
121 |
# clean |
|
|
122 |
rm(xenium_data2) |
|
|
123 |
rm(xenium_data3) |
|
|
124 |
|
|
|
125 |
}) |
|
|
126 |
|
|
|
127 |
test_that("neighborhood analysis", { |
|
|
128 |
|
|
|
129 |
data("xenium_data") |
|
|
130 |
|
|
|
131 |
xenium_data <- getSpatialNeighbors(xenium_data, method = "delaunay", verbose = FALSE) |
|
|
132 |
results <- vrNeighbourhoodEnrichment(xenium_data, group.by = "clusters", graph.type = "delaunay") |
|
|
133 |
expect_error(results <- vrNeighbourhoodEnrichment(xenium_data, group.by = "clusters2", graph.type = "delaunay")) |
|
|
134 |
}) |