Commit 30a53702 authored by Peter Eastman's avatar Peter Eastman
Browse files

More support for triclinic boxes

parent 21a39eaf
...@@ -572,7 +572,7 @@ with the name :file:`simulateGromacs.py`. ...@@ -572,7 +572,7 @@ with the name :file:`simulateGromacs.py`.
from sys import stdout from sys import stdout
gro = GromacsGroFile('input.gro') gro = GromacsGroFile('input.gro')
top = GromacsTopFile('input.top', unitCellDimensions=gro.getUnitCellDimensions(), top = GromacsTopFile('input.top', periodicBoxVectors=gro.getPeriodicBoxVectors(),
includeDir='/usr/local/gromacs/share/gromacs/top') includeDir='/usr/local/gromacs/share/gromacs/top')
system = top.createSystem(nonbondedMethod=PME, nonbondedCutoff=1*nanometer, system = top.createSystem(nonbondedMethod=PME, nonbondedCutoff=1*nanometer,
constraints=HBonds) constraints=HBonds)
...@@ -593,10 +593,10 @@ This script is nearly identical to the previous one, just replacing ...@@ -593,10 +593,10 @@ This script is nearly identical to the previous one, just replacing
:class:`AmberInpcrdFile` and :class:`AmberPrmtopFile` with :class:`GromacsGroFile` and :class:`GromacsTopFile`. :class:`AmberInpcrdFile` and :class:`AmberPrmtopFile` with :class:`GromacsGroFile` and :class:`GromacsTopFile`.
Note that when we create the :class:`GromacsTopFile`, we specify values for two extra Note that when we create the :class:`GromacsTopFile`, we specify values for two extra
options. First, we specify options. First, we specify
:code:`unitCellDimensions=gro.getUnitCellDimensions()`\ . Unlike OpenMM and :code:`periodicBoxVectors=gro.getPeriodicBoxVectors()`\ . Unlike OpenMM and
AMBER, which can store periodic unit cell dimensions with the topology, Gromacs AMBER, which can store periodic unit cell information with the topology, Gromacs
only stores them with the coordinates. To let :class:`GromacsTopFile` create a :class:`Topology` only stores it with the coordinates. To let :class:`GromacsTopFile` create a :class:`Topology`
object, we therefore need to tell it the unit cell dimensions that were loaded object, we therefore need to tell it the periodic box vectors that were loaded
from the :file:`gro` file. You only need to do this if you are simulating a periodic from the :file:`gro` file. You only need to do this if you are simulating a periodic
system. For implicit solvent simulations, it usually can be omitted. system. For implicit solvent simulations, it usually can be omitted.
......
...@@ -4,7 +4,7 @@ from simtk.unit import * ...@@ -4,7 +4,7 @@ from simtk.unit import *
from sys import stdout from sys import stdout
gro = GromacsGroFile('input.gro') gro = GromacsGroFile('input.gro')
top = GromacsTopFile('input.top', unitCellDimensions=gro.getUnitCellDimensions(), includeDir='/usr/local/gromacs/share/gromacs/top') top = GromacsTopFile('input.top', periodicBoxVectors=gro.getPeriodicBoxVectors(), includeDir='/usr/local/gromacs/share/gromacs/top')
system = top.createSystem(nonbondedMethod=PME, nonbondedCutoff=1*nanometer, constraints=HBonds) system = top.createSystem(nonbondedMethod=PME, nonbondedCutoff=1*nanometer, constraints=HBonds)
integrator = LangevinIntegrator(300*kelvin, 1/picosecond, 0.002*picoseconds) integrator = LangevinIntegrator(300*kelvin, 1/picosecond, 0.002*picoseconds)
simulation = Simulation(top.topology, system, integrator) simulation = Simulation(top.topology, system, integrator)
......
...@@ -77,14 +77,17 @@ class DCDFile(object): ...@@ -77,14 +77,17 @@ class DCDFile(object):
header += struct.pack('<4i', 164, 4, len(list(topology.atoms())), 4) header += struct.pack('<4i', 164, 4, len(list(topology.atoms())), 4)
file.write(header) file.write(header)
def writeModel(self, positions, unitCellDimensions=None): def writeModel(self, positions, unitCellDimensions=None, periodicBoxVectors=None):
"""Write out a model to the DCD file. """Write out a model to the DCD file.
The periodic box can be specified either by the unit cell dimensions (for a rectangular box), or the full set of box
vectors (for an arbitrary triclinic box). If neither is specified, the box vectors specified in the Topology will be
used. Regardless of the value specified, no dimensions will be written if the Topology does not represent a periodic system.
Parameters: Parameters:
- positions (list) The list of atomic positions to write - positions (list) The list of atomic positions to write
- unitCellDimensions (Vec3=None) The dimensions of the crystallographic unit cell. If None, the dimensions specified in - unitCellDimensions (Vec3=None) The dimensions of the crystallographic unit cell.
the Topology will be used. Regardless of the value specified, no dimensions will be written if the Topology does not - periodicBoxVectors (tuple of Vec3=None) The vectors defining the periodic box.
represent a periodic system.
""" """
if len(list(self._topology.atoms())) != len(positions): if len(list(self._topology.atoms())) != len(positions):
raise ValueError('The number of positions must match the number of atoms') raise ValueError('The number of positions must match the number of atoms')
...@@ -107,12 +110,22 @@ class DCDFile(object): ...@@ -107,12 +110,22 @@ class DCDFile(object):
# Write the data. # Write the data.
file.seek(0, os.SEEK_END) file.seek(0, os.SEEK_END)
boxSize = self._topology.getUnitCellDimensions() boxVectors = self._topology.getPeriodicBoxVectors()
if boxSize is not None: if boxVectors is not None:
if unitCellDimensions is not None: if getPeriodicBoxVectors is not None:
boxSize = unitCellDimensions boxVectors = getPeriodicBoxVectors
size = boxSize.value_in_unit(angstroms) elif unitCellDimensions is not None:
file.write(struct.pack('<i6di', 48, size[0], 0, size[1], 0, 0, size[2], 48)) if is_quantity(unitCellDimensions):
unitCellDimensions = unitCellDimensions.value_in_unit(nanometers)
boxVectors = (Vec3(unitCellDimensions[0], 0, 0), Vec3(0, unitCellDimensions[1], 0), Vec3(0, 0, unitCellDimensions[2]))*nanometers
(a_length, b_length, c_length, alpha, beta, gamma) = computeLengthsAndAngles(boxVectors)
a_length = a_length.value_in_unit(angstroms)
b_length = b_length.value_in_unit(angstroms)
c_length = c_length.value_in_unit(angstroms)
angle1 = math.sin(math.pi/2-gamma)
angle2 = math.sin(math.pi/2-beta)
angle3 = math.sin(math.pi/2-alpha)
file.write(struct.pack('<i6di', 48, a_length, angle1, b_length, angle2, angle3, c_length, 48))
length = struct.pack('<i', 4*len(positions)) length = struct.pack('<i', 4*len(positions))
for i in range(3): for i in range(3):
file.write(length) file.write(length)
......
...@@ -37,42 +37,11 @@ __version__ = "1.0" ...@@ -37,42 +37,11 @@ __version__ = "1.0"
from simtk.openmm.vec3 import Vec3 from simtk.openmm.vec3 import Vec3
import simtk.unit as unit import simtk.unit as unit
from .. import element from .. import element
from unitcell import computePeriodicBoxVectors
import warnings import warnings
import sys import sys
import math import math
def computePeriodicBoxVectors(a_length, b_length, c_length, alpha, beta, gamma):
"""Convert lengths and angles from a CRYST1 record to periodic box vectors."""
# Compute the vectors.
a = [a_length, 0, 0]
b = [b_length*math.cos(gamma), b_length*math.sin(gamma), 0]
cx = c_length*math.cos(beta)
cy = c_length*(math.cos(alpha)-math.cos(beta)*math.cos(gamma))/math.sin(gamma)
cz = math.sqrt(c_length*c_length-cx*cx-cy*cy)
c = [cx, cy, cz]
# If any elements are very close to 0, set them to exactly 0.
for i in range(3):
if abs(a[i]) < 1e-6:
a[i] = 0.0
if abs(b[i]) < 1e-6:
b[i] = 0.0
if abs(c[i]) < 1e-6:
c[i] = 0.0
a = Vec3(*a)
b = Vec3(*b)
c = Vec3(*c)
# Make sure they're in the reduced form required by OpenMM.
c = c - b*round(c[1]/b[1])
c = c - a*round(c[0]/a[0])
b = b - a*round(b[0]/a[0])
return (a, b, c)*unit.angstroms
class PdbStructure(object): class PdbStructure(object):
""" """
PdbStructure object holds a parsed Protein Data Bank format file. PdbStructure object holds a parsed Protein Data Bank format file.
...@@ -203,9 +172,9 @@ class PdbStructure(object): ...@@ -203,9 +172,9 @@ class PdbStructure(object):
self._current_model._current_chain._add_ter_record() self._current_model._current_chain._add_ter_record()
self._reset_residue_numbers() self._reset_residue_numbers()
elif (pdb_line.find("CRYST1") == 0): elif (pdb_line.find("CRYST1") == 0):
a_length = float(pdb_line[6:15]) a_length = float(pdb_line[6:15])*0.1
b_length = float(pdb_line[15:24]) b_length = float(pdb_line[15:24])*0.1
c_length = float(pdb_line[24:33]) c_length = float(pdb_line[24:33])*0.1
alpha = float(pdb_line[33:40])*math.pi/180.0 alpha = float(pdb_line[33:40])*math.pi/180.0
beta = float(pdb_line[40:47])*math.pi/180.0 beta = float(pdb_line[40:47])*math.pi/180.0
gamma = float(pdb_line[47:54])*math.pi/180.0 gamma = float(pdb_line[47:54])*math.pi/180.0
......
"""
unitcell.py: Routines for converting between different representations of the periodic unit cell.
This is part of the OpenMM molecular simulation toolkit originating from
Simbios, the NIH National Center for Physics-Based Simulation of
Biological Structures at Stanford, funded under the NIH Roadmap for
Medical Research, grant U54 GM072970. See https://simtk.org.
Portions copyright (c) 2015 Stanford University and the Authors.
Authors: Peter Eastman
Contributors:
Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
THE AUTHORS, CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
USE OR OTHER DEALINGS IN THE SOFTWARE.
"""
__author__ = "Peter Eastman"
__version__ = "1.0"
from simtk.openmm import Vec3
from simtk.unit import nanometers, is_quantity, norm, dot
import math
def computePeriodicBoxVectors(a_length, b_length, c_length, alpha, beta, gamma):
"""Convert lengths and angles to periodic box vectors.
Lengths should be given in nanometers and angles in radians.
"""
# Compute the vectors.
a = [a_length, 0, 0]
b = [b_length*math.cos(gamma), b_length*math.sin(gamma), 0]
cx = c_length*math.cos(beta)
cy = c_length*(math.cos(alpha)-math.cos(beta)*math.cos(gamma))/math.sin(gamma)
cz = math.sqrt(c_length*c_length-cx*cx-cy*cy)
c = [cx, cy, cz]
# If any elements are very close to 0, set them to exactly 0.
for i in range(3):
if abs(a[i]) < 1e-6:
a[i] = 0.0
if abs(b[i]) < 1e-6:
b[i] = 0.0
if abs(c[i]) < 1e-6:
c[i] = 0.0
a = Vec3(*a)
b = Vec3(*b)
c = Vec3(*c)
# Make sure they're in the reduced form required by OpenMM.
c = c - b*round(c[1]/b[1])
c = c - a*round(c[0]/a[0])
b = b - a*round(b[0]/a[0])
return (a, b, c)*nanometers
def computeLengthsAndAngles(periodicBoxVectors):
"""Convert periodic box vectors to lengths and angles.
Lengths are returned in nanometers and angles in radians.
"""
(a, b, c) = vectors.value_in_unit(nanometers)
a_length = norm(a)
b_length = norm(b)
c_length = norm(c)
alpha = math.acos(dot(b, c)/(b_length*c_length))
beta = math.acos(dot(c, a)/(c_length*a_length))
gamma = math.acos(dot(a, b)/(a_length*b_length))
return (a_length, b_length, c_length, alpha, beta, gamma)
...@@ -36,7 +36,7 @@ import sys ...@@ -36,7 +36,7 @@ import sys
import math import math
from simtk.openmm import Vec3 from simtk.openmm import Vec3
from simtk.openmm.app.internal.pdbx.reader.PdbxReader import PdbxReader from simtk.openmm.app.internal.pdbx.reader.PdbxReader import PdbxReader
from simtk.openmm.app.internal.pdbstructure import computePeriodicBoxVectors from simtk.openmm.app.internal.unitcell import computePeriodicBoxVectors
from simtk.openmm.app import Topology from simtk.openmm.app import Topology
from simtk.unit import nanometers, angstroms, is_quantity, norm, Quantity from simtk.unit import nanometers, angstroms, is_quantity, norm, Quantity
import element as elem import element as elem
...@@ -147,7 +147,7 @@ class PDBxFile(object): ...@@ -147,7 +147,7 @@ class PDBxFile(object):
cell = block.getObj('cell') cell = block.getObj('cell')
if cell is not None and cell.getRowCount() > 0: if cell is not None and cell.getRowCount() > 0:
row = cell.getRow(0) row = cell.getRow(0)
(a, b, c) = [float(row[cell.getAttributeIndex(attribute)]) for attribute in ('length_a', 'length_b', 'length_c')] (a, b, c) = [float(row[cell.getAttributeIndex(attribute)])*0.1 for attribute in ('length_a', 'length_b', 'length_c')]
(alpha, beta, gamma) = [float(row[cell.getAttributeIndex(attribute)])*math.pi/180.0 for attribute in ('angle_alpha', 'angle_beta', 'angle_gamma')] (alpha, beta, gamma) = [float(row[cell.getAttributeIndex(attribute)])*math.pi/180.0 for attribute in ('angle_alpha', 'angle_beta', 'angle_gamma')]
self.topology.setPeriodicBoxVectors(computePeriodicBoxVectors(a, b, c, alpha, beta, gamma)) self.topology.setPeriodicBoxVectors(computePeriodicBoxVectors(a, b, c, alpha, beta, gamma))
......
...@@ -3,6 +3,7 @@ from simtk.openmm.app import * ...@@ -3,6 +3,7 @@ from simtk.openmm.app import *
from simtk.openmm import * from simtk.openmm import *
from simtk.unit import * from simtk.unit import *
import simtk.openmm.app.element as elem import simtk.openmm.app.element as elem
import cStringIO
class TestPdbFile(unittest.TestCase): class TestPdbFile(unittest.TestCase):
"""Test the PDB file parser""" """Test the PDB file parser"""
...@@ -39,6 +40,26 @@ class TestPdbFile(unittest.TestCase): ...@@ -39,6 +40,26 @@ class TestPdbFile(unittest.TestCase):
self.assertEqual('Na', atom.name) self.assertEqual('Na', atom.name)
self.assertEqual('Na', atom.residue.name) self.assertEqual('Na', atom.residue.name)
def test_WriteFile(self):
"""Write a file, read it back, and make sure it matches the original."""
pdb1 = PDBFile('systems/triclinic.pdb')
output = cStringIO.StringIO()
PDBFile.writeFile(pdb1.topology, pdb1.positions, output)
input = cStringIO.StringIO(output.getvalue())
pdb2 = PDBFile(input)
output.close();
input.close();
self.assertEqual(len(pdb2.positions), 8)
for (p1, p2) in zip(pdb1.positions, pdb2.positions):
self.assertVecAlmostEqual(p1, p2)
for (v1, v2) in zip(pdb1.topology.getPeriodicBoxVectors(), pdb2.topology.getPeriodicBoxVectors()):
self.assertVecAlmostEqual(v1, v2, 1e-4)
self.assertVecAlmostEqual(pdb1.topology.getUnitCellDimensions(), pdb2.topology.getUnitCellDimensions(), 1e-4)
for atom1, atom2 in zip(pdb1.topology.atoms(), pdb2.topology.atoms()):
self.assertEqual(atom1.element, atom2.element)
self.assertEqual(atom1.name, atom2.name)
self.assertEqual(atom1.residue.name, atom2.residue.name)
def assertVecAlmostEqual(self, p1, p2, tol=1e-7): def assertVecAlmostEqual(self, p1, p2, tol=1e-7):
unit = p1.unit unit = p1.unit
p1 = p1.value_in_unit(unit) p1 = p1.value_in_unit(unit)
......
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