By Gengxin, 31 December, 2025
Forums

  1. 准备文件

/home/gengxin/ZLbud_1.methratio.txt

  1. 脚本使用方法

Rscript meth_depth_analysis.R 输入文件路径 输出文件前缀

#!/usr/bin/env Rscript

# 功能:处理甲基化数据(8条染色体),绘制无图例马卡龙配色箱线图并输出无表头平均深度汇总表
# 运行方式:Rscript meth_depth_analysis.R 输入文件路径 输出文件前缀

# 1. 获取命令行参数(输入文件 + 输出文件前缀)
args <- commandArgs(trailingOnly = TRUE)

# 参数校验:判断是否传入了2个必要参数
if (length(args) != 2) {
  stop("使用格式:Rscript meth_depth_analysis.R <输入methratio.txt文件路径> <输出文件前缀>\n示例:Rscript meth_depth_analysis.R /home/gengxin/ZLbud_1.methratio.txt /home/gengxin/result")
}

# 提取输入输出路径
input_file <- args[1]  # 第1个参数:输入甲基化数据文件
output_prefix <- args[2]  # 第2个参数:输出文件前缀

# 2. 加载依赖包(静默加载,避免冗余输出)
suppressPackageStartupMessages({
  library(tidyverse)
})

# 3. 读取并处理甲基化数据
tryCatch({
  methratio <- read_delim(
    file = input_file,
    delim = "\t",
    escape_double = FALSE,
    trim_ws = TRUE
  ) %>%
    dplyr::select(
      chr = `#chromsome`,  # 匹配实际列名(注意少字母o)
      loci,
      strand,
      context,
      C_count,
      CT_count,
      methRatio,
      MethContext,
      FiveContext = `5context`
    ) %>%
    mutate(context = if_else(context == "CG", "CpG", context)) %>%
    mutate(context = factor(context, levels = c("CpG", "CHG", "CHH")))
}, error = function(e) {
  stop(paste("读取输入文件失败:", e$message))
})

# ============= 数据筛选模块:更新为8条染色体 =============
message(paste("原始数据总行数:", nrow(methratio)))

# 场景1:按染色体筛选(LG01-LG08,共8条,可自定义染色体名称)
methratio_filtered <- methratio %>%
  filter(chr %in% c("LG01", "LG02", "LG03", "LG04",
                    "LG05", "LG06", "LG07", "LG08"))
boxplot_file <- paste0(output_prefix, "_CT_count_boxplot.pdf")

# 1. 定义8种马卡龙色系(每条染色体对应唯一颜色,柔和低饱和度,可自定义调整)
macaron_colors <- c(
  "#FFC2D1",  # 马卡龙淡粉(LG01)
  "#BDE0FE",  # 马卡龙淡蓝(LG02)
  "#e1f9e8",  # 马卡龙淡绿(LG03)
  "#FFFACD",  # 马卡龙淡黄(LG04)
  "#e1e1f1",  # 马卡龙淡紫(LG05)
  "#FFDAB9",  # 马卡龙淡橙(LG06)
  "#AFEEEE",  # 马卡龙淡青(LG07)
  "#D8BFD8"   # 马卡龙淡紫粉(LG08)
)

# 2. 绘制箱线图(应用8种马卡龙配色,无图例,纵坐标限定5~30)
p <- ggplot(data = methratio_filtered, aes(x = chr, y = CT_count)) +
  geom_boxplot(aes(fill = chr), outlier.shape = NA, na.rm = TRUE) +
  # 应用8种马卡龙色系,与8条染色体一一对应
  scale_fill_manual(values = macaron_colors) +
  # 限定纵坐标范围5~30,避免过高
  ylim(c(5, 30)) +
  labs(x = NULL, y = "depth", fill = NULL) +
  theme_classic() +
  # 优化x轴文字展示 + 核心修改:隐藏图例
  theme(
    axis.text.x = element_text(angle = 0, hjust = 0.5, vjust = 0.5, size = 10),
    plot.title = element_text(hjust = 0.5, size = 14, face = "bold"),
    legend.position = "none"  # 关键:设置图例位置为“无”,彻底隐藏图例
  )

# 保存箱线图(调整宽度为14,适配8条染色体展示,不拥挤不空旷)
ggsave(
  filename = boxplot_file,
  plot = p,
  width = 14,  # 比11条染色体的16更窄,适配8条染色体布局
  height = 8,
  dpi = 300
)
message(paste("8条染色体马卡龙配色箱线图(无图例)已保存至:", boxplot_file))

# ============= 无表头汇总表输出(保留原有逻辑) =============
chr_mean_depth <- methratio_filtered %>%
  dplyr::group_by(chr) %>%
  dplyr::summarise(
    mean_depth = round(mean(CT_count, na.rm = TRUE), digits = 1),
    .groups = "drop"
  )

# 保存无表头汇总表
summary_file <- paste0(output_prefix, "_chr_mean_depth.txt")
write_delim(
  chr_mean_depth,
  file = summary_file,
  delim = "\t",
  col_names = FALSE  # 去除表头,仅保留纯数据
)
message(paste("8条染色体无表头平均深度汇总表已保存至:", summary_file))

# 运行完成提示
message("所有分析完成!")
  1. 结果