Created
December 15, 2022 00:02
-
-
Save tsibley/f10f4c03ddf8cc149080b37c938e0ca0 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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