我想编写一个蛇形wrapper,它可以根据在主蛇形文件中定义的变量的值来适应其行为。
这似乎并不简单,当我尝试在包装器中使用此类变量时,我得到了NameError
。
特别是,我想让包装器在群集中运行时在外壳命令周围添加module load ...
和module unload ...
命令,该群集使用这种机制将某些程序放入PATH
中,根据配置文件中定义的信息:
在主要的蛇文件中:
load_modules = False
# By default, cluster nodes are expected
# to have names starting with "tars"
cluster_prefix = config.get("cluster_prefix", "tars")
if cluster_prefix:
from socket import gethostname
if gethostname().startswith(cluster_prefix):
load_modules = True
# ...
rule ...:
# ...
wrapper:
"file://path/to/the/wrapper/folder"
然后,在相应的wrapper.py
文件中:
if load_modules:
# ...
结果是:
NameError: name 'load_modules' is not defined
是否可以使包装程序知道某些变量,而无需通过规则中的额外params
节?
答案 0 :(得分:0)
不需要在规则的params
部分中添加内容的一种可能性是将变量存储在config
中,这可以在snakemake包装器中以snakemake.config
的形式访问。
例如,在snakefile中:
# By default, cluster nodes are expected
# to have names starting with "tars"
cluster_prefix = config.get("cluster_prefix", "tars")
if cluster_prefix:
from socket import gethostname
if gethostname().startswith(cluster_prefix):
config["load_modules"] = True
# ...
rule ...:
# ...
wrapper:
"file://path/to/the/wrapper/folder"
在wrapper.py
中:
load_modules = snakemake.config.get("load_modules", False)
if load_modules:
# ...