这个代码的应用对象为:已经用Excel列出“SampleID”对应的“突变信息“之后的Excel数据!
library(readxl)
library(openxlsx)
library(tidyverse)
library(corrplot)
library(circlize)
library(grid)
library(ComplexHeatmap)
library(dplyr)
library(tidyr)
library(tibble)
#使得ID和Gene唯一
ID = unique(somatic$ID)
Gene = unique(somatic$Gene)
#创建空表格
results = matrix(NA, nrow = length(ID), ncol = length(Gene))
rownames(results) = ID
colnames(results) = Gene
#后循环
for (i in 1:nrow(somatic)){
row = somatic[i,]
results[row[,"ID"], row[,"Gene"]] = row[,"Mutation type"]}
results = t(results)#转置
#读入数据
mut <- read.xlsx("/Users/bidepan/Desktop/Dragon/2/瀑布图2.xlsx",sep = "\t",sheet = "Sheet1")
cli <- read.xlsx("/Users/bidepan/Desktop/Dragon/2/注释信息.xlsx",sep = "\t",sheet = "注释信息")
rownames(mut) = mut[,1]#以首列为列名
mat <- mut[,-1]
mat[is.na(mat)]<-""#不显示空的数据
mat[1:6,1:6]#查看前6行/列数据
col <- c("missence" = "#009ACD", "framshift" = "#EE6A50", "splice" = "#63886e",
"nonsense"="#CDC673","Multihit"="#0e2232")
alter_fun <- list(
background = function(x, y, w, h) {
grid.rect(x, y, w-unit(0.5, "mm"), h-unit(0.5, "mm"),
gp = gpar(fill = "WhiteSmoke", col = NA))
},
missence = function(x, y, w, h) {
grid.rect(x, y, w-unit(0.5, "mm"), h-unit(0.5, "mm"),
gp = gpar(fill = col["missence"], col = NA))
},
framshift = function(x, y, w, h) {
grid.rect(x, y, w-unit(0.5, "mm"), h-unit(0.5, "mm"),
gp = gpar(fill = col["framshift"], col = NA))
},
splice = function(x, y, w, h) {
grid.rect(x, y, w-unit(0.5, "mm"), h-unit(0.5, "mm"),
gp = gpar(fill = col["splice"], col = NA))
},
nonsense = function(x, y, w, h) {
grid.rect(x, y, w-unit(0.5, "mm"), h-unit(0.5, "mm"),
gp = gpar(fill = col["nonsense"], col = NA))
},
Multihit = function(x, y, w, h) {
grid.rect(x, y, w-unit(0.5, "mm"), h-unit(0.5, "mm"),
gp = gpar(fill = col["Multihit"], col = NA))
})
#开始处理注释信息
pdata <- cli
head(pdata)
#对应患者
pdata <- subset(pdata,pdata$sampleID %in% colnames(mat))
mat <- mat[, pdata$sampleID ]
#定义注释信息
col_Order = colorRamp2(c(0, 43), c("white", "blue"))#连续性变量的颜色渐变设置
cli<-HeatmapAnnotation(
Mutational_subtype=pdata$Mutational_subtype,
Gender=pdata$Gender,
T = pdata$T,
N = pdata$N,
Grade = pdata$Grade,
Carcinoma_in_Situ = pdata$Carcinoma_in_Situ,
Location = pdata$Location,
Adjuvant_chemotherapy=pdata$Adjuvant_chemotherapy,
col = list(
Mutational_subtype = c("1Hyper" = "#DFA245", "2TP53" = "#4375A4","3RAS"="#70B379","4FGFR3"="#CC4B33","5Negative"="#5E5E5E"),
Gender = c("F"="#9370DB","M"="#AB82FF"),
T = c("T1"="#FFF68F","T2_4"="#CDC673"),
N = c("N0"="#F0FFF0","N1"="#E0EEE0","N2"="#C1CDC1"),
Grade = c("Grade0"="#EDA5AC","Grade1"="#E0689D"),
Carcinoma_in_Situ = c("Car0"="#FFE4C4","Car1"="#CDB79E"),
Location = c("SY"="#BCEE68","SNG"="#A2CD5A","SYSNG"="#7CCD7C"),
Adjuvant_chemotherapy = c("GC"="#87CEFF","GP"="#6CA6CD")
),
show_annotation_name = TRUE,
annotation_name_gp = gpar(fontsize = 7)
)
#自定义样本顺序
s <- pdata[order(pdata$Mutational_subtype),]
sample_order <- as.character(s$sampleID)
oncoPrint(
mat,#作图对象
col = col,alter_fun = alter_fun,alter_fun_is_vectorized = FALSE,
row_names_side = "left", pct_side = "F",#基因名和百分比的位置/是否显示
remove_empty_columns = F,#处理空列
remove_empty_rows = F, #处理空行
row_order = 1:nrow(mat),
bottom_annotation = cli,#注释信息在底部,
column_order = sample_order#设置是否显示注释
)