首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >snakemake -来自python字典的通配符

snakemake -来自python字典的通配符
EN

Stack Overflow用户
提问于 2021-06-22 21:00:08
回答 1查看 36关注 0票数 1

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

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

蛇造者看起来像这样:

代码语言:javascript
运行
复制
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脚本

代码语言:javascript
运行
复制
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时,我会得到

代码语言:javascript
运行
复制
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变量,而不是存在于我的系统中的文件。

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

有没有人知道如何解决这个问题,任何其他不同的方法都是受欢迎的。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-06-22 22:42:48

如果我没理解错的话,我想这会更容易……

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

代码语言:javascript
运行
复制
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 -我认为如果出现问题,调试会更容易。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68084019

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档