Snakemake:如何在config.yml文件中保存和访问示例详细信息?

时间:2018-01-09 18:16:18

标签: python snakemake

当样本名称没有写入snakemake工作流程时,有人可以帮助我理解是否可以从config.yml文件中访问样本详细信息?这样我就可以重复使用不同项目的工作流程,只调整配置文件。让我举个例子:

我有四个属于一起的样本,应该一起分析。它们被称为sample1-4。每个样本都附带了一些更多的信息,但为了简单起见,我们可以说它只是一个名称标签,如S1,S2等。

我的config.yml文件可能如下所示:

samples: ["sample1","sample2","sample3","sample4"]

sample1:
  tag: "S1"
sample2:
  tag: "S2"
sample3:
  tag: "S3"
sample4:
  tag: "S4"

这是我们使用的snakefile的一个例子:

configfile: "config.yaml"

rule final: 
  input: expand("{sample}.txt", sample=config["samples"])

rule rule1:
  output:  "{sample}.txt"
  params:  tag=config["{sample}"]["tag"]
  shell:   """
           touch {output}
           echo {params.tag} > {output}

rule1尝试做的是创建一个以每个样本命名的文件,保存在配置文件中的samples变量中。到目前为止没问题。然后,我想将示例标记打印到该文件中。正如上面编写的代码,运行snakemake将失败,因为config["{sample}"]将在配置文件中查找不存在的{sample}变量,因为我需要将其替换为例如,运行规则的当前样本sample1

有人知道这是否可行,如果是,我该怎么做?

理想情况下,我想更多地压缩信息(见下文),但这还有待进一步发展。

samples:
    sample1:
        tag: "S1"
    sample2:
        tag: "S2"
    sample3:
        tag: "S3"
    sample4:
        tag: "S4"

2 个答案:

答案 0 :(得分:4)

我建议使用制表符分隔文件来存储样本信息。

sample.tab:

Sample     Tag      
1          S1   
2          S2

您可以在配置文件中存储此文件的路径,并在Snakefile中读取它。

config.yaml:

sample_file: "sample.tab"

Snakefile:

configfile: "config.yaml"

sample_file = config["sample_file"]

samples = read_table(sample_file)['Sample']
tags    = read_table(sample_file)['Tag']

通过这种方式,您可以针对任意数量的样本重复使用您的工作流程。

除此之外,在Snakemake中,你通常可以通过加倍来逃避大括号,也许你可以试试。

祝你好运!

答案 1 :(得分:2)

params部分中,您需要提供wildcards的功能。以下对您的工作流程的修改似乎有效:

configfile: "config.yaml"

rule final: 
    input: expand("{sample}.txt", sample=config["samples"])

rule rule1:
    output:
        "{sample}.txt"
    params:
        tag = lambda wildcards: config[wildcards.sample]["tag"]
    shell:
        """
        touch {output}
        echo {params.tag} > {output}
        """