Skip to content

Commit

Permalink
export: Allow --node-data to be repeated to build up a file list
Browse files Browse the repository at this point in the history
…instead of each repetition overriding the last values.  While there is
some theoretical utility in being able to override one list of node data
files with another in a subsequent option, I think it's more likely that
repetition is intended to built up a single list.  I was surprised, at
least.

See also <https://discussion.nextstrain.org/t/1194/4>.
  • Loading branch information
tsibley committed Jul 26, 2022
1 parent 0c19eda commit 14f48ec
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 2 deletions.
19 changes: 19 additions & 0 deletions augur/argparse_.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,22 @@ class HideAsFalseAction(Action):
"""
def __call__(self, parser, namespace, values, option_string=None):
setattr(namespace, self.dest, option_string[2:6] != 'hide')


# XXX TODO: Drop this when we drop support for 3.7 and use builtin "extend"
# action.
class ExtendAction(Action):
"""
Loosely backported version of builtin "extend" action
(argparse._ExtendAction) in CPython v3.10.5-172-gf118661a18. Some of the
ceremony we don't need has been ditched.
>>> import argparse
>>> p = argparse.ArgumentParser()
>>> a = p.add_argument("-x", action=ExtendAction, nargs="+")
>>> p.parse_args(["-x", "a", "b", "-x", "c", "-x", "d"]).x
['a', 'b', 'c', 'd']
"""
def __call__(self, parser, namespace, values, option_string=None):
current = getattr(namespace, self.dest, None) or []
setattr(namespace, self.dest, [*current, *values])
3 changes: 2 additions & 1 deletion augur/export_v1.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from Bio import Phylo
from argparse import SUPPRESS
from collections import defaultdict
from .argparse_ import ExtendAction
from .io import read_metadata
from .utils import read_node_data, write_json, read_config, read_lat_longs, read_colors

Expand Down Expand Up @@ -311,7 +312,7 @@ def add_core_args(parser):
core = parser.add_argument_group("REQUIRED")
core.add_argument('--tree','-t', required=True, help="tree to perform trait reconstruction on")
core.add_argument('--metadata', required=True, metavar="FILE", help="sequence metadata, as CSV or TSV")
core.add_argument('--node-data', required=True, nargs='+', help="JSON files with meta data for each node")
core.add_argument('--node-data', required=True, nargs='+', action=ExtendAction, help="JSON files with meta data for each node")
core.add_argument('--output-tree', help="JSON file name that is passed on to auspice (e.g., zika_tree.json).")
core.add_argument('--output-meta', help="JSON file name that is passed on to auspice (e.g., zika_meta.json).")
core.add_argument('--auspice-config', help="file with auspice configuration")
Expand Down
3 changes: 2 additions & 1 deletion augur/export_v2.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import re
from Bio import Phylo

from .argparse_ import ExtendAction
from .io import read_metadata
from .utils import read_node_data, write_json, read_config, read_lat_longs, read_colors
from .validate import export_v2 as validate_v2, auspice_config_v2 as validate_auspice_config_v2, ValidateError
Expand Down Expand Up @@ -826,7 +827,7 @@ def register_parser(parent_subparsers):
title="REQUIRED"
)
required.add_argument('--tree','-t', metavar="newick", required=True, help="Phylogenetic tree, usually output from `augur refine`")
required.add_argument('--node-data', metavar="JSON", required=True, nargs='+', help="JSON files containing metadata for nodes in the tree")
required.add_argument('--node-data', metavar="JSON", required=True, nargs='+', action=ExtendAction, help="JSON files containing metadata for nodes in the tree")
required.add_argument('--output', metavar="JSON", required=True, help="Ouput file (typically for visualisation in auspice)")

config = parser.add_argument_group(
Expand Down

0 comments on commit 14f48ec

Please sign in to comment.