You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Snakemake调用R脚本绘图异常:耗时久、图形异常求助

Snakemake调用R绘图脚本卡顿且输出异常的解决方案

问题情况

直接在shell运行R绘图脚本1分钟即可完成,但通过Snakemake调用时耗时长达2小时,输出PDF尺寸远大于正常大小、部分内容重复绘制。已尝试单线程(-c1)运行,问题仍存在。R脚本使用ggplot2绘图并通过ggsave保存,无屏幕绘图操作。

相关代码

Snakemake规则

rule polyA_results:
    input:
        "./{example}/analysis/polyA_estimate/header.tsv",
        "./{example}/analysis/polyA_estimate/polya_results.pass_only.tsv"
    output:
        "./{example}/analysis/polyA_estimate/polyA_estimate.tsv"
    shell:
        "cat {input[0]} {input[1]} > {output}"

rule R_drawing:
    input:
        "./{example}/analysis/polyA_estimate/polyA_estimate.tsv"
    output:
        "./{example}/analysis/polyA_estimate/polyA_estimate.pdf"
    script:
        "./Tail.R"

R绘图脚本

library(ggplot2)
library(hrbrthemes)
library(viridis)
library(cowplot)
library(showtext)
font_add('Arial','/usr/share/fonts/LiberationSans-Regular.ttf')
showtext_auto() 

percontig <- read.csv(snakemake@input[[1]],sep = "\t",header = F)
contigsummary <- aggregate(percontig$V9,by=list(type=percontig$V2),mean)
contigsummary$y <- c("plus")


p1 <- ggplot(contigsummary) +
  geom_violin(mapping = aes(x=contigsummary$y,y=contigsummary$x),width=0.6,fill="#5A005A") + 
  geom_boxplot(mapping=aes(x=contigsummary$y,y=contigsummary$x), color="grey",width=0.1, alpha=0.2) +
  scale_fill_viridis(discrete = TRUE) +
  theme_ipsum() +
  theme(
    legend.position="none",
    plot.title = element_text(size=16,hjust = 0.5),
    axis.text.y = element_text(size = 10, family = "myFont", vjust = 0.5, hjust = 0.5),
    axis.title.y = element_text(size=13, face="plain" , vjust = 10.2, hjust = 0.5),
    axis.title.x = element_text(size=13, face="plain" ,  hjust = 0.5),
    panel.border = element_blank(),
    axis.line = element_line(size=1, colour = "black")
  ) +
  ggtitle("Poly(A) Length of Reads") +
  theme(axis.text.x = element_blank()) +
  xlab("Reads") +
  ylab("Poly(A) Length (nt)")



p2 <- ggplot(percontig) + 
  geom_violin(mapping = aes(x=percontig$V10,y=percontig$V9),width=0.6,fill="#316397")+
  geom_boxplot(mapping=aes(x=percontig$V10,y=percontig$V9), color="grey",width=0.1, alpha=0.2) +
  scale_fill_viridis(discrete = TRUE) +
  theme_ipsum() +
  theme(
    legend.position="none",
    plot.title = element_text(size=16,hjust = 0.5),
    axis.text.y = element_text(size = 10, family = "myFont", vjust = 0.5, hjust = 0.5),
    axis.title.y = element_text(size=13, face="plain" , vjust = 10.2, hjust = 0.5),
    axis.title.x = element_text(size=13, face="plain" , hjust = 0.5),
    panel.border = element_blank(),
    axis.line = element_line(size=1, colour = "black")
  ) +
  ggtitle("Poly(A) Length of Contigs") +
  theme(axis.text.x = element_blank()) +
  xlab("Contigs") +
  ylab("Poly(A) Length (nt)")

p3 <- cowplot::plot_grid(p1, p2, ncol = 2,labels = LETTERS[1:2])

ggsave(snakemake@output[[1]],p3,width = 20, height = 20)

解决方案建议

  • 移除showtext依赖:showtext在非交互式批量环境中易引发渲染卡顿和异常,注释掉library(showtext)、font_add、showtext_auto()三行代码,改用系统默认字体支持。
  • 修正ggplot映射方式:避免在aes()中直接引用data$column,改用列名简化语法,比如将aes(x=contigsummary$y,y=contigsummary$x)改为aes(x=y,y=x),减少ggplot重复解析数据的开销,避免绘图重复。
  • 调整ggsave输出尺寸:当前width=20, height=20默认单位是英寸,导致PDF过大,添加单位参数改为合理尺寸,例如:
    ggsave(snakemake@output[[1]], p3, width=20, height=10, units="cm", dpi=300)
    
  • 统一R运行环境:在Snakemake规则中指定与shell一致的R环境,比如用conda环境锁定包版本:
    rule R_drawing:
        input:
            "./{example}/analysis/polyA_estimate/polyA_estimate.tsv"
        output:
            "./{example}/analysis/polyA_estimate/polyA_estimate.pdf"
        conda:
            "r_env.yaml"  # 包含ggplot2、hrbrthemes等包的yaml文件
        script:
            "./Tail.R"
    
  • 添加调试日志:在R脚本关键步骤添加时间打印,定位卡顿环节:
    print(paste("Data loaded at:", Sys.time()))
    # ... 绘图后
    print(paste("Plot generated at:", Sys.time()))
    # ... 保存后
    print(paste("PDF saved at:", Sys.time()))
    
    同时用snakefile --verbose运行,查看详细输出日志。

内容的提问来源于stack exchange,提问作者伊沢石之助

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.24 22:12:55