-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathanalyze_grow1000.py
307 lines (230 loc) · 7.54 KB
/
analyze_grow1000.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
'''
Written by Jan H. Jensen 2018
'''
from rdkit import Chem
from rdkit.Chem import AllChem
import numpy as np
#import pickle
import operator
import collections
import re
def read_file(file_name):
smiles_list = []
with open(file_name,'r') as file:
for smiles in file:
smiles_list.append(smiles)
return smiles_list
def get_probs(smarts_list,smiles_list,ring=False):
bonds = []
probs = collections.OrderedDict()
for smarts in smarts_list:
probs[smarts] = 0
number_of_molecules = 0
tot = 0
for smiles in smiles_list:
#print smiles
number_of_molecules += 1
mol = Chem.MolFromSmiles(smiles)
Chem.Kekulize(mol)
for smarts in smarts_list:
matches = mol.GetSubstructMatches(Chem.MolFromSmarts(smarts),uniquify=ring)
num_bonds = len(matches)
probs[smarts] += num_bonds
tot += num_bonds
tot = 0
probs2 = collections.OrderedDict()
for key in probs:
if probs[key] > 0:
#print key, probs[key]
tot += probs[key]
probs2[key] = probs[key]
return tot, probs2
def clean_probs(probs):
exceptions = ['[#7]#','[#8]=','[#9]','[#17]','[#35]','[#53]']
probs2 = collections.OrderedDict()
# for key in probs:
# skip = False
# for exception in exceptions:
# if exception in key:
# tokens = re.split('\[|\]|;',key)
# alt_key = '['+tokens[3]+']'+tokens[2]+'['+tokens[1]+';!R]'
# probs[alt_key] += probs[key]
for key in probs:
skip = False
for exception in exceptions:
if exception in key:
skip = True
if not skip:
probs2[key] = probs[key]
tot = 0
for key in probs2:
tot += probs2[key]
return tot, probs2
return probs
def get_p(probs):
p = []
for key in probs:
p.append(float(probs[key])/tot)
return p
def get_rxn_smarts_make_rings(probs):
X = {'[#6R': 'X4', '[#7R': 'X3'}
rxn_smarts = []
for key in probs:
tokens = key.split(']')
smarts = ''
if '=' in key:
smarts += tokens[0][:-1] + X[tokens[0]] + ';!R:1]'
else:
smarts += tokens[0][:-1] + ';!R:1]=,'
smarts += tokens[2][:-1] + ';!R:2]>>'
smarts += '[*:1]1' + tokens[1] + '][*:2]1'
rxn_smarts.append(smarts)
return rxn_smarts
def get_rxn_smarts_rings(probs):
X = {'[#6R': 'X4', '[#7R': 'X3'}
rxn_smarts = []
for key in probs:
tokens = key.split(']')
smarts = ''
if '=' in key:
smarts += tokens[0] + X[tokens[0]] + ';!r6;!r7;!R2:1]'
else:
smarts += tokens[0] + ';!r6;!r7;!R2:1]'
smarts += tokens[2] + ';!r6;!r7:2]>>'
smarts += '[*:1]' + tokens[1] + '][*:2]'
rxn_smarts.append(smarts)
return rxn_smarts
def get_rxn_smarts(probs):
rxn_smarts = []
for key in probs:
smarts = ''
tokens = key.split(']')
smarts = tokens[0]
if '-' in key and '#16' not in smarts: # key <-> smarts
smarts += ';!H0:1]>>[*:1]'
if '=' in key and '#16' not in smarts: # key <-> smarts
smarts += ';!H1;!H0:1]>>[*:1]'
if ']#[' in key:
smarts += ';H3:1]>>[*:1]'
if '#16' in smarts: # key <-> smarts
smarts += ':1]>>[*:1]'
smarts += tokens[-2] + ']'
rxn_smarts.append(smarts)
return rxn_smarts
def get_mean_size(smiles_list):
size = []
for smiles in smiles_list:
mol = Chem.MolFromSmiles(smiles)
num_atoms = mol.GetNumAtoms()
size.append(num_atoms)
return np.mean(size), np.std(size)
def count_macro_cycles(smiles_list,smarts_list,tot,probs):
#probs = collections.OrderedDict()
for smarts in smarts_list:
probs[smarts] = 0
for smiles in smiles_list:
for smarts in smarts_list:
mol = Chem.MolFromSmiles(smiles)
Chem.Kekulize(mol)
matches = mol.GetSubstructMatches(Chem.MolFromSmarts(smarts),uniquify=True)
if len(matches) > 0:
probs[smarts] += 1
tot += 1
return tot,probs
#################################
file_name = 'grow1000.smi'
elements = ['#5','#6','#7','#8','#9','#14','#15','#16','#17','#35','#53']
bonds = ['-','=','#']
smiles_list = read_file(file_name)
mean_size, size_stdv = get_mean_size(smiles_list)
print('mean number of non-H atoms',mean_size,'+/-', size_stdv)
print('')
smarts = ['[*]','[R]','[!R]','[R2]']
tot,probs = get_probs(smarts,smiles_list)
print('Probability of ring atoms',float(probs['[R]'])/probs['[*]'])
print('Probability of non-ring atoms',float(probs['[!R]'])/probs['[*]'])
print('Probability of fused-ring atoms',float(probs['[R2]'])/probs['[*]'])
print('')
smarts = ['[R]~[R]~[R]','[R]-[R]-[R]','[R]=[R]-[R]']
tot,probs = get_probs(smarts,smiles_list,ring=True)
#print(tot,probs)
print('Probability of [R]-[R]-[R]',float(probs['[R]-[R]-[R]'])/probs['[R]~[R]~[R]'])
print('Probability of [R]=[R]-[R]',float(probs['[R]=[R]-[R]'])/probs['[R]~[R]~[R]'])
print('')
smarts = []
for element in elements:
smarts.append('['+element+']')
#print(get_probs(smarts,smiles_list))
smarts = []
for element in elements:
smarts.append('['+element+'R]')
tot_Ratoms,probs_Ratoms = get_probs(smarts,smiles_list)
#print (tot_Ratoms,probs_Ratoms)
R_elements = []
for key in probs_Ratoms:
R_elements.append(key)
#print (R_elements)
smarts = []
for i,e1 in enumerate(R_elements):
for e2 in R_elements:
for j,e3 in enumerate(R_elements):
if j >= i:
sm_s = e1 + '-' + e2 + '-' + e3
if sm_s not in smarts:
smarts.append(sm_s)
sm_d = e1 + '=' + e2 + '-' + e3
if sm_d not in smarts:
smarts.append(sm_d)
#print (len(smarts),smarts)
tot,probs = get_probs(smarts,smiles_list,ring=True)
#print (tot,probs)
sorted_x = sorted(probs.items(), key=operator.itemgetter(1), reverse=True)
count = 0
for i in range(len(sorted_x)):
print (sorted_x[i][0],sorted_x[i][1]/tot)
print('')
#print (count)
rxn_smarts_rings = get_rxn_smarts_rings(probs)
#print (rxn_smarts_rings)
rxn_smarts_make_rings = get_rxn_smarts_make_rings(probs)
#print (rxn_smarts_make_rings)
p_rings = get_p(probs)
#print (p_rings)
#pickle.dump(p_rings,open('p_ring.p','wb'))
#pickle.dump(rxn_smarts_rings,open('rs_ring.p','wb'))
#pickle.dump(rxn_smarts_make_rings,open('rs_make_ring.p','wb'))
smarts = []
for bond in bonds:
for element1 in elements:
for element2 in elements:
smarts.append('['+element1+']'+bond+'['+element2+';!R]')
#print (len(smarts))
tot,probs = get_probs(smarts,smiles_list)
tot,probs = clean_probs(probs)
#print (tot, probs)
p = get_p(probs)
#print (p)
sorted_x = sorted(probs.items(), key=operator.itemgetter(1), reverse=True)
count = 0
for i in range(len(sorted_x)):
print (sorted_x[i][0],sorted_x[i][1]/tot)
rxn_smarts = get_rxn_smarts(probs)
#print (rxn_smarts)
#pickle.dump(p,open('p1.p','wb'))
#pickle.dump(rxn_smarts,open('r_s1.p','wb'))
smarts_list = ['[*]1-[*]-[*]-1','[*]1-[*]=[*]-1','[*]1-[*]-[*]-[*]-1','[*]1=[*]-[*]-[*]-1','[*]1=[*]-[*]=[*]-1',
'[*]1-[*]-[*]-[*]-[*]-1','[*]1=[*]-[*]-[*]-[*]-1','[*]1=[*]-[*]=[*]-[*]-1',
'[*]1-[*]-[*]-[*]-[*]-[*]-1','[*]1=[*]-[*]-[*]-[*]-[*]-1','[*]1=[*]-[*]=[*]-[*]-[*]-1',
'[*]1=[*]-[*]-[*]=[*]-[*]-1','[*]1=[*]-[*]=[*]-[*]=[*]-1']
smarts_macro = ['[r;!r3;!r4;!r5;!r6;!r8;!r9;!r10;!r11;!r12]','[r;!r3;!r4;!r5;!r6;!r7;!r9;!r10;!r11;!r12]',
'[r;!r3;!r4;!r5;!r6;!r7;!r8;!r10;!r11;!r12]','[r;!r3;!r4;!r5;!r6;!r7;!r8;!r9;!r11;!r12]',
'[r;!r3;!r4;!r5;!r6;!r7;!r8;!r9;!r10;!r12]','[r;!r3;!r4;!r5;!r6;!r7;!r8;!r9;!r10;!r11]']
tot, probs = get_probs(smarts_list,smiles_list,ring=True)
tot, probs = count_macro_cycles(smiles_list,smarts_macro,tot,probs)
num_rings = 0
print('')
for key in probs:
print(key,probs[key])
num_rings += probs[key]
print('')
print('number of rings',num_rings)