I have a snakemake rule like:
rule get_coverage:
input:
cram=lambda wildcards: expand(config['input']['cram'][wildcards.reference_genome], #access_id = access_id[wildcards.sample_name],
sample_name='{sample_name}'),
bai=lambda wildcards: expand(config['input']['cram'][wildcards.reference_genome] + '.crai', #access_id = access_id[wildcards.sample_name],
sample_name='{sample_name}'),
ref=lambda wildcards: config['reference_genome'][wildcards.reference_genome],
chrom_size=lambda wildcards: config['chrom_size'][wildcards.reference_genome]
output:
coverage=directory(config['output']['median_coverage'])
conda:
src + '/env/acc_mask.yml'
threads: 19
script:
"""
mosdepth \
-n \
-t {threads} \
--use-median \
-f {input.ref} \
-b {input.chrom_size} \
{output.median_coverage} \
{input.cram}
"""
I got a error:
RuleException:
NameError in line xxx of xxx.smk:
The name 'threads' 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 ...
In my impression, threads, as a wildcards, can be replaced in the shell. Is there anything wrong with my script?
>Solution :
You are correct that {threads} can be used in a shell directive. It cannot be used in a script directive however. Your rule contains the latter, so fixing that should make it work:
rule get_coverage:
input:
cram=lambda wildcards: expand(config['input']['cram'][wildcards.reference_genome], #access_id = access_id[wildcards.sample_name],
sample_name='{sample_name}'),
bai=lambda wildcards: expand(config['input']['cram'][wildcards.reference_genome] + '.crai', #access_id = access_id[wildcards.sample_name],
sample_name='{sample_name}'),
ref=lambda wildcards: config['reference_genome'][wildcards.reference_genome],
chrom_size=lambda wildcards: config['chrom_size'][wildcards.reference_genome]
output:
coverage=directory(config['output']['median_coverage'])
conda:
src + '/env/acc_mask.yml'
threads: 19
shell:
"""
mosdepth \
-n \
-t {threads} \
--use-median \
-f {input.ref} \
-b {input.chrom_size} \
{output.median_coverage} \
{input.cram}
"""