From c905b9868574d70bd90c52b9dba79e2e56cc0ca5 Mon Sep 17 00:00:00 2001 From: Lester Hedges Date: Fri, 15 Aug 2025 11:30:45 +0100 Subject: [PATCH] Add kwarg to allow user to avoid writing velocities. --- python/BioSimSpace/IO/_io.py | 37 ++++++++++++++++++- .../BioSimSpace/Sandpit/Exscientia/IO/_io.py | 37 ++++++++++++++++++- 2 files changed, 70 insertions(+), 4 deletions(-) diff --git a/python/BioSimSpace/IO/_io.py b/python/BioSimSpace/IO/_io.py index 3ff9d854e..87a4915f2 100644 --- a/python/BioSimSpace/IO/_io.py +++ b/python/BioSimSpace/IO/_io.py @@ -592,7 +592,13 @@ def readMolecules( def saveMolecules( - filebase, system, fileformat, match_water=True, property_map={}, **kwargs + filebase, + system, + fileformat, + match_water=True, + save_velocities=True, + property_map={}, + **kwargs, ): """ Save a molecular system to file. @@ -617,6 +623,9 @@ def saveMolecules( is being saved to a different file format to that from which it was loaded. + save_velocities : bool + Whether to write velocities to the output files. + property_map : dict A dictionary that maps system "properties" to their user defined values. This allows the user to refer to properties @@ -710,6 +719,10 @@ def saveMolecules( if not isinstance(match_water, bool): raise TypeError("'match_water' must be of type 'bool'.") + # Validate the save_velocities flag. + if not isinstance(save_velocities, bool): + raise TypeError("'save_velocities' must be of type 'bool'.") + # Make a list of the matched file formats. formats = [] @@ -735,6 +748,11 @@ def saveMolecules( if _gmx_path is not None and ("GROMACS_PATH" not in _property_map): _property_map["GROMACS_PATH"] = _gmx_path + # If the user doesn't wish to save velocities, then remap the + # velocity property. + if not save_velocities: + _property_map["velocity"] = "null" + # Get the directory name. dirname = _os.path.dirname(filebase) @@ -861,7 +879,7 @@ def saveMolecules( return files -def savePerturbableSystem(filebase, system, property_map={}): +def savePerturbableSystem(filebase, system, save_velocities=True, property_map={}): """ Save a system containing a perturbable molecule. This will be written in AMBER format, with a topology file for each end state of the perturbation, @@ -877,6 +895,9 @@ def savePerturbableSystem(filebase, system, property_map={}): system : :class:`System ` The molecular system. + save_velocities : bool + Whether to write velocities to the output files. + property_map : dict A dictionary that maps system "properties" to their user defined values. This allows the user to refer to properties with their @@ -907,6 +928,10 @@ def savePerturbableSystem(filebase, system, property_map={}): "or a list of 'BiSimSpace._SireWrappers.Molecule' types." ) + # Validate the save_velocities flag. + if not isinstance(save_velocities, bool): + raise TypeError("'save_velocities' must be of type 'bool'.") + # Validate the map. if not isinstance(property_map, dict): raise TypeError("'property_map' must be of type 'dict'") @@ -926,6 +951,14 @@ def savePerturbableSystem(filebase, system, property_map={}): system0 = system.copy() system1 = system.copy() + # Create a copy of the property map. + property_map = property_map.copy() + + # If the user doesn't wish to save velocities, then remap the + # velocity property. + if not save_velocities: + property_map["velocity"] = "null" + # Update the perturbable molecule in each system. system0.updateMolecules( pert_mol._toRegularMolecule(property_map=property_map, is_lambda1=False) diff --git a/python/BioSimSpace/Sandpit/Exscientia/IO/_io.py b/python/BioSimSpace/Sandpit/Exscientia/IO/_io.py index 3ff9d854e..87a4915f2 100644 --- a/python/BioSimSpace/Sandpit/Exscientia/IO/_io.py +++ b/python/BioSimSpace/Sandpit/Exscientia/IO/_io.py @@ -592,7 +592,13 @@ def readMolecules( def saveMolecules( - filebase, system, fileformat, match_water=True, property_map={}, **kwargs + filebase, + system, + fileformat, + match_water=True, + save_velocities=True, + property_map={}, + **kwargs, ): """ Save a molecular system to file. @@ -617,6 +623,9 @@ def saveMolecules( is being saved to a different file format to that from which it was loaded. + save_velocities : bool + Whether to write velocities to the output files. + property_map : dict A dictionary that maps system "properties" to their user defined values. This allows the user to refer to properties @@ -710,6 +719,10 @@ def saveMolecules( if not isinstance(match_water, bool): raise TypeError("'match_water' must be of type 'bool'.") + # Validate the save_velocities flag. + if not isinstance(save_velocities, bool): + raise TypeError("'save_velocities' must be of type 'bool'.") + # Make a list of the matched file formats. formats = [] @@ -735,6 +748,11 @@ def saveMolecules( if _gmx_path is not None and ("GROMACS_PATH" not in _property_map): _property_map["GROMACS_PATH"] = _gmx_path + # If the user doesn't wish to save velocities, then remap the + # velocity property. + if not save_velocities: + _property_map["velocity"] = "null" + # Get the directory name. dirname = _os.path.dirname(filebase) @@ -861,7 +879,7 @@ def saveMolecules( return files -def savePerturbableSystem(filebase, system, property_map={}): +def savePerturbableSystem(filebase, system, save_velocities=True, property_map={}): """ Save a system containing a perturbable molecule. This will be written in AMBER format, with a topology file for each end state of the perturbation, @@ -877,6 +895,9 @@ def savePerturbableSystem(filebase, system, property_map={}): system : :class:`System ` The molecular system. + save_velocities : bool + Whether to write velocities to the output files. + property_map : dict A dictionary that maps system "properties" to their user defined values. This allows the user to refer to properties with their @@ -907,6 +928,10 @@ def savePerturbableSystem(filebase, system, property_map={}): "or a list of 'BiSimSpace._SireWrappers.Molecule' types." ) + # Validate the save_velocities flag. + if not isinstance(save_velocities, bool): + raise TypeError("'save_velocities' must be of type 'bool'.") + # Validate the map. if not isinstance(property_map, dict): raise TypeError("'property_map' must be of type 'dict'") @@ -926,6 +951,14 @@ def savePerturbableSystem(filebase, system, property_map={}): system0 = system.copy() system1 = system.copy() + # Create a copy of the property map. + property_map = property_map.copy() + + # If the user doesn't wish to save velocities, then remap the + # velocity property. + if not save_velocities: + property_map["velocity"] = "null" + # Update the perturbable molecule in each system. system0.updateMolecules( pert_mol._toRegularMolecule(property_map=property_map, is_lambda1=False)