# Path: augur/utils.py
# def json_to_tree(json_dict, root=True):
#     """Returns a Bio.Phylo tree corresponding to the given JSON dictionary exported
#     by `tree_to_json`.
# 
#     Assigns links back to parent nodes for the root of the tree.
# 
#     Test opening a JSON from augur export v1.
# 
#     >>> import json
#     >>> json_fh = open("tests/data/json_tree_to_nexus/flu_h3n2_ha_3y_tree.json", "r")
#     >>> json_dict = json.load(json_fh)
#     >>> tree = json_to_tree(json_dict)
#     >>> tree.name
#     'NODE_0002020'
#     >>> len(tree.clades)
#     2
#     >>> tree.clades[0].name
#     'NODE_0001489'
#     >>> hasattr(tree, "attr")
#     True
#     >>> "dTiter" in tree.attr
#     True
#     >>> tree.clades[0].parent.name
#     'NODE_0002020'
#     >>> tree.clades[0].branch_length > 0
#     True
# 
#     Test opening a JSON from augur export v2.
# 
#     >>> json_fh = open("tests/data/zika.json", "r")
#     >>> json_dict = json.load(json_fh)
#     >>> tree = json_to_tree(json_dict)
#     >>> hasattr(tree, "name")
#     True
#     >>> len(tree.clades) > 0
#     True
#     >>> tree.clades[0].branch_length > 0
#     True
#     """
#     # Check for v2 JSON which has combined metadata and tree data.
#     if root and "meta" in json_dict and "tree" in json_dict:
#         json_dict = json_dict["tree"]
# 
#     node = Bio.Phylo.Newick.Clade()
# 
#     # v1 and v2 JSONs use different keys for strain names.
#     if "name" in json_dict:
#         node.name = json_dict["name"]
#     else:
#         node.name = json_dict["strain"]
# 
#     if "children" in json_dict:
#         # Recursively add children to the current node.
#         node.clades = [json_to_tree(child, root=False) for child in json_dict["children"]]
# 
#     # Assign all non-children attributes.
#     for attr, value in json_dict.items():
#         if attr != "children":
#             setattr(node, attr, value)
# 
#     # Only v1 JSONs support a single `attr` attribute.
#     if hasattr(node, "attr"):
#         node.numdate = node.attr.get("num_date")
#         node.branch_length = node.attr.get("div")
# 
#         if "translations" in node.attr:
#             node.translations = node.attr["translations"]
#     elif hasattr(node, "node_attrs"):
#         node.branch_length = node.node_attrs.get("div")
# 
#     if root:
#         node = annotate_parents_for_tree(node)
# 
#     return node


import argparse
import json
import numpy as np
import pandas as pd
import requests
import sys
from augur.utils import json_to_tree

    parser.add_argument("--output-table", required=True, help="tab-delimited data frame with mutations per putative subclade")
    parser.add_argument("--output-html", help="optional HTML page with links to the given Nextstrain build highlighting positions from each putative subclade")

    args = parser.parse_args()

    # Define inputs.
    tree_url = args.tree_url
    frequencies_url = args.frequencies_url
    nextstrain_url = args.nextstrain_url

    # Define parameters.
    clade_membership_attribute = args.clade_membership_attribute
    mutation_region = args.mutation_region
    minimum_mutations = args.minimum_mutations
    minimum_frequency = args.minimum_frequency
    minimum_timepoints_at_frequency = args.minimum_timepoints_at_frequency

    filter_attribute = args.filter_attribute
    filter_value = args.filter_value

    # Define outputs.
    subclades_table = args.output_table
    subclades_links = args.output_html

    if args.output_html is not None and args.nextstrain_url is None:
        print("WARNING: HTML output requested, but a Nextstrain URL was not provided. Skipping HTML output.", file=sys.stderr)

    # Load data
    tree_json = json.loads(requests.get(tree_url).content)

# Path: augur/utils.py
# def json_to_tree(json_dict, root=True):
#     """Returns a Bio.Phylo tree corresponding to the given JSON dictionary exported
#     by `tree_to_json`.
# 
#     Assigns links back to parent nodes for the root of the tree.
# 
#     Test opening a JSON from augur export v1.
# 
#     >>> import json
#     >>> json_fh = open("tests/data/json_tree_to_nexus/flu_h3n2_ha_3y_tree.json", "r")
#     >>> json_dict = json.load(json_fh)
#     >>> tree = json_to_tree(json_dict)
#     >>> tree.name
#     'NODE_0002020'
#     >>> len(tree.clades)
#     2
#     >>> tree.clades[0].name
#     'NODE_0001489'
#     >>> hasattr(tree, "attr")
#     True
#     >>> "dTiter" in tree.attr
#     True
#     >>> tree.clades[0].parent.name
#     'NODE_0002020'
#     >>> tree.clades[0].branch_length > 0
#     True
# 
#     Test opening a JSON from augur export v2.
# 
#     >>> json_fh = open("tests/data/zika.json", "r")
#     >>> json_dict = json.load(json_fh)
#     >>> tree = json_to_tree(json_dict)
#     >>> hasattr(tree, "name")
#     True
#     >>> len(tree.clades) > 0
#     True
#     >>> tree.clades[0].branch_length > 0
#     True
#     """
#     # Check for v2 JSON which has combined metadata and tree data.
#     if root and "meta" in json_dict and "tree" in json_dict:
#         json_dict = json_dict["tree"]
# 
#     node = Bio.Phylo.Newick.Clade()
# 
#     # v1 and v2 JSONs use different keys for strain names.
#     if "name" in json_dict:
#         node.name = json_dict["name"]
#     else:
#         node.name = json_dict["strain"]
# 
#     if "children" in json_dict:
#         # Recursively add children to the current node.
#         node.clades = [json_to_tree(child, root=False) for child in json_dict["children"]]
# 
#     # Assign all non-children attributes.
#     for attr, value in json_dict.items():
#         if attr != "children":
#             setattr(node, attr, value)
# 
#     # Only v1 JSONs support a single `attr` attribute.
#     if hasattr(node, "attr"):
#         node.numdate = node.attr.get("num_date")
#         node.branch_length = node.attr.get("div")
# 
#         if "translations" in node.attr:
#             node.translations = node.attr["translations"]
#     elif hasattr(node, "node_attrs"):
#         node.branch_length = node.node_attrs.get("div")
# 
#     if root:
#         node = annotate_parents_for_tree(node)
# 
#     return node


# Path: scripts/identify_emerging_clades.py
import argparse
import json
import numpy as np
import pandas as pd
import requests
import sys
from augur.utils import json_to_tree


    parser.add_argument("--output-table", required=True, help="tab-delimited data frame with mutations per putative subclade")
    parser.add_argument("--output-html", help="optional HTML page with links to the given Nextstrain build highlighting positions from each putative subclade")

    args = parser.parse_args()

    # Define inputs.
    tree_url = args.tree_url
    frequencies_url = args.frequencies_url
    nextstrain_url = args.nextstrain_url

    # Define parameters.
    clade_membership_attribute = args.clade_membership_attribute
    mutation_region = args.mutation_region
    minimum_mutations = args.minimum_mutations
    minimum_frequency = args.minimum_frequency
    minimum_timepoints_at_frequency = args.minimum_timepoints_at_frequency

    filter_attribute = args.filter_attribute
    filter_value = args.filter_value

    # Define outputs.
    subclades_table = args.output_table
    subclades_links = args.output_html

    if args.output_html is not None and args.nextstrain_url is None:
        print("WARNING: HTML output requested, but a Nextstrain URL was not provided. Skipping HTML output.", file=sys.stderr)

    # Load data
    tree_json = json.loads(requests.get(tree_url).content)

    tree = json_to_tree(tree_json)