Skip to content

Commit

Permalink
Merge pull request #170 from OpenBioSim/feature_167
Browse files Browse the repository at this point in the history
Decouple custom parameters and additional LEaP commands
  • Loading branch information
lohedges authored Sep 22, 2023
2 parents e3014dc + 1d9bdf7 commit 44def0b
Show file tree
Hide file tree
Showing 4 changed files with 124 additions and 28 deletions.
44 changes: 36 additions & 8 deletions python/BioSimSpace/Parameters/_Protocol/_amber.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ def __init__(
tolerance=1.2,
max_distance=_Length(6, "A"),
water_model=None,
custom_parameters=None,
leap_commands=None,
bonds=None,
ensure_compatible=True,
Expand Down Expand Up @@ -154,11 +155,14 @@ def __init__(
Run 'BioSimSpace.Solvent.waterModels()' to see the supported
water models.
custom_parameters: [str]
A list of paths to custom parameter files. When this option is set,
we can no longer fall back on GROMACS's pdb2gmx.
leap_commands : [str]
An optional list of extra commands for the LEaP program. These
will be added after any default commands and can be used to, e.g.,
load additional parameter files. When this option is set, we can no
longer fall back on GROMACS's pdb2gmx.
will be added after any default commands. When this option is set,
we can no longer fall back on GROMACS's pdb2gmx.
bonds : ((class:`Atom <BioSimSpace._SireWrappers.Atom>`, class:`Atom <BioSimSpace._SireWrappers.Atom>`))
An optional tuple of atom pairs to specify additional atoms that
Expand Down Expand Up @@ -214,6 +218,26 @@ def __init__(
else:
self._water_model = water_model

# Validate the custom parameter file list.
if custom_parameters is not None:
if not isinstance(custom_parameters, (list, tuple)):
raise TypeError("'custom_parameters' must be a 'list' of 'str' types.")
else:
if not all(isinstance(x, str) for x in custom_parameters):
raise TypeError(
"'custom_parameters' must be a 'list' of 'str' types."
)
for x in custom_parameters:
if not os.path.isfile(x):
raise ValueError(f"Custom parameter file does not exist: '{x}'")

# Convert to absolute paths.
self._custom_parameters = []
for x in enumerate(custom_parameters):
self._custom_parameters.append(_os.path.abspath(x))
else:
self._custom_parameters = None

# Validate the additional leap commands.
if leap_commands is not None:
if not isinstance(leap_commands, (list, tuple)):
Expand Down Expand Up @@ -249,7 +273,7 @@ def __init__(

# Set the compatibility flags.
self._tleap = True
if self._leap_commands is not None:
if self._custom_parameters is not None or self._leap_commands is not None:
self._pdb2gmx = False

def run(self, molecule, work_dir=None, queue=None):
Expand Down Expand Up @@ -488,17 +512,21 @@ def _run_tleap(self, molecule, work_dir):
file.write("source leaprc.water.tip4pew\n")
else:
file.write("source leaprc.water.%s\n" % self._water_model)
# Write extra user commands.
if self._leap_commands is not None:
for command in self._leap_commands:
file.write("%s\n" % command)
# Write custom parameters.
if self._custom_parameters is not None:
for param in self._custom_parameters:
file.write("%s\n" % param)
file.write("mol = loadPdb leap.pdb\n")
# Add any disulphide bond records.
for bond in disulphide_bonds:
file.write("%s\n" % bond)
# Add any additional bond records.
for bond in pruned_bond_records:
file.write("%s\n" % bond)
# Write user leap commands.
if self._leap_commands is not None:
for command in self._leap_commands:
file.write("%s\n" % command)
file.write("saveAmberParm mol leap.top leap.crd\n")
file.write("quit")

Expand Down
32 changes: 26 additions & 6 deletions python/BioSimSpace/Parameters/_parameters.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ def _parameterise_amber_protein(
tolerance=1.2,
max_distance=_Length(6, "A"),
water_model=None,
custom_parameters=None,
leap_commands=None,
bonds=None,
ensure_compatible=True,
Expand Down Expand Up @@ -171,11 +172,14 @@ def _parameterise_amber_protein(
or lone oxygen atoms corresponding to structural (crystal) water
molecules.
custom_parameters: [str]
A list of paths to custom parameter files. When this option is set,
we can no longer fall back on GROMACS's pdb2gmx.
leap_commands : [str]
An optional list of extra commands for the LEaP program. These
will be added after any default commands and can be used to, e.g.,
load additional parameter files. When this option is set, we can no
longer fall back on GROMACS's pdb2gmx.
will be added after any default commands. When this option is set,
we can no longer fall back on GROMACS's pdb2gmx.
bonds : ((class:`Atom <BioSimSpace._SireWrappers.Atom>`, class:`Atom <BioSimSpace._SireWrappers.Atom>`))
An optional tuple of atom pairs to specify additional atoms that
Expand Down Expand Up @@ -237,6 +241,7 @@ def _parameterise_amber_protein(
max_distance=max_distance,
water_model=water_model,
check_ions=True,
custom_parameters=custom_parameters,
leap_commands=leap_commands,
bonds=bonds,
ensure_compatible=ensure_compatible,
Expand All @@ -250,6 +255,7 @@ def _parameterise_amber_protein(
tolerance=tolerance,
water_model=water_model,
max_distance=max_distance,
custom_parameters=custom_parameters,
leap_commands=leap_commands,
bonds=bonds,
ensure_compatible=ensure_compatible,
Expand Down Expand Up @@ -796,6 +802,7 @@ def _validate(
max_distance=None,
water_model=None,
check_ions=False,
custom_parameters=None,
leap_commands=None,
bonds=None,
ensure_compatible=True,
Expand Down Expand Up @@ -832,11 +839,14 @@ def _validate(
Whether to check for the presence of structural ions. This is only
required when parameterising with protein force fields.
custom_parameters: [str]
A list of paths to custom parameter files. When this option is set,
we can no longer fall back on GROMACS's pdb2gmx.
leap_commands : [str]
An optional list of extra commands for the LEaP program. These
will be added after any default commands and can be used to, e.g.,
load additional parameter files. When this option is set, we can no
longer fall back on GROMACS's pdb2gmx.
will be added after any default commands. When this option is set,
we can no longer fall back on GROMACS's pdb2gmx.
bonds : ((class:`Atom <BioSimSpace._SireWrappers.Atom>`, class:`Atom <BioSimSpace._SireWrappers.Atom>`))
An optional tuple of atom pairs to specify additional atoms that
Expand Down Expand Up @@ -895,6 +905,16 @@ def _validate(
"Please choose a 'water_model' for the ion parameters."
)

if custom_parameters is not None:
if not isinstance(custom_parameters, (list, tuple)):
raise TypeError("'custom_parameters' must be a 'list' of 'str' types.")
else:
if not all(isinstance(x, str) for x in custom_parameters):
raise TypeError("'custom_parameters' must be a 'list' of 'str' types.")
for x in custom_parameters:
if not os.path.isfile(x):
raise ValueError(f"Custom parameter file does not exist: '{x}'")

if leap_commands is not None:
if not isinstance(leap_commands, (list, tuple)):
raise TypeError("'leap_commands' must be a 'list' of 'str' types.")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ def __init__(
tolerance=1.2,
max_distance=_Length(6, "A"),
water_model=None,
custom_parameters=None,
leap_commands=None,
bonds=None,
ensure_compatible=True,
Expand Down Expand Up @@ -154,11 +155,14 @@ def __init__(
Run 'BioSimSpace.Solvent.waterModels()' to see the supported
water models.
custom_parameters: [str]
A list of paths to custom parameter files. When this option is set,
we can no longer fall back on GROMACS's pdb2gmx.
leap_commands : [str]
An optional list of extra commands for the LEaP program. These
will be added after any default commands and can be used to, e.g.,
load additional parameter files. When this option is set, we can no
longer fall back on GROMACS's pdb2gmx.
will be added after any default commands. When this option is set,
we can no longer fall back on GROMACS's pdb2gmx.
bonds : ((class:`Atom <BioSimSpace._SireWrappers.Atom>`, class:`Atom <BioSimSpace._SireWrappers.Atom>`))
An optional tuple of atom pairs to specify additional atoms that
Expand Down Expand Up @@ -214,6 +218,26 @@ def __init__(
else:
self._water_model = water_model

# Validate the custom parameter file list.
if custom_parameters is not None:
if not isinstance(custom_parameters, (list, tuple)):
raise TypeError("'custom_parameters' must be a 'list' of 'str' types.")
else:
if not all(isinstance(x, str) for x in custom_parameters):
raise TypeError(
"'custom_parameters' must be a 'list' of 'str' types."
)
for x in custom_parameters:
if not os.path.isfile(x):
raise ValueError(f"Custom parameter file does not exist: '{x}'")

# Convert to absolute paths.
self._custom_parameters = []
for x in enumerate(custom_parameters):
self._custom_parameters.append(_os.path.abspath(x))
else:
self._custom_parameters = None

# Validate the additional leap commands.
if leap_commands is not None:
if not isinstance(leap_commands, (list, tuple)):
Expand Down Expand Up @@ -249,7 +273,7 @@ def __init__(

# Set the compatibility flags.
self._tleap = True
if self._leap_commands is not None:
if self._custom_parameters is not None or self._leap_commands is not None:
self._pdb2gmx = False

def run(self, molecule, work_dir=None, queue=None):
Expand Down Expand Up @@ -488,17 +512,21 @@ def _run_tleap(self, molecule, work_dir):
file.write("source leaprc.water.tip4pew\n")
else:
file.write("source leaprc.water.%s\n" % self._water_model)
# Write extra user commands.
if self._leap_commands is not None:
for command in self._leap_commands:
file.write("%s\n" % command)
# Write custom parameters.
if self._custom_parameters is not None:
for param in self._custom_parameters:
file.write("%s\n" % param)
file.write("mol = loadPdb leap.pdb\n")
# Add any disulphide bond records.
for bond in disulphide_bonds:
file.write("%s\n" % bond)
# Add any additional bond records.
for bond in pruned_bond_records:
file.write("%s\n" % bond)
# Write user leap commands.
if self._leap_commands is not None:
for command in self._leap_commands:
file.write("%s\n" % command)
file.write("saveAmberParm mol leap.top leap.crd\n")
file.write("quit")

Expand Down
32 changes: 26 additions & 6 deletions python/BioSimSpace/Sandpit/Exscientia/Parameters/_parameters.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ def _parameterise_amber_protein(
tolerance=1.2,
max_distance=_Length(6, "A"),
water_model=None,
custom_parameters=None,
leap_commands=None,
bonds=None,
ensure_compatible=True,
Expand Down Expand Up @@ -171,11 +172,14 @@ def _parameterise_amber_protein(
or lone oxygen atoms corresponding to structural (crystal) water
molecules.
custom_parameters: [str]
A list of paths to custom parameter files. When this option is set,
we can no longer fall back on GROMACS's pdb2gmx.
leap_commands : [str]
An optional list of extra commands for the LEaP program. These
will be added after any default commands and can be used to, e.g.,
load additional parameter files. When this option is set, we can no
longer fall back on GROMACS's pdb2gmx.
will be added after any default commands. When this option is set,
we can no longer fall back on GROMACS's pdb2gmx.
bonds : ((class:`Atom <BioSimSpace._SireWrappers.Atom>`, class:`Atom <BioSimSpace._SireWrappers.Atom>`))
An optional tuple of atom pairs to specify additional atoms that
Expand Down Expand Up @@ -237,6 +241,7 @@ def _parameterise_amber_protein(
max_distance=max_distance,
water_model=water_model,
check_ions=True,
custom_parameters=custom_parameters,
leap_commands=leap_commands,
bonds=bonds,
ensure_compatible=ensure_compatible,
Expand All @@ -250,6 +255,7 @@ def _parameterise_amber_protein(
tolerance=tolerance,
water_model=water_model,
max_distance=max_distance,
custom_parameters=custom_parameters,
leap_commands=leap_commands,
bonds=bonds,
ensure_compatible=ensure_compatible,
Expand Down Expand Up @@ -796,6 +802,7 @@ def _validate(
max_distance=None,
water_model=None,
check_ions=False,
custom_parameters=None,
leap_commands=None,
bonds=None,
ensure_compatible=True,
Expand Down Expand Up @@ -832,11 +839,14 @@ def _validate(
Whether to check for the presence of structural ions. This is only
required when parameterising with protein force fields.
custom_parameters: [str]
A list of paths to custom parameter files. When this option is set,
we can no longer fall back on GROMACS's pdb2gmx.
leap_commands : [str]
An optional list of extra commands for the LEaP program. These
will be added after any default commands and can be used to, e.g.,
load additional parameter files. When this option is set, we can no
longer fall back on GROMACS's pdb2gmx.
will be added after any default commands. When this option is set,
we can no longer fall back on GROMACS's pdb2gmx.
bonds : ((class:`Atom <BioSimSpace._SireWrappers.Atom>`, class:`Atom <BioSimSpace._SireWrappers.Atom>`))
An optional tuple of atom pairs to specify additional atoms that
Expand Down Expand Up @@ -895,6 +905,16 @@ def _validate(
"Please choose a 'water_model' for the ion parameters."
)

if custom_parameters is not None:
if not isinstance(custom_parameters, (list, tuple)):
raise TypeError("'custom_parameters' must be a 'list' of 'str' types.")
else:
if not all(isinstance(x, str) for x in custom_parameters):
raise TypeError("'custom_parameters' must be a 'list' of 'str' types.")
for x in custom_parameters:
if not os.path.isfile(x):
raise ValueError(f"Custom parameter file does not exist: '{x}'")

if leap_commands is not None:
if not isinstance(leap_commands, (list, tuple)):
raise TypeError("'leap_commands' must be a 'list' of 'str' types.")
Expand Down

0 comments on commit 44def0b

Please sign in to comment.