예제 #1
0
def tree():
    """Returns an annotated Bio.Phylo tree.
    """
    with open("tests/data/flu_seasonal_h3n2_ha_3y_tree.json") as fh:
        json_tree = json.load(fh)

    tree = json_to_tree(json_tree)
    return tree
예제 #2
0
def get_phylogeny(tree_path, tree_type="newick"):
    if tree_type == "newick":
        with open(tree_path) as f:
            for phylogeny in Phylo.parse(f, "newick"):
                break
            return phylogeny

    elif tree_type == "auspice_json":
        with open(tree_path) as f:
            tree = json.load(f)

        phylogeny = json_to_tree(tree)
        return phylogeny

    else:
        raise ValueError(f"current tree_type {tree_type} not supported")
예제 #3
0
        help="tab-delimited file of attributes per node of the given tree")
    parser.add_argument("--include-internal-nodes",
                        action="store_true",
                        help="include data from internal nodes in output")
    parser.add_argument(
        "--attributes",
        nargs="+",
        help="names of attributes to export from the given tree")

    args = parser.parse_args()

    # Load tree from JSON.
    with open(args.tree, "r") as fh:
        tree_json = json.load(fh)

    tree = json_to_tree(tree_json)

    # Collect attributes per node from the tree to export.
    records = []

    if args.attributes:
        attributes = args.attributes
    else:
        attributes = sorted(
            list(tree.root.node_attr.keys()) +
            list(tree.root.branch_attrs.keys()))

    for node in tree.find_clades(terminal=True):
        if node.is_terminal() or args.include_internal_nodes:
            record = {"name": node.name}
                        help="earliest date to show on the x-axis")
    parser.add_argument("--end-date", help="latest date to show on the x-axis")
    parser.add_argument(
        "--include-color-bar",
        action="store_true",
        help=
        "display a color bar for the color by option at the bottom of the plot"
    )
    args = parser.parse_args()

    if args.tree.endswith(".json"):
        with open(args.tree, "r") as json_fh:
            json_tree = json.load(json_fh)

        # Convert JSON tree layout to a Biopython Clade instance.
        tree = json_to_tree(json_tree)

        # Plot the tree.
        plot_tree(tree, args.output, args.colorby, args.branch_width,
                  args.tip_size, args.start_date, args.end_date,
                  args.include_color_bar)
    else:
        tree = read_tree(args.tree)
        tree.ladderize()
        fig, ax = plt.subplots(1, 1, figsize=(8, 8))
        Bio.Phylo.draw(tree,
                       axes=ax,
                       label_func=lambda node: "",
                       show_confidence=False)
        plt.savefig(args.output)