"platforms/cuda-old/src/CudaKernels.h" did not exist on "1b2ebaf0212fd822a7afd21f566ff9201c2fa98e"
desmonddmsfile.py 21.5 KB
Newer Older
1
"""
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
desmonddmsfile.py: Load Desmond dms files

Portions copyright (c) 2013 Stanford University and the Authors
Authors: Robert McGibbon
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.
26
"""
27

28
import os
29
30
31
32
33
34
import math

from simtk import openmm as mm
from simtk.openmm.app import forcefield as ff
from simtk.openmm.app import Element, Topology, PDBFile
from simtk.openmm.app.element import hydrogen
Robert McGibbon's avatar
Robert McGibbon committed
35
from simtk.unit import (nanometer, angstrom, dalton, radian,
36
                        kilocalorie_per_mole, kilojoule_per_mole,
Robert McGibbon's avatar
Robert McGibbon committed
37
                        degree, elementary_charge)
38
39
40


class DesmondDMSFile(object):
41
    """DesmondDMSFile parses a Desmond DMS (desmond molecular system) and
42
    constructs a topology and (optionally) an OpenMM System from it
43
    """
44
45

    def __init__(self, file):
46
        """Load a DMS file
47
48

        Parameters:
49
50
         - file (string) the name of the file to load
        """
51
52
53
54
55
56
57
58
59
60

        # sqlite3 is included in the standard lib, but at python
        # compile time, you can disable support (I think), so it's
        # not *guarenteed* to be available. Doing the import here
        # means we only raise an ImportError if people try to use
        # this class, so the module can be safely imported
        import sqlite3

        self._open = False
        self._tables = None
61
62
        if not  os.path.exists(str(file)):
            raise IOError("No such file or directory: '%s'" % str(file))
63
64
65
66
        self._conn = sqlite3.connect(file)
        self._open = True
        self._readSchemas()

67
68
        if len(self._tables) == 0:
            raise IOError('DMS file was not loaded sucessfully. No tables found')
69
70
71
72
73
        if 'nbtype' not in self._tables['particle']:
            raise ValueError('No nonbonded parameters associated with this '
                             'DMS file. You can add a forcefield with the '
                             'viparr command line tool distributed with desmond')

Robert McGibbon's avatar
Robert McGibbon committed
74
75
        # build the provenance string
        provenance = []
76
77
        q = """SELECT id, user, timestamp, version, workdir, cmdline, executable
        FROM provenance"""
Robert McGibbon's avatar
Robert McGibbon committed
78
79
80
81
82
83
84
        #for id, user, timestamp, version, workdir, cmdline, executable in self._conn.execute(q):
        for row in self._conn.execute('SELECT * FROM provenance'):
            rowdict = dict(zip(self._tables['provenance'], row))
            provenance.append('%(id)d) %(timestamp)s: %(user)s\n  version: %(version)s\n  '
                              'cmdline: %(cmdline)s\n  executable: %(executable)s\n' % rowdict)
        self.provenance = ''.join(provenance)

85
86
87
88
89
90
91
        # Build the topology
        self.topology, self.positions = self._createTopology()
        self._topologyAtoms = list(self.topology.atoms())
        self._atomBonds = [{} for x in range(len(self._topologyAtoms))]
        self._angleConstraints = [{} for x in range(len(self._topologyAtoms))]

    def getPositions(self):
92
93
        """Get the positions of each atom in the system
        """
94
95
96
        return self.positions

    def getTopology(self):
97
98
        """Get the topology of the system
        """
99
100
        return self.topology

Robert McGibbon's avatar
Robert McGibbon committed
101
    def getProvenance(self):
102
103
        """Get the provenance string of this system
        """
Robert McGibbon's avatar
Robert McGibbon committed
104
105
        return self.provenance

106
    def _createTopology(self):
107
108
        """Build the topology of the system
        """
109
110
111
112
113
        top = Topology()
        positions = []

        boxVectors = []
        for x, y, z in self._conn.execute('SELECT x, y, z FROM global_cell'):
114
            boxVectors.append(mm.Vec3(x, y, z))
115
        unitCellDimensions = [boxVectors[0][0], boxVectors[1][1], boxVectors[2][2]]
116
        top.setUnitCellDimensions(unitCellDimensions*angstrom)
117
118
119
120
121

        atoms = {}
        lastChain = None
        lastResId = None
        c = top.addChain()
122
123
        q = """SELECT id, name, anum, resname, resid, chain, x, y, z
        FROM particle"""
124
        for (atomId, atomName, atomNumber, resName, resId, chain, x, y, z) in self._conn.execute(q):
125
            newChain = False
126
127
128
            if chain != lastChain:
                lastChain = chain
                c = top.addChain()
129
130
                newChain = True
            if resId != lastResId or newChain:
131
132
133
134
135
136
137
138
139
                lastResId = resId
                if resName in PDBFile._residueNameReplacements:
                    resName = PDBFile._residueNameReplacements[resName]
                r = top.addResidue(resName, c)
                if resName in PDBFile._atomNameReplacements:
                    atomReplacements = PDBFile._atomNameReplacements[resName]
                else:
                    atomReplacements = {}

140
141
142
143
144
            if atomNumber == 0 and atomName.startswith('Vrt'):
                elem = None
            else:
                elem = Element.getByAtomicNumber(atomNumber)

145
146
            if atomName in atomReplacements:
                atomName = atomReplacements[atomName]
147

148
            atoms[atomId] = top.addAtom(atomName, elem, r)
149
            positions.append(mm.Vec3(x, y, z))
150
151
152
153

        for p0, p1 in self._conn.execute('SELECT p0, p1 FROM bond'):
            top.addBond(atoms[p0], atoms[p1])

Robert McGibbon's avatar
Robert McGibbon committed
154
        positions = positions*angstrom
155
156
        return top, positions

Robert McGibbon's avatar
Robert McGibbon committed
157
    def createSystem(self, nonbondedMethod=ff.NoCutoff, nonbondedCutoff=1.0*nanometer,
158
                     ewaldErrorTolerance=0.0005, removeCMMotion=True, hydrogenMass=None):
159
        """Construct an OpenMM System representing the topology described by this dms file
160
161

        Parameters:
162
163
164
165
166
167
168
169
         - nonbondedMethod (object=NoCutoff) The method to use for nonbonded interactions.  Allowed values are
           NoCutoff, CutoffNonPeriodic, CutoffPeriodic, Ewald, or PME.
         - nonbondedCutoff (distance=1*nanometer) The cutoff distance to use for nonbonded interactions
         - ewaldErrorTolerance (float=0.0005) The error tolerance to use if nonbondedMethod is Ewald or PME.
         - removeCMMotion (boolean=True) If true, a CMMotionRemover will be added to the System
         - hydrogenMass (mass=None) The mass to use for hydrogen atoms bound to heavy atoms.  Any mass added to a hydrogen is
           subtracted from the heavy atom to keep their total mass the same.
        """
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
        self._checkForUnsupportedTerms()
        sys = mm.System()

        # Buld the box dimensions
        sys = mm.System()
        boxSize = self.topology.getUnitCellDimensions()
        if boxSize is not None:
            sys.setDefaultPeriodicBoxVectors((boxSize[0], 0, 0), (0, boxSize[1], 0), (0, 0, boxSize[2]))
        elif nonbondedMethod in (ff.CutoffPeriodic, ff.Ewald, ff.PME):
            raise ValueError('Illegal nonbonded method for a non-periodic system')

        # Create all of the particles
        for mass in self._conn.execute('SELECT mass from particle'):
            sys.addParticle(mass[0]*dalton)

        # Add all of the forces
        self._addBondsToSystem(sys)
        self._addAnglesToSystem(sys)
        self._addConstraintsToSystem(sys)
        self._addPeriodicTorsionsToSystem(sys)
        self._addImproperHarmonicTorsionsToSystem(sys)
        self._addCMAPToSystem(sys)
192
        self._addVirtualSitesToSystem(sys)
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
        nb = self._addNonbondedForceToSystem(sys)

        # Finish configuring the NonbondedForce.
        methodMap = {ff.NoCutoff:mm.NonbondedForce.NoCutoff,
                     ff.CutoffNonPeriodic:mm.NonbondedForce.CutoffNonPeriodic,
                     ff.CutoffPeriodic:mm.NonbondedForce.CutoffPeriodic,
                     ff.Ewald:mm.NonbondedForce.Ewald,
                     ff.PME:mm.NonbondedForce.PME}
        nb.setNonbondedMethod(methodMap[nonbondedMethod])
        nb.setCutoffDistance(nonbondedCutoff)
        nb.setEwaldErrorTolerance(ewaldErrorTolerance)

        # Adjust masses.
        if hydrogenMass is not None:
            for atom1, atom2 in self.topology.bonds():
                if atom1.element == hydrogen:
                    (atom1, atom2) = (atom2, atom1)
                if atom2.element == hydrogen and atom1.element not in (hydrogen, None):
                    transferMass = hydrogenMass-sys.getParticleMass(atom2.index)
                    sys.setParticleMass(atom2.index, hydrogenMass)
                    sys.setParticleMass(atom1.index, sys.getParticleMass(atom1.index)-transferMass)

        # Add a CMMotionRemover.
        if removeCMMotion:
            sys.addForce(mm.CMMotionRemover())

        return sys

    def _addBondsToSystem(self, sys):
222
223
        """Create the harmonic bonds
        """
224
225
226
        bonds = mm.HarmonicBondForce()
        sys.addForce(bonds)

227
        q = """SELECT p0, p1, r0, fc, constrained
228
        FROM stretch_harm_term INNER JOIN stretch_harm_param
229
        ON stretch_harm_term.param=stretch_harm_param.id"""
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
        for p0, p1, r0, fc, constrained in self._conn.execute(q):
            if constrained:
                sys.addConstraint(p0, p1, r0*angstrom)
            else:
                # Desmond writes the harmonic bond force without 1/2
                # so we need to to double the force constant
                bonds.addBond(p0, p1, r0*angstrom, 2*fc*kilocalorie_per_mole/angstrom**2)

            # Record information that will be needed for constraining angles.
            self._atomBonds[p0][p1] = r0*angstrom
            self._atomBonds[p1][p0] = r0*angstrom

        return bonds

    def _addAnglesToSystem(self, sys):
245
246
        """Create the harmonic angles
        """
247
248
249
250
        angles = mm.HarmonicAngleForce()
        sys.addForce(angles)
        degToRad = math.pi/180

251
        q = """SELECT p0, p1, p2, theta0, fc, constrained
252
        FROM angle_harm_term INNER JOIN angle_harm_param
253
        ON angle_harm_term.param=angle_harm_param.id"""
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
        for p0, p1, p2, theta0, fc, constrained in self._conn.execute(q):
            if constrained:
                l1 = self._atomBonds[p1][p0]
                l2 = self._atomBonds[p1][p2]
                length = (l1*l1 + l2*l2 - 2*l1*l2*math.cos(theta0*degToRad)).sqrt()
                sys.addConstraint(p0, p2, length)
                self._angleConstraints[p1][p0] = p2
                self._angleConstraints[p1][p2] = p0
            else:
                # Desmond writes the harmonic angle force without 1/2
                # so we need to to double the force constant
                angles.addAngle(p0, p1, p2, theta0*degToRad, 2*fc*kilocalorie_per_mole/radian**2)

        return angles

    def _addConstraintsToSystem(self, sys):
270
        """Add constraints to system. Normally these should already be
271
272
        added by the bonds table, but we want to make sure that there's
        no extra information in the constraints table that we're not
273
        including in the system"""
274
275
        for term_table in [n for n in self._tables.keys() if n.startswith('constraint_a') and n.endswith('term')]:
            param_table = term_table.replace('term', 'param')
276
            q = """SELECT p0, p1, r1
277
            FROM %(term)s INNER JOIN %(param)s
278
            ON %(term)s.param=%(param)s.id""" % \
279
280
281
282
283
284
285
286
287
                {'term': term_table, 'param': param_table}
            for p0, p1, r1 in self._conn.execute(q):
                if not p1 in self._atomBonds[p0]:
                    sys.addConstraint(p0, p1, r1*angstrom)
                    self._atomBonds[p0][p1] = r1*angstrom
                    self._atomBonds[p1][p0] = r1*angstrom

        if 'constraint_hoh_term' in self._tables:
            degToRad = math.pi/180
288
            q = """SELECT p0, p1, p2, r1, r2, theta
289
            FROM constraint_hoh_term INNER JOIN constraint_hoh_param
290
            ON constraint_hoh_term.param=constraint_hoh_param.id"""
291
292
293
294
295
296
297
298
            for p0, p1, p2, r1, r2, theta in self._conn.execute(q):
                # Here, p0 is the heavy atom and p1 and p2 are the H1 and H2
                # wihth O-H1 and O-H2 distances r1 and r2
                if not (self._angleConstraints[p0].get(p1, None) == p2):
                    length = (r1*r1 + r2*r2 - 2*r1*r2*math.cos(theta*degToRad)).sqrt()
                    sys.addConstraint(p1, p2, length)

    def _addPeriodicTorsionsToSystem(self, sys):
299
300
        """Create the torsion terms
        """
301
302
303
        periodic = mm.PeriodicTorsionForce()
        sys.addForce(periodic)

304
        q = """SELECT p0, p1, p2, p3, phi0, fc0, fc1, fc2, fc3, fc4, fc5, fc6
305
        FROM dihedral_trig_term INNER JOIN dihedral_trig_param
306
        ON dihedral_trig_term.param=dihedral_trig_param.id"""
307
308
309
310
311
312
313
314
        for p0, p1, p2, p3, phi0, fc0, fc1, fc2, fc3, fc4, fc5, fc6 in self._conn.execute(q):
            for order, fc in enumerate([fc0, fc1, fc2, fc3, fc4, fc5, fc6]):
                if fc == 0:
                    continue
                periodic.addTorsion(p0, p1, p2, p3, order, phi0*degree, fc*kilocalorie_per_mole)


    def _addImproperHarmonicTorsionsToSystem(self, sys):
315
316
        """Create the improper harmonic torsion terms
        """
317
318
319
320
321
322
323
324
        if not self._hasTable('improper_harm_term'):
            return

        harmonicTorsion = mm.CustomTorsionForce('k*(theta-theta0)^2')
        harmonicTorsion.addPerTorsionParameter('theta0')
        harmonicTorsion.addPerTorsionParameter('k')
        sys.addForce(harmonicTorsion)

325
        q = """SELECT p0, p1, p2, p3, phi0, fc
326
        FROM improper_harm_term INNER JOIN improper_harm_param
327
        ON improper_harm_term.param=improper_harm_param.id"""
328
329
330
331
        for p0, p1, p2, p3, phi0, fc in self._conn.execute(q):
            harmonicTorsion.addTorsion(p0, p1, p2, p3, [phi0*degree, fc*kilocalorie_per_mole])

    def _addCMAPToSystem(self, sys):
332
333
        """Create the CMAP terms
        """
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
        if not self._hasTable('torsiontorsion_cmap_term'):
            return

        # Create CMAP torsion terms
        cmap = mm.CMAPTorsionForce()
        sys.addForce(cmap)
        cmap_indices = {}

        for name in [k for k in self._tables.keys() if k.startswith('cmap')]:
            size2 = self._conn.execute('SELECT COUNT(*) FROM %s' % name).fetchone()[0]
            fsize = math.sqrt(size2)
            if fsize != int(fsize):
                raise ValueError('Non-square CMAPs are not supported')
            size = int(fsize)

            map = [0 for i in range(size2)]
            for phi, psi, energy in self._conn.execute("SELECT phi, psi, energy FROM %s" % name):
                i = int((phi % 360) / (360.0 / size))
                j = int((psi % 360) / (360.0 / size))
                map[i+size*j] = energy
            index = cmap.addMap(size, map*kilocalorie_per_mole)
            cmap_indices[name] = index

357
        q = """SELECT p0, p1, p2, p3, p4, p5, p6, p7, cmapid
358
        FROM torsiontorsion_cmap_term INNER JOIN torsiontorsion_cmap_param
359
        ON torsiontorsion_cmap_term.param=torsiontorsion_cmap_param.id"""
360
361
362
363
        for p0, p1, p2, p3, p4, p5, p6, p7, cmapid in self._conn.execute(q):
            cmap.addTorsion(cmap_indices[cmapid], p0, p1, p2, p3, p4, p5, p6, p7)

    def _addNonbondedForceToSystem(self, sys):
364
365
        """Create the nonbonded force
        """
366
367
368
        nb = mm.NonbondedForce()
        sys.addForce(nb)

369
        q = """SELECT charge, sigma, epsilon
370
        FROM particle INNER JOIN nonbonded_param
371
        ON particle.nbtype=nonbonded_param.id"""
372
373
374
        for charge, sigma, epsilon in self._conn.execute(q):
            nb.addParticle(charge, sigma*angstrom, epsilon*kilocalorie_per_mole)

375
376
377
        for p0, p1 in self._conn.execute('SELECT p0, p1 FROM exclusion'):
            nb.addException(p0, p1, 0.0, 1.0, 0.0)

378
        q = """SELECT p0, p1, aij, bij, qij
379
        FROM pair_12_6_es_term INNER JOIN pair_12_6_es_param
380
        ON pair_12_6_es_term.param=pair_12_6_es_param.id;"""
381
382
383
384
385
386
387
388
389
390
391
        for p0, p1, a_ij, b_ij, q_ij in self._conn.execute(q):
            a_ij = (a_ij*kilocalorie_per_mole*(angstrom**12)).in_units_of(kilojoule_per_mole*(nanometer**12))
            b_ij = (b_ij*kilocalorie_per_mole*(angstrom**6)).in_units_of(kilojoule_per_mole*(nanometer**6))
            q_ij = q_ij*elementary_charge**2

            if (b_ij._value == 0.0) or (a_ij._value == 0.0):
                new_epsilon = 0
                new_sigma = 1
            else:
                new_epsilon =  b_ij**2/(4*a_ij)
                new_sigma = (a_ij / b_ij)**(1.0/6.0)
392
            nb.addException(p0, p1, q_ij, new_sigma, new_epsilon, True)
393

394
395
        n_total = self._conn.execute("""SELECT COUNT(*) FROM pair_12_6_es_term""").fetchone()
        n_in_exclusions = self._conn.execute("""SELECT COUNT(*)
396
        FROM exclusion INNER JOIN pair_12_6_es_term
397
        ON exclusion.p0==pair_12_6_es_term.p0 AND exclusion.p1==pair_12_6_es_term.p1""").fetchone()
398
399
400
401
402
        if not n_total == n_in_exclusions:
            raise NotImplementedError('All pair_12_6_es_terms must have a corresponding exclusion')

        return nb

403
    def _addVirtualSitesToSystem(self, sys):
404
405
        """Create any virtual sites in the systempy
        """
406
407
        if not any(t.startswith('virtual_') for t in self._tables.keys()):
            return
408
409

        if 'virtual_lc2_term' in self._tables:
410
            q = """SELECT p0, p1, p2, c1
411
            FROM virtual_lc2_term INNER JOIN virtual_lc2_param
412
            ON virtual_lc2_term.param=virtual_lc2_param.id"""
413
414
415
416
417
            for p0, p1, p2, c1 in self._conn.execute(q):
                vsite = mm.TwoParticleAverageSite(p1, p2, (1-c1), c1)
                sys.setVirtualSite(p0, vsite)

        if 'virtual_lc3_term' in self._tables:
418
            q = """SELECT p0, p1, p2, p3, c1, c2
419
            FROM virtual_lc3_term INNER JOIN virtual_lc3_param
420
            ON virtual_lc3_term.param=virtual_lc3_param.id"""
421
422
423
424
            for p0, p1, p2, p3, c1, c2 in self._conn.execute(q):
                vsite = mm.ThreeParticleAverageSite(p1, p2, p3, (1-c1-c2), c1, c2)
                sys.setVirtualSite(p0, vsite)

425
        if 'virtual_out3_term' in self._tables:
426
            q = """SELECT p0, p1, p2, p3, c1, c2, c3
427
            FROM virtual_out3_term INNER JOIN virtual_out3_param
428
            ON virtual_out3_term.param=virtual_out3_param.id"""
429
            for p0, p1, p2, p3, c1, c2, c3 in self._conn.execute(q):
430
431
432
433
434
435
                vsite = mm.OutOfPlaneSite(p1, p2, p3, c1, c2, c3)
                sys.setVirtualSite(p0, vsite)

        if 'virtual_fdat3_term' in self._tables:
            raise NotImplementedError('OpenMM does not currently support '
                                      'fdat3-style virtual sites')
436
437


438
    def _hasTable(self, table_name):
439
440
        """Does our DMS file contain this table?
        """
441
442
443
        return table_name in self._tables

    def _readSchemas(self):
444
        """Read the schemas of each of the tables in the dms file, populating
445
        the `_tables` instance attribute
446
        """
447
448
449
450
451
452
453
454
455
        tables = {}
        for table in self._conn.execute("SELECT name FROM sqlite_master WHERE type='table'"):
            names = []
            for e in self._conn.execute('PRAGMA table_info(%s)' % table):
                names.append(str(e[1]))
            tables[str(table[0])] = names
        self._tables = tables

    def _checkForUnsupportedTerms(self):
456
        """Check the file for forcefield terms that are not currenty supported,
457
        raising a NotImplementedError
458
        """
459
460
461
462
463
464
465
        if 'posre_harm_term' in self._tables:
            raise NotImplementedError('Position restraints are not implemented.')
        flat_bottom_potential_terms = ['stretch_fbhw_term', 'angle_fbhw_term',
                                       'improper_fbhw_term', 'posre_fbhw_term']
        if any((t in self._tables) for t in flat_bottom_potential_terms):
            raise NotImplementedError('Flat bottom potential terms '
                                      'are not implemeneted')
466

467
468
469
470
        nbinfo = dict(zip(self._tables['nonbonded_info'],
                          self._conn.execute('SELECT * FROM nonbonded_info').fetchone()))

        if nbinfo['vdw_funct'] != u'vdw_12_6':
471
472
            raise NotImplementedError('Only Leonard-Jones van der Waals '
                                      'interactions are currently supported')
473
        if nbinfo['vdw_rule'] != u'arithmetic/geometric':
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
            raise NotImplementedError('Only Lorentz-Berthelot nonbonded '
                                      'combining rules are currently supported')

        if 'nonbonded_combined_param' in self._tables:
            raise NotImplementedError('nonbonded_combined_param interactions '
                                      'are not currently supported')

        if 'alchemical_particle' in self._tables:
            raise NotImplementedError('Alchemical particles are not supported')
        if 'alchemical_stretch_harm' in self._tables:
            raise NotImplementedError('Alchemical bonds are not supported')

        if 'polar_term' in self._tables:
            if self._conn.execute("SELECT COUNT(*) FROM polar_term").fetchone()[0] != 0:
                raise NotImplementedError('Drude particles are not currently supported')
489
490

    def close(self):
491
492
        """Close the SQL connection
        """
493
494
495
496
497
        if self._open:
            self._conn.close()

    def __del__(self):
        self.close()