根据元表中的列有条件地执行 snakemake 规则
conditional execution of snakemake rules based on column in metatable
我正在尝试使用文本文件中的列有条件地执行 snakemake 工作流中的规则。
文本文件如下:
id end sample_name fq1 fq2
a paired test_paired resources/SRR1945436_1.fastq.gz resources/SRR1945436_2.fastq.gz
b single test_single resources/SRR1945436.fastq.gz NA
对于文本文件中的每个样本,如果结束列中的值是成对的,我想使用规则 cp_fastq_pe,如果结束是单一的,那么我想使用规则 cp_fastq_pe 来处理分别是 fq1 和 fq2 或仅 fq1 文件。
Snakefile相关部分如下:
import pandas as pd
samples = pd.read_table("config/samples.tsv").set_index("id", drop=False)
all_ids=list(samples["id"])
rule cp_fastq_pe:
"""
copy file to resources
"""
input:
fq1=lambda wildcards: samples.loc[wildcards.id, "fq1"],
fq2=lambda wildcards: samples.loc[wildcards.id, "fq2"]
output:
"resources/fq/{id}_1.fq.gz",
"resources/fq/{id}_2.fq.gz"
shell:
"""
cp {input.fq1} {output[0]}
cp {input.fq2} {output[1]}
"""
rule cp_fastq_se:
"""
copy file to resources
"""
input:
fq1=lambda wildcards: samples.loc[wildcards.id, "fq1"]
output:
"resources/fq/{id}.fq.gz",
shell:
"""
cp {input.fq1} {output}
"""
可以这样做吗?
我有一个类似的问题,我在这里解决了:How to make Snakemake input optional but not empty?
这是根据您的问题调整的想法。首先,您需要指定 ruleorder
来解决歧义(否则只要可以配对,就可以始终应用单个):
ruleorder: cp_fastq_pe > cp_fastq_se
接下来,在您的 cp_fastq_pe
规则中,您需要定义一个函数,该函数 returns 一个有效文件(对于成对的情况)或 returns 一个不存在文件的占位符:
rule cp_fastq_pe:
input:
fq1=lambda wildcards: samples.loc[wildcards.id, "fq1"],
fq2=lambda wildcards: samples.loc[wildcards.id, "fq2"] if "fq2" in samples else "non-existing-filename"
此规则将应用于存在 "fq2"
字段并表示有效文件的所有样本。其他规则将被选择用于其余样本。
我正在尝试使用文本文件中的列有条件地执行 snakemake 工作流中的规则。
文本文件如下:
id end sample_name fq1 fq2
a paired test_paired resources/SRR1945436_1.fastq.gz resources/SRR1945436_2.fastq.gz
b single test_single resources/SRR1945436.fastq.gz NA
对于文本文件中的每个样本,如果结束列中的值是成对的,我想使用规则 cp_fastq_pe,如果结束是单一的,那么我想使用规则 cp_fastq_pe 来处理分别是 fq1 和 fq2 或仅 fq1 文件。
Snakefile相关部分如下:
import pandas as pd
samples = pd.read_table("config/samples.tsv").set_index("id", drop=False)
all_ids=list(samples["id"])
rule cp_fastq_pe:
"""
copy file to resources
"""
input:
fq1=lambda wildcards: samples.loc[wildcards.id, "fq1"],
fq2=lambda wildcards: samples.loc[wildcards.id, "fq2"]
output:
"resources/fq/{id}_1.fq.gz",
"resources/fq/{id}_2.fq.gz"
shell:
"""
cp {input.fq1} {output[0]}
cp {input.fq2} {output[1]}
"""
rule cp_fastq_se:
"""
copy file to resources
"""
input:
fq1=lambda wildcards: samples.loc[wildcards.id, "fq1"]
output:
"resources/fq/{id}.fq.gz",
shell:
"""
cp {input.fq1} {output}
"""
可以这样做吗?
我有一个类似的问题,我在这里解决了:How to make Snakemake input optional but not empty?
这是根据您的问题调整的想法。首先,您需要指定 ruleorder
来解决歧义(否则只要可以配对,就可以始终应用单个):
ruleorder: cp_fastq_pe > cp_fastq_se
接下来,在您的 cp_fastq_pe
规则中,您需要定义一个函数,该函数 returns 一个有效文件(对于成对的情况)或 returns 一个不存在文件的占位符:
rule cp_fastq_pe:
input:
fq1=lambda wildcards: samples.loc[wildcards.id, "fq1"],
fq2=lambda wildcards: samples.loc[wildcards.id, "fq2"] if "fq2" in samples else "non-existing-filename"
此规则将应用于存在 "fq2"
字段并表示有效文件的所有样本。其他规则将被选择用于其余样本。