Skip to content

Commit

Permalink
Merge pull request #123 from KrishnaswamyLab/dev
Browse files Browse the repository at this point in the history
Update to MAGIC v1.2.0
  • Loading branch information
scottgigante authored Jul 24, 2018
2 parents e88d508 + 936d75e commit eb3c0b9
Show file tree
Hide file tree
Showing 50 changed files with 3,808 additions and 4,407 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ python/*.dll
python/*.egg-info
python/magic/__pycache__
python/magic/*.pyc
python/tutorial_notebooks/.ipynb_checkpoints

matlab/EMT.csv

# Mac detritus
Expand Down
37 changes: 19 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
Markov Affinity-based Graph Imputation of Cells (MAGIC)
-------------------------------------------------------

[![Latest PyPI version](https://img.shields.io/pypi/v/magic-impute.svg)](https://pypi.org/project/magic-impute/)
[![Latest CRAN version](https://img.shields.io/cran/v/Rmagic.svg)](https://cran.r-project.org/package=Rmagic)
[![Travis CI Build](https://api.travis-ci.com/KrishnaswamyLab/MAGIC.svg?branch=master)](https://travis-ci.com/KrishnaswamyLab/MAGIC)
[![Read the Docs](https://img.shields.io/readthedocs/magic.svg)](https://magic.readthedocs.io/)
Expand Down Expand Up @@ -47,7 +48,7 @@ MAGIC has been implemented in Python, Matlab, and R.

To install with `pip`, run the following from a terminal:

pip install --user git+git://github.com/KrishnaswamyLab/MAGIC.git#subdirectory=python
pip install --user magic-impute

#### Installation from GitHub

Expand All @@ -63,15 +64,15 @@ To clone the repository and install manually, run the following from a terminal:

The following code runs MAGIC on test data located in the MAGIC repository.

import magic
import pandas as pd
import matplotlib.pyplot as plt
X = pd.read_csv("MAGIC/data/test_data.csv")
magic_operator = magic.MAGIC()
X_magic = magic_operator.fit_transform(X, genes=['VIM', 'CDH1', 'ZEB1'])
plt.scatter(X_magic['VIM'], X_magic['CDH1'], c=X_magic['ZEB1'], s=1, cmap='inferno')
plt.show()
magic.plot.animate_magic(X, gene_x='VIM', gene_y='CDH1', gene_color='ZEB1', operator=magic_operator)
import magic
import pandas as pd
import matplotlib.pyplot as plt
X = pd.read_csv("MAGIC/data/test_data.csv")
magic_operator = magic.MAGIC()
X_magic = magic_operator.fit_transform(X, genes=['VIM', 'CDH1', 'ZEB1'])
plt.scatter(X_magic['VIM'], X_magic['CDH1'], c=X_magic['ZEB1'], s=1, cmap='inferno')
plt.show()
magic.plot.animate_magic(X, gene_x='VIM', gene_y='CDH1', gene_color='ZEB1', operator=magic_operator)

#### Tutorials

Expand Down Expand Up @@ -106,7 +107,7 @@ In R, run this command to install MAGIC and all dependencies:
In a terminal, run the following command to install the Python
repository.

pip install --user git+git://github.com/KrishnaswamyLab/MAGIC.git#subdirectory=python
pip install --user magic-impute

#### Installation from GitHub

Expand All @@ -124,16 +125,16 @@ To clone the repository and install manually, run the following from a terminal:

After installing the package, MAGIC can be run by loading the library and calling `magic()`:

library(Rmagic)
library(ggplot2)
data(magic_testdata)
MAGIC_data <- magic(magic_testdata, genes=c("VIM", "CDH1", "ZEB1"))
ggplot(MAGIC_data) +
geom_point(aes(x=VIM, y=CDH1, color=ZEB1))
library(Rmagic)
library(ggplot2)
data(magic_testdata)
MAGIC_data <- magic(magic_testdata, genes=c("VIM", "CDH1", "ZEB1"))
ggplot(MAGIC_data) +
geom_point(aes(x=VIM, y=CDH1, color=ZEB1))

#### Tutorials

For a working example, see the Rmarkdown tutorials at https://github.com/KrishnaswamyLab/MAGIC/blob/master/Rmagic/inst/examples/bonemarrow_tutorial.md and https://github.com/KrishnaswamyLab/MAGIC/blob/master/Rmagic/inst/examples/EMT_tutorial.md or in `R/inst/examples`.
For a working example, see the Rmarkdown tutorials at <http://htmlpreview.github.io/?https://github.com/KrishnaswamyLab/MAGIC/blob/master/Rmagic/inst/examples/bonemarrow_tutorial.html> and <http://htmlpreview.github.io/?https://github.com/KrishnaswamyLab/MAGIC/blob/master/Rmagic/inst/examples/EMT_tutorial.hmtl> or in `Rmagic/inst/examples`.

## Help

Expand Down
6 changes: 3 additions & 3 deletions Rmagic/R/magic.R
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#' Perform MAGIC on a data matrix
#'
#'
#' Markov Affinity-based Graph Imputation of Cells (MAGIC) is an
#' algorithm for denoising and transcript recover of single cells
#' applied to single-cell RNA sequencing data, as described in
Expand All @@ -18,7 +18,7 @@
#' power to which the diffusion operator is powered
#' sets the level of diffusion. If 'auto', t is selected according to the
#' Procrustes disparity of the diffused data.'
#' @param npca number of PCA components that should be used; default: 20.
#' @param npca number of PCA components that should be used; default: 100.
#' @param init magic object, optional
#' object to use for initialization. Avoids recomputing
#' intermediate steps if parameters are the same.
Expand Down Expand Up @@ -83,7 +83,7 @@ magic <- function(data,
seed=NULL) {
# check installation
if (!reticulate::py_module_available(module = "magic")) {
install.magic()
load_pymagic()
}
tryCatch(pymagic, error = function(e) load_pymagic())
k <- as.integer(k)
Expand Down
32 changes: 21 additions & 11 deletions Rmagic/R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,20 @@ null_equal <- function(x, y) {
}
}

load_pymagic <- function() {
pymagic <<- reticulate::import("magic", delay_load = TRUE)
load_pymagic <- function(delay_load = FALSE) {
result <- try(pymagic <<- reticulate::import("magic", delay_load = delay_load))
if (methods::is(result, "try-error")) {
install.magic()
}
}

#' Install PHATE Python Package
#' Install MAGIC Python Package
#'
#' Install PHATE Python package into a virtualenv or conda env.
#' Install MAGIC Python package into a virtualenv or conda env.
#'
#' On Linux and OS X the "virtualenv" method will be used by default
#' ("conda" will be used if virtualenv isn't available). On Windows,
#' the "conda" method is always used.
#' As of reticulate v1.7, this functionality is only available in the
#' development version of reticulate, which can be installed using
#' `devtools::install_github('rstudio/reticulate')`
#'
#' @param envname Name of environment to install packages into
#' @param method Installation method. By default, "auto" automatically finds
Expand All @@ -38,14 +38,24 @@ load_pymagic <- function() {
#' @export
install.magic <- function(envname = "r-reticulate", method = "auto",
conda = "auto", pip=TRUE, ...) {
stop(paste0(
"Cannot install MAGIC, please install from a console with ",
"pip install --user git+git://github.com/KrishnaswamyLab/MAGIC.git#subdirectory=python"
message("Attempting to install MAGIC python package with reticulate")
tryCatch({
reticulate::py_install("magic-impute",
envname = envname, method = method,
conda = conda, pip=pip, ...
)
},
error = function(e) {
stop(paste0(
"Cannot locate MAGIC Python package, please install through pip ",
"(e.g. pip install magic-impute)."
))
}
)
}

pymagic <- NULL

.onLoad <- function(libname, pkgname) {
load_pymagic()
load_pymagic(delay_load = TRUE)
}
7 changes: 4 additions & 3 deletions Rmagic/README.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ knitr::opts_chunk$set(
)
```

[![Latest PyPI version](https://img.shields.io/pypi/v/magic-impute.svg)](https://pypi.org/project/magic-impute/)
[![Latest CRAN version](https://img.shields.io/cran/v/Rmagic.svg)](https://cran.r-project.org/package=Rmagic)
[![Travis CI Build](https://api.travis-ci.com/KrishnaswamyLab/MAGIC.svg?branch=master)](https://travis-ci.com/KrishnaswamyLab/MAGIC)
[![Read the Docs](https://img.shields.io/readthedocs/magic.svg)](https://magic.readthedocs.io/)
Expand All @@ -37,8 +38,8 @@ Markov Affinity-based Graph Imputation of Cells (MAGIC) is an algorithm for deno
* MAGIC can be performed on a variety of datasets
* Here, we show the usage of MAGIC on a toy dataset
* You can view further examples of MAGIC on real data in our notebooks under `inst/examples`:
* https://github.com/KrishnaswamyLab/MAGIC/blob/master/Rmagic/inst/examples/EMT_tutorial.md
* https://github.com/KrishnaswamyLab/MAGIC/blob/master/Rmagic/inst/examples/bonemarrow_tutorial.md
* http://htmlpreview.github.io/?https://github.com/KrishnaswamyLab/MAGIC/blob/master/Rmagic/inst/examples/EMT_tutorial.html
* http://htmlpreview.github.io/?https://github.com/KrishnaswamyLab/MAGIC/blob/master/Rmagic/inst/examples/bonemarrow_tutorial.html

### Installation

Expand All @@ -57,7 +58,7 @@ install.packages("Rmagic")
In a terminal, run the following command to install the Python repository.

```{bash install_python_magic, eval=FALSE}
pip install --user git+git://github.com/KrishnaswamyLab/MAGIC.git#subdirectory=python
pip install --user magic-impute
```

#### Installaton from source
Expand Down
7 changes: 4 additions & 3 deletions Rmagic/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ Rmagic v1.0.0

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

[![Latest PyPI version](https://img.shields.io/pypi/v/magic-impute.svg)](https://pypi.org/project/magic-impute/)
[![Latest CRAN version](https://img.shields.io/cran/v/Rmagic.svg)](https://cran.r-project.org/package=Rmagic)
[![Travis CI
Build](https://api.travis-ci.com/KrishnaswamyLab/MAGIC.svg?branch=master)](https://travis-ci.com/KrishnaswamyLab/MAGIC)
Expand Down Expand Up @@ -36,8 +37,8 @@ Diffusion*, Cell
- You can view further examples of MAGIC on real data in our notebooks
under
`inst/examples`:
- <https://github.com/KrishnaswamyLab/MAGIC/blob/master/Rmagic/inst/examples/EMT_tutorial.md>
- <https://github.com/KrishnaswamyLab/MAGIC/blob/master/Rmagic/inst/examples/bonemarrow_tutorial.md>
- <http://htmlpreview.github.io/?https://github.com/KrishnaswamyLab/MAGIC/blob/master/Rmagic/inst/examples/EMT_tutorial.html>
- <http://htmlpreview.github.io/?https://github.com/KrishnaswamyLab/MAGIC/blob/master/Rmagic/inst/examples/bonemarrow_tutorial.html>

### Installation

Expand All @@ -59,7 +60,7 @@ In a terminal, run the following command to install the Python
repository.

``` bash
pip install --user git+git://github.com/KrishnaswamyLab/MAGIC.git#subdirectory=python
pip install --user magic-impute
```

#### Installaton from source
Expand Down
Binary file modified Rmagic/inst/examples/BMMSC_data_R_after_magic.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified Rmagic/inst/examples/BMMSC_data_R_pca_colored_by_magic.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified Rmagic/inst/examples/BMMSC_data_R_phate_colored_by_magic.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified Rmagic/inst/examples/EMT_data_R_after_magic.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified Rmagic/inst/examples/EMT_data_R_pca_colored_by_magic.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
29 changes: 18 additions & 11 deletions Rmagic/inst/examples/bonemarrow_tutorial.Rmd
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
---
title: "Rmagic Bone Marrow Tutorial"
output:
github_document: default
toc: true
html_document:
df_print: paged
toc: yes
toc_depth: '3'
---

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

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
Expand Down Expand Up @@ -34,7 +36,7 @@ if (!require(Rmagic)) devtools::install_github("KrishnaswamyLab/magic/Rmagic")
In a terminal, run the following command to install the Python repository.

```{bash install_python_magic, eval=FALSE}
pip install --user git+git://github.com/KrishnaswamyLab/MAGIC.git#subdirectory=python
pip install --user magic-impute
```

We'll install a couple more tools for this tutorial.
Expand Down Expand Up @@ -66,7 +68,7 @@ library(phateR)

### Loading data

In this tutorial, we will analyse myeloid and erythroid cells in mouse bone marrow, as described in Paul et al., 2015. The example data is located in the PHATE Github repository and we can load it directly from the web.
In this tutorial, we will analyse myeloid and erythroid cells in mouse bone marrow, as described in Paul et al., 2015. The example data is located in the PHATE Github repository and we can load it directly from the web. You can run this tutorial with your own data by downloading <https://raw.githubusercontent.com/KrishnaswamyLab/MAGIC/master/Rmagic/inst/examples/bonemarrow_tutorial.Rmd> and opening it in RStudio.

```{r load_data}
# load data
Expand All @@ -75,6 +77,8 @@ bmmsc <- bmmsc[,2:ncol(bmmsc)]
bmmsc[1:5,1:10]
```

### Filtering data

First, we need to remove lowly expressed genes and cells with small library size.

```{r}
Expand All @@ -93,6 +97,7 @@ keep_rows <- rowSums(bmmsc) > 1000
bmmsc <- bmmsc[keep_rows,]
```

### Normalizing data

We should library size normalize and transform the data prior to MAGIC. Many people use a log transform, which requires adding a "pseudocount" to avoid log(0). We square root instead, which has a similar form but doesn't suffer from instabilities at zero.

Expand All @@ -114,29 +119,31 @@ We can plot the data before and after MAGIC to visualize the results.

```{r plot_raw}
ggplot(bmmsc) +
geom_point(aes(Mpo, Klf1, colour=Ifitm1)) +
scale_colour_viridis(option="B")
geom_point(aes(Mpo, Klf1, color=Ifitm1)) +
scale_color_viridis(option="B")
ggsave('BMMSC_data_R_before_magic.png', width=5, height=5)
```

The data suffers from dropout to the point that we cannot infer anything about the gene-gene relationships.

```{r plot_magic}
ggplot(bmmsc_MAGIC) +
geom_point(aes(Mpo, Klf1, colour=Ifitm1)) +
scale_colour_viridis(option="B")
geom_point(aes(Mpo, Klf1, color=Ifitm1)) +
scale_color_viridis(option="B")
```

As you can see, the gene-gene relationships are much clearer after MAGIC. These relationships also match the biological progression we expect to see - Ifitm1 is a stem cell marker, Klf1 is an erythroid marker, and Mpo is a myeloid marker.

### Rerunning MAGIC with new parameters

The data is a little too smooth - we can decrease `t` from the automatic value to reduce the amount of diffusion. We pass the original result to the argument `init` to avoid recomputing intermediate steps.

```{r decrease_t}
bmmsc_MAGIC <- magic(bmmsc, genes=c("Mpo", "Klf1", "Ifitm1"),
t=4, init=bmmsc_MAGIC)
ggplot(bmmsc_MAGIC) +
geom_point(aes(Mpo, Klf1, colour=Ifitm1)) +
scale_colour_viridis(option="B")
geom_point(aes(Mpo, Klf1, color=Ifitm1)) +
scale_color_viridis(option="B")
ggsave('BMMSC_data_R_after_magic.png', width=5, height=5)
```

Expand Down
1,783 changes: 1,783 additions & 0 deletions Rmagic/inst/examples/bonemarrow_tutorial.html

Large diffs are not rendered by default.

Loading

0 comments on commit eb3c0b9

Please sign in to comment.