Snakemake:在expand()中使用regex

2024-10-01 00:32:07 发布

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

在使用expand函数时,我很难使用正则表达式。由于某些原因,通配符总是作为纯文本而不是执行的正则表达式导入。不管regex是作为通配符引入还是与expand函数相连接都没有区别(请参见all_decompress vs.all\u decompress2)。错误总是:

Missing input files for rule DECOMPRESS:
Resources/raw/run1_lane1_read[1,2]_index\d+-\d+=[1-9], [11-32].fastq.gz

-

^{pr2}$

Tags: 函数文本input错误原因filesalldecompress
3条回答

我想在expand中执行正则表达式是不可能的。但是我找到了一个解决办法。对于其中两个通配符,我找到了不同的描述方式(“read”和“index”),对于第三个通配符,我准备了一个函数并将其用作输入。在

#!/usr/bin/env python3

import re

###### WILDCARDS #####

## General descriptive parameters
read = (1,2)
index = list(range(1,9)) + list(range(11,32))

## Functions
def getDCinput(wildcards):
    read = wildcards.read
    index = wildcards.index
    path = wd + "Resources/raw/run1_lane1_read" + read + r"_index[0-9]??-[0-9]??=" + sample + ".fastq.gz"
    return(glob.glob(path))

##### RULES #####

### CONJUNCTION RULES ("all") ###
# PREANALYSIS #

rule all_decompress:
    input:
        expand("Resources/decompressed/read{read}_index{index}.fastq", read=read, index=index)

### TASK RULES ###
# FILE PREPARATION AND SMOOTHING #

# Decompress .gz zipped raw files
rule DECOMPRESS:
    input:
        getDCinput
    output:
        "Resources/decompressed/read{read}_index{index}.fastq"
    shell:
        "gzip -d -c {input} > {output}"

你查过bli的链接吗?在

具体来说,this part of the documentation。在

下面是一个简单的示例,说明我如何使用它来生成png、pdf或两者兼而有之:

rule all:
  input: expand("{graph}.png", graph=["dag", "rulegraph"])

rule dot_to_image:
    input: "{graph}.dot"
    output: "{graph}.{ext,(pdf|png)}"
    shell: "dot -T{wildcards.ext} -o {output} {input}"

希望这有帮助。在

如果我是对的,函数expand in Snakemake生成一个字符串列表。此函数用于文件名,就像您使用它一样。在

我不知道expand函数是否可以与regex关联以创建列表。在

但是您可以用python生成这个列表并将其交给ruleall或expand函数。在

在您的情况下,您可以使用以下代码来获取并生成文件名列表:

import re
import os

path='.'
listoffiles=[]
for file in os.listdir(path):
  if(re.search('read[1-2]_index\d{3}-\d{3}=[1-9]',file)):
    listoffiles.append(os.path.splitext(file)[0])

然后在listoffiles中,你有你所有的文件名,你只需要像这样使用你的扩展:

^{pr2}$

那么一切都应该完美运作。在

记住,在创建所有规则和dag之前,snakefile中的所有python代码都将在工作流开始时执行。所以它可以很强大。在

相关问题 更多 >