首页 > 解决方案 > snakemake - python 字典中的通配符

问题描述

我正在编写一个蛇形文件,该文件在特定位置具有输入文件,并具有特定的文件夹名称(对于此示例,barcode9[456])。我需要更改这些目录中的命名约定,因此我现在想向我的snakemake 添加第一条规则,它将原始位置(FASTQ_PATH)中的文件夹链接到snakemake 工作目录中的输出文件夹。此目录中的链接文件夹的名称来自 python 字典d,在蛇形文件中定义。然后我会使用这些目录作为下游规则的输入。

所以我的snakemake的第一条规则实际上是一个python脚本(scripts/ln.py),它将原始目录中的命名约定映射到所需的命名约定,并链接fastqs:

蛇形看起来像这样:

FASTQ_PATH = '/path/to/original_location'


# dictionary that maps the subdirectories in FASTQ_PATH (keys) with the directory names that I want in the snakemake working directory (values)
d = {'barcode94': 'IBC_UZL-CV5-04',
 'barcode95': 'IBC_UZL-CV5-42',
 'barcode96': 'IBC_UZL-CV5-100'}



rule all:
    input:
        expand('symLinkFq/{barcode}', barcode = list(d.values())) # for each element in list(d.values()) I want to create a subdirectory that would point to the corresponding path in the original location (FASTQ_PATH)



rule symlink:
    input:
        FASTQ_PATH,
        d
    output:
        'symLinkFq/{barcode}'
    script:
        "scripts/ln.py"

我调用来创建链接的 python 脚本如下所示

import pandas as pd
import subprocess as sp
import os

# parsing variables from Snakefile
d_sampleNames = snakemake.input[1]
fastq_path = snakemake.input[0]


os.makedirs('symLinkFq')
for barcode in list(d_sampleNames.keys()):
    idx = list(d_sampleNames.keys()).index(barcode)
    sampleName = list(d_sampleNames.values())[idx]
    
    sp.run(f"ln -s {fastq_path}/{barcode} symLinkFq/{sampleName}", shell=True) # the relative path with respect to the working directory should suffice for the DEST in the ln -s command

但是当我打电话时,snakemake -np -s Snakefile我得到

Building DAG of jobs...
MissingInputException in line 15 of /nexusb/SC2/ONT/scripts/SnakeMake/minimalExample/renameFq/Snakefile:
Missing input files for rule symlink:
barcode95
barcode94
barcode96

这个错误对我来说很有意义。我拥有的唯一“输入”文件是 python 变量,而不是我系统中确实存在的文件。

我想我遇到的问题归结为这样一个事实,即我想用于所有规则的通配符不存在于任何可用作输入的文件中,所以我能想到使用的是带有对应关系的字典,尽管它不像我尝试的那样工作。

有谁知道如何解决这个问题,欢迎任何其他不同的方法。

标签: pythonsnakemake

解决方案


如果我理解正确,我认为这可能会更容易......

我会反转键/值映射(此处为dict(zip(...))),而不是使用 lambda 输入函数来获取每个输出键的源目录:

FASTQ_PATH = '/path/to/files'

d = {'barcode94': 'IBC_UZL-CV5-04',
 'barcode95': 'IBC_UZL-CV5-42',
 'barcode96': 'IBC_UZL-CV5-100'}

d = dict(zip(d.values(), d.keys())) # Values become keys and viceversa

rule all:
    input:
        expand('symLinkFq/{barcode}', barcode = d.keys()) 

rule symlink:
    input:
        indir= lambda wc: os.path.join(FASTQ_PATH, d[wc.barcode]),
    output:
        outdir= directory('symLinkFq/{barcode}'),
    shell:
        r"""
        ln -s {input.indir} {output.outdir}
        """

顺便说一句,在 python 脚本中,我会使用os.symlink()而不是生成子进程并调用ln -s- 我认为如果出现问题更容易调试。


推荐阅读