-
Notifications
You must be signed in to change notification settings - Fork 0
/
plot-free-energy-traj-length.py
385 lines (323 loc) · 10.8 KB
/
plot-free-energy-traj-length.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
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
import json
from pathlib import Path
import click
import numpy
import pandas
import seaborn
from matplotlib import pyplot
def _plot_pooled_free_energy(
plot_data_df,
category_labels: list[str],
output_path: str,
x_df_column: str,
y_df_column: str,
y_uncertainty_df_column: str,
figure_size: tuple[float, float],
category_df_column="Force Field",
x_label: str = "Collective variable",
y_label: str = "Free energy (kcal mol$^{-1}$)",
x_ticks: list[float] = None,
y_ticks: list[float] = None,
x_range: tuple[float, float] = None,
y_range: tuple[float, float] = None,
):
min_x = numpy.floor(plot_data_df[x_df_column].min() / 0.05) * 0.05
max_x = numpy.ceil(plot_data_df[x_df_column].max() / 0.05) * 0.05
max_y = (
numpy.ceil(
plot_data_df.loc[
plot_data_df[y_df_column] != numpy.inf,
y_df_column,
].max()
/ 0.5
)
* 0.5
)
if x_ticks is None:
x_ticks = numpy.round(numpy.arange(min_x, max_x + 0.025, 0.05), 1)
if x_range is None:
x_range = (min_x, max_x)
if y_ticks is None:
y_ticks = numpy.arange(0.0, max_y + 0.5, 1.0)
if y_range is None:
y_range = (0, max_y)
figure = pyplot.figure(figsize=figure_size)
ax = pyplot.gca()
for category_index, category_label in enumerate(category_labels):
subplot_df = plot_data_df[plot_data_df[category_df_column] == category_label]
if subplot_df.shape[0] == 0:
continue
x_data = subplot_df[x_df_column].values
y_data = subplot_df[y_df_column].values
y_uncertainty = subplot_df[y_uncertainty_df_column].values
if x_data.size == 0 or y_data.size == 0:
continue
category_color = seaborn.color_palette()[category_index % 10]
ax.plot(
x_data,
y_data,
label=category_label.replace("-OPC3", ""),
color=category_color,
)
ax.fill_between(
x_data,
y_data - y_uncertainty,
y_data + y_uncertainty,
linewidth=0,
color=category_color,
alpha=0.5,
)
ax.set_xticks(x_ticks)
pyplot.setp(ax.get_xticklabels()[0::2], visible=False)
ax.set_xlim(x_range[0], x_range[1])
ax.set_yticks(y_ticks)
pyplot.setp(ax.get_yticklabels()[1::2], visible=False)
ax.set_ylim(y_range[0], y_range[1])
ax.set_xlabel(x_label)
ax.set_ylabel(y_label)
figure.legend(loc="outside upper center", ncol=2)
pyplot.savefig(output_path)
pyplot.close(figure)
def _plot_free_energy(
plot_data_df,
row_labels: list[str],
column_labels: list[str],
output_path: str,
x_df_column: str,
y_df_column: str,
figure_size: tuple[float, float],
row_df_column="Force Field",
column_df_column="Replica",
x_label: str = "Collective variable",
y_label: str = "Free energy (kcal mol$^{-1}$)",
x_ticks: list[float] = None,
y_ticks: list[float] = None,
x_range: tuple[float, float] = None,
y_range: tuple[float, float] = None,
):
min_x = numpy.floor(plot_data_df[x_df_column].min() / 0.05) * 0.05
max_x = numpy.ceil(plot_data_df[x_df_column].max() / 0.05) * 0.05
max_y = numpy.ceil(plot_data_df[y_df_column].max() / 0.5) * 0.5
if x_ticks is None:
x_ticks = numpy.round(numpy.arange(min_x + 0.05, max_x + 0.05, 0.1), 1)
if x_range is None:
x_range = (min_x, max_x)
if y_ticks is None:
y_ticks = numpy.arange(0.0, max_y + 0.5, 1.0)
if y_range is None:
y_range = (0, max_y)
figure, axes = pyplot.subplots(
len(row_labels),
len(column_labels),
figsize=figure_size,
sharex=True,
sharey=True,
squeeze=False,
)
for row_index, row_label in enumerate(row_labels):
for column_index, column_label in enumerate(column_labels):
ax = axes[row_index, column_index]
subplot_df = plot_data_df[
(plot_data_df[row_df_column] == row_label)
& (plot_data_df[column_df_column] == column_label)
]
if subplot_df.shape[0] == 0:
continue
x_data = subplot_df[x_df_column].values
y_data = subplot_df[y_df_column].values
if x_data.size == 0 or y_data.size == 0:
continue
ax.plot(
x_data,
y_data,
color=seaborn.color_palette()[row_index],
)
ax.set_xticks(x_ticks)
pyplot.setp(ax.get_xticklabels()[1::2], visible=False)
ax.set_xlim(x_range[0], x_range[1])
ax.set_yticks(y_ticks)
pyplot.setp(ax.get_yticklabels()[1::2], visible=False)
ax.set_ylim(y_range[0], y_range[1])
if "ff14SB" in row_label:
ax.set_ylabel(row_label.replace("-", "\n", 1))
elif "TIP3P-FB" in row_label:
a = row_label.split("-")
ax.set_ylabel(
"-".join(a[:1]) + "\n" + "-".join(a[1:-2]) + "\n" + "-".join(a[-2:])
)
else:
a = row_label.split("-")
ax.set_ylabel(
"-".join(a[:1]) + "\n" + "-".join(a[1:-1]) + "\n" + "-".join(a[-1:])
)
for ax in axes.flat:
ax.label_outer()
figure.supxlabel(x_label)
figure.supylabel(y_label)
# figure.legend(loc="outside upper center", ncol=2)
pyplot.savefig(output_path)
pyplot.close(figure)
@click.command()
@click.option(
"-d/-l",
"--dark_background/--light_background",
default=True,
help="Use the pyplot `dark_background` style.",
)
@click.option(
"-e",
"--extension",
type=click.STRING,
default="pdf",
show_default=True,
help="File extension for output plots.",
)
@click.option(
"-f",
"--figure_width",
type=click.FLOAT,
default=4.25,
show_default=True,
help="Width of plots in inches.",
)
@click.option(
"-h",
"--figure_height",
type=click.FLOAT,
default=None,
show_default=True,
help="Height of plots in inches. Default is 0.75 times figure_width.",
)
@click.option(
"-i",
"--input_dir",
type=click.STRING,
default="results",
show_default=True,
help="Directory path containing benchmark results.",
)
@click.option(
"-o",
"--output_dir",
type=click.STRING,
default="plots",
show_default=True,
help="Directory path to which plots should be written.",
)
@click.option(
"-s",
"--font_size",
type=click.INT,
default=None,
show_default=True,
help="Font size in pt. Default is matplotlib rcParams.",
)
def main(
dark_background,
extension,
figure_width,
figure_height,
input_dir,
output_dir,
font_size,
):
if dark_background:
pyplot.style.use("dark_background")
# Reorder seaborn colorblind palette to avoid similar orange and red hues
seaborn.set_palette(
seaborn.color_palette(
[
seaborn.color_palette("colorblind")[i]
for i in [0, 1, 2, 4, 8, 9, 7, 5, 6, 3]
# for i in [0, 4, 8, 9, 7, 5, 6, 3]
]
)
)
if figure_height is None:
figure_size = tuple(figure_width * x for x in (1, 0.75))
else:
figure_size = (figure_width, figure_height)
if font_size is not None:
pyplot.rcParams.update({"font.size": font_size})
N_replicas = 3
N_windows = 31
replicas = [str(replica) for replica in numpy.arange(1, N_replicas + 1)]
windows = [f"{i:02d}" for i in range(N_windows)]
traj_lengths = [500, 400, 300, 200, 100]
for output_prefix in [
#"gb3-ff14sb-opc3",
"gb3-null-0.0.3-pair-opc3",
#"gb3-specific-0.0.3-pair-opc3",
#"gb3-specific-0.0.3-sage-pair-opc3",
]:
if output_prefix == "gb3-ff14sb-opc3":
ff_labels = {
"ff14SB-OPC3": "ff14sb-opc3",
# "Null-0.0.3-Pair-OPC3": "null-0.0.3-pair-opc3",
# "Specific-0.0.3-SPair-OPC3": "specific-0.0.3-sage-pair-opc3",
}
target_labels = {
"GB3": "gb3",
}
elif output_prefix == "gb3-null-0.0.3-pair-opc3":
ff_labels = {
"Null-0.0.3-Pair-OPC3": "null-0.0.3-pair-opc3",
}
target_labels = {
"GB3": "gb3",
}
elif output_prefix == "gb3-specific-0.0.3-pair-opc3":
ff_labels = {
"Specific-0.0.3-Pair-OPC3": "specific-0.0.3-pair-opc3",
}
target_labels = {
"GB3": "gb3",
}
elif output_prefix == "gb3-specific-0.0.3-sage-pair-opc3":
ff_labels = {
"Specific-0.0.3-SPair-OPC3": "specific-0.0.3-sage-pair-opc3",
}
target_labels = {
"GB3": "gb3",
}
free_energy_df = pandas.DataFrame()
for target_label, target in target_labels.items():
for ff_label, force_field in ff_labels.items():
for traj_length in traj_lengths:
mbar_free_energy_path = Path(
input_dir,
f"{target}-{force_field}",
"analysis",
f"{target}-{force_field}-mbar-last-{traj_length}ns-free-energy.dat",
)
mbar_df = pandas.read_csv(
mbar_free_energy_path,
index_col=0,
header=0,
names=[
"Fraction Native Contacts",
"Free Energy (kcal mol^-1)",
"Free Energy Uncertainty (kcal mol^-1)",
],
)
mbar_df["Force Field"] = f"{ff_label} {traj_length} ns"
free_energy_df = pandas.concat([free_energy_df, mbar_df])
free_energy_df.to_csv(
Path(output_dir, f"{output_prefix}-free-energy-traj-length.dat"),
)
plot_ff_labels = free_energy_df["Force Field"].unique()
_plot_pooled_free_energy(
plot_data_df=free_energy_df,
category_labels=plot_ff_labels,
output_path=Path(
output_dir,
f"{output_prefix}-pooled-free-energy-last-traj-length.{extension}",
),
x_df_column="Fraction Native Contacts",
y_df_column="Free Energy (kcal mol^-1)",
y_uncertainty_df_column="Free Energy Uncertainty (kcal mol^-1)",
figure_size=figure_size,
x_label="Fraction native contacts",
)
if __name__ == "__main__":
main()