|
5 | 5 | #' applied to single-cell RNA sequencing data, as described in |
6 | 6 | #' van Dijk et al, 2018. |
7 | 7 | #' |
8 | | -#' @param data input data matrix |
| 8 | +#' @param data input data matrix or Seurat object |
9 | 9 | #' @param genes character or integer vector, default: NULL |
10 | 10 | #' vector of column names or column indices for which to return smoothed data |
11 | 11 | #' If 'all_genes' or NULL, the entire smoothed matrix is returned |
|
38 | 38 | #' n_jobs = -2, all CPUs but one are used |
39 | 39 | #' @param seed int or `NULL`, random state (default: `NULL`) |
40 | 40 | #' |
| 41 | +#' @return If a Seurat object is passed, a Seurat object is returned. Otherwise, a "magic" object containing: |
| 42 | +#' * **result**: matrix containing smoothed expression values |
| 43 | +#' * **operator**: The MAGIC operator (python magic.MAGIC object) |
| 44 | +#' * **params**: Parameters passed to magic |
| 45 | +#' |
41 | 46 | #' @examples |
42 | 47 | #' if (reticulate::py_module_available("magic")) { |
43 | 48 | #' |
@@ -113,7 +118,12 @@ magic <- function(data, |
113 | 118 | if (is.numeric(verbose)) { |
114 | 119 | verbose <- as.integer(verbose) |
115 | 120 | } |
116 | | - if (!methods::is(data, "Matrix")) { |
| 121 | + use_seurat <- FALSE |
| 122 | + if (methods::is(data, "seurat")) { |
| 123 | + seurat_obj <- data |
| 124 | + use_seurat <- TRUE |
| 125 | + data <- t(data@data) |
| 126 | + } else if (!methods::is(data, "Matrix")) { |
117 | 127 | data <- as.matrix(data) |
118 | 128 | } |
119 | 129 | if (is.null(genes) || is.na(genes)) { |
@@ -170,13 +180,18 @@ magic <- function(data, |
170 | 180 | result <- operator$fit_transform(data, |
171 | 181 | genes = genes, |
172 | 182 | t_max = t.max) |
173 | | - result <- as.data.frame(result) |
174 | 183 | colnames(result) <- gene_names |
175 | 184 | rownames(result) <- rownames(data) |
176 | | - result <- list("result" = result, "operator" = operator, |
177 | | - "params" = params) |
178 | | - class(result) <- c("magic", "list") |
179 | | - return(result) |
| 185 | + if (use_seurat) { |
| 186 | + seurat_obj@data <- t(result) |
| 187 | + return(seurat_obj) |
| 188 | + } else { |
| 189 | + result <- as.data.frame(result) |
| 190 | + result <- list("result" = result, "operator" = operator, |
| 191 | + "params" = params) |
| 192 | + class(result) <- c("magic", "list") |
| 193 | + return(result) |
| 194 | + } |
180 | 195 | } |
181 | 196 |
|
182 | 197 |
|
|
0 commit comments