Skip to content

Commit

Permalink
Centralize --validation-mode and --skip-validation
Browse files Browse the repository at this point in the history
Since --validation-mode is used in many subcommands, create a
centralized function that adds the option as well as the related
--skip-validation option.

This adds --skip-validation to ancestral and translate.
  • Loading branch information
victorlin committed Oct 22, 2024
1 parent 862aa37 commit cd3ccb0
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 36 deletions.
6 changes: 2 additions & 4 deletions augur/ancestral.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,8 @@
from .io.vcf import is_vcf as is_filename_vcf
from treetime.vcf_utils import read_vcf, write_vcf
from collections import defaultdict
from .types import ValidationMode
from .argparse_ import add_validation_arguments
from .util_support.node_data_file import NodeDataObject
from .export_v2 import validation_mode_help_message

def ancestral_sequence_inference(tree=None, aln=None, ref=None, infer_gtr=True,
marginal=False, fill_overhangs=True, infer_tips=False,
Expand Down Expand Up @@ -335,8 +334,7 @@ def register_parser(parent_subparsers):
general_group = parser.add_argument_group(
"general",
)
general_group.add_argument('--validation-mode', type=ValidationMode, choices=[mode for mode in ValidationMode], default=ValidationMode.ERROR,
help=validation_mode_help_message)
add_validation_arguments(general_group)

return parser

Expand Down
35 changes: 34 additions & 1 deletion augur/argparse_.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
"""
Custom helpers for the argparse standard library.
"""
from argparse import Action, ArgumentDefaultsHelpFormatter
from argparse import Action, ArgumentDefaultsHelpFormatter, ArgumentParser, _ArgumentGroup
from typing import Union
from .types import ValidationMode


# Include this in an argument help string to suppress the automatic appending
Expand Down Expand Up @@ -93,3 +95,34 @@ def __call__(self, parser, namespace, value, option_string = None):
current = []

setattr(namespace, self.dest, [*current, *value])


def add_validation_arguments(parser: Union[ArgumentParser, _ArgumentGroup]):
"""
Add arguments to configure validation mode of node data JSON files.
"""
parser.add_argument(
'--validation-mode',
dest="validation_mode",
type=ValidationMode,
choices=[mode for mode in ValidationMode],
default=ValidationMode.ERROR,
help="""
Control if optional validation checks are performed and what
happens if they fail.
'error' and 'warn' modes perform validation and emit messages about
failed validation checks. 'error' mode causes a non-zero exit
status if any validation checks failed, while 'warn' does not.
'skip' mode performs no validation.
Note that some validation checks are non-optional and as such are
not affected by this setting.
""")
parser.add_argument(
'--skip-validation',
dest="validation_mode",
action="store_const",
const=ValidationMode.SKIP,
help="Skip validation of input/output files, equivalent to --validation-mode=skip. Use at your own risk!")
30 changes: 2 additions & 28 deletions augur/export_v2.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from Bio import Phylo
from typing import Dict, Union, TypedDict, Any, Tuple

from .argparse_ import ExtendOverwriteDefault
from .argparse_ import ExtendOverwriteDefault, add_validation_arguments
from .errors import AugurError
from .io.file import open_file
from .io.metadata import DEFAULT_DELIMITERS, DEFAULT_ID_COLUMNS, InvalidDelimiter, read_metadata
Expand Down Expand Up @@ -926,20 +926,6 @@ def node_data_prop_is_normal_trait(name):

return True

validation_mode_help_message = """
Control if optional validation checks are performed and what
happens if they fail.
'error' and 'warn' modes perform validation and emit messages about
failed validation checks. 'error' mode causes a non-zero exit
status if any validation checks failed, while 'warn' does not.
'skip' mode performs no validation.
Note that some validation checks are non-optional and as such are
not affected by this setting.
"""


def register_parser(parent_subparsers):
parser = parent_subparsers.add_parser("v2", help=__doc__)
Expand Down Expand Up @@ -1007,19 +993,7 @@ def register_parser(parent_subparsers):
optional_settings = parser.add_argument_group(
title="OTHER OPTIONAL SETTINGS"
)
optional_settings.add_argument(
'--validation-mode',
dest="validation_mode",
type=ValidationMode,
choices=[mode for mode in ValidationMode],
default=ValidationMode.ERROR,
help=validation_mode_help_message)
optional_settings.add_argument(
'--skip-validation',
dest="validation_mode",
action="store_const",
const=ValidationMode.SKIP,
help="Skip validation of input/output files, equivalent to --validation-mode=skip. Use at your own risk!")
add_validation_arguments(optional_settings)

return parser

Expand Down
5 changes: 2 additions & 3 deletions augur/translate.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,8 @@
from treetime.vcf_utils import read_vcf
from augur.errors import AugurError
from textwrap import dedent
from .types import ValidationMode
from .argparse_ import add_validation_arguments
from .util_support.node_data_file import NodeDataObject
from .export_v2 import validation_mode_help_message

class MissingNodeError(Exception):
pass
Expand Down Expand Up @@ -373,7 +372,7 @@ def register_parser(parent_subparsers):
parser.add_argument('--alignment-output', type=str, help="write out translated gene alignments. "
"If a VCF-input, a .vcf or .vcf.gz will be output here (depending on file ending). If fasta-input, specify the file name "
"like so: 'my_alignment_%%GENE.fasta', where '%%GENE' will be replaced by the name of the gene")
parser.add_argument('--validation-mode', type=ValidationMode, choices=[mode for mode in ValidationMode], default=ValidationMode.ERROR, help=validation_mode_help_message)
add_validation_arguments(parser)

vcf_only = parser.add_argument_group(
title="VCF specific",
Expand Down

0 comments on commit cd3ccb0

Please sign in to comment.