Skip to content

Instantly share code, notes, and snippets.

@ctb
Last active February 1, 2020 17:26
Show Gist options
  • Save ctb/8815576b7b38e3b9c635c705d19aa09a to your computer and use it in GitHub Desktop.
Save ctb/8815576b7b38e3b9c635c705d19aa09a to your computer and use it in GitHub Desktop.
Testing out some snakemake namespace stuff.
global_var = 'a global variable'
rule all:
input:
"1-foo-hello.txt",
"2-bar-hello.txt",
"3-fiz-hello.txt",
"4-bif-hello.txt"
#
# 'word' is a wildcard that is automatically determined from desired output.
# must use 'wildcards.word' in shell block.
#
rule ex_1:
output: "1-{word}-hello.txt"
shell:
"echo {wildcards.word} > {output}"
#
# 'some_arg' is a parameter determined from Python code in the params: block.
# must use 'params.some_arg' to refer to it in shell block.
#
rule ex_2:
output: "2-{word}-hello.txt"
params:
some_arg="a param"
shell:
"echo {wildcards.word} {params.some_arg} > {output}"
#
# 'global_var' is a Python variable that is accessible from root level
# of Snakefile. No prefix needed, automatically substituted.
#
rule ex_3:
output: "3-{word}-hello.txt"
params:
some_arg="a param"
shell:
"echo {wildcards.word} {params.some_arg} {global_var} > {output}"
#
# use a 'run:' block instead of a shell block, which allows for Python code.
#
rule ex_4:
output: "4-{word}-hello.txt"
params:
some_arg="a param"
run:
with open(output[0], 'wt') as fp:
fp.write('{} {} {}\n'.format(wildcards.word, params.some_arg,
global_var))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment