【问题标题】:Snakemake: Error when trying to run a rule for multiple directories and filesSnakemake:尝试为多个目录和文件运行规则时出错
【发布时间】:2020-12-17 06:03:27
【问题描述】:

我在 python 中创建了一个字典,并将目录的路径(我希望软件在其上运行)保存为键,相应的值是每个目录的预期输出列表。现在我有这样的结构:

sampleDict = {'/path_to_directory1': ["sample1","sample2","sample3"], 
              '/path_to_directory2': ["sample1","sample2"], 
              '/path_to_directory3': ["sample1","sample2","sample3"]}

# sampleDict looks pretty much like this
# key is a path to the directory that I want the rule to be executed on and the corresponding value sampleDict[key] is an array e.g. ["a","b","c"]

def input():
    input=[]
    for key in dirSampleDict:
            input.extend(expand('{dir}/{sample}*.foo', dir = key, sample=dirSampleDict[key]))
    return input




rule all:
    input:
        input()


# example should run some software on different directories for each set of directories and their expected output samples
rule example:
      input:
          # the path to each set of samples should be the wildcard
          dir = lambda wildcards: expand("{dir}", dir=dirSampleDict.keys())
      params:
       # some params
      output:
          expand('{dir}/{sample}*.foo', dir = key, sample=dirSampleDict[key])
      log:
          log = '{dir}/{sample}.log'
      run:
        cmd = "software {dir}"
        shell(cmd)

这样做我收到以下错误:

没有为通配符 'dir 指定值

编辑:也许我不太清楚我真正想要做什么,所以我填写了一些数据。

我也尝试使用我在 rule all 中设置的通配符如下:

sampleDict = {'/path_to_directory1': ["sample1","sample2","sample3"], 
              '/path_to_directory2': ["sample1","sample2"], 
              '/path_to_directory3': ["sample1","sample2","sample3"]}

# sampleDict looks pretty much like this
# key is a path to the directory that I want the rule to be executed on and the corresponding value sampleDict[key] is an array e.g. ["a","b","c"]

def input():
    input=[]
    for key in dirSampleDict:
            input.extend(expand('{dir}/{sample}*.foo', dir = key, sample=dirSampleDict[key]))
    return input




rule all:
    input:
        input()


# example should run some software on different directories for each set of directories and their expected output samples
rule example:
      input:
          # the path to each set of samples should be the wildcard
          dir = "{{dir}}"
      params:
       # some params
      output:
          '{dir}/{sample}*.foo'
      log:
          log = '{dir}/{sample}.log'
      run:
        cmd = "software {dir}"
        shell(cmd)

这样做我收到以下错误:

并非所有规则示例的输出、日志和基准文件都包含 相同的通配符。这是至关重要的,但为了避免这两个或 更多作业写入同一个文件。

我很确定第二部分更可能是我真正想要做的,因为 expand() 作为输出只会运行一次规则,但我需要为字典中的每个键值对运行它。

【问题讨论】:

    标签: snakemake


    【解决方案1】:

    首先,您对输出中的星号有什么期望?

        output:
            '{dir}/{sample}*.foo'
    

    输出必须是一个有效文件名列表,可以通过将每个通配符替换为某个字符串来形成。

    下一个问题是您在run: 部分中使用了"{dir}"。用于运行的脚本中没有定义变量dir。如果要使用通配符,则需要使用wildcards.dir 对其进行寻址。但是 run: 可以替换为 shell: 部分:

        shell:
            "software {wildcards.dir}"
    

    关于你的第一个脚本:没有定义dir 通配符(实际上根本没有通配符):

        output:
            expand('{dir}/{sample}*.foo', dir = key, sample=dirSampleDict[key])
    

    {dir}{sample} 都是expand 函数上下文中的变量,它们完全被命名参数替换。

    现在是第二个脚本。这个输入是什么意思?

        input:
            dir = "{{dir}}"
    

    这里的"{{dir}}" 不是通配符,而是对全局变量的引用(您还没有提供脚本的其余部分,所以我无法判断它是否已定义)。此外,输入需要什么?您根本不会使用 {input} 变量,并且不需要任何依赖项来将 rule example 与任何其他规则连接以生成 rule example 的输入。

    【讨论】:

      猜你喜欢
      • 2021-02-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-10-11
      • 1970-01-01
      • 1970-01-01
      • 2021-05-18
      • 2016-10-23
      相关资源
      最近更新 更多