ForceBalance API  1.3
Automated optimization of force fields and empirical potentials
smirnoff_hack.py
Go to the documentation of this file.
1 
2 from openforcefield.utils.toolkits import OpenEyeToolkitWrapper, RDKitToolkitWrapper, AmberToolsToolkitWrapper
3 
4 # time based on total 540s evaluation
5 # cache for OE find_smarts_matches (save 300+ s)
6 oe_original_find_smarts_matches = OpenEyeToolkitWrapper.find_smarts_matches
7 OE_TOOLKIT_CACHE_find_smarts_matches = {}
8 def oe_cached_find_smarts_matches(self, molecule, smarts, aromaticity_model='OEAroModel_MDL'):
9  cache_key = hash((molecule, smarts, aromaticity_model))
10  if cache_key not in OE_TOOLKIT_CACHE_find_smarts_matches:
11  OE_TOOLKIT_CACHE_find_smarts_matches[cache_key] = oe_original_find_smarts_matches(self, molecule, smarts, aromaticity_model=aromaticity_model)
12  return OE_TOOLKIT_CACHE_find_smarts_matches[cache_key]
13 # replace the original function with new one
14 OpenEyeToolkitWrapper.find_smarts_matches = oe_cached_find_smarts_matches
15 
16 # cache for RDK find_smarts_matches
17 rdk_original_find_smarts_matches = RDKitToolkitWrapper.find_smarts_matches
18 RDK_TOOLKIT_CACHE_find_smarts_matches = {}
19 def rdk_cached_find_smarts_matches(self, molecule, smarts, aromaticity_model='OEAroModel_MDL'):
20  cache_key = hash((molecule, smarts, aromaticity_model))
21  if cache_key not in RDK_TOOLKIT_CACHE_find_smarts_matches:
22  RDK_TOOLKIT_CACHE_find_smarts_matches[cache_key] = rdk_original_find_smarts_matches(self, molecule, smarts, aromaticity_model=aromaticity_model)
23  return RDK_TOOLKIT_CACHE_find_smarts_matches[cache_key]
24 # replace the original function with new one
25 RDKitToolkitWrapper.find_smarts_matches = rdk_cached_find_smarts_matches
26 
27 
28 # cache for the validate function (save 94s)
29 from openforcefield.typing.chemistry.environment import ChemicalEnvironment
30 original_validate = ChemicalEnvironment.validate
31 TOOLKIT_CACHE_ChemicalEnvironment_validate = {}
32 def cached_validate(smirks, validate_valence_type=True, toolkit_registry=OpenEyeToolkitWrapper):
33  cache_key = hash((smirks, validate_valence_type, toolkit_registry))
34  if cache_key not in TOOLKIT_CACHE_ChemicalEnvironment_validate:
35  TOOLKIT_CACHE_ChemicalEnvironment_validate[cache_key] = original_validate(smirks, validate_valence_type=validate_valence_type, toolkit_registry=toolkit_registry)
36  return TOOLKIT_CACHE_ChemicalEnvironment_validate[cache_key]
37 ChemicalEnvironment.validate = cached_validate
38 
39 
40 # cache for compute_partial_charges_am1bcc (save 69s)
41 # No longer needed as of 0.7.0 since all partial charge assignment is routed through ToolkitWrapper.assign_partial_charges
42 # original_compute_partial_charges_am1bcc = OpenEyeToolkitWrapper.compute_partial_charges_am1bcc
43 # TOOLKIT_CACHE_compute_partial_charges_am1bcc = {}
44 # def cached_compute_partial_charges_am1bcc(self, molecule, use_conformers=None, strict_n_conformers=False):
45 # cache_key = hash(molecule, use_conformers, strict_n_conformers)
46 # if cache_key not in TOOLKIT_CACHE_compute_partial_charges_am1bcc:
47 # TOOLKIT_CACHE_compute_partial_charges_am1bcc[cache_key] = original_compute_partial_charges_am1bcc(self, molecule, use_conformers=use_conformers, strict_n_conformers=strict_n_conformers)
48 # return TOOLKIT_CACHE_compute_partial_charges_am1bcc[cache_key]
49 # OpenEyeToolkitWrapper.compute_partial_charges_am1bcc = cached_compute_partial_charges_am1bcc
50 
51 
52 # Cache for OETK assign_partial_charges
53 oe_original_assign_partial_charges = OpenEyeToolkitWrapper.assign_partial_charges
54 OE_TOOLKIT_CACHE_assign_partial_charges = {}
55 def oe_cached_assign_partial_charges(self, molecule, partial_charge_method=None, use_conformers=None, strict_n_conformers=False):
56  cache_key = hash((molecule, partial_charge_method, str(use_conformers), strict_n_conformers))
57  if cache_key not in OE_TOOLKIT_CACHE_assign_partial_charges:
58  oe_original_assign_partial_charges(self, molecule, partial_charge_method=partial_charge_method, use_conformers=use_conformers, strict_n_conformers=strict_n_conformers)
59  OE_TOOLKIT_CACHE_assign_partial_charges[cache_key] = molecule.partial_charges
60  else:
61  molecule.partial_charges = OE_TOOLKIT_CACHE_assign_partial_charges[cache_key]
62  return
63 OpenEyeToolkitWrapper.assign_partial_charges = oe_cached_assign_partial_charges
64 
65 
66 # Cache for AmberTools assign_partial_charges
67 at_original_assign_partial_charges = AmberToolsToolkitWrapper.assign_partial_charges
68 AT_TOOLKIT_CACHE_assign_partial_charges = {}
69 def at_cached_assign_partial_charges(self, molecule, partial_charge_method=None, use_conformers=None, strict_n_conformers=False):
70  cache_key = hash((molecule, partial_charge_method, str(use_conformers), strict_n_conformers))
71  if cache_key not in AT_TOOLKIT_CACHE_assign_partial_charges:
72  at_original_assign_partial_charges(self, molecule, partial_charge_method=partial_charge_method, use_conformers=use_conformers, strict_n_conformers=strict_n_conformers)
73  AT_TOOLKIT_CACHE_assign_partial_charges[cache_key] = molecule.partial_charges
74  else:
75  molecule.partial_charges = AT_TOOLKIT_CACHE_assign_partial_charges[cache_key]
76  return
77 AmberToolsToolkitWrapper.assign_partial_charges = at_cached_assign_partial_charges
78 
79 
80 # cache the OE generate_conformers function (save 15s)
81 OE_TOOLKIT_CACHE_molecule_conformers = {}
82 oe_original_generate_conformers = OpenEyeToolkitWrapper.generate_conformers
83 def oe_cached_generate_conformers(self, molecule, n_conformers=1, rms_cutoff=None, clear_existing=True):
84  cache_key = hash((molecule, n_conformers, str(rms_cutoff), clear_existing))
85  if cache_key not in OE_TOOLKIT_CACHE_molecule_conformers:
86  oe_original_generate_conformers(self, molecule, n_conformers=n_conformers, rms_cutoff=rms_cutoff, clear_existing=clear_existing)
87  OE_TOOLKIT_CACHE_molecule_conformers[cache_key] = molecule._conformers
88  molecule._conformers = OE_TOOLKIT_CACHE_molecule_conformers[cache_key]
89 OpenEyeToolkitWrapper.generate_conformers = oe_cached_generate_conformers
90 
91 
92 # cache the RDKit generate_conformers function
93 RDK_TOOLKIT_CACHE_molecule_conformers = {}
94 rdk_original_generate_conformers = RDKitToolkitWrapper.generate_conformers
95 def rdk_cached_generate_conformers(self, molecule, n_conformers=1, rms_cutoff=None, clear_existing=True):
96  cache_key = hash((molecule, n_conformers, str(rms_cutoff), clear_existing))
97  if cache_key not in RDK_TOOLKIT_CACHE_molecule_conformers:
98  rdk_original_generate_conformers(self, molecule, n_conformers=n_conformers, rms_cutoff=rms_cutoff, clear_existing=clear_existing)
99  RDK_TOOLKIT_CACHE_molecule_conformers[cache_key] = molecule._conformers
100  molecule._conformers = RDK_TOOLKIT_CACHE_molecule_conformers[cache_key]
101 RDKitToolkitWrapper.generate_conformers = rdk_cached_generate_conformers
102 
103 
104 # final timing: 56s
105 
106 # cache the ForceField creation (no longer needed since using OpenFF API for parameter modifications)
107 
108 # import hashlib
109 # from openforcefield.typing.engines.smirnoff import ForceField
110 # SMIRNOFF_FORCE_FIELD_CACHE = {}
111 # def getForceField(*ffpaths):
112 # hasher = hashlib.md5()
113 # for path in ffpaths:
114 # with open(path, 'rb') as f:
115 # hasher.update(f.read())
116 # cache_key = hasher.hexdigest()
117 # if cache_key not in SMIRNOFF_FORCE_FIELD_CACHE:
118 # SMIRNOFF_FORCE_FIELD_CACHE[cache_key] = ForceField(*ffpaths, allow_cosmetic_attributes=True)
119 # return SMIRNOFF_FORCE_FIELD_CACHE[cache_key]
def rdk_cached_generate_conformers(self, molecule, n_conformers=1, rms_cutoff=None, clear_existing=True)
def rdk_cached_find_smarts_matches(self, molecule, smarts, aromaticity_model='OEAroModel_MDL')
def oe_cached_assign_partial_charges(self, molecule, partial_charge_method=None, use_conformers=None, strict_n_conformers=False)
def oe_cached_find_smarts_matches(self, molecule, smarts, aromaticity_model='OEAroModel_MDL')
Definition: smirnoff_hack.py:8
def oe_cached_generate_conformers(self, molecule, n_conformers=1, rms_cutoff=None, clear_existing=True)
def cached_validate(smirks, validate_valence_type=True, toolkit_registry=OpenEyeToolkitWrapper)
def at_cached_assign_partial_charges(self, molecule, partial_charge_method=None, use_conformers=None, strict_n_conformers=False)