Passing Variables
If the variable is specified in the Snakefile, then it could be passed via params
. For example,
Snakefile
# global variable to use
FOO = 100
rule test:
input: "a.in"
output: "a.out"
params:
foo=FOO # pass the variable value as 'foo'
script: "scripts/test.py"
scripts/test.py
#!/usr/bin/env python
# access the variable through the `snakemake` object
print(snakemake.params.foo)
See Snakemake documentation on external Python scripts.
Additional Comments
Note, that generally I find it better practice to place a variable like the above example in a config.yaml
instead. That helps centralize adjustable parameters, providing a single point of configuration for reuse. Despite the availability of snakemake.config
in external scripts, I still prefer to explicitly pass configuration values as params
, so as to make it clear which rules depend on what configuration values.
Example
config.yaml
foo: 100
Snakefile
configfile: "config.yaml"
rule test:
input: "a.in"
output: "a.out"
params:
foo=config["foo"]
script: "scripts/test.py"
scripts/test.py
#!/usr/bin/env python
# access the variable through the `snakemake` object
print(snakemake.params.foo)
Overriding Configuration Parameters
If the value is provided in the config.yaml
, one can also then (optionally) override it at the CLI:
snakemake --config foo=150
See documentation on configuration parameters.