pareto_visualizer.py
14 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
#!/usr/bin/python3
# Created by Louis Becquey, louis.becquey@univ-evry.fr, Oct 2019
# This script processes files containing RNA structures obtained from bi-objective
# optimization programs, and a dot-bracket database of reference structures, to plot
# where are the best solutions in the Pareto set.
#
# The result files should follow this kind of format:
# for Biokop: (option --biokop)
# Structure Free energy score Expected accuracy score
# (((...(((...)))))) <tab> obj1_value <tab> obj2_value
# (((............))) <tab> obj1_value <tab> obj2_value
# ((((((...)))...))) <tab> obj1_value <tab> obj2_value
# ...
#
# for BiORSEO: (options --biorseo_**stuff**)
# >Header of the sequence
# GGCACAGAGUUAUGUGCC
# (((...(((...)))))) + Motif1 + Motif2 <tab> obj1_value <tab> obj2_value
# (((............))) <tab> obj1_value <tab> obj2_value
# ((((((...)))...))) + Motif1 <tab> obj1_value <tab> obj2_value
#
# typical Biokop usage:
# python3 pareto_visualizer.py --biokop --folder path/to/your/results/folder --database path/to/the/database_file.dbn
# typical Biorseo usage:
# python3 pareto_visualizer.py --folder path/to/your/results/folder --database path/to/the/database_file.dbn
#
from math import sqrt
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import cm
import scipy.stats as st
import sys
import os
import subprocess
import getopt
class SecStruct:
def __init__(self, dot_bracket, obj1_value, obj2_value):
self.dbn = dot_bracket
self.objectives = [obj1_value, obj2_value]
self.basepair_list = self.get_basepairs()
self.length = len(dot_bracket)
def get_basepairs(self):
parenthesis = []
brackets = []
braces = []
rafters = []
basepairs = []
As = []
Bs = []
for i, c in enumerate(self.dbn):
if c == '(':
parenthesis.append(i)
if c == '[':
brackets.append(i)
if c == '{':
braces.append(i)
if c == '<':
rafters.append(i)
if c == 'A':
As.append(i)
if c == 'B':
Bs.append(i)
if c == '.':
continue
if c == ')':
basepairs.append((i, parenthesis.pop()))
if c == ']':
basepairs.append((i, brackets.pop()))
if c == '}':
basepairs.append((i, braces.pop()))
if c == '>':
basepairs.append((i, rafters.pop()))
if c == 'a':
basepairs.append((i, As.pop()))
if c == 'b':
basepairs.append((i, Bs.pop()))
return basepairs
def get_MCC_with(self, reference_structure):
# Get true and false positives and negatives
tp = 0
fp = 0
tn = 0
fn = 0
for bp in reference_structure.basepair_list:
if bp in self.basepair_list:
tp += 1
else:
fn += 1
for bp in self.basepair_list:
if bp not in reference_structure.basepair_list:
fp += 1
tn = reference_structure.length * (reference_structure.length - 1) * 0.5 - fp - fn - tp
# Compute MCC
if (tp + fp == 0):
print("We have an issue : no positives detected ! (linear structure)")
return (tp * tn - fp * fn) / sqrt((tp + fp) * (tp + fn) * (tn + fp) * (tn + fn))
class Pareto:
def __init__(self, list_of_structs, reference):
self.predictions = list_of_structs
self.true_structure = reference
self.n_pred = len(list_of_structs)
self.max_obj1 = max([s.objectives[0] for s in self.predictions])
self.max_obj2 = max([s.objectives[1] for s in self.predictions])
self.index_of_best = self.find_best_solution()
def find_best_solution(self):
# returns the index of the solution of the Pareto set which is the closest
# to the real 2D structure (the one with the max MCC)
max_i = -1
max_mcc = -1
for i, s in enumerate(self.predictions):
mcc = s.get_MCC_with(self.true_structure)
if mcc > max_mcc:
max_mcc = mcc
max_i = i
return max_i
def get_normalized_coords(self):
# retrieves the objective values of the best solution and normlizes them
coords = self.predictions[self.index_of_best].objectives
if self.max_obj1: # avoid divide by zero if all solutions are 0
x = coords[0] / self.max_obj1
else:
x = 0.5
if self.max_obj2: # avoid divide by zero if all solutions are 0
y = coords[1] / self.max_obj2
else:
y = 0.5
return (x, y)
class RNA:
def __init__(self, filename, header, seq, struct):
self.seq_ = seq
self.header_ = header
self.struct_ = struct
self.basename_ = filename
ignored_nt_dict = {}
def is_canonical_nts(seq):
for c in seq[:-1]:
if c not in "ACGU":
if c in ignored_nt_dict.keys():
ignored_nt_dict[c] += 1
else:
ignored_nt_dict[c] = 1
return False
return True
def is_canonical_bps(struct):
if "()" in struct:
return False
if "(.)" in struct:
return False
if "(..)" in struct:
return False
if "[]" in struct:
return False
if "[.]" in struct:
return False
if "[..]" in struct:
return False
return True
def load_from_dbn(file, header_style=3):
container = []
pkcounter = 0
db = open(file, "r")
c = 0
header = ""
seq = ""
struct = ""
while True:
l = db.readline()
if l == "":
break
c += 1
c = c % 3
if c == 1:
header = l[:-1]
if c == 2:
seq = l[:-1].upper()
if c == 0:
struct = l[:-1]
n = len(seq)
if n < 10 or n > 100:
continue # ignore too short and too long RNAs
if is_canonical_nts(seq) and is_canonical_bps(struct) and '(' in struct:
if header_style == 1: container.append(RNA(header.replace('/', '_').split('(')[-1][:-1], header, seq, struct))
if header_style == 2: container.append(RNA(header.replace('/', '_').split('[')[-1][:-41], header, seq, struct))
if header_style == 3: container.append(RNA(header[1:], header, seq, struct))
if '[' in struct: pkcounter += 1
db.close()
return container, pkcounter
def parse_biokop(folder, basename, ext=".biok"):
solutions = []
err = 0
if os.path.isfile(os.path.join(folder, basename + ext)):
rna = open(os.path.join(folder, basename + ext), "r")
lines = rna.readlines()
rna.close()
different_2ds = []
for s in lines[1:]:
if s == '\n':
continue
splitted = s.split('\t')
db2d = splitted[0]
if db2d not in different_2ds:
different_2ds.append(db2d)
# here is a negative sign because Biokop actually minimizes -MEA instead
# of maximizing MEA : we switch back to MEA
solutions.append(SecStruct(db2d, -float(splitted[2][:-1]), -float(splitted[1]))) # MEA first, MFE second
# check the range of MEA in this pareto set
min_mea = solutions[0].objectives[0]
max_mea = min_mea
for s in solutions:
mea = s.objectives[0]
if mea < min_mea:
min_mea = mea
if mea > max_mea:
max_mea = mea
# normalize so the minimum MEA of the set is 0
for i in range(len(solutions)):
solutions[i].objectives[0] -= min_mea
if len(different_2ds) > 1:
return solutions, err
else:
print("[%s] \033[36mWARNING: ignoring this RNA, only one 2D solution is found.\033[0m" % (basename))
err = 1
return None, err
def parse_biorseo(folder, basename, ext):
solutions = []
err = 0
if os.path.isfile(os.path.join(folder, basename + ext)):
rna = open(os.path.join(folder, basename + ext), "r")
lines = rna.readlines()
rna.close()
different_2ds = []
for s in lines[2:]:
if s == '\n':
continue
splitted = s.split('\t')
db2d = splitted[0].split(' ')[0]
if db2d not in different_2ds:
different_2ds.append(db2d)
solutions.append(SecStruct(db2d, float(splitted[2][:-1]), float(splitted[1]))) # put MEA first, modules in 2nd (y axis)
if len(different_2ds) > 1:
return solutions, err
else:
print("[%s] \033[36mWARNING: ignoring this RNA, only one 2D solution is found.\033[0m" % (basename))
err = 1
return None, err
def prettify_biorseo(code):
name = ""
if "json" in code:
name += "JSON motifs + "
elif "rin" in code:
name += "CaRNAval + "
else:
name += "Rna3Dmotifs + "
# name += " + $f_{1" + code[-1] + "}$"
return name
def process_extension(ax, pos, ext, nsolutions=False, xlabel="Best solution performs\nwell on obj1", ylabel="Best solution performs\n well on obj2"):
points = []
sizes = []
skipped = 0
for rna in RNAcontainer:
# Extracting the predictions from the results file
solutions, err = parse(results_folder, rna.basename_, ext)
if solutions is None:
if err == 0:
skipped += 1
continue
reference = SecStruct(rna.struct_, float("inf"), float("inf"))
pset = Pareto(solutions, reference)
points.append(pset.get_normalized_coords())
sizes.append(pset.n_pred)
print("[%s] Loaded %d solutions in a Pareto set, max(obj1)=%f, max(obj2)=%f" % (rna.basename_, pset.n_pred, pset.max_obj1, pset.max_obj2))
print("Loaded %d points on %d." % (len(points), len(RNAcontainer)-skipped))
x = np.array([p[0] for p in points])
y = np.array([p[1] for p in points])
xmin, xmax = 0, 1
ymin, ymax = 0, 1
xx, yy = np.mgrid[xmin:xmax:100j, ymin:ymax:100j]
positions = np.vstack([xx.ravel(), yy.ravel()])
values = np.vstack([x, y])
kernel = st.gaussian_kde(values)
f = np.reshape(kernel(positions).T, xx.shape)
ax[pos].axhline(y=0, alpha=0.2, color='black')
ax[pos].axhline(y=1, alpha=0.2, color='black')
ax[pos].axvline(x=0, alpha=0.2, color='black')
ax[pos].axvline(x=1, alpha=0.2, color='black')
ax[pos].contourf(xx, yy, f, cmap=cm.Blues, alpha=0.5)
ax[pos].scatter(x, y, s=25, alpha=0.1)
ax[pos].set_xlim((-0.1, 1.1))
ax[pos].set_ylim((-0.1, 1.1))
ax[pos].set_title(prettify_biorseo(ext[1:]), fontsize=10)
ax[pos].annotate("(" + str(len(points)) + '/' + str(len(RNAcontainer)-skipped) + " RNAs)", (0.08, 0.15))
ax[pos].set_xlabel(xlabel)
ax[pos].set_ylabel(ylabel)
if nsolutions:
ax[pos + 1].hist(sizes, bins=range(0, max(sizes) + 1, 2), histtype='bar')
ax[pos + 1].set_xlim((0, max(sizes) + 2))
ax[pos + 1].set_xticks(range(0, max(sizes), 10))
ax[pos + 1].set_xticklabels(range(0, max(sizes), 10), rotation=90)
ax[pos + 1].set_xlabel("# solutions")
ax[pos + 1].set_ylabel("# RNAs")
if __name__ == "__main__":
try:
opts, args = getopt.getopt( sys.argv[1:], "",
[ "biorseo_desc_A", "biorseo_desc_B",
"biorseo_rin_A", "biorseo_rin_B",
"biorseo_json_A", "biorseo_json_B",
"biokop", "folder=", "database=", "output="
])
except getopt.GetoptError as err:
print(err)
sys.exit(2)
results_folder = "."
extension = "all"
outputf = ""
for opt, arg in opts:
if opt == "--biokop":
extension = ".biok"
parse = parse_biokop
elif opt == "--folder":
results_folder = arg
elif opt == "--database":
database = arg
elif opt == "--output":
outputf = arg
else:
extension = '.' + opt[2:]
parse = parse_biorseo
RNAcontainer, _ = load_from_dbn(database)
if results_folder[-1] != '/':
results_folder = results_folder + '/'
if outputf == "":
outputf = results_folder
if outputf[-1] != '/':
outputf = outputf + '/'
if extension == "all":
parse = parse_biorseo
fig, ax = plt.subplots(2,3,figsize=(8,10), sharex=True, sharey=True)
ax = ax.flatten()
process_extension(ax, 0, ".biorseo_desc_A", ylabel="Normalized $f_{1A}$", xlabel="Normalized MEA")
process_extension(ax, 1, ".biorseo_rin_A", ylabel="Normalized $f_{1A}$", xlabel="Normalized MEA")
process_extension(ax, 2, ".biorseo_json_A", ylabel="Normalized $f_{1A}$", xlabel="Normalized MEA")
ax[0].set_title(prettify_biorseo("biorseo_desc_A"), fontsize=10)
ax[1].set_title(prettify_biorseo("biorseo_rin_A"), fontsize=10)
ax[2].set_title(prettify_biorseo("biorseo_json_A"), fontsize=10)
process_extension(ax, 3, ".biorseo_desc_B", ylabel="Normalized $f_{1B}$", xlabel="Normalized MEA")
process_extension(ax, 4, ".biorseo_rin_B", ylabel="Normalized $f_{1B}$", xlabel="Normalized MEA")
process_extension(ax, 5, ".biorseo_json_B", ylabel="Normalized $f_{1B}$", xlabel="Normalized MEA")
for a in ax:
a.label_outer()
plt.subplots_adjust(bottom=0.05, top=0.95, left=0.07, right=0.98, hspace=0.1, wspace = 0.05)
plt.savefig("pareto_visualizer.png")
else:
fig, ax = plt.subplots(2,1, figsize=(6,10))
plt.subplots_adjust(bottom=0.12, top=0.9, left=0.15, right=0.9, hspace=0.4)
if extension == ".biok":
process_extension(ax, 0, extension, nsolutions=True, ylabel="Normalized MFE", xlabel="Normalized MEA")
else:
process_extension(ax, 0, extension, nsolutions=False)
plt.savefig("pareto_visualizer_ext.png")