在ete3中向热图添加标签

时间:2018-07-16 16:55:21

标签: python-3.x visualization phylogeny ete3

我阅读了有关生成轮廓面热图的整个文档,但找不到任何方法将标签添加到ete3生成的热图。例如,在下面的代码中,热图的7列的名称为'Marker1'至'Marker7'。是否可以将这些名称添加到ete3生成的热图中,例如“ BarChartFace”可用的“ labels”选项?还是唯一的方法是将树移植到matplotlib并在那里添加标签?

from ete3 import Tree, TextFace, NodeStyle, TreeStyle, faces, AttrFace, ClusterTree, ProfileFace
PhenoDict={'a':1,'b':0,'c':1}
matrix = """
#Names\tmarker1\tmarker2\tmarker3\tmarker4\tmarker5\tmarker6\tmarker7
a\t1\t-1\t1\t-1\t-1\t-1\t1
b\t-1\t-1\t1\t1\t-1\t-1\t1
c\t1\t1\t1\t-1\t1\t1\t1
"""

t=ClusterTree( "((a,b),c);" , text_array=matrix)

#Defining a function to generate textFace for each node

def ColorCodedNode (node):
    if node.is_leaf():
        ColorCode=PhenoDict[node.name]

        if ColorCode == 0:
            faces.add_face_to_node(AttrFace('name',fsize=20,fgcolor='blue'), node, column=0,aligned=True)
column=1,position='aligned')
            faces.add_face_to_node(ProfileFace(1, -1, 0, width=200, height=40, style='heatmap', colorscheme=2),node,column=1,position='aligned')
        elif ColorCode == 1:
            faces.add_face_to_node(AttrFace("name",fsize=20,fgcolor='red'), node, column=0,aligned=True)
            faces.add_face_to_node(ProfileFace(1, -1, 0, width=200, height=40, style='heatmap', colorscheme=2),node,column=1,position='aligned')


ts = TreeStyle()
ts.layout_fn= ColorCodedNode
ts.show_scale = False
ts.show_leaf_name = False
ts.draw_guiding_lines=True
t.show(tree_style=ts)

现在它会生成像这样的树,但是我需要向热图的每一列添加标签。 enter image description here

1 个答案:

答案 0 :(得分:1)

部分答案:

我找不到方法,但是您可能对 aligned_headeraligned_footTreeStyle 属性感兴趣:

假设您设法创建了一个轴作为面,命名为 axisface。我认为我的回答是片面的,因为我创建这张脸的解决方案是基于 BarChartFace 的一种不完美的解决方法:

from ete3 import BarChartFace

labels = matrix.split('\n')[1].split('\t')[1:]
axisface = BarChartFace([0]*len(labels), width=200, height=0, labels=labels, max_value=1, scale_fsize=1) # Can't get rid of the Y axis tick labels though

然后您可以将其添加到页脚/页眉:

ts.aligned_foot.add_face(axisface, 1)


除了单个面之外,您还可以为每个标签创建一个 RectFace:

from ete3 import RectFace

for i, lab in enumerate(labels):
    labface = RectFace(50, 200/len(labels), '#fff', '#eee', label={'text':lab, 'fontsize':8, 'color': 'black'})
    labface.rotation = -90
    ts.aligned_foot.add_face(labface, 1+i)

但是矩形没有与每个热图列正确对齐。