我正在创建一个简单的 Snakemake 管道,其中包含 Snakefile 中的全局变量。在我的规则中调用的 Python 脚本中使用这些全局变量的推荐方法是什么?
我目前正在使用 argparse 命令行参数,如此处所述(Snakemake:将命令行参数传递给脚本),但我想知道是否有更好的方法。
如果该变量在 Snakefile 中指定,则可以通过params. 例如,
蛇形锉刀
# 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"
Run Code Online (Sandbox Code Playgroud)
脚本/测试.py
#!/usr/bin/env python
# access the variable through the `snakemake` object
print(snakemake.params.foo)
Run Code Online (Sandbox Code Playgroud)
请参阅有关外部 Python 脚本的 Snakemake 文档。
请注意,通常我发现最好的做法是将像上面示例这样的变量放在 a 中config.yaml。这有助于集中可调参数,提供单点配置以供重用。尽管可以在外部脚本中使用snakemake.config,但我仍然更喜欢将配置值显式传递为params,以便明确哪些规则取决于哪些配置值。
配置.yaml
foo: 100
Run Code Online (Sandbox Code Playgroud)
蛇形锉刀
configfile: "config.yaml"
rule test:
input: "a.in"
output: "a.out"
params:
foo=config["foo"]
script: "scripts/test.py"
Run Code Online (Sandbox Code Playgroud)
脚本/测试.py
#!/usr/bin/env python
# access the variable through the `snakemake` object
print(snakemake.params.foo)
Run Code Online (Sandbox Code Playgroud)
如果在 中提供了该值config.yaml,则还可以(可选)在 CLI 中覆盖它:
snakemake --config foo=150
Run Code Online (Sandbox Code Playgroud)
请参阅有关配置参数的文档。