如何快速识别 Snakemake 中的规则是否需要输入函数

2024-02-27

我正在关注其文档页面上的 Snakemake 教程,并且确实陷入了输入函数的概念https://snakemake.readthedocs.io/en/stable/tutorial/advanced.html#step-3-input-functions https://snakemake.readthedocs.io/en/stable/tutorial/advanced.html#step-3-input-functions

基本上他们定义了一个config.yaml如下:

samples:
  A: data/samples/A.fastq
  B: data/samples/B.fastq

and the Snakefile没有任何输入功能时如下:

configfile: "config.yaml"

rule all:
    input:
        "plots/quals.svg"

rule bwa_map:
    input:
        "data/genome.fa",
        "data/samples/{sample}.fastq"
    output:
        "mapped_reads/{sample}.bam"
    threads: 12
    shell:
        "bwa mem -t {threads} {input} | samtools view -Sb - > {output}"

rule samtools_sort:
    input:
        "mapped_reads/{sample}.bam"
    output:
        "sorted_reads/{sample}.bam"
    shell:
        "samtools sort -T sorted_reads/{wildcards.sample} -O bam {input} > {output}"

rule samtools_index:
    input:
        "sorted_reads/{sample}.bam"
    output:
        "sorted_reads/{sample}.bam.bai"
    shell:
        "samtools index {input}"

rule bcftools_call:
    input:
        fa = "data/genome.fa",
        bam = expand("sorted_reads/{sample}.bam",sample=config['samples']),
        bai = expand("sorted_reads/{sample}.bam.bai",sample=config['samples'])
    output:
        "calls/all.vcf"
    shell:
        "bcftools mpileup -f {input.fa} {input.bam} | "
        "bcftools call -mv - > {output}"

rule plot_quals:
    input:
        "calls/all.vcf"
    output:
        "plots/quals.svg"
    script:
        "scripts/plot-quals.py"

在教程中,他们提到这种扩展发生在初始化步骤中:

bam = expand("sorted_reads/{sample}.bam",sample=config['samples']),
bai = expand("sorted_reads/{sample}.bam.bai",sample=config['samples'])

并且无法确定规则的 FASTQ 路径bwa_map在这个阶段。但是,如果我们按原样运行,代码就可以工作,为什么呢?

然后他们建议使用输入函数来推迟bwa_map进入下一阶段(DAG阶段)如下:

def get_bwa_map_input_fastqs(wildcards):
    return config["samples"][wildcards.sample]

rule bwa_map:
    input:
        "data/genome.fa",
        get_bwa_map_input_fastqs
    output:
        "mapped_reads/{sample}.bam"
    threads: 8
    shell:
        "bwa mem -t {threads} {input} | samtools view -Sb - > {output}"

我真的很困惑输入函数什么时候有意义,什么时候没有意义?


SultanOrazbayev 已经有了一个很好的答案。这是另一个典型的例子。

通常,输入和输出文件共享相同的模式(通配符)。例如,如果你想对文件进行排序,你可以这样做:input: {name}.txt -> output: {name}.sorted.txt.

然而,有时输入文件不会通过简单的模式链接到输出。生物信息学的一个例子是将读数与基因组对齐的规则:

rule align:
    input:
        reads= '{name}.fastq',
        genome= 'human_genome.fa',
    output:
        bam= '{name}.bam',
    shell: ...

这里基因组文件的名称与输入读取文件的名称和输出 bam 文件的名称无关。上述规则之所以有效,是因为参考基因组是不带通配符的具体文件名。

但是:如果参考基因组的选择取决于输入的 fastq 文件怎么办?对于相同的输入读取,您可能需要小鼠基因组,而对于其他输入读取,您可能需要人类基因组。输入功能很方便:

def get_genome(wildcards):
    if wildcards.name in ['bob', 'alice']:
        return 'human_genome.fa',
    if wildcards.name in ['mickey', 'jerry'],
        return 'mouse_genome.fa',

rule align:
    input:
        reads= '{name}.fastq',
        genome= get_genome,
    output:
        bam= '{name}.bam',
    shell: ...

现在参考基因组是小鼠或人类,具体取决于输入读数。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何快速识别 Snakemake 中的规则是否需要输入函数 的相关文章

随机推荐