Commit 80f9dfff authored by Jason Swails's avatar Jason Swails
Browse files

Add a positions setter and improve the error message when someone fails to load

parameters to a ProteinStructure before calling createSystem.
parent 77b4ddf3
...@@ -115,6 +115,8 @@ class ProteinStructure(object): ...@@ -115,6 +115,8 @@ class ProteinStructure(object):
self.title = title self.title = title
self.flags = flags self.flags = flags
self.box_vectors = None self.box_vectors = None
# Determine if we've loaded any parameters into our parameter set
self._parameters_loaded = False
@staticmethod @staticmethod
def _convert(string, type, message): def _convert(string, type, message):
...@@ -589,6 +591,10 @@ class ProteinStructure(object): ...@@ -589,6 +591,10 @@ class ProteinStructure(object):
separate Dihedral object for each term for types that have a separate Dihedral object for each term for types that have a
multi-term expansion multi-term expansion
""" """
# If parameters have already been loaded, issue a warning and return
if self._parameters_loaded:
warnings.warn('PSF has already been parametrized. Skipping.')
return
# First load the atom types # First load the atom types
types_are_int = False types_are_int = False
for atom in self.atom_list: for atom in self.atom_list:
...@@ -695,6 +701,7 @@ class ProteinStructure(object): ...@@ -695,6 +701,7 @@ class ProteinStructure(object):
# If the types started out as integers, change them back # If the types started out as integers, change them back
if types_are_int: if types_are_int:
for atom in self.atom_list: atom.type_to_int() for atom in self.atom_list: atom.type_to_int()
self._parameters_loaded = True
def set_coordinates(self, positions, velocities=None): def set_coordinates(self, positions, velocities=None):
""" """
...@@ -896,7 +903,9 @@ class ProteinStructure(object): ...@@ -896,7 +903,9 @@ class ProteinStructure(object):
verbose=False): verbose=False):
""" """
Construct an OpenMM System representing the topology described by the Construct an OpenMM System representing the topology described by the
prmtop file. prmtop file. You MUST have loaded a parameter set into this PSF before
calling createSystem. If not, AttributeError will be raised. ValueError
is raised for illegal input.
Parameters: Parameters:
- nonbondedMethod (object=NoCutoff) The method to use for nonbonded - nonbondedMethod (object=NoCutoff) The method to use for nonbonded
...@@ -937,6 +946,9 @@ class ProteinStructure(object): ...@@ -937,6 +946,9 @@ class ProteinStructure(object):
- flexibleConstraints (bool=True) Are our constraints flexible or not? - flexibleConstraints (bool=True) Are our constraints flexible or not?
- verbose (bool=False) Optionally prints out a running progress report - verbose (bool=False) Optionally prints out a running progress report
""" """
if not self._parameters_loaded:
raise AttributeError('You must load a parameter set before '
'creating the OpenMM System.')
hasbox = self.topology.getUnitCellDimensions() is not None hasbox = self.topology.getUnitCellDimensions() is not None
# Set the cutoff distance in nanometers # Set the cutoff distance in nanometers
cutoff = None cutoff = None
...@@ -1366,6 +1378,24 @@ class ProteinStructure(object): ...@@ -1366,6 +1378,24 @@ class ProteinStructure(object):
for a in self.atom_list]) * u.angstroms for a in self.atom_list]) * u.angstroms
return self._positions return self._positions
@positions.setter
def positions(self, stuff):
"""
Replace the cached positions and the positions of each atom. If no units
are applied to "stuff", it is assumed to be Angstroms.
"""
if not u.is_quantity(stuff):
# Assume this is Angstroms
stuff *= u.angstroms
# If we got a 1-D array, reshape it into an natom list of Vec3's
if len(stuff) == len(self.atom_list) * 3:
stuff = [Vec3(stuff[i*3], stuff[i*3+1], stuff[i*3+2])
for i in range(len(self.atom_list))]
self._positions = stuff
for atom, pos in zip(self.atom_list, stuff):
atom.xx, atom.xy, atom.xz = pos.value_in_unit(u.angstrom)
@property @property
def velocities(self): def velocities(self):
""" Same as for positions, but for velocities """ """ Same as for positions, but for velocities """
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment