小能豆

snakemake 使用通配符将文件分组到一起

py

我有一个 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}/fastq 中的文件)合并到 {sample}/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}
        """

阅读 20

收藏
2024-12-29

共1个答案

小能豆

在您的规则中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}
        """
2024-12-29