Skip to content

Commit

Permalink
🚧 fix action='extend' behavior with defaults
Browse files Browse the repository at this point in the history
  • Loading branch information
victorlin committed Apr 15, 2024
1 parent 3a758f1 commit d0d0bf0
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 4 deletions.
6 changes: 6 additions & 0 deletions augur/argparse_.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@
SKIP_AUTO_DEFAULT_IN_HELP = "%(default).0s"


# We use nargs='+' with action='extend' for a more intuitive user experience,
# but that prevents any default from being overridden. Allow overriding defaults
# by deferring defaults to a later point in the code.
DEFER_LIST_DEFAULT = []


def add_default_command(parser):
"""
Sets the default command to run when none is provided.
Expand Down
9 changes: 5 additions & 4 deletions augur/filter/__init__.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
"""
Filter and subsample a sequence set.
"""
from augur.argparse_ import DEFER_LIST_DEFAULT
from augur.dates import numeric_date_type, SUPPORTED_DATE_HELP_TEXT
from augur.filter.io import ACCEPTED_TYPES, column_type_pair
from augur.io.metadata import DEFAULT_DELIMITERS, DEFAULT_ID_COLUMNS, METADATA_DATE_COLUMN
from augur.io.metadata import METADATA_DATE_COLUMN
from augur.types import EmptyOutputReportingMethod
from . import constants

Expand All @@ -19,8 +20,8 @@ def register_arguments(parser):
input_group.add_argument('--sequences', '-s', help="sequences in FASTA or VCF format")
input_group.add_argument('--sequence-index', help="sequence composition report generated by augur index. If not provided, an index will be created on the fly.")
input_group.add_argument('--metadata-chunk-size', type=int, default=100000, help="maximum number of metadata records to read into memory at a time. Increasing this number can speed up filtering at the cost of more memory used.")
input_group.add_argument('--metadata-id-columns', default=DEFAULT_ID_COLUMNS, nargs="+", action="extend", help="names of possible metadata columns containing identifier information, ordered by priority. Only one ID column will be inferred.")
input_group.add_argument('--metadata-delimiters', default=DEFAULT_DELIMITERS, nargs="+", action="extend", help="delimiters to accept when reading a metadata file. Only one delimiter will be inferred.")
input_group.add_argument('--metadata-id-columns', default=DEFER_LIST_DEFAULT, nargs="+", action="extend", help="names of possible metadata columns containing identifier information, ordered by priority. Only one ID column will be inferred.")
input_group.add_argument('--metadata-delimiters', default=DEFER_LIST_DEFAULT, nargs="+", action="extend", help="delimiters to accept when reading a metadata file. Only one delimiter will be inferred.")

metadata_filter_group = parser.add_argument_group("metadata filters", "filters to apply to metadata")
metadata_filter_group.add_argument(
Expand Down Expand Up @@ -102,7 +103,7 @@ def run(args):
'''
from .validate_arguments import validate_arguments
# Validate arguments before attempting any I/O.
validate_arguments(args)
args = validate_arguments(args)

from ._run import run as _run
return _run(args)
13 changes: 13 additions & 0 deletions augur/filter/validate_arguments.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
from augur.argparse_ import DEFER_LIST_DEFAULT
from augur.errors import AugurError
from augur.io.metadata import DEFAULT_DELIMITERS, DEFAULT_ID_COLUMNS
from augur.io.vcf import is_vcf as filename_is_vcf


Expand Down Expand Up @@ -43,3 +45,14 @@ def validate_arguments(args):
# If user requested grouping, confirm that other required inputs are provided, too.
if args.group_by and not any((args.sequences_per_group, args.subsample_max_sequences)):
raise AugurError("You must specify a number of sequences per group or maximum sequences to subsample.")


# Set defaults

if args.metadata_delimiters is DEFER_LIST_DEFAULT:
args.metadata_delimiters = DEFAULT_DELIMITERS

if args.metadata_id_columns is DEFER_LIST_DEFAULT:
args.metadata_id_columns = DEFAULT_ID_COLUMNS

return args

0 comments on commit d0d0bf0

Please sign in to comment.