社区所有版块导航
Python
python开源   Django   Python   DjangoApp   pycharm  
DATA
docker   Elasticsearch  
aigc
aigc   chatgpt  
WEB开发
linux   MongoDB   Redis   DATABASE   NGINX   其他Web框架   web工具   zookeeper   tornado   NoSql   Bootstrap   js   peewee   Git   bottle   IE   MQ   Jquery  
机器学习
机器学习算法  
Python88.com
反馈   公告   社区推广  
产品
短视频  
印度
印度  
Py学习  »  Python

PyComplexHeatmap进阶教程:用python画热图的【行】/【列】注释信息

生信技能树 • 2 年前 • 464 次点击  

微信公众号:Computational Biology

关注生物信息学和计算表观遗传学。问题或建议,请公众号留言。

今天,给大家详细地介绍一下PyComplexheatmap(https://github.com/DingWB/PyComplexHeatmap) 中annotation的使用方法,也就是如何用python在热图中添加【行】/【列】注释信息。比如样本的疾病状态(肿瘤或者正常样本、年龄、性别、分型等)。


如何用python画热图上, 下, 左、右不同方向的【行】/【列】注释信息

# 导入示例数据
with open(os.path.join(os.path.dirname(PyComplexHeatmap.__file__),"../data/mammal_array.pkl"), 'rb'as f:
    data = pickle.load(f)
df, df_rows, df_cols, col_colors_dict = data
df

GSM4412025GSM4412026GSM4412027GSM4412028GSM4412029GSM4412030GSM4412031GSM4412032GSM4412033GSM4412034...GSM4997945GSM4997946GSM4997947GSM4997948GSM4997949GSM4997950GSM4997951GSM4997952GSM4997953GSM4997954
sheep1.0000001.0000001.0000001.0000001.0000001.0000001.0000001.0000001.0000001.000000...0.4350330.4329000.4466260.4491230.4971800.5159180.4837060.5046810.529076 0.446443
beluga whale0.6874880.6942070.7065250.7027340.6870140.7040030.7058870.6938060.7194170.712677...0.5603810.5715520.6103920.6136190.6758320.6685020.6248200.6583770.7023340.575034
house mouse0.0000000.0000000.0000000.0000000.0000000.0000000.0000000.0000000.0000000.000000...0.0000000.0000000.0000000.0000000.0000000.0000000.0000000.0000000.0000000.000000
vaquita0.6935230.7025250.7167920.7250950.7112610.7086510.7179520.7054860.7209150.724171...0.5818620.5944430.6289080.6394570.6808010.7074930.6623380.6651420.7518590.584952
large flying fox0.2868220.2694060.2967960.3147190.3050740.3084190.2684210.2972360.2950190.297973...0.2816840.3639260.3672160.3593920.2898210.2573510.2343010.2958400.2493360.344848
greater horseshoe bat0.5306060.5179890.5207190.5258550.5075830.5119930.5281070.5215630.5421590.509526...0.7351510.7148050.7088680.7382070.8236040.8280400.8233820.7968820.8787830.693625
little brown bat0.2025250.2159900.2389770.2418720.2677500.2367290.2023840.2405190.2511480.267257...1.0000001.0000001.0000001.0000001.0000001.0000001.0000001.0000001.0000001.000000

7 rows × 883 columns

df_rows

PredictedTaxidPredictedSpeciescommon_namesFamily
sheep9940.0ovis_aries_rambouilletsheepBovidae
beluga whale9749.0delphinapterus_leucasbeluga whaleMonodontidae
house mouse10090.0mus_musculushouse mouseMuridae
vaquita42100.0phocoena_sinusvaquitaPhocoenidae
large flying fox132908.0pteropus_vampyruslarge flying foxPteropodidae
greater horseshoeNaNNaNNaNNaN
bat59479.0rhinolophus_ferrumequinumgreater horseshoe batRhinolophidae
little brown bat59463.0myotis_lucifuguslittle brown batVespertilionidae
df_cols

GSEBasenameNCBI_scientific_nametaxidTissueSexFamilyOrderSpeciesSuccessRatecommon_names
GSM4412025GSE147003GSM4412025Ovis aries9940BloodFemaleBovidaeArtiodactylaOvis aries0.765818sheep
GSM4412026GSE147003GSM4412026Ovis aries9940BloodFemaleBovidaeArtiodactylaOvis aries0.797669sheep
GSM4412027GSE147003GSM4412027Ovis aries9940Blood FemaleBovidaeArtiodactylaOvis aries0.759256sheep
GSM4412028GSE147003GSM4412028Ovis aries9940BloodMaleBovidaeArtiodactylaOvis aries0.749813sheep
GSM4412029GSE147003GSM4412029Ovis aries9940BloodFemaleBovidaeArtiodactylaOvis aries0.770433sheep
....................................
GSM4997950GSE164127GSM4997950Eptesicus fuscus29078SkinMaleVespertilionidaeChiropteraEptesicus fuscus0.660425big brown bat
GSM4997951GSE164127GSM4997951Eptesicus fuscus29078SkinMaleVespertilionidaeChiropteraEptesicus fuscus0.652822big brown bat
GSM4997952GSE164127GSM4997952Eptesicus fuscus29078SkinFemaleVespertilionidaeChiropteraEptesicus fuscus0.664746big brown bat
GSM4997953 GSE164127GSM4997953Eptesicus fuscus29078SkinMaleVespertilionidaeChiropteraEptesicus fuscus0.650848big brown bat
GSM4997954GSE164127GSM4997954Eptesicus fuscus29078SkinMaleVespertilionidaeChiropteraEptesicus fuscus0.657170big brown bat

883 rows × 11 columns

注释信息放在热图「上方」

#Put annotations on the top
col_ha = HeatmapAnnotation(label=anno_label(df_cols.Family, merge=True, rotation=45),
                               Family=anno_simple(df_cols.Family, legend=True),
                               Tissue=df_cols.Tissue,label_side='right', axis=1)
plt.figure(figsize=(74))
cm = ClusterMapPlotter(data=df, top_annotation=col_ha,
                       show_rownames=True, show_colnames=False,row_names_side='left',
                       col_split=df_cols.Family, cmap='exp1', label='AUC',
                       rasterized=True, legend=True)
#plt.savefig("clustermap.pdf", bbox_inches='tight')
plt.show()
Starting calculating row orders..
Starting calculating col orders..
Starting plotting HeatmapAnnotations
Collecting annotation legends..
Incresing ncol
Incresing ncol
More than 3 cols is not supported
Legend too long, generating a new column..

如果有很多图例,PyComplexHeatmap会自动将所有的图例按顺序排列,如果一列放不下,就「自动增加一列」,用两列来画图例(figure legends)。如上图所示,就有2列图例。此外,在注释文字(比如Bovidae)与热图之间曲线的形状和颜色都会随着文字的旋转角度和颜色一起变化,会自动调整角度,使之与注释文字的角度相匹配

注释信息放在热图「下方」

#Put annotations on the bottom
col_ha = HeatmapAnnotation(Tissue=anno_simple(df_cols.Tissue,height=5),
                           Family=anno_simple(df_cols.Family, legend=False,height=6),
                           label=anno_label(df_cols.Family, merge=True,rotation=-45),
                           label_side='right',axis=1)
plt.figure(figsize=(74))
cm = ClusterMapPlotter(data=df, bottom_annotation=col_ha,
                       show_rownames=True, show_colnames=False,row_names_side='right',
                       col_split=df_cols.Family, cmap='jet', label='AUC',
                       rasterized=True, legend=True)
plt.show()
Starting calculating row orders..
Starting calculating col orders..
Starting plotting HeatmapAnnotations
Collecting annotation legends..

如果想要把列注释信息放在热图下方,那就需要「改变HeatmapAnnotation的顺序」, anno_label 应该放在「最后」anno_label(df_cols.Family) 应该是倒数第二个。anno_label中的rotation参数应该从45度变为-45度,因为旋转方向发生了改变。

此外,可以通过设置row_names_side='right'来将【行】名字row labels挪到热图右边。值得注意的是,「热图与图例之间的间隙是自动调节」的,比如,当row_names_side='right'时,热图右边有了文字,图例就自动往右边挪了,不用我们额外设置。

【行】注释(annotation bar)的高度,可以通过height (mm)参数来设置,比如,在上图的anno_simple中,我们将Family这个bar图的高度设置成了5mm,而Tissue的高度仍然是默认(4mm)。

注释信息放在热图「左方」

#Put annotations on the left
row_ha = HeatmapAnnotation(label=anno_label(df_cols.Family, merge=True,rotation=45),
                           Family=anno_simple(df_cols.Family, legend=False,height=5),
                           Tissue=anno_simple(df_cols.Tissue,height=5),
                           label_side='top',
                           label_kws={'rotation':45,'rotation_mode':'anchor','color':'red'},
                           axis=0)
plt.figure(figsize=(47))
cm = ClusterMapPlotter(data=df.T,left_annotation=row_ha,
                       show_rownames=False, show_colnames=True,col_names_side='top',
                       row_split=df_cols.Family, cmap='exp1', label='AUC',
                       rasterized=True, legend=True,
                       xticklabels_kws={'labelrotation':45,'labelcolor':'blue'})
plt.show()
Starting calculating row orders..
Starting calculating col orders..
Starting plotting HeatmapAnnotations
Collecting annotation legends..

我们首先把df进行转制(df.T),col_ha改成row_ha(现在变成了【行】注释)。我们可以通过改变参数col_names_side='top'来把【行】注释的标签(xlabel)放到热图上方(或者bottom,放到热图下方),另外,改变xticklabels_kws参数可以改变【行】注释标签的旋转角度颜色(比如上图中的Tissue和Family,旋转45度,颜色为红色):label_kws={'rotation':45,'rotation_mode':'anchor','color':'red'}

注释信息放在热图「右方」

#Put annotation on the right
row_ha = HeatmapAnnotation(Tissue=df_cols.Tissue,
                           Family=anno_simple(df_cols.Family, legend=False,height=5),
                           label=anno_label(df_cols.Family, merge=True,rotation=45),
                           label_side='bottom',
                           label_kws={'rotation':-45,'rotation_mode':'anchor','color':'red'},
                           axis=0)
plt.figure(figsize=(47))
cm = ClusterMapPlotter(data=df.T,right_annotation=row_ha,
                       show_rownames=False, show_colnames=True ,col_names_side='bottom',
                       row_split=df_cols.Family, cmap='jet', label='AUC',
                       rasterized=True, legend=True,
                       xticklabels_kws={'labelrotation':-45,'labelcolor':'blue'})
#plt.savefig("annotation.pdf", bbox_inches='tight')
plt.show()
Starting calculating row orders..
Starting calculating col orders..
Starting plotting HeatmapAnnotations
Collecting annotation legends..

同理,【行】注释信息也可以放到右侧。根据上述方法,我们也可以同时添加【行】和【列】注释,并调整注释文字和标签的颜色、旋转角度等。

如果大家需要用python来画热图的话,请试用一下,然后在公众号后台给一些反馈,欢迎提出一些改进建议。

欢迎关注本公众号,后期将持续更新本系列和其它更多的原创文章。

往期精选文章


Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/151433
 
464 次点击