Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update analyse_freenrg to use pymbar 4 #91

Merged
merged 3 commits into from
Aug 7, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions doc/source/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ organisation on `GitHub <https://github.com/openbiosim/sire>`__.
* Make sure atom serial number in PDB files are capped when renumbering when
TER records are present.

* Updated ``FreeEnergyAnalysis.py`` to use the new pymbar 4 API.

* Please add the changelog entry for your PR here. We will add the link to your PR
during the code review :-)

Expand Down
2 changes: 1 addition & 1 deletion requirements_bss.txt
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ rdkit>=2023.0.0
# The below are packages that aren't available on all
# platforms/OSs and so need to be conditionally included

alchemlyb<2 ; platform_machine != "aarch64" # Needs pymbar, not on Linux/aarch64
alchemlyb ; platform_machine != "aarch64" # Needs pymbar, not on Linux/aarch64

mdtraj ; platform_machine != "aarch64" # not on Linux/aarch64

Expand Down
29 changes: 12 additions & 17 deletions wrapper/Tools/FreeEnergyAnalysis.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
try:
# This should force the installation of pymbar if it isn't
# installed already
_pymbar = Sire.try_import("pymbar", version="<4")
_pymbar = Sire.try_import("pymbar", version=">=4")

from pymbar import MBAR
from pymbar import timeseries
Expand Down Expand Up @@ -105,21 +105,16 @@ def run_mbar(self, test_overlap=True):
try:
MBAR_obj = MBAR(self._u_kln, self._N_k, verbose=True)
self._f_k = MBAR_obj.f_k
(
deltaF_ij,
dDeltaF_ij,
theta_ij,
) = MBAR_obj.getFreeEnergyDifferences(return_theta=True)
results = MBAR_obj.compute_free_energy_differences()
deltaF_ij = results["Delta_f"]
dDeltaF_ij = results["dDelta_f"]
except:
solver_options = {"maximum_iterations": 10000, "verbose": True}
solver_protocol = {"method": "BFGS", "options": solver_options}
MBAR_obj = MBAR(self._u_kln, self._N_k, solver_protocol=(solver_protocol,))
self._f_k = MBAR_obj.f_k
(
deltaF_ij,
dDeltaF_ij,
theta_ij,
) = MBAR_obj.getFreeEnergyDifferences(return_theta=True)
results = MBAR_obj.compute_free_energy_differences()
deltaF_ij = results["Delta_f"]
dDeltaF_ij = results["dDelta_f"]
self._deltaF_mbar = deltaF_ij[0, self._lambda_array.shape[0] - 1]
self._dDeltaF_mbar = dDeltaF_ij[0, self._lambda_array.shape[0] - 1]
self._pmf_mbar = numpy.full(
Expand All @@ -138,7 +133,7 @@ def run_mbar(self, test_overlap=True):

##testing data overlap:
if test_overlap:
overlap_matrix = MBAR_obj.computeOverlap()
overlap_matrix = MBAR_obj.compute_overlap()
self._overlap_matrix = overlap_matrix["matrix"]

@property
Expand Down Expand Up @@ -255,13 +250,13 @@ def subsample_gradients(self):
shape=(self._gradients_kn.shape[0]), fill_value=numpy.nan
)
for i in range(g_k.shape[0]):
g_k[i] = timeseries.statisticalInefficiency(self._gradients_kn[i, :])
g_k[i] = timeseries.statistical_inefficiency(self._gradients_kn[i, :])
g = int(numpy.max(g_k))
# now we need to figure out what the indices in the data are for subsampling
indices_k = []
for i in range(g_k.shape[0]):
indices_k.append(
timeseries.subsampleCorrelatedData(self._gradients_kn[i, :], g=g)
timeseries.subsample_correlated_data(self._gradients_kn[i, :], g=g)
)
self._subsampled_N_k_gradients[i] = len(indices_k[i])
N_max = int(numpy.max(self._subsampled_N_k_gradients))
Expand Down Expand Up @@ -319,7 +314,7 @@ def subsample_energies(self):
# first we compute statistical inefficiency
g_k = numpy.full(shape=(self._energies_kn.shape[0]), fill_value=numpy.nan)
for i in range(g_k.shape[0]):
g_k[i] = timeseries.statisticalInefficiency(
g_k[i] = timeseries.statistical_inefficiency(
self._energies_kn[i, percentage_removal[i] :]
)
g = numpy.max(g_k)
Expand All @@ -330,7 +325,7 @@ def subsample_energies(self):
)
for i in range(g_k.shape[0]):
indices_k.append(
timeseries.subsampleCorrelatedData(self._energies_kn[i, :], g=g)
timeseries.subsample_correlated_data(self._energies_kn[i, :], g=g)
)
self._subsampled_N_k_energies[i] = len(indices_k[i])
# self._subsampled_N_k_energies = (numpy.ceil(self._N_k / g)).astype(int)
Expand Down