def set_node_style(node, status, n_gl_sets, ref_label=None): if status != 'internal': if status not in scolors: raise Exception('status \'%s\' not in scolors' % status) node.img_style['bgcolor'] = scolors[status] if status not in used_colors: used_colors[status] = scolors[status] if glutils.is_novel(node.name): node.add_face(ete3.CircleFace(args.novel_dot_size, scolors['novel']), column=1) #, position='float') # if args.leaf_names else 'branch') # linewidth = 2 # node.img_style['hz_line_width'] = linewidth # node.img_style['vt_line_width'] = linewidth names = status.split('-&-') if node.is_leaf(): if args.pie_chart_faces and len(names) > 1: pcf = ete3.PieChartFace(percents=[100./len(names) for _ in range(len(names))], width=args.leafheight, height=args.leafheight, colors=[scolors[n] for n in names], line_color=None) # pcf = ete3.StackedBarFace(percents=[100./len(names) for _ in range(len(names))], width=30, height=50, colors=[scolors[n] for n in names], line_color=None) node.add_face(pcf, column=0, position='aligned') elif len(names) == 1 and names[0] in used_faces: node.add_face(ete3.RectFace(width=5, height=args.leafheight, bgcolor=used_faces[names[0]], fgcolor=None), column=0, position='aligned') elif n_gl_sets > 2: rectnames = [n for n in names if n in used_faces] node.add_face(ete3.StackedBarFace(percents=[100./len(names) for _ in range(len(rectnames))], width=5 * len(rectnames), height=args.leafheight, colors=[used_faces[rn] for rn in rectnames], line_color=None), column=0, position='aligned') else: # every leaf has to have a face, so that every leaf takes up the same vertical space node.add_face(ete3.RectFace(width=1, height=args.leafheight, bgcolor=None, fgcolor=None), column=0, position='aligned')
def write_legend(plotdir): def get_leg_name(status): if args.legends is not None and status in args.glslabels: lname = args.legends[args.glslabels.index(status)] elif status == 'both': if len(args.glsfnames) == 2: lname = 'both' elif len(args.glsfnames) == 3: lname = 'two' else: raise Exception('wtf %d' % len(args.glsfnames)) elif status == 'all': if len(args.glsfnames) == 2: lname = 'both' elif len(args.glsfnames) == 3: lname = 'all three' else: raise Exception('wtf %d' % len(args.glsfnames)) else: lname = status return lname def add_stuff(status, leg_name, color): legfo[leg_name] = color if status in used_faces: facefo[leg_name] = used_faces[status] legfo, facefo = {}, {} if args.ref_label is not None: for status, color in simu_colors.items(): add_stuff(status, status, color) else: added_two_method_color = False for status, color in used_colors.items(): if '-&-' in status: for substatus in status.split( '-&-' ): # arg, have to handle cases where the single one isn't in there if get_leg_name(substatus) not in legfo: add_stuff(substatus, get_leg_name(substatus), scolors[substatus]) if not added_two_method_color: leg_name = get_leg_name('both') added_two_method_color = True else: continue else: leg_name = get_leg_name(status) add_stuff(status, leg_name, color) # figure out the order we want 'em in lnames = sorted(legfo.keys()) for status in ['both', 'all']: if get_leg_name(status) in lnames: lnames.remove(get_leg_name(status)) lnames.append(get_leg_name(status)) etree = ete3.ClusterTree() #'(a);') tstyle = ete3.TreeStyle() tstyle.show_scale = False # tstyle.show_leaf_name = False # for node in etree.traverse(): # print node.name # node.add_face(ete3.CircleFace(args.novel_dot_size, scolors['novel']), column=1) #, position='float') # if args.leaf_names else 'branch') dummy_column = 0 pic_column = 1 text_column = 2 leg_title_height = 1.5 * args.leafheight # if args.legend_title is not None else 0.75 * args.leafheight for icol in range(text_column + 1): # add a top border tstyle.title.add_face(ete3.RectFace(0.9 * args.leafheight, 0.9 * args.leafheight, fgcolor=None, bgcolor=None), column=icol) tstyle.title.add_face(ete3.TextFace(' ', fsize=leg_title_height), column=dummy_column) # adds a left border if args.legend_title is not None: tstyle.title.add_face( ete3.TextFace('', fsize=leg_title_height), column=pic_column ) # keeps the first legend entry from getting added on this line tstyle.title.add_face( ete3.TextFace(args.legend_title, fsize=leg_title_height, fgcolor='black', bold=True), column=text_column ) # add an empty title so there's some white space at the top, even with no actual title text for leg_name in lnames: color = legfo[leg_name] size_factor = 2. if leg_name in facefo: tstyle.title.add_face( ete3.StackedBarFace([80., 20.], width=size_factor * args.leafheight, height=size_factor * args.leafheight, colors=[color, facefo[leg_name]], line_color='black'), column=pic_column ) # looks like maybe they reversed fg/bg kwarg names else: tstyle.title.add_face( ete3.RectFace(size_factor * args.leafheight, size_factor * args.leafheight, fgcolor='black', bgcolor=color), column=pic_column ) # looks like maybe they reversed fg/bg kwarg names tstyle.title.add_face(ete3.TextFace(' ' + leg_name, fsize=args.leafheight, fgcolor='black'), column=text_column) tstyle.title.add_face(ete3.CircleFace(1.5 * args.novel_dot_size, scolors['novel']), column=pic_column) tstyle.title.add_face( ete3.TextFace('novel allele', fsize=args.leafheight), column=text_column ) # keeps the first legend entry from getting added on this line etree.render(plotdir + '/legend.svg', tree_style=tstyle)