-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplot_supported.py
More file actions
120 lines (105 loc) · 3.62 KB
/
plot_supported.py
File metadata and controls
120 lines (105 loc) · 3.62 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
import plotly
import plotly.tools as pltools
import plotly.plotly as py
import plotly.graph_objs as go
import csv
import sys
import numpy as np
import scipy.stats
def scatter(title, xtitle, ytitle, X, Y, L, outfn, logx = False, xmin = None, xmax = None,):
print(title, len(X), np.mean(Y), np.std(Y), np.percentile(Y, [25, 50, 75]))
if logx:
XY = np.vstack((np.log(X), Y))
else:
XY = np.vstack((X, Y))
density = scipy.stats.gaussian_kde(XY)(XY)
assert len(X) == len(Y) == len(density)
trace = go.Scatter(
x = X,
y = Y,
mode = 'markers',
text = L,
marker = dict(
color = density,
colorscale = 'Viridis',
),
)
xaxis = {
'title': xtitle,
'type': logx and 'log' or 'linear',
'range': [xmin, xmax],
}
layout = go.Layout(
title = title,
hovermode = 'closest',
xaxis = xaxis,
yaxis = {
'title': ytitle,
},
)
fig = go.Figure(data=[trace], layout=layout)
plotly.offline.plot(fig, filename=outfn)
def extract_vals(rows, key):
return [R[key] for R in rows]
def extract_floats(rows, key):
return np.array([float(V) for V in extract_vals(rows, key)])
def pie(title, values, labels, outfn):
trace = go.Pie(labels=labels, values=values)
layout = go.Layout(title = title)
fig = go.Figure(data=[trace], layout=layout)
plotly.offline.plot(fig, filename=outfn)
def plot(cnvfn):
with open(cnvfn) as cnvf:
reader = csv.DictReader(cnvf, delimiter='\t')
rows = list(reader)
sampids = extract_vals(rows, 'dataset')
vals = {K: np.array(extract_floats(rows, K)) for K in (
'num_bp_replaced_by_sv',
'num_lone_sv',
'num_bp_replaced_by_sv',
'num_bp_away_from_cents_and_telos'
)}
num_sv = vals['num_lone_sv'] + vals['num_bp_replaced_by_sv']
frac_sv_supported_by_cbps = vals['num_bp_replaced_by_sv'] / (vals['num_lone_sv'] + vals['num_bp_replaced_by_sv'])
sv_indices = num_sv > 0
print('Discarded %s vals from sv_indices' % (len(sampids) - np.sum(sv_indices)))
num_bp = vals['num_bp_away_from_cents_and_telos']
frac_cbp_supported_by_svs = vals['num_bp_replaced_by_sv'] / vals['num_bp_away_from_cents_and_telos']
bp_indices = num_bp > 0
print('Discarded %s vals from bp_indices' % (len(sampids) - np.sum(bp_indices)))
scatter(
title = 'Support of SVs by consensus BPs for %s cancers' % len(num_sv[sv_indices]),
xtitle = 'Number of SVs',
ytitle = 'Fraction of SVs with associated consensus BP',
X = num_sv[sv_indices],
Y = frac_sv_supported_by_cbps[sv_indices],
L = [S for S, use_S in zip(sampids, sv_indices) if use_S is True],
outfn = 'sv_bp_support.scatter.html',
logx = True,
)
scatter(
title = 'Support of consensus BPs by SVs for %s cancers' % len(num_bp[bp_indices]),
xtitle = 'Number of BPs',
ytitle = 'Fraction of consensus BPs with associated SV',
X = num_bp[bp_indices],
Y = frac_cbp_supported_by_svs[bp_indices],
L = [S for S, use_S in zip(sampids, bp_indices) if use_S is True],
outfn = 'bp_sv_support.scatter.html',
logx = True,
)
pie(
title = 'Support of SVs by consensus BPs',
values = [np.sum(vals['num_bp_replaced_by_sv']), np.sum(vals['num_lone_sv'])],
labels = ['SVs supported by consensus BP', 'SVs not supported by consensus BP'],
outfn = 'sv_bp_support.pie.html',
)
pie(
title = 'Support of consensus BPs by SVs',
values = [np.sum(vals['num_bp_replaced_by_sv']), np.sum(vals['num_bp_away_from_cents_and_telos']) - np.sum(vals['num_bp_replaced_by_sv'])],
labels = ['Consensus BPs supported by SV', 'Consensus BPs not supported by SV'],
outfn = 'bp_sv_support.pie.html',
)
def main():
cnvfn = sys.argv[1]
plot(cnvfn)
main()