Skip to content

Commit

Permalink
Merge pull request #343 from OpenBioSim/backport_341_342
Browse files Browse the repository at this point in the history
Backport fixes from #341 and #342
  • Loading branch information
lohedges authored Sep 25, 2024
2 parents 00eceef + c135b61 commit 71f86d7
Show file tree
Hide file tree
Showing 8 changed files with 72 additions and 8 deletions.
2 changes: 2 additions & 0 deletions python/BioSimSpace/Process/_amber.py
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,7 @@ def _setup(self, **kwargs):

# Set the simulation box.
system.setBox(*_cubic(box_length))
reference_system.setBox(*_cubic(box_length))

# Apply SOMD1 compatibility to the perturbation.
if (
Expand All @@ -322,6 +323,7 @@ def _setup(self, **kwargs):
else:
# Check for perturbable molecules and convert to the chosen end state.
system = self._checkPerturbable(system)
reference_system = self._checkPerturbable(self._reference_system)

# RST file (coordinates).
try:
Expand Down
29 changes: 22 additions & 7 deletions python/BioSimSpace/Process/_gromacs.py
Original file line number Diff line number Diff line change
Expand Up @@ -2131,16 +2131,31 @@ def _add_position_restraints(self):
)

with open(restraint_file, "w") as file:
# Write the header.
file.write("[ position_restraints ]\n")
file.write("; i funct fcx fcy fcz\n")

# Write restraints for each atom.
for atom_idx in restrained_atoms:
if isinstance(self._protocol, _FreeEnergyMixin):
# Write the header.
file.write("[ position_restraints ]\n")
file.write(
f"{atom_idx+1:4} 1 {force_constant} {force_constant} {force_constant}\n"
"; i funct fcx_A fcy_A fcz_A fcx_B fcy_B fcz_B\n"
)
# Write restraints for each atom.
for atom_idx in restrained_atoms:
file.write(
f"{atom_idx+1:4} 1 {force_constant} {force_constant} {force_constant} {force_constant} {force_constant} {force_constant}\n"
)

else:
# Write the header.
file.write("[ position_restraints ]\n")
file.write(
"; i funct fcx fcy fcz\n"
)

# Write restraints for each atom.
for atom_idx in restrained_atoms:
file.write(
f"{atom_idx+1:4} 1 {force_constant} {force_constant} {force_constant}\n"
)

# Work out the offset.
offset = num_restraint - 1

Expand Down
3 changes: 3 additions & 0 deletions python/BioSimSpace/Process/_namd.py
Original file line number Diff line number Diff line change
Expand Up @@ -2115,6 +2115,9 @@ def _createRestrainedSystem(self, system, restraint):
# Copy the original system.
s = system.copy()

# Convert to the chosen end state.
s = self._checkPerturbable(s)

# Keyword restraint.
if isinstance(restraint, str):
# Loop over all molecules by number.
Expand Down
3 changes: 2 additions & 1 deletion python/BioSimSpace/Process/_openmm.py
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,7 @@ def _setup(self):

# Check for perturbable molecules and convert to the chosen end state.
system = self._checkPerturbable(system)
reference_system = self._checkPerturbable(self._reference_system)

# Create the input files...

Expand All @@ -274,7 +275,7 @@ def _setup(self):
file = _os.path.splitext(self._ref_file)[0]
_IO.saveMolecules(
file,
self._reference_system,
reference_system,
"rst7",
property_map=self._property_map,
)
Expand Down
11 changes: 11 additions & 0 deletions tests/Process/test_amber.py
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,17 @@ def test_backbone_restraint_mask_rna(rna_system):
assert " restraintmask=\"@P,C5',C3',O3',O5'\"," in config


@pytest.mark.skipif(has_amber is False, reason="Requires AMBER to be installed.")
def test_perturbable_restraint(perturbable_system):
"""Test a free energy perturbation protocol."""

# Create a short minimisation prototocol with a restraint.
protocol = BSS.Protocol.Minimisation(steps=100, restraint="heavy")

# Run the process, check that it finished without error, and returns a system.
run_process(perturbable_system, protocol)


def run_process(system, protocol, check_data=False):
"""Helper function to run various simulation protocols."""

Expand Down
11 changes: 11 additions & 0 deletions tests/Process/test_gromacs.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,17 @@ def test_restraints(perturbable_system, restraint):
process = BSS.Process.Gromacs(perturbable_system, protocol)


@pytest.mark.skipif(has_gromacs is False, reason="Requires GROMACS to be installed.")
def test_perturbable_restraint(perturbable_system):
"""Test a free energy perturbation protocol."""

# Create a short minimisation protocol with a restraint.
protocol = BSS.Protocol.Minimisation(steps=100, restraint="heavy")

# Run the process, check that it finished without error, and returns a system.
run_process(perturbable_system, protocol)


def run_process(system, protocol, **kwargs):
"""Helper function to run various simulation protocols."""

Expand Down
11 changes: 11 additions & 0 deletions tests/Process/test_namd.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,17 @@ def test_production(namd_system, restraint):
run_process(namd_system, protocol)


@pytest.mark.skipif(has_namd is False, reason="Requires NAMD to be installed.")
def test_perturbable_restraint(perturbable_system):
"""Test a free energy perturbation protocol."""

# Create a short minimisation prototocol with a restraint.
protocol = BSS.Protocol.Minimisation(steps=100, restraint="heavy")

# Run the process, check that it finished without error, and returns a system.
run_process(perturbable_system, protocol)


def run_process(namd_system, protocol):
"""Helper function to run various simulation protocols."""

Expand Down
10 changes: 10 additions & 0 deletions tests/Process/test_openmm.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,16 @@ def test_parmed_triclinic():
run_process(system, protocol)


def test_perturbable_restraint(perturbable_system):
"""Test a free energy perturbation protocol."""

# Create a short minimisation prototocol with a restraint.
protocol = BSS.Protocol.Minimisation(steps=100, restraint="heavy")

# Run the process, check that it finished without error, and returns a system.
run_process(perturbable_system, protocol)


def run_process(system, protocol):
"""Helper function to run various simulation protocols."""

Expand Down

0 comments on commit 71f86d7

Please sign in to comment.