建议使用shell()执行多个shell命令的方法

ted*_*oal 11 shell snakemake

在snakemake中,使用shell()函数执行多个命令的推荐方法是什么?

tom*_*nsc 24

您可以shell()run规则块内多次调用(规则可以指定run:而不是shell:):

rule processing_step:
    input:
        # [...]
    output:
        # [...]
    run:
        shell("somecommand {input} > tempfile")
        shell("othercommand tempfile {output}")
Run Code Online (Sandbox Code Playgroud)

否则,由于运行块接受Python代码,您可以构建一个命令列表作为字符串并迭代它们:

rule processing_step:
    input:
        # [...]
    output:
        # [...]
    run:
        commands = [
            "somecommand {input} > tempfile",
            "othercommand tempfile {output}"
        ]
        for c in commands:
            shell(c)
Run Code Online (Sandbox Code Playgroud)

如果在执行规则期间不需要Python代码,则可以在shell块中使用三引号字符串,并像在shell脚本中一样编写命令.对于纯shell规则,这可以说是最具可读性的:

rule processing_step:
    input:
        # [...]
    output:
        # [...]
    shell:
        """
        somecommand {input} > tempfile
        othercommand tempfile {output}
        """
Run Code Online (Sandbox Code Playgroud)

如果壳命令取决于前面的命令的成功/失败,它们可以与通常的外壳脚本运营商如被接合||&&:

rule processing_step:
    input:
        # [...]
    output:
        # [...]
    shell:
        "command_one && echo 'command_one worked' || echo 'command_one failed'"
Run Code Online (Sandbox Code Playgroud)

  • 好的,谢谢。在网站上展示这些示例对人们很有用。 (2认同)