forked from choderalab/gbff
-
Notifications
You must be signed in to change notification settings - Fork 1
/
utils.py
583 lines (439 loc) · 20 KB
/
utils.py
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
#!/usr/bin/env python
#=============================================================================================
# MODULE DOCSTRING
#=============================================================================================
"""
Utilities for Bayesian parameterization of GBSA models based on hydration free energies of small molecules.
AUTHORS
John D. Chodera <[email protected]>
The AtomTyper class is based on 'patty' by Pat Walters, Vertex Pharmaceuticals.
"""
#=============================================================================================
# GLOBAL IMPORTS
#=============================================================================================
import os
import os.path
import time
import copy
import simtk.openmm as openmm
import simtk.unit as units
import simtk.openmm.app as app
import openeye.oechem
from openeye import oechem
import numpy as np
import numpy.linalg as linalg
from atomtyping import type_atoms
#=============================================================================================
# Constants
#=============================================================================================
kB = units.BOLTZMANN_CONSTANT_kB * units.AVOGADRO_CONSTANT_NA # Boltzmann constant
def read_gbsa_parameters(filename):
"""
Read a GBSA parameter set from a file.
ARGUMENTS
filename (string) - the filename to read parameters from
RETURNS
parameters (dict) - parameters[(atomtype,parameter_name)] contains the dimensionless parameter
TODO
* Replace this with a standard format?
"""
parameters = dict()
infile = open(filename, 'r')
for line in infile:
# Strip trailing comments
index = line.find('%')
if index != -1:
line = line[0:index]
# Parse parameters
elements = line.split()
if len(elements) == 3:
[atomtype, radius, scalingFactor] = elements
parameters['%s_%s' % (atomtype,'radius')] = float(radius)
parameters['%s_%s' % (atomtype,'scalingFactor')] = float(scalingFactor)
return parameters
#=============================================================================================
# Generate simulation data.
#=============================================================================================
def generate_simulation_data(database, parameters):
"""
Regenerate simulation data for given parameters.
ARGUMENTS
database (dict) - database of molecules
parameters (dict) - dictionary of GBSA parameters keyed on GBSA atom types
"""
platform = openmm.Platform.getPlatformByName("Reference")
from pymbar import timeseries
for cid in database.keys():
entry = database[cid]
molecule = entry['molecule']
iupac_name = entry['iupac']
# Retrieve vacuum system.
vacuum_system = copy.deepcopy(entry['system'])
# Retrieve OpenMM System.
solvent_system = copy.deepcopy(entry['system'])
# Get nonbonded force.
forces = { solvent_system.getForce(index).__class__.__name__ : solvent_system.getForce(index) for index in range(solvent_system.getNumForces()) }
nonbonded_force = forces['NonbondedForce']
# Add GBSA term
gbsa_force = openmm.GBSAOBCForce()
gbsa_force.setNonbondedMethod(openmm.GBSAOBCForce.NoCutoff) # set no cutoff
gbsa_force.setSoluteDielectric(1)
gbsa_force.setSolventDielectric(78)
# Build indexable list of atoms.
atoms = [atom for atom in molecule.GetAtoms()]
natoms = len(atoms)
# Assign GBSA parameters.
for (atom_index, atom) in enumerate(atoms):
[charge, sigma, epsilon] = nonbonded_force.getParticleParameters(atom_index)
atomtype = atom.GetStringData("gbsa_type") # GBSA atomtype
radius = parameters['%s_%s' % (atomtype, 'radius')] * units.angstroms
scalingFactor = parameters['%s_%s' % (atomtype, 'scalingFactor')]
gbsa_force.addParticle(charge, radius, scalingFactor)
# Add the force to the system.
solvent_system.addForce(gbsa_force)
# Create context for solvent system.
timestep = 2.0 * units.femtosecond
collision_rate = 20.0 / units.picoseconds
temperature = entry['temperature']
integrator = openmm.LangevinIntegrator(temperature, collision_rate, timestep)
context = openmm.Context(vacuum_system, integrator, platform)
# Set the coordinates.
positions = entry['positions']
context.setPositions(positions)
# Minimize.
openmm.LocalEnergyMinimizer.minimize(context)
# Simulate, saving periodic snapshots of configurations.
kT = kB * temperature
beta = 1.0 / kT
initial_time = time.time()
nsteps_per_iteration = 2500
niterations = 200
x_n = np.zeros([niterations,natoms,3], np.float32) # positions, in nm
u_n = np.zeros([niterations], np.float64) # energy differences, in kT
for iteration in range(niterations):
integrator.step(nsteps_per_iteration)
state = context.getState(getEnergy=True, getPositions=True)
x_n[iteration,:,:] = state.getPositions(asNumpy=True) / units.nanometers
u_n[iteration] = beta * state.getPotentialEnergy()
if np.any(np.isnan(u_n)):
raise Exception("Encountered NaN for molecule %s | %s" % (cid, iupac_name))
final_time = time.time()
elapsed_time = final_time - initial_time
# Clean up.
del context, integrator
# Discard initial transient to equilibration.
[t0, g, Neff_max] = timeseries.detectEquilibration(u_n)
x_n = x_n[t0:,:,:]
u_n = u_n[t0:]
# Subsample to remove correlation.
indices = timeseries.subsampleCorrelatedData(u_n, g=g)
x_n = x_n[indices,:,:]
u_n = u_n[indices]
# Store data.
entry['x_n'] = x_n
entry['u_n'] = u_n
print "%48s | %64s | simulation %12.3f s | %5d samples discarded | %5d independent samples remain" % (cid, iupac_name, elapsed_time, t0, len(indices))
return
#=============================================================================================
# Computation of hydration free energies
#=============================================================================================
def compute_hydration_energies(database, parameters):
"""
Compute solvation energies of a set of molecules given a GBSA parameter set.
ARGUMENTS
molecules (list of OEMol) - molecules with GBSA assigned atom types in type field
parameters (dict) - dictionary of GBSA parameters keyed on GBSA atom types
RETURNS
energies (dict) - energies[molecule] is the computed solvation energy of given molecule
"""
energies = dict() # energies[index] is the computed solvation energy of molecules[index]
platform = openmm.Platform.getPlatformByName("Reference")
from pymbar import MBAR
if 'gbmodel' in parameters:
gbmodel = parameters['gbmodel'].value
else:
gbmodel = None
for cid in database.keys():
entry = database[cid]
molecule = entry['molecule']
iupac_name = entry['iupac']
# Retrieve OpenMM System.
vacuum_system = entry['system']
solvent_system = copy.deepcopy(entry['system'])
# Get nonbonded force.
forces = { solvent_system.getForce(index).__class__.__name__ : solvent_system.getForce(index) for index in range(solvent_system.getNumForces()) }
nonbonded_force = forces['NonbondedForce']
# Add GBSA force.
from simtk.openmm.app.internal import customgbforces
if gbmodel is None:
gbsa_force = openmm.GBSAOBCForce()
gbsa_force.setNonbondedMethod(openmm.GBSAOBCForce.NoCutoff) # set no cutoff
gbsa_force.setSoluteDielectric(1)
gbsa_force.setSolventDielectric(78)
elif gbmodel == 0:
gbsa_force = customgbforces.GBSAHCTForce(SA='ACE')
elif gbmodel == 1:
gbsa_force = customgbforces.GBSAOBC1Force(SA='ACE')
elif gbmodel == 2:
gbsa_force = customgbforces.GBSAOBC2Force(SA='ACE')
elif gbmodel == 3:
gbsa_force = customgbforces.GBSAGBnForce(SA='ACE')
elif gbmodel == 4:
gbsa_force = customgbforces.GBSAGBn2Force(SA='ACE')
# Build indexable list of atoms.
atoms = [atom for atom in molecule.GetAtoms()]
natoms = len(atoms)
# Assign GBSA parameters.
for (atom_index, atom) in enumerate(atoms):
[charge, sigma, epsilon] = nonbonded_force.getParticleParameters(atom_index)
atomtype = atom.GetStringData("gbsa_type") # GBSA atomtype
radius = parameters['%s_%s' % (atomtype, 'radius')] * units.angstroms
scalingFactor = parameters['%s_%s' % (atomtype, 'scalingFactor')]
if gbmodel is None:
gbsa_force.addParticle(charge, radius, scalingFactor)
else:
gbsa_force.addParticle([charge, radius, scalingFactor])
# Add the force to the system.
solvent_system.addForce(gbsa_force)
# Create context for solvent system.
timestep = 2.0 * units.femtosecond
solvent_integrator = openmm.VerletIntegrator(timestep)
solvent_context = openmm.Context(solvent_system, solvent_integrator, platform)
# Create context for vacuum system.
vacuum_integrator = openmm.VerletIntegrator(timestep)
vacuum_context = openmm.Context(vacuum_system, vacuum_integrator, platform)
# Compute energy differences.
temperature = entry['temperature']
kT = kB * temperature
beta = 1.0 / kT
initial_time = time.time()
x_n = entry['x_n']
u_n = entry['u_n']
nsamples = len(u_n)
nstates = 3 # number of thermodynamic states
u_kln = np.zeros([3,3,nsamples], np.float64)
for sample in range(nsamples):
positions = units.Quantity(x_n[sample,:,:], units.nanometers)
u_kln[0,0,sample] = u_n[sample]
vacuum_context.setPositions(positions)
vacuum_state = vacuum_context.getState(getEnergy=True)
u_kln[0,1,sample] = beta * vacuum_state.getPotentialEnergy()
solvent_context.setPositions(positions)
solvent_state = solvent_context.getState(getEnergy=True)
u_kln[0,2,sample] = beta * solvent_state.getPotentialEnergy()
N_k = np.zeros([nstates], np.int32)
N_k[0] = nsamples
mbar = MBAR(u_kln, N_k)
try:
df_ij, ddf_ij, _ = mbar.getFreeEnergyDifferences()
except linalg.LinAlgError:
return np.inf
DeltaG_in_kT = df_ij[1,2]
dDeltaG_in_kT = ddf_ij[1,2]
final_time = time.time()
elapsed_time = final_time - initial_time
print "%48s | %48s | reweighting took %.3f s" % (cid, iupac_name, elapsed_time)
# Clean up.
del solvent_context, solvent_integrator
del vacuum_context, vacuum_integrator
energies[molecule] = kT * DeltaG_in_kT
print "%48s | %48s | DeltaG = %.3f +- %.3f kT" % (cid, iupac_name, DeltaG_in_kT, dDeltaG_in_kT)
print ""
return energies
def compute_hydration_energy(entry, parameters, hydration_factory_parameters, platform_name="Reference"):
"""
Compute hydration energy of a single molecule given a GBSA parameter set.
ARGUMENTS
molecule (OEMol) - molecule with GBSA atom types
parameters (dict) - parameters for GBSA atom types
RETURNS
energy (float) - hydration energy in kcal/mol
"""
platform = openmm.Platform.getPlatformByName(platform_name)
from pymbar import MBAR
gbmodel = hydration_factory_parameters['gbmodel'].value
molecule = entry['molecule']
iupac_name = entry['iupac']
cid = molecule.GetData('cid')
# Retrieve OpenMM System.
vacuum_system = entry['system']
solvent_system = copy.deepcopy(entry['system'])
# Get nonbonded force.
forces = { solvent_system.getForce(index).__class__.__name__ : solvent_system.getForce(index) for index in range(solvent_system.getNumForces()) }
nonbonded_force = forces['NonbondedForce']
# Add GBSA force.
from simtk.openmm.app.internal import customgbforces
if gbmodel is None:
gbsa_force = openmm.GBSAOBCForce()
gbsa_force.setNonbondedMethod(openmm.GBSAOBCForce.NoCutoff) # set no cutoff
gbsa_force.setSoluteDielectric(1)
gbsa_force.setSolventDielectric(78)
elif gbmodel == 0:
gbsa_force = customgbforces.GBSAHCTForce(SA='ACE')
elif gbmodel == 1:
gbsa_force = customgbforces.GBSAOBC1Force(SA='ACE')
elif gbmodel == 2:
gbsa_force = customgbforces.GBSAOBC2Force(SA='ACE')
elif gbmodel == 3:
gbsa_force = customgbforces.GBSAGBnForce(SA='ACE')
elif gbmodel == 4:
gbsa_force = customgbforces.GBSAGBn2Force(SA='ACE')
else:
print("GBmodel %i out of range" % gbmodel)
# Build indexable list of atoms.
atoms = [atom for atom in molecule.GetAtoms()]
natoms = len(atoms)
# Assign GBSA parameters.
for (atom_index, atom) in enumerate(atoms):
[charge, sigma, epsilon] = nonbonded_force.getParticleParameters(atom_index)
atomtype = atom.GetStringData("gbsa_type") # GBSA atomtype
radius = parameters['%s_%s' % (atomtype, 'radius')] * units.angstroms
scalingFactor = parameters['%s_%s' % (atomtype, 'scalingFactor')]
if gbmodel is None:
gbsa_force.addParticle(charge, radius, scalingFactor)
else:
gbsa_force.addParticle([charge, radius, scalingFactor])
# Add the force to the system.
solvent_system.addForce(gbsa_force)
# Create context for solvent system.
timestep = 2.0 * units.femtosecond
solvent_integrator = openmm.VerletIntegrator(timestep)
solvent_context = openmm.Context(solvent_system, solvent_integrator, platform)
# Create context for vacuum system.
vacuum_integrator = openmm.VerletIntegrator(timestep)
vacuum_context = openmm.Context(vacuum_system, vacuum_integrator, platform)
# Compute energy differences.
temperature = entry['temperature']
kT = kB * temperature
beta = 1.0 / kT
initial_time = time.time()
x_n = entry['x_n']
u_n = entry['u_n']
nsamples = len(u_n)
nstates = 3 # number of thermodynamic states
u_kln = np.zeros([3,3,nsamples], np.float64)
for sample in range(nsamples):
positions = units.Quantity(x_n[sample,:,:], units.nanometers)
u_kln[0,0,sample] = u_n[sample]
vacuum_context.setPositions(positions)
vacuum_state = vacuum_context.getState(getEnergy=True)
u_kln[0,1,sample] = beta * vacuum_state.getPotentialEnergy()
solvent_context.setPositions(positions)
solvent_state = solvent_context.getState(getEnergy=True)
u_kln[0,2,sample] = beta * solvent_state.getPotentialEnergy()
N_k = np.zeros([nstates], np.int32)
N_k[0] = nsamples
mbar = MBAR(u_kln, N_k)
try:
df_ij, ddf_ij, _ = mbar.getFreeEnergyDifferences()
except linalg.LinAlgError:
return np.inf
DeltaG_in_kT = df_ij[1,2]
dDeltaG_in_kT = ddf_ij[1,2]
final_time = time.time()
elapsed_time = final_time - initial_time
#print "%48s | %48s | reweighting took %.3f s" % (cid, iupac_name, elapsed_time)
# Clean up.
del solvent_context, solvent_integrator
del vacuum_context, vacuum_integrator
energy = kT * DeltaG_in_kT
print "%48s | %48s | DeltaG = %.3f +- %.3f kT | gbmodel = %d" % (cid, iupac_name, DeltaG_in_kT, dDeltaG_in_kT, gbmodel)
#print ""
return energy / units.kilocalories_per_mole
def hydration_energy_factory(entry, hydration_factory_parameters):
def hydration_energy(**parameters):
return compute_hydration_energy(entry, parameters, hydration_factory_parameters, platform_name="Reference")
return hydration_energy
#=============================================================================================
# Prepare the FreeSolv-format database for calculations.
#=============================================================================================
def prepare_database(database, atomtypes_filename,parameters, mol2_directory, verbose=False):
"""
Wrapper function to prepare the database for sampling
"""
database_prepped = load_database(database, mol2_directory, verbose=verbose)
database_with_systems = create_openmm_systems(database_prepped, verbose=verbose)
database_atomtyped = type_atoms(database_with_systems, atomtypes_filename, verbose=verbose)
database_simulated = generate_simulation_data(database_atomtyped, parameters)
return database_simulated
def load_database(database, mol2_directory, verbose=False):
"""
This function prepares the database that will be use in sampling.
Arguments
---------
database : dict
an unpickled version of the FreeSolv database
mol2_directory : String
the path to the FreeSolv mol2 files containing geometry and charges
verbose : Boolean, optional
verbosity
Returns
-------
database : dict
An updated version of the database dict containing OEMols
"""
start_time = time.time()
if verbose:
print("Reading all molecules in dataset. Will use charges and coordinates from dataset.")
for cid in database.keys():
entry = database[cid]
# Store temperature
# TODO: Get this from database?
entry['temperature'] = 300.0 * units.kelvin
# Extract relevant entry data from database.
smiles = entry['smiles']
iupac_name = entry['iupac']
experimental_DeltaG = entry['expt'] * units.kilocalories_per_mole
experimental_dDeltaG = entry['d_expt'] * units.kilocalories_per_mole
# Read molecule.
molecule = openeye.oechem.OEMol()
# Load the mol2 file.
tripos_mol2_filename = os.path.join(mol2_directory, cid + '.mol2')
omolstream = oechem.oemolistream(tripos_mol2_filename)
oechem.OEReadMolecule(omolstream, molecule)
omolstream.close()
molecule.SetTitle(iupac_name)
molecule.SetData('cid', cid)
# Add explicit hydrogens.
oechem.OEAddExplicitHydrogens(molecule)
# Store molecule.
entry['molecule'] = oechem.OEMol(molecule)
if verbose:
print "%d molecules read" % len(database.keys())
end_time = time.time()
elapsed_time = end_time - start_time
print "%.3f s elapsed" % elapsed_time
return database
def create_openmm_systems(database, verbose=False, path_to_prmtops=None):
"""
Create an OpenMM system for each molecule in the database
Arguments
---------
database : dict
dict containing FreeSolv molecules (prepared using prepare_database)
verbose : Boolean, optional
verbosity
path_to_prmtops : str, optional, default=None
Path to directory containing inpcrd and prmtop files.
If None, will be set to ${FREESOLV_PATH}/mol2files_gaff/
Returns
-------
database : dict
The FreeSolv database dict containing OpenMM systems for each molecule
"""
print("Creating")
if path_to_prmtops is None:
FREESOLV_PATH = os.environ["FREESOLV_PATH"]
path_to_prmtops = os.path.join(FREESOLV_PATH + "/mol2files_gaff/")
for cid, entry in database.items():
prmtop_filename = os.path.join(path_to_prmtops, "%s.prmtop" % cid)
inpcrd_filename = os.path.join(path_to_prmtops, "%s.inpcrd" % cid)
# Create OpenMM System object for molecule in vacuum.
prmtop = app.AmberPrmtopFile(prmtop_filename)
inpcrd = app.AmberInpcrdFile(inpcrd_filename)
system = prmtop.createSystem(nonbondedMethod=app.NoCutoff, constraints=app.HBonds, implicitSolvent=None, removeCMMotion=False)
positions = inpcrd.getPositions()
# Store system and positions.
entry['system'] = system
entry['positions'] = positions
return database