【问题标题】:Run snakemake rules iteratively迭代运行snakemake规则
【发布时间】:2020-10-19 14:52:22
【问题描述】:

所以我以为我终于掌握了snakemake,但是当我尝试运行几个不同的数据文件时,我意识到它并不能像我一样工作。这是蛇文件:

import pandas as pd

configfile: "config.json"
experiments = pd.read_csv(config["experiments"], sep = '\t')
experiments['Name'] = [filename.split('/')[-1].split('.fa')[0] for filename in experiments['Files']]

rule all:
    input:
        expand("{output}/Preprocess/Trimmomatic/quality_trimmed_{name}{fr}.fq", output = config["output"],
            fr = (['_forward_paired', '_reverse_paired'] if experiments["Files"].str.contains(',').tolist() else ''),
               name = experiments['Name'])

rule preprocess:
    input:
        experiments["Files"].str.split(',')
    output:
        expand("{output}/Preprocess/Trimmomatic/quality_trimmed_{name}{fr}.fq", output = config["output"],
            fr = (['_forward_paired', '_reverse_paired'] if experiments["Files"].str.contains(',').tolist() else ''),
               name = experiments['Name'])
    threads:
        config["threads"]
    run:
        shell("python preprocess.py -i {reads} -t {threads} -o {output} -adaptdir MOSCA/Databases/illumina_adapters -rrnadbs MOSCA/Databases/rRNA_databases -d {data_type}",
            output = config["output"], data_type = experiments["Data type"].tolist(), reads = ",".join(input))

这是配置文件:

{
  "output": "test_snakemake",
  "threads": 14,
  "experiments": "experiments.tsv"
}

这是实验文件

Files   Sample  Data type   Condition
path/to/mg_R1.fastq,path/to/mg_R2.fastq Sample  dna
path/to/a/0.01/mt_0.01a_R1.fastq,path/to/a/0.01/mt_0.01a_R2.fastq   Sample  rna c1
path/to/b/0.01/mt_0.01b_R1.fastq,path/to/b/0.01/mt_0.01b_R2.fastq   Sample  rna c1
path/to/c/0.01/mt_0.01c_R1.fastq,path/to/c/0.01/mt_0.01c_R2.fastq   Sample  rna c1
path/to/a/1/mt_1a_R1.fastq,path/to/a/1/mt_1a_R2.fastq   Sample  rna c2
path/to/b/1/mt_1b_R1.fastq,path/to/b/1/mt_1b_R2.fastq   Sample  rna c2
path/to/c/1/mt_1c_R1.fastq,path/to/c/1/mt_1c_R2.fastq   Sample  rna c2
path/to/a/100/mt_100a_R1.fastq,path/to/a/100/mt_100a_R2.fastq   Sample  rna c3
path/to/b/100/mt_100b_R1.fastq,path/to/b/100/mt_100b_R2.fastq   Sample  rna c3
path/to/c/100/mt_100c_R1.fastq,path/to/c/100/mt_100c_R2.fastq   Sample  rna c3

我想要做的是让预处理规则分别处理每一行。我认为这是 shell 解释命令的方式,它会运行命令 python preprocess.py -i path/to/mg_R1.fastq,path/to/mg_R2.fastq -t 14 -o test_snakemake -adaptdir MOSCA/Databases/illumina_adapters -rrnadbs MOSCA/Databases/rRNA_databases -d dna,而不是尝试加入所有行并同时对所有样本运行 python preprocess.py -i path/to/mg_R1.fastq,path/to/mg_R2.fastq,path/to/a/0.01/mt_0.01a_R1.fastq,path/to/a/0.01/mt_0.01a_R2.fastq,path/to/b/0.01/mt_0.01b_R1.fastq,path/to/b/0.01/mt_0.01b_R2.fastq,... -t 14 -o test_snakemake -adaptdir MOSCA/Databases/illumina_adapters -rrnadbs MOSCA/Databases/rRNA_databases -d dna rna rna rna rna rna rna rna rna rna。

如何让snakemake 分别考虑每一行?

【问题讨论】:

    标签: python config sample snakemake


    【解决方案1】:

    这是一个非常常见的错误。要记住的是,规则应该适用于单个样本。 Snakemake 将采用您的路径(使用通配符)并根据规则生成特定的工作。您已经编写了包含所有输入和所有输出的内容,那么我认为 preprocess.py 需要一个输入/输出。

    相反,一次考虑一个文件。对于输出"{output}/Preprocess/Trimmomatic/quality_trimmed_{name}{fr}.fq",您如何生成该文件?您必须使用名称作为键来匹配实验数据框中的输入文件。

    def preprocess_input(wildcards):
        # get files with matching names
        df = experiments.loc[experiments['Name'] == wildcards.name, 'Files']
        # get first value (in case multiple) and split on commas
        return df.iloc[0].split(',')
    
    rule preprocess:
        input:
            preprocess_input
        output:
            "{output}/Preprocess/Trimmomatic/quality_trimmed_{name}{fr}.fq"
        threads:
            config["threads"]
        shell:
            'python preprocess.py -i {reads} -t {threads} -o {config[output]} ...'
    

    使用输入函数从输出文件中找到正确的输入文件。它并不完美,但应该能让你朝着正确的方向前进。

    【讨论】:

    • 非常感谢!使用您的解决方案的一个版本已经让它工作了!我可能现在才开始理解通配符...
    猜你喜欢
    • 2021-02-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-05
    • 2023-02-01
    • 2021-12-10
    • 1970-01-01
    相关资源
    最近更新 更多