Skip to content

Commit

Permalink
Check branch labels upon v2 dataset JSON validation
Browse files Browse the repository at this point in the history
If a default branch label is set in the JSON then ensure it is defined on at least one node in the tree. We allow the special case of setting the default label to `none`. Note that `augur export` currently only exports clade and/or aa labels, but we should still be able to validate JSONs made somewhere else.

A test build is added.
  • Loading branch information
jameshadfield committed Feb 18, 2020
1 parent 300410a commit 90ed862
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 2 deletions.
3 changes: 1 addition & 2 deletions augur/data/schema-export-v2.json
Original file line number Diff line number Diff line change
Expand Up @@ -224,8 +224,7 @@
},
"branch_label": {
"description": "What branch label should be displayed by default.",
"type": "string",
"enum": ["clade", "aa", "none"]
"type": "string"
}
}
},
Expand Down
14 changes: 14 additions & 0 deletions augur/validate_export.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,14 @@ def recurse(node):
genes -= {"nuc"}
return genes

def collectBranchLabels(root):
labels = set()
def recurse(node):
labels.update(node.get("branch_attrs", {}).get("labels", {}).keys())
if "children" in node:
[recurse(child) for child in node["children"]]
recurse(root)
return labels

def verifyMainJSONIsInternallyConsistent(data, ValidateError):
"""
Expand Down Expand Up @@ -144,6 +152,12 @@ def warn(msg):
if gene not in data["meta"]["genome_annotations"]:
warn("The tree defined mutations on gene {} which doesn't appear in the metadata annotations object.".format(gene))

default_branch_label = data.get("meta").get("display_defaults", {}).get("branch_label")
if default_branch_label and default_branch_label.lower() != "none":
labels = collectBranchLabels(data['tree'])
if not default_branch_label in labels:
warn("Default label to display \"{}\" isn't found anywhere on the tree!".format(default_branch_label))

if not warnings:
print("Validation succeeded")
else:
Expand Down
5 changes: 5 additions & 0 deletions tests/builds/validation/Snakefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
rule all:
shell:
"""
augur validate export-v2 auspice/v2_zika.json
"""

0 comments on commit 90ed862

Please sign in to comment.