【问题标题】:Using the expand() function in snakemake to perform a shell command multiple times使用snakemake中的expand()函数多次执行shell命令
【发布时间】:2019-10-02 06:23:30
【问题描述】:

我想在snakemake 的帮助下对不同的输入文件多次执行R 脚本。为此,我尝试使用扩展功能。

我对snakemake 比较陌生,当我正确理解它时,扩展功能会为我提供例如多个输入文件,然后将它们全部连接起来并通过{input} 提供。

是否可以对文件逐个调用shell命令?

假设我的 config.yaml 中有这个定义:

types:
    - "A"
    - "B" 

这将是我的示例规则:

rule manual_groups:
    input:
        expand("chip_{type}.bed",type=config["types"])
    output:
        expand("data/p_chip_{type}.model",type=config["types"])
    shell:
        "Rscript scripts/pre_process.R {input}"

这将导致命令:

Rscript scripts/pre_process.R chip_A.bed chip_B.bed

是否可以使用如下两种类型独立调用命令两次:

Rscript scripts/pre_process.R chip_A.bed
Rscript scripts/pre_process.R chip_B.bed

提前感谢您的帮助!

【问题讨论】:

    标签: snakemake


    【解决方案1】:

    rule all 中定义final target files,然后在规则manual_groups 中使用适当的通配符(即type)。这将为rule all 中列出的每个输出文件单独运行rule manual_groups

    rule all:
        input:
            expand("data/p_chip_{type}.model",type=config["types"])
    
    
    rule manual_groups:
        input:
            "chip_{type}.bed"
        output:
            "data/p_chip_{type}.model"
        shell:
            "Rscript scripts/pre_process.R {input}"
    

    PS- 由于与 Python 的 type 方法可能存在冲突,您可能需要更改通配符术语 type

    【讨论】:

    • 谢谢,它有效!我不知道您可以在规则本身之外的其他部分定义类型通配符。
    【解决方案2】:

    我同意@ManavalanGajapathy 的回答,即这是解决您的 问题的最可靠解决方案。然而,这不是一个完整的答案。

    expand 只是一个在 Snakemake 中定义的常规 Python 函数。这意味着当您可以使用 Python 时,您可以在任何地方使用它。它只是一个实用程序,它接受一个字符串和参数进行替换,并返回字符串列表,其中每个字符串都是单个替换的结果。这个实用程序在很多地方都很方便。下面我提供了一个很好的例子来说明这个想法。假设您需要将文本文件作为输入并替换一些字符(该列表应从配置中提供)。让我们假设您知道唯一的方法:作为sed 脚本的管道。像这样:

    cat input.txt | sed 's/A/a/g' | sed 's/B/b/g' | sed 's/C/c/g' > output.txt
    

    您得出的结论是,您需要流水线化一系列 sed 命令,这些命令在两个符号上有所不同:sed 's/X/x/g'。这是使用expand 函数的解决方案:

    rule substitute:
        input: "input.txt"
        output: "output.txt"
        params:
           from = ["A", "B", "C"],
           to = ["a", "b", "c"]
        shell: "cat {input} | " + " | ".join(expand("sed 's/{from}/{to}/g'", zip, from=params.from, to=params.to)) + " > {output}"
    

    【讨论】:

      猜你喜欢
      • 2022-11-13
      • 1970-01-01
      • 1970-01-01
      • 2013-01-08
      • 2011-03-28
      • 1970-01-01
      • 2021-02-03
      • 1970-01-01
      • 2013-10-13
      相关资源
      最近更新 更多