-
Notifications
You must be signed in to change notification settings - Fork 1
/
DrawMolsToImage.py
408 lines (364 loc) · 15.2 KB
/
DrawMolsToImage.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
#!/usr/bin/env python
# coding: utf-8
# In[6]:
import numpy as np
import pandas as pd
import os
import rdkit
from rdkit import Chem
from rdkit.Chem.Draw import IPythonConsole
#from __future__ import print_function
from rdkit import Chem
from rdkit.Chem.Draw import IPythonConsole
from IPython.display import SVG
from rdkit.Chem import rdDepictor
from rdkit.Chem.Draw import rdMolDraw2D
def moltosvg(mol,molSize=(450,150),kekulize=True):
mc = Chem.Mol(mol.ToBinary())
if kekulize:
try:
Chem.Kekulize(mc)
except:
mc = Chem.Mol(mol.ToBinary())
if not mc.GetNumConformers():
rdDepictor.Compute2DCoords(mc)
drawer = rdMolDraw2D.MolDraw2DSVG(molSize[0],molSize[1])
drawer.DrawMolecule(mc)
drawer.FinishDrawing()
svg = drawer.GetDrawingText()
return svg
def render_svg(svg):
return SVG(svg.replace('svg:',''))
'''smiles = 'O'
mol = Chem.MolFromSmiles(smiles)
print('smiles:', smiles)
render_svg(moltosvg(mol))'''
from rdkit import Chem
from rdkit.Chem import AllChem
import itertools
from rdkit.Chem import rdmolops
from collections import defaultdict
import copy
import networkx as nx # uncomment if you don't want to use "quick"/install networkx
global __ATOM_LIST__
__ATOM_LIST__ = [x.strip() for x in ['h ', 'he', 'li', 'be', 'b ', 'c ', 'n ', 'o ', 'f ', 'ne', 'na', 'mg', 'al', 'si', 'p ', 's ', 'cl', 'ar', 'k ', 'ca', 'sc', 'ti', 'v ', 'cr', 'mn', 'fe', 'co', 'ni', 'cu', 'zn', 'ga', 'ge', 'as', 'se', 'br', 'kr', 'rb', 'sr', 'y ', 'zr', 'nb', 'mo', 'tc', 'ru', 'rh', 'pd', 'ag', 'cd', 'in', 'sn', 'sb', 'te', 'i ', 'xe', 'cs', 'ba', 'la', 'ce', 'pr', 'nd', 'pm', 'sm', 'eu', 'gd', 'tb', 'dy', 'ho', 'er', 'tm', 'yb', 'lu', 'hf', 'ta', 'w ', 're', 'os', 'ir', 'pt', 'au', 'hg', 'tl', 'pb', 'bi', 'po', 'at', 'rn', 'fr', 'ra', 'ac', 'th', 'pa', 'u ', 'np', 'pu']]
def get_atom(atom):
global __ATOM_LIST__
atom = atom.lower()
return __ATOM_LIST__.index(atom) + 1
def getUA(maxValence_list, valence_list):
UA = []
DU = []
for i, (maxValence, valence) in enumerate(zip(maxValence_list, valence_list)):
if maxValence - valence > 0:
UA.append(i)
DU.append(maxValence - valence)
return UA, DU
def get_BO(AC, UA, DU, valences, UA_pairs, quick):
BO = AC.copy()
DU_save = []
while DU_save != DU:
for i, j in UA_pairs:
BO[i, j] += 1
BO[j, i] += 1
BO_valence = list(BO.sum(axis=1))
DU_save = copy.copy(DU)
UA, DU = getUA(valences, BO_valence)
UA_pairs = get_UA_pairs(UA, AC, quick)[0]
return BO
def valences_not_too_large(BO, valences):
number_of_bonds_list = BO.sum(axis=1)
for valence, number_of_bonds in zip(valences, number_of_bonds_list):
if number_of_bonds > valence:
return False
return True
def BO_is_OK(BO, AC, charge, DU, atomic_valence_electrons, atomicNumList, charged_fragments):
Q = 0 # total charge
q_list = []
if charged_fragments:
BO_valences = list(BO.sum(axis=1))
for i, atom in enumerate(atomicNumList):
q = get_atomic_charge(atom, atomic_valence_electrons[atom], BO_valences[i])
Q += q
if atom == 6:
number_of_single_bonds_to_C = list(BO[i, :]).count(1)
if number_of_single_bonds_to_C == 2 and BO_valences[i] == 2:
Q += 1
q = 2
if number_of_single_bonds_to_C == 3 and Q + 1 < charge:
Q += 2
q = 1
if q != 0:
q_list.append(q)
if (BO - AC).sum() == sum(DU) and charge == Q and len(q_list) <= abs(charge):
return True
else:
return False
def get_atomic_charge(atom, atomic_valence_electrons, BO_valence):
if atom == 1:
charge = 1 - BO_valence
elif atom == 5:
charge = 3 - BO_valence
elif atom == 15 and BO_valence == 5:
charge = 0
elif atom == 16 and BO_valence == 6:
charge = 0
else:
charge = atomic_valence_electrons - 8 + BO_valence
return charge
def clean_charges(mol):
# this hack should not be needed any more but is kept just in case
rxn_smarts = ['[N+:1]=[*:2]-[C-:3]>>[N+0:1]-[*:2]=[C-0:3]',
'[N+:1]=[*:2]-[O-:3]>>[N+0:1]-[*:2]=[O-0:3]',
'[N+:1]=[*:2]-[*:3]=[*:4]-[O-:5]>>[N+0:1]-[*:2]=[*:3]-[*:4]=[O-0:5]',
'[#8:1]=[#6:2]([!-:6])[*:3]=[*:4][#6-:5]>>[*-:1][*:2]([*:6])=[*:3][*:4]=[*+0:5]',
'[O:1]=[c:2][c-:3]>>[*-:1][*:2][*+0:3]',
'[O:1]=[C:2][C-:3]>>[*-:1][*:2]=[*+0:3]']
fragments = Chem.GetMolFrags(mol, asMols=True, sanitizeFrags=False)
for i, fragment in enumerate(fragments):
for smarts in rxn_smarts:
patt = Chem.MolFromSmarts(smarts.split(">>")[0])
while fragment.HasSubstructMatch(patt):
rxn = AllChem.ReactionFromSmarts(smarts)
ps = rxn.RunReactants((fragment,))
fragment = ps[0][0]
if i == 0:
mol = fragment
else:
mol = Chem.CombineMols(mol, fragment)
return mol
def BO2mol(mol, BO_matrix, atomicNumList, atomic_valence_electrons, mol_charge, charged_fragments):
# based on code written by Paolo Toscani
l = len(BO_matrix)
l2 = len(atomicNumList)
BO_valences = list(BO_matrix.sum(axis=1))
if (l != l2):
raise RuntimeError('sizes of adjMat ({0:d}) and atomicNumList '
'{1:d} differ'.format(l, l2))
rwMol = Chem.RWMol(mol)
bondTypeDict = {
1: Chem.BondType.SINGLE,
2: Chem.BondType.DOUBLE,
3: Chem.BondType.TRIPLE}
for i in range(l):
for j in range(i + 1, l):
bo = int(round(BO_matrix[i, j]))
if (bo == 0):
continue
bt = bondTypeDict.get(bo, Chem.BondType.SINGLE)
rwMol.AddBond(i, j, bt)
mol = rwMol.GetMol()
if charged_fragments:
mol = set_atomic_charges(mol, atomicNumList, atomic_valence_electrons, BO_valences, BO_matrix, mol_charge)
else:
mol = set_atomic_radicals(mol, atomicNumList, atomic_valence_electrons, BO_valences)
return mol
def set_atomic_charges(mol, atomicNumList, atomic_valence_electrons, BO_valences, BO_matrix, mol_charge):
q = 0
for i, atom in enumerate(atomicNumList):
a = mol.GetAtomWithIdx(i)
charge = get_atomic_charge(atom, atomic_valence_electrons[atom], BO_valences[i])
q += charge
if atom == 6:
number_of_single_bonds_to_C = list(BO_matrix[i, :]).count(1)
if number_of_single_bonds_to_C == 2 and BO_valences[i] == 2:
q += 1
charge = 0
if number_of_single_bonds_to_C == 3 and q + 1 < mol_charge:
q += 2
charge = 1
if (abs(charge) > 0):
a.SetFormalCharge(int(charge))
# shouldn't be needed anymore bit is kept just in case
# mol = clean_charges(mol)
return mol
def set_atomic_radicals(mol, atomicNumList, atomic_valence_electrons, BO_valences):
# The number of radical electrons = absolute atomic charge
for i, atom in enumerate(atomicNumList):
a = mol.GetAtomWithIdx(i)
charge = get_atomic_charge(atom, atomic_valence_electrons[atom], BO_valences[i])
if (abs(charge) > 0):
a.SetNumRadicalElectrons(abs(int(charge)))
return mol
def get_bonds(UA, AC):
bonds = []
for k, i in enumerate(UA):
for j in UA[k + 1:]:
if AC[i, j] == 1:
bonds.append(tuple(sorted([i, j])))
return bonds
def get_UA_pairs(UA, AC, quick):
bonds = get_bonds(UA, AC)
if len(bonds) == 0:
return [()]
if quick:
G = nx.Graph()
G.add_edges_from(bonds)
UA_pairs = [list(nx.max_weight_matching(G))]
return UA_pairs
max_atoms_in_combo = 0
UA_pairs = [()]
for combo in list(itertools.combinations(bonds, int(len(UA) / 2))):
flat_list = [item for sublist in combo for item in sublist]
atoms_in_combo = len(set(flat_list))
if atoms_in_combo > max_atoms_in_combo:
max_atoms_in_combo = atoms_in_combo
UA_pairs = [combo]
elif atoms_in_combo == max_atoms_in_combo:
UA_pairs.append(combo)
return UA_pairs
def AC2BO(AC, atomicNumList, charge, charged_fragments, quick):
# TODO
atomic_valence = defaultdict(list)
atomic_valence[1] = [1]
atomic_valence[6] = [4]
atomic_valence[7] = [4, 3]
atomic_valence[8] = [2, 1]
atomic_valence[9] = [1]
atomic_valence[14] = [4]
atomic_valence[15] = [5, 4, 3]
atomic_valence[16] = [6, 4, 2]
atomic_valence[17] = [1]
atomic_valence[32] = [4]
atomic_valence[35] = [1]
atomic_valence[53] = [1]
atomic_valence_electrons = {}
atomic_valence_electrons[1] = 1
atomic_valence_electrons[6] = 4
atomic_valence_electrons[7] = 5
atomic_valence_electrons[8] = 6
atomic_valence_electrons[9] = 7
atomic_valence_electrons[14] = 4
atomic_valence_electrons[15] = 5
atomic_valence_electrons[16] = 6
atomic_valence_electrons[17] = 7
atomic_valence_electrons[32] = 4
atomic_valence_electrons[35] = 7
atomic_valence_electrons[53] = 7
# make a list of valences, e.g. for CO: [[4],[2,1]]
valences_list_of_lists = []
for atomicNum in atomicNumList:
valences_list_of_lists.append(atomic_valence[atomicNum])
valences_list = list(itertools.product(*valences_list_of_lists))
best_BO = AC.copy()
for valences in valences_list:
AC_valence = list(AC.sum(axis=1))
UA, DU_from_AC = getUA(valences, AC_valence)
if len(UA) == 0 and BO_is_OK(AC, AC, charge, DU_from_AC, atomic_valence_electrons, atomicNumList, charged_fragments):
return AC, atomic_valence_electrons
UA_pairs_list = get_UA_pairs(UA, AC, quick)
for UA_pairs in UA_pairs_list:
BO = get_BO(AC, UA, DU_from_AC, valences, UA_pairs, quick)
if BO_is_OK(BO, AC, charge, DU_from_AC, atomic_valence_electrons, atomicNumList, charged_fragments):
return BO, atomic_valence_electrons
elif BO.sum() >= best_BO.sum() and valences_not_too_large(BO, valences):
best_BO = BO.copy()
return best_BO, atomic_valence_electrons
def AC2mol(mol, AC, atomicNumList, charge, charged_fragments, quick):
# convert AC matrix to bond order (BO) matrix
BO, atomic_valence_electrons = AC2BO(AC, atomicNumList, charge, charged_fragments, quick)
# add BO connectivity and charge info to mol object
mol = BO2mol(mol, BO, atomicNumList, atomic_valence_electrons, charge, charged_fragments)
return mol
def get_proto_mol(atomicNumList):
mol = Chem.MolFromSmarts("[#" + str(atomicNumList[0]) + "]")
rwMol = Chem.RWMol(mol)
for i in range(1, len(atomicNumList)):
a = Chem.Atom(atomicNumList[i])
rwMol.AddAtom(a)
mol = rwMol.GetMol()
return mol
def get_atomicNumList(atomic_symbols):
atomicNumList = []
for symbol in atomic_symbols:
atomicNumList.append(get_atom(symbol))
return atomicNumList
def read_xyz_file(filename):
atomic_symbols = []
xyz_coordinates = []
with open(filename, "r") as file:
for line_number, line in enumerate(file):
if line_number == 0:
num_atoms = int(line)
elif line_number == 1:
if "charge=" in line:
charge = int(line.split("=")[1])
else:
charge = 0
else:
atomic_symbol, x, y, z = line.split()
atomic_symbols.append(atomic_symbol)
xyz_coordinates.append([float(x), float(y), float(z)])
atomicNumList = get_atomicNumList(atomic_symbols)
return atomicNumList, charge, xyz_coordinates
def xyz2AC(atomicNumList, xyz):
import numpy as np
mol = get_proto_mol(atomicNumList)
conf = Chem.Conformer(mol.GetNumAtoms())
for i in range(mol.GetNumAtoms()):
conf.SetAtomPosition(i, (xyz[i][0], xyz[i][1], xyz[i][2]))
mol.AddConformer(conf)
dMat = Chem.Get3DDistanceMatrix(mol)
pt = Chem.GetPeriodicTable()
num_atoms = len(atomicNumList)
AC = np.zeros((num_atoms, num_atoms)).astype(int)
for i in range(num_atoms):
a_i = mol.GetAtomWithIdx(i)
Rcov_i = pt.GetRcovalent(a_i.GetAtomicNum()) * 1.30
for j in range(i + 1, num_atoms):
a_j = mol.GetAtomWithIdx(j)
Rcov_j = pt.GetRcovalent(a_j.GetAtomicNum()) * 1.30
if dMat[i, j] <= Rcov_i + Rcov_j:
AC[i, j] = 1
AC[j, i] = 1
return AC, mol
def chiral_stereo_check(mol):
Chem.SanitizeMol(mol)
Chem.DetectBondStereochemistry(mol, -1)
Chem.AssignStereochemistry(mol, flagPossibleStereoCenters=True, force=True)
Chem.AssignAtomChiralTagsFromStructure(mol, -1)
return mol
def xyz2mol(atomicNumList, charge, xyz_coordinates, charged_fragments, quick):
AC, mol = xyz2AC(atomicNumList, xyz_coordinates)
new_mol = AC2mol(mol, AC, atomicNumList, charge, charged_fragments, quick)
new_mol = chiral_stereo_check(new_mol)
return new_mol
def mol_from_xyz(filepath):
charged_fragments = True # alternatively radicals are made
# quick is faster for large systems but requires networkx
# if you don't want to install networkx set quick=False and
# uncomment 'import networkx as nx' at the top of the file
quick = True
atomicNumList, charge, xyz_coordinates = read_xyz_file(filepath)
mol = xyz2mol(atomicNumList, charge, xyz_coordinates, charged_fragments, quick)
# Canonical hack
smiles = Chem.MolToSmiles(mol, isomericSmiles=True)
m = Chem.MolFromSmiles(smiles)
return m
from glob import glob
import pdb
xyz_filepath_list = list(glob('/Users/fangjia/PycharmProjects/fj/Practice/champs-scalar-coupling/structures/*.xyz'))
xyz_filepath_list.sort()
print('total xyz filepath # ', len(xyz_filepath_list))
print("Start")
#pred_df_list_1J_df3 = pd.read_csv('/Users/fangjia/Desktop/pred_df_list_1J_df.csv')
#pred_df_list_1J_df3_list = list((pred_df_list_3J_df3['Mol_Name'].values))[::4]
#pred_df_list_3J_df3_list = list(f'/Users/fangjia/Desktop/test_structure/{i}.xyz' for i in pred_df_list_3J_df3_list)
#xyz_filepath_list = pred_df_list_3J_df3_list
####xyz_filepath_list = ['/Users/fangjia/Desktop/test_structure/dsgdb9nsd_113830.xyz',
### '/Users/fangjia/Desktop/test_structure/dsgdb9nsd_112475.xyz']
#print(xyz_filepath_list[-307])
num_mols = len(xyz_filepath_list)
#pdb.set_trace()
mol_list = []
for i in range(3000):
filepath = xyz_filepath_list[i]
mol = mol_from_xyz(filepath)
if mol is None:
print(f'{i}-th file {filepath} failed, skip')
mol_list.append(mol)
from rdkit.Chem import Draw
img = Draw.MolsToGridImage(mol_list, molsPerRow=7, subImgSize=(150, 150), maxMols=num_mols, legends=None, useSVG=True)
#print(type(img))
#print(img)
img