-
Notifications
You must be signed in to change notification settings - Fork 0
/
plot-folding-free-energy.py
279 lines (236 loc) · 8.42 KB
/
plot-folding-free-energy.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
import json
from pathlib import Path
import click
import numpy
from openmm import unit
import pandas
from proteinbenchmark import benchmark_targets
import seaborn
from matplotlib import pyplot
def _plot_folding_free_energy(
folding_cv: dict[numpy.typing.ArrayLike],
weights: dict[numpy.typing.ArrayLike],
RT: float,
output_path: str,
figure_size: tuple[float, float],
x_label: str = "Folded state cutoff",
y_label: str = "Folding 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 = max([x.min() for x in folding_cv.values()])
max_x = min([x.max() for x in folding_cv.values()])
min_x = (numpy.floor(min_x / 0.05) + 1) * 0.05
max_x = (numpy.ceil(max_x / 0.05) - 1) * 0.05
cutoff_values = numpy.arange(min_x, max_x + 0.001, 0.002)
min_y, max_y = 0.0, 0.0
figure = pyplot.figure(figsize=figure_size)
ax = pyplot.gca()
for category_index, category_label in enumerate(folding_cv.keys()):
folding_free_energy = numpy.zeros(cutoff_values.size)
for cutoff_index, cutoff_value in enumerate(cutoff_values):
folded_samples = folding_cv[category_label] > cutoff_value
folded_probability = weights[category_label][folded_samples].sum()
unfolded_probability = weights[category_label][~folded_samples].sum()
folding_free_energy[cutoff_index] = (
-RT * numpy.log(folded_probability / unfolded_probability)
)
min_y = min(min_y, folding_free_energy.min())
max_y = max(max_y, folding_free_energy.max())
category_color = seaborn.color_palette()[category_index % 10]
ax.plot(
cutoff_values,
folding_free_energy,
label=category_label.replace("-OPC3", ""),
linestyle="-",
color=category_color,
)
min_y = numpy.floor(min_y / 2) * 2
max_y = numpy.ceil(max_y / 2) * 2
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(min_y, max_y + 0.5, 1.0)
if y_range is None:
y_range = (min_y, max_y)
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)
@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]
]
)
)
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
replicas = [str(replica) for replica in numpy.arange(1, N_replicas + 1)]
for output_prefix in [
#"gb3-opc3",
"gb3-nmr-opc3",
#"gb3-nmr-0.8-opc3",
]:
if output_prefix == "gb3-opc3":
ff_labels = {
"ff14SB-OPC3": "ff14sb-opc3",
"ff14SBonlysc-OPC3": "ff14sbonlysc-opc3",
"Null-0.0.3-Pair-OPC3": "null-0.0.3-pair-opc3",
"Specific-0.0.3-Pair-OPC3": "specific-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-nmr-opc3":
ff_labels = {
"ff14SB-OPC3": "ff14sb-opc3",
"Null-QM-OPC3": "null-0.0.3-pair-opc3",
"Null-NMR-0.8-1E4-OPC3": "null-0.0.3-pair-nmr-0.8-1e4-opc3",
"Null-NMR-0.8-1E4-2-OPC3": "null-0.0.3-pair-nmr-0.8-1e4-2-opc3",
"Null-NMR-0.8-1E4-3-OPC3": "null-0.0.3-pair-nmr-0.8-1e4-3-opc3",
"Null-NMR-0.8-1E4-4-OPC3": "null-0.0.3-pair-nmr-0.8-1e4-3-opc3",
#"Null-NMR-1E5-OPC3": "null-0.0.3-pair-nmr-1e5-opc3",
#"Null-NMR-1E5-2-OPC3": "null-0.0.3-pair-nmr-1e5-2-opc3",
#"Null-NMR-1E5-3-OPC3": "null-0.0.3-pair-nmr-1e5-3-opc3",
#"Null-NMR-1E4-OPC3": "null-0.0.3-pair-nmr-1e4-opc3",
#"Null-NMR-1E3-OPC3": "null-0.0.3-pair-nmr-1e3-opc3",
#"Null-NMR-1E2-OPC3": "null-0.0.3-pair-nmr-1e2-opc3",
#"Null-Gen-1E5-OPC3": "null-0.0.3-pair-general-nmr-1e5-opc3",
}
target_labels = {
"GB3": "gb3",
}
elif output_prefix == "gb3-nmr-0.8-opc3":
ff_labels = {
"ff14SB-OPC3": "ff14sb-opc3",
"Null-QM-OPC3": "null-0.0.3-pair-opc3",
"Null-NMR-0.8-1E4-OPC3": "null-0.0.3-pair-nmr-0.8-1e4-opc3",
"Null-NMR-0.8-1E4-2-OPC3": "null-0.0.3-pair-nmr-0.8-1e4-2-opc3",
"Null-NMR-0.8-1E4-3-OPC3": "null-0.0.3-pair-nmr-0.8-1e4-3-opc3",
}
target_labels = {
"GB3": "gb3",
}
fraction_native_contacts = dict()
mbar_weights = dict()
for target_label, target in target_labels.items():
temperature = benchmark_targets[target]["temperature"]
RT = unit.MOLAR_GAS_CONSTANT_R * temperature.to_openmm()
RT = RT.value_in_unit(unit.kilocalorie_per_mole)
for ff_label, force_field in ff_labels.items():
if output_prefix.startswith("gb3-nmr-0.8"):
mbar_str = "mbar-0.8"
else:
mbar_str = "mbar"
mbar_samples_path = Path(
input_dir,
f"{target}-{force_field}",
"analysis",
f"{target}-{force_field}-{mbar_str}-samples.dat",
)
mbar_df = pandas.read_csv(
mbar_samples_path,
index_col=0,
)
fraction_native_contacts[ff_label] = mbar_df["Fraction Native Contacts"].values
unnormalized_weights = 1.0 / mbar_df["MBAR Weight Denominator"].values
mbar_weights[ff_label] = unnormalized_weights / unnormalized_weights.sum()
_plot_folding_free_energy(
folding_cv=fraction_native_contacts,
weights=mbar_weights,
RT=RT,
output_path=Path(
output_dir,
f"{output_prefix}-folding-free-energy.{extension}",
),
figure_size=figure_size,
x_label="Folded state cutoff",
)
if __name__ == "__main__":
main()