我相信以下内容展示了您想要实现的目标:
# 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_
But由于硬编码,它不是很通用{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 等))。
有趣的是,$
被附加到模式中,但是^
没有前置。
这种行为与我最初的想法不同,我最初的想法是这样的(这将允许使用^
and $
在你的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()