Skip to content

Commit c1fa4ed

Browse files
authored
Merge pull request #141 from KrishnaswamyLab/dev
MAGIC v1.4.0
2 parents f640e6f + 1bab041 commit c1fa4ed

File tree

10 files changed

+1092
-798
lines changed

10 files changed

+1092
-798
lines changed

Rmagic/DESCRIPTION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
Package: Rmagic
22
Type: Package
33
Title: MAGIC - Markov Affinity-Based Graph Imputation of Cells
4-
Version: 1.3.0
4+
Version: 1.4.0
55
Authors@R: c(person(given = "David", family = "van Dijk", email = "davidvandijk@gmail.com", role = c("aut")),
66
person(given = 'Scott', family = 'Gigante', email = 'scott.gigante@yale.edu', role = 'cre',
77
comment = c(ORCID = '0000-0002-4544-2764')))

Rmagic/R/magic.R

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
#' applied to single-cell RNA sequencing data, as described in
66
#' van Dijk et al, 2018.
77
#'
8-
#' @param data input data matrix
8+
#' @param data input data matrix or Seurat object
99
#' @param genes character or integer vector, default: NULL
1010
#' vector of column names or column indices for which to return smoothed data
1111
#' If 'all_genes' or NULL, the entire smoothed matrix is returned
@@ -38,6 +38,11 @@
3838
#' n_jobs = -2, all CPUs but one are used
3939
#' @param seed int or `NULL`, random state (default: `NULL`)
4040
#'
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+
#'
4146
#' @examples
4247
#' if (reticulate::py_module_available("magic")) {
4348
#'
@@ -113,7 +118,12 @@ magic <- function(data,
113118
if (is.numeric(verbose)) {
114119
verbose <- as.integer(verbose)
115120
}
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")) {
117127
data <- as.matrix(data)
118128
}
119129
if (is.null(genes) || is.na(genes)) {
@@ -170,13 +180,18 @@ magic <- function(data,
170180
result <- operator$fit_transform(data,
171181
genes = genes,
172182
t_max = t.max)
173-
result <- as.data.frame(result)
174183
colnames(result) <- gene_names
175184
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+
}
180195
}
181196

182197

Rmagic/README.Rmd

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
title : Rmagic v1.3.0
2+
title : Rmagic v1.4.0
33
output: github_document
44
toc: true
55
---

Rmagic/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
Rmagic v1.3.0
1+
Rmagic v1.4.0
22
================
33

44
<!-- README.md is generated from README.Rmd. Please edit that file -->

Rmagic/tests/test_magic.R

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,14 @@ test_magic <- function() {
2424
scale_colour_viridis(option="B")
2525
ggsave('EMT_data_R_after_magic.png', plot=p_m, width=5, height=5)
2626
}
27+
28+
test_seurat <- function() {
29+
data(magic_testdata)
30+
31+
seurat_obj <- Seurat::CreateSeuratObject(raw.data=t(magic_testdata))
32+
33+
# run MAGIC
34+
data_MAGIC <- magic(magic_testdata, seed = 42)
35+
seurat_MAGIC <- magic(seurat_obj, seed = 42)
36+
stopifnot(all(data_MAGIC$result == t(seurat_MAGIC@data)))
37+
}

python/doc/source/requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,6 @@ scipy>=1.1.0
55
matplotlib>=2.0.1
66
future
77
graphtools>=0.1.8
8+
scprep>=0.7.1
89
sphinx
910
sphinxcontrib-napoleon

python/magic/magic.py

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -521,8 +521,24 @@ def fit_transform(self, X, graph=None, **kwargs):
521521
If given, provides a precomputed kernel matrix with which to
522522
perform diffusion.
523523
524-
kwargs : further arguments for `PHATE.transform()`
525-
Keyword arguments as specified in :func:`~phate.PHATE.transform`
524+
genes : list or {"all_genes", "pca_only"}, optional (default: None)
525+
List of genes, either as integer indices or column names
526+
if input data is a pandas DataFrame. If "all_genes", the entire
527+
smoothed matrix is returned. If "pca_only", PCA on the smoothed
528+
data is returned. If None, the entire matrix is also
529+
returned, but a warning may be raised if the resultant matrix
530+
is very large.
531+
532+
t_max : int, optional, default: 20
533+
maximum t to test if `t` is set to 'auto'
534+
535+
plot_optimal_t : boolean, optional, default: False
536+
If true and `t` is set to 'auto', plot the disparity used to
537+
select t
538+
539+
ax : matplotlib.axes.Axes, optional
540+
If given and `plot_optimal_t` is true, plot will be drawn
541+
on the given axis.
526542
527543
Returns
528544
-------

python/magic/version.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
__version__ = "1.3.0"
1+
__version__ = "1.4.0"

0 commit comments

Comments
 (0)