蛇行检查点和通配符

2024-10-04 05:24:19 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个snakemake工作流,它失败了,因为最后一个作业创建了两个输出文件,或者没有。我尝试用checkpoint来解决这个问题,但我认为当我试图在聚合函数中整理outfiles时,通配符会被卡住

工作流(1)从biom社区档案创建fasta文件。然后在fasta文件上运行in-silico PCR(2),创建一个txt文件作为输出

最后一步是解析器(3),它输出csv和fasta文件。但是,如果txt文件中没有匹配项(即insilico PCR未产生任何结果),则不会创建csv或fasta文件

SAMPLES, = glob_wildcards("input/metaphlan/{sample}.biom")
ID = "0 1 2 3 4".split()

TARGETS = expand("output/metaphlan/isPCR/final/{id}_mismatch_{sample}.fasta", sample = SAMPLES, id = ID)

rule all:
    input:
        TARGETS

rule getgenome:
    input:
        "input/metaphlan/{sample}.biom"
    output:
        csv="output/metaphlan/fasta_dump/{sample}.csv",
        fas="output/metaphlan/fasta_dump/{sample}_dump.fasta"
    conda:
        "envs/synth_genome.yaml"
    shell:
        "python scripts/get_genomes_noabund_Snakemake.py {input} 1 {output.fas} {output.csv}"

rule PCR:
    input:
        "output/metaphlan/fasta_dump/{sample}_dump.fasta"
    output:
        "output/metaphlan/isPCR/raw/{id}_mismatch/{sample}.txt"
    params:
        id = "{id}"
    shell:
        "software/exonerate-2.2.0-x86_64/bin/ipcress --products --mismatch {params.id} scripts/primers-miseq.txt {input} > {output}"

rule parse:
    input:
        "output/metaphlan/isPCR/raw/{id}_mismatch/{sample}.txt"
    output:
        "output/metaphlan/isPCR/final/{id}_mismatch_{sample}.csv",
        "output/metaphlan/isPCR/final/{id}_mismatch_{sample}.fasta"
    shell:
        "python scripts/iPCRess_parser_v2.py {input} {output}"

试运行正常-无错误。但如果我正确运行,snakemake会中止它,说作业执行失败:

Waiting at most 5 seconds for missing files.
MissingOutputException in line 31 of snakeflow/Snakefile:
Missing files after 5 seconds:
output/metaphlan/isPCR/final/2_mismatch_metaphlan_rectal_SRR5907487.csv
output/metaphlan/isPCR/final/2_mismatch_metaphlan_rectal_SRR5907487.fasta
This might be due to filesystem latency. If that is the case, consider to increase the wait time with --latency-wait.

我知道我可以将解析器脚本更改为只创建两个空文件,但我不想创建不必要的文件。 我研究了动态,但这不适用于两个潜在的输出文件,所以我研究了检查点。据我所知,这应该能帮助我解决问题

以下是我使用检查点的尝试:

SAMPLES, = glob_wildcards("input/metaphlan/{sample}.biom")
ID = "0 1 2 3 4".split()

TARGETS = expand("output/metaphlan/isPCR/final/{id}_mismatch_{sample}n.txt", sample = SAMPLES, id = ID)
print(TARGETS)

rule all:
    input:
        TARGETS

rule getgenome:
    input:
        "input/metaphlan/{sample}.biom"
    output:
        csv="output/metaphlan/fasta_dump/{sample}.csv",
        fas="output/metaphlan/fasta_dump/{sample}_dump.fasta"
    conda:
        "envs/synth_genome.yaml"
    shell:
        "python scripts/get_genomes_noabund_Snakemake.py {input} 1 {output.fas} {output.csv}"

rule PCR:
    input:
        "output/metaphlan/fasta_dump/{sample}_dump.fasta"
    output:
        "output/metaphlan/isPCR/raw/{id}_mismatch/{sample}.txt"
    params:
        id = "{id}"
    shell:
        "software/exonerate-2.2.0-x86_64/bin/ipcress --products --mismatch {params.id} scripts/primers-miseq.txt {input} > {output}"

checkpoint parse:
    input:
        "output/metaphlan/isPCR/raw/{id}_mismatch/{sample}.txt"
    output:
        "output/metaphlan/isPCR/final/{id}_mismatch_{sample}.csv",
        "output/metaphlan/isPCR/final/{id}_mismatch_{sample}.fasta"
    shell:
        "python scripts/iPCRess_parser_v2.py {input} {output}"

def aggregate_input(wildcards):
    checkpoint_output = checkpoints.parse.get(**wildcards).output[0,1]
    return expand('output/metaphlan/isPCR/final/{id}_mismatch_{sample}.csv','output/metaphlan/isPCR/final/{id}_mismatch_{sample}.fasta', sample = wildcards.SAMPLES, id=wildcards.ID)

rule collect:
    input:
        aggregate_input
    output:
        "output/metaphlan/isPCR/final/{id}_mismatch_{sample}n.txt"
    shell:
        "cat {input} >> {output}"

错误呢


<function aggregate_input at 0x7f63eade2158>
SyntaxError:
Input and output files have to be specified as strings or lists of strings.
  File "snakeflow/Snakefile", line 52, in <module>

我相信这是因为我在聚合函数中使用通配符的方式有问题,但我无法理解。我尝试了在checkpoint教程中找到的各种版本,但都没有用

非常感谢您的帮助, 谢谢


Tags: 文件csvsampletxtidinputoutputshell
1条回答
网友
1楼 · 发布于 2024-10-04 05:24:19

我认为直接的错误与您的扩展有关:

return expand('output/metaphlan/isPCR/final/{id}_mismatch_{sample}.csv',
   'output/metaphlan/isPCR/final/{id}_mismatch_{sample}.fasta', 
   sample=wildcards.SAMPLES, id=wildcards.ID)

样本和ID应为小写,以匹配通配符名称

由于指定的输出文件在运行脚本后可能不存在,因此仍然会遇到缺少的输出异常。您必须将检查点的输出更改为一个目录(特定于每个示例,id),该目录将包含0或2个文件。然后,您可以在输入函数中对该目录的内容进行全局搜索,以查看存在哪些文件

对我来说,我会选择空文件路径。你可以避免检查点,这样可以使规则更清晰。对于空文件,您可以区分未运行的文件和没有结果的文件。请注意,如果使用检查点,则最终会出现空目录,因此无法完全避免emtpy文件问题

如果您担心inode或系统中的其他内容,请将输出标记为temp,snakemake将在聚合后清除它们

相关问题 更多 >