如何在 Snakemake 中将 stderr 和 stdout 重定向全局设置到日志文件中?

问题描述 投票:0回答:1

我正在使用snakemake来构建工作流程。我希望所有规则的stderr 和stdout 默认情况下分别重定向到文件logs/{rule}/{wildcards}.o 和logs/{rule}/{wildcards}.e 中。我怎样才能实现这个目标?

以下代码通过添加给定规则的 shell 命令来完成我想要的操作。但是,我不想将其添加到每个规则中。我尝试使用 shell.prefix(...),它为每个规则添加命令前缀,但我找不到访问规则名称或规则通配符的方法。

SAMPLES = ['A', 'B']

rule all:
    input:
        expand('test.{sample}',sample=SAMPLES)

rule test_rule:
    output: 'test.{sample}'
    shell:
        #These three lines need to be prepended for logging.
        'mkdir -p logs/{rule}; '
        'exec 2> logs/{rule}/{wildcards}.e; '
        'exec 1> logs/{rule}/{wildcards}.o; '
        #This is the actual code for the rule
        'touch {output}; '
        'echo "test for {wildcards.sample} created";'
        ' >&2 echo "error message"'

上面的代码给出了logs/{rule}/{wildcards}.o/e中带有stdout和stderr的日志文件的预期结果,但我想全局设置它,而不是针对每个规则。

snakemake
1个回答
0
投票

这似乎是应该实现的东西,所以我希望其他人用内置方法回答!您可以将前导码替换为字符串,以减少重复,只要它是一个字符串,您就可以与 shell 指令连接以使其工作:

redirect_string = (
        'mkdir -p logs/{rule}; '
        'exec 2> logs/{rule}/{wildcards}.e; '
        'exec 1> logs/{rule}/{wildcards}.o; '
      )

rule test_rule:
    output: 'test.{sample}'
    shell:
        redirect_string +
        #This is the actual code for the rule
        'touch {output}; '
        'echo "test for {wildcards.sample} created";'
        ' >&2 echo "error message"'

您可以使用 f 字符串,但如果您使用 Snakemake 的格式,则内部通配符不会被替换:

shell:
    f'{redirect_string} '  # this is ok
...
shell:
    '{redirect_string} '  # this does not format rule and wildcards
© www.soinside.com 2019 - 2024. All rights reserved.