Snakemake:如何在 config.yml 文件中保存和访问示例详细信息?
Snakemake: How to save and access sample details in config.yml file?
当样本名称未写入 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
变量中。到目前为止没问题。然后,我想将示例标签打印到该文件中。正如上面所写的代码,运行ning snakemake
将失败,因为 config["{sample}"]
将逐字查找配置文件中不存在的 {sample}
变量,因为我需要将其替换为规则为 运行 的当前样本,例如sample1
.
有人知道这是否可以做到吗?如果可以,我该怎么做?
理想情况下,我想进一步压缩信息(见下文),但这还需要进一步的努力。
samples:
sample1:
tag: "S1"
sample2:
tag: "S2"
sample3:
tag: "S3"
sample4:
tag: "S4"
我建议使用制表符分隔的文件来存储样本信息。
sample.tab:
Sample Tag
1 S1
2 S2
您可以将此文件的路径存储在配置文件中,并在您的 Snakefile 中读取它。
config.yaml:
sample_file: "sample.tab"
蛇文件:
configfile: "config.yaml"
sample_file = config["sample_file"]
samples = read_table(sample_file)['Sample']
tags = read_table(sample_file)['Tag']
这样您就可以对任意数量的样本和任意数量的列重复使用您的工作流程。
除此之外,在 Snakemake 中通常你可以通过将大括号加倍来转义大括号,也许你可以试试。
祝你好运!
在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}
"""
当样本名称未写入 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
变量中。到目前为止没问题。然后,我想将示例标签打印到该文件中。正如上面所写的代码,运行ning snakemake
将失败,因为 config["{sample}"]
将逐字查找配置文件中不存在的 {sample}
变量,因为我需要将其替换为规则为 运行 的当前样本,例如sample1
.
有人知道这是否可以做到吗?如果可以,我该怎么做?
理想情况下,我想进一步压缩信息(见下文),但这还需要进一步的努力。
samples:
sample1:
tag: "S1"
sample2:
tag: "S2"
sample3:
tag: "S3"
sample4:
tag: "S4"
我建议使用制表符分隔的文件来存储样本信息。
sample.tab:
Sample Tag
1 S1
2 S2
您可以将此文件的路径存储在配置文件中,并在您的 Snakefile 中读取它。
config.yaml:
sample_file: "sample.tab"
蛇文件:
configfile: "config.yaml"
sample_file = config["sample_file"]
samples = read_table(sample_file)['Sample']
tags = read_table(sample_file)['Tag']
这样您就可以对任意数量的样本和任意数量的列重复使用您的工作流程。
除此之外,在 Snakemake 中通常你可以通过将大括号加倍来转义大括号,也许你可以试试。
祝你好运!
在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}
"""