add 去除多余绘图
This commit is contained in:
parent
5213ab4a4d
commit
41c723fe3e
105
heatmap_script.R
105
heatmap_script.R
@ -1,11 +1,8 @@
|
|||||||
# 加载所需的包
|
# 加载所需的包
|
||||||
suppressPackageStartupMessages(library(grid))
|
|
||||||
suppressPackageStartupMessages(library(ComplexHeatmap))
|
|
||||||
suppressPackageStartupMessages(library(circlize))
|
suppressPackageStartupMessages(library(circlize))
|
||||||
suppressPackageStartupMessages(library(RColorBrewer))
|
suppressPackageStartupMessages(library(RColorBrewer))
|
||||||
|
suppressPackageStartupMessages(library(ComplexHeatmap))
|
||||||
suppressPackageStartupMessages(library(dendextend))
|
suppressPackageStartupMessages(library(dendextend))
|
||||||
suppressPackageStartupMessages(library(dendsort))
|
|
||||||
suppressPackageStartupMessages(library(gridBase))
|
|
||||||
|
|
||||||
# 从命令行获取参数
|
# 从命令行获取参数
|
||||||
args <- commandArgs(trailingOnly = TRUE)
|
args <- commandArgs(trailingOnly = TRUE)
|
||||||
@ -21,76 +18,47 @@ output_pdf <- args[2]
|
|||||||
|
|
||||||
# 检查输入文件是否存在
|
# 检查输入文件是否存在
|
||||||
if (!file.exists(input_csv)) {
|
if (!file.exists(input_csv)) {
|
||||||
stop("输入文件不存在,请检查路径是否正确。", call. = FALSE)
|
stop("输入文件不存在。请检查文件路径。", call. = FALSE)
|
||||||
}
|
}
|
||||||
|
|
||||||
# 加载绘图数据
|
# 函数加载和预处理数据
|
||||||
data <- read.table(file = input_csv, header = TRUE, row.names = 1, sep = ',')
|
load_and_preprocess_data <- function(file_path) {
|
||||||
|
data <- read.table(file = file_path, header = TRUE, row.names = 1, sep = ',')
|
||||||
|
data_matrix <- as.matrix(data)
|
||||||
|
normalized_data <- t(scale(t(data_matrix)))
|
||||||
|
normalized_data[is.na(normalized_data)] <- 0
|
||||||
|
return(normalized_data)
|
||||||
|
}
|
||||||
|
|
||||||
# 查看数据
|
# 加载和预处理数据
|
||||||
head(data)
|
cir1 <- load_and_preprocess_data(input_csv)
|
||||||
|
|
||||||
# 转化matrix格式矩阵及数据归一化
|
# 打印数据维度和头部
|
||||||
data <- as.matrix(data)
|
print(dim(cir1))
|
||||||
cir1 <- t(scale(t(data)))
|
print(head(cir1))
|
||||||
|
|
||||||
# 处理 NA 值
|
# 定义颜色梯度
|
||||||
cir1[is.na(cir1)] <- 0
|
|
||||||
|
|
||||||
# 查看归一化后的数据
|
|
||||||
head(cir1)
|
|
||||||
|
|
||||||
# 定义热图颜色梯度
|
|
||||||
mycol <- colorRamp2(c(-2.5, 0.3, 3.1), c("blue", "white", "red"))
|
mycol <- colorRamp2(c(-2.5, 0.3, 3.1), c("blue", "white", "red"))
|
||||||
mycol1 <- colorRamp2(c(-2, 0, 2), c("#003399", "white", "#cccc00"))
|
|
||||||
|
|
||||||
# 打开PDF设备
|
# 打开PDF设备,设置宽度和高度相等
|
||||||
pdf(output_pdf, width = 10, height = 8)
|
pdf(output_pdf, width = 8, height = 8)
|
||||||
|
|
||||||
# 默认参数绘制普通热图
|
# 设置绘图参数
|
||||||
Heatmap(cir1, row_names_gp = gpar(fontsize = 6),
|
|
||||||
column_names_gp = gpar(fontsize = 8),
|
|
||||||
col = mycol,
|
|
||||||
name = "legend")
|
|
||||||
|
|
||||||
# 计算数据大小范围
|
|
||||||
range(cir1)
|
|
||||||
|
|
||||||
# 绘制基础环形热图
|
|
||||||
circos.heatmap(cir1, col = mycol)
|
|
||||||
circos.clear()
|
|
||||||
|
|
||||||
# 在circos.heatmap()中添加参数进行环形热图的调整和美化
|
|
||||||
circos.par(gap.after = c(30))
|
circos.par(gap.after = c(30))
|
||||||
circos.heatmap(cir1, col = mycol,
|
|
||||||
dend.side = "inside",
|
|
||||||
rownames.side = "outside",
|
|
||||||
rownames.col = "black",
|
|
||||||
rownames.cex = 1.2,
|
|
||||||
rownames.font = 1.2,
|
|
||||||
bg.border = "black",
|
|
||||||
cluster = TRUE)
|
|
||||||
circos.clear()
|
|
||||||
|
|
||||||
# 聚类树的调整和美化
|
# 绘制调整后的圆形热图
|
||||||
circos.par(gap.after = c(30))
|
circos.heatmap(cir1,
|
||||||
circos.heatmap(cir1, col = mycol, dend.side = "inside",
|
col = mycol,
|
||||||
rownames.side = "outside",
|
dend.side = "inside",
|
||||||
track.height = 0.28,
|
rownames.side = "outside",
|
||||||
rownames.col = "black",
|
rownames.col = "black",
|
||||||
rownames.cex = 1.3,
|
rownames.cex = 1.3,
|
||||||
rownames.font = 1.3,
|
track.height = 0.35, # 增加轨道高度
|
||||||
cluster = TRUE,
|
cluster = TRUE,
|
||||||
dend.track.height = 0.18,
|
dend.track.height = 0.18,
|
||||||
dend.callback = function(dend, m, si) {
|
dend.callback = function(dend, m, si) {
|
||||||
color_branches(dend, k = 15, col = 1:15)
|
color_branches(dend, k = 15, col = 1:15)
|
||||||
})
|
})
|
||||||
circos.clear()
|
|
||||||
|
|
||||||
# 添加图例标签等
|
|
||||||
lg <- Legend(title = "Exp", col_fun = mycol,
|
|
||||||
direction = c("vertical"))
|
|
||||||
grid.draw(lg)
|
|
||||||
|
|
||||||
# 添加列名
|
# 添加列名
|
||||||
circos.track(track.index = get.current.track.index(), panel.fun = function(x, y) {
|
circos.track(track.index = get.current.track.index(), panel.fun = function(x, y) {
|
||||||
@ -98,14 +66,19 @@ circos.track(track.index = get.current.track.index(), panel.fun = function(x, y)
|
|||||||
cn <- colnames(cir1)
|
cn <- colnames(cir1)
|
||||||
n <- length(cn)
|
n <- length(cn)
|
||||||
circos.text(rep(CELL_META$cell.xlim[2], n) + convert_x(0.5, "mm"),
|
circos.text(rep(CELL_META$cell.xlim[2], n) + convert_x(0.5, "mm"),
|
||||||
1:n + 5,
|
1:n + 2.5,
|
||||||
cn, cex = 0.6, adj = c(0, 0.5), facing = "inside")
|
cn, cex = 0.6, adj = c(0, 0.5), facing = "inside")
|
||||||
}
|
}
|
||||||
}, bg.border = NA)
|
}, bg.border = NA)
|
||||||
|
|
||||||
|
# 添加图例
|
||||||
|
lg <- Legend(title = "Exp", col_fun = mycol, direction = "vertical")
|
||||||
|
grid.draw(lg)
|
||||||
|
|
||||||
|
# 清除圆形图
|
||||||
circos.clear()
|
circos.clear()
|
||||||
|
|
||||||
# 关闭PDF设备
|
# 关闭PDF设备
|
||||||
dev.off()
|
dev.off()
|
||||||
|
|
||||||
message("热图已成功保存到: ", output_pdf)
|
message("热图已成功保存至: ", output_pdf)
|
Reference in New Issue
Block a user