细胞打分 addmodule addmodule对所有细胞进行循环评分
细胞打分 addmodule addmodule对所有细胞进行循环评分
library(patchwork)
library(ggplot2)
library(ggalluvial)
library(svglite)
library(Seurat)
library(openxlsx)
library(Hmisc)
#https://www.jianshu.com/p/cef5663888ff
getwd()
path="G:/silicosis/sicosis/silicosis_ST/overlapped_map/addmodule-cluster013-mixed"
dir.create(path)
setwd(path)
从这里开始!!!!!!!!!!!!!!!!!!!!!!!1
getwd()
load("G:/silicosis/sicosis/silicosis_ST/yll/0214/harmony_cluster/d_all/silicosis_ST_harmony_SCT_r0.6.rds")
marker = read.xlsx("G:/silicosis/sicosis/silicosis_ST/overlapped_map/Rigional and cell markers.xlsx",
sheet = "SingleCell_markers")
**marker=read.xlsx("G:/silicosis/sicosis/yll/macrophage/no cluster2/0.3/pure_cluster01345_dotplot/cluster01345.xlsx")
head(marker)
library(dplyr)**
#判断
cellnames=colnames(marker) #取出细胞的名字,即列名 ##number=length(marker[,cellname])
for (each in cellnames) {
cellname=each
mymarker=marker[,cellname] %>% na.exclude() %>% unique() %>%
capitalize() %>% list()
number=length(mymarker[[1]])
unlist(mymarker)
#对给定的基因集合进行打分 并画图
if(1==1){d.all=AddModuleScore(d.all,
features = mymarker,
name = "mymarker")
#结果保存在这里
colnames(d.all@meta.data)
###
p1=SpatialFeaturePlot(d.all, features = "mymarker1", slot = "scale.data",images = "image")+ ggtitle(paste(unlist(mymarker), collapse = "|"))
p2=SpatialFeaturePlot(d.all, features = "mymarker1", slot = "scale.data",images = "image.1")
p3=SpatialFeaturePlot(d.all, features = "mymarker1", slot = "scale.data",images = "image.2")+ ggtitle(paste(cellname))
p4=SpatialFeaturePlot(d.all, features = "mymarker1", slot = "scale.data",images = "image.3")
jpeg(paste0(cellname,"_","total_",length(unlist(mymarker)),"_",paste0(min(number),"-",max(number)),
paste(unlist(mymarker)[1:15],collapse = "_"),"_.jpeg"), #只取前15个
height = 12, width = 12, units = 'in', res=600)
p=ggpubr::ggarrange(p1,p2,p3,p4,ncol = 2,nrow =2)
print(p)
dev.off()}
}
#下面的代码是为了测试
if(1==1){#
names(d.all)
SpatialFeaturePlot(d.all, features = "mymarker1", slot = "scale.data")+
ggtitle(paste(unlist(mymarker), collapse = "|"))
#https://ggplot2-book.org/polishing.html?q=title#themes
SpatialFeaturePlot(d.all, features = "mymarker1", slot = "scale.data")+
theme(plot.title =element_text(paste(unlist(mymarker), collapse = "|")) )
####
SpatialFeaturePlot(d.all, features = "mymarker1", slot = "scale.data")+
labs(title = paste(unlist(mymarker), collapse = "|"),
colour="Cylinders")
}
#再画一个所有细胞的dotplot气泡图 批量气泡图
##再画一个所有细胞的dotplot气泡图 批量气泡图
head(marker)
load(file = 'G:/silicosis/sicosis/silicosis-1122-merge/silicosis_cluster_merge.rds')
table(All.merge$new.cluster.idents)
table(All.merge$cell.type,All.merge$orig.ident)
Idents(All.merge)=All.merge$new.cluster.idents
#把datafram形式的marker基因制作成基因列表形式
cluster_marker=list()
for (eachcolum in colnames(marker)) {
cluster_marker[[paste(eachcolum)]]= marker[,eachcolum] %>%unique() %>% capitalize()
}
unlist(cluster_marker)
unlist(lapply(cluster_marker, length))
head(cluster_marker)
#开始画图
getwd()
#批量做气泡图 Dotplot all.merge
for (j in names(cluster_marker)) {#一定要记得是向量 而不是一个数 for j in(1:11) 一定记得加括号!!!1:length(cluster_marker!!!!!!!!!
#j=5
times=(length(cluster_marker[[j]])+10)/10
times
if (floor(times)>=2 ){
for (i in seq(1,330,10)) { #使用循环出图 画图
#i=1
if ((i+9)>(10*floor(times))) {next}
print(cluster_marker[[j]][i:(i+9)])
jpeg(paste0("cluster_",j,"_",paste0(unlist(strsplit(cluster_marker[[j]][i:(i+2)], " "))[seq(1,3,1)],seq='_',collapse = "") , ".jpeg"),height = 9, width = 18, units = 'in', res=300)
p=DotPlot(All.merge,
features = cluster_marker[[j]][i:(i+9)])
print(p)
dev.off()
print(paste0("cluster_",j));print(cluster_marker[[j]][i:(i+9)])
}
print(j)
} else{ #条件一定记得加括号! !!!!!!!!!!!!!!!11!!!!!!!!!!!
print(j); print(cluster_marker[[j]][1:(1+9)])
jpeg(paste0("cluster_",j,"_",paste0(unlist(strsplit(cluster_marker[[j]][1:(1+2)], " "))[seq(1,3,1)],seq='_',collapse = "") , ".jpeg"),height = 9, width = 18, units = 'in', res=300)
p=DotPlot(All.merge,features = cluster_marker[[j]][1:(1+9)])
print(p)
dev.off()
}
}
getwd()
##批量做气泡图 Dotplot 画在同一张图上
#制作基因向量
all.gene=vector()
for (eachcellname in names(cluster_marker)) {
all.gene=union(all.gene,cluster_marker[[eachcellname]]) %>% unique()
}
head(all.gene)
#画图
jpeg(paste0("allmerge" ,".jpeg"),height = 9, width = 18, units = 'in', res=300)
p=DotPlot(All.merge,features = all.gene)
print(p)
dev.off()