-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
447 lines (409 loc) · 13.1 KB
/
utils.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
import numpy as np
from rdkit.Chem import AllChem
from ast import literal_eval
class MorganDictVectorizer(object):
'''
Morgan fingerprint generator for organic molecules Source: https://programtalk.com/vs4/python/hcji/PyFingerprint/PyFingerprint/heteroencoder.py/
'''
def __init__(self, radius=2, augment=None):
self.radius = radius
self.augment = augment #Not used
self.dims = None
def fit(self, mols):
"""Analyses the molecules and creates the key index for the creation of the dense array"""
keys=set()
for mol in mols:
fp = AllChem.GetMorganFingerprint(mol,self.radius)
keys.update(fp.GetNonzeroElements().keys())
keys = list(keys)
keys.sort()
self.keys= np.array(keys)
self.dims = len(self.keys)
def transform_mol(self, mol, misses=False):
""" transforms the mol into a dense array using the fitted keys as index
:parameter mol: the RDKit molecule to be transformed
:parameter misses: wheter to return the number of key misses for the molecule
"""
assert type(self.keys) is np.ndarray, "keys are not defined or is not an np.array, has the .fit(mols) function been used?"
#Get fingerprint as a dictionary
fp = AllChem.GetMorganFingerprint(mol,self.radius)
fp_d = fp.GetNonzeroElements()
#Prepare the array, and set the values
#TODO is there a way to vectorize and speed up this?
arr = np.zeros((self.dims,))
_misses = 0
for key, value in fp_d.items():
if key in self.keys:
arr[self.keys == key] = value
else:
_misses = _misses + 1
if misses:
return arr, _misses
else:
return arr
def transform(self, mols, misses=False):
"""Transforms a list or array of RDKit molecules into a dense array using the key dictionary (see .fit())
:parameter mols: list or array of RDKit molecules
:parameter misses: Wheter to return the number of key misses for each molecule
"""
arr = np.zeros((len(mols), self.dims))
if misses:
_misses = np.zeros((len(mols),1))
for i, mol in enumerate(mols):
arr[i,:], _misses[i] = self.transform_mol(mol, misses=misses)
return arr, _misses
else:
for i, mol in enumerate(mols):
arr[i,:] = self.transform_mol(mol, misses=False)
return arr
def rename_disordered_interrupted(truncated_code):
'''
Maps 3-letter zeolite IZA code to codes with "*" and/or "-" (disordered/interrupted)
Args:
truncated_code (str): 3-letter zeolite IZA code without "*" and/or "-"
Returns:
str: 3-letter zeolite IZA code with "*" and/or "-"
'''
disordered_interrupted = {
'BEA': '*BEA',
'MRE': '*MRE',
'STO': '*STO',
'SVY': '*-SVY',
'CTH': '*CTH',
'ITV': '-ITV',
'CLO': '-CLO',
'ITN': '*-ITN',
'UOE': '*UOE',
'IRY': '-IRY',
'SVR': '-SVR',
'LIT': '-LIT',
'IFT': '-IFT',
'SFV': '*SFV',
'IFU': '-IFU',
}
if truncated_code in disordered_interrupted.keys():
return disordered_interrupted[truncated_code]
else:
return truncated_code
def adjacent_values(vals, q1, q3):
'''
Helper function for plotting boxplot whiskers are defined by the interquartile range (IQR: Q3-Q1) and the adjacent values.
'''
upper_adjacent_value = q3 + (q3 - q1) * 1.5
upper_adjacent_value = np.clip(upper_adjacent_value, q3, vals[-1])
lower_adjacent_value = q1 - (q3 - q1) * 1.5
lower_adjacent_value = np.clip(lower_adjacent_value, vals[0], q1)
return lower_adjacent_value, upper_adjacent_value
def rmsd_matrix(A, B, squared=False, axis = 0):
"""
Compute all pairwise distances between vectors in A and B.
Parameters
----------
A : np.array
shape should be (M, K)
B : np.array
shape should be (N, K)
Returns
-------
D : np.array
A matrix D of shape (M, N). Each entry in D i,j represnets the
distance between row i in A and row j in B.
See also
--------
A more generalized version of the distance matrix is available from
scipy (https://www.scipy.org) using scipy.spatial.distance_matrix,
which also gives a choice for p-norm.
"""
M = A.shape[0]
N = B.shape[0]
assert A.shape[1] == B.shape[1], f"The number of components for vectors in A \
{A.shape[1]} does not match that of B {B.shape[1]}!"
A_dots = (A*A).sum(axis=1).reshape((M,1))*np.ones(shape=(1,N))
B_dots = (B*B).sum(axis=1)*np.ones(shape=(M,1))
if axis == 0:
D_squared = 1/M*(A_dots + B_dots -2*A.dot(B.T))
elif axis == 1:
D_squared = 1/N*(A_dots + B_dots -2*A.dot(B.T))
if squared == False:
zero_mask = np.less(D_squared, 0.0)
D_squared[zero_mask] = 0.0
return np.sqrt(D_squared)
return D_squared
def smoothsegment(seg, Nsmooth=100):
'''
Helper function for plotting circular dendrogram
'''
return np.concatenate([[seg[0]], np.linspace(seg[1], seg[2], Nsmooth), [seg[3]]])
def get_pore_type(pore_size):
if (pore_size > 0.) & (pore_size <= 8.):
return 'Small'
elif (pore_size > 8.) and (pore_size <= 10.):
return 'Medium'
elif (pore_size > 10.) and (pore_size <= 12.):
return 'Large'
elif (pore_size > 12.):
return 'Extra-large'
else:
return 'Failed'
def clean_cbus(cbu_str):
# This fixes cbus column contains a str representation of list, instead of list itself
if type(cbu_str) == str: # if str
return literal_eval(cbu_str)
else: # if not str, eg. NaN
cbu_str
# Column names for t-SNE plots
tsne_cols = [
'Si', 'Al','P','Na','K','Li','Sr','Rb','Cs','Ba','Ca','F','Ge','Ti','In','B','H2O','sda1','OH','cryst_time','cryst_temp',
'osda_asphericity_mean_0','osda_axes_mean_0','osda_axes_mean_1','osda_bertz_ct_mean_0',
'osda_eccentricity_mean_0','osda_free_sasa_mean_0',
'osda_gyration_radius_mean_0','osda_inertial_shape_factor_mean_0', 'osda_npr1_mean_0', 'osda_npr2_mean_0',
'osda_num_bonds_mean_0', 'osda_num_rot_bonds_mean_0', 'osda_pmi1_mean_0','osda_pmi2_mean_0','osda_pmi3_mean_0','osda_spherocity_index_mean_0','osda_volume_mean_0','osda_mol_weight','osda_formal_charge',
]
# Continuous zeolite descriptors
zeo_cols_cont = [
'num_atoms',
'a',
'b',
'c',
'alpha',
'beta',
'gamma',
'volume',
'td',
'rdls',
'td_10',
'ring_size_0',
'ring_size_1',
'ring_size_2',
'ring_size_3',
'ring_size_4',
'ring_size_5',
'ring_size_6',
'framework_density',
'largest_free_sphere',
'accessible_volume_izc',
'largest_free_sphere_a',
'largest_free_sphere_b',
'largest_free_sphere_c',
'largest_free_sphere_izc',
'largest_included_sphere',
'largest_free_sphere_a_izc',
'largest_free_sphere_b_izc',
'largest_free_sphere_c_izc',
'largest_included_sphere_a',
'largest_included_sphere_b',
'largest_included_sphere_c',
'largest_included_sphere_fsp',
'largest_included_sphere_izc',
'num_atoms_per_vol',
'unitcell_vol',
'density',
'asa_a2',
'asa_m2_cm3',
'asa_m2_g',
'nasa_a2',
'nasa_m2_cm3',
'nasa_m2_g',
'chan_0_sa_a2',
'chan_1_sa_a2',
'chan_2_sa_a2',
'chan_3_sa_a2',
'chan_4_sa_a2',
'chan_5_sa_a2',
'chan_0_dim',
'sa_num_pockets',
'av_a3',
'av_frac',
'av_cm3_g',
'nav_a3',
'nav_frac',
'nav_cm3_g',
'chan_0_vol_a3',
'chan_1_vol_a3',
'chan_2_vol_a3',
'chan_3_vol_a3',
'chan_4_vol_a3',
'chan_5_vol_a3',
'avol_num_pockets',
'poav_a3',
'poav_frac',
'poav_cm3_g',
'ponav_a3',
'ponav_frac',
'ponav_cm3_g',
'probe_rad',
'N_points',
'probe_ctr_A_fract',
'probe_ctr_NA_fract',
'A_fract',
'NA_fract',
'narrow_fract',
'ovlpvfract',
'deriv_mean',
'deriv_variance',
'deriv_skewness',
'deriv_kurtosis',
'cum_mean',
'cum_variance',
'cum_skewness',
'cum_kurtosis',
'num_si',
]
# Discrete zeolite descriptors
zeo_cols_disc = [
'chan_num_channels',
'sa_num_channels',
'avol_num_channels',
'isdisordered',
'isinterrupted',
]
cols_to_drop = [
'doi',
'normed',
'seed',
'aging_time',
'aging_temp',
'rotation',
'Seed_type',
'react_vol',
'pH',
'osda1',
'osda2',
'osda3',
'product1',
'product2',
'product3',
'precursors',
'brands',
'Si/Al',
'yield',
'percent cryst',
'crystal size',
'micropore volume',
'micropore diameter',
'bet area',
'external surface area',
'Notes',
'title',
'abstract_keywords',
'recipe_keywords',
'osda1 synonyms',
'osda2 synonyms',
'osda3 synonyms',
'osda1 iupac',
'osda2 iupac',
'osda3 iupac',
'osda1 smiles',
'osda2 smiles',
'osda3 smiles',
'osda1 formula',
'osda2 formula',
'osda3 formula',
'Code1',
'Code2',
'Code3',
'year',
]
# OSDA features for classification model
osda_cols = [
'asphericity',
'axes',
'bertz_ct',
'binding',
'eccentricity',
'formal_charge',
'free_sasa',
'getaway',
'gyration_radius',
'inertial_shape_factor',
'mol_weight',
'npr1',
'npr2',
'num_bonds',
'num_rot_bonds',
'pmi1',
'pmi2',
'pmi3',
'spherocity_index',
'volume',
'whim',
]
X_cols = {
# Gel composition
'Si': 'Si',
'Al': 'Al',
'P' : 'P',
'Na': 'Na',
'K' : 'K',
'Li': 'Li',
'Sr': 'Sr',
'Rb': 'Rb',
'Cs': 'Cs',
'Ba': 'Ba',
'Ca': 'Ca',
'F' : 'F',
'Ge': 'Ge',
'Ti': 'Ti',
'B' : 'B',
'Mg': 'Mg',
'Ga': 'Ga',
'Zn': 'Zn',
'Be': 'Be',
'W' : 'W',
'Cu': 'Cu',
'Sn': 'Sn',
'Zr': 'Zr',
'V' : 'V',
'H2O': 'H$_2$O',
'sda1': 'OSDA amount',
'OH': 'OH',
# Reaction conditions
'cryst_time': 'Crystallization time',
'cryst_temp': 'Crystallization temp',
# OSDA descriptors
'osda1_asphericity_mean_0': 'OSDA asphericity',
'osda1_axes_mean_0': 'OSDA axis 1',
'osda1_axes_mean_1': 'OSDA axis 2',
'osda1_formal_charge': 'OSDA charge',
'osda1_free_sasa_mean_0': 'OSDA SASA',
'osda1_mol_weight': 'OSDA molecular weight',
'osda1_npr1_mean_0': 'OSDA NPR 1',
'osda1_npr2_mean_0': 'OSDA NPR 2',
'osda1_num_rot_bonds_mean_0': 'OSDA rotatable bonds',
'osda1_pmi1_mean_0': 'OSDA PMI 1',
'osda1_pmi2_mean_0': 'OSDA PMI 2',
'osda1_pmi3_mean_0': 'OSDA PMI 3',
'osda1_spherocity_index_mean_0': 'OSDA sphericity',
'osda1_volume_mean_0': 'OSDA volume',
}
# Zeolites according to pore size
s_pore = ['CHA', 'LTA', 'AEI', 'LEV', 'ANA', 'SOD', 'AEN', 'GIS', 'NON', 'AST',
'ERI', 'DDR', 'RTH', 'MTN', 'MRT', 'RHO', 'ITE', 'AFX', 'ITW', 'DOH',
'AWO', 'RUT', 'MER', 'ABW', 'PHI', 'LOS', 'EDI', 'SAV', 'SAS', 'MTF',
'KFI', 'ZON', 'ATN', 'AFN', 'PAU', 'NSI', 'SGT', 'JSW', 'AFV', 'AVL',
'SWY', 'UFI', 'ETL', 'APC', 'RTE', 'JBW', 'APD', 'CDO', 'MWF', 'MSO',
'SVV', 'AVE', 'IHW', 'UOZ', 'OWE', 'DFT', 'PWN', 'AWW', 'SFW', 'ATT',
'LTJ', 'ESV', 'THO', 'LTN', 'EEI', 'POR', 'ACO', 'IRN', 'CAS', 'EAB',
'BCT', 'ATV', 'SAT']
m_pore = ['MFI', 'MWW', 'TON', 'AEL', 'FER', 'MTT', 'MEL', 'STF', '*MRE', 'ITH',
'EUO', 'STW', 'SZR', 'AFO', 'STT', 'IMF', 'CSV', 'TUN', 'CGS', 'MFS',
'NES', 'STI', 'NAT', 'PON', 'LAU', 'HEU', '*UOE', 'UOS', 'PWW', 'ITR',
'-SVR', '-LIT', 'SFF', 'IFW', 'JST', 'PTY', 'SBN', 'SFG', 'RSN', 'VSV',
'PWO', 'EWS', 'RRO', 'JRY', 'MVY', 'CGF', 'ETV', 'AHT']
l_pore = ['*BEA', 'AFI', 'MTW', 'FAU', 'MOR', 'BEC', 'EMT', 'IWR', 'ATO', 'MAZ',
'*STO', 'LTL', 'ISV', 'IFR', 'IWW', 'VET', 'ATS', 'OFF', 'MEI', 'GME',
'CON', 'BPH', 'SFO', 'MSE', 'CAN', 'UOV', 'IWV', 'SFE', 'ITG', '*-ITN',
'EZT', 'AFY', 'AFS', 'AFR', 'ASV', 'SFS', 'SAF', 'SSY', 'SBE', 'EON',
'IWS', 'YFI', 'SAO', 'BOG', 'USI', 'DFO', '*SFV', 'BSV', 'CZP', 'RWY',
'UWY', 'MOZ', 'GON', 'SOS', 'SSF', 'PUN', 'SOV', 'SOR', 'POS', 'LTF',
'SOF', 'JSR']
xl_pore = ['UTL', 'IRR', '*-SVY', '*CTH', 'ITT', '-ITV', 'CFI', '-CLO', 'VFI',
'-IRY', 'IFO', '-IFT', 'SFH', 'SFN', '-IFU', 'ETR', 'DON']
# Small cbus
small_cbus = [
'lov', 'nat', 'vsv', 'mei', 'sti', 'bea', 'bre', 'jbw', 'mtt', 'afi', 'ats', 'bog', 'cas', 'lau', 'rth', 'bik', 'fer',
# 'ifw', # not present in df_zeos
'abw', 'bph', 'mel', 'mtw', 'non', 'ton', 'aww', 'ddr', 'rte', 'can', 'mso', 'gis', 'mtn', 'atn', 'gme', 'obw', 'phi', 'sod',
'd3r', 'd4r', 'd6r', 'd8r']
# Large cbus
large_cbus = ['rut', 'lev', 'mwf', 'los', 'clo', 'pau', 'ast', 'ave', 'cha', 'doh', 'eab', 'pcr', 'boz', 'lio', 'aft', 'afy', 'lta', 'ltl']