📖 Documentation: https://zaoqu-liu.github.io/MAGICR
MAGICR is a native R implementation of the MAGIC (Markov Affinity-based Graph Imputation of Cells) algorithm for denoising and imputation of single-cell RNA sequencing (scRNA-seq) data.
Single-cell RNA sequencing enables transcriptomic profiling at single-cell resolution, but technical limitations result in sparse count matrices with prevalent dropout events. MAGIC addresses this challenge by leveraging the underlying manifold structure of the data through diffusion geometry, effectively recovering missing transcripts while preserving biological signal.
MAGIC performs data imputation through the following steps:
$$K(x_i, x_j) = \exp\left(-\left(\frac{|x_i - x_j|_2}{\varepsilon_i}\right)^\alpha\right)$$
where $\varepsilon_i$ is the distance to the $k$-th nearest neighbor of cell $i$.
$$P = D^{-1}K$$
$$X_{\text{imputed}} = P^t X$$
The diffusion time $t$ controls the degree of smoothing. When t = "auto", optimal $t$ is selected by minimizing Procrustes disparity between successive iterations.
install.packages("MAGICR", repos = "https://zaoqu-liu.r-universe.dev")
# install.packages("remotes")
remotes::install_github("Zaoqu-Liu/MAGICR")
Required:
Optional:
library(MAGICR)
# Load example data
data(magic_testdata)
# Run MAGIC with default parameters
result <- magic(magic_testdata, t = 3)
# Access imputed data
imputed_data <- as.matrix(result)
library(Seurat)
library(MAGICR)
# Run MAGIC on normalized data
seurat_obj <- magic(seurat_obj, assay = "RNA", t = 3)
# Results stored in "MAGIC_RNA" assay
DefaultAssay(seurat_obj) <- "MAGIC_RNA"
# Automatically determine optimal t using Procrustes analysis
result <- magic(data, t = "auto", t_max = 20)
# Check selected t
print(result$params$t)
| Parameter | Default | Description |
|---|---|---|
knn |
5 | Number of nearest neighbors for bandwidth estimation |
knn_max |
15 | Maximum neighbors for graph construction |
decay |
1 | α parameter controlling kernel sharpness |
t |
3 | Diffusion time (integer or "auto") |
npca |
100 | PCA components for distance computation |
solver |
"exact" | "exact" or "approximate" (PCA-space) |
distance |
"euclidean" | Distance metric: "euclidean", "cosine", "correlation" |
The α-decaying kernel uses cell-specific bandwidth $\varepsilon_i$, defined as the distance to the $k$-th nearest neighbor. This provides:
When t = "auto", MAGIC iteratively applies the diffusion operator and monitors convergence using Procrustes disparity:
$$d_{\text{Procrustes}}(X_t, X_{t+1}) < \theta$$
The algorithm terminates when the disparity falls below threshold $\theta$ (default: 0.001).
If you use MAGICR in your research, please cite:
van Dijk, D., Sharma, R., Nainys, J., Yim, K., Kathail, P., Carr, A.J., Burdziak, C., Moon, K.R., Chaffer, C.L., Pattabiraman, D., Bierie, B., Mazutis, L., Wolf, G., Krishnaswamy, S., & Pe'er, D. (2018). Recovering Gene Interactions from Single-Cell Data Using Data Diffusion. Cell, 174(3), 716-729.e27. https://doi.org/10.1016/j.cell.2018.05.061
GPL-2
Zaoqu Liu (liuzaoqu@163.com)
This implementation is based on the original MAGIC algorithm developed by the Krishnaswamy Lab at Yale University. The MATLAB implementation served as the primary reference for this native R port.