添加longphase_phase规则后Snakemake管道出现KeyError求助
解决Snakemake添加longphase_phase规则后的KeyError问题
问题现象
运行SNP与5mC相位分析的Snakemake管道,添加longphase_phase规则后触发KeyError,错误日志如下:
Building DAG of jobs... InputFunctionException in rule call_snps_indels in file /data1/greenbab/users/ahunos/apps/workflows/phased_modifications/phase_5mC.smk, line 34: Error: KeyError: 'D-0-2_5000/D-0-2_5000' Wildcards: samples=D-0-2_5000/D-0-2_5000
错误原因
核心问题是longphase_phase规则中snpFile的路径定义错误:
原代码中snpFile路径为:
snpFile='results/call_snps_indels/{samples}/{samples}/snv.vcf.gz'
这里多了一层/{samples}目录,但call_snps_indels规则的输出路径是results/call_snps_indels/{samples}/snv.vcf.gz(仅单层级样本目录)。
当Snakemake解析该错误路径时,会将D-0-2_5000/D-0-2_5000整体识别为{samples}通配符的值,而样本配置文件中仅存在D-0-2_5000这一键,因此触发KeyError。
修复步骤
- 修改
longphase_phase规则的snpFile输入路径,移除多余的/{samples}层级:
rule longphase_phase: input: bamfile=lambda wildcards: config["samples"][wildcards.samples], modcallfile='results/longphase_modcall/{samples}/modcall_{samples}.vcf', snpFile='results/call_snps_indels/{samples}/snv.vcf.gz' # 修复此处路径 # 其余参数、输出、shell命令保持不变
- (可选)清理之前因错误路径生成的无效文件/目录,避免干扰后续运行:
rm -rf results/call_snps_indels/D-0-2_5000/D-0-2_5000/
验证方法
执行dry-run检查DAG构建是否正常:
snakemake -n
若输出无KeyError且显示正确的任务依赖,则修复成功,可正式运行管道。
内容的提问来源于stack exchange,提问作者sahuno
相关产品推荐
相关产品推荐

