add 优化R脚本

This commit is contained in:
freefire 2025-01-12 03:51:54 +08:00
parent 16cd091932
commit 1fce34b1ca

View File

@ -1,27 +1,27 @@
# Load required packages
# 加载所需的包
suppressPackageStartupMessages(library(circlize))
suppressPackageStartupMessages(library(RColorBrewer))
suppressPackageStartupMessages(library(ComplexHeatmap))
suppressPackageStartupMessages(library(dendextend))
# Get arguments from the command line
# 获取命令行参数
args <- commandArgs(trailingOnly = TRUE)
# Check the number of arguments
# 检查参数数量
if (length(args) != 2) {
stop("Please provide two arguments: the input CSV file path and the output PDF file path.", call. = FALSE)
stop("请提供两个参数输入CSV文件路径和输出PDF文件路径。", call. = FALSE)
}
# Get the input CSV file path and the output PDF file path
# 获取输入CSV文件路径和输出PDF文件路径
input_csv <- args[1]
output_pdf <- args[2]
# Check if the input file exists
# 检查输入文件是否存在
if (!file.exists(input_csv)) {
stop("The input file does not exist. Please check the file path.", call. = FALSE)
stop("输入文件不存在。请检查文件路径。", call. = FALSE)
}
# Function to load and preprocess data
# 函数加载和预处理数据
load_and_preprocess_data <- function(file_path) {
data <- read.table(file = file_path, header = TRUE, row.names = 1, sep = ',')
data_matrix <- as.matrix(data)
@ -30,31 +30,37 @@ load_and_preprocess_data <- function(file_path) {
return(normalized_data)
}
# Load and preprocess data
# 加载和预处理数据
cir1 <- load_and_preprocess_data(input_csv)
# Define the color gradient for the heatmap
# 打印数据维度和头部
print(dim(cir1))
print(head(cir1))
# 定义颜色梯度
mycol <- colorRamp2(c(-2.5, 0.3, 3.1), c("blue", "white", "red"))
# Open the PDF device
pdf(output_pdf, width = 10, height = 8)
# 打开PDF设备设置宽度和高度相等
pdf(output_pdf, width = 8, height = 8)
# Draw the adjusted and beautified circular heatmap
# 设置绘图参数
circos.par(gap.after = c(30))
# 绘制调整后的圆形热图
circos.heatmap(cir1,
col = mycol,
dend.side = "inside",
rownames.side = "outside",
rownames.col = "black",
rownames.cex = 1.3,
track.height = 0.35,
track.height = 0.35, # 增加轨道高度
cluster = TRUE,
dend.track.height = 0.18,
dend.callback = function(dend, m, si) {
color_branches(dend, k = 15, col = 1:15)
})
# Add column names
# 添加列名
circos.track(track.index = get.current.track.index(), panel.fun = function(x, y) {
if (CELL_META$sector.numeric.index == 1) {
cn <- colnames(cir1)
@ -65,14 +71,14 @@ circos.track(track.index = get.current.track.index(), panel.fun = function(x, y)
}
}, bg.border = NA)
# Add legend
# 添加图例
lg <- Legend(title = "Exp", col_fun = mycol, direction = "vertical")
grid.draw(lg)
# Clear the circular plot
# 清除圆形图
circos.clear()
# Close the PDF device
# 关闭PDF设备
dev.off()
message("The heatmap has been successfully saved to: ", output_pdf)
message("热图已成功保存至: ", output_pdf)