11######################################################################
22# BioSimSpace: Making biomolecular simulation a breeze!
33#
4- # Copyright: 2017-2023
4+ # Copyright: 2017-2024
55#
66# Authors: Lester Hedges <lester.hedges@gmail.com>
77#
2626
2727__all__ = ["Somd" ]
2828
29- from .._Utils import _try_import
30-
3129import os as _os
3230
31+ from .._Utils import _try_import
32+
3333_pygtail = _try_import ("pygtail" )
3434import glob as _glob
3535import random as _random
3838import timeit as _timeit
3939import warnings as _warnings
4040
41- from sire .legacy import Base as _SireBase
4241from sire .legacy import CAS as _SireCAS
4342from sire .legacy import IO as _SireIO
4443from sire .legacy import MM as _SireMM
44+ from sire .legacy import Base as _SireBase
4545from sire .legacy import Mol as _SireMol
46+ from sire .legacy import Units as _SireUnits
4647
47- from .. import _isVerbose
48+ from .. import IO as _IO
49+ from .. import Protocol as _Protocol
50+ from .. import Trajectory as _Trajectory
51+ from .. import _isVerbose , _Utils
4852from .._Exceptions import IncompatibleError as _IncompatibleError
4953from .._Exceptions import MissingSoftwareError as _MissingSoftwareError
5054from .._SireWrappers import Molecule as _Molecule
5155from .._SireWrappers import System as _System
52-
53- from .. import IO as _IO
54- from .. import Protocol as _Protocol
55- from .. import Trajectory as _Trajectory
56- from .. import _Utils
57-
5856from . import _process
5957
6058
@@ -1001,6 +999,9 @@ def _to_pert_file(
1001999 if not isinstance (perturbation_type , str ):
10021000 raise TypeError ("'perturbation_type' must be of type 'str'" )
10031001
1002+ if not isinstance (property_map , dict ):
1003+ raise TypeError ("'property_map' must be of type 'dict'" )
1004+
10041005 # Convert to lower case and strip whitespace.
10051006 perturbation_type = perturbation_type .lower ().replace (" " , "" )
10061007
@@ -1027,6 +1028,60 @@ def _to_pert_file(
10271028 # Extract and copy the Sire molecule.
10281029 mol = molecule ._sire_object .__deepcopy__ ()
10291030
1031+ # If the molecule is decoupled (for an ABFE calculation), then we need to
1032+ # set the end-state properties of the molecule.
1033+ if molecule .isDecoupled ():
1034+ # Invert the user property mappings.
1035+ inv_property_map = {v : k for k , v in property_map .items ()}
1036+
1037+ # Get required properties.
1038+ lj = inv_property_map .get ("LJ" , "LJ" )
1039+ charge = inv_property_map .get ("charge" , "charge" )
1040+ element = inv_property_map .get ("element" , "element" )
1041+ ambertype = inv_property_map .get ("ambertype" , "ambertype" )
1042+
1043+ # Check for missing information.
1044+ if not mol .hasProperty (lj ):
1045+ raise _IncompatibleError ("Cannot determine LJ terms for molecule" )
1046+ if not mol .hasProperty (charge ):
1047+ raise _IncompatibleError ("Cannot determine charges for molecule" )
1048+ if not mol .hasProperty (element ):
1049+ raise _IncompatibleError ("Cannot determine elements in molecule" )
1050+
1051+ # Check for ambertype property.
1052+ has_ambertype = True
1053+ if not mol .hasProperty (ambertype ):
1054+ has_ambertype = False
1055+
1056+ mol_edit = mol .edit ()
1057+
1058+ # Set final charges and LJ terms to 0, elements to "X" and (if required) ambertypes to du
1059+ for atom in mol .atoms ():
1060+ mol_edit = (
1061+ mol_edit .atom (atom .index ())
1062+ .setProperty ("charge1" , 0 * _SireUnits .e_charge )
1063+ .molecule ()
1064+ )
1065+ mol_edit = (
1066+ mol_edit .atom (atom .index ())
1067+ .setProperty ("LJ1" , _SireMM .LJParameter ())
1068+ .molecule ()
1069+ )
1070+ mol_edit = (
1071+ mol_edit .atom (atom .index ())
1072+ .setProperty ("element1" , _SireMol .Element (0 ))
1073+ .molecule ()
1074+ )
1075+ if has_ambertype :
1076+ mol_edit = (
1077+ mol_edit .atom (atom .index ())
1078+ .setProperty ("ambertype1" , "du" )
1079+ .molecule ()
1080+ )
1081+
1082+ # Update the Sire molecule object of the new molecule.
1083+ mol = mol_edit .commit ()
1084+
10301085 # First work out the indices of atoms that are perturbed.
10311086 pert_idxs = []
10321087
0 commit comments