forked from comocheng/RMG-database
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconvertPrIMe.py
97 lines (76 loc) · 3.67 KB
/
convertPrIMe.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
#!/usr/bin/env python
# encoding: utf-8
import sys
from rmgpy.data.kinetics import KineticsDatabase
from rmgpy.data.rmg import RMGDatabase
def convertPrIMe(family):
print 'Loading kinetics families...'
database = RMGDatabase()
database.kinetics = KineticsDatabase()
database.kinetics.loadFamilies('input/kinetics/families')
database.loadForbiddenStructures('input/forbiddenStructures.py')
for depository in database.kinetics.families[family].depositories:
if depository.label == '{0}/PrIMe'.format(family):
break
else:
raise Exception('Could not find PrIMe depository in {0} family.'.format(family))
entries = []
print 'Determining unique list of reactions...'
for entry0 in depository.entries.values():
for entry in entries:
if entry.item.isIsomorphic(entry0.item):
break
else:
entries.append(entry0)
print 'Found {0:d} unique reactions out of {1:d} entries.'.format(len(entries), len(depository.entries))
print 'Sorting unique reactions...'
entries.sort(key=lambda entry: sum([1 for r in entry.item.reactants for a in r.atoms if a.isNonHydrogen()]))
print 'Saving reactions...'
for index, entry in enumerate(entries):
label = entry.label
reaction = entry.item
# Determine degeneracy in both directions
reactions = database.kinetics.generateReactionsFromFamilies(reaction.reactants, reaction.products, only_families=[family])
if len(reactions) != 1:
print 'Warning: could not determine forward degeneracy for reaction #{0:d}.'.format(index+1)
forwardDegeneracy = 1
else:
forwardDegeneracy = reactions[0].degeneracy
reactions = database.kinetics.generateReactionsFromFamilies(reaction.products, reaction.reactants, only_families=[family])
if len(reactions) != 1:
print 'Warning: could not determine reverse degeneracy for reaction #{0:d}.'.format(index+1)
reverseDegeneracy = 1
else:
reverseDegeneracy = reactions[0].degeneracy
saveReaction('input/kinetics/families/{0}/training/{0}.py'.format(family, index+1), index+1, label, reaction, forwardDegeneracy, reverseDegeneracy)
def saveReaction(filename, index, label, reaction, forwardDegeneracy, reverseDegeneracy):
with open(filename, 'w') as f:
f.write('#!/usr/bin/env python\n')
f.write('# encoding: utf-8\n\n')
f.write('reaction(\n')
f.write(' index = {0:d},\n'.format(index))
f.write(' label = "{0!s}",\n'.format(label))
f.write(' reactant1 = \n')
f.write('"""\n')
f.write(reaction.reactants[0].toAdjacencyList())
f.write('""",\n')
f.write(' reactant2 = \n')
f.write('"""\n')
f.write(reaction.reactants[1].toAdjacencyList())
f.write('""",\n')
f.write(' product1 = \n')
f.write('"""\n')
f.write(reaction.products[0].toAdjacencyList())
f.write('""",\n')
f.write(' product2 = \n')
f.write('"""\n')
f.write(reaction.products[1].toAdjacencyList())
f.write('""",\n')
f.write(' forwardDegeneracy = {0:d},\n'.format(forwardDegeneracy))
f.write(' reverseDegeneracy = {0:d},\n'.format(reverseDegeneracy))
f.write(')\n\n')
################################################################################
if __name__ == '__main__':
if len(sys.argv) != 2:
raise Exception('You must provide the name of the reaction family as the lone command-line argument.')
convertPrIMe(family=sys.argv[1])