【问题标题】:snakemake group files together by wildcardsnakemake 通过通配符将文件组合在一起
【发布时间】:2018-02-14 11:32:59
【问题描述】:

我有一个 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}
        """

【问题讨论】:

    标签: python wildcard snakemake


    【解决方案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}
            """
    

    【讨论】:

      猜你喜欢
      • 2021-12-03
      • 1970-01-01
      • 2022-12-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多