-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathenergy_eval.py
825 lines (735 loc) · 29.2 KB
/
energy_eval.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
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
import base64
import json
import os
import copy
from io import BytesIO
from typing import Tuple
import matplotlib.pyplot as plt
import mdtraj
import numpy as np
import pandas as pd
from forcebalance.molecule import Molecule as fb_molecule
from forcebalance.openmmio import MTSVVVRIntegrator
from openff.toolkit.topology import Molecule, Topology
from openff.toolkit.typing.engines.smirnoff.forcefield import ForceField
from scipy import optimize
from simtk import openmm, unit
def get_MM_energy(system, positions):
"""
Return the single point potential energy.
Parameters
----------
system : simtk.openmm.System
The system to check
positions : simtk.unit.Quantity of dimension (natoms,3) with units of length
The positions to use
Returns
---------
energy : simtk.unit.Quantity
sum of total energy
"""
integrator = MTSVVVRIntegrator(
300 * unit.kelvin,
1 / unit.picoseconds,
1.0 * unit.femtoseconds,
system,
ninnersteps=int(1 / 0.25),
)
# openmm.VerletIntegrator(1.0 * unit.femtoseconds)
context = openmm.Context(system, integrator)
context.setPositions(positions)
state = context.getState(getEnergy=True)
energy = state.getPotentialEnergy().in_units_of(unit.kilocalorie_per_mole)
return energy
def show_oemol_struc(oemol, torsions=False, atom_indices=[]):
"""
Returns the oedepict image with or without the torsion highlighted
Parameters
----------
oemol: openeye oechem mol object
torsions: boolean, to highlight dihedrals
atom_indices: dihedral atom indices to highlight
Returns
-------
Image: image in png format
"""
from IPython.display import Image
from openeye import oechem, oedepict
width = 400
height = 300
# Highlight element of interest
class NoAtom(oechem.OEUnaryAtomPred):
def __call__(self, atom):
return False
class AtomInTorsion(oechem.OEUnaryAtomPred):
def __call__(self, atom):
return atom.GetIdx() in atom_indices
class NoBond(oechem.OEUnaryBondPred):
def __call__(self, bond):
return False
class BondInTorsion(oechem.OEUnaryBondPred):
def __call__(self, bond):
return (bond.GetBgn().GetIdx() in atom_indices) and (
bond.GetEnd().GetIdx() in atom_indices
)
class CentralBondInTorsion(oechem.OEUnaryBondPred):
def __call__(self, bond):
return (bond.GetBgn().GetIdx() in atom_indices[1:3]) and (
bond.GetEnd().GetIdx() in atom_indices[1:3]
)
opts = oedepict.OE2DMolDisplayOptions(width, height, oedepict.OEScale_AutoScale)
opts.SetAtomPropertyFunctor(oedepict.OEDisplayAtomIdx())
oedepict.OEPrepareDepiction(oemol)
img = oedepict.OEImage(width, height)
display = oedepict.OE2DMolDisplay(oemol, opts)
if torsions:
atoms = oemol.GetAtoms(AtomInTorsion())
bonds = oemol.GetBonds(NoBond())
abset = oechem.OEAtomBondSet(atoms, bonds)
oedepict.OEAddHighlighting(
display,
oechem.OEColor(oechem.OEYellow),
oedepict.OEHighlightStyle_BallAndStick,
abset,
)
oedepict.OERenderMolecule(img, display)
png = oedepict.OEWriteImageToString("png", img)
return Image(png)
def get_wbo(offmol, dihedrals):
"""
Returns the specific wbo of the dihedral bonds
Parameters
----------
offmol: openforcefield molecule
dihedrals: list of atom indices in dihedral
Returns
-------
bond.fractional_bond_order: wiberg bond order calculated using openforcefield toolkit for the specific dihedral central bond
"""
offmol.assign_fractional_bond_orders(bond_order_model="am1-wiberg-elf10")
bond = offmol.get_bond_between(dihedrals[1], dihedrals[2])
return bond.fractional_bond_order
def get_torsion_barrier(energies, angles):
"""
gets the torsion barrier as the maximum barrier among the barriers present in the torsion profile
Parameters
----------
energies: list of energies along a torsion profile, can be MM or QM (y-axis values)
angles: list of corresponding angles (x-axis values)
Returns
-------
torsion_barriers: torsion barrier in the provided energies units
"""
idx = []
flat_list = [item for sublist in angles for item in sublist]
angles = np.array(flat_list)
angles = angles * np.pi / 180
# appending the first three and last three energies to the end and beginning energies so as to have a continuous curve and all barriers at the edge are also covered
angles = np.append(
angles[-3:] - 2 * np.pi, np.append(angles, angles[:3] + 2 * np.pi)
)
energies = np.append(energies[-3:], np.append(energies, energies[:3]))
for i in range(len(angles) - 2):
m1 = (energies[i + 1] - energies[i]) / (angles[i + 1] - angles[i])
m2 = (energies[i + 2] - energies[i + 1]) / (angles[i + 2] - angles[i + 1])
if np.sign(m1) == np.sign(m2):
continue
else:
idx.append(i + 1)
torsion_barriers = []
for i in range(int(len(idx) - 1)):
torsion_barriers.append(abs(energies[idx[i]] - energies[idx[i + 1]]))
# 4.184 KJ/mol = 1 kcal/mol
torsion_barriers = np.array(torsion_barriers)
return max(torsion_barriers)
def zero_dihedral_contribution(openmm_system: openmm.System, dihedral_indices: Tuple[int, int]):
"""
Author Simon Boothroyd
Link gist.github.com/SimonBoothroyd/667b50314c628aabe5064f0defb6ad8e
Zeroes out the contributions of a particular dihedral to the total potential
energy for a given OpenMM system object.
Parameters
----------
openmm_system:
The OpenMM system object which will be modified so the specified dihedral will
not contribute to the total potential energy of the system.
dihedral_indices:
The indices of the two central atoms in the dihedral whose contributions should
be zeroed.
"""
torsion_forces = [
force
for force in openmm_system.getForces()
if isinstance(force, openmm.PeriodicTorsionForce)
]
for torsion_force in torsion_forces:
for torsion_index in range(torsion_force.getNumTorsions()):
(
index_i,
index_j,
index_k,
index_l,
periodicity,
phase,
k,
) = torsion_force.getTorsionParameters(torsion_index)
if (
index_j not in [dihedral_indices[0], dihedral_indices[1]]
or index_k not in [dihedral_indices[0], dihedral_indices[1]]
):
continue
torsion_force.setTorsionParameters(
torsion_index,
index_i,
index_j,
index_k,
index_l,
periodicity,
phase,
0.0
)
def build_restrained_context(offxml, molfile, traj, dihedrals):
"""
Builds openmm context for a full MM calculation keeping the dihedral frozen
and applying a restraint of 1 kcal/mol on atoms not involved in torsion
Parameters
----------
offxml: forcefield file
molfile: molecule file in sdf format
positions: list of positions of all conformers
dihedrals: list of atom indices in the dihedral
Returns
-------
list of openmm contexts
"""
contexts = []
forcefield = ForceField(offxml, allow_cosmetic_attributes=True)
molecule = Molecule.from_file(molfile, allow_undefined_stereo=True)
for pos in traj:
system = forcefield.create_openmm_system(molecule.to_topology())
add_restraints_to_system(system, pos * unit.angstrom, dihedrals)
for force in system.getForces():
if force.__class__.__name__ == "CustomExternalForce":
force.setForceGroup(11)
for i in dihedrals:
system.setParticleMass(i, 0.0)
integrator = openmm.LangevinIntegrator(
300.0 * unit.kelvin, 1.0 / unit.picosecond, 2.0 * unit.femtosecond
)
platform = openmm.Platform.getPlatformByName("Reference")
contexts.append(openmm.Context(system, integrator, platform))
return contexts
def build_restrained_context_for_residual(offxml, molfile, traj, dihedrals):
"""
Builds openmm context for a full MM calculation keeping the dihedral frozen
and applying a restraint of 1 kcal/mol on atoms not involved in torsion
Parameters
----------
offxml: forcefield file
molfile: molecule file in sdf format
positions: list of positions of all conformers
dihedrals: list of atom indices in the dihedral
Returns
-------
list of openmm contexts
"""
contexts = []
forcefield = ForceField(offxml, allow_cosmetic_attributes=True)
molecule = Molecule.from_file(molfile, allow_undefined_stereo=True)
for pos in traj:
system = forcefield.create_openmm_system(molecule.to_topology())
add_restraints_to_system(system, pos * unit.angstrom, dihedrals)
for force in system.getForces():
if force.__class__.__name__ == "CustomExternalForce":
force.setForceGroup(11)
zero_dihedral_contribution(system, (dihedrals[1], dihedrals[2]))
for i in dihedrals:
system.setParticleMass(i, 0.0)
integrator = openmm.LangevinIntegrator(
300.0 * unit.kelvin, 1.0 / unit.picosecond, 2.0 * unit.femtosecond
)
platform = openmm.Platform.getPlatformByName("Reference")
contexts.append(openmm.Context(system, integrator, platform))
return contexts
def build_context_residual(offxml, molfile, dihedrals):
"""Builds context for the openmm calculation keeping the dihedral frozen and the
specific energy contributions from the dihedral zeroed out
Parameters
----------
offxml: forcefield file
molfile: molecule file in sdf format
dihedrals: list of atom indices in the dihedral
Returns
-------
"""
forcefield = ForceField(offxml, allow_cosmetic_attributes=True)
molecule = Molecule.from_file(molfile, allow_undefined_stereo=True)
system = forcefield.create_openmm_system(molecule.to_topology())
# freeze the dihedral atoms so that the dihedral angle stays fixed
for i in dihedrals:
system.setParticleMass(i, 0.0)
# zero out the energy contributions from the dihedral atoms
zero_dihedral_contribution(system, (dihedrals[1], dihedrals[2]))
integrator = openmm.LangevinIntegrator(
300.0 * unit.kelvin, 1.0 / unit.picosecond, 2.0 * unit.femtosecond
)
platform = openmm.Platform.getPlatformByName("Reference")
context = openmm.Context(system, integrator, platform)
return context
def add_restraints_to_system(system, positions, dihedrals):
k = 1.0 * unit.kilocalories_per_mole / unit.angstroms ** 2
for i in range(system.getNumParticles()):
if i not in dihedrals:
positional_restraint = openmm.CustomExternalForce(
"0.5 * k * ((x-x0)^2 + (y-y0)^2 + (z-z0)^2)"
)
positional_restraint.addPerParticleParameter("k")
positional_restraint.addPerParticleParameter("x0")
positional_restraint.addPerParticleParameter("y0")
positional_restraint.addPerParticleParameter("z0")
position = positions[i]
positional_restraint.addParticle(
i,
[
k,
position[0].value_in_unit(unit.nanometers),
position[1].value_in_unit(unit.nanometers),
position[2].value_in_unit(unit.nanometers),
],
)
system.addForce(positional_restraint)
def evaluate_minimized_energies(context, trajectory, dihedrals):
"""
Evaluate minimized energies for all the geometries in the trajectory in given openmm context
Parameters
----------
context: openmm context
trajectory: list of (list of xyz coordinates) for different points on the trajectory, usually for different points on the torsion profile
Returns
-------
list of energies in kcal/mol
"""
energies = []
min_pos = []
rmsd_pos = []
for frame_geo in trajectory:
initial_positions = frame_geo * unit.angstrom
context.setPositions(initial_positions)
# apply restraint of 1 kcal/mol per ang^2 to non-torsion atoms
# add_restraints_to_system(context.getSystem(), frame_geo * unit.angstrom, dihedrals)
openmm.LocalEnergyMinimizer_minimize(
context, tolerance=5.0e-09, maxIterations=15000
)
energy = context.getState(getEnergy=True).getPotentialEnergy()
energy = energy.value_in_unit(unit.kilocalories_per_mole)
minimized_positions = (
context.getState(getPositions=True)
.getPositions(asNumpy=True)
.value_in_unit(unit.angstroms)
)
rmsd_pos.append(
get_rmsd(
initial_positions.value_in_unit(unit.angstroms), minimized_positions
)
)
min_pos.append(minimized_positions)
energies.append(energy)
return np.array(energies), min_pos, rmsd_pos
def evaluate_restrained_energies(contexts, fbmol, dihedrals):
"""
Evaluate minimized energies for all the geometries in the trajectory in given openmm context
Parameters
----------
context: openmm context
trajectory: list of (list of xyz coordinates) for different points on the trajectory, usually for different points on the torsion profile
Returns
-------
list of energies in kcal/mol
"""
energies = []
min_pos = []
rmsd_pos = []
trajectory = fbmol.xyzs
fmol = copy.deepcopy(fbmol)
for context, frame_geo in zip(contexts, trajectory):
initial_positions = frame_geo * unit.angstrom
context.setPositions(initial_positions)
openmm.LocalEnergyMinimizer_minimize(
context, tolerance=5.0e-09, maxIterations=1500
)
# Remove the restraint energy from the total energy
groups = set(range(32))
# frc = context.getSystem().getForce(11)
groups.remove(11)
energy = context.getState(getEnergy=True, groups=groups).getPotentialEnergy()
energy = energy.value_in_unit(unit.kilocalories_per_mole)
minimized_positions = (
context.getState(getPositions=True)
.getPositions(asNumpy=True)
.value_in_unit(unit.angstroms)[:]
)
fmol.xyzs = [frame_geo, minimized_positions]
fmol.align(center=False)
rmsd_pos.append(
get_rmsd(
fmol.xyzs[0]*unit.angstroms, fmol.xyzs[1]*unit.angstroms
)
)
min_pos.append(fmol.xyzs[1]*unit.angstroms)
energies.append(energy)
return np.array(energies), min_pos, rmsd_pos
def plot_energies_data(energies_data_dict, title, ylab):
"""
Build the torsion profile plots from the energy dict for all the forcefields or energy data tagged per key in the dict
Parameters
----------
energies_data_dict: dict, energy key and the associated list of energies, eg., 'QM': [e1, e2, e3,...]
title: string, plot title
ylab: string, label on the y-axis
Returns
-------
plot in iobytes/string format
"""
CB_color_cycle = ["#0072B2", "#009E73", "#D55E00", "#F0E442", "#CC79A7", "#56B4E9", "#0072B2", "#009E73", "#D55E00",
"#F0E442", "#CC79A7", "#56B4E9"]
marks = ["P", "o", "^", "X", "p", 4, 5, 6, 7]
plt.Figure(figsize=(3, 3))
x_axis = energies_data_dict.pop("td_angles")
count = 0
for dataname, datavalues in energies_data_dict.items():
if dataname == "QM":
plt.plot(
x_axis,
datavalues,
color=CB_color_cycle[count],
linestyle="solid",
marker="D",
label=dataname,
markersize=7,
)
else:
if ylab == "res":
dataname = "QM - " + dataname + "_intrinsic"
plt.plot(
x_axis,
datavalues,
color=CB_color_cycle[count],
linestyle="dashed",
marker=marks[count - 1],
label=dataname,
markersize=8,
)
count = count + 1
plt.legend()
if ylab == "res":
plt.title(title + " Residuals")
plt.ylabel("Residuals (QM - MM_intrinsic) [ kcal/mol ]")
plt.xlabel("Torsion Angle [ degree ]")
elif ylab == "rel":
plt.title(title + " Relative energies")
plt.ylabel("Relative energies (QM Vs MM_full) [ kcal/mol ]")
plt.xlabel("Torsion Angle [ degree ]")
plot_iobytes = BytesIO()
plt.savefig(plot_iobytes, format="png", dpi=150)
plt.close()
return plot_iobytes
def plot_rmsd_data(rmsd_dict, title):
"""
Build the rmsd profile plots from the rmsd dict for all the forcefields or energy data tagged per key in the dict
Parameters
----------
rmsd_dict: dict, energy key and the associated list of energies, eg., 'QM': [e1, e2, e3,...]
title: string, plot title
Returns
-------
plot in iobytes/string format
"""
CB_color_cycle = ["#0072B2", "#009E73", "#D55E00", "#F0E442", "#CC79A7", "#56B4E9", "#0072B2", "#009E73", "#D55E00", "#F0E442", "#CC79A7", "#56B4E9"]
marks = ["P", "o", "^", "X", "p", 4, 5, 6, 7]
plt.Figure(figsize=(3, 3))
x_axis = rmsd_dict.pop("td_angles")
count = 0
for dataname, datavalues in rmsd_dict.items():
plt.plot(
x_axis,
datavalues,
color=CB_color_cycle[count+1],
linestyle="dashed",
marker=marks[count],
label=dataname,
markersize= 8,
)
count = count + 1
plt.legend()
plt.title(title + " RMSD wrt initial pos")
plt.ylabel("RMSD in Angstroms")
plt.xlabel("Torsion Angle [ degree ]")
plot_iobytes = BytesIO()
plt.savefig(plot_iobytes, format="png", dpi=150)
plt.close()
return plot_iobytes
def fig2inlinehtml(fig):
"""
small hack to convert image data to image string for html display
Parameters
----------
fig: image to be converted
Returns
-------
imgestr: image in string format
"""
figfile = fig.data
# for python 3.x:
figdata_png = base64.b64encode(figfile).decode()
imgstr = '<img src="data:image/png;base64,{}" />'.format(figdata_png)
return imgstr
def get_assigned_torsion_param(molecule, forcefield, dihedrals):
"""
for a molecule and specific dihedral check the assigned torsion parameter
Parameters
----------
molecule: openforcefield molecule object
ff: ForceField offxml file
dihedrals: list of atom indices in the dihedral
Returns
-------
parameter.id: str of the torsion parameter associated with the dihedral
"""
topology = Topology.from_molecules([molecule])
# Run the molecule labeling
molecule_force_list = forcefield.label_molecules(topology)
# Print out a formatted description of the parameters applied to this molecule
for mol_idx, mol_forces in enumerate(molecule_force_list):
for force_tag, force_dict in mol_forces.items():
if force_tag == "ProperTorsions":
for (atom_indices, parameter) in force_dict.items():
if atom_indices == tuple(dihedrals) or tuple(
reversed(atom_indices)
) == tuple(dihedrals):
return parameter.id
def create_energy_dataframe(subdirs, offxml_list, molfile_format, qdata_filename):
"""
Creates a dataframe with the entries listed under cols below
"""
cols = [
"Torsion ID",
"assigned params",
"wbo",
"Max - min for (QM - [MM_SF3, MM_1.3.0]) kcal/mol",
"Chemical Structure",
"QM-MM_intrinsic relative energies",
"QM Vs MM_full",
"rmsd",
]
df = pd.DataFrame(columns=cols)
ff_names = [os.path.splitext(os.path.basename(f))[0] for f in offxml_list]
ff_dict = {}
for key, ff_loc in zip(ff_names, offxml_list):
ff_dict[key] = ForceField(ff_loc, allow_cosmetic_attributes=True)
count = 0
for mol_folder in subdirs:
count += 1
# if(count > 10):
# break
mol_folder = mol_folder.rstrip()
if count % 10 == 0:
print("processed ", count)
count += 1
mol_file = mol_folder + "/" + molfile_format
mol = Molecule.from_file(
mol_folder + "/" + molfile_format, allow_undefined_stereo=True
)
# build openmm contexts for each force field file
# find scan trajectories
traj_files = [
f for f in os.listdir(mol_folder) if os.path.splitext(f)[-1] == ".xyz"
]
# create output subfolder
mol_name = os.path.basename(mol_folder)
with open(mol_folder + "/metadata.json") as json_data:
metadata = json.load(json_data)
dihedrals = metadata["dihedrals"][0]
assigned_torsion_params = dict(
(
os.path.splitext(os.path.basename(key))[0],
get_assigned_torsion_param(mol, ff_dict[key], dihedrals),
)
for key in ff_dict.keys()
)
# {key: value for (key, value) in iterable}[ for x in offxml_list]
# zero out the assigned torsion parameter for this particular dihedral to get the intrinsic torsion potential
# "(full QM energy) - (full MM energy, locally optimized, with that specific torsion zeroed out)"
for i in range(len(offxml_list)):
key = os.path.splitext(os.path.basename(offxml_list[i]))[0]
# contexts_full = [build_context_full(offxml, mol_file, dihedrals) for offxml in offxml_list]
wbo = get_wbo(mol, dihedrals)
for f in traj_files:
# print(f"- {f}")
# hold energy data for this trajectory
energies_data_dict = {}
energies_full_dict = {}
tb_dict = {}
rmsd_dict = {}
# use ForceBalance.molecule to read the xyz file
fb_mol = fb_molecule(os.path.join(mol_folder, f))
# read torsion angles
with open(mol_folder + "/metadata.json") as json_data:
metadata = json.load(json_data)
energies_data_dict["td_angles"] = metadata["torsion_grid_ids"]
rmsd_dict["td_angles"] = metadata["torsion_grid_ids"]
# read QM energies
qdata = np.genfromtxt(
str(mol_folder) + "/" + str(qdata_filename),
delimiter="ENERGY ",
dtype=None,
)
# print(qdata.shape)
eqm = qdata[:, 1]
# record the index of the minimum energy structure
ground_idx = np.argmin(eqm)
eqm = [627.509 * (x - eqm[ground_idx]) for x in eqm]
energies_data_dict["QM"] = eqm
energies_full_dict["QM"] = eqm
tb_dict["QM"] = get_torsion_barrier(
energies_data_dict["QM"], energies_data_dict["td_angles"]
)
# print("WBO of conformers")
for i in range(len(fb_mol.xyzs)):
g_frame = fb_mol.xyzs[i]
pos = np.array(g_frame) * unit.angstrom
mol.assign_fractional_bond_orders(
bond_order_model="am1-wiberg-elf10"
) # , use_conformers=[pos])
bond = mol.get_bond_between(dihedrals[1], dihedrals[2])
# print(metadata["torsion_grid_ids"][i], bond.fractional_bond_order)
### FULL
# evalute full mm energies for each force field
energies_full_dict = energies_data_dict.copy()
for offxml, ffname in zip(offxml_list, ff_names):
contexts_full = build_restrained_context(
offxml, mol_file, fb_mol.xyzs, dihedrals
)
mm_energies, minimized_pos, rmsd = evaluate_restrained_energies(
contexts_full, fb_mol, dihedrals
)
# mm_energies -= mm_energies[ground_idx]
mm_energies -= mm_energies.min()
key_name = ffname + "_full"
energies_full_dict[ffname] = mm_energies
rmsd_dict[ffname] = rmsd
tb_dict[key_name] = get_torsion_barrier(
energies_full_dict[ffname], energies_full_dict["td_angles"]
)
### INTRINSIC
# evalute intrinsic mm energies for each force field
for offxml, ffname in zip(offxml_list, ff_names):
contexts_res = build_restrained_context_for_residual(
offxml, mol_file, fb_mol.xyzs, dihedrals
)
mm_energies, _, _ = evaluate_restrained_energies(
contexts_res, fb_mol, dihedrals
)
# mm_energies -= mm_energies[ground_idx]
mm_energies -= mm_energies.min()
energies_data_dict[ffname] = mm_energies
energies_data_dict[ffname] = (
energies_data_dict["QM"] - energies_data_dict[ffname]
)
key_name = ffname + "_residual"
tb_dict[key_name] = get_torsion_barrier(
energies_data_dict[ffname], energies_data_dict["td_angles"]
)
plot_iobytes_full = plot_energies_data(energies_full_dict, mol_name, "rel")
figdata_png_full = base64.b64encode(plot_iobytes_full.getvalue()).decode()
plot_str_full = '<img src="data:image/png;base64,{}" />'.format(
figdata_png_full
)
plot_iobytes = plot_energies_data(energies_data_dict, mol_name, "res")
figdata_png = base64.b64encode(plot_iobytes.getvalue()).decode()
plot_str = '<img src="data:image/png;base64,{}" />'.format(figdata_png)
rmsd_iobytes = plot_rmsd_data(rmsd_dict, mol_name)
rmsddata_png = base64.b64encode(rmsd_iobytes.getvalue()).decode()
plot_rmsd = '<img src="data:image/png;base64,{}" />'.format(rmsddata_png)
oemol = mol.to_openeye()
dihedral_indices = metadata["dihedrals"][0]
struc_str = fig2inlinehtml(
show_oemol_struc(oemol, torsions=True, atom_indices=dihedral_indices)
)
info_dict = {}
tid = mol_name[4:]
info_dict["assigned_params"] = assigned_torsion_params
df = df.append(
{
cols[0]: tid,
cols[1]: info_dict["assigned_params"].values(),
cols[2]: wbo,
cols[3]: tb_dict,
cols[4]: struc_str,
cols[5]: plot_str,
cols[6]: plot_str_full,
cols[7]: plot_rmsd,
},
ignore_index=True,
)
return df
def fix_dihedral(molecule, system, dihedrals):
"""
Author: Simon Boothroyd
impose a dihedral angle constraint so that bonds and angles can change during optimization
"""
mdtraj_trajectory = mdtraj.Trajectory(
xyz=molecule.conformers[0],
topology=mdtraj.Topology.from_openmm(molecule.topology.to_openmm()),
)
dihedral_angle = mdtraj.compute_dihedrals(mdtraj_trajectory, np.array([dihedrals]))[
0
][0].item()
dihedral_restraint = openmm.CustomTorsionForce(
f"k * min(min(abs(theta - theta_0), abs(theta - theta_0 + 2 * "
f"{np.pi})), abs(theta - theta_0 - 2 * {np.pi}))^2"
)
dihedral_restraint.addPerTorsionParameter("k")
dihedral_restraint.addPerTorsionParameter("theta_0")
theta_0 = dihedral_angle
k = 1.0 * unit.kilocalories_per_mole / unit.radian ** 2
dihedral_restraint.addTorsion(
dihedrals[0],
dihedrals[1],
dihedrals[2],
dihedrals[3],
[k, theta_0],
)
system.addForce(dihedral_restraint)
def target_function(xyz, context):
"""
for using different optimizers from scipy defining the target function as change in positions
"""
context.setPositions(xyz.reshape(-1, 3))
state = context.getState(getEnergy=True, getForces=True)
frc = state.getForces(asNumpy=True)
ene = state.getPotentialEnergy().value_in_unit(unit.kilocalorie_per_mole)
frc = frc.value_in_unit(unit.kilocalorie_per_mole / unit.nanometer)
return ene, -frc.flatten()
def use_scipy_opt(pos, context):
"""
using a scipy optimization method for openmm optimizations
"""
pos = pos.value_in_unit(unit.nanometer)
results = optimize.minimize(target_function, pos, context, method="SLSQP")
# L-BFGS-B', jac=True, options=dict(maxiter=20000, disp=True))
if results.success:
print(results.nit, ", ", results.fun)
else:
print("didn't converge for mol")
return results.fun * unit.kilocalorie_per_mole
def get_rmsd(initial, final):
"""
Evaluate the RMSD between two arrays
"""
assert len(initial) == len(final)
n = len(initial)
if n == 0:
return 0.0
diff = np.subtract(initial, final)
rmsd = np.sqrt(np.sum(diff ** 2) / n)
return rmsd