Seurat Object
基本構造
colnames(so) # cells
rownames(so) # genes
ASSAY
DefaultAssay(so) <- "RNA"
Ident
# IDENT 1
Idents(so)
levels(so)
so <- RenameIdents(so, "0"="beta_cell")
# IDENT 2
ids <- c(....)
names(ids) <- levels(so)
so <- RenameIdents(so, ids)
Metadata
so[[]] # 簡略記法
so[["col"]]
so[[]][N]
colnames(so@meta.data)["hoge"] <- "foo" # 列名変更hoge -> foo: so[[]]ではだめ
so@meta.data$col <- Idents(so) # 列内容変更
Rename rowname
so@assays$RNA@counts@Dimnames[[1]] <- c()
so@assays$RNA@data@Dimnames[[1]] <- c()
so@assays$RNA@scale.data@Dimnames[[1]] <- c()
so@assays$RNA@meta.features <- as.data.frame(c())
13 slot
so@assays
so@meta.data
so@active.assay
so@graphs
so@reductions
so@commands
関数出力
CreateSeuratObject
@meta.data$orig.ident
@meta.data$nCount_RNA
@meta.data$nFeature_RNA
NormalizeData
FindVariableFeatures
@assays$RNA@var.features
@assays$RNA@meta.features
ScaleData
RunPCA
FindNeighbors
@graphs$RNA_nn
@graphs$RNA_snn
FindClusters
@meta.data$RNA_snn_res.N
@meta.data$seurat_clusters
RunUMAP
Subset/Downsampling/Integrate/Merge
SUBSET
sub <- subset(so, idents=c("0", "1")) # Idents
sub <- subset(so, subset=seurat_clusters!="0") # meta.data
sub <- subset(so, subset=Ins1>5) # Gene Exp
"%nin%" <- Negate("%in%")
sub_1 <- subset(sub, subset = seurat_clusters %in% c("4") & Ins2 < 4.5)
sub_2 <- subset(sub, subset = seurat_clusters %nin% c("4"))
sub <- merge(sub_1,sub_2)
DOWNSAMPLING
sub <- so[, sample(colnames(so), size = 3000, replace=F)]
INTEGRATE
so_1 <- NormalizeData(so_1)
so_2 <- NormalizeData(so_2)
so_1 <- FindVariableFeatures(so_1)
so_2 <- FindVariableFeatures(so_2)
so <- FindIntegrationAnchors(c(so_1, so_2), dims=1:30)
so <- IntegrateData(so, dims=1:30)
so <- ScaleData(so)
MERGE
so <- merge(so1, so2)
Visualize
options(repr.plot.width=10, repr.plot.height=10)
DimPlot(so,
reduction = "umap",
group.by = "meta1",
label = TRUE,
pt.size = .8,
label.size = 8
)
DimPlot() + NoLegend()
DimPlot() + NoAxes()
DimPlot(sub,
cells.highlight = list(
high = WhichCells(so, expression = Gcg > 6 ),
low = WhichCells(so, expression = Gcg < 6 & Gcg > 3)
),
cols.highlight = c("blue", "red")
)
VlnPlot(so, features=c("gene1", "gene2"))
VlnPlot(sub, features = c("gene1","gene2"), stack = T, flip = T)
FeaturePlot(so, features=c("gene1", "gene2"))
FeaturePlot(so, features="nCount_RNA")
DotPlot(sub, features = c("Ins2", "Gcg", "Sst"), dot.scale = 10)
DoHeatmap(so, features = c("Ins2", "Gcg", "Sst")) + scale_fill_gradientn()
DoHeatmap(so, features = , size = 0) +
theme (axis.text.y = element_text(size = 18, family = "Helvetica")
) + NoLegend()
DEGs
FindMarkers(so, ident.1="0", ident.2=c("1", "2"))
TOP10 HEATMAP
mks <- FindAllMarkers(so, verbose=FALSE)
mks_pos <- mks[mks$avg_log2FC > 0,]
mks_neg <- mks[mks$avg_log2FC < 0,]
top10 <- do.call(rbind, lapply(split(mks_pos, mks_pos$cluster), function(x) {
x[order(x$avg_log2FC, decreasing = TRUE), ][1:10,]}))
top10 <- na.omit(top10)
DoHeatmap(so, top10)
Convert object
Seurat to AnnData
# step1 on R
library(Seurat)
library(SeuratDisk)
so <- readRDS("so.rds")
SaveH5Seurat(so, filename = "so.h5Seurat")
Convert("so.h5Seurat", dest = "h5ad")
# step2 on python
import scanpy as sc
adata = sc.read_h5ad("so.h5ad")
# 名前変更など
adata.obs.seurat_clusters = adata.obs.seurat_clusters.astype("category")
adata.obs.drop(columns="orig.ident", inplace=True)
adata.obs.rename(columns={"seurat_clusters":"clusters"}, inplace = True)
sc.pl.umap(adata, color=['clusters'])
Seurat to Loom
library(Seurat)
library(SeuratDisk)
so <- readRDS("so.rds")
SaveLoom(so, "so.loom")
Alevin Fry to Seurat
library(SingleCellExperiment)
library(fishpond)
library(Seurat)
sce <- loadFry("ALEVIN_FRY_OUTPUT",
outputFormat = list("counts" = c("S", "A"),"unspliced" = c("U")))
so <- CreateSeuratObject(counts(sce))
saveRDS(so, "XXX.rds")
Others
I/O
so <- CreateSeuratObject(Read10X(CELLRANGER_DIR), min.cells=3)
so <- readRDS("obj.rds")
saveRDS(so, file="obj.rds")
ggsave("hoge.png", obj)
write.table(obj, "filename", sep=" ")
SUBCLUSTER
so <- RunPCA(so)
so <- FindNeighbors(so, graph.name="grp")
so <- FindClusters(so, graph.name="grp")
so <- RunUMAP(so)
so <- FindSubCluster(so, "1", "grp", subcluster.name="sub", resolution=.5)
# input: seurat_cluster 1
# output: so@meta.data$sub
Rename after doubletfinder
rename <- function(X){
DF = colnames(X[[]])[grepl("DF.classification", colnames(X[[]]))]
n = which(colnames(X[[]]) == DF)
colnames(X@meta.data)[n] = "doublet"
return(X)
}
so <- rename(so)
#@ Make Doublet Table
mktable <- function(X){
x = X[[]]$doublet
y = X[[]]$seurat_clusters
nDouble = tapply(x == "Doublet", y, sum)
nTotal = tapply(x, y, length)
pDouble = sprintf("%.1f", nDouble/nTotal*100)
df = data.frame(doublet_count=nDouble,total_count=nTotal, doublet_percent=pDouble)
return(df)
}
so <- doubletFinder_v3(XXX)
df <- mktable(so)
barplot(as.double(df$doublet_percent), names.arg=rownames(df), las=2)
so_sub <- subset(so, subset = doublet == "singlet")
# IDENT/BAR PLOT
cnt <- table(so@active.ident, so@meta.data$origin)
cnt <- as.data.frame(cnt)
head(cnt)
cnt$cluster <- factor(cnt$cluster, levels = cnt$cluster)
ggplot(cnt, aes(x = Var2, Freq, fill=Var1)) +
geom_bar(
position = "fill",
stat = "identity")