From d129d8e3fa769b269df18401dcee50dd779732a1 Mon Sep 17 00:00:00 2001 From: Victor Lin <13424970+victorlin@users.noreply.github.com> Date: Tue, 28 Oct 2025 13:58:47 -0700 Subject: [PATCH] Write a specific config section This will be used by workflows that call 'augur subsample' to dump the specific section of config to use for that command. --- snakemake/config.smk | 28 ++++++++++++++++++++++++---- 1 file changed, 24 insertions(+), 4 deletions(-) diff --git a/snakemake/config.smk b/snakemake/config.smk index ada7c1c..b47ee9b 100644 --- a/snakemake/config.smk +++ b/snakemake/config.smk @@ -147,18 +147,38 @@ def resolve_config_path(path: str, defaults_dir: Optional[str] = None) -> Callab return _resolve_config_path -def write_config(path): +def write_config(path, section=None): """ - Write Snakemake's 'config' variable to a file. + Write Snakemake's 'config' variable, or a section of it, to a file. + + *section* is an optional list of keys to navigate to a specific section of + config. If provided, only that section will be written. """ global config os.makedirs(os.path.dirname(path), exist_ok=True) + data = config + section_str = "config" + + if section: + # Navigate to the specified section + for key in section: + # Error if key doesn't exist + if key not in data: + raise Exception(f"ERROR: Key {key!r} not found in {section_str!r}.") + + data = data[key] + section_str += f".{key}" + + # Error if value is not a mapping + if not isinstance(data, dict): + raise Exception(f"ERROR: {section_str!r} is not a mapping of key/value pairs.") + with open(path, 'w') as f: - yaml.dump(config, f, sort_keys=False, Dumper=NoAliasDumper) + yaml.dump(data, f, sort_keys=False, Dumper=NoAliasDumper) - print(f"Saved current run config to {path!r}.", file=sys.stderr) + print(f"Saved {section_str!r} to {path!r}.", file=sys.stderr) class NoAliasDumper(yaml.SafeDumper):