add 更新代码结构,允许传入输入输出参数
This commit is contained in:
parent
5a42a4c880
commit
af494db908
111
heatmap_script.R
Normal file
111
heatmap_script.R
Normal file
@ -0,0 +1,111 @@
|
||||
# 加载所需的包
|
||||
library(grid)
|
||||
library(ComplexHeatmap)
|
||||
library(circlize)
|
||||
library(RColorBrewer)
|
||||
library(dendextend)
|
||||
library(dendsort)
|
||||
library(gridBase)
|
||||
|
||||
# 从命令行获取参数
|
||||
args <- commandArgs(trailingOnly = TRUE)
|
||||
|
||||
# 检查参数数量
|
||||
if (length(args) != 2) {
|
||||
stop("请提供两个参数:输入CSV文件路径和输出PDF文件路径。", call. = FALSE)
|
||||
}
|
||||
|
||||
# 获取输入CSV文件路径和输出PDF文件路径
|
||||
input_csv <- args[1]
|
||||
output_pdf <- args[2]
|
||||
|
||||
# 检查输入文件是否存在
|
||||
if (!file.exists(input_csv)) {
|
||||
stop("输入文件不存在,请检查路径是否正确。", call. = FALSE)
|
||||
}
|
||||
|
||||
# 加载绘图数据
|
||||
data <- read.table(file = input_csv, header = TRUE, row.names = 1, sep = ',')
|
||||
|
||||
# 查看数据
|
||||
head(data)
|
||||
|
||||
# 转化matrix格式矩阵及数据归一化
|
||||
data <- as.matrix(data)
|
||||
cir1 <- t(scale(t(data)))
|
||||
|
||||
# 处理 NA 值
|
||||
cir1[is.na(cir1)] <- 0
|
||||
|
||||
# 查看归一化后的数据
|
||||
head(cir1)
|
||||
|
||||
# 定义热图颜色梯度
|
||||
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(output_pdf, width = 10, 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.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, col = mycol, dend.side = "inside",
|
||||
rownames.side = "outside",
|
||||
track.height = 0.28,
|
||||
rownames.col = "black",
|
||||
rownames.cex = 1.3,
|
||||
rownames.font = 1.3,
|
||||
cluster = TRUE,
|
||||
dend.track.height = 0.18,
|
||||
dend.callback = function(dend, m, si) {
|
||||
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) {
|
||||
if (CELL_META$sector.numeric.index == 1) {
|
||||
cn <- colnames(cir1)
|
||||
n <- length(cn)
|
||||
circos.text(rep(CELL_META$cell.xlim[2], n) + convert_x(0.5, "mm"),
|
||||
1:n + 5,
|
||||
cn, cex = 0.6, adj = c(0, 0.5), facing = "inside")
|
||||
}
|
||||
}, bg.border = NA)
|
||||
|
||||
circos.clear()
|
||||
|
||||
# 关闭PDF设备
|
||||
dev.off()
|
||||
|
||||
message("热图已成功保存到: ", output_pdf)
|
Reference in New Issue
Block a user