-
Notifications
You must be signed in to change notification settings - Fork 1
/
databasePrep.py
197 lines (175 loc) · 6.85 KB
/
databasePrep.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
"""
Loads the TS_training.py file and removes duplicate reactions, then the .data files generated
by the automated TS generator are added (again removing any duplicate reactions) and printed to
a new TS_training.py in the folder where this is run.
Reverse reactions are also duplicates, so run `duplicate.py` to generate those.
"""
import os
import logging
import numpy
from rmgpy.molecule import Molecule
from rmgpy.reaction import Reaction
from rmgpy.data.kinetics.transitionstates import DistanceData, Entry
def loadTraining(index,
reactant1=None,
reactant2=None,
reactant3=None,
product1=None,
product2=None,
product3=None,
distances=None,
degeneracy=1,
label='',
reference=None,
referenceType='',
shortDesc='',
longDesc='',
rank=None,
):
reactants = [Molecule().fromAdjacencyList(reactant1, saturateH=True)]
if reactant2 is not None: reactants.append(Molecule().fromAdjacencyList(reactant2, saturateH=True))
if reactant3 is not None: reactants.append(Molecule().fromAdjacencyList(reactant3, saturateH=True))
products = [Molecule().fromAdjacencyList(product1, saturateH=True)]
if product2 is not None: products.append(Molecule().fromAdjacencyList(product2, saturateH=True))
if product3 is not None: products.append(Molecule().fromAdjacencyList(product3, saturateH=True))
reaction = Reaction(reactants=reactants, products=products)
alreadyHave = False
for testReaction in reactionList:
if testReaction.isIsomorphic(reaction):
alreadyHave = True
if not alreadyHave:
entry = Entry(
index = index,
label = label,
item = reaction,
data = distances,
reference = reference,
referenceType = referenceType,
shortDesc = shortDesc,
longDesc = longDesc.strip(),
rank = rank,
)
reactionList.append(reaction)
entries['{0:d}:{1}'.format(index,label)] = entry
return entry
def loadEntry(index,
reactant1=None,
reactant2=None,
reactant3=None,
product1=None,
product2=None,
product3=None,
distances=None,
degeneracy=1,
label='',
reference=None,
referenceType='',
shortDesc='',
longDesc='',
rank=None,
):
reactants = [Molecule().fromAdjacencyList(reactant1, saturateH=True)]
if reactant2 is not None: reactants.append(Molecule().fromAdjacencyList(reactant2, saturateH=True))
if reactant3 is not None: reactants.append(Molecule().fromAdjacencyList(reactant3, saturateH=True))
products = [Molecule().fromAdjacencyList(product1, saturateH=True)]
if product2 is not None: products.append(Molecule().fromAdjacencyList(product2, saturateH=True))
if product3 is not None: products.append(Molecule().fromAdjacencyList(product3, saturateH=True))
reaction = Reaction(reactants=reactants, products=products)
alreadyHave = False
for testReaction in reactionList:
if testReaction.isIsomorphic(reaction):
alreadyHave = True
if not alreadyHave:
index = len(entries) + 1
entry = Entry(
index = index,
label = label,
item = reaction,
data = distances,
reference = reference,
referenceType = referenceType,
shortDesc = shortDesc,
longDesc = longDesc.strip(),
rank = rank,
)
entries['{0:d}:{1}'.format(index,label)] = entry
return entry
def saveEntries(entryList):
"""
Save the entries.
"""
with open('TS_training.py', 'w') as resultFile:
resultFile.write('#!/usr/bin/env python\n')
resultFile.write('# encoding: utf-8\n\n')
resultFile.write('name = "H_Abstraction/TS_training"\n')
resultFile.write('shortDesc = u"Distances used to train group additivity values for TS geometries"\n')
resultFile.write('longDesc = u"""\nPut interatomic distances for reactions to use as a training set for fitting\ngroup additivity values in this file.\n"""\n')
resultFile.write('recommended = True\n\n')
for i in range(len(entryList)):
entry = entryList[str(i+1)+':']
resultFile.write('entry(\n')
resultFile.write(' index = {0},\n'.format(entry.index))
resultFile.write(' reactant1 = """\n{0!s}""",\n'.format(entry.item.reactants[0].toAdjacencyList()))
resultFile.write(' reactant2 = """\n{0!s}""",\n'.format(entry.item.reactants[1].toAdjacencyList()))
resultFile.write(' product1 = """\n{0!s}""",\n'.format(entry.item.products[0].toAdjacencyList()))
resultFile.write(' product2 = """\n{0!s}""",\n'.format(entry.item.products[1].toAdjacencyList()))
resultFile.write(' distances = DistanceData(\n')
resultFile.write(' distances = {0},\n'.format(entry.data.distances))
resultFile.write(' method = "{0!s}",\n'.format(entry.data.method))
resultFile.write(' ),\n')
resultFile.write(' reference = None,\n')
resultFile.write(' referenceType = "",\n')
resultFile.write(' rank = 3,\n')
resultFile.write(' shortDesc = u"""{0!s}""",\n'.format(entry.shortDesc))
resultFile.write(' longDesc = \nu"""{0!s}\n""",\n)\n\n'.format(entry.longDesc))
# global_context = None
# local_context = None
# # Set up global and local context
# if global_context is None: global_context = {}
# global_context['__builtins__'] = None
# global_context['True'] = True
# global_context['False'] = False
# if local_context is None: local_context = {}
# local_context['__builtins__'] = None
# local_context['entry'] = loadEntry
# local_context['tree'] = self.__loadTree
# local_context['name'] = self.name
# local_context['shortDesc'] = self.shortDesc
# local_context['longDesc'] = self.longDesc
# local_context['recommended'] = False
# add in anything from the Class level dictionary.
# for key, value in Database.local_context.iteritems():
# local_context[key]=value
entries = {}
reactionList = []
filePath = os.path.abspath(os.path.join(os.getenv('RMGpy'),'../RMG-database/input/kinetics/families/H_Abstraction/TS_training.py'))
with open(filePath) as resultFile:
global_context = { '__builtins__': None }
local_context = {
'__builtins__': None,
'True': True,
'False': False,
'entry': loadTraining,
'DistanceData': DistanceData,
'array': numpy.array,
'int32': numpy.int32,
}
exec resultFile in global_context, local_context
# for i in range(1507):
# newpath = os.path.join('QMfiles', str(i+1))
# if os.path.exists(newpath):
# for files in os.listdir(newpath):
# if files.endswith('.data'):
# with open(os.path.join(newpath, files)) as resultFile:
# global_context = { '__builtins__': None }
# local_context = {
# '__builtins__': None,
# 'True': True,
# 'False': False,
# 'entry': loadEntry,
# 'DistanceData': DistanceData,
# 'array': numpy.array,
# 'int32': numpy.int32,
# }
# exec resultFile in global_context, local_context
saveEntries(entries)