# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python (phylopandas)
#     language: python
#     name: phylopandas
# ---

from phylogenetics import TreeProject
import toyplot

p_ = TreeProject.load('project')

p_.ancestors['ml_']

ancs

ancs

len(anc_label_color)

len(p_.tree.nodes())

p_.ancestors[['id','ml_posterior']]



ancs

anc_labels

axes.x.spine.

toyplot.__version__

# +
import numpy as np
import toytree

tip_labels='id'
tip_labels_color=None

canvas = toyplot.Canvas(width=500, height=600)
axes = canvas.cartesian(gutter=30)#, padding=200)

newick_str = p_.tree.as_string(schema='newick')
tree = toytree.tree(newick_str)

# Tip settings
tree_tips_labels = tree.get_tip_labels()
tree_ancs_labels = tree.get_node_values('support', show_root=False, show_tips=False)

# Get tips
tips = p_.data['tips']
ancs = p_.data['ancs']

# set tip labels to column in tip dataframe
if type(tip_labels) is str:
    mapping = dict(zip(tree_tips_labels, tips[tip_labels]))
    tip_labels = [mapping[label] for label in tree_tips_labels]

# set tip label colors to column in tips dataframe
if type(tip_labels_color) is str:
    mapping = dict(zip(tree_tips_labels, tips[tip_label_colors]))
    tip_labels_color = [mapping[label] for label in tree_tips_labels]

    
    
ml = p_.ancestors['ml_posterior']
colormap = toyplot.color.brewer.map(
    name="PurpleRed", 
    reverse=True,
    domain_min=.5,#0ml.min(), 
    domain_max=1)#ml.max())

anc_labels = []
for label in tree_ancs_labels:
    if label == '':
        anc_labels.append(0)
    else:
        val = ancs[ancs['id'] == label]['ml_posterior']
        anc_labels.append(val.values[0])
anc_labels = np.array(anc_labels, dtype=float)       

colors = toyplot.color.broadcast((anc_labels, colormap), shape=anc_labels.shape).tolist()
anc_label_color = []
for c in colors:
    c = [round(i*100, 1)for i in c]
    anc_label_color.append("rgba({}%,{}%,{}%,1.000)".format(*c[0:3]))    



tree.draw(axes=axes, 
    tip_labels=None,
    tip_labels_align=True,
    use_edge_lengths=True,
    node_labels='id',
    node_labels_style={'fill':'#000000', 'font-size':14},
    node_color=anc_label_color),
    #node_style={'stroke':'black', 'opacity':1})

axes.y.show = False
axes.x.spine.show = False
axes.x.ticks.show = False
axes.x.ticks.labels.show=False

numberline = canvas.color_scale(x1="20%", x2="20%", y1="80%", y2="60%", colormap=colormap, padding=5, width=20)
numberline.axis.ticks.show = True
numberline.axis.ticks.labels.angle=-90
numberline.axis.ticks.far = 0
# -

axes.x.domain.max = 1




StarcoderdataJupyterScriptsDedupFiltered