Skip to content

Commit

Permalink
feat: BWA mem2 index - remove prefix parameter and determine prefix f…
Browse files Browse the repository at this point in the history
…rom output (#515)

* remove prefix parameter and determine prefix from output

* snakefmt

* case that .btw.2bit.64 is the first output

* typo in comment

* assertions for output files

* raise errors instead of assertions

* reformat

* Apply suggestions from code review

Co-authored-by: Johannes Köster <johannes.koester@uni-due.de>
Co-authored-by: Filipe G. Vieira <fgarrettvieira@gmail.com>

* fmt

* simplify code

* fix access to only element in prefixes set

Co-authored-by: Johannes Köster <johannes.koester@uni-due.de>
Co-authored-by: Filipe G. Vieira <fgarrettvieira@gmail.com>
  • Loading branch information
3 people authored Aug 18, 2022
1 parent 5313db6 commit ab8d4ad
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 11 deletions.
2 changes: 0 additions & 2 deletions bio/bwa-mem2/index/test/Snakefile
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,5 @@ rule bwa_mem2_index:
"{genome}.pac",
log:
"logs/bwa-mem2_index/{genome}.log",
params:
prefix=lambda w: w.genome,
wrapper:
"master/bio/bwa-mem2/index"
28 changes: 19 additions & 9 deletions bio/bwa-mem2/index/wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,24 @@
elif len(snakemake.input) > 1:
raise ValueError("Please provide exactly one reference genome as input.")

# Prefix that should be used for the database
prefix = None
if "prefix" in snakemake.params.keys():
prefix = snakemake.params["prefix"]
else:
prefix = splitext(snakemake.output[0])[0]
valid_suffixes = {".0123", ".amb", ".ann", ".bwt.2bit.64", ".pac"}

if len(prefix) > 0:
prefix = "-p " + prefix

shell("bwa-mem2 index" " {prefix}" " {snakemake.input[0]}" " {log}")
def get_valid_suffix(path):
for suffix in valid_suffixes:
if path.endswith(suffix):
return suffix


prefixes = set()
for s in snakemake.output:
suffix = get_valid_suffix(s)
if suffix is None:
raise ValueError(f"{s} cannot be generated by bwa-mem2 index (invalid suffix).")
prefixes.add(s[: -len(suffix)])

if len(prefixes) != 1:
raise ValueError("Output files must share common prefix up to their file endings.")
(prefix,) = prefixes

shell("bwa-mem2 index -p {prefix} {snakemake.input[0]} {log}")

0 comments on commit ab8d4ad

Please sign in to comment.