/
heatmap.py
executable file
·236 lines (207 loc) · 8.41 KB
/
heatmap.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
#!/usr/bin/python3
# -*- coding: utf-8 -*-
#
# ------------------------------
# Name: heatmap.py
# Purpose: Create a clustermap (clustered heatmap with dendrograms)
#
# @uthor: acph - dragopoot@gmail.com
#
# Created:
# Copyright: (c) acph 2015
# Licence: GNU GENERAL PUBLIC LICENSE, Version 3, 29 June 2007
# Modified: October 12, 2021
# Modified by: Ian Rambo (imrambo)
# ------------------------------
""" This script creates a cluster map """
import argparse
import numpy as np
import pandas as pd
import matplotlib
import matplotlib.pyplot as plt
import matplotlib.patches as mpatches
import scipy.cluster.hierarchy as sch
def argparser():
"""Arguments"""
epilog = """Example:
$ python3 heatmap.py data.heatmap.tsv """
parser = argparse.ArgumentParser(description=__doc__, epilog=epilog,
formatter_class=argparse.RawDescriptionHelpFormatter)
parser.add_argument('filename',
help="Input file derived mebs_output with classification")
parser.add_argument('-f', '--im_format', default='png', type=str,
choices=['png', 'pdf', 'ps', 'eps', 'svg', 'tif', 'jpg'],
help='''Output format for images [png].''')
parser.add_argument('-r', '--im_res', default=300, type=int,
help='''Output resolution for images in
dot per inch (dpi) [dpi].''',
metavar='dpi')
parser.add_argument('-o', '--outfig', required = False,
help = 'Specified filename for output heatmap')
# parser.add_argument('') # cluster files
# parser.add_argument('') # distance metric
# parser.add_argument('') # linkage method
# parser.add_argument('') # columns labels
# parser.add_argument('') # colormap
args = parser.parse_args()
return args
def main():
"""Main function"""
args = argparser()
matplotlib.rcParams['lines.linewidth'] = 0.4
# load data
print('[INFO] Loading data')
data = pd.read_csv(args.filename,
sep='\t',
index_col=[0, 1])
# *************************************************************************
# * data scaling normaliation *
# *************************************************************************
# to do
# data_scaling = (data.T - data.T.min())/(data.T.max() - data.T.min())
# data = data_scaling.T
# *************************************************************************
# * Fix columns (rows) with 0 or show error and *
# * warning to the user *
# *************************************************************************
# Fix for 0 columns
print("[WARN] Fixing all 0s columns")
data = data.loc[:, data.sum() != 0]
# *************************************************************************
# * Calculate distnaces - add to options *
# *************************************************************************
# Rows distances
d_metric = 'braycurtis'
linkage_m = 'average'
metadist = sch.distance.pdist(data, metric=d_metric)
metalink = sch.linkage(metadist, method=linkage_m)
metalink = metalink.clip(0, metalink.max() + 1)
# columns distances
profdist = sch.distance.pdist(data.T, metric=d_metric)
proflink = sch.linkage(profdist, method=linkage_m)
proflink = proflink.clip(0, proflink.max() + 1)
############
# Plotting #
############
print('[INFO] Plotting ...')
# - Figure setup
xf = 6.7
yf = 8.6
fig = plt.figure(figsize=(xf, yf))
# Axes positions
# # Axes without column names
# posm = [0.01, 0.01, 0.2, 0.82]
# posp = [0.24, 0.84, 0.67, 0.15]
# posmat = [0.24, 0.01, 0.67, 0.82]
# posm_colors = [0.215, 0.01, 0.02, 0.82]
# poscbar = [0.92, 0.01, 0.015, 0.40]
# new with labels
posm = [0.01, 0.23, 0.2, 0.62]
posp = [0.24, 0.855, 0.67, 0.14]
posmat = [0.24, 0.23, 0.67, 0.62]
posm_colors = [0.215, 0.23, 0.02, 0.62]
# poscbar = [0.94, 0.01, 0.02, 0.41]
poscbar = [0.92, 0.23, 0.015, 0.30]
poslegend = [0.01, 0.84, 0.23, 0.15]
# colors for dendrograms
sch.set_link_color_palette(['#1f77b4', '#ff7f0e',
'#2ca02c', '#d62728',
'#9467bd', '#8c564b',
'#e377c2', '#7f7f7f',
'#bcbd22', '#17becf'])
# # - rows dendrogram
meta_ax = fig.add_axes(posm, frameon=False)
metadend = sch.dendrogram(metalink,
color_threshold=0.2 * max(metalink[:, 2]),
orientation='left')
meta_ax.set_xticks([])
meta_ax.set_yticks([])
# # - columns dendrogram
prof_ax = fig.add_axes(posp, frameon=False)
profdend = sch.dendrogram(proflink,
color_threshold=0.2 * max(proflink[:, 2]),
orientation='top')
prof_ax.set_xticks([])
prof_ax.set_yticks([])
# # - Matrix - HEATMAP
matrix_ax = fig.add_axes(posmat)
mmask = metadend['leaves']
pmask = profdend['leaves']
#mat = data.get_values()
mat = data.to_numpy()
mat = mat[mmask, :]
mat = mat[:, pmask]
im = matrix_ax.matshow(mat, aspect='auto', origin='lower', cmap='viridis')
# ****************************************************************************
# * Here we can add options to show labels - needs to modify axes positions *
# ****************************************************************************
# Labels
matrix_ax.set_yticks([])
matrix_ax.set_xticks(range(len(data.columns)))
matrix_ax.set_xticklabels(data.columns[pmask], rotation=90,
fontsize=5, color='k')
matrix_ax.xaxis.set_ticks_position('bottom')
# # - Colorbar
colorbar_ax = fig.add_axes(poscbar)
cb = plt.colorbar(im, cax=colorbar_ax)
cb.set_label('Completeness', fontsize='x-small')
cb.ax.tick_params(labelsize='xx-small')
# # - Color code
# Get colors from the first element in the index
# general_index = sorted(pd.MultiIndex.to_frame(data.index)[0].unique(),
# key=lambda x: int(x[1:]))
#Sort the taxonomic groups for color assignment
general_index = sorted(pd.MultiIndex.to_frame(data.index)[0].unique(),
key=str.lower)
print(general_index)
color_as = {}
for i, v in enumerate(range(len(general_index))):
icolor = plt.cm.tab20(v / len(general_index))
color_as[general_index[i]] = icolor
# color vector
print(color_as)
color_vec = [color_as[i[0]] for i in data.index]
color_vec = np.array(color_vec)
color_vec = color_vec[mmask]
color_ax = fig.add_axes(posm_colors, frameon=False)
lefts = range(0, len(color_vec), 1)
height = np.ones(len(color_vec))
width = 1
metabars = color_ax.barh(lefts, height, width, color=color_vec,
edgecolor=color_vec)
# Can you use matshow, pcolor or imshow?
# im_col = color_ax.matshow(color_mat, aspect='auto',
# origin="lower")
# color_ax.set_xlim(-0.5, 0.5)
color_ax.set_xticks([])
color_ax.set_yticks([])
color_ax.set_ylim((0, len(color_vec)))
# # - Legend
legend_ax = fig.add_axes(poslegend, frameon=False)
legend_ax.set_xticks([])
legend_ax.set_yticks([])
patches = []
for name, color_ in color_as.items():
p = mpatches.Patch(color=color_, label=name)
patches.append(p)
plt.legend(handles=patches, fancybox=True, fontsize='xx-small',
loc=2, framealpha=0.75)
# # - show
# plt.show()
# # - Save Figure
figname = 'heatmap.{}'.format(args.im_format)
if args.outfig:
figname = str(args.outfig)
else:
pass
print('[INFO] Output figure will be saved to {}'.format(figname))
fig.savefig(figname, dpi=args.im_res)
# ****************************************************************************
# * Save cluster data *
# ****************************************************************************
# # - Save cluster data!!!!
# clustdata = data.iloc[mmask, pmask]
# clustdata.to_csv('matdata_clust_pru.txt', sep='\t')
if __name__ == '__main__':
main()
print('[INFO] End!!!. Thanks for using :D')