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

使用Snakemake通配符+expand聚合样本时遭遇语法错误

解决Snakemake规则input中expand语法错误问题

问题定位

你遇到的语法错误并非expand函数本身的问题(毕竟其他位置调用正常),而是Snakemake规则块内的语法解析细节导致的,大概率是缩进、参数书写格式或解析器对inline调用的兼容问题。

可行解决方案

方案1:规范规则input的书写格式

确保规则内的input块缩进正确(用4个空格,和output、shell等关键字对齐),直接使用expand返回的列表即可:

# 全局定义样本列表
all_samples = ["TN2L_S1", "TN3L_S3", "TN2R_S2", "TN3R_S4"]

rule aggregate_summary:
    input:
        expand("{sample}.dedup.bam", sample=all_samples)
    output:
        "summary_stats.txt"
    shell:
        """
        # 替换成你的汇总命令,示例用samtools flagstat统计
        for bam in {input}; do
            samtools flagstat $bam >> {output}
        done
        """

方案2:提前预存expand结果(规避解析器问题)

如果方案1仍报错,可将expand的结果提前赋值给全局变量,再在规则input中引用:

all_samples = ["TN2L_S1", "TN3L_S3", "TN2R_S2", "TN3R_S4"]
# 提前生成所有BAM路径列表
all_dedup_bams = expand("{sample}.dedup.bam", sample=all_samples)

rule aggregate_summary:
    input: all_dedup_bams
    output: "summary_stats.txt"
    shell:
        """
        for bam in {input}; do
            samtools flagstat $bam >> {output}
        done
        """

方案3:给输入项命名(更清晰的写法)

如果需要在shell中明确引用输入集合,可给input项命名,增强可读性:

all_samples = ["TN2L_S1", "TN3L_S3", "TN2R_S2", "TN3R_S4"]

rule aggregate_summary:
    input:
        bam_files = expand("{sample}.dedup.bam", sample=all_samples)
    output:
        "summary_stats.txt"
    shell:
        """
        for bam in {input.bam_files}; do
            samtools flagstat $bam >> {output}
        done
        """

排查要点

  • 检查规则块内的缩进:必须用4个空格,不能混用制表符
  • 若input包含多个项,确保项之间用逗号分隔(比如input: expand(...), "other_file.txt")
  • 确认all_samples变量是全局定义(在所有规则之前),而非局部变量

内容的提问来源于stack exchange,提问作者Pipho

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.15 14:47:24