Go富集分析.R(配合Tbtools使用)

library(ggplot2)
library(dplyr)
library(tidyr)
library(stringr)

# 获取所有.xls文件
xls_files <- list.files(pattern = "\\.xls$")

# 循环处理每个文件
for (xls_file in xls_files) {
  # 读取数据
  go_data <- read.delim(xls_file, 
                        header = TRUE, 
                        check.names = FALSE,
                        stringsAsFactors = FALSE)
  
  # 数据预处理
  clean_data <- go_data %>%
    rename(
      "Class" = "Class",
      "GO_term" = "GO_Name",
      "GO_ID" = "GO_ID",
      "Pvalue" = "P_value",
      "FDR" = "corrected p-value(BH method)"
    ) %>%
    mutate(
      Pvalue = as.numeric(Pvalue),
      FDR = as.numeric(FDR),
      EnrichmentScore = as.numeric(EnrichmentScore)
    ) %>%
    filter(FDR < 0.05) %>%
    mutate(Significance = ifelse(FDR < 0.01, "**", ifelse(FDR < 0.05, "*", "")))
  
  # 跳过没有显著结果的文件
  if (nrow(clean_data) == 0) {
    message(paste("No significant terms in", xls_file, "skipping..."))
    next
  }
  
  # 分面显示所有显著类别
  all_significant <- clean_data %>%
    group_by(Class) %>%
    arrange(Pvalue) %>%
    slice_head(n = 10) %>%
    ungroup() %>%
    mutate(
      Term_label = str_wrap(paste0(GO_term, "\n(", GO_ID, ")"), width = 40)
    )
  
  # 生成图表
  p <- ggplot(all_significant, 
              aes(x = EnrichmentScore, 
                  y = reorder(Term_label, Pvalue), 
                  color = -log10(FDR), 
                  size = HitsGenesCountsInSelectedSet)) +
    geom_point() +
    facet_grid(Class ~ ., scales = "free_y", space = "free") +
    scale_color_gradient(low = "blue", high = "red") +
    labs(
      title = "GO Term Enrichment Analysis",
      x = "Enrichment Score",
      y = "GO Term",
      color = "-log10(FDR)",
      size = "Gene Count"
    ) +
    theme_bw() +
    theme(
      strip.text.y = element_text(angle = 0),
      axis.text.y = element_text(size = 9),
      panel.spacing = unit(0.5, "lines")
    )
  
  # 生成输出文件名(移除.xls后缀)
  output_name <- sub("\\.xls$", "", xls_file)
  
  # 保存图片
  ggsave(
    filename = paste0(output_name, ".png"),
    plot = p,
    width = 12,
    height = 8,
    dpi = 300
  )
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

终是蝶衣梦晓楼

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值