Skip to content

Commit

Permalink
Merge pull request #141 from KrishnaswamyLab/dev
Browse files Browse the repository at this point in the history
MAGIC v1.4.0
  • Loading branch information
scottgigante authored Nov 22, 2018
2 parents f640e6f + 1bab041 commit c1fa4ed
Show file tree
Hide file tree
Showing 10 changed files with 1,092 additions and 798 deletions.
2 changes: 1 addition & 1 deletion Rmagic/DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Package: Rmagic
Type: Package
Title: MAGIC - Markov Affinity-Based Graph Imputation of Cells
Version: 1.3.0
Version: 1.4.0
Authors@R: c(person(given = "David", family = "van Dijk", email = "davidvandijk@gmail.com", role = c("aut")),
person(given = 'Scott', family = 'Gigante', email = 'scott.gigante@yale.edu', role = 'cre',
comment = c(ORCID = '0000-0002-4544-2764')))
Expand Down
29 changes: 22 additions & 7 deletions Rmagic/R/magic.R
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#' applied to single-cell RNA sequencing data, as described in
#' van Dijk et al, 2018.
#'
#' @param data input data matrix
#' @param data input data matrix or Seurat object
#' @param genes character or integer vector, default: NULL
#' vector of column names or column indices for which to return smoothed data
#' If 'all_genes' or NULL, the entire smoothed matrix is returned
Expand Down Expand Up @@ -38,6 +38,11 @@
#' n_jobs = -2, all CPUs but one are used
#' @param seed int or `NULL`, random state (default: `NULL`)
#'
#' @return If a Seurat object is passed, a Seurat object is returned. Otherwise, a "magic" object containing:
#' * **result**: matrix containing smoothed expression values
#' * **operator**: The MAGIC operator (python magic.MAGIC object)
#' * **params**: Parameters passed to magic
#'
#' @examples
#' if (reticulate::py_module_available("magic")) {
#'
Expand Down Expand Up @@ -113,7 +118,12 @@ magic <- function(data,
if (is.numeric(verbose)) {
verbose <- as.integer(verbose)
}
if (!methods::is(data, "Matrix")) {
use_seurat <- FALSE
if (methods::is(data, "seurat")) {
seurat_obj <- data
use_seurat <- TRUE
data <- t(data@data)
} else if (!methods::is(data, "Matrix")) {
data <- as.matrix(data)
}
if (is.null(genes) || is.na(genes)) {
Expand Down Expand Up @@ -170,13 +180,18 @@ magic <- function(data,
result <- operator$fit_transform(data,
genes = genes,
t_max = t.max)
result <- as.data.frame(result)
colnames(result) <- gene_names
rownames(result) <- rownames(data)
result <- list("result" = result, "operator" = operator,
"params" = params)
class(result) <- c("magic", "list")
return(result)
if (use_seurat) {
seurat_obj@data <- t(result)
return(seurat_obj)
} else {
result <- as.data.frame(result)
result <- list("result" = result, "operator" = operator,
"params" = params)
class(result) <- c("magic", "list")
return(result)
}
}


Expand Down
2 changes: 1 addition & 1 deletion Rmagic/README.Rmd
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
title : Rmagic v1.3.0
title : Rmagic v1.4.0
output: github_document
toc: true
---
Expand Down
2 changes: 1 addition & 1 deletion Rmagic/README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Rmagic v1.3.0
Rmagic v1.4.0
================

<!-- README.md is generated from README.Rmd. Please edit that file -->
Expand Down
11 changes: 11 additions & 0 deletions Rmagic/tests/test_magic.R
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,14 @@ test_magic <- function() {
scale_colour_viridis(option="B")
ggsave('EMT_data_R_after_magic.png', plot=p_m, width=5, height=5)
}

test_seurat <- function() {
data(magic_testdata)

seurat_obj <- Seurat::CreateSeuratObject(raw.data=t(magic_testdata))

# run MAGIC
data_MAGIC <- magic(magic_testdata, seed = 42)
seurat_MAGIC <- magic(seurat_obj, seed = 42)
stopifnot(all(data_MAGIC$result == t(seurat_MAGIC@data)))
}
1 change: 1 addition & 0 deletions python/doc/source/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@ scipy>=1.1.0
matplotlib>=2.0.1
future
graphtools>=0.1.8
scprep>=0.7.1
sphinx
sphinxcontrib-napoleon
20 changes: 18 additions & 2 deletions python/magic/magic.py
Original file line number Diff line number Diff line change
Expand Up @@ -521,8 +521,24 @@ def fit_transform(self, X, graph=None, **kwargs):
If given, provides a precomputed kernel matrix with which to
perform diffusion.
kwargs : further arguments for `PHATE.transform()`
Keyword arguments as specified in :func:`~phate.PHATE.transform`
genes : list or {"all_genes", "pca_only"}, optional (default: None)
List of genes, either as integer indices or column names
if input data is a pandas DataFrame. If "all_genes", the entire
smoothed matrix is returned. If "pca_only", PCA on the smoothed
data is returned. If None, the entire matrix is also
returned, but a warning may be raised if the resultant matrix
is very large.
t_max : int, optional, default: 20
maximum t to test if `t` is set to 'auto'
plot_optimal_t : boolean, optional, default: False
If true and `t` is set to 'auto', plot the disparity used to
select t
ax : matplotlib.axes.Axes, optional
If given and `plot_optimal_t` is true, plot will be drawn
on the given axis.
Returns
-------
Expand Down
2 changes: 1 addition & 1 deletion python/magic/version.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "1.3.0"
__version__ = "1.4.0"
Loading

0 comments on commit c1fa4ed

Please sign in to comment.