# Colormap for seq type
seqtype_cmap = {
"Rebound": "#ffa600",
"Control IgG Outgrowth": "#63bfcf",
"Autologous IgG Outgrowth": "#DC3F93",
"No IgG Outgrowth": "#2B488C",
}
# Setting node style
def set_seqtype_color(node):
if "SeqType" in node.props:
nstyle = NodeStyle()
nstyle["fgcolor"] = seqtype_cmap[node.get_prop('SeqType')]
nstyle["size"] = 8
nstyle["hz_line_width"] = 2
node.set_style(nstyle)
def make_branches_bigger(node, new_size):
node.img_style["size"] = 0
node.img_style["hz_line_width"] = new_size # Change the horizotal lines stroke size
node.img_style["vt_line_width"] = new_size # Change the vertical lines stroke size
for node in t.traverse():
set_seqtype_color(node)
for c in node.children:
make_branches_bigger(c, 2)
# plotting entire tree
ts = TreeStyle()
ts.show_leaf_name = False
def custom_layout(node):
"""
This function creates the stacking of clonal sequences
"""
if 'Weight' in node.props:
if node.get_prop('Weight') != -1:
for i in range(node.get_prop('Weight')):
faces.add_face_to_node(faces.CircleFace(4, seqtype_cmap[node.get_prop('SeqType')]),
node, column=i*2+1, position="branch-right")
spot_sum = node.get_prop('Weight')
# Go through the DF of different SeqTypes
if leaf_df.loc[node.name].sum() > 0:
for seq_type_i in df_cols:
range_val = leaf_df.loc[node.name, seq_type_i]
for i in range(range_val):
faces.add_face_to_node(faces.CircleFace(4, seqtype_cmap[seq_type_i]),
node, column=spot_sum*2+1, position="branch-right")
spot_sum += 1
ts.layout_fn = custom_layout
ts.margin_left = 20
ts.margin_right = 20
ts.margin_top = 20
# Setting root node
rootstyle = NodeStyle()
rootstyle["size"] = 0
rootstyle["fgcolor"] = 'black'
rootstyle["shape"] = "square"
rootstyle["vt_line_width"] = 2
rootstyle["hz_line_width"] = 2
t.set_style(rootstyle)
# LEGEND INFORMATION
for key, val in seqtype_cmap.items():
ts.legend.add_face(TextFace(f" {key}", fsize=10), column=1)
ts.legend.add_face(CircleFace(3, val), column=0)
ts.legend_position = 2
# more space between branches
ts.branch_vertical_margin = 2
# ts.scale_length = 0.0005
# ts.tree_width = 1
ts.scale = 1000 # def = 10000
# showing plot
t.render("%%inline", tree_style=ts, w=10, dpi=300, units='in')