-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdraw.py
More file actions
646 lines (501 loc) · 17.3 KB
/
Copy pathdraw.py
File metadata and controls
646 lines (501 loc) · 17.3 KB
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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
import networkx as nx
from matplotlib import pyplot as plt
import numpy as np
def pos_from_helix_classes(G):
pos = {}
for helix in G.nodes():
pos[helix] = (helix[0] + .5*helix[2],
helix[1] - .5*helix[2])
return pos
from matplotlib.lines import Line2D
class LineDataUnits(Line2D):
def __init__(self, *args, **kwargs):
_lw_data = kwargs.pop("linewidth", 1)
super().__init__(*args, **kwargs)
self._lw_data = _lw_data
def _get_lw(self):
if self.axes is not None:
ppd = 72./self.axes.figure.dpi
trans = self.axes.transData.transform
return ((trans((1, self._lw_data))-trans((0, 0)))*ppd)[1]
else:
return 1
def _set_lw(self, lw):
self._lw_data = lw
_linewidth = property(_get_lw, _set_lw)
import matplotlib as mpl
import matplotlib.cm as cm
import matplotlib.patheffects as PathEffects
def plot_helix_class_diagram(
ax,
graph,
selected_helix_classes = None,
sequence_length = None,
component_labels = None,
filename=None,
edge_color_key=None):
if selected_helix_classes is None:
selected_helix_classes = ()
for helix in graph.nodes():
i,j,k = helix
if helix in selected_helix_classes:
color = "lightgreen"
else:
color = "lightsteelblue"
helix_line = LineDataUnits(
[i, i + k],
[j, j - k],
linewidth=.7,
solid_capstyle="round",
color=color,
zorder=-10)
ax.add_line(helix_line)
node_positions = pos_from_helix_classes(graph)
edges = graph.edges()
ax.grid(True)
if edge_color_key is not None:
edge_color_dict = nx.get_edge_attributes(graph, edge_color_key)
edge_colors = [edge_color_dict[edge] for edge in edges]
mcl = nx.draw_networkx_edges(
graph,
node_positions,
edgelist=edges,
width=1.,
alpha=0.6,
ax=ax,
edge_color=edge_colors,
arrows=False)
else:
mcl = nx.draw_networkx_edges(
graph,
node_positions,
edgelist=edges,
width=1.,
alpha=0.6,
ax=ax,
edge_color="black",
arrows=False)
if component_labels is not None:
for component in nx.connected_components(graph):
max_i = 0
min_j = 10000000000
for helix in component:
max_i = max(helix[0] + helix[2] - 1, max_i)
min_j = min(helix[1] - helix[2] + 1, min_j)
txt = ax.text(max_i + 0.5, min_j + 0.5,
component_labels[tuple(sorted(component))],
color='darkred')
txt.set_path_effects([PathEffects.withStroke(linewidth=1., foreground='w')])
if sequence_length is None:
ax.autoscale(True)
else:
ax.set_xlim(0, sequence_length + 1)
ax.set_ylim(0, sequence_length + 1)
if filename is None:
plt.show()
else:
plt.savefig(filename,dpi=300)
plt.close()
def plot_radial_diagram(
helix_structure,
sequence,
filename = None,
label = None):
import RNA
import data
dot_bracket_string, skipped_pairs = data.To_Dot_Bracket(
helix_structure, len(sequence))
#print(helix_structure)
#print(dot_bracket_string)
fig, ax = plt.subplots(figsize=(8.,6.))
coords = RNA.get_xy_coordinates(dot_bracket_string)
coords = np.array(
[(coords.get(idx).X, coords.get(idx).Y)
for idx in range(len(dot_bracket_string))])
ax.plot(coords[:,0], coords[:,1], zorder=0, color="black")
ax.scatter(coords[:,0], coords[:,1], zorder=1, color="orange", s=50, edgecolor="black")
basepair_list = []
for helix in helix_structure:
i,j,k = helix
for idx in range(k):
pair = (i + idx, j - idx)
if pair not in skipped_pairs:
basepair_list.append(pair)
bp_coords = np.array([
[coords[pair[0]], coords[pair[1]]] for pair in basepair_list])
ax.plot(bp_coords[:,:,0].T, bp_coords[:,:,1].T, zorder=0, color="black", linewidth=3)
ax.set_aspect(1)
ax.tick_params(left=False, labelleft=False, bottom=False, labelbottom=False)
ax.spines["right"].set_visible(False)
ax.spines["left"].set_visible(False)
ax.spines["top"].set_visible(False)
ax.spines["bottom"].set_visible(False)
if label is not None:
txt = ax.text(0.9,0.9, label, transform = ax.transAxes, size=30)
txt.set_path_effects([PathEffects.withStroke(linewidth=4.,foreground='w')])
fig.tight_layout(pad=0.05)
if filename is None:
plt.show()
else:
plt.savefig(filename,dpi=300)
plt.close()
def plot_clustered_stems(
ax,
graph,
stem_clusters,
sequence_length = None,
component_labels = None,
filename = None):
helix_clusters = []
for cluster in stem_clusters:
helix_cluster = []
for stem in cluster:
helix_cluster += stem
helix_clusters.append(helix_cluster)
plot_clustered_helix_classes(
ax,
graph,
helix_clusters,
sequence_length,
component_labels,
filename)
def plot_clustered_helix_classes(
ax,
graph,
helix_clusters,
sequence_length = None,
component_labels = None,
filename = None):
import numpy as np
helix_class_cluster_dict = {}
for idx, cluster in enumerate(helix_clusters):
for helix_class in cluster:
helix_class_cluster_dict[helix_class] = idx
colormap = plt.cm.tab10
for helix in graph.nodes():
i,j,k = helix
color = colormap(helix_class_cluster_dict[helix])
helix_line = LineDataUnits(
[i, i + k],
[j, j - k],
linewidth=.7,
solid_capstyle="round",
color=color,
zorder=-10)
ax.add_line(helix_line)
node_positions = pos_from_helix_classes(graph)
edges = graph.edges()
cmap = plt.cm.get_cmap('gist_heat')
ax.grid(True)
mcl = nx.draw_networkx_edges(
graph,
node_positions,
edgelist=edges,
width=1.,
alpha=0.6,
ax=ax,
arrows=False)
if component_labels is not None:
for component in nx.connected_components(graph):
max_i = 0
min_j = 10000000000
for helix in component:
max_i = max(helix[0] + helix[2] - 1, max_i)
min_j = min(helix[1] - helix[2] + 1, min_j)
txt = ax.text(max_i + 0.5, min_j + 0.5,
component_labels[tuple(sorted(component))],
color='darkred')
txt.set_path_effects([PathEffects.withStroke(linewidth=1., foreground='w')])
if sequence_length is None:
ax.autoscale(True)
else:
ax.set_xlim(0, sequence_length + 1)
ax.set_ylim(0, sequence_length + 1)
if filename is None:
plt.show()
else:
plt.savefig(filename,dpi=300)
plt.close()
def plot_clustered_dotplot(
ax,
clustered_basepairs,
sequence_length = None,
noise_index = None,
filename = None):
import numpy as np
colormap = plt.cm.tab20
for idx, cluster in enumerate(clustered_basepairs):
x = [basepair[0] for basepair in cluster]
y = [basepair[1] for basepair in cluster]
color = np.array([colormap(idx)])
ax.scatter(x, y, c = color)
if sequence_length is None:
ax.autoscale(True)
else:
ax.set_xlim(0, sequence_length + 1)
ax.set_ylim(0, sequence_length + 1)
if filename is None:
plt.show()
else:
plt.savefig(filename,dpi=300)
plt.close()
def generate_arc_diagram(
filename,
sequence_length,
helix_structure_list,
important_helices = None):
print(important_helices)
seen_helices = set()
seen_important_helices = set()
for structure in helix_structure_list:
for helix in structure:
if helix in important_helices:
seen_important_helices.add(helix)
else:
seen_helices.add(helix)
if filename.endswith(".png"):
filename = filename[:4] + ".txt"
with open(filename, "w") as f:
f.write("# {}\n".format(sequence_length))
f.write("i\tj\tlength\tvalue\n")
for helix in seen_helices:
i,j,k = helix
f.write("{}\t{}\t{}\t0.0\n".format(i,j,k))
for helix in seen_important_helices:
i,j,k = helix
f.write("{}\t{}\t{}\t1.0\n".format(i,j,k))
f.write("\n")
import os
import subprocess
with open(os.devnull, "w") as FNULL:
retcode = subprocess.call(
['Rscript', 'arc_diagram_emph.R',filename],
stdout=FNULL,
stderr=subprocess.STDOUT)
def generate_arc_diagram_mpl(
sequence_length,
helix_classes,
important_classes = None,
max_diameter = None,
filename = None,
label_dict = None):
from matplotlib.patches import Arc
from matplotlib.collections import PatchCollection
import matplotlib.patheffects as PathEffects
patches = []
if max_diameter is None:
max_diameter = 1
for helix_class in helix_classes:
i,j,k = helix_class
center = (i+j)/2
if helix_class in important_classes:
edge_color = (0.,0.,0.)
else:
edge_color = (0.5,0.5,0.5)
for idx in range(k):
diameter = (j - i) - (2 * idx)
patch = Arc(
xy=(center, 0),
width=diameter,
height=diameter,
theta1=0,
theta2=180,
linewidth=1.5,
edgecolor=edge_color,
alpha=0.9)
patches.append(patch)
if diameter > max_diameter:
max_diameter = diameter
figure_ratio = max_diameter / sequence_length * 0.6
fig, ax = plt.subplots(figsize=(8., 8. * figure_ratio))
for patch in patches:
ax.add_patch(patch)
if label_dict is not None:
seen_labels = set()
for helix_class in sorted(helix_classes):
if label_dict[helix_class] in seen_labels:
continue
seen_labels.add(label_dict[helix_class])
i, j, k = helix_class
diameter = j - i
root_2_over_2 = 0.7071
x = (j + i) / 2 - diameter * root_2_over_2
y = diameter * root_2_over_2
txt = ax.text(x, y,
label_dict[helix_class],
color="black")
txt.set_path_effects([PathEffects.withStroke(linewidth=1.,foreground='w')])
ax.set_xlim(0,sequence_length)
ax.set_ylim(0, max_diameter / 2)
ax.set_aspect(1)
ax.tick_params(left=False, labelleft=False)
ax.spines["right"].set_visible(False)
ax.spines["left"].set_visible(False)
ax.spines["top"].set_visible(False)
fig.tight_layout(pad=0.05)
if filename is None:
plt.show()
else:
plt.savefig(filename,dpi=300)
plt.close()
def make_contiguous(approximate_class_sets):
return approximate_class_sets
def find_outer_arc(class_set):
return (6, 80)
def find_inner_arcs(class_set):
return [(9, 27), (33, 76)]
def shift(helix):
return (helix[0] + 1, helix[1] + 1, helix[2])
from operator import sub
def get_aspect(ax):
# Total figure size
figW, figH = ax.get_figure().get_size_inches()
# Axis size on figure
_, _, w, h = ax.get_position().bounds
# Ratio of display units
disp_ratio = (figH * h) / (figW * w)
# Ratio of data units
# Negative over negative because of the order of subtraction
data_ratio = sub(*ax.get_ylim()) / sub(*ax.get_xlim())
return disp_ratio / data_ratio
def generate_region_arc_diagram(
sequence_length,
helix_classes,
approximate_class_sets = None,
important_classes = None,
negative_classes = None,
figure_ratio = 0.6,
filename = None,
label = None,
label_dict = None):
flat_approximate_set = set()
[flat_approximate_set.update(class_set) for class_set in approximate_class_sets]
#contiguous_approximate_classes = make_contiguous(approximate_class_sets)
#outer_arc_list = [find_outer_arc( class_set) for class_set in contiguous_approximate_classes]
#inner_arcs_list = [find_inner_arcs(class_set) for class_set in contiguous_approximate_classes]
#outer_arc_list = []
#inner_arcs_list = []
from matplotlib.patches import Arc, PathPatch
from matplotlib.path import Path
from matplotlib.collections import PatchCollection
patches = []
max_height = 0
min_height = 0
top_max_height = figure_ratio * (0.65) * sequence_length
bottom_max_height = figure_ratio * (0.35) * sequence_length
top_height = 1
bottom_height = 1
for helix_class in helix_classes:
i,j,k = helix_class
diameter = (j - i)
height = diameter * 0.5
if helix_class in negative_classes:
if height > bottom_height:
bottom_height = height
else:
if height > top_height:
top_height = height
top_ratio = top_max_height / top_height
bottom_ratio = bottom_max_height / bottom_height
top_ratio = (1 if top_ratio > 1 else top_ratio)
bottom_ratio = (1 if bottom_ratio > 1 else bottom_ratio)
for helix_class in helix_classes:
i,j,k = helix_class
center = (i+j)/2
if helix_class in important_classes:
edge_color = (0.,0.,0.)
if helix_class in negative_classes:
edge_color = (0.5, 0., 0.)
else:
edge_color = (0.5,0.5,0.5)
if helix_class in flat_approximate_set:
line_style = ":"
else:
line_style = "-"
for idx in range(k):
diameter = (j - i) - (2 * idx)
if helix_class in negative_classes:
theta1, theta2 = 180, 360
height = diameter * bottom_ratio
else:
theta1, theta2 = 0, 180
height = diameter * top_ratio
patch = Arc(
xy=(center, 0),
width=diameter,
height=height,
theta1=theta1,
theta2=theta2,
linewidth=2,
edgecolor=edge_color,
linestyle=line_style,
alpha=0.9)
patches.append(patch)
fig, ax = plt.subplots(figsize=(8., 8. * figure_ratio))
for patch in patches:
ax.add_patch(patch)
ax.set_xlim(0, sequence_length)
ax.set_ylim(-bottom_max_height-1, top_max_height+1)
ax.set_aspect(1)
txt_list = []
if label_dict is not None:
seen_labels = set()
for helix_class in sorted(helix_classes):
if label_dict[shift(helix_class)] in seen_labels:
continue
seen_labels.add(label_dict[shift(helix_class)])
i, j, k = helix_class
radius = (j - i) * 0.5
root_2_over_2 = 0.7071
x = (j + i) / 2 - radius * root_2_over_2
y = radius * root_2_over_2
if helix_class in negative_classes:
y = (-y * bottom_ratio) - 2
else:
y = y * top_ratio
txt = ax.text(x, y,
label_dict[shift(helix_class)],
color="black",
size=20)
txt.set_path_effects([PathEffects.withStroke(linewidth=4.,foreground='w')])
txt_list.append(txt)
#ax.set_ylim(0, max_diameter / 2 + 2)
ax.spines["right"].set_visible(False)
ax.spines["left"].set_visible(False)
ax.spines["top"].set_visible(False)
#if (negative_classes is not None
# and len(negative_classes) < len(helix_classes)
# and len(negative_classes) > 0):
# ax.set_ylim(min_diameter / 2 - 2, max_diameter / 2 + 2)
# #ax.spines["bottom"].set_position("center")
# ax.set_aspect("auto")
#elif (negative_classes is not None and
# len(negative_classes) > 0):
# ax.set_ylim(min_diameter / 2 - 2, 0)
# #ax.spines["bottom"].set_position(("data",0))
# ax.spines["top"].set_visible(True)
# ax.spines["bottom"].set_visible(False)
#if get_aspect(ax) > 1:
# ax.set_aspect(1)
ax.tick_params(left=False, labelleft=False)
ax.spines["bottom"].set_position(("data",0))
#ax.spines["top"].set_position(("data",0))
fig.tight_layout(pad=0.05)
if label is not None:
txt = ax.text(0.9,0.9, label, transform = ax.transAxes, size=30)
txt.set_path_effects([PathEffects.withStroke(linewidth=2.,foreground='w')])
if filename is None:
plt.show()
else:
plt.savefig(filename,dpi=300)
plt.close()
if __name__ == "__main__":
helix_list = [(1, 100, 5), (6, 30, 4), (10, 23, 4), (29, 80, 5), (40, 55, 5), (58, 70, 5)]
sequence_length = 100
approximate_class_sets = [[(6, 30, 4), (29, 80, 5)]]
important_classes = []
generate_region_arc_diagram(
sequence_length,
helix_list,
approximate_class_sets,
important_classes)