Skip to content

Commit

Permalink
Merge pull request #165 from KrishnaswamyLab/dev
Browse files Browse the repository at this point in the history
MAGIC v1.5.6
  • Loading branch information
scottgigante committed Oct 10, 2019
2 parents 84c052d + 0a51509 commit 32724af
Show file tree
Hide file tree
Showing 9 changed files with 117 additions and 63 deletions.
11 changes: 6 additions & 5 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,11 @@ r:

addons:
apt:
sources:
- deadsnakes
packages:
- libhdf5-dev
- python3.6

cache:
- packages
Expand All @@ -27,18 +30,16 @@ cache:

before_install:
- curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py
- sudo python3 get-pip.py
- sudo python3.6 get-pip.py
- mkdir -p ~/bin && export PATH=~/bin/:$PATH
- ln -s $(which python3) ~/bin/python
- ln -s $(which pip3) ~/bin/pip
- ln -s $(which python3.6) ~/bin/python
- ln -s $(which pip3.6) ~/bin/pip
- sudo pip install --upgrade pip
- pip install --user -q phate

install:
- cd python; pip install --user -q .
- cd ../Rmagic; R -e 'install.packages("devtools", repos="http://cloud.r-project.org")'
- R_VERSION=$(R -e "message(R.version[['minor']])" 2>&1 1>/dev/null | head -c 1)
- if [ $R_VERSION -lt 5 ]; then export _R_CHECK_FORCE_SUGGESTS_="FALSE"; else R -e "devtools::install_github(repo = 'satijalab/seurat', ref = 'release/3.0')"; fi
- R -e 'devtools::install_deps(dep = T, upgrade="always")'
- cd ..

Expand Down
127 changes: 81 additions & 46 deletions Rmagic/R/magic.R
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
#' * **params**: Parameters passed to magic
#'
#' @examples
#' if (reticulate::py_module_available("magic")) {
#' if (pymagic_is_available()) {
#'
#' data(magic_testdata)
#'
Expand Down Expand Up @@ -76,7 +76,7 @@
#'
#' }
#'
#' if (reticulate::py_module_available("magic") && require(Seurat)) {
#' if (pymagic_is_available() && require(Seurat)) {
#'
#' data(magic_testdata)
#'
Expand Down Expand Up @@ -255,22 +255,41 @@ magic.seurat <- function(
seed = NULL,
...
) {
results <- magic(
data = as.matrix(x = t(x = data@data)),
genes = genes,
knn = knn,
decay = decay,
t = t,
npca = npca,
init = init,
t.max = t.max,
knn.dist.method = knn.dist.method,
verbose = verbose,
n.jobs = n.jobs,
seed = seed
)
data@data <- t(x = as.matrix(x = results$result))
return(data)
if (requireNamespace("Seurat", quietly = TRUE)) {
results <- magic(
data = as.matrix(x = t(x = data@data)),
genes = genes,
knn = knn,
decay = decay,
t = t,
npca = npca,
init = init,
t.max = t.max,
knn.dist.method = knn.dist.method,
verbose = verbose,
n.jobs = n.jobs,
seed = seed
)
data@data <- t(x = as.matrix(x = results$result))
return(data)
} else {
message("Seurat package not available. Running default MAGIC implementation.")
return(magic(
data,
genes = genes,
knn = knn,
decay = decay,
t = t,
npca = npca,
init = init,
t.max = t.max,
knn.dist.method = knn.dist.method,
verbose = verbose,
n.jobs = n.jobs,
seed = seed,
...
))
}
}

#' @param assay Assay to use for imputation, defaults to the default assay
Expand All @@ -295,32 +314,48 @@ magic.Seurat <- function(
seed = NULL,
...
) {
if (!requireNamespace(package = 'Seurat', quietly = TRUE)) {
stop("Please install Seurat v3 to run MAGIC on new Seurat objects")
}
if (is.null(x = assay)) {
assay <- Seurat::DefaultAssay(object = data)
if (requireNamespace("Seurat", quietly = TRUE)) {
if (is.null(x = assay)) {
assay <- Seurat::DefaultAssay(object = data)
}
results <- magic(
data = t(x = Seurat::GetAssayData(object = data, slot = 'data', assay = assay)),
genes = genes,
knn = knn,
decay = decay,
t = t,
npca = npca,
init = init,
t.max = t.max,
knn.dist.method = knn.dist.method,
verbose = verbose,
n.jobs = n.jobs,
seed = seed
)
assay_name <- paste0('MAGIC_', assay)
data[[assay_name]] <- Seurat::CreateAssayObject(data = t(x = as.matrix(x = results$result)))
print(paste0("Added MAGIC output to ", assay_name, ". To use it, pass assay='", assay_name,
"' to downstream methods or set seurat_object@active.assay <- '", assay_name, "'."))
Seurat::Tool(object = data) <- results[c('operator', 'params')]
return(data)
} else {
message("Seurat package not available. Running default MAGIC implementation.")
return(magic(
data,
genes = genes,
knn = knn,
decay = decay,
t = t,
npca = npca,
init = init,
t.max = t.max,
knn.dist.method = knn.dist.method,
verbose = verbose,
n.jobs = n.jobs,
seed = seed,
...
))
}
results <- magic(
data = t(x = Seurat::GetAssayData(object = data, slot = 'data', assay = assay)),
genes = genes,
knn = knn,
decay = decay,
t = t,
npca = npca,
init = init,
t.max = t.max,
knn.dist.method = knn.dist.method,
verbose = verbose,
n.jobs = n.jobs,
seed = seed
)
assay_name <- paste0('MAGIC_', assay)
data[[assay_name]] <- Seurat::CreateAssayObject(data = t(x = as.matrix(x = results$result)))
print(paste0("Added MAGIC output to ", assay_name, ". To use it, pass assay='", assay_name,
"' to downstream methods or set seurat_object@active.assay <- '", assay_name, "'."))
Seurat::Tool(object = data) <- results[c('operator', 'params')]
return(data)
}

#' Print a MAGIC object
Expand All @@ -330,7 +365,7 @@ magic.Seurat <- function(
#' @param x A fitted MAGIC object
#' @param ... Arguments for print()
#' @examples
#' if (reticulate::py_module_available("magic")) {
#' if (pymagic_is_available()) {
#'
#' data(magic_testdata)
#' data_magic <- magic(magic_testdata)
Expand Down Expand Up @@ -359,7 +394,7 @@ print.magic <- function(x, ...) {
#' @param object A fitted MAGIC object
#' @param ... Arguments for summary()
#' @examples
#' if (reticulate::py_module_available("magic")) {
#' if (pymagic_is_available()) {
#'
#' data(magic_testdata)
#' data_magic <- magic(magic_testdata)
Expand Down Expand Up @@ -413,7 +448,7 @@ as.data.frame.magic <- function(x, ...) {
#' @param data A fitted MAGIC object
#' @param ... Arguments for ggplot()
#' @examples
#' if (reticulate::py_module_available("magic") && require(ggplot2)) {
#' if (pymagic_is_available() && require(ggplot2)) {
#'
#' data(magic_testdata)
#' data_magic <- magic(magic_testdata, genes=c("VIM", "CDH1", "ZEB1"))
Expand Down
17 changes: 17 additions & 0 deletions Rmagic/R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,23 @@ load_pymagic <- function() {
}
}

#' Check whether MAGIC Python package is available and can be loaded
#'
#' This is used primarily to avoid running tests on CRAN
#' and elsewhere where the Python package should not be
#' installed.
#'
#' @export
pymagic_is_available <- function() {
tryCatch({
reticulate::py_module_available("magic")$MAGIC
},
error = function(e) {
FALSE
}
)
}

#' Install MAGIC Python Package
#'
#' Install MAGIC Python package into a virtualenv or conda env.
Expand Down
4 changes: 2 additions & 2 deletions python/doc/source/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
scikit-learn>=0.19.1
numpy>=1.14.0
pandas>=0.21.0
pandas>=0.25
scprep>=1.0
scipy>=1.1.0
matplotlib>=2.0.1
future
graphtools>=1.0.0
scprep>=0.10.0
sphinx
sphinxcontrib-napoleon
3 changes: 2 additions & 1 deletion python/magic/magic.py
Original file line number Diff line number Diff line change
Expand Up @@ -668,7 +668,8 @@ def _impute(self, data, t_max=20, plot=False, ax=None,
# than those of the data matrix. (M^t) * D
if (t_opt is not None) and \
(self.diff_op.shape[1] < data_imputed.shape[1]):
diff_op_t = np.linalg.matrix_power(self.diff_op, t_opt)
diff_op_t = np.linalg.matrix_power(
scprep.utils.toarray(self.diff_op), t_opt)
data_imputed = diff_op_t.dot(data_imputed)

# fast magic
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.5.5"
__version__ = "1.5.6"
4 changes: 2 additions & 2 deletions python/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
numpy>=1.14.0
pandas>=0.21.0,<0.24
scipy>=1.1.0
pandas>=0.25
scprep>=1.0
matplotlib
scikit-learn>=0.19.1
future
tasklogger>=0.4.0
graphtools>=1.0.0
scprep>=0.11.1
4 changes: 2 additions & 2 deletions python/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@

install_requires = [
'numpy>=1.14.0',
'pandas>=0.21.0,<0.24',
'scipy>=1.1.0',
'matplotlib',
'scikit-learn>=0.19.1',
'future',
'tasklogger>=0.4.0',
'graphtools>=1.0.0',
'scprep>=0.11.1'
'pandas>=0.25',
'scprep>=1.0',
]

test_requires = [
Expand Down
8 changes: 4 additions & 4 deletions python/test/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@


def test_scdata():
scdata = scprep.io.load_csv("../data/test_data.csv")
scdata = scprep.filter.remove_empty_cells(scdata)
scdata = scprep.filter.remove_empty_genes(scdata)
scdata = scprep.io.load_csv("../data/test_data.csv", cell_names=False)
scdata = scprep.filter.filter_empty_cells(scdata)
scdata = scprep.filter.filter_empty_genes(scdata)
scdata_norm = scprep.normalize.library_size_normalize(scdata)
scdata_norm = scprep.transform.sqrt(scdata_norm)
assert scdata.shape == scdata_norm.shape
Expand All @@ -37,7 +37,7 @@ def test_scdata():
genes="all_genes")
assert scdata_norm.shape == magic_all_genes.shape
dremi = magic_op.knnDREMI("VIM", "ZEB1", plot=True)
np.testing.assert_allclose(dremi, 1.5687165, atol=0.0000005)
np.testing.assert_allclose(dremi, 1.573619, atol=0.0000005)


def test_anndata():
Expand Down

0 comments on commit 32724af

Please sign in to comment.