【pySCENIC】SCENIC python版本結(jié)果可視化

繼續(xù)跟著大咖們的步伐學習腰懂。(http://www.reibang.com/p/c47332b880aa

分析結(jié)果让腹,用上面pbmc的輸出結(jié)果霎箍。

=====第一步:還是獲得loom文件=====

可以使用SCopeLoomR把上個帖子的前兩步合成一步闯冷。get_count_from_seurat.R文件代碼如下:

library(optparse)
op_list <- list(
make_option(c("-i", "--inrds"), type = "character", default = NULL, action = "store", help = "The input of Seurat RDS",metavar="rds"),
make_option(c("-d", "--ident"), type = "character", default = NULL, action = "store", help = "The sample Ident of Seurat object",metavar="idents"),
make_option(c("-s", "--size"), type = "integer", default = NULL, action = "store", help = "The sample size of Seurat object",metavar="size"),
make_option(c("-l", "--label"), type = "character", default = "out", action = "store", help = "The label of output file",metavar="label"),
make_option(c("-a", "--assay"), type = "character", default = "RNA", action = "store", help = "The assay of input file",metavar="assay")
)
parser <- OptionParser(option_list = op_list)
opt = parse_args(parser)

assay <- opt$assay

library(Seurat)
obj <- readRDS(optinrds) if (!is.null(optident)) {
Idents(obj) <- optident size=optsize
if (!is.null(size)) {
obj <- subset(x = obj, downsample = optsize) } saveRDS(obj,"subset.rds") } if (is.null(optlabel)) {
label1 <- 'out'
}else{
label1 <- opt$label
}

library(SCopeLoomR)
outloom <- paste0(label1,".loom")
build_loom(file.name = outloom,dgem = obj@assays[[assay]]@counts)
write.table(obj@meta.data,'metadata_subset.xls',sep='\t',quote=F)

運行如下:Rscript get_count_from_seurat.R -i pbmc.rds -s 20 -l out -a RNA

=== 第二步:運行pySCENIC===

代碼和前面一樣
./pyscenic_from_loom.sh -i out.loom -n 20

====第三步:計算RSS===

代碼和前面一樣
Rscript calcRSS_by_scenic.R -l aucell.loom -m metadata_subset.xls -c cell_type

=======第四步:可視化=====

加載一些必要的包

library(Seurat)
library(SCopeLoomR)
library(AUCell)
library(SCENIC)
library(dplyr)
library(KernSmooth)
library(RColorBrewer)
library(plotly)
library(BiocParallel)
library(pheatmap)

library(cowplot)
library(ggpubr)
library(ggsci)
library(ggplot2)
library(tidygraph)
library(ggraph)
library(stringr)

做一些顏色上的基本設置

colpalettes<-unique(c(pal_npg("nrc")(10),pal_aaas("default")(10),pal_nejm("default")(8),pal_lancet("lanonc")(9),
pal_jama("default")(7),pal_jco("default")(10),pal_ucscgb("default")(26),pal_d3("category10")(10),
pal_locuszoom("default")(7),pal_igv("default")(51),
pal_uchicago("default")(9),pal_startrek("uniform")(7),
pal_tron("legacy")(7),pal_futurama("planetexpress")(12),pal_rickandmorty("schwifty")(12),
pal_simpsons("springfield")(16),pal_gsea("default")(12)))

len <- 100

incolor<-c(brewer.pal(8, "Dark2"),brewer.pal(12, "Paired"),brewer.pal(8, "Set2"),brewer.pal(9, "Set1"),colpalettes,rainbow(len))

輸入文件的設置

inloom='aucell.loom'
incolor=incolor
inrss='cell_type_rss.rds'
inrds='subset.rds'
infun='median'
ct.col='cell_type'
inregulons=NULL
ingrn='grn.tsv'
ntop1=5
ntop2=50

load data

loom <- open_loom(inloom)

regulons_incidMat <- get_regulons(loom, column.attr.name="Regulons")
regulons <- regulonsToGeneLists(regulons_incidMat)
regulonAUC <- get_regulons_AUC(loom,column.attr.name='RegulonsAUC')
regulonAucThresholds <- get_regulon_thresholds(loom)

embeddings <- get_embeddings(loom)
close_loom(loom)

rss <- readRDS(inrss)
sce <- readRDS(inrds)

calculate RSS fc

df = do.call(rbind,
lapply(1:ncol(rss), function(i){
dat= data.frame(
regulon = rownames(rss),
cluster = colnames(rss)[i],
sd.1 = rss[,i],
sd.2 = apply(rss[,-i], 1, get(infun))
)
}))

dffc = dfsd.1 - df$sd.2

select top regulon

ntopg <- df %>% group_by(cluster) %>% top_n(ntop1, fc)

ntopgene <- unique(ntopg$regulon)
write.table(ntopgene,'sd_regulon_RSS.list',sep='\t',quote=F,row.names=F,col.names=F)

plot rss by cluster

using plotRSS

rssPlot <- plotRSS(rss)
regulonsToPlot <- rssPlotrowOrder rp_df <- rssPlotdf

write.table(regulonsToPlot,'rss_regulon.list',sep='\t',quote=F,row.names=F,col.names=F)
write.table(rp_df,'rssPlot_data.xls',sep='\t',quote=F)

image.png

nlen <- length(regulonsToPlot)
hei <- ceiling(nlen)*0.4
blu<-colorRampPalette(brewer.pal(9,"Blues"))(100)
lgroup <- levels(rssPlotdfcellType)

nlen2 <- length(lgroup)
wei <- nlen2*2
pdf(paste0('regulons_RSS_',ct.col,'_in_dotplot.pdf'))
print(rssPlot$plot)
dev.off()


regulons_RSS_cell_type_in_dotplot.png

sd top gene

anrow = data.frame( group = ntopgcluster) lcolor <- incolor[1:length(unique(ntopgcluster))]
names(lcolor) <- unique(anrow$group)
annotation_colors <- list(group=lcolor)

pn1 = rss[ntopgregulon,] pn2 = rss[unique(ntopgregulon),]
rownames(pn1) <- make.unique(rownames(pn1))
rownames(anrow) <- rownames(pn1)
scale='row'
hei <- ceiling(length(ntopg$regulon)*0.4)

pdf(paste0('regulon_RSS_in_sd_topgene_',ct.col,'.pdf'))
print(
pheatmap(pn1,annotation_row = anrow,scale=scale,annotation_colors=annotation_colors,show_rownames = T,main='sd top regulons')
)
print(
pheatmap(pn2,scale=scale,show_rownames = T, main='sd top unique regulons')
)
dev.off()

regulon_RSS_in_sd_topgene_cell_type_頁面_1.png
regulon_RSS_in_sd_topgene_cell_type_頁面_2.png

plotRSS gene

pn2 = rss[unique(rp_dfTopic),] scale='row' hei <- ceiling(length(unique(rp_dfTopic))*0.4)
pdf(paste0('regulon_RSS_in_plotRSS_',ct.col,'.pdf'))
print(
pheatmap(pn2,scale=scale,show_rownames = T, main='plotRSS unique regulons')
)
dev.off()

regulon_RSS_in_plotRSS_cell_type.png

all regulons

hei <- ceiling(length(rownames(rss))*0.2)
pdf(paste0('all_regulons_RSS_in_',ct.col,'.pdf'))
print(
pheatmap(rss,scale=scale,show_rownames = T,main='all regulons RSS')
)
dev.off()

all_regulons_RSS_in_cell_type.png

plot rss by all cells

if (is.null(inregulons)){
inregulons <- regulonsToPlot
}else{
inregulons <- intersect(inregulons,rownames(rss))
regulonsToPlot <- inregulons

}

pn3=as.matrix(regulonAUC@assays@data$AUC)
regulon <- rownames(pn3)

regulon <- inregulons

pn3 <- pn3[regulon,]

pn3 <- pn3[,sample(1:dim(pn3)[2],500)]

sce$group1=sce@meta.data[,ct.col]

meta <- sce@meta.data
meta <- meta[order(meta$group1),]

meta <- meta[colnames(pn3),]

ancol = data.frame(meta[,c('group1')])
colnames(ancol) <- c('group1')
rownames(ancol) <- rownames(meta)
lcolor <- incolor[1:length(unique(ntopgcluster))] names(lcolor) <- unique(ntopgcluster)
annotation_colors <- list(group1 =lcolor)

df1 <- ancol
df1cell <- rownames(df1) df1 <- df1[order(df1group1),]
pn3 <- pn3[,rownames(df1)]
torange=c(-2,2)
pn3 <- scales::rescale(pn3,to=torange)
pn3 <- pn3[,rownames(ancol)]

scale='none'
hei <- ceiling(length(unique(regulon))*0.2)
pdf(paste0('all_regulon_activity_in_allcells.pdf'))
print(
pheatmap(pn3,annotation_col = ancol,scale=scale,annotation_colors=annotation_colors,show_rownames = T,show_colnames = F,cluster_cols=F)
)

pheatmap(pn3,scale=scale,show_rownames = T, show_colnames = F,cluster_cols=F)

dev.off()

all_regulon_activity_in_allcells.png

plot in seurat

regulonsToPlot = inregulons
sce$sub_celltype <- sce@meta.data[,ct.col]
sub_regulonAUC <- regulonAUC[,match(colnames(sce),colnames(regulonAUC))]

cellClusters <- data.frame(row.names = colnames(sce),
seurat_clusters = as.character(sceseurat_clusters)) cellTypes <- data.frame(row.names = colnames(sce), celltype = scesub_celltype)

sce@meta.data = cbind(sce@meta.data ,t(sub_regulonAUC@assays@data@listDataAUC[regulonsToPlot,])) Idents(sce) <- scesub_celltype

nlen <- length(regulonsToPlot)
hei <- ceiling(nlen)0.4
blu<-colorRampPalette(brewer.pal(9,"Blues"))(100)
nlen2 <- length(unique(sce$sub_celltype))
wei <- nlen2
2
pdf('regulons_activity_in_dotplot.pdf')
print(DotPlot(sce, features = unique(regulonsToPlot)) + coord_flip()+
theme(axis.text.x = element_text(angle = 90, hjust = 1, vjust = .5))+
scale_color_gradientn(colours = blu)
)
dev.off()

regulons_activity_in_dotplot.png

hei=ceiling(nlen/4)*4
pdf('regulons_activity_in_umap.pdf')
print(DotPlot(sce, features = regulonsToPlot))
print(RidgePlot(sce, features = regulonsToPlot , ncol = 4))
print(VlnPlot(sce, features = regulonsToPlot,pt.size = 0 ))
print(FeaturePlot(sce, features = regulonsToPlot))
dev.off()

會顯示類似于下面的圖榨咐,但是我的測試數(shù)據(jù)list太多了璧亚,就不放了。

640.jpg

grn <- read.table(ingrn,sep='\t',header=T,stringsAsFactors=F)
inregulons1=gsub('[(+)]','',inregulons)

c1 <- which(grn$TF %in% inregulons1)
grn <- grn[c1,]

edge1 <- data.frame()

node1 <- data.frame()

pdf(paste0(ntop2,'_regulon_netplot.pdf'))
for (tf in unique(grnTF)) { tmp <- subset(grn,TF==tf) if (dim(tmp)[1] > ntop2) { tmp <- tmp[order(tmpimportance,decreasing=T),]
tmp <- tmp[1:ntop2,]
}
node2 <- data.frame(tmptarget) node2node.size=1.5
node2$node.colour <- 'black'
colnames(node2) <- c('node','node.size','node.colour')
df1 <- data.frame(node=tf,node.size=2,node.colour='#FFDA00')
node2 <- rbind(df1,node2)

edge2 <- tmp
colnames(edge2) <- c('from','to','edge.width')
edge2edge.colour <- "#1B9E77" torange=c(0.1,1) edge2edge.width <- scales::rescale(edge2$edge.width,to=torange)

graph_data <- tidygraph::tbl_graph(nodes = node2, edges = edge2, directed = T)
p1 <- ggraph(graph = graph_data, layout = "stress", circular = TRUE) + geom_edge_arc(aes(edge_colour = edge.colour, edge_width = edge.width)) +
scale_edge_width_continuous(range = c(1,0.2)) +geom_node_point(aes(colour = node.colour, size = node.size))+ theme_void() +
geom_node_label(aes(label = node,colour = node.colour),size = 3.5, repel = TRUE)
p1 <- p1 + scale_color_manual(values=c('#FFDA00','black'))+scale_edge_color_manual(values=c("#1B9E77"))
print(p1)
}
dev.off()

每個轉(zhuǎn)錄因子生成一個網(wǎng)絡圖

image.png

plot activity heatmap

meta <- sce@meta.data
celltype <- ct.col
cellsPerGroup <- split(rownames(meta),meta[,celltype])
sub_regulonAUC <- regulonAUC[onlyNonDuplicatedExtended(rownames(regulonAUC)),]

Calculate average expression:

regulonActivity_byGroup <- sapply(cellsPerGroup,
function(cells)
rowMeans(getAUC(sub_regulonAUC)[,cells]))
scale='row'
rss <- regulonActivity_byGroup
hei <- ceiling(length(regulonsToPlot)*0.4)
pn1 <- rss[regulonsToPlot,]
pdf(paste0('regulon_activity_in_',ct.col,'.pdf'))
print(
pheatmap(pn1,scale=scale,show_rownames = T, main='regulons activity')
)
dev.off()

regulon_activity_in_cell_type.png

pdf(paste0('all_regulons_activity_in_',ct.col,'.pdf'))
print(
pheatmap(rss,scale=scale,show_rownames = T,main='all regulons activity')
)
dev.off()

all_regulons_activity_in_cell_type.png

calculate fc

df = do.call(rbind,
lapply(1:ncol(rss), function(i){
dat= data.frame(
regulon = rownames(rss),
cluster = colnames(rss)[i],
sd.1 = rss[,i],
sd.2 = apply(rss[,-i], 1, get(infun))
)
}))

dffc = dfsd.1 - df$sd.2

select top regulon

ntopg <- df %>% group_by(cluster) %>% top_n(ntop1, fc)

ntopgene <- unique(ntopg$regulon)
write.table(ntopgene,'sd_regulon_activity.list',sep='\t',quote=F,row.names=F,col.names=F)

anrow = data.frame( group = ntopgcluster) lcolor <- incolor[1:length(unique(ntopgcluster))]
names(lcolor) <- unique(anrowgroup) annotation_colors <- list(group=lcolor) pn1 = rss[ntopgregulon,]
pn2 = rss[unique(ntopgregulon),] rownames(pn1) <- make.unique(rownames(pn1)) rownames(anrow) <- rownames(pn1) scale='row' hei <- ceiling(length(ntopgregulon)*0.4)
pdf(paste0('regulon_activity_in_sd_topgene_',ct.col,'.pdf'))
print(
pheatmap(pn1,annotation_row = anrow,scale=scale,annotation_colors=annotation_colors,show_rownames = T,main='sd top regulons')
)
print(
pheatmap(pn2,scale=scale,show_rownames = T, main='sd top unique regulons ')
)
dev.off()

regulon_activity_in_sd_topgene_cell_type_頁面_1.png
regulon_activity_in_sd_topgene_cell_type_頁面_2.png

當然可以把整個過程給封裝起來了一個函數(shù)柱搜,放在calcRSS_by_scenic.R函數(shù)里面迟郎。

?著作權歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市聪蘸,隨后出現(xiàn)的幾起案子宪肖,更是在濱河造成了極大的恐慌,老刑警劉巖宇姚,帶你破解...
    沈念sama閱讀 211,376評論 6 491
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件匈庭,死亡現(xiàn)場離奇詭異夫凸,居然都是意外死亡浑劳,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 90,126評論 2 385
  • 文/潘曉璐 我一進店門夭拌,熙熙樓的掌柜王于貴愁眉苦臉地迎上來魔熏,“玉大人,你說我怎么就攤上這事鸽扁∷庹溃” “怎么了?”我有些...
    開封第一講書人閱讀 156,966評論 0 347
  • 文/不壞的土叔 我叫張陵桶现,是天一觀的道長躲雅。 經(jīng)常有香客問我,道長骡和,這世上最難降的妖魔是什么相赁? 我笑而不...
    開封第一講書人閱讀 56,432評論 1 283
  • 正文 為了忘掉前任,我火速辦了婚禮慰于,結(jié)果婚禮上钮科,老公的妹妹穿的比我還像新娘。我一直安慰自己婆赠,他們只是感情好绵脯,可當我...
    茶點故事閱讀 65,519評論 6 385
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著,像睡著了一般蛆挫。 火紅的嫁衣襯著肌膚如雪赃承。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 49,792評論 1 290
  • 那天悴侵,我揣著相機與錄音楣导,去河邊找鬼。 笑死畜挨,一個胖子當著我的面吹牛筒繁,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播巴元,決...
    沈念sama閱讀 38,933評論 3 406
  • 文/蒼蘭香墨 我猛地睜開眼毡咏,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了逮刨?” 一聲冷哼從身側(cè)響起呕缭,我...
    開封第一講書人閱讀 37,701評論 0 266
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎修己,沒想到半個月后恢总,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 44,143評論 1 303
  • 正文 獨居荒郊野嶺守林人離奇死亡睬愤,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 36,488評論 2 327
  • 正文 我和宋清朗相戀三年片仿,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片尤辱。...
    茶點故事閱讀 38,626評論 1 340
  • 序言:一個原本活蹦亂跳的男人離奇死亡砂豌,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出光督,到底是詐尸還是另有隱情阳距,我是刑警寧澤,帶...
    沈念sama閱讀 34,292評論 4 329
  • 正文 年R本政府宣布结借,位于F島的核電站筐摘,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏船老。R本人自食惡果不足惜咖熟,卻給世界環(huán)境...
    茶點故事閱讀 39,896評論 3 313
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望努隙。 院中可真熱鬧球恤,春花似錦、人聲如沸荸镊。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,742評論 0 21
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至张惹,卻和暖如春舀锨,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背宛逗。 一陣腳步聲響...
    開封第一講書人閱讀 31,977評論 1 265
  • 我被黑心中介騙來泰國打工坎匿, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人雷激。 一個月前我還...
    沈念sama閱讀 46,324評論 2 360
  • 正文 我出身青樓替蔬,卻偏偏與公主長得像,于是被迫代替她去往敵國和親屎暇。 傳聞我的和親對象是個殘疾皇子承桥,可洞房花燭夜當晚...
    茶點故事閱讀 43,494評論 2 348

推薦閱讀更多精彩內(nèi)容