library(Seurat) library(openxlsx) library (Hmisc) library(dplyr) library(patchwork) library(ggplot2) library(ggalluvial) library(svglite) library(Seurat)
所需文件的格式
load(file = “G:/silicosis/sicosis/silicosis-1122-merge/silicosis_cluster_merge.rds”)
#通过transferanchor得到的 load(file = “G:/silicosis/geo/GSE164621_RNA-seq_wildtype_mouse_lung/wildtype_mouse_lung_query.rds”)
cluster_gene_stat=read.xlsx(“G:/silicosis/sicosis/silicosis_ST/overlapped_map/Rigional and cell markers.xlsx”, sheet = “Addition”)
head(cluster_gene_stat) cluster_gene_stat[,1] colnames(cluster_gene_stat) dim(cluster_gene_stat)
cluster_marker = list() for(i in 1:ncol(cluster_gene_stat)){ #按照p0.05值、flodchange 1标准 获得11个cluster的marker基因 列表 #i=1 #因为某列可能为空值 可能会报错 cluster_marker[[paste(“col”,i, colnames(cluster_gene_stat)[i], sep = “_”)]] <- try(capitalize(unique(cluster_gene_stat[,i])), silent = TRUE) #加unique去重复 } head(cluster_marker) unlist(lapply(cluster_marker, length)) cluster_gene_stat$ig-producing.b.cell
‘’’ names(cluster_marker) #以clara细胞为例 cluster_gene_stat$“macroph-1” cellname=“macroph-1” mymarkers=cluster_marker[[1]] mymarkers mymarkers<-capitalize(mymarkers) %>% na.omit() mymarkers length(mymarkers) markers=mymarkers[11:21] markers getwd() cellname ‘’’ #批量做气泡图 Dotplot 如果运行出错 就一个个改循环
getwd() file=“G:/silicosis/geo/GSE164621_RNA-seq_wildtype_mouse_lung/dotplot_for_selected_genes” file=“G:/silicosis/geo/GSE164621_RNA-seq_wildtype_mouse_lung/dotplot_for_selected_genes/wild_mouse_dotplot” dir.create(file) setwd(file) length(cluster_marker)
for (j in 1:length(cluster_marker)) {#一定要记得是向量 而不是一个数 for j in(1:11) 一定记得加括号!!!!!!!!!!!! #j=1 times=(length(cluster_marker[[j]])+10)/10 times
if (floor(times)>=2 ){ try(for (i in seq(1,100,10)) { #使用循环出图 画图 以最多的基因数目为准!!!!!!!!!!!!!!!try忽略报错!!!1 #i=1 if ((i+9)>(10*floor(times))) {next} # print(cluster_marker[[j]][i:(i+9)]) jpeg(paste0(“col_”,j,"",paste0(unlist(strsplit(cluster_marker[[j]][i:(i+2)], " "))[seq(1,10,1)],seq=’’,collapse = “”) , “.jpeg”),height = 9, width = 18, units = ‘in’, res=300) p=DotPlot(wildtype_mouse_lung.query, #可以改名字换成想要画的seurat对象 features = cluster_marker[[j]][i:(i+9)]) print§ dev.off() print(paste0(“clo_”,j))#;print(cluster_marker[[j]][i:(i+9)]) } ,silent = TRUE)
} else{ #条件一定记得加括号! !!!!!!!!!!!!!!!11!!!!!!!!!!! print(paste0(“col_”,j))#; print(cluster_marker[[j]][1:(1+9)]) jpeg(paste0(“col_”,j,"",paste0(unlist(strsplit(cluster_marker[[j]][1:(1+2)], " "))[seq(1,10,1)],seq=’’,collapse = “”) , “.jpeg”),height = 9, width = 18, units = ‘in’, res=300) p=DotPlot(wildtype_mouse_lung.query,features = cluster_marker[[j]][1:(1+9)]) print§ dev.off() } }
while (dev.off()) {dev.off()} #循环关闭画板 head(cluster_marker) names(cluster_marker)
for (j in 1:length(cluster_marker)) {#一定要记得是向量 而不是一个数 for j in(1:11) 一定记得加括号!!!!!!!!!!!! #j=1 times=(length(cluster_marker[[j]])+10)/10 times
if (floor(times)>=2 ){ try(for (i in seq(1,100,10)) { #使用循环出图 画图 以最多的基因数目为准!!!!!!!!!!!!!!!try忽略报错!!!1 #i=1 if ((i+9)>(10*floor(times))) {next} # print(cluster_marker[[j]][i:(i+9)]) jpeg(paste0(names(cluster_marker)[j],"", paste0(unlist(strsplit(cluster_marker[[j]][i:(i+9)], " "))[seq(1,10,1)],seq=’’,collapse = “”) , “.jpeg”), height = 9, width = 18, units = ‘in’, res=300) p=DotPlot(wildtype_mouse_lung.query, #可以改名字换成想要画的seurat对象 features = cluster_marker[[j]][i:(i+9)]) print§ dev.off() print(paste0(“clo_”,j))#;print(cluster_marker[[j]][i:(i+9)]) } ,silent = TRUE)
} else{ #条件一定记得加括号! !!!!!!!!!!!!!!!11!!!!!!!!!!! print(paste0(“col_”,j))#; print(cluster_marker[[j]][1:(1+9)]) jpeg(paste0(names(cluster_marker)[j],"", paste0(unlist(strsplit(cluster_marker[[j]][1:(1+9)], " "))[seq(1,10,1)],seq=’’,collapse = “”) , “.jpeg”), height = 9, width = 18, units = ‘in’, res=300) p=DotPlot(wildtype_mouse_lung.query,features = cluster_marker[[j]][1:(1+9)]) print§ dev.off() } }
getwd() dir.create(“G:/silicosis/geo/GSE164621_RNA-seq_wildtype_mouse_lung/dotplot_for_selected_genes/wild_mouse_dotplot-groups”) setwd(“G:/silicosis/geo/GSE164621_RNA-seq_wildtype_mouse_lung/dotplot_for_selected_genes/wild_mouse_dotplot-groups”) #改变分组方式 for (j in 1:length(cluster_marker)) {#一定要记得是向量 而不是一个数 for j in(1:11) 一定记得加括号!!!!!!!!!!!! #j=1 times=(length(cluster_marker[[j]])+10)/10 times
if (floor(times)>=2 ){ try(for (i in seq(1,100,10)) { #使用循环出图 画图 以最多的基因数目为准!!!!!!!!!!!!!!!try忽略报错!!!1 #i=1 if ((i+9)>(10*floor(times))) {next} # print(cluster_marker[[j]][i:(i+9)]) jpeg(paste0(names(cluster_marker)[j],"", paste0(unlist(strsplit(cluster_marker[[j]][i:(i+9)], " "))[seq(1,10,1)],seq=’’,collapse = “”) , “.jpeg”), height = 9, width = 18, units = ‘in’, res=300) p=DotPlot(wildtype_mouse_lung.query, #可以改名字换成想要画的seurat对象 features = cluster_marker[[j]][i:(i+9)], group.by = “predicted.id”) #!!!而不是split.by print§ dev.off() print(paste0(“clo_”,j))#;print(cluster_marker[[j]][i:(i+9)]) } ,silent = TRUE)
} else{ #条件一定记得加括号! !!!!!!!!!!!!!!!11!!!!!!!!!!! print(paste0(“col_”,j))#; print(cluster_marker[[j]][1:(1+9)]) jpeg(paste0(names(cluster_marker)[j],"", paste0(unlist(strsplit(cluster_marker[[j]][1:(1+9)], " "))[seq(1,10,1)],seq=’’,collapse = “”) , “.jpeg”), height = 9, width = 18, units = ‘in’, res=300) p=DotPlot(wildtype_mouse_lung.query, features = cluster_marker[[j]][1:(1+9)], group.by = “predicted.id”) print§ dev.off() } } while (dev.off()) {dev.off()} #循环关闭画板
getwd() dir.create(“G:/silicosis/geo/GSE164621_RNA-seq_wildtype_mouse_lung/dotplot_for_selected_genes/wild_mouse_dotplot-groups_elegant/for_wild_”, recursive = TRUE) setwd(“G:/silicosis/geo/GSE164621_RNA-seq_wildtype_mouse_lung/dotplot_for_selected_genes/wild_mouse_dotplot-groups_elegant/for_wild_”)
for (j in 1:length(cluster_marker)) {#一定要记得是向量 而不是一个数 for j in(1:11) 一定记得加括号!!!!!!!!!!!! #j=1 times=(length(cluster_marker[[j]])+10)/10 times
if (floor(times)>=2 ){ try(for (i in seq(1,100,10)) { #使用循环出图 画图 以最多的基因数目为准!!!!!!!!!!!!!!!try忽略报错!!!1 #i=1 if ((i+9)>(10*floor(times))) {next} # print(cluster_marker[[j]][i:(i+9)]) jpeg(paste0(names(cluster_marker)[j],"", paste0(unlist(strsplit(cluster_marker[[j]][i:(i+9)], " "))[seq(1,10,1)],seq=’’,collapse = “”) , “.jpeg”), height = 9, width = 18, units = ‘in’, res=300) p=DotPlot(wildtype_mouse_lung.query, #可以改名字换成想要画的seurat对象 features = cluster_marker[[j]][i:(i+9)], group.by = “predicted.id”) #!!!而不是split.by print§ dev.off() print(paste0(“clo_”,j))#;print(cluster_marker[[j]][i:(i+9)]) } ,silent = TRUE)
} else{ #条件一定记得加括号! !!!!!!!!!!!!!!!11!!!!!!!!!!!
try(if(1==1){print(paste0("col_",j))#; print(cluster_marker[[j]][1:(1+9)])
jpeg(paste0(names(cluster_marker)[j],"_",
paste0(unlist(strsplit(cluster_marker[[j]][1:(1+9)], " "))[seq(1,10,1)],seq='_',collapse = "") , ".jpeg"),
height = 9, width = 18, units = 'in', res=300)
p=DotPlot(wildtype_mouse_lung.query,
features = cluster_marker[[j]][1:(1+9)],
group.by = "predicted.id")
print(p)
dev.off()},silent = TRUE)
} }
结果图 10个为一组 输出所有基因的dotplot图气泡图
#方式二 把所有列都读入列表 cluster_marker = list() for(i in 1:ncol(cluster_gene_stat)){ #循环读取excel里的不同列 #i=1 cluster_marker[[paste(“col”,i, colnames(cluster_gene_stat)[i], sep = “_”)]] <- capitalize(na.omit(unique(cluster_gene_stat[,i])))#加unique去重复 } nchar(cluster_marker[[1]]) head(cluster_marker) unlist(lapply(cluster_marker, length)) cluster_marker=na.omit(cluster_marker) head(cluster_marker) na.omit(unique(cluster_gene_stat[,i])) cluster_marker[[1]][4]
|