2017-02-21 9 views
1

私は、展開関数を使用するときに正規表現を使用するのに苦労しています。何らかの理由で、ワイルドカードは、実行された正規表現ではなくプレーンテキストとして常にインポートされます。正規表現が事前にワイルドカードとして導入されているのか、拡張機能に関連して導入されているのかに違いはありません(all_decompressとall_decompress2を参照)。エラーが常にある:Snakemake:expand()でregexを使用する

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

-

#!/usr/bin/env python3 

import re 

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

## General descriptive parameters 
read = "[1,2]" 
index_prefix = r"\d{3}-\d{3}" 
index = r"[1-9], [11-32]" 

##### RULES ##### 

### CONJUNCTION RULES ("all") ### 
# PREANALYSIS # 
rule all_decompress: 
    input: 
     expand("Resources/decompressed/read{read}_index{index_prefix}={index}.fastq", read=read, index_prefix=index_prefix, index=index) 

rule all_decompress2: 
    input: 
     expand("Resources/decompressed/read{read}_index{index_prefix}={index}.fastq", read=[1,2], index_prefix=r"\d{3}-\d{3}", index=r"[1-9], [11-32]") 

### TASK RULES ### 
# PREANALYSIS # 

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

便利なことがいくつかあります: 'wildcard_constraints'(https://snakemake.readthedocs.io/en/stable/snakefiles/rules.html#wildcards)と' glob_wildcards'(https://snakemake.readthedocs.io /en/stable/project_info/faq.html#glob-wildcards) – bli

+0

私は慎重に見ていないかもしれませんが、それを使用せずにあなたが 'reをインポートする 'と私には思われます。 – bli

答えて

1

私は右の機能がSnakemakeに拡大していた場合は文字列のリストを作ります。この関数は、使用したのと同じようにファイル名に使用されます。

拡張機能を正規表現に関連付けてリストを作成できるかどうかわかりません。

しかし、このリストをPythonで生成して、それをルールallまたはexpand関数に渡すことができます。あなたはすべてのファイル名を持っていて、ちょうどこのように拡大し、あなたを使用する必要がlistoffilesで次に

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]) 

は、あなたのケースでは、取得したファイル名のごLISTEを作るために、次のコードを使用することができます。

expand("{repertory}{filename}{extension}, 
     repertory = "Resources/decompressed/", 
     filename = listoffiles, 
     extension = ".fastq") 

すべてが完全に機能するはずです。

スネークファイル内のすべてのPythonコードが、すべてのルールとダグの作成より前にワークフローの開始時に実行されることを忘れないでください。それは強力なことができます。

+0

Pinchot!あなたのコメントをありがとう!あなたのアプローチは確かに正しいファイル文字列を得るでしょうが、私が信じるワイルドカードを使ってそれらにアクセスすることはできません。さらに、記述された問題は多かれ少なかれ一般的な問題であり、私はこれらのすべてのケースに対して明示的な関数を宣言する必要はありません。 – Zuup

0

私はおそらく拡張で正規表現を実行することはできないと思う。しかし、私は回避策を見つけました。 2つのワイルドカードについて、私はそれらを記述するさまざまな方法(「読み込み」と「インデックス」)を見つけました.3つ目は、関数を用意して入力として使用しました。

#!/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}" 
1

bliさんのリンクを見ましたか?

具体的には、this part of the documentation。このことができます

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}" 

希望:

は、ここで私は、PNG、PDF、またはその両方を生成するためにそれを使用する方法の簡単な例です。

関連する問題