首先,这是一个测试示例(Snakefile):
rule test:
input:"path/file1", "path/file2"
output:"path/file3"
shell:
"""
awk 'NR==FNR{score[$3]=$5;next}{{sum=0}for(i=$2;i<=$3;i++){sum+=score[i]}printf "%-10s\\t%-10s\\n",sum,$4}' {input[0]} {input[1]} >> {output}
"""
当我运行此脚本时,它返回NameError:在此上下文中名称“score”未知。请确保您定义了该变量。另请注意,不用于变量访问的大括号必须通过重复进行转义,即 {{print $1}}
,我尝试过 {score} 或复制 {},但都不起作用。所以,我想知道如何解决这个问题。谢谢。
最佳答案
这是因为snakemake尝试格式化文本并将变量放入字符串中。因为例如Score 是脚本的一部分,snakemake 无法推断它属于哪个变量并且崩溃。要避免此行为,请使用双大括号:{{score[$3]}}
。就像您的规则中那样,使用多个大括号会变得相当难看
rule test:
input:"path/file1", "path/file2"
output:"path/file3"
shell:
"""
awk 'NR==FNR{{score[$3]=$5;next}}{{{{sum=0}}for(i=$2;i<=$3;i++){{sum+=score[i]}}printf "%-10s\\t%-10s\\n",sum,$4}}' {input[0]} {input[1]} >> {output}
"""
(我希望我没有错过任何内容,但我想你明白了)
关于snakemake - 在snakemake中使用awk时出现NameError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64364682/