https://snakemake.readthedocs.io/en/stable/snakefiles/rules.html#wildcards

expand

  1. #normal
  2. expand(["{dataset}/a.{ext}", "{dataset}/b.{ext}"], dataset=DATASETS, ext=FORMATS)
  3. #leads to
  4. ["ds1/a.txt", "ds1/b.txt", "ds2/a.txt", "ds2/b.txt", "ds1/a.csv", "ds1/b.csv", "ds2/a.csv", "ds2/b.csv"]
  5. #zip
  6. expand(["{dataset}/a.{ext}", "{dataset}/b.{ext}"], zip, dataset=DATASETS, ext=FORMATS)
  7. #leads to
  8. ["ds1/a.txt", "ds1/b.txt", "ds2/a.csv", "ds2/b.csv"]
  9. #mask from rule all
  10. expand("{{dataset}}/a.{ext}", ext=FORMATS)
  11. #simplest format: multiext
  12. rule plot:
  13. output:
  14. multiext("some/plot", ".pdf", ".svg", ".png")
  15. ##Error
  16. ###Not all output, log and benchmark files of rule bam_to_tbi_ribo contain the same wildcards. This is crucial though, in order to avoid that two or more jobs write to the same file
  17. ###解决方法多wildcards输入对更多wildcards输出的时候,要用expand把结果和mask把结果弄好,不然snakemake遍历之后会分不清你的输入所对应的输出结果是哪些。
  18. ###也要注意mask的变量是否写在后面,对输出数量的影响(循环的原理)。
  19. rule bam_to_tbi_ribo:
  20. input:
  21. ribo_bam = 'ribohmm/ribo_bam_tbi/{merge}_{mpsf}.bam',
  22. lengths = 'ribohmm/configs/{merge}_{mpsf}.txt'
  23. output:
  24. fwd = expand('ribohmm/ribo_bam_tbi/{{merge}}_{{mpsf}}_fwd.{read_length}.gz.tbi', read_length=READ_LENGTHS),
  25. rev = expand('ribohmm/ribo_bam_tbi/{{merge}}_{{mpsf}}_rev.{read_length}.gz.tbi', read_length=READ_LENGTHS)
  26. benchmark: 'time_benchmarks/ribohmm/bam_to_tbi/RIBO_{merge}_{mpsf}.txt'
  27. shell:
  28. r''' {PYTHON3} {RIBOHMM_SRC_DIR}/bam_to_tbi.py --dtype riboseq --length {input.lengths} {input.ribo_bam}'''

Constrain wildcards

  1. #First, a wildcard can be constrained within the file pattern, by appending a regular expression separated by a comma:
  2. output: "{dataset,\d+}.{group}.txt"
  3. #Second, a wildcard can be constrained within the rule via the keyword wildcard_constraints:
  4. rule complex_conversion:
  5. input:
  6. "{dataset}/inputfile"
  7. output:
  8. "{dataset}/file.{group}.txt"
  9. wildcard_constraints:
  10. dataset="\d+"
  11. shell:
  12. "somecommand --group {wildcards.group} < {input} > {output}"
  13. #global constraining wildcards
  14. wildcard_constraints:
  15. dataset="\d+"
  16. rule a:
  17. ...
  18. rule b:
  19. ...