首页 > 解决方案 > 将大型管道转换为蛇形

问题描述

我开发了 MOSCA,这是一个元组学分析管道(MG 与 MT),可通过 Bioconda 获得。我想将它转换为snakemake,因为它可以很容易地允许MOSCA同时运行一些通过API的访问和一些计算要求高的任务。此外,我认为这将有助于更好地将工具塑造成标准格式。

我的问题是,MOSCA 有很多参数,必须将它们转移到配置文件中。虽然这对于大多数参数来说都是微不足道的,但将 MG 和 MT 文件一起输入则比较棘手。此外,MOSCA 将样本一起考虑。所以我创建了一个样本文件,samples.tsv

MG files    MT files    Sample
path/to/mg_R1.fastq,path/to/mg_R2.fastq path/to/mt_R1.fastq,path/to/mt_R2.fastq Sample

我希望 Snakefile 读取它并保留“样本”信息。遵循包含 MG 预处理和流水线组装的示例,其中preprocess.pyassembly.py脚本包含相应的功能。这是Snakefile

from pandas import read_table

configfile: "config.json"

rule all:
  input:
    expand("{output}/Assembly/{experiment[1][Sample]}/contigs.fasta", 
            output = config["output"], experiment = read_table(config["experiments"], "\t").iterrows())

rule mg_preprocess:             # mt_preprocess make same, but data = mrna?
  input:
    list(expand({experiment}[1]["MG files"], experiment = read_table(config["experiments"], "\t").iterrows()))[0]
  output:
    expand("{output}/Preprocess/Trimmomatic/quality_trimmed_{name}{fr}_paired.fq", 
            fr = ['forward', 'reverse'], output = config["output"], name = 'pretty_commune')
  threads: 
    config["threads"]
  run:
    shell("""python preprocess.py -i {reads} -t {threads} -o {output} 
            -adaptdir MOSCA/Databases/illumina_adapters -rrnadbs MOSCA/Databases/rRNA_databases""", 
            output = config["output"])

rule assembly:
  input:
    expand("{output}/Preprocess/Trimmomatic/quality_trimmed_{name}{fr}_paired.fq", 
            fr = ['forward', 'reverse'], output = config["output"], name = 'pretty_commune')
  output:
    expand("{output}/Assembly/{experiment[1][Sample]}/contigs.fasta", 
            output = config["output"], experiment = read_table(config["experiments"], "\t").iterrows())
  threads:
    config["threads"]
  run:
    reads = ",".join(map(str, input))
    shell("python assembly.py -r {input} -t {threads} -o {output}/Assembly/{sample} -a {assembler}", 
    output = config["output"], sample = config["experiments"][1]["sample"], 
    assembler = config["assembler"])

这是config.json

{
"output": 
    "snakemake_learn",
"threads":
    14,
"experiments":
    "samples.tsv",
"assembler":
    "metaspades"
    }

我得到了错误

NameError in line 12 of /path/to/Snakefile:
name 'experiment' is not defined
  File "/path/to/Snakefile", line 12, in <module>

实验怎么没有定义?

标签: pythonconfigsnakemake

解决方案


我不确定你想做什么,但对我来说这条线

list(expand({experiment}[1]["MG files"], experiment = read_table(config["experiments"], "\t").iterrows()))[0]

似乎太复杂了。我会将样本表放在 DataFrame 中并使用它而不是read_table每次都使用迭代器。例如:

import pandas

ss = pandas.read_csv(config["experiments"], sep= '\t')

...

rule mg_preprocess:             # mt_preprocess make same, but data = mrna?
    input:
        expand('{experiment}', experiment= ss["MG files"][0]),

无论如何,我认为错误name 'experiment' is not defined是因为{experiment}inlist(expand({experiment}[1]["MG files"]不在可以替换为实际值的字符串中。


编辑:

事实上这条线expand('{experiment}', experiment= ss["MG files"][0]),没有多大意义......它只是一样ss["MG files"][0]


推荐阅读