CircosHeatmap-aardio/res/heatmap_script.R

78 lines
2.4 KiB
R
Raw Normal View History

2025-01-12 00:52:51 +08:00
# Load required packages
suppressPackageStartupMessages(library(circlize))
suppressPackageStartupMessages(library(RColorBrewer))
suppressPackageStartupMessages(library(ComplexHeatmap))
2025-01-12 00:52:51 +08:00
suppressPackageStartupMessages(library(dendextend))
# Get arguments from the command line
args <- commandArgs(trailingOnly = TRUE)
2025-01-12 00:52:51 +08:00
# 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)
}
# Get the input CSV file path and the output PDF file path
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)
}
# 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)
normalized_data <- t(scale(t(data_matrix)))
normalized_data[is.na(normalized_data)] <- 0
return(normalized_data)
}
2025-01-12 00:52:51 +08:00
# Load and preprocess data
cir1 <- load_and_preprocess_data(input_csv)
2025-01-12 00:52:51 +08:00
# Define the color gradient for the heatmap
mycol <- colorRamp2(c(-2.5, 0.3, 3.1), c("blue", "white", "red"))
# Open the PDF device
pdf(output_pdf, width = 10, height = 8)
# Draw the adjusted and beautified circular heatmap
2025-01-12 00:52:51 +08:00
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,
cluster = TRUE,
dend.track.height = 0.18,
dend.callback = function(dend, m, si) {
color_branches(dend, k = 15, col = 1:15)
})
# Add column names
2025-01-12 00:52:51 +08:00
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 + 2.5,
2025-01-12 00:52:51 +08:00
cn, cex = 0.6, adj = c(0, 0.5), facing = "inside")
}
}, bg.border = NA)
# Add legend
2025-01-12 03:27:50 +08:00
lg <- Legend(title = "Exp", col_fun = mycol, direction = "vertical")
grid.draw(lg)
# Clear the circular plot
2025-01-12 00:52:51 +08:00
circos.clear()
# Close the PDF device
dev.off()
2025-01-12 03:27:50 +08:00
message("The heatmap has been successfully saved to: ", output_pdf)