-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathPCircos-phylo.py
329 lines (241 loc) · 12 KB
/
PCircos-phylo.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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
import sys
import plotly.graph_objs as go
from plotly.offline import download_plotlyjs, init_notebook_mode, plot, iplot
init_notebook_mode(connected=True)
import numpy as np
import pandas as pd
from Complex import Complex
from Bio import Phylo
from time import time
import json
from fig import Figure
from config import json_config
import colorlover as cl
# Dependency:
## Biopython
# important function in biopython Phylo:
# tree.depths()
def json_dict(json_path):
with open(json_path, 'r') as f:
json_read = f.read()
f.close()
json_dict = json.loads(json_read)
class circularTree(Figure):
def __init__(self, *args, **kwargs):
## TEMP
assert 'config' in kwargs
self.config = json_config.json_dict(kwargs.pop('config'))
self.treepath = self.config['Category']['tree']['file']['path']
self.treeformat = self.config['Category']['tree']['file']['format']
self.cladogram = self.config['Category']['tree']['cladogram']
self.confidence = self.config['Category']['tree']['confidence']
self.degreerange = self.config['Category']['tree']['degreerange']
self.npoints = self.config['Category']['tree']['npoints']
self.start_leaf = self.config['Category']['tree']['start_leaf']
if 'meta' in self.config['Category']['tree'] and self.config['Category']['tree']['meta']['meta']:
self.meta = pd.read_csv(self.config['Category']['tree']['meta']['path'],
header = self.config['Category']['tree']['meta']['header'],
sep = self.config['Category']['tree']['meta']['sep'])
else:
self.meta = None
self.colorscale = [[0.0, 'rgb(10,10,150)'],
[0.0009, 'rgb(10,10,150)'],
[0.001, 'rgb(214, 47, 38)'],
[0.1, 'rgb(214, 47, 38)'],
[0.2, 'rgb(244, 109, 67)'],
[0.3, 'rgb(252, 172, 96)'],
[0.4, 'rgb(254, 224, 139)'],
[0.5, 'rgb(254, 254, 189)'],
[0.6, 'rgb(217, 239, 139)'],
[0.7, 'rgb(164, 216, 105)'],
[0.8, 'rgb(102, 189, 99)'],
[0.9, 'rgb(63, 170, 89)'],
[1.0, 'rgb(25, 151, 79)']]
self.tree = Phylo.read(self.treepath, self.treeformat)
self.treeList = self.fill_tree(self.tree)
def fill_tree(self, tree):
# in cases where only confidence value color needs to be drawn, self.confidence2color
traverse_order = 'preorder'
all_clades = list(tree.find_clades(order=traverse_order))
start_degree = self.degreerange[0] * np.pi/180
end_degree = self.degreerange[1] * np.pi/180
degreerange = 2*np.pi - (end_degree - start_degree)
n = 0
for k in range(len(all_clades)):
all_clades[k].id = k
if all_clades[k].is_terminal():
if self.meta is not None:
all_clades[k].color = self.meta2color()[all_clades[k].name]
n += 1
all_clades[k].theta = start_degree + degreerange * n/self.tree.count_terminals()
for k in reversed(range(len(all_clades))):
if not all_clades[k].is_terminal():
all_clades[k].theta = (all_clades[k][0].theta + all_clades[k][-1].theta)/2.0
if self.meta is not None:
leaves = [*map(lambda t: self.meta2color()[t.name], all_clades[k].get_terminals())]
print('leaves are:')
print(leaves)
all_clades[k].color = tuple(np.mean(np.array(leaves)), axis=0)
#all_clades[k].color =
for k in range(len(all_clades)):
all_clades[k].radius = self.get_branch_length_dict()[all_clades[k]]
# the first element in all_clades will be the center of the circle, sometimes the branch_length could be Nontype
if k == 0:
all_clades[k].root_radius = 0.0
else:
all_clades[k].root_radius = round(all_clades[k].radius - all_clades[k].branch_length, 6)
all_clades[k].complex = np.zeros(1, dtype='complex')
all_clades[k].complex.real = np.sin(all_clades[k].theta) * all_clades[k].radius
all_clades[k].complex.imag = np.cos(all_clades[k].theta) * all_clades[k].radius
if all_clades[k].confidence:
all_clades[k].hovertext = 'id: {}<br>branch-length: {}<br>confidence: {}'.format(all_clades[k].id, round(all_clades[k].branch_length, 4), all_clades[k].confidence.value)
else:
all_clades[k].hovertext = 'id: {}<br>name: {}<br>branch-length: {}'.format(all_clades[k].id, all_clades[k].name, all_clades[k].branch_length)
return all_clades
def complex_array(self, radius=True):
complex_array = np.zeros(len(self.treeList), dtype='complex')
if radius:
for k in range(len(self.treeList)):
complex_array.real[k] = np.sin(self.treeList[k].theta) * self.treeList[k].radius
complex_array.imag[k] = np.cos(self.treeList[k].theta) * self.treeList[k].radius
else:
for k in range(len(self.treeList)):
try:
complex_array.real[k] = np.sin(self.treeList[k].theta) * (self.treeList[k].radius - self.treeList[k].branch_length)
complex_array.imag[k] = np.cos(self.treeList[k].theta) * (self.treeList[k].radius - self.treeList[k].branch_length)
except TypeError:
complex_array.real[k] = 0.0
complex_array.imag[k] = 0.0
return complex_array
def confidence2col(self):
if not self.confidence:
return None
else:
color_list = []
all_clades = self.treeList
for k in range(len(all_clades)):
if all_clades[k].confidence:
color_list.append(all_clades[k].confidence.value)
else:
color_list.append(0.0)
#size_list = [9 if c!=-1 else 7 for c in color_list]
return color_list
def meta2color(self):
# ONGOING
if self.meta is None:
return None
else:
color_keys = self.meta.iloc[:,self.config['Category']['tree']['meta']['idcolumn']]
color_categories = self.meta.iloc[:,self.config['Category']['tree']['meta']['colorcolumn']]
unique_categories = np.unique(color_categories)
warm = cl.scales['5']['seq']['YlOrBr']
cold = cl.scales['5']['seq']['YlGnBu']
length = round(len(unique_categories)/2)
t = 5 - length % 5
length += t
d = length * 2 - len(unique_categories)
unique_color_vals = list(reversed(cl.interp(cold,length)))
unique_color_vals.extend(cl.interp(warm,length))
unique_color_vals = cl.to_numeric(cl.to_rgb(unique_color_vals))
while len(unique_color_vals) > len(unique_categories):
unique_color_vals.pop(len(unique_color_vals)//2)
unique_color_dict = dict(zip(unique_categories, unique_color_vals))
color_vals = [*map(lambda t:unique_color_dict[t], color_categories)]
'''
print('\t')
print('color_vals is:\t')
print(color_vals)
print('\t')
'''
return dict(zip(color_keys,color_vals))
def check_unit_branch_lengths(self):
if self.cladogram is True:
return True
elif not np.count_nonzero(self.tree.depths().values()):
return True
else:
return False
def get_branch_length_dict(self):
return self.tree.depths(unit_branch_lengths=self.check_unit_branch_lengths())
def phylo_trace(self):
x, y = self.complex_array().real, self.complex_array().imag
if self.confidence:
colors = self.confidence2col()
elif self.meta is not None:
colors = [*map(lambda t:t.color, self.treeList)]
else:
colors = np.repeat('black',len(x))
trace = [go.Scatter(x=x,
y=y,
mode='markers',
hoverinfo='text',
text=[*map(lambda x:x.hovertext, self.treeList)],
opacity=1,
marker=dict(color=colors,
size=8,
#colorscale=self.colorscale,
#colorbar=dict(thickness=20, dtick=10, ticklen=4, title='confidence')
))]
return trace
def get_radial_line(self):
x, y = self.complex_array().real, self.complex_array().imag
x0, y0 = self.complex_array(radius=False).real, self.complex_array(radius=False).imag
arcid = []
def generate_arcid(clade):
tmp = []
for i in range(len(clade)):
tmp.append(clade[i].id)
arcid.append(tmp)
if not clade.is_terminal():
for n in range(len(clade)):
if not clade[n].is_terminal():
generate_arcid(clade[n])
generate_arcid(self.tree.clade)
def map_id(id, list=self.treeList, attribute='theta'):
res = []
for i in range(len(id)):
res.append([])
if attribute == 'theta':
res[i] = [*map(lambda t: list[t].theta, id[i])]
elif attribute == 'root_radius':
res[i] = [*map(lambda t: list[t].root_radius, id[i])]
return res
theta_list = map_id(arcid)
#theta_list = np.array([*map(lambda x: x.theta, self.treeList)])[arcid]
theta_linspace = [*map(lambda x: np.linspace(x[0],x[-1],int(self.npoints * (np.max(x)-np.min(x))/(2*np.pi))+2), theta_list)]
arc_radius = map_id(arcid, attribute='root_radius')
#arc_radius = np.array([*map(lambda x: x.root_radius, self.treeList)])[arcid]
arc_complex = [*map(lambda x, y: np.sin(x)*np.mean(y) + np.array([1j])*np.cos(x)*np.mean(y), theta_linspace, arc_radius)]
# the circle center, we can pop that
arc_complex.pop(0)
arc_path_list = [*map(lambda x: " ".join(np.column_stack((np.concatenate((np.full(1,'M'), np.full(len(x)-1,'L'))), x.real, x.imag)).ravel()), arc_complex)]
length = len(x)
assert len(x) == len(y) == len(x0) == len(y0)
radial_path_list = np.column_stack((np.full(length,'M'), x, y, np.full(length, 'L'), x0, y0)).ravel().tolist()
path_list = arc_path_list + radial_path_list
path = " ".join(path_list)
return path
def phylo_layout(self):
layout = go.Layout(autosize=False,
showlegend=False,
xaxis=dict(visible=False),
yaxis=dict(visible=False),
hovermode='closest',
width=1500,
height=1500)
layout['shapes'] = [dict(path=self.get_radial_line())]
return layout
def phylo_fig(self):
return go.Figure(data=self.phylo_trace(), layout=self.phylo_layout())
t=time()
phylo = circularTree(config=sys.argv[1])
'''
if len(sys.argv) -1 == 3:
phylo = circularTree(path=sys.argv[1], format=sys.argv[2])
elif len(sys.argv) -1 == 2:
phylo = circularTree(path=sys.argv[1], format=sys.argv[2])
else:
phylo = circularTree(path=sys.argv[1])
'''
plot(phylo.phylo_fig())
print (time()-t)