CmhaDSO SingleR

使用例

###############################################################################
# Case1: Reference is NOT scRNA-seq data (for example: HumanPrimaryCell Atlas)
#   In the folling example, Test data is Seurat object and
#   reference data is SingleCellExperiment object. Cell 
#   annotation is stocked at LABEL column of reference data.
###############################################################################
#@ Load Library
# Conda env: SingleR-v2.0.0
library(SingleR)
library(SingleCellExperiment)
library(Seurat)

#@ Input data
so   <- readRDS("SO.rds")
sceT <- as.SingleCellExperiment(so)
sceR <- readRDS("SCE.rds")

#@ SingleR
pred <- SingleR(
  sceT, 
  sceR, 
  sceR$LABEL, 
  assay.type.test = 1
)

#@ Transfer the result to seurat object 
so$pred <- pred$labels
################################################################################
# Case2: Reference is scRNA-seq data
#   In the folling example, Test data is Seurat object and
#   reference data is SingleCellExperiment object. Cell 
#   annotation is stocked at LABEL column of reference data.
################################################################################
#@ Load Library
# Conda env: SingleR-v2.0.0
library(SingleR)
library(SingleCellExperiment)
library(Seurat)
library(scran)

#@ input data
so   <- readRDS("SO.rds")
sceT <- as.SingleCellExperiment(so)
sceR <- readRDS("SCE.rds")

#@ SingleR
pred <- SingleR(
  sceT, 
  sceR, 
  sceR$LABEL,
  de.method = "wilcox",
  de.n = 50
)

#@ Transfer the result to seurat object 
so$pred <- pred$labels
################################################################################
# Explore the Result 
################################################################################
#@ Table format 1
head(sort(table(pred$labels), decreasing = TRUE))

#@ Table format 2 (no NA data)
to.remove <- is.na(pred$pruned.labels)
table(Label = pred$labels, Removed = to.remove)

#@ Table format 3 (remove data by the specified threshold)
to.remove <- pruneScores(pred, min.diff.med = 0.2)
table(Label = pred$labels, Removed = to.remove)

#@ Visualization 1
plotScoreHeatmap(pred)

#@ Visualization 2
plotDeltaDistribution(pred)
################################################################################
# Make SCE object from Count matrix for SingleR
#   meta data include cell annotation LABEL.  
################################################################################
#@ Load Library
# Conda env: SingleR-v2.0.0
library(SingleCellExperiment)
library(scuttle)  # logNormCounts()

#@ input data
count <- read.table("CountData")
meta  <- read.table("MetaData")

#@ Make SingleCellExperiment object from count Matrix
sce <- SingleCellExperiment(
  assays  = list( counts = as.matrix (count)),
  colData = meta
)

  # Remove cells that do not belong to any Clusters (if needed)
  # For example: sce <- sce[,sce$LABEL != ""]

#@ logNorm Conversion
# IMPORTANT!: For SingleR, raw counts MUST be converted to logNorm data
sce <- logNormCounts(sce)

#@ Save
saveRDS(sce, "SCE.rds")

参考文献