snakemake组通过通配符一起归档

时间:2018-02-14 11:33:00

标签: python wildcard snakemake

我有一个snakemake文件,其中包含连接样本表中列出的文件的规则。样本表看起来像:

sample  unit    fq1 fq2
A   lane1   A.l1.1.R1.txt   A.l1.1.R2.txt
A   lane1   A.l1.2.R1.txt   A.l1.2.R2.txt
A   lane2   A.l2.R1.txt A.l2.R2.txt
B   lane1   B.l1.R1.txt B.l1.R2.txt
B   lane2   B.l2.R1.txt B.l2.R2.txt

我的目标是合并来自同一样本和同一单位的fq1文件,并将它们放在{sample} / fastq /中,并合并来自{sample}中样本的结果文件({sample} / fastq中的文件) / BAM /

适用于{sample} / fastq,但对于{sample} / bam {sample} / fastq中列出的所有文件,未检索的样本将在{sample} / bam中连接。有什么想法解决这个问题吗?

import pandas as pd
shell.executable("bash")

configfile: "config.yaml"

# open samplesheet
units = pd.read_table(config["units"], dtype=str)

# set df index
units=units.set_index(["sample","unit"])

rule all:
    input:
        expand("{sample}/bam/{sample}_bam.txt",
            sample=units.index.get_level_values('sample').unique().values),

# functions to return information in the samplesheet
def get_fastq_r1(wildcards):
    return units.loc[(wildcards.sample, wildcards.unit), ["fq1"]].dropna().values.flatten()

def get_fastq_r2(wildcards):
    return units.loc[(wildcards.sample, wildcards.unit), ["fq2"]].dropna().values.flatten()

# merge files from the same sample and unit
rule merge_fastq_lane:
    input:
        r1 = get_fastq_r1,
        r2 = get_fastq_r2
    output:
        r1_o = "{sample}/fastq/{sample}_{unit}_merge_R1.fastq",
        r2_o = "{sample}/fastq/{sample}_{unit}_merge_R2.fastq"
    message:
        "Merge fastq from the same sample and lane"
    shell:
        """
        cat {input.r1} > {output.r1_o}
        cat {input.r2} > {output.r2_o}
        """

# merge files from the same sample
rule align_lane:
    input:
        r1 = expand("{sample}/fastq/{sample}_{unit}_merge_R1.fastq",
            unit=units.index.get_level_values('unit').unique().values,
            sample=units.index.get_level_values('sample').unique().values),
        r2 = expand("{sample}/fastq/{sample}_{unit}_merge_R2.fastq",
            unit=units.index.get_level_values('unit').unique().values,
            sample=units.index.get_level_values('sample').unique().values)
    output:
        bam = "{sample}/bam/{sample}_bam.txt"
    message: 
        "Align lane with bwa mem"
    shell:
        """
        cat {input.r1} {input.r2} > {output.bam}
        """

1 个答案:

答案 0 :(得分:0)

在您的规则align_lane中,您的输入会列出所有可能的样本。由于您在输出中使用了sample通配符,我想您想在输入中使用它。在扩展函数中使用通配符的方法是将括号加倍。所以我猜你的规则应该是这样的(如果我理解正确的话):

rule align_lane:
    input:
        r1 = expand("{{sample}}/fastq/{{sample}}_{unit}_merge_R1.fastq",
            unit=units.index.get_level_values('unit').unique().values),
        r2 = expand("{{sample}}/fastq/{{sample}}_{unit}_merge_R2.fastq",
            unit=units.index.get_level_values('unit').unique().values)
    output:
        bam = "{sample}/bam/{sample}_bam.txt"
    message: 
        "Align lane with bwa mem"
    shell:
        """
        cat {input.r1} {input.r2} > {output.bam}
        """