Skip to content

Instantly share code, notes, and snippets.

@tsibley
Created December 15, 2022 00:02
Show Gist options
  • Save tsibley/f10f4c03ddf8cc149080b37c938e0ca0 to your computer and use it in GitHub Desktop.
Save tsibley/f10f4c03ddf8cc149080b37c938e0ca0 to your computer and use it in GitHub Desktop.
from typing import Dict, Union
from shlex import quote as shquote, split as shsplitwords
def shquotewords(s: str) -> str:
"""
Split string *s* into (POSIX) shell words, quote each word, and join them
back into a string.
This is suitable for properly quoting multi-word, user-defined values which
should follow shell quoting and escaping semantics (e.g. to allow spaces in
single words) but not allow shell features like variable interpolation,
command substition, redirection, piping, etc.
"""
return " ".join(shquote(word) for word in shsplitwords(s))
def augur_options(opts: Dict[str, Union[str, list, None]]) -> str:
return " ".join(
shquotewords(opt if val is None else f"{opt} {val}")
for opt, val in opts.items())
from typing import Dict, Union
from itertools import starmap
from shlex import quote as shquote
def augur_options(opts: Dict[str, Union[str, list, None]]) -> str:
def optval(opt, val):
if val is None:
return shquote(opt)
elif isinstance(val, list):
return shquote(opt) + " " + " ".join(map(shquote, map(str, val)))
else:
return shquote(opt) + "=" + shquote(str(val))
return " ".join(starmap(optval, opts.items()))
configfile: "config.yaml"
rule filter:
input:
metadata = config["data"]["metadata"],
sequences = config["data"]["sequences"],
excluded_strains = config["data"]["excluded_strains"],
included_strains = config["data"]["included_strains"],
output:
sequences = "filtered.fasta",
params:
options = augur_options(config["filter"]),
shell:
"""
augur filter \\
--metadata {input.metadata:q} \\
--sequences {input.sequences:q} \\
--exclude {input.excluded_strains:q} \\
--include {input.included_strains:q} \\
--output {output.sequences:q} \\
{params.options}
"""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment