การดำเนินการตามเงื่อนไขของกฎ Snakemake ตามคอลัมน์ใน 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 และถ้า end เป็นแบบเดี่ยวฉันต้องการใช้กฎ 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}
"""
เป็นไปได้ไหมที่จะทำเช่นนี้?
คำตอบ
ฉันมีปัญหาที่คล้ายกันซึ่งฉันแก้ไขได้ที่นี่: วิธีทำให้อินพุต Snakemake เป็นทางเลือก แต่ไม่ว่างเปล่า?
นี่คือแนวคิดที่ปรับให้เข้ากับปัญหาของคุณ ขั้นแรกคุณต้องระบุruleorder
เพื่อแก้ไขความคลุมเครือ (มิฉะนั้นจะสามารถใช้ซิงเกิ้ลได้ทุกเมื่อที่จับคู่ได้):
ruleorder: cp_fastq_pe > cp_fastq_se
ถัดไปในcp_fastq_pe
กฎของคุณคุณต้องกำหนดฟังก์ชันที่ส่งคืนไฟล์ที่ถูกต้อง (สำหรับกรณีที่จับคู่) หรือส่งคืนตัวยึดสำหรับไฟล์ที่ไม่มีอยู่:
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"
มีฟิลด์และแสดงไฟล์ที่ถูกต้อง กฎอื่น ๆ จะถูกเลือกให้กับกลุ่มตัวอย่างที่เหลือ