-
Notifications
You must be signed in to change notification settings - Fork 3
/
plot_matrix.py
executable file
·148 lines (133 loc) · 3.63 KB
/
plot_matrix.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
#!/usr/bin/env python3
import argparse
import lib.parse_matrix as pm
from plotly.io import write_image
import plotly.graph_objs as go
parser = argparse.ArgumentParser(
description = 'Plot measure under matrix'
)
parser.add_argument('-i', required = True, help = 'Input matrix')
parser.add_argument('-p', required = True, help = 'Output figure prefix')
parser.add_argument('--measure', help = 'Input measure')
parser.add_argument('--name', default = '', help = 'Measure name')
args = parser.parse_args()
vectors = pm.matrix_to_vectors(pm.import_sparse_matrix(args.i))
if args.measure:
measure = pm.matrix_to_diagonal(
pm.import_sparse_matrix(args.measure, diagonal = True)
)
resolution = str(vectors['resolution'] // 1000) + 'k'
comments = '<br>'.join(vectors['comments'])
for chromosome in vectors['interactions']:
for r, values in enumerate(vectors['interactions'][chromosome]):
replicate = vectors['replicates'][r]
annotations = [
dict(
text = 'resolution: ' + resolution + '<br>' +
'chromosome: ' + chromosome + '<br>' +
'replicate: ' + replicate + '<br>' + comments,
font = dict(
family = 'Open Sans Condensed',
size = 35
),
align = 'left',
showarrow = False,
x = 0,
y = 0,
xref = 'paper',
yref = 'paper',
yshift = -50,
xanchor = 'left',
yanchor = 'top',
)
]
yaxes = dict(
yaxis = dict(
domain = [0.1 if args.measure else 0, 1],
scaleanchor = 'x',
autorange = 'reversed',
visible = False
)
)
data = [
go.Heatmap(
z = values,
showscale = False,
colorscale = [
[0, 'rgba(255, 255, 255, 0)'],
[1/256, 'rgba(255, 250, 250, 1)'],
[1/128, 'rgba(255, 240, 240, 1)'],
[1/64, 'rgba(255, 230, 230, 1)'],
[1/32, 'rgba(255, 220, 220, 1)'],
[1/16, 'rgba(255, 190, 190, 1)'],
[1/8, 'rgba(255, 160, 160, 1)'],
[1/4, 'rgba(255, 80, 80, 1)'],
[1/2, 'rgba(255, 50, 50, 1)'],
[1, 'rgba(255, 0, 0, 1)']
]
)
]
if args.measure:
annotations += [dict(
text = args.name,
font = dict(
family = 'Open Sans Condensed',
size = 35
),
align = 'left',
showarrow = False,
x = len(values)-1,
y = 0,
xref = 'x',
yref = 'y2',
xshift = 30,
xanchor = 'left',
yanchor = 'middle',
)]
yaxes = dict(
**yaxes,
yaxis2 = dict(
domain = [0, 0.1],
ticklen = 10,
tickfont = dict(
family = 'Open Sans Condensed',
size = 20
)
)
)
data += [
go.Scatter(
x = list(range(len(values))),
y = measure['entries'][chromosome][r],
mode = 'lines',
line = dict(
width = 2,
color = 'rgb(20, 60, 170)'
),
xaxis = 'x',
yaxis = 'y2',
)
]
layout = go.Layout(
margin = go.layout.Margin(
l = 100 if args.measure else 70,
r = 250 if args.measure else 70,
b = 950,
t = 70
),
showlegend = False,
annotations = annotations,
xaxis = dict(
domain = [0, 1],
range = [0, len(values)-1],
visible = False
),
**yaxes
)
figure = go.Figure(data = data, layout = layout)
write_image(
figure,
args.p + '_' + resolution + '_chr' + chromosome + '_' + replicate + '.png',
width = 2480,
height = 3508
)