From ea85954ffeb6eb77c962fd06f1b311380cdb26e6 Mon Sep 17 00:00:00 2001 From: Victor Lin <13424970+victorlin@users.noreply.github.com> Date: Tue, 1 Oct 2024 13:37:27 -0700 Subject: [PATCH] fixup! Use ISO duration format for clade recency --- defaults/parameters.yaml | 2 +- docs/src/reference/workflow-config-file.rst | 7 ++++--- workflow/snakemake_rules/common.smk | 11 ++++++++++- 3 files changed, 15 insertions(+), 5 deletions(-) diff --git a/defaults/parameters.yaml b/defaults/parameters.yaml index bd91600c2..2ff6f2ac0 100644 --- a/defaults/parameters.yaml +++ b/defaults/parameters.yaml @@ -135,7 +135,7 @@ ancestral: inference: "joint" colors: - # Months back to color clades, if "all" then all clades are colored + # Amount of time back to color clades, if "all" then all clades are colored # Can be specified per build in builds.yaml default: clade_recency: "all" diff --git a/docs/src/reference/workflow-config-file.rst b/docs/src/reference/workflow-config-file.rst index bfa004f78..ac621b4d4 100644 --- a/docs/src/reference/workflow-config-file.rst +++ b/docs/src/reference/workflow-config-file.rst @@ -965,9 +965,10 @@ Each named traits configuration (``default`` or build-named) supports the follow clade_recency ~~~~~~~~~~~~~ -- type: integer -- description: if integer value is provided, restrict to clades found in tree within X months of present -- default: ``all`` +- type: string +- format: `ISO 8601 `__ duration with optional ``P`` prefix (e.g. ``2M``, ``18M``, ``1Y6M``) +- description: restrict to clades found in tree within this duration from present +- default: ``all`` (no restriction) traits ------ diff --git a/workflow/snakemake_rules/common.smk b/workflow/snakemake_rules/common.smk index 9a15f2841..91c687e11 100644 --- a/workflow/snakemake_rules/common.smk +++ b/workflow/snakemake_rules/common.smk @@ -1,6 +1,7 @@ """Small, shared functions used to generate inputs and parameters. """ import datetime +import isodate from itertools import product from shlex import ( quote as shquote, # shquote() is used in this file and also other workflow files @@ -181,9 +182,17 @@ def _get_clade_recency_for_wildcards(wildcards): def _get_clade_recency_argument(wildcards): clade_recency_setting = _get_clade_recency_for_wildcards(wildcards) + + def is_duration(date): + try: + isodate.parse_duration(duration) + return True + except: + return False + if clade_recency_setting == "all": return "" - elif isinstance(clade_recency_setting, int): + elif is_duration(clade_recency_setting): return "--clade-recency " + shquote(str(clade_recency_setting)) else: return ""