Python snakemake中的通配符问题

Python snakemake中的通配符问题,python,pandas,wildcard,sample,snakemake,Python,Pandas,Wildcard,Sample,Snakemake,我遇到了通配符无法转换为假定值的问题。这是蛇形档案: import pandas as pd configfile: "config.json" experiments = pd.read_csv(config["experiments"], sep = '\t') experiments['Name'] = [filename.split('/')[-1].split('_R' if ',' in filename else '.fa')[0] for

我遇到了通配符无法转换为假定值的问题。这是蛇形档案:

import pandas as pd

configfile: "config.json"
experiments = pd.read_csv(config["experiments"], sep = '\t')
experiments['Name'] = [filename.split('/')[-1].split('_R' if ',' in filename else '.fa')[0] for filename in experiments['Files']]
name2sample = {experiments.iloc[i]['Name'] : experiments.iloc[i]['Sample'] for i in range(len(experiments))}
mg_experiments = experiments[experiments["Data type"] == 'dna']

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(',')

def join_reads_input(wildcards):
    df = mg_experiments.loc[mg_experiments['Sample'] == wildcards.sample, 'Files']
    names = [filename.split('/')[-1].split('_R' if ',' in filename else '.fa')[0] for filename in df]
    return ['{}/Preprocess/Trimmomatic/quality_trimmed_{}{}.fq'.format(config["output"], name, fr) for name in names
        for files in df for fr in (['_forward_paired', '_reverse_paired'] if ',' in files else [''])]

rule all:
    input:
        expand("{output}/Annotation/uniprotinfo.tsv", output = config["output"], sample = experiments["Sample"]),
        expand("{output}/Annotation/{sample}/protein2cog.tsv", output = config["output"], sample = experiments["Sample"]),
        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:
        preprocess_input
    output:
        expand("{{output}}/Preprocess/Trimmomatic/quality_trimmed_{{name}}{fr}.fq",
            fr = (['_forward_paired', '_reverse_paired'] if experiments["Files"].str.contains(',').tolist() else ''))
    threads:
        config["threads"]
    run:
        shell("python preprocess.py -i {reads} -t {threads} -o {output}/Preprocess -adaptdir MOSCA/Databases/illumina_adapters -rrnadbs MOSCA/Databases/rRNA_databases -d {data_type}",
            output = config["output"], data_type = experiments.loc[experiments['Name'] == wildcards.name]["Data type"].iloc[0], reads = ",".join(input))

rule join_reads:
    input:
        join_reads_input
    output:
        expand("{output}/Assembly/{{sample}}/{{sample}}{fr}.fastq", output = config["output"],
            fr = (['_forward', '_reverse'] if experiments["Files"].str.contains(',').tolist() else ''))
    run:
        for file in input:
            print(file)
            if 'forward' in file:
                shell("touch {output}/Assembly/{wildcards.sample}/{wildcards.sample}_forward.fastq; cat {file} >> {output}/Assembly/{wildcards.sample}/{wildcards.sample}_forward.fastq", output = config["output"])
            elif 'reverse' in file:
                shell("touch {output}/Assembly/{wildcards.sample}/{wildcards.sample}_reverse.fastq; cat {file} >> {output}/Assembly/{wildcards.sample}/{wildcards.sample}_reverse.fastq", output = config["output"])
            else:
                shell("touch {output}/Assembly/{wildcards.sample}/{wildcards.sample}.fastq; cat {file} >> {output}/Assembly/{wildcards.sample}/{wildcards.sample}.fastq", output = config["output"])

rule assembly:
    input:
        expand("{output}/Assembly/{{sample}}/{{sample}}{fr}.fastq", output = config["output"],
            fr = (['_forward', '_reverse'] if experiments["Files"].str.contains(',').tolist() else ''))
    output:
        expand("{output}/Assembly/{{sample}}/contigs.fasta", output = config["output"])
    threads:
        config["threads"]
    run:
        reads = ",".join(input)
        shell("python assembly.py -r {reads} -t {threads} -o {output}/Assembly/{{sample}} -a {assembler}",
            output = config["output"], assembler = config["assembler"])
这可能会让人很困惑,因为我不知道<代码>规则预处理运行预处理脚本,
rule join_reads
cats将样本(在下面的
实验
文件中定义的
预处理/微调/质量
零件)获得的读数一起提交给程序集。这是配置文件:

{
  "output": "output",
  "threads": 14,
  "experiments": "experiments.tsv",
  "assembler": "metaspades"
}
这是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  mrna    c1
path/to/b/0.01/mt_0.01b_R1.fastq,path/to/b/0.01/mt_0.01b_R2.fastq   Sample  mrna    c1
path/to/c/0.01/mt_0.01c_R1.fastq,path/to/c/0.01/mt_0.01c_R2.fastq   Sample  mrna    c1
path/to/a/1/mt_1a_R1.fastq,path/to/a/1/mt_1a_R2.fastq   Sample  mrna    c2
path/to/b/1/mt_1b_R1.fastq,path/to/b/1/mt_1b_R2.fastq   Sample  mrna    c2
path/to/c/1/mt_1c_R1.fastq,path/to/c/1/mt_1c_R2.fastq   Sample  mrna    c2
path/to/a/100/mt_100a_R1.fastq,path/to/a/100/mt_100a_R2.fastq   Sample  mrna    c3
path/to/b/100/mt_100b_R1.fastq,path/to/b/100/mt_100b_R2.fastq   Sample  mrna    c3
path/to/c/100/mt_100c_R1.fastq,path/to/c/100/mt_100c_R2.fastq   Sample  mrna    c3
这里的问题是:cat报告一个
MissingOutputException
,因为它找不到文件
output/Assembly/{wildcards.sample}\u forward.fastq
(反之亦然)。这意味着通配符.sample没有转换为“sample”,我不明白为什么。但是,cat规则仍然能够正确生成文件,尽管它会停止工作流,而工作流必须再次执行。从这里开始,它运行良好,因为汇编规则已经有了它的输入文件


为什么wildcards.sample没有转换成“sample”?

这里有很多。我认为对于您的特定问题,当您使用关键字参数来shell时,它会阻止snakemake格式化剩余的通配符。将
{output}/Assembly/{wildcards.sample}/{wildcards.sample}\u reverse.fastq
更改为
{output}/Assembly/{sample}/{sample}\u reverse.fastq
并将sample作为参数传递给shell

其他建议:

  • 将输入函数置于应用规则之上
  • 在输入/输出中使用多个规则,而不是复杂的扩展。对于同一个输出文件,可以有两个规则,一个接受成对输入,另一个接受不成对输入
  • 如果您有一个仅调用shell的run指令,请将其替换为shell。您可以捕获
    reads=','.join(input)
    逻辑到params指令中。您可以直接将config[assembler]放入shell格式标记中,例如,
    shell:python assembly.py-一个{config[assembler]}
  • 在展开中使用
    allow_missing
    ,而不是使用
    {{}
    转义通配符格式
  • cat{file}>{output}
    将文件附加到输出,即使输出不存在(不需要触摸)
  • 尽量使您的行少于100个字符,以便它们在stackoverflow或github上正确显示

我认为您可以对逻辑进行大量简化,但我对您的工具了解不够,无法推荐更多细节。

使用
touch{file}的原因是什么;猫…>>{file}
?为什么不删除此
触摸按钮
?第一只猫将找不到该文件。这解决了问题,并允许始终追加!为什么不使用
cat file1 file2 file3>{output}
?此外,蛇人通过
'forward'
'reverse'
分离文件的惯用方法是使用单独的规则。但是文件可能是不成对的。我试图对应这两种情况:/每当文件
/Assembly/sample\u向前时,它应该用
“sample”
替换
{sample}
通配符。需要fastq
来实现目标:直接或间接。