如何在蛇形包装器中访问蛇形文件中定义的变量?

时间:2019-06-04 13:18:23

标签: python snakemake

我想编写一个蛇形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节?

1 个答案:

答案 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:
    # ...