awk 命令在 snakemake --use-singularity 中失败

sss*_*cha 4 bash snakemake singularity-container

我正在尝试将 Snakemake 与 Singularity 结合起来,并且我注意到awk在使用 Singularity 时一个简单的命令不再起作用。在$1最后一行被取代的bash,而不是被用来作为第一个字段awk

这是一个最小的工作示例(Snakefile):

singularity: "docker://debian:stretch"
rule all:
    input: "test.txt"
rule test:
    output: 
        "test.txt"
    shell:
        "cat /etc/passwd | awk -F':' '{{print $1}}' > {output}"
Run Code Online (Sandbox Code Playgroud)

当我在snakemake没有奇点的情况下运行时,输出test.txt看起来如预期(仅包含用户名)。当我运行时snakemake --use-singularity,文件包含整行,例如root:x:0:0:root:/root:/bin/bash.

这是 Snakemake 的日志:

$ snakemake --use-singularity --printshellcmd                                                                                                               
Building DAG of jobs...
Using shell: /usr/bin/bash
Provided cores: 1
Rules claiming more threads will be scaled down.
Job counts:
        count   jobs
        1       all
        1       test
        2

rule test:
    output: test.txt
    jobid: 1

cat /etc/passwd | awk -F':' '{print $1}' > test.txt
Activating singularity image /scratch/test/.snakemake/singularity/fa9c8c7220ff16e314142a5d78ad6cff.simg
Finished job 1.
1 of 2 steps (50%) done

localrule all:
    input: test.txt
    jobid: 0

Finished job 0.
2 of 2 steps (100%) done
Run Code Online (Sandbox Code Playgroud)

小智 6

我有一个类似的问题,经过大量的反复试验终于解决了它。目前(2018 年 11 月,对于 Snakemake 5.3),这有点没有记录,所以我认为最好把它放在这里以供将来参考以帮助他人......

上面的所有示例都错误地将双引号与 bash -c 一起使用,这不是 Snakemake 构造它的方式。相反,Snakemake 使用bash -c ' modified_command ', 所以单引号调用 Singularity 。首先,这改变了命令中特殊字符的处理方式。其次,截至目前,Snakemake 将实际命令中的所有单引号替换为转义版本 \'。但是,这仅适用于与 Singularity 一起使用时。

因此,如果您的命令包含单引号,则在使用 --use-singularity 提交或在正常模式下运行时会出现问题。我所知道的在这两种情况下都有效的唯一可行解决方案如下:

shell: """awk "{{OFS="\\t"}};{{print \$2}}" {input}"""
Run Code Online (Sandbox Code Playgroud)

因此,以下规则适用:

  1. 不要在命令中使用单引号,否则它们会被替换,这会导致错误。
  2. 转义某些字符,例如 \t 转 \\t、$ 转义 \$ 和 { 转义 {{。
  3. 使用三引号将命令行调用括起来。

我希望这会有所帮助,一旦有实施更新,我会更新这篇文章。

  • 实际上,我发现在“shell:”下运行“awk”时,使用双大括号“{{ }}”效果很好,无需转义“\t”或“$” (2认同)