python - 无法让此正则表达式适用于 Snakemake 中的 wildcard_constraints

标签 python regex snakemake

我有一个用 Snakemake 编写的工作流程,用于分析生物测序数据。工作流程期望对所有数据文件进行组织,以便每个原始读取文件都以检测类型(RNASeq、DNaseSeq 等)开头,并且在工作流程生成的所有文件中都保持此文件名约定。

我有一个规则来对齐除 RNASeq 之外的每个测定中的数据的读数,以及一个仅应用于 RNASeq 数据的不同规则。我在设置这些规则时遇到了麻烦,以便 Snakemake 知道对哪些文件使用哪些规则。

在 RNASeq 规则中,我有这个:

wildcard_constraints: library='RNASeq_.+'

这可以确保 RNASeq 库使用该规则。不过,我仍然收到有关其他分析的不明确规则的错误,因此我认为我需要限制其他规则中的通配符。我试过这个:

wildcard_constraints: library='(!?RNASeq)_.+'

要说匹配任何没有 RNASeq 的东西,但是如果我在 python 解释器中尝试它,这会起作用,但 Snakemake 似乎无法将任何东西与这个正则表达式匹配。我尝试过其他方法,例如“[^R][^N][^A]”,但没有任何效果。

由于这些正则表达式在我手动尝试针对字符串时起作用,因此我认为蛇形制作如何应用正则表达式存在错误,或者我不明白蛇形制作如何使用它们。我假设它只是“如果此正则表达式与通配符字符串匹配,则使用此规则。如果不匹配,则不要使用此规则。”

最佳答案

我相信以下内容演示了您想要实现的目标:

# Snakefile

rule sam_startswith_dna:
    output: '{pattern}.sam'
    wildcard_constraints: pattern='dna.+'
    shell: 'touch {output}'

rule sam_not_startswith_dna:
    output: '{pattern}.sam'
    wildcard_constraints: pattern='(?!dna).+'  # negative lookahead assertion
    shell: 'touch {output}'

rule bam_endswith_rna:
    output: '{pattern}.bam'
    wildcard_constraints: pattern='.+rna'
    shell: 'touch {output}'

rule bam_not_endswith_rna:
    output: '{pattern}.bam'
    wildcard_constraints: pattern='.+(?<!rna)'  # negative lookbehind assertion
    shell: 'touch {output}'

使用它(snakemake 4.6.0,python 3.6):

$ snakemake -n dna_sample.sam   # runs rule: sam_startswith_sam

$ snakemake -n sample.sam       # runs rule: sam_not_startswith_sam
$ snakemake -n sample_dna.sam   # runs rule: sam_not_startswith_sam

$ snakeamke -n sample_rna.bam   # runs rule: bam_endswith_rna

$ snakemake -n sample.bam       # runs rule: bam_not_endswith_rna
$ snakemake -n rna_sample.bam   # runs rule: bam_not_endswith_rna

这就是我认为你正在做的事情:

# Snakefile2

rule sam_startswith_dna_:
    output: '{pattern}.sam'
    wildcard_constraints: pattern='dna_.+'
    shell: 'touch {output}'

rule sam_not_startswith_dna_:
    output: '{pattern}.sam'
    wildcard_constraints: pattern='(?!dna)_.+'
    shell: 'touch {output}'

使用它:

$ snakemake -s Snakefile2 dna_data.sam  # runs rule: sam_startswith_dna_

$ snakemake -s Snakefile2 rna_data.sam  # raises MissingRuleException :( :( :(

解决方法如下:

# Snakefile3

rule sam_startswith_dna_:
    output: '{pattern}.sam'
    wildcard_constraints: pattern='dna_.+'
    shell: 'touch {output}'

rule sam_not_startswith_dna_:
    output: '{pattern}.sam'
    wildcard_constraints: pattern='(?!dna)[^_]{3}_.+'
    shell: 'touch {output}'

使用它:

$ snakemake -s Snakefile3 -n dna_data.sam  # runs rule: sam_startswith_dna_

$ snakemake -s Snakefile3 -n rna_data.sam  # runs rule: sam_not_startswith_dna_

但是它不是很通用,因为硬编码的 {3} :

$ snakemake -s Snakefile3 -n gdna_data.sam  # raises MissingRuleException

以下内容基于我对 snakemake.io.regex 的简要阅读还有一些四处闲逛;可能包含错误

一般来说,给出这样的规则:

rule some_rule:
    output: 'some.{pattern}.txt'
    wildcard_constraints: pattern='[a-z_]+'
    shell: 'touch {output}'

以及像这样的命令行调用:

$ snakemake some.tar_get.txt

规则some_rule将被执行如果

re.search('some\.(?P<pattern>[a-z_]+)\.txt$', 'some.tar_get.txt')

返回匹配项(假设其他检查通过(例如歧义、循环 dag 等))。

有趣的是,$被附加到模式中,但是 ^没有前置。

这种行为与我最初的想法不同,我最初的想法是这样的(这将允许在 ^ 中使用 $wildcard_constraints ):

# python3, pseudo-code-ish

output = 'some.{pattern}.txt'
pattern = '[a-z_]+'

target = 'some.tar_get.txt'

# First test: does the target file name match the output (without the constraint)?
m = re.search('some\.(?P<pattern>.+)\.txt', target)
if not m:
    raise MissingInputException

# Second test: does the wildcard satisfy user-supplied constraint?
m = re.search(pattern, m.group('pattern'))
if not m:
    raise MissingInputException

run_rule()

关于python - 无法让此正则表达式适用于 Snakemake 中的 wildcard_constraints,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46856698/

相关文章:

python - Pandas 将一列列表转换为假人

python - 为什么没有桶排序库(或者有?)

regex - 使用正则表达式搜索文本

snakemake - 如何访问 `shell` 部分内的 Snakemake 配置变量?

logging - snakemake - 为每个规则动态设置日志

python - Robot Framework中的自定义执行状态

python - 正则表达式搜索多个后缀

python - 分析 DNA 序列中的串联重复基序

java - 在 Java 中匹配字符串中的单词

python - snakemake:访问字典值的正确语法