snakemake:从目录结构读取输入文件并创建通配符

问题描述 投票:0回答:1

我希望创建一个管道,可以接受多个输入文件,将各种工具应用于输出,并产生所需的结果。我的

input/
目录模式如下所示:

  - organism_A
    - gene_1
      - seq_1.fasta
      - seq_2.fasta
    - gene_2
      - seq_3.fasta
  - organism_B
    - gene_1
      - seq_4.fasta
      - seq_5.fasta
      - seq_6.fasta
    - gene_3
      - seq_7.fasta

目前,我的解决方案仅读取一个通配符 - {sequence} - 对应于 input/ 目录中文件的整个路径,例如

input/organism_A/gene_1/seq_1.fasta

虽然它对于单个规则效果很好,但在添加更多规则时,很难准确指定输入和输出。

我相信根据

{organism}
目录中的文件创建
{gene}
{sequence}
input/
等通配符可能是一个很好的解决方案。拥有三个通配符将使我更容易维护输出名称并解决这些问题。

我想知道是否有办法根据

input/
目录下的文件实现多个通配符?

import os

# Get a list of all .fasta files in the input directory and its subdirectories
def get_input_files(directory):
    input_files = []
    for root, _, files in os.walk(directory):
        for file in files:
            if not file.startswith('.') and file.endswith('.fasta'):
                # Get the relative path by stripping the "input/" prefix
                relative_path = os.path.relpath(os.path.join(root, file), directory)
                input_files.append(relative_path)
    return input_files

input_files = get_input_files("input")

rule all:
    input:  expand("output/{sequence}", sequence=input_files)


rule run_script:
    input:  sequence=lambda wildcards: f"input/{wildcards.sequence}"

    output: directory("output/{sequence}")

    shell:
        r"""
        my scipt which will produce files in output directory
        """
wildcard snakemake
1个回答
0
投票

我发现了一篇非常有帮助的post,它很好地解释了事情。它描述了一种更符合 Snakemake 理念的解决方案。

以下是我的改编:

files = glob_wildcards("tool1/input/{organism}/{gene}/{sequence}.fasta")


rule all:
    input:  expand("tool2/output/{organism}/{gene}/{sequence}/{sequence}.txt", organism=files.organism, gene=files.gene, sequence=files.sequence)
            

rule tool1:
    output: "tool1/output/{organism}/{gene}/{sequence}/{sequence}.txt"

    input:  "tool1/input/{organism}/{gene}/{sequence}.fasta"

    shell:
        r"""
        tool1 command

        """


rule tool2:
    output: "tool2/output/{organism}/{gene}/{sequence}/{sequence}.txt"

    input:  "tool1/output/{organism}/{gene}/{sequence}/{sequence}.txt"


    shell:
        r"""
        tool2 command

        """
© www.soinside.com 2019 - 2024. All rights reserved.