Skip to contents

This function wraps the Rust-based MAGIC diffusion kernel (diffuse_expr_r) and seamlessly integrates it into a Seurat workflow. It will extract a sparse expression matrix from your chosen assay/slot, build or reuse a k-NN graph, row-normalize it to form a transition matrix, run diffusion for t steps, and store the imputed values back into your Seurat object.

Usage

seurat_magic(
  obj,
  assay = "RNA",
  slot = "data",
  t = 3L,
  alpha = 1L,
  k = 30L,
  chunk = 2048L,
  out_slot = c("new_assay", "magic"),
  verbose = TRUE
)

Arguments

obj

A Seurat object.

assay

Character(1). Which assay to use (default "RNA").

slot

Character(1). Which data slot within the assay: "counts", "data", or "scale.data" (default "data").

t

Integer(1). Diffusion time (number of steps, default 3).

alpha

Numeric(1) interpolation weight in 0,1

k

Integer(1). Number of neighbors to use if no graph exists (default 30).

chunk

Integer(1). Number of gene-columns per block in the Rust kernel (trades memory vs speed; default 2048).

out_slot

Character(1). Where to store the result: "magic" (adds to @misc$magic in the same assay) or "new_assay" (creates a new assay named "MAGIC"). Default is "magic".

verbose

Logical(1). Whether to print progress messages (default TRUE).

Value

The input Seurat object, with a new imputed matrix stored according to out_slot.

Examples

if (FALSE) { # \dontrun{
library(Seurat)
so <- CreateSeuratObject(counts = my_counts)
so <- NormalizeData(so)
so <- seurat_magic(so, assay = "RNA", slot = "data", t = 3, k = 30)
} # }