我正在关注其文档页面上的 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}"
我真的很困惑输入函数什么时候有意义,什么时候没有意义?