Skip to content

Commit

Permalink
Add parameter to allow user to optionally match water naming convention.
Browse files Browse the repository at this point in the history
[ref #148]
  • Loading branch information
lohedges committed Aug 2, 2023
1 parent 3a18893 commit 92a815c
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 14 deletions.
35 changes: 28 additions & 7 deletions python/BioSimSpace/IO/_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -526,7 +526,9 @@ def readMolecules(
return _System(system)


def saveMolecules(filebase, system, fileformat, property_map={}, **kwargs):
def saveMolecules(
filebase, system, fileformat, match_water=True, property_map={}, **kwargs
):
"""
Save a molecular system to file.
Expand All @@ -544,6 +546,12 @@ def saveMolecules(filebase, system, fileformat, property_map={}, **kwargs):
fileformat : str, [str]
The file format (or formats) to save to.
match_water : bool
Whether to update the naming of water molecules to match the expected
convention for the chosen file format. This is useful when a system
is being saved to a different file format to that from which it was
loaded.
property_map : dict
A dictionary that maps system "properties" to their user
defined values. This allows the user to refer to properties
Expand Down Expand Up @@ -633,6 +641,10 @@ def saveMolecules(filebase, system, fileformat, property_map={}, **kwargs):
if not all(isinstance(x, str) for x in fileformat):
raise TypeError("'fileformat' must be a 'str' or a 'list' of 'str' types.")

# Validate the match_water flag.
if not isinstance(match_water, bool):
raise TypeError("'match_water' must be of type 'bool'.")

# Make a list of the matched file formats.
formats = []

Expand Down Expand Up @@ -714,23 +726,32 @@ def saveMolecules(filebase, system, fileformat, property_map={}, **kwargs):
# and save GROMACS files with an extension such that they can be run
# directly by GROMACS without needing to be renamed.
if format.upper() == "PRM7":
system_copy = system.copy()
system_copy._set_water_topology("AMBER", _property_map)
if match_water:
system_copy = system.copy()
system_copy._set_water_topology("AMBER", _property_map)
else:
system_copy = system
file = _SireIO.MoleculeParser.save(
system_copy._sire_object, filebase, _property_map
)
elif format.upper() == "GROTOP":
system_copy = system.copy()
system_copy._set_water_topology("GROMACS", _property_map)
if match_water:
system_copy = system.copy()
system_copy._set_water_topology("GROMACS", _property_map)
else:
system_copy = system
file = _SireIO.MoleculeParser.save(
system_copy._sire_object, filebase, _property_map
)[0]
new_file = file.replace("grotop", "top")
_os.rename(file, new_file)
file = [new_file]
elif format.upper() == "GRO87":
system_copy = system.copy()
system_copy._set_water_topology("GROMACS", _property_map)
if match_water:
system_copy = system.copy()
system_copy._set_water_topology("GROMACS", _property_map)
else:
system_copy = system
# Write to 3dp by default, unless greater precision is
# requested by the user.
if "precision" not in _property_map:
Expand Down
35 changes: 28 additions & 7 deletions python/BioSimSpace/Sandpit/Exscientia/IO/_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -526,7 +526,9 @@ def readMolecules(
return _System(system)


def saveMolecules(filebase, system, fileformat, property_map={}, **kwargs):
def saveMolecules(
filebase, system, fileformat, match_water=True, property_map={}, **kwargs
):
"""
Save a molecular system to file.
Expand All @@ -544,6 +546,12 @@ def saveMolecules(filebase, system, fileformat, property_map={}, **kwargs):
fileformat : str, [str]
The file format (or formats) to save to.
match_water : bool
Whether to update the naming of water molecules to match the expected
convention for the chosen file format. This is useful when a system
is being saved to a different file format to that from which it was
loaded.
property_map : dict
A dictionary that maps system "properties" to their user
defined values. This allows the user to refer to properties
Expand Down Expand Up @@ -633,6 +641,10 @@ def saveMolecules(filebase, system, fileformat, property_map={}, **kwargs):
if not all(isinstance(x, str) for x in fileformat):
raise TypeError("'fileformat' must be a 'str' or a 'list' of 'str' types.")

# Validate the match_water flag.
if not isinstance(match_water, bool):
raise TypeError("'match_water' must be of type 'bool'.")

# Make a list of the matched file formats.
formats = []

Expand Down Expand Up @@ -714,23 +726,32 @@ def saveMolecules(filebase, system, fileformat, property_map={}, **kwargs):
# and save GROMACS files with an extension such that they can be run
# directly by GROMACS without needing to be renamed.
if format.upper() == "PRM7":
system_copy = system.copy()
system_copy._set_water_topology("AMBER", _property_map)
if match_water:
system_copy = system.copy()
system_copy._set_water_topology("AMBER", _property_map)
else:
system_copy = system
file = _SireIO.MoleculeParser.save(
system_copy._sire_object, filebase, _property_map
)
elif format.upper() == "GROTOP":
system_copy = system.copy()
system_copy._set_water_topology("GROMACS", _property_map)
if match_water:
system_copy = system.copy()
system_copy._set_water_topology("GROMACS", _property_map)
else:
system_copy = system
file = _SireIO.MoleculeParser.save(
system_copy._sire_object, filebase, _property_map
)[0]
new_file = file.replace("grotop", "top")
_os.rename(file, new_file)
file = [new_file]
elif format.upper() == "GRO87":
system_copy = system.copy()
system_copy._set_water_topology("GROMACS", _property_map)
if match_water:
system_copy = system.copy()
system_copy._set_water_topology("GROMACS", _property_map)
else:
system_copy = system
# Write to 3dp by default, unless greater precision is
# requested by the user.
if "precision" not in _property_map:
Expand Down

0 comments on commit 92a815c

Please sign in to comment.