【发布时间】:2018-11-15 22:26:26
【问题描述】:
这个问题来自我之前问过的question,它涉及理解如何使用 Snakemake 正确访问配置文件。我有一个需要解决的具体问题,我会先问这个问题,然后我会问一个理解索引如何工作的一般问题。
我正在使用 snakemake 运行从对齐/QC 到基序分析的 ATAC-seq 管道。
答:具体问题
我正在尝试添加一个名为 trim_galore_pe 的规则,以在对齐之前从我的 fastq 文件中修剪适配器,并且由于 trim galore 生成的输出文件的名称与预期的不匹配,snakemake 会抛出错误语句由蛇制造。这是因为我无法弄清楚如何在我的蛇形文件中正确编写输出文件语句以使名称匹配。
TRIM GALORE 生成的名称示例包含 SRA 编号,例如:
trimmed_fastq_files/SRR2920475_1_val_1.fq.gz
而 snakemake 预期的文件包含 sample 引用,应为:
trimmed_fastq_files/Corces2016_4983.7B_Mono_1_val_1.fq.gz
这也会影响trim_galore_pe 规则之后的后续规则。我需要找到一种方法来使用我的配置文件中的信息来生成所需的输出文件。
对于 Snakefile 中显示的规则之后的所有规则,我需要用 示例名称 命名文件,即 Corces2016_4983.7A_Mono。在下面的 Snakefile 中显示的所有 FAST_QC 和 MULTIQC 规则在输出文件名结构中具有 示例名称 也很有用,它们都已经在当前的 Snakefile 中这样做了。
但是,Bowtie2 的输入、FASTQC 规则以及trim_galore_pe 规则的输入和输出需要包含 SRA 编号。问题从trim_galore 的输出开始,并影响所有下游规则。
虽然我在之前的规则中提取了 SRA 编号,但我不确定在不使用配置文件中明确说明的 fastq_files 文件夹时如何执行此操作。通过引入trim_galore_pe 规则,我有效地将一组新的SRA 文件移动到新的trimmed_fastq_files 文件夹中。如何仅从包含旧文件夹名称的 SRA 文件配置文件列表中提取 SRA 编号,同时引用 Snakefile 中的新 trimmed_fastq_files 文件夹是我的问题的症结所在。
我希望这很清楚。
这是我的配置文件:
samples:
Corces2016_4983.7A_Mono: fastq_files/SRR2920475
Corces2016_4983.7B_Mono: fastq_files/SRR2920476
cell_types:
Mono:
- Corces2016_4983.7A
index: /home/genomes_and_index_files/hg19
这是我的蛇文件:
# read config info into this namespace
configfile: "config.yaml"
print (config['samples'])
rule all:
input:
expand("FastQC/PRETRIM/{sample}_{num}_fastqc.zip", sample=config["samples"], num=['1', '2']),
expand("bam_files/{sample}.bam", sample=config["samples"]),
"FastQC/PRETRIM/fastq_multiqc.html",
"FastQC/POSTTRIM/fastq_multiqc.html"
rule fastqc_pretrim:
input:
sample=lambda wildcards: f"{config['samples'][wildcards.sample]}_{wildcards.num}.fastq.gz"
output:
# Output needs to end in '_fastqc.html' for multiqc to work
html="FastQC/PRETRIM/{sample}_{num}_fastqc.html",
zip="FastQC/PRETRIM/{sample}_{num}_fastqc.zip"
wrapper:
"0.23.1/bio/fastqc"
rule multiqc_fastq_pretrim:
input:
expand("FastQC/PRETRIM/{sample}_{num}_fastqc.html", sample=config["samples"], num=['1', '2'])
output:
"FastQC/PRETRIM/fastq_multiqc.html"
wrapper:
"0.23.1/bio/multiqc"
rule trim_galore_pe:
input:
sample=lambda wildcards: expand(f"{config['samples'][wildcards.sample]}_{{num}}.fastq.gz", num=[1,2])
output:
"trimmed_fastq_files/{sample}_1_val_1.fq.gz",
"trimmed_fastq_files/{sample}_1.fastq.gz_trimming_report.txt",
"trimmed_fastq_files/{sample}_2_val_2.fq.gz",
"trimmed_fastq_files/{sample}_2.fastq.gz_trimming_report.txt"
params:
extra="--illumina -q 20"
log:
"logs/trim_galore/{sample}.log"
wrapper:
"0.23.1/bio/trim_galore/pe"
rule fastqc_posttrim:
input:
"trimmed_fastq_files/{sample}_1_val_1.fq.gz", "trimmed_fastq_files/{sample}_2_val_2.fq.gz"
output:
# Output needs to end in '_fastqc.html' for multiqc to work
html="FastQC/POSTTRIM/{sample}_{num}_fastqc.html",
zip="FastQC/POSTTRIM/{sample}_{num}_fastqc.zip"
wrapper:
"0.23.1/bio/fastqc"
rule multiqc_fastq_posttrim:
input:
expand("FastQC/POSTTRIM/{sample}_{num}.trim_fastqc.html", sample=config["samples"], num=['1', '2'])
output:
"FastQC/POSTTRIM/fastq_multiqc.html"
wrapper:
"0.23.1/bio/multiqc"
rule bowtie2:
input:
"trimmed_fastq_files/{sample}_1_val_1.fq.gz", "trimmed_fastq_files/{sample}_2_val_2.fq.gz"
output:
"bam_files/{sample}.bam"
log:
"logs/bowtie2/{sample}.txt"
params:
index=config["index"], # prefix of reference genome index (built with bowtie2-build),
extra=""
threads: 8
wrapper:
"0.23.1/bio/bowtie2/align"
目前正在运行,并使用snakemake -np 提供完整的作业列表,但会引发上述错误。
B:一般问题
是否有在线资源简洁地解释了如何使用 python 引用配置文件,尤其是关于 snakemake 的引用?在线文档是相当不足的,并且假设有 python 的先验知识。
我的编程经验主要是 bash 和 R,但我喜欢 Snakemake,并且大致了解字典和列表在 python 中的工作方式以及如何引用存储在其中的项目。然而,我发现在上面的一些 Snakemake 规则中,括号、通配符和引号的复杂使用令人困惑,因此在尝试引用配置文件中文件名的不同部分时往往会遇到困难。我想充分了解如何利用这些元素。
例如,在上面发布的 Snakefile 中这样的规则中:
sample=lambda wildcards: expand(f"{config['samples'][wildcards.sample]}_{{num}}.fastq.gz", num=[1,2])
这个命令实际上发生了什么?我的理解是我们正在使用config['samples'] 访问配置文件,并且我们正在使用[wildcards.sample] 部分显式访问配置文件的fastq_files/SRR2920475 部分。扩展允许我们遍历配置文件中适合命令参数的每个项目,即所有 SRA 文件,并且需要 lambda 通配符才能在命令中使用 wildcards 调用。我不确定的是:
-
f在展开之后会做什么?为什么需要它? - 为什么
config['samples']在方括号内包含引号,但[wildcards.sample]周围不需要引号? - 为什么要使用单双大括号?
- 查看上面的 Snakefile,其中一些规则包含将一系列数字分配给
num的部分,但这些数字有时会用引号括起来,有时不会……为什么?
任何建议、提示、指点将不胜感激。
C:澄清@bli 提出的以下建议
我已按照您在评论中的建议编辑了我的配置文件,并省略了文件夹名称,只留下了 SRA 编号。这对我来说很有意义,但我还有其他一些问题阻止我运行这个 Snakefile。
新配置文件:
samples:
Corces2016_4983.7A_Mono: SRR2920475
Corces2016_4983.7B_Mono: SRR2920476
cell_types:
Mono:
- Corces2016_4983.7A
index: /home/c1477909/genomes_and_index_files/hg19
新的蛇文件:
# read config info into this namespace
configfile: "config.yaml"
print (config['samples'])
rule all:
input:
expand("FastQC/PRETRIM/{sample}_{num}_fastqc.zip", sample=config["samples"], num=['1', '2']),
expand("bam_files/{sample}.bam", sample=config["samples"]),
"FastQC/PRETRIM/fastq_multiqc.html",
"FastQC/POSTTRIM/fastq_multiqc.html",
rule fastqc_pretrim:
input:
lambda wildcards: f"fastq_files/{config['samples'][wildcards.sample]}_{wildcards.num}.fastq.gz"
output:
# Output needs to end in '_fastqc.html' for multiqc to work
html="FastQC/PRETRIM/{sample}_{num}_fastqc.html",
zip="FastQC/PRETRIM/{sample}_{num}_fastqc.zip"
wrapper:
"0.23.1/bio/fastqc"
rule multiqc_fastq_pretrim:
input:
expand("FastQC/PRETRIM/{sample}_{num}_fastqc.html", sample=config["samples"], num=['1', '2'])
output:
"FastQC/PRETRIM/fastq_multiqc.html"
wrapper:
"0.23.1/bio/multiqc"
rule trim_galore_pe:
input:
lambda wildcards: expand(f"fastq_files/{config['samples'][wildcards.sample]}_{{num}}.fastq.gz", num=[1,2])
output:
"trimmed_fastq_files/{wildcards.sample}_1_val_1.fq.gz",
"trimmed_fastq_files/{wildcards.sample}_1.fastq.gz_trimming_report.txt",
"trimmed_fastq_files/{wildcards.sample}_2_val_2.fq.gz",
"trimmed_fastq_files/{wildcards.sample}_2.fastq.gz_trimming_report.txt"
params:
extra="--illumina -q 20"
log:
"logs/trim_galore/{sample}.log"
wrapper:
"0.23.1/bio/trim_galore/pe"
rule fastqc_posttrim:
input:
lambda wildcards: expand(f"trimmed_fastq_files/{config['samples'][wildcards.sample]}_{{num}}_val_{{num}}.fq.gz", num=[1,2])
output:
# Output needs to end in '_fastqc.html' for multiqc to work
html="FastQC/POSTTRIM/{sample}_{num}_fastqc.html",
zip="FastQC/POSTTRIM/{sample}_{num}_fastqc.zip"
wrapper:
"0.23.1/bio/fastqc"
rule multiqc_fastq_posttrim:
input:
expand("FastQC/POSTTRIM/{sample}_{num}.trim_fastqc.html", sample=config["samples"], num=['1', '2'])
output:
"FastQC/POSTTRIM/fastq_multiqc.html"
wrapper:
"0.23.1/bio/multiqc"
rule bowtie2:
input:
lambda wildcards: expand(f"trimmed_fastq_files/{config['samples'][wildcards.sample]}_{{num}}_val_{{num}}.fq.gz", num=[1,2])
output:
"bam_files/{sample}.bam"
log:
"logs/bowtie2/{sample}.txt"
params:
index=config["index"], # prefix of reference genome index (built with bowtie2-build),
extra=""
threads: 8
wrapper:
"0.23.1/bio/bowtie2/align"
使用这些新文件最初一切正常,snakemake -np 创建了部分作业列表。但是,这是因为已经运行了一半的完整作业列表;也就是说,trimmed_fastq_files 文件夹已生成,并且正确命名的修剪过的 fastq 文件已在其中到位。当我删除所有之前创建的文件以查看整个新版本的 Snakefile 是否可以正常工作时,snakemake -np 失败,指出trim_galore_pe 规则下游的规则缺少输入文件。
如您所见,我试图在输出部分的trim_galore_pe 规则的输入部分调用{wildcard.sample} 变量集,但snakemake 不喜欢这样。可以这样做吗?
我也尝试过使用以下答案中的提示,但这也不起作用:
rule trim_galore_pe:
input:
sample=lambda wildcards: expand(f"fastq_files/{config['samples'][wildcards.sample]}_{{num}}.fastq.gz", num=[1,2])
output:
expand(f"trimmed_fastq_files/{config['samples'][wildcards.sample]}_{{num}}_val_{{num}}.fq.gz", num=[1,2]),
expand(f"trimmed_fastq_files/{config['samples'][wildcards.sample]}_{{num}}.fastq.gz_trimming_report.txt", num=[1,2])
params:
extra="--illumina -q 20"
log:
"logs/trim_galore/{sample}.log"
wrapper:
"0.23.1/bio/trim_galore/pe"
错误然后指出wildcards not defined。因此,从逻辑上讲,我尝试将lambda wildcards: 放在输出部分的两个扩展语句前面以尝试定义通配符,但这引发了语法错误Only input files can be specified as functions。我还尝试使用下面的一些索引建议,但无法获得正确的组合。
这可能是由于我对 Snakefiles 不确定的另一件事引起的,这就是作用域的工作原理。
- 如果我在
rule all中定义了一个变量,所有其他规则都可以访问它吗? - 如果我在规则的输入部分定义了一个变量,它是否可用于该规则的所有其他部分(即输出、shell 命令等),但仅适用于该规则?
- 如果是,如果我在输入部分定义了
{wildcard.sample}变量,为什么不能访问它?那是因为该变量包含在“封闭”范围的 lambda 函数中吗?
任何(进一步的)建议将不胜感激。
【问题讨论】:
-
“如何从包含旧文件夹名称的 SRA 文件配置文件列表中仅提取 SRA 编号,同时引用 Snakefile 中的新 trimmed_fastq_files 文件夹是我的问题的症结所在。”也许您可以在配置中使用 SRA 编号,并在 snakemake 代码中构建文件列表,根据规则知道您应该在哪个目录中搜索?
-
谢谢 Bli,我对此有过尝试,但遇到了障碍,请参阅相关的 C 部分。
-
在
output部分,你不用{wildcards.sample},你直接用{sample}。 Snakemake 已经假设您正在谈论通配符属性。实际上,规则范围内存在的通配符属性是通过解析output部分来确定的。如果这可能有用,我尝试写一些关于我目前对该主题的理解的解释:bitbucket.org/blaiseli/snakemake/src/… -
我在答案末尾添加了更多解释。希望这可以帮助。至少它帮助我提高了对蛇形的理解。
-
非常感谢 Bli - 这些答案极大地帮助了我理解 Snakemake。
标签: indexing config wildcard curly-braces snakemake