def main():
    args = read_params()
    tree = dendropy.Tree.get_from_path(args.ifn_tree, schema='newick', preserve_underscores=True)
    tree.reroot_at_midpoint()
    count = 0
    metadatas = set([])
    node2metadata = {}
    for node in tree.preorder_node_iter():
        nodestr = node.__getattribute__('taxon').__str__().strip("'")
        if node.is_leaf():
            if ('.' in nodestr):
                nodestr = nodestr.replace('.', ',')
                node.taxon = dendropy.Taxon(label=nodestr)
            substrs = re.findall(('%s-[a-zA-Z0-9.]*' % args.colorized_metadata), nodestr)
            if substrs:
                md = substrs[0].replace((args.colorized_metadata + '-'), '')
                metadatas.add(md)
                node2metadata[nodestr] = md
        else:
            count += 1
            node.taxon = dendropy.Taxon(label=('node_%d' % count))
    metadatas = sorted(list(metadatas))
    color_names = list(colors.cnames.keys())
    metadata2color = {}
    for (i, md) in enumerate(metadatas):
        metadata2color[md] = color_names[(i % len(color_names))]
    if (not args.ofn_prefix):
        args.ofn_prefix = args.ifn_tree
    ofn_tree = (args.ofn_prefix + '.graphlantree')
    tree.write_to_path(ofn_tree, 'newick')
    ofn_annot = (args.ofn_prefix + '.annot')
    with open(ofn_annot, 'w') as ofile:
        ofile.write('branch_bracket_width\t0\n')
        ofile.write('branch_bracket_depth\t0\n')
        ofile.write('annotation_background_width\t0\n')
        ofile.write('#legends\n')
        ofile.write(('class_legend_font_size\t%d\n' % args.legend_font_size))
        for md in metadata2color:
            ofile.write(('%s\tclade_marker_size\t%d\n' % (md, args.legend_marker_size)))
            ofile.write(('%s\tclade_marker_color\t%s\n' % (md, metadata2color[md])))
            ofile.write(('%s\tclade_marker_edge_width\t%f\n' % (md, args.legend_marker_edge_width)))
        for node in tree.preorder_node_iter():
            if (not node.is_leaf()):
                nodestr = node.__getattribute__('taxon').__str__().strip("'")
                ofile.write(('%s\tclade_marker_size\t0\n' % nodestr))
        for node in tree.seed_node.leaf_nodes():
            nodestr = node.__getattribute__('taxon').__str__().strip("'")
            if (nodestr in node2metadata):
                leaf_color = metadata2color[node2metadata[nodestr]]
                ofile.write(('%s\tclade_marker_size\t%d\n' % (nodestr, args.leaf_marker_size)))
                ofile.write(('%s\tclade_marker_color\t%s\n' % (nodestr, leaf_color)))
                ofile.write(('%s\tclade_marker_edge_width\t%f\n' % (nodestr, args.leaf_marker_edge_width)))
    ofn_xml = (args.ofn_prefix + '.xml')
    cmd = ('graphlan_annotate.py --annot %s %s %s' % (ofn_annot, ofn_tree, ofn_xml))
    run(cmd)
    ofn_fig = (args.ofn_prefix + args.figure_extension)
    cmd = ('graphlan.py %s %s --dpi %d --size %f' % (ofn_xml, ofn_fig, args.dpi, args.fig_size))
    run(cmd)
    print(('Output file: %s' % ofn_fig))
def main():
    args = read_params()
    tree = dendropy.Tree.get_from_path(args.ifn_tree, schema='newick', preserve_underscores=True)
    tree.reroot_at_midpoint()
    count = 0
    metadatas = set([])
    node2metadata = {}
    for node in tree.preorder_node_iter():
        nodestr = node.__getattribute__('taxon').__str__().strip("'")
        if node.is_leaf():
            if ('.' in nodestr):
                nodestr = nodestr.replace('.', ',')
                node.taxon = dendropy.Taxon(label=nodestr)
            substrs = re.findall(('%s-[a-zA-Z0-9.]*' % args.colorized_metadata), nodestr)
            if substrs:
                md = substrs[0].replace((args.colorized_metadata + '-'), )
                metadatas.add(md)
                node2metadata[nodestr] = md
        else:
            count += 1
            node.taxon = dendropy.Taxon(label=('node_%d' % count))
    metadatas = sorted(list(metadatas))
    color_names = list(colors.cnames.keys())
    metadata2color = {}
    for (i, md) in enumerate(metadatas):
        metadata2color[md] = color_names[(i % len(color_names))]
    if (not args.ofn_prefix):
        args.ofn_prefix = args.ifn_tree
    ofn_tree = (args.ofn_prefix + '.graphlantree')
    tree.write_to_path(ofn_tree, 'newick')
    ofn_annot = (args.ofn_prefix + '.annot')
    with open(ofn_annot, 'w') as ofile:
        ofile.write('branch_bracket_width\t0\n')
        ofile.write('branch_bracket_depth\t0\n')
        ofile.write('annotation_background_width\t0\n')
        ofile.write('#legends\n')
        ofile.write(('class_legend_font_size\t%d\n' % args.legend_font_size))
        for md in metadata2color:
            ofile.write(('%s\tclade_marker_size\t%d\n' % (md, args.legend_marker_size)))
            ofile.write(('%s\tclade_marker_color\t%s\n' % (md, metadata2color[md])))
            ofile.write(('%s\tclade_marker_edge_width\t%f\n' % (md, args.legend_marker_edge_width)))
        for node in tree.preorder_node_iter():
            if (not node.is_leaf()):
                nodestr = node.__getattribute__('taxon').__str__().strip("'")
                ofile.write(('%s\tclade_marker_size\t0\n' % nodestr))
        for node in tree.seed_node.leaf_nodes():
            nodestr = node.__getattribute__('taxon').__str__().strip("'")
            if (nodestr in node2metadata):
                leaf_color = metadata2color[node2metadata[nodestr]]
                ofile.write(('%s\tclade_marker_size\t%d\n' % (nodestr, args.leaf_marker_size)))
                ofile.write(('%s\tclade_marker_color\t%s\n' % (nodestr, leaf_color)))
                ofile.write(('%s\tclade_marker_edge_width\t%f\n' % (nodestr, args.leaf_marker_edge_width)))
    ofn_xml = (args.ofn_prefix + '.xml')
    cmd = ('graphlan_annotate.py --annot %s %s %s' % (ofn_annot, ofn_tree, ofn_xml))
    run(cmd)
    ofn_fig = (args.ofn_prefix + args.figure_extension)
    cmd = ('graphlan.py %s %s --dpi %d --size %f' % (ofn_xml, ofn_fig, args.dpi, args.fig_size))
    run(cmd)
    print(('Output file: %s' % ofn_fig))