課程筆記施工中。。。。。
1.SCENIC簡介
目前單細胞轉錄組領域用的比較多的細胞聚類方法大多是直接從基因表達矩陣推斷,但是對于多樣本合并分析,很多情況下會出現難以解決的批次效應,例如:有些癌癥多樣本的聚類結果大多每個樣本單獨分成一群;對于發育樣本,發育前期和后期細胞類型可能存在較大差異,某些樣本特異的細胞群,難以判斷是批次效應產生的還是真正的生物學效應。
細胞異質性的決定于細胞轉錄狀態的差異,轉錄狀態又是由基因調控網絡(GRN, gene regulatory network)決定并維持穩定的。因此分析單細胞的基因調控網絡有助于深入挖掘細胞異質性背后的生物學意義。
2017年發表在Nature Methods雜志上的Single-cell regulatory network inference and clustering SCENIC算法,利用scRNA-seq數據,同時進行基因調控網絡重建和細胞狀態鑒定,應用于腫瘤和小鼠大腦單細胞圖譜數據,提出并證明了順式調控網絡分析能夠用于指導轉錄因子和細胞狀態的鑒定。SCENIC通過使用生物學驅動的features自動清除腫瘤樣本特異性等批次效應。
1.1 SCENIC工作流程
SCENIC工作流程主要有四步:
- 用GENIE3(隨機森林) 或GRNBoost (Gradient Boosting) 推斷轉錄因子與候選靶基因之間的共表達模塊。每個模塊包含一個轉錄因子及其靶基因,純粹基于共表達。
- 使用RcisTarget分析每個共表達模塊中的基因,以鑒定enriched motifs;僅保留TF motif富集的模塊和targets,每個TF及其潛在的直接targets gene被稱作一個調節子(regulon)
- 使用AUCell評估每個細胞中每個regulon的活性,AUCell分數用于生成Regulon活性矩陣,通過為每個regulon設置AUC閾值,可以將該矩陣進行二值化(0|1,on|off),這將確定Regulon在哪些細胞中處于“打開”狀態。
- 細胞聚類:基于regulons的活性鑒定穩定的細胞狀態并對結果進行探索。
2.SCENIC分析前的準備
2.1 SCENIC安裝
詳細教程:https://rawcdn.githack.com/aertslab/SCENIC/701cc7cc4ac762b91479b3bd2eaf5ad5661dd8c2/inst/doc/SCENIC_Setup.html
安裝一些可能用到的依賴包
if (!requireNamespace("BiocManager", quietly = TRUE)) install.packages("BiocManager")
BiocManager::version()
# If your bioconductor version is previous to 3.9, see the section bellow
## Required
BiocManager::install(c("AUCell", "RcisTarget"))
BiocManager::install(c("GENIE3")) # Optional. Can be replaced by GRNBoost
## Optional (but highly recommended):
# To score the network on cells (i.e. run AUCell):
BiocManager::install(c("zoo", "mixtools", "rbokeh"))
# For various visualizations and perform t-SNEs:
BiocManager::install(c("DT", "NMF", "pheatmap", "R2HTML", "Rtsne"))
# To support paralell execution (not available in Windows):
BiocManager::install(c("doMC", "doRNG"))
# To export/visualize in http://scope.aertslab.org
if (!requireNamespace("devtools", quietly = TRUE)) install.packages("devtools")
devtools::install_github("aertslab/SCopeLoomR", build_vignettes = TRUE)
安裝SCENIC包
if (!requireNamespace("devtools", quietly = TRUE)) install.packages("devtools")
devtools::install_github("aertslab/SCENIC")
packageVersion("SCENIC")
2.2 相關數據庫的下載
鏈接:https://resources.aertslab.org/cistarget/
感覺這個數據庫國內連接有些問題,很難下載下來,還可以嘗試 wget等下載方式
##1, For human:
dbFiles <- c("https://resources.aertslab.org/cistarget/databases/homo_sapiens/hg19/refseq_r45/mc9nr/gene_based/hg19-500bp-upstream-7species.mc9nr.feather",
"https://resources.aertslab.org/cistarget/databases/homo_sapiens/hg19/refseq_r45/mc9nr/gene_based/hg19-tss-centered-10kb-7species.mc9nr.feather")
# mc9nr: Motif collection version 9: 24k motifs
##2, For mouse:
dbFiles <- c("https://resources.aertslab.org/cistarget/databases/mus_musculus/mm9/refseq_r45/mc9nr/gene_based/mm9-500bp-upstream-7species.mc9nr.feather",
"https://resources.aertslab.org/cistarget/databases/mus_musculus/mm9/refseq_r45/mc9nr/gene_based/mm9-tss-centered-10kb-7species.mc9nr.feather")
# mc9nr: Motif collection version 9: 24k motifs
##3, For fly:
dbFiles <- c("https://resources.aertslab.org/cistarget/databases/drosophila_melanogaster/dm6/flybase_r6.02/mc8nr/gene_based/dm6-5kb-upstream-full-tx-11species.mc8nr.feather")
# mc8nr: Motif collection version 8: 20k motifs
##4, download
dir.create("cisTarget_databases"); #創建一個文件夾保存數據庫
setwd("cisTarget_databases")
#如果3個參考數據庫都想下載,每次設置變量dbFiles后,都要運行以下代碼
for(featherURL in dbFiles)
{
download.file(featherURL, destfile=basename(featherURL)) # saved in current dir
}
2.3 相關數據的準備
library(Seurat)
library(SeuratData)
library(ggplot2)
library(patchwork)
library(dplyr)
#加載Seurat標準流程跑完的數據
load(file = '../section-01-cluster/basic.sce.pbmc.Rdata')
sce=pbmc
table( Idents(sce ))
table(sce@meta.data$seurat_clusters)
table(sce@meta.data$orig.ident)
#挑選感興趣的亞群
levels(Idents(sce))
sce = sce[, Idents(sce) %in% c( "FCGR3A+ Mono", "CD14+ Mono" )]
levels(Idents(sce))
#尋找兩個亞群的差異基因
markers_df <- FindMarkers(object = sce, ident.1 = 'FCGR3A+ Mono', ident.2 = 'CD14+ Mono',#logfc.threshold = 0,min.pct = 0.25)
head(markers_df)
#篩選差異基因
cg_markers_df=markers_df[abs(markers_df$avg_log2FC) >1,]
dim(cg_markers_df)
cg_markers_df=cg_markers_df[order(cg_markers_df$avg_log2FC),]
#由于后續計算量很大,計算資源不足可以抽樣
sce=subset(sce, downsample = 10)
sce
3.運行SCENIC
3.1 準備counts矩陣和細胞的meta信息
table(Idents(sce))
phe=sce@meta.data
mat=sce@assays$RNA@counts
mat[1:4,1:4]
exprMat =as.matrix(mat)
dim(exprMat)
exprMat[1:4,1:4]
head(phe)
cellInfo <- phe[,c('seurat_clusters','nCount_RNA' ,'nFeature_RNA' )]
colnames(cellInfo)=c('CellType', 'nGene' ,'nUMI')
head(cellInfo)
table(cellInfo$CellType)
cellInfo$CellType=Idents(sce)
table(cellInfo$CellType)
3.2 run-SCENIC
db='~/cisTarget_databases'
list.files(db)
# 保證cisTarget_databases 文件夾下面有下載好 的文件
#創建scenicOptions對象 存儲 SCENIC 設置
scenicOptions <- initializeScenic(org="hgnc",
dbDir=db , nCores=4)
#nCores 線程數;dbDIR 存放數據庫的目錄。
saveRDS(scenicOptions, file="int/scenicOptions.Rds")
saveRDS(cellInfo, file="int/cellInfo.Rds")
相關性回歸分析
#過濾基因
genesKept <- geneFiltering(exprMat, scenicOptions)
length(genesKept)
exprMat_filtered <- exprMat[genesKept, ]
exprMat_filtered[1:4,1:4]
dim(exprMat_filtered)
#計算輸入表達式矩陣的 spearman 相關性并以 SCENIC 格式保存結果
runCorrelation(exprMat_filtered, scenicOptions)
exprMat_filtered_log <- log2(exprMat_filtered+1)
# 最耗費時間的就是這個步驟,推斷轉錄因子與候選靶基因之間的共表達模塊
runGenie3(exprMat_filtered_log, scenicOptions)
#runGenie3(exprMat_filtered_log, scenicOptions, nParts = 20)需要注意nParts參數,它的作用是把表達矩陣分成n份分開計算,目的是防止數據量大時內存不夠。以上代碼運行后,int目錄下有不少中間結果產生,簡要解釋一下:1.2_corrMat.Rds:基因之間的相關性矩陣;1.3_GENIE3_weightMatrix_part_1.Rds等:GENIE3的中間結果;1.4_GENIE3_linkList.Rds:GENIE3最終結果,是把“1.3_”開頭的文件合并在一起。
推斷共表達模塊
上一步計算了轉錄因子與每一個基因的相關性,接下來需要過濾低相關性的組合形成共表達基因集(模塊)。作者嘗試了多種策略(標準)過濾低相關性TF-Target,研究發現沒有一種最佳策略,因此他們的建議是6種過濾標準都用。這6種方法分別是:
- w001:以每個TF為核心保留weight>0.001的基因形成共表達模塊;
- w005:以每個TF為核心保留weight>0.005的基因形成共表達模塊;
- top50:以每個TF為核心保留weight值top50的基因形成共表達模塊;
- top5perTarget:每個基因保留weight值top5的TF得到精簡的TF-Target關聯表,然后把基因分配給TF構建共表達模塊;
- top10perTarget:每個基因保留weight值top10的TF得到精簡的TF-Target關聯表,然后把基因分配給TF構建共表達模塊;
- top50perTarget:每個基因保留weight值top50的TF得到精簡的TF-Target關聯表,然后把基因分配給TF構建共表達模塊;
### Build and score the GRN
exprMat_log <- log2(exprMat+1)
scenicOptions@settings$dbs <- scenicOptions@settings$dbs["10kb"] # Toy run settings
scenicOptions <- runSCENIC_1_coexNetwork2modules(scenicOptions)
Motif驗證共表達模塊
經過上述分析每個轉錄因子都找到了強相關的靶基因,很多基因調控網絡分析到此就結束了。SCENIC的創新之處是對此結果提出了質疑,并通過以下步驟修剪共表達模塊形成有生物學意義的調控單元(regulons):
對每個共表達模塊進行motif富集分析,保留顯著富集的motif;此項分析依賴gene-motif評分(排行)數據庫,其行為基因、列為motif、值為排名,就是我們下載的cisTarget數據庫。
使用數據庫對motif進行TF注釋,注釋結果分高、低可信度 。數據庫直接注釋和同源基因推斷的TF是高可信結果,使用motif序列相似性注釋的TF是低可信結果。
用保留的motif對共表達模塊內的基因進行打分(同樣依據cisTarget數據庫),識別顯著高分的基因(理解為motif離這些基因的TSS很近);
刪除共表達模塊內與motif評分不高的基因,剩下的基因集作者稱為調控單元(regulon)。
# 斷轉錄調控網絡(regulon)
scenicOptions <- runSCENIC_2_createRegulons(scenicOptions,coexMethod=c("top5perTarget")) # Toy run settings
結果保存在output文件夾:Step2_regulonTargetsInfo.tsv,表頭含義如下:
- TF:轉錄因子名稱
- gene:TF靶基因名稱
- nMotif:靶基因在數據庫的motif數量
- bestMotif:最顯著富集的motif名稱
- NES:標準富集分數,分值越高越顯著
- highConfAnnot:是不是高可信注釋
- Genie3Weight:TF與靶基因的相關性權重
請特別注意這個文件,后續分析找到了有價值的regulon,需要回到這個文件找對應的轉錄因子和靶基因 。SCENIC中regulon的名稱有兩種,一種是TF名稱+extended+靶基因數目,另一種是TF名稱+靶基因數目。第一種是轉錄因子與所有靶基因組成的基因調控網絡,第二種是轉錄因子與高可信靶基因(即highConfAnnot=TRUE的基因)組成的基因調控網絡。
Regulon活性評分與可視化
每個Regulon就是一個轉錄因子及其直接調控靶基因的基因集,SCENIC接下來的工作就是對每個regulon在各個細胞中的活性評分。評分的基礎是基因的表達值,分數越高代表基因集的激活程度越高。我們推斷regulons雖然只用了1000個隨機抽取的細胞,但是regulon評分的時候可以把所有細胞導進來計算。
library(doParallel)
scenicOptions <- initializeScenic(org="hgnc", dbDir=db , nCores=4)
## 如果報錯,嘗試多線程重新設置成為 1 個線程
scenicOptions <- runSCENIC_3_scoreCells(scenicOptions, exprMat_log )
runSCENIC_3_scoreCells()是一個多種功能打包的函數,它包含的子功能有:
- 計算regulon在每個細胞中AUC值,最后得到一個以regulon為行細胞為列的矩陣。結果目錄:int/3.4_regulonAUC.Rds
- 計算每個regulon的AUC閾值,細胞中regulonAUC值>閾值,代表此regulon在細胞中處于激活狀態,否則代表沉默狀態。結果目錄:int/3.5_AUCellThresholds.Rds
- 使用regulonAUC矩陣對細胞進行降維聚類
- 用heatmap圖展示regulonAUC矩陣,用t-SNE圖分別展示每個regulon的活性分布情況。結果目錄:output/Step3_開頭的系列文件
Regulon活性二進制轉換與可視化
對于細胞類型清晰的數據集而言,構建regulons并對其活性打分足夠后續分析。但是,在很多情況下將評分轉換為二進制的“開/關”,則既方便解釋,又能最大化體現細胞類型的差異。將特定的regulon轉換為“0/1”有利于探索和解釋關鍵轉錄因子。將所有的regulons轉換為“0/1”后創建二進制的活性矩陣,則可以用于細胞聚類,對消除技術偏倚特別有用。AUCell會自動計算可能的閾值進行“0/1”轉換,作者建議在轉換之前手工檢查并調整這些閾值。
查看調整閾值的代碼(可選 )
#使用shiny互動調整閾值
aucellApp <- plotTsne_AUCellApp(scenicOptions, exprMat_all)
savedSelections <- shiny::runApp(aucellApp)
#保存調整后的閾值
newThresholds <- savedSelections$thresholds
scenicOptions@fileNames$int["aucell_thresholds",1] <- "int/newThresholds.Rds"
saveRDS(newThresholds, file=getIntName(scenicOptions, "aucell_thresholds"))
saveRDS(scenicOptions, file="int/scenicOptions.Rds")
二進制轉換及衍生分析
scenicOptions <- runSCENIC_4_aucell_binarize(scenicOptions)
export2loom(scenicOptions, exprMat)
saveRDS(scenicOptions, file="int/scenicOptions.Rds")
runSCENIC_4_aucell_binarize()將regulonAUC矩陣轉換為二進制矩陣后,會重新降維聚類,輸出的結果與runSCENIC_3_scoreCells()類似。
3.Seurat可視化SCENIC結果
把SCENIC結果中最重要的regulonAUC矩陣導入Seurat,這樣得到的可視化結果更容易與我們之前的分析聯系起來。
##導入原始regulonAUC矩陣
AUCmatrix <- readRDS("int/3.4_regulonAUC.Rds")
AUCmatrix <- AUCmatrix@assays@data@listData$AUC
AUCmatrix <- data.frame(t(AUCmatrix), check.names=F)
RegulonName_AUC <- colnames(AUCmatrix)
RegulonName_AUC <- gsub(' \\(','_',RegulonName_AUC)
RegulonName_AUC <- gsub('\\)','',RegulonName_AUC)
colnames(AUCmatrix) <- RegulonName_AUC
sceauc <- AddMetaData(sce, AUCmatrix)
sceauc@assays$integrated <- NULL
saveRDS(sceauc,'sceauc.rds')
##導入二進制regulonAUC矩陣
BINmatrix <- readRDS("int/4.1_binaryRegulonActivity.Rds")
BINmatrix <- data.frame(t(BINmatrix), check.names=F)
RegulonName_BIN <- colnames(BINmatrix)
RegulonName_BIN <- gsub(' \\(','_',RegulonName_BIN)
RegulonName_BIN <- gsub('\\)','',RegulonName_BIN)
colnames(BINmatrix) <- RegulonName_BIN
scebin <- AddMetaData(sce, BINmatrix)
scebin@assays$integrated <- NULL
saveRDS(scebin, 'scebin.rds')
##利用Seurat可視化AUC
dir.create('scenic_seurat')
#FeaturePlot
p1 = FeaturePlot(sceauc, features='CEBPB_extended_2290g', label=T, reduction = 'tsne')
p2 = FeaturePlot(scebin, features='CEBPB_extended_2290g', label=T, reduction = 'tsne')
p3 = DimPlot(sce, reduction = 'tsne', group.by = "celltype_Monaco", label=T)
plotc = p1|p2|p3
ggsave('scenic_seurat/CEBPB_extended_2290g.png', plotc, width=14 ,height=4)
決定單核細胞命運的regulon:轉錄因子CEBPB主導的調控網絡
#RidgePlot&VlnPlot
p1 = RidgePlot(sceauc, features = "CEBPB_extended_2290g", group.by="celltype_Monaco") +
theme(legend.position='none')
p2 = VlnPlot(sceauc, features = "CEBPB_extended_2290g", pt.size = 0, group.by="celltype_Monaco") +
theme(legend.position='none')
plotc = p1 + p2
ggsave('scenic_seurat/Ridge-Vln_CEBPB_extended_2290g.png', plotc, width=10, height=8)
pheatmap可視化SCENIC結果
library(pheatmap)
cellInfo <- readRDS("int/cellInfo.Rds")
celltype = subset(cellInfo,select = 'celltype')
AUCmatrix <- t(AUCmatrix)
BINmatrix <- t(BINmatrix)
#挑選部分感興趣的regulons
my.regulons <- c('ETS1_2372g','ETV7_981g','IRF7_239g','XBP1_854g','ATF4_37g',
'KLF13_78g','ATF6_129g','CREB3L2_619g','TAGLN2_13g',
'STAT1_extended_1808g','CEBPB_extended_2290g','IRF5_extended_422g',
'SPI1_1606g','HMGA1_14g','SPIB_1866g','IRF8_348g','BCL11A_136g',
'EBF1_40g','MAF_45g','BATF_131g','FOXP3_55g','TBX21_388g',
'EOMES_extended_101g','TCF7_extended_31g','LEF1_extended_49g')
myAUCmatrix <- AUCmatrix[rownames(AUCmatrix)%in%my.regulons,]
myBINmatrix <- BINmatrix[rownames(BINmatrix)%in%my.regulons,]
#使用regulon原始AUC值繪制熱圖
pheatmap(myAUCmatrix, show_colnames=F, annotation_col=celltype,
filename = 'scenic_seurat/myAUCmatrix_heatmap.png',
width = 6, height = 5)
#使用regulon二進制AUC值繪制熱圖
pheatmap(myBINmatrix, show_colnames=F, annotation_col=celltype,
filename = 'scenic_seurat/myBINmatrix_heatmap.png',
color = colorRampPalette(colors = c("white","black"))(100),
width = 6, height = 5)
參考來源
#section 3已更新#「生信技能樹」單細胞公開課2021_嗶哩嗶哩_bilibili
致謝
I thank Dr.Jianming Zeng(University of Macau), and all the members of his bioinformatics team, biotrainee, for generously sharing their experience and codes.
THE END