Snakemake输入输出通配符不一致求助:单chr输入对应多method输出
Snakemake通配符匹配问题解决
问题背景
需求是针对多染色体(chr1/chr2/chr3)运行工作流,每个染色体对应多种分析方法(method1/method2):输入为仅含{chr}通配符的单个文件,输出需同时包含{chr}和{method}两个通配符。但运行时遇到两个问题:
- 输出命名为
{chr}_{method}_rslts.csv时,Snakemake要求输入也包含{method}通配符; - 输出改为
{chr}_rslts_{method}.csv时,提示规则缺少输入。
问题原因
Snakemake的通配符匹配逻辑是反向推导:从目标输出出发,匹配规则中的通配符并推导输入文件。当输出包含两个通配符时,默认会认为输入也需要对应匹配这两个通配符。但我们的输入只依赖{chr},和{method}无关,因此需要明确告知Snakemake:{method}是独立通配符,无需从输入中匹配。
解决方案
在规则中添加wildcard_constraints,声明{method}的合法格式,让Snakemake识别该通配符与输入无关。
完整代码示例
rule all: input: expand( config["outdir"] + "/{chr}_{method}_rslts.csv", chr=["chr1", "chr2", "chr3"], method=["method1", "method2"] ) # 定义method通配符的合法格式,可根据实际方法名修改正则 wildcard_constraints: method=r"method\d+" rule example_rule: input: config["indir"] + "/{chr}_fltrd.csv", output: config["outdir"] + "/{chr}_{method}_rslts.csv", shell: """ # 替换为实际分析命令,可通过{wildcards.method}区分不同分析逻辑 touch {output} """
补充说明
- 如果你的方法名不是
method1/method2这类数字后缀格式,可修改正则表达式,比如允许任意字符串:method=r".+"; - 若
rule all中已明确枚举所有method的可能值,也可省略wildcard_constraints,Snakemake会自动识别{method}为合法通配符:
rule all: input: expand( config["outdir"] + "/{chr}_{method}_rslts.csv", chr=["chr1", "chr2", "chr3"], method=["method1", "method2"] ) rule example_rule: input: config["indir"] + "/{chr}_fltrd.csv", output: config["outdir"] + "/{chr}_{method}_rslts.csv", shell: """ touch {output} """
内容的提问来源于stack exchange,提问作者Toffi
相关产品推荐
相关产品推荐

