... | ... |
@@ -22,8 +22,10 @@ |
22 | 22 |
#' over |
23 | 23 |
#' |
24 | 24 |
#' @examples |
25 |
+#' ## Mock up a SpatialExperiment object wit 400 cells and 3 genes |
|
25 | 26 |
#' set.seed(42) |
26 |
-#' mock <- mockSVG(size = 10, tot_genes = 500, de_genes = 10) |
|
27 |
+#' mock <- mockSVG(size = 20, tot_genes = 3, de_genes = 1) |
|
28 |
+#' |
|
27 | 29 |
#' stabilized <- stabilize(mock$counts) |
28 | 30 |
#' sample_info <- mock$coordinates |
29 | 31 |
#' sample_info$total_counts <- colSums(mock$counts) |
... | ... |
@@ -49,7 +49,7 @@ run <- function(x, coordinates, verbose = FALSE) { |
49 | 49 |
|
50 | 50 |
proc <- basiliskStart(spatialDE_env, testload="scipy.optimize") |
51 | 51 |
|
52 |
- .importPyModule(proc, !verbose, .set_fake_tqdm, .set_real_tqdm) |
|
52 |
+ .importPyModule(proc, !verbose) |
|
53 | 53 |
.spatialDE_run(proc, x, coordinates) |
54 | 54 |
|
55 | 55 |
out <- basiliskRun(proc, function(store) { |
... | ... |
@@ -40,29 +40,40 @@ |
40 | 40 |
#' @export |
41 | 41 |
#' @importFrom checkmate assert_data_frame assert_names assert_matrix |
42 | 42 |
#' @importFrom checkmate assert_flag |
43 |
+#' @importFrom basilisk basiliskStart basiliskRun |
|
43 | 44 |
run <- function(x, coordinates, verbose = FALSE) { |
44 | 45 |
assert_data_frame(coordinates, any.missing = FALSE) |
45 | 46 |
assert_names(colnames(coordinates), identical.to = c("x", "y")) |
46 | 47 |
assert_matrix(x, any.missing = FALSE) |
47 | 48 |
assert_flag(verbose) |
48 |
- |
|
49 |
- out <- basilisk::basiliskRun( |
|
50 |
- env = spatialDE_env, |
|
51 |
- fun = .spatialDE_run, |
|
52 |
- x = x, |
|
53 |
- coordinates = coordinates, |
|
54 |
- verbose = verbose |
|
55 |
- ) |
|
49 |
+ |
|
50 |
+ proc <- basiliskStart(spatialDE_env, testload="scipy.optimize") |
|
51 |
+ |
|
52 |
+ .importPyModule(proc, !verbose, .set_fake_tqdm, .set_real_tqdm) |
|
53 |
+ .spatialDE_run(proc, x, coordinates) |
|
54 |
+ |
|
55 |
+ out <- basiliskRun(proc, function(store) { |
|
56 |
+ as.data.frame(store$de_results) |
|
57 |
+ }, persist=TRUE) |
|
58 |
+ |
|
56 | 59 |
out |
60 |
+ |
|
57 | 61 |
} |
58 | 62 |
|
59 | 63 |
#' @importFrom reticulate r_to_py |
60 |
-.spatialDE_run <- function(x, coordinates, verbose) { |
|
61 |
- spatialDE <- .importPyModule(!verbose) |
|
62 |
- |
|
63 |
- X <- r_to_py(coordinates) |
|
64 |
- ## Need to transpose counts for `spatialDE$run` |
|
65 |
- res_py <- r_to_py(as.data.frame(t(x))) |
|
66 |
- |
|
67 |
- spatialDE$run(X, res_py) |
|
64 |
+#' @importFrom basilisk basiliskRun |
|
65 |
+.spatialDE_run <- function(proc, x, coordinates) { |
|
66 |
+ basiliskRun(proc, function(x, coordinates, store) { |
|
67 |
+ spatialDE <- store$spatialDE |
|
68 |
+ |
|
69 |
+ X <- r_to_py(coordinates) |
|
70 |
+ ## Need to transpose counts for `spatialDE$run` |
|
71 |
+ res_py <- r_to_py(as.data.frame(t(x))) |
|
72 |
+ |
|
73 |
+ de_results <- spatialDE$run(X, res_py) |
|
74 |
+ |
|
75 |
+ store$de_results <- as.data.frame(de_results) |
|
76 |
+ |
|
77 |
+ invisible(NULL) |
|
78 |
+ }, x=x, coordinates=coordinates, persist=TRUE) |
|
68 | 79 |
} |
... | ... |
@@ -1,5 +1,3 @@ |
1 |
-# TODO: add more details regarding DE results format in @return |
|
2 |
- |
|
3 | 1 |
#' Perform SpatialDE test |
4 | 2 |
#' |
5 | 3 |
#' Wraps the `run` function from the |
... | ... |
@@ -11,7 +9,17 @@ |
11 | 9 |
#' Each row is a sample, the columns with coordinates must be named 'x' and 'y'. |
12 | 10 |
#' @param verbose `logical` controlling the display of the progress bar. |
13 | 11 |
#' |
14 |
-#' @return `data.frame` with DE results. |
|
12 |
+#' @return A `data.frame` with DE results where each row is a gene and columns |
|
13 |
+#' contain relevant statistics. |
|
14 |
+#' |
|
15 |
+#' The most important columns are: |
|
16 |
+#' |
|
17 |
+#' * `g`: the name of the gene |
|
18 |
+#' * `pval`: the p-value for spatial differential expression |
|
19 |
+#' * `qval`: the q-value, indicating significance after correcting for |
|
20 |
+#' multiple testing |
|
21 |
+#' * `l`: A parameter indicating the distance scale a gene changes expression |
|
22 |
+#' over |
|
15 | 23 |
#' |
16 | 24 |
#' @examples |
17 | 25 |
#' set.seed(42) |
1 | 1 |
new file mode 100644 |
... | ... |
@@ -0,0 +1,60 @@ |
1 |
+# TODO: add more details regarding DE results format in @return |
|
2 |
+ |
|
3 |
+#' Perform SpatialDE test |
|
4 |
+#' |
|
5 |
+#' Wraps the `run` function from the |
|
6 |
+#' [SpatialDE](https://github.com/Teichlab/SpatialDE) Python package. |
|
7 |
+#' |
|
8 |
+#' @param x `matrix`-like object of normalized counts. E.g. resulting from |
|
9 |
+#' [regress_out()]. |
|
10 |
+#' @param coordinates `data.frame` with sample coordinates. |
|
11 |
+#' Each row is a sample, the columns with coordinates must be named 'x' and 'y'. |
|
12 |
+#' @param verbose `logical` controlling the display of the progress bar. |
|
13 |
+#' |
|
14 |
+#' @return `data.frame` with DE results. |
|
15 |
+#' |
|
16 |
+#' @examples |
|
17 |
+#' set.seed(42) |
|
18 |
+#' mock <- mockSVG(size = 10, tot_genes = 500, de_genes = 10) |
|
19 |
+#' stabilized <- stabilize(mock$counts) |
|
20 |
+#' sample_info <- mock$coordinates |
|
21 |
+#' sample_info$total_counts <- colSums(mock$counts) |
|
22 |
+#' regressed <- regress_out(counts = stabilized, sample_info = sample_info) |
|
23 |
+#' |
|
24 |
+#' ## Run SpatialDE |
|
25 |
+#' de_results <- run(regressed, coordinates = mock$coordinates) |
|
26 |
+#' |
|
27 |
+#' @references |
|
28 |
+#' Svensson, V., Teichmann, S. & Stegle, O. |
|
29 |
+#' SpatialDE: identification of spatially variable genes. |
|
30 |
+#' Nat Methods 15, 343–346 (2018). \url{https://doi.org/10.1038/nmeth.4636} |
|
31 |
+#' |
|
32 |
+#' @export |
|
33 |
+#' @importFrom checkmate assert_data_frame assert_names assert_matrix |
|
34 |
+#' @importFrom checkmate assert_flag |
|
35 |
+run <- function(x, coordinates, verbose = FALSE) { |
|
36 |
+ assert_data_frame(coordinates, any.missing = FALSE) |
|
37 |
+ assert_names(colnames(coordinates), identical.to = c("x", "y")) |
|
38 |
+ assert_matrix(x, any.missing = FALSE) |
|
39 |
+ assert_flag(verbose) |
|
40 |
+ |
|
41 |
+ out <- basilisk::basiliskRun( |
|
42 |
+ env = spatialDE_env, |
|
43 |
+ fun = .spatialDE_run, |
|
44 |
+ x = x, |
|
45 |
+ coordinates = coordinates, |
|
46 |
+ verbose = verbose |
|
47 |
+ ) |
|
48 |
+ out |
|
49 |
+} |
|
50 |
+ |
|
51 |
+#' @importFrom reticulate r_to_py |
|
52 |
+.spatialDE_run <- function(x, coordinates, verbose) { |
|
53 |
+ spatialDE <- .importPyModule(!verbose) |
|
54 |
+ |
|
55 |
+ X <- r_to_py(coordinates) |
|
56 |
+ ## Need to transpose counts for `spatialDE$run` |
|
57 |
+ res_py <- r_to_py(as.data.frame(t(x))) |
|
58 |
+ |
|
59 |
+ spatialDE$run(X, res_py) |
|
60 |
+} |