-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHeatmap
381 lines (346 loc) · 13.8 KB
/
Heatmap
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
library(Seurat)
library(patchwork)
library(dplyr)
library(cowplot)
library(tidyr)
library(ggplot2)
library(RColorBrewer)
library(SeuratObject)
library(reshape2)
library(harmony)
library(ggpubr)
library(stringr)
library(ggrepel)
library(pheatmap)
##################################################################通路热图
##读取数据
setwd("/Users/mac/Desktop/dengshan/TNK_2/")
celltype <- readRDS("CD8zhushi.rds")
#celltype2 <- readRDS("NKzhushi.rds")
#celltype3 <- merge(celltype1,celltype2)
#celltype <- subset(celltype3,seurat_annotations %in% c("CD16+ NK","CD8+ TEM","Cytotoxic CD8+ T","γδT"))
##通路打分
setwd("/Users/mac/Desktop/dengshan/通路_热图/Geneset1/set2/")
filename <- dir("/Users/mac/Desktop/dengshan/通路_热图/Geneset1/set2/")
pathname <- gsub(".txt","",filename)
pathlist <- list()
for (i in 1:length(pathname)){
data <- read.table(filename[[i]],header = TRUE,sep = "")
pathlist[[i]] <- as.character(data[,1])
}
names(pathlist) <- pathname
##AddModuleScore
cell <- celltype
for (i in 1:length(pathlist)){
cell <- AddModuleScore(
object = cell,
features = pathlist[i],
ctrl = 100,
name = names(pathlist[i])
)
}
##得到计算结果
setwd("/Users/mac/Desktop/dengshan/通路_热图/test/CD8")
file <- dir("/Users/mac/Desktop/dengshan/通路_热图/Geneset1/set2/")
pathname <- gsub(".txt","",file)
Idents(cell) <- cell$seurat_annotations
for (b in names(table(cell@active.ident))){
object <- subset(cell,idents=b)
for (i in 1:length(pathname)){
f <- paste0(pathname[[i]],"1")
data <- as.data.frame(aggregate(object@meta.data[[f]], by=list(type=object$group),FUN=mean))
#data <- as.data.frame(aggregate(object@meta.data[[f]], by=list(type=object$seurat_annotations),FUN=mean))
data$celltype <- b
data$path <- pathname[[i]]
write.csv(data,paste0(pathname[[i]],"_",b,"_value.csv"))
}
}
filepath <- dir("/Users/mac/Desktop/dengshan/通路_热图/test/CD8")
filename <- gsub(".csv","",filepath)
deglist <- list()
for (i in 1:length(filename)){
deglist[[i]] <- read.csv(paste0(filename[i],".csv"),header = TRUE)
row.names(deglist[[i]]) <- deglist[[i]]$X
deglist[[i]] <- deglist[[i]][,-1]
}
names(deglist) <- filename
data <- dplyr::bind_rows(deglist)
##只是为了创建一个dataframe
data$group <- paste(data$type,data$celltype)
data2 <- subset(data, data$group %in% "D01 CD8+ TCM")
data2 <- as.data.frame(data2$x,data2$path)
colnames(data2) <- "D01 CD8+ TCM"
##作图data准备
name <- names(table(data$group))
for (i in name){
g <- subset(data, data$group %in% i)
data2[,paste0(i,"")] <- g$x
}
data2 <- select(data2,
1,10,19,28,37,
2,11,20,29,38,
3,12,21,30,39,
4,13,22,31,40,
5,14,23,32,41,
6,15,24,33,42,
7,16,25,34,43,
8,17,26,35,44,
9,18,27,36,45
)##按照目的改变列顺序
setwd("/Users/mac/Desktop/dengshan/通路_热图")
write.csv(data2,"test_CD8.csv")
##计算p值
setwd("/Users/mac/Desktop/dengshan/通路_热图/pvalue/NK")
groups <- list(c("D01","D04"), c("D01","D09"),c("D01","D11"),c("D01","D16"))
for (j in 1:length(groups)){
object_sub <- subset(cell,group %in% groups[[j]])
for (b in names(table(object_sub$seurat_annotations))){
object_tmp <- subset(object_sub,idents=b)
for (i in 1:length(pathname)){
f <- paste0(pathname[[i]],"1")
data <- as.data.frame((pairwise.wilcox.test(object_tmp@meta.data[[f]],object_tmp$group,paired = F,
p.adjust.method="BH")$p.value))
data$celltype <- b
data$path <- pathname[[i]]
data$type <- groups[[j]][2]
write.csv(data,paste0(pathname[[i]],"_",b,"_",j,"_pvalue.csv"))
}
}
}
filepath <- dir("/Users/mac/Desktop/dengshan/通路_热图/pvalue/NK")
filename <- gsub(".csv","",filepath)
deglist <- list()
for (i in 1:length(filename)){
deglist[[i]] <- read.csv(paste0(filename[i],".csv"),header = TRUE)
row.names(deglist[[i]]) <- deglist[[i]]$X
deglist[[i]] <- deglist[[i]][,-1]
}
names(deglist) <- filename
data <- dplyr::bind_rows(deglist)
colnames(data)[1] <- "x"
##只是为了创建一个dataframe
data$group <- paste(data$type,data$celltype)
data2 <- subset(data, data$group %in% "D04 Active CD16+ NK")
data2 <- as.data.frame(data2$x,data2$path)
colnames(data2) <- "D04 Active CD16+ NK"
##作图data准备
name <- names(table(data$group))
for (i in name){
g <- subset(data, data$group %in% i)
data2[,paste0(i,"")] <- g$x
}
data2 <- select(data2,
1,6,11,16,
2,7,12,17,
3,8,13,18,
4,9,14,19,
5,10,15,20)##按照目的改变列顺序
setwd("/Users/mac/Desktop/dengshan/通路_热图")
write.csv(data2,"pvalue_NK.csv")
##作图
setwd('/Users/mac/Desktop/BGI/BGI项目/dengshan/通路_热图/')
data <- as.matrix(t(read.table("value_row.txt",row.names = 1,header = T,sep = "\t",check.names = F)))
pvalue <- as.matrix(t(read.table("value_p.txt",row.names = 1,header = T,sep = "\t",check.names = F)))
p <- pheatmap(data,
# 去掉聚类树:
cluster_cols = F,
cluster_rows = F,
#breaks=seq(-1.5,1.5,length.out = 100),
# 加color bar:列注释信息;
#annotation_col = annotation_col,
# 行注释信息:
#annotation_row = annotation_row,
# color bar 颜色设定:
#annotation_colors = ann_colors,
display_numbers = ifelse(pvalue > 0.01 & pvalue < 0.05, "*", ifelse(pvalue > 0.001 & pvalue < 0.01, "**", (ifelse(pvalue < 0.001, "***", "")))),
number_color = "black",
#display_numbers = pvalue,
fontsize=7,
# 设置单元格颜色渐变;(5)表示分5段渐变;
#color = colorRampPalette(c("#2166AC","#4393C3","#92C5DE","#D1E5F0","#FDDBC7","#F4A582","#D6604D","#B2182B"))(100),
color = colorRampPalette(c("#78BBE2","#D1E5F0","white","#F4A582","#D6604D"))(100),
#color = colorRampPalette(c("#78BBE2","white","#DEA1A5"))(100),
# 行、列标签的字体大小
fontsize_col = 8,
fontsize_row = 8,
# 是否显示行、列名
show_colnames = T,
# 设置每个单元格的宽度和高度
cellwidth = 12,
cellheight = 12,
# 设置标题:
main = "Hypoxic",
# 设置标签是否显示
annotation_legend = F,
scale="column",
border= F)
p
setwd('/Users/mac/Desktop/')
ggsave("Hypoxic.pdf",p,width=10,height=3)
##################################################################基因热图(多个基因)
##读取数据
setwd('/Users/mac/Desktop/dengshan/TNK_2/')
celltype <- readRDS("CD8zhushi.rds")
#celltype = subset(celltype,seurat_annotations == c("cDC2"))
exprs <- data.frame(FetchData(object = celltype, vars = c('MYC')))
exprs$Barcod<-rownames(exprs)
ident<-data.frame()
#barcode与聚类信息提取
ident<-data.frame(Barcod=names(celltype$orig.ident),orig.ident=celltype$orig.ident,group=celltype$group,celltype=celltype$seurat_annotations)
#ident<-data.frame(Barcod=names(celltype$orig.ident),orig.ident=celltype$orig.ident,group=celltype$group)
#通过merge函数,将表达量与聚类号对应起来
c<-merge(exprs,ident,by='Barcod')
#对其进行排序
c$orig.ident<-factor(c$orig.ident,levels=c(sort(unique(celltype$orig.ident))))
c <- arrange(c,group)
setwd('/Users/mac/Desktop/dengshan/通路_热图/gene/1119')
summary <- c
summary <- summary[,-c(1,3)]
summary <- aggregate(summary[, c(1)], by = list(summary$group,summary$celltype), FUN = mean)
#summary <- aggregate(summary[, c(1:196)], by = list(summary$group), FUN = mean)
summary <- t(summary)
write.csv(summary,"CD8.csv")
##计算p值
setwd("/Users/mac/Desktop/dengshan/通路_热图/gene/1119/pvalue/CD8")
groups <- list(c("D01","D04"), c("D01","D09"),c("D01","D11"),c("D01","D16"))
genename <- c("MYC")
for (j in 1:length(groups)){
object_sub <- subset(celltype,group %in% groups[[j]])
for (b in names(table(object_sub@active.ident))){
object_tmp <- subset(object_sub,idents=b)
for (i in 1:length(genename)){
f <- paste0(genename[[i]])
exprs <- data.frame(FetchData(object = object_tmp, vars = c("MYC"),))
data <- as.data.frame((wilcox.test(exprs[[f]]~object_tmp$group,paired = F))$p.value)
data$celltype <- b
data$path <- genename[[i]]
data$type <- groups[[j]][2]
write.csv(data,paste0(genename[[i]],"_",b,"_",j,"_pvalue.csv"))
}
}
}
filepath <- dir("/Users/mac/Desktop/dengshan/通路_热图/gene/1119/pvalue/CD8")
filename <- gsub(".csv","",filepath)
deglist <- list()
for (i in 1:length(filename)){
deglist[[i]] <- read.csv(paste0(filename[i],".csv"),header = TRUE)
row.names(deglist[[i]]) <- deglist[[i]]$X
deglist[[i]] <- deglist[[i]][,-1]
}
names(deglist) <- filename
data <- dplyr::bind_rows(deglist)
colnames(data)[1] <- "x"
##只是为了创建一个dataframe
data$group <- paste(data$type,data$celltype)
data2 <- subset(data, data$group %in% "D04 CD8+ TCM")
data2 <- as.data.frame(data2$x,data2$path)
colnames(data2) <- "D04 CD8+ TCM"
##作图data准备
name <- names(table(data$group))
for (i in name){
g <- subset(data, data$group %in% i)
data2[,paste0(i,"")] <- g$x
}
data2 <- select(data2,
1,10,19,28,
2,11,20,29,
3,12,21,30,
4,13,22,31,
5,14,23,32,
6,15,24,33,
7,16,25,34,
8,17,26,35,
9,18,27,36)##按照目的改变列顺序
setwd("/Users/mac/Desktop/dengshan/通路_热图/gene/1119/")
write.csv(data2,"pvalue_CD8.csv")
##作图
setwd('/Users/mac/Desktop/dengshan/通路_热图/gene')
data <- as.matrix(t(read.csv("HALLMARK_MTORC1_SIGNALING_Mono.csv",row.names = 1,header = T,check.names = F)))
data <- data[,1:25]
#setwd('/Users/mac/Desktop/dengshan/通路_热图/')
#data <- as.matrix(t(read.table("value_row.txt",row.names = 1,header = T,sep = "\t",check.names = F)))
#pvalue <- as.matrix(t(read.table("value_p.txt",row.names = 1,header = T,sep = "\t",check.names = F)))
p <- pheatmap(data,
# 去掉聚类树:
cluster_cols = T,
cluster_rows = F,
breaks=seq(-1.5,1.5,length.out = 100),
#display_numbers = ifelse(pvalue > 0.01 & pvalue < 0.05, "*", ifelse(pvalue > 0.001 & pvalue < 0.01, "**", (ifelse(pvalue < 0.001, "***", "")))),
number_color = "black",
#display_numbers = pvalue,
fontsize=7,
# 设置单元格颜色渐变;(5)表示分5段渐变;
#color = colorRampPalette(c("#2166AC","#4393C3","#92C5DE","#D1E5F0","#FDDBC7","#F4A582","#D6604D","#B2182B"))(100),
#color = colorRampPalette(c("#78BBE2","#D1E5F0","white","#F4A582","#D6604D"))(100),
color = colorRampPalette(c("#78BBE2","white","#DEA1A5"))(100),
# 行、列标签的字体大小
fontsize_col = 8,
fontsize_row = 8,
# 是否显示行、列名
show_colnames = T,
# 设置每个单元格的宽度和高度
cellwidth = 12,
cellheight = 12,
# 设置标题:
main = "HALLMARK_MTORC1_SIGNALING_Mono",
# 设置标签是否显示
annotation_legend = F,
scale="column",
border=F)
p
setwd('/Users/mac/Desktop/')
ggsave("HALLMARK_MTORC1_SIGNALING_Mono.pdf",p,width=6,height=5)
##################################################################基因热图(一个基因多种细胞类型)
##读取数据
setwd('/Users/mac/Desktop/BGI/BGI项目/dengshan/NK/')
NK <- readRDS("NKzhushi.rds")
for (i in 1:length(celllist)) {
celltype = celllist[[i]]
exprs <- data.frame(FetchData(object = celltype, vars = c('PRKAA2')))
exprs$Barcod<-rownames(exprs)
ident<-data.frame()
#barcode与聚类信息提取
ident<-data.frame(Barcod=names(celltype$orig.ident),orig.ident=celltype$orig.ident,group=celltype$group)
#通过merge函数,将表达量与聚类号对应起来
c<-merge(exprs,ident,by='Barcod')
#对其进行排序
c$orig.ident<-factor(c$orig.ident,levels=c(sort(unique(celltype$orig.ident))))
summary <- aggregate(c[, 4], by = list(c$group), FUN = mean)
summary <- t(summary)
setwd('/Users/mac/Desktop/dengshan/通路_热图/gene/HIF3A/NK')
write.csv(summary,paste0(namelist[[i]],".csv"))
}
##作图
setwd('/Users/mac/Desktop/dengshan/通路_热图/gene')
data <- as.matrix(read.table("all.txt",row.names = 1,header = T,sep = "\t"))
library(pheatmap)
p <- pheatmap(data,
# 去掉聚类树:
cluster_cols = F,
cluster_rows = F,
# 加color bar:列注释信息;
annotation_col = annotation_col,
# 行注释信息:
annotation_row = annotation_row,
# color bar 颜色设定:
annotation_colors = ann_colors,
# 设置单元格颜色渐变;(5)表示分5段渐变;
color = colorRampPalette(c("#2166AC","#4393C3","#92C5DE","#D1E5F0","#FDDBC7","#F4A582","#D6604D","#B2182B"))(100),
# 行、列标签的字体大小
fontsize_col = 5,
fontsize_row = 8,
# 是否显示行、列名
show_colnames = F,
# 设置每个单元格的宽度和高度
cellwidth = 20,
cellheight = 12,
display_numbers = F ,number_color = "black",
# 设置标题:
main = "HIF3A",
# 设置标签是否显示
annotation_legend = F,
scale= "row",
border=F)
p
setwd('/Users/mac/Desktop/dengshan/通路_热图/result/基因/')
ggsave("HIF3A_Mono.pdf",p,width=5,height=5)