| 1 |
""" |
| 2 |
Example illustrating the application of MBAR to compute a 1D free energy profile from an umbrella sampling simulation. |
| 3 |
|
| 4 |
The data represents an umbrella sampling simulation for the chi torsion of |
| 5 |
a valine sidechain in lysozyme L99A with benzene bound in the cavity. |
| 6 |
|
| 7 |
Reference: |
| 8 |
|
| 9 |
D. L. Mobley, A. P. Graves, J. D. Chodera, A. C. McReynolds, B. K. Shoichet and K. A. Dill, |
| 10 |
"Predicting absolute ligand binding free energies to a simple model site," |
| 11 |
Journal of Molecular Biology 371(4):1118-1134 (2007). |
| 12 |
http://dx.doi.org/10.1016/j.jmb.2007.06.002 |
| 13 |
|
| 14 |
|
| 15 |
""" |
| 16 |
|
| 17 |
import numpy as np |
| 18 |
|
| 19 |
import pymbar # multistate Bennett acceptance ratio |
| 20 |
from pymbar import timeseries # timeseries analysis |
| 21 |
|
| 22 |
# Constants. |
| 23 |
kB = 1.381e-23 * 6.022e23 / 1000.0 # Boltzmann constant in kJ/mol/K |
| 24 |
|
| 25 |
temperature = 300 # assume a single temperature -- can be overridden with data from center.dat |
| 26 |
|
| 27 |
# Parameters |
| 28 |
K = 26 # number of umbrellas |
| 29 |
N_max = 501 # maximum number of snapshots/simulation |
| 30 |
T_k = np.ones(K, float) * temperature # inital temperatures are all equal |
| 31 |
beta = 1.0 / (kB * temperature) # inverse temperature of simulations (in 1/(kJ/mol)) |
| 32 |
chi_min = -180.0 # min for free energy profile |
| 33 |
chi_max = +180.0 # max for free energy profile |
| 34 |
nbins = 36 # number of bins for 1D free energy profile |
| 35 |
|
| 36 |
# Allocate storage for simulation data |
| 37 |
# N_k[k] is the number of snapshots from umbrella simulation k |
| 38 |
N_k = np.zeros([K], dtype=int) |
| 39 |
# K_k[k] is the spring constant (in kJ/mol/deg**2) for umbrella simulation k |
| 40 |
K_k = np.zeros([K]) |
| 41 |
# chi0_k[k] is the spring center location (in deg) for umbrella simulation k |
| 42 |
chi0_k = np.zeros([K]) |
| 43 |
# chi_kn[k,n] is the torsion angle (in deg) for snapshot n from umbrella simulation k |
| 44 |
chi_kn = np.zeros([K, N_max]) |
| 45 |
# u_kn[k,n] is the reduced potential energy without umbrella restraints of snapshot n of umbrella simulation k |
| 46 |
u_kn = np.zeros([K, N_max]) |
| 47 |
g_k = np.zeros([K]) |
| 48 |
|
| 49 |
# Read in umbrella spring constants and centers. |
| 50 |
with open("data/centers.dat") as infile: |
| 51 |
lines = infile.readlines() |
| 52 |
|
| 53 |
for k in range(K): |
| 54 |
# Parse line k. |
| 55 |
line = lines[k] |
| 56 |
tokens = line.split() |
| 57 |
chi0_k[k] = float(tokens[0]) # spring center location (in deg) |
| 58 |
# spring constant (read in kJ/mol/rad**2, converted to kJ/mol/deg**2) |
| 59 |
K_k[k] = float(tokens[1]) * (np.pi / 180) ** 2 |
| 60 |
if len(tokens) > 2: |
| 61 |
T_k[k] = float(tokens[2]) # temperature the kth simulation was run at. |
| 62 |
|
| 63 |
beta_k = 1.0 / (kB * T_k) # beta factor for the different temperatures |
| 64 |
different_temperatures = True |
| 65 |
if min(T_k) == max(T_k): |
| 66 |
# if all the temperatures are the same, then we don't have to read in energies. |
| 67 |
different_temperatures = False |
| 68 |
|
| 69 |
# Read the simulation data |
| 70 |
for k in range(K): |
| 71 |
# Read torsion angle data. |
| 72 |
filename = f"data/prod{k:d}_dihed.xvg" |
| 73 |
print(f"Reading {filename}...") |
| 74 |
n = 0 |
| 75 |
with open(filename, "r") as infile: |
| 76 |
for line in infile: |
| 77 |
if line[0] != "#" and line[0] != "@": |
| 78 |
tokens = line.split() |
| 79 |
chi = float(tokens[1]) # torsion angle |
| 80 |
# wrap chi_kn to be within [-180,+180) |
| 81 |
while chi < -180.0: |
| 82 |
chi += 360.0 |
| 83 |
while chi >= +180.0: |
| 84 |
chi -= 360.0 |
| 85 |
chi_kn[k, n] = chi |
| 86 |
n += 1 |
| 87 |
N_k[k] = n |
| 88 |
|
| 89 |
if different_temperatures: # if different temperatures are specified the metadata file, |
| 90 |
# then we need the energies to compute the free energy profile |
| 91 |
# Read energies |
| 92 |
filename = f"data/prod{k:d}_energies.xvg" |
| 93 |
print(f"Reading {filename}...") |
| 94 |
n = 0 |
| 95 |
with open(filename, "r") as infile: |
| 96 |
for line in infile: |
| 97 |
if line[0] != "#" and line[0] != "@": |
| 98 |
tokens = line.split() |
| 99 |
# reduced potential energy without umbrella restraint |
| 100 |
u_kn[k, n] = beta_k[k] * (float(tokens[2]) - float(tokens[1])) |
| 101 |
n += 1 |
| 102 |
|
| 103 |
# Compute correlation times for potential energy and chi |
| 104 |
# timeseries. If the temperatures differ, use energies to determine samples; otherwise, use the cosine of chi |
| 105 |
|
| 106 |
if different_temperatures: |
| 107 |
g_k[k] = timeseries.statistical_inefficiency(u_kn[k, :], u_kn[k, 0 : N_k[k]]) |
| 108 |
print(f"Correlation time for set {k:5d} is {g_k[k]:10.3f}") |
| 109 |
indices = timeseries.subsample_correlated_data(u_kn[k, 0 : N_k[k]]) |
| 110 |
else: |
| 111 |
chi_radians = chi_kn[k, 0 : N_k[k]] / (180.0 / np.pi) |
| 112 |
g_cos = timeseries.statistical_inefficiency(np.cos(chi_radians)) |
| 113 |
g_sin = timeseries.statistical_inefficiency(np.sin(chi_radians)) |
| 114 |
print(f"g_cos = {g_cos:.1f} | g_sin = {g_sin:.1f}") |
| 115 |
g_k[k] = max(g_cos, g_sin) |
| 116 |
print(f"Correlation time for set {k:5d} is {g_k[k]:10.3f}") |
| 117 |
indices = timeseries.subsample_correlated_data(chi_radians, g=g_k[k]) |
| 118 |
# Subsample data. |
| 119 |
N_k[k] = len(indices) |
| 120 |
u_kn[k, 0 : N_k[k]] = u_kn[k, indices] |
| 121 |
chi_kn[k, 0 : N_k[k]] = chi_kn[k, indices] |
| 122 |
|
| 123 |
N_max = np.max(N_k) # shorten the array size |
| 124 |
# u_kln[k,l,n] is the reduced potential energy of snapshot n from umbrella simulation k evaluated at umbrella l |
| 125 |
u_kln = np.zeros([K, K, N_max]) |
| 126 |
|
| 127 |
# Set zero of u_kn -- this is arbitrary. |
| 128 |
u_kn -= u_kn.min() |
| 129 |
|
| 130 |
# compute bin centers |
| 131 |
bin_center_i = np.zeros([nbins]) |
| 132 |
bin_edges = np.linspace(chi_min, chi_max, nbins + 1) |
| 133 |
for i in range(nbins): |
| 134 |
bin_center_i[i] = 0.5 * (bin_edges[i] + bin_edges[i + 1]) |
| 135 |
|
| 136 |
N = np.sum(N_k) |
| 137 |
chi_n = pymbar.utils.kn_to_n(chi_kn, N_k=N_k) |
| 138 |
|
| 139 |
# Evaluate reduced energies in all umbrellas |
| 140 |
print("Evaluating reduced potential energies...") |
| 141 |
for k in range(K): |
| 142 |
for n in range(N_k[k]): |
| 143 |
# Compute minimum-image torsion deviation from umbrella center l |
| 144 |
dchi = chi_kn[k, n] - chi0_k |
| 145 |
for l in range(K): |
| 146 |
if abs(dchi[l]) > 180.0: |
| 147 |
dchi[l] = 360.0 - abs(dchi[l]) |
| 148 |
|
| 149 |
# Compute energy of snapshot n from simulation k in umbrella potential l |
| 150 |
u_kln[k, :, n] = u_kn[k, n] + beta_k[k] * (K_k / 2.0) * dchi**2 |
| 151 |
|
| 152 |
# initialize free energy profile with the data collected |
| 153 |
fes = pymbar.FES(u_kln, N_k, verbose=True) |
| 154 |
# Compute free energy profile in unbiased potential (in units of kT). |
| 155 |
histogram_parameters = {} |
| 156 |
histogram_parameters["bin_edges"] = bin_edges |
| 157 |
fes.generate_fes(u_kn, chi_n, fes_type="histogram", histogram_parameters=histogram_parameters) |
| 158 |
results = fes.get_fes(bin_center_i, reference_point="from-lowest", uncertainty_method="analytical") |
| 159 |
center_f_i = results["f_i"] |
| 160 |
center_df_i = results["df_i"] |
| 161 |
|
| 162 |
# Write out free energy profile |
| 163 |
print("free energy profile (in units of kT), from histogramming") |
| 164 |
print(f"{'bin':>8s} {'f':>8s} {'df':>8s}") |
| 165 |
for i in range(nbins): |
| 166 |
print(f"{bin_center_i[i]:8.1f} {center_f_i[i]:8.3f} {center_df_i[i]:8.3f}") |
| 167 |
|
| 168 |
# NOW KDE: |
| 169 |
kde_parameters = {} |
| 170 |
kde_parameters["bandwidth"] = 0.5 * ((chi_max - chi_min) / nbins) |
| 171 |
fes.generate_fes(u_kn, chi_n, fes_type="kde", kde_parameters=kde_parameters) |
| 172 |
results = fes.get_fes(bin_center_i, reference_point="from-lowest") |
| 173 |
# Write out free energy profile from KDE |
| 174 |
center_f_i = results["f_i"] |
| 175 |
print("") |
| 176 |
print("free energy profile (in units of kT), from KDE") |
| 177 |
print(f"{'bin':>8s} {'f':>8s}") |
| 178 |
for i in range(nbins): |
| 179 |
print(f"{bin_center_i[i]:8.1f} {center_f_i[i]:8.3f}") |