-
Notifications
You must be signed in to change notification settings - Fork 128
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor measurements module to a package
Moves the measurements module to its own package where the subcommands are split out into their own modules. The subcommand subparsers are added with the new `add_command_subparsers` function. This is an effort to reorganize augur commands with subcommands as packages instead of continuously adding subcommands as modules. Starting with the measurements command since it is a new command that is unlikely to already be used by anyone outside of the Nextstrain. In the future, we may want to consider refactoring export and validate in the same way. Putting that off for now since reorganizing those modules may result in breaking outside uses of their APIs.
- Loading branch information
1 parent
d531d48
commit dc4f58b
Showing
3 changed files
with
156 additions
and
134 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
""" | ||
Create JSON files suitable for visualization within the measurements panel of Auspice. | ||
""" | ||
from augur.argparse_ import add_command_subparsers | ||
from augur.utils import first_line | ||
from . import export, concat | ||
|
||
SUBCOMMANDS = [ | ||
export, | ||
concat, | ||
] | ||
|
||
|
||
def register_parser(parent_subparsers): | ||
parser = parent_subparsers.add_parser("measurements", help=first_line(__doc__)) | ||
# Add subparsers for subcommands | ||
subparsers = parser.add_subparsers(dest='subcommand') | ||
add_command_subparsers(subparsers, SUBCOMMANDS) | ||
return parser |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
""" | ||
Concatenate multiple measurements JSONs into a single JSON file | ||
""" | ||
import sys | ||
|
||
from augur.utils import first_line, write_json | ||
from augur.validate import ( | ||
measurements as read_measurements_json, | ||
ValidateError | ||
) | ||
|
||
|
||
def register_parser(parent_subparsers): | ||
parser = parent_subparsers.add_parser("concat", help=first_line(__doc__)) | ||
|
||
concat_required = parser.add_argument_group( | ||
title="REQUIRED" | ||
) | ||
concat_required.add_argument("--jsons", required=True, type=str, nargs="+", metavar="JSONs", | ||
help="Measurement JSON files to concatenate.") | ||
concat_required.add_argument("--output-json", required=True, metavar="JSON", type=str, | ||
help="Output JSON file") | ||
|
||
concat_optional = parser.add_argument_group( | ||
title="OPTIONAL SETTINGS" | ||
) | ||
concat_optional.add_argument("--default-collection", type=str, | ||
help="The key of the default collection to display. " + | ||
"If not provided, the first collection of the first JSON file will be displayed") | ||
concat_optional.add_argument("--minify-json", action="store_true", | ||
help="Concatenate JSONs without indentation or line returns.") | ||
|
||
return parser | ||
|
||
|
||
def run(args): | ||
output = { | ||
'collections': [] | ||
} | ||
if args.default_collection is not None: | ||
output['default_collection'] = args.default_collection | ||
|
||
for json in args.jsons: | ||
measurements = read_measurements_json(json) | ||
output['collections'].extend(measurements['collections']) | ||
|
||
indent = {"indent": None} if args.minify_json else {} | ||
write_json(output, args.output_json, include_version=False, **indent) | ||
try: | ||
read_measurements_json(measurements_json=args.output_json) | ||
except ValidateError: | ||
print( | ||
"ERROR: Validation of output JSON failed. See detailed errors above.", | ||
file=sys.stderr, | ||
) | ||
sys.exit(1) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters