Reputation: 21
First, here is a test example (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}
"""
When I run this script, it returns NameError: The name 'score' is unknown in this context. Please make sure that you defined that variable. Also note that braces not used for variable access have to be escaped by repeating them, i.e. {{print $1}}
, I have tried to {score} or replicate {}, but all doesn't work. So, I want to know how to solve this quetion. Thanks.
Upvotes: 2
Views: 911
Reputation: 3701
That is because snakemake tries to format the text and put the variables in the string. Since e.g. score are part of the script, snakemake can not deduce which variable it belongs to and it crashes. To escape this behaviour use double curly brackets: {{score[$3]}}
. It gets rather ugly with multiple curly brackets, like in your rule
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}
"""
(I hope I didn't miss any, but I think you get the idea)
Upvotes: 4