-
Notifications
You must be signed in to change notification settings - Fork 0
/
build_torsion_tree.py
495 lines (407 loc) · 23.2 KB
/
build_torsion_tree.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
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
from typing import Union, Dict, Tuple
import os
import shutil
import re
import numpy as np
import networkx as nx
import glob
from concurrent.futures import ThreadPoolExecutor
import rdkit
from rdkit import Chem
from rdkit.Chem import GetMolFrags, FragmentOnBonds
from rdkit.Chem.rdPartialCharges import ComputeGasteigerCharges
ATOM_TYPE_DEFINITION_LIST = [
{'smarts': '[#1]', 'atype': 'H', 'comment': 'invisible'},
{'smarts': '[#1][#7,#8,#9,#15,#16]', 'atype': 'HD', 'comment': None},
{'smarts': '[#5]', 'atype': 'B', 'comment': None},
{'smarts': '[C]', 'atype': 'C', 'comment': None},
{'smarts': '[c]', 'atype': 'A', 'comment': None},
{'smarts': '[#7]', 'atype': 'NA', 'comment': None},
{'smarts': '[#8]', 'atype': 'OA', 'comment': None},
{'smarts': '[#9]', 'atype': 'F', 'comment': None},
{'smarts': '[#12]', 'atype': 'Mg', 'comment': None},
{'smarts': '[#14]', 'atype': 'Si', 'comment': None},
{'smarts': '[#15]', 'atype': 'P', 'comment': None},
{'smarts': '[#16]', 'atype': 'S', 'comment': None},
{'smarts': '[#17]', 'atype': 'Cl', 'comment': None},
{'smarts': '[#20]', 'atype': 'Ca', 'comment': None},
{'smarts': '[#25]', 'atype': 'Mn', 'comment': None},
{'smarts': '[#26]', 'atype': 'Fe', 'comment': None},
{'smarts': '[#30]', 'atype': 'Zn', 'comment': None},
{'smarts': '[#35]', 'atype': 'Br', 'comment': None},
{'smarts': '[#53]', 'atype': 'I', 'comment': None},
{'smarts': '[#7X3v3][a]', 'atype': 'N', 'comment': 'pyrrole, aniline'},
{'smarts': '[#7X3v3][#6X3v4]', "atype": 'N', 'comment': 'amide'},
{'smarts': '[#7+1]', 'atype': 'N', 'comment': 'ammonium, pyridinium'},
{'smarts': '[SX2]', 'atype': 'SA', 'comment': 'sulfur acceptor'}
]
def assign_atom_properties(mol):
atom_positions = mol.GetConformer().GetPositions()
num_atoms = mol.GetNumAtoms()
internal_atom_idx = 0
for atom_idx in range(num_atoms):
atom = mol.GetAtomWithIdx(atom_idx)
atom.SetIntProp('sdf_atom_idx', atom_idx+1)
if not atom.HasProp('atom_name'):
atom_element = atom.GetSymbol()
atom_name = atom_element + str(internal_atom_idx+1)
atom.SetProp('atom_name', atom_name)
atom.SetProp('residue_name', 'MOL')
atom.SetIntProp('residue_idx', 1)
atom.SetProp('chain_idx', 'A')
internal_atom_idx += 1
atom.SetDoubleProp('charge', atom.GetDoubleProp('_GasteigerCharge'))
atom.SetDoubleProp('x', atom_positions[atom_idx, 0])
atom.SetDoubleProp('y', atom_positions[atom_idx, 1])
atom.SetDoubleProp('z', atom_positions[atom_idx, 2])
class AtomType(object):
def __init__(self):
self.atom_type_definition_list = ATOM_TYPE_DEFINITION_LIST
def assign_atom_types(self, mol):
for atom_type_dict in self.atom_type_definition_list:
smarts = atom_type_dict['smarts']
atom_type = atom_type_dict['atype']
pattern_mol = Chem.MolFromSmarts(smarts)
pattern_matches = mol.GetSubstructMatches(pattern_mol)
for pattern_match in pattern_matches:
atom = mol.GetAtomWithIdx(pattern_match[0])
atom.SetProp('atom_type', atom_type)
def get_docking_atom_types(self, mol:Chem.rdchem.Mol) -> Dict[int, str]:
atom_ind_type_map = dict()
for atom_type_dict in self.atom_type_definition_list:
smarts = atom_type_dict["smarts"]
atom_type = atom_type_dict["atype"]
pattern_mol = Chem.MolFromSmarts(smarts)
pattern_matches = mol.GetSubstructMatches(pattern_mol)
for pattern_match in pattern_matches:
atom_ind = pattern_match[0]
atom_ind_type_map[atom_ind] = atom_type
return atom_ind_type_map
class RotatableBond(object):
def __init__(self,
min_macrocycle_size=7,
max_macrocycle_size=33,
double_bond_penalty=50,
max_breaks=4):
self.rotatable_bond_smarts = '[!$(*#*)&!D1]-&!@[!$(*#*)&!D1]'
self.amide_bond_smarts = '[C&$(C=O)]-[N&$(NC=O);v3;H1,H2;0]'
self.conjugate_bond_smarts = '*=*[*]=,#,:[*]'
self.rotatable_bond_pattern = Chem.MolFromSmarts(self.rotatable_bond_smarts)
self.amide_bond_pattern = Chem.MolFromSmarts(self.amide_bond_smarts)
self.conjugate_bond_pattern = Chem.MolFromSmarts(self.conjugate_bond_smarts)
self.min_macrocycle_size = min_macrocycle_size
self.max_macrocycle_size = max_macrocycle_size
self.double_bond_penalty = double_bond_penalty
self.max_breaks = max_breaks
def identify_rotatable_bonds(self, mol):
default_rotatable_bond_info_list = list(mol.GetSubstructMatches(self.rotatable_bond_pattern))
amide_rotatable_bond_info_list = list(mol.GetSubstructMatches(self.amide_bond_pattern))
for amide_rotatable_bond_info in amide_rotatable_bond_info_list:
amide_rotatable_bond_info_reversed = tuple(reversed(amide_rotatable_bond_info))
if amide_rotatable_bond_info in default_rotatable_bond_info_list:
default_rotatable_bond_info_list.remove(amide_rotatable_bond_info)
elif amide_rotatable_bond_info_reversed in default_rotatable_bond_info_list:
default_rotatable_bond_info_list.remove(amide_rotatable_bond_info_reversed)
return default_rotatable_bond_info_list
class TopologyBuilder(object):
def __init__(self, mol:Chem.rdchem.Mol):
self.mol = mol
self.atom_typer = AtomType()
self.rotatable_bond_finder = RotatableBond()
def build_molecular_graph(self):
mol = self.mol
self.atom_typer.assign_atom_types(mol)
ComputeGasteigerCharges(mol)
assign_atom_properties(mol)
rotatable_bond_info_list = self.rotatable_bond_finder.identify_rotatable_bonds(mol)
bond_list = list(mol.GetBonds())
rotatable_bond_idx_list = []
for bond in bond_list:
bond_info = (bond.GetBeginAtomIdx(), bond.GetEndAtomIdx())
bond_info_reversed = (bond.GetEndAtomIdx(), bond.GetBeginAtomIdx())
if bond_info in rotatable_bond_info_list or bond_info_reversed in rotatable_bond_info_list:
rotatable_bond_idx_list.append(bond.GetIdx())
if len(rotatable_bond_idx_list) != 0:
splitted_mol = FragmentOnBonds(mol, rotatable_bond_idx_list, addDummies=False)
splitted_mol_list = list(GetMolFrags(splitted_mol, asMols=True, sanitizeFrags=False))
else:
splitted_mol_list = [mol]
num_fragments = len(splitted_mol_list)
## Find fragment as the root node
##############################################################################
num_fragment_atoms_list = [None] * num_fragments
for fragment_idx in range(num_fragments):
fragment = splitted_mol_list[fragment_idx]
num_atoms = fragment.GetNumAtoms()
num_fragment_atoms_list[fragment_idx] = num_atoms
root_fragment_idx = None
root_fragment_idx = np.argmax(num_fragment_atoms_list)
##############################################################################
## Build torsion tree
### Add atom info into nodes
##############################################################################
torsion_tree = nx.Graph()
node_idx = 0
root_fragment = splitted_mol_list[root_fragment_idx]
num_root_atoms = root_fragment.GetNumAtoms()
atom_info_list = [None] * num_root_atoms
for root_atom_idx in range(num_root_atoms):
root_atom = root_fragment.GetAtomWithIdx(root_atom_idx)
atom_info_dict = {}
atom_info_dict['sdf_atom_idx'] = root_atom.GetIntProp('sdf_atom_idx')
atom_info_dict['atom_name'] = root_atom.GetProp('atom_name')
atom_info_dict['residue_name'] = root_atom.GetProp('residue_name')
atom_info_dict['chain_idx'] = root_atom.GetProp('chain_idx')
atom_info_dict['residue_idx'] = root_atom.GetIntProp('residue_idx')
atom_info_dict['x'] = root_atom.GetDoubleProp('x')
atom_info_dict['y'] = root_atom.GetDoubleProp('y')
atom_info_dict['z'] = root_atom.GetDoubleProp('z')
atom_info_dict['charge'] = root_atom.GetDoubleProp('charge')
atom_info_dict['atom_type'] = root_atom.GetProp('atom_type')
atom_info_list[root_atom_idx] = atom_info_dict
torsion_tree.add_node(node_idx, atom_info_list=atom_info_list)
node_idx += 1
for fragment_idx in range(num_fragments):
if fragment_idx == root_fragment_idx:
continue
else:
fragment = splitted_mol_list[fragment_idx]
num_fragment_atoms = fragment.GetNumAtoms()
atom_info_list = [None] * num_fragment_atoms
for atom_idx in range(num_fragment_atoms):
atom = fragment.GetAtomWithIdx(atom_idx)
atom_info_dict = {}
atom_info_dict['sdf_atom_idx'] = atom.GetIntProp('sdf_atom_idx')
atom_info_dict['atom_name'] = atom.GetProp('atom_name')
atom_info_dict['residue_name'] = atom.GetProp('residue_name')
atom_info_dict['chain_idx'] = atom.GetProp('chain_idx')
atom_info_dict['residue_idx'] = atom.GetIntProp('residue_idx')
atom_info_dict['x'] = atom.GetDoubleProp('x')
atom_info_dict['y'] = atom.GetDoubleProp('y')
atom_info_dict['z'] = atom.GetDoubleProp('z')
atom_info_dict['charge'] = atom.GetDoubleProp('charge')
atom_info_dict['atom_type'] = atom.GetProp('atom_type')
atom_info_list[atom_idx] = atom_info_dict
torsion_tree.add_node(node_idx, atom_info_list=atom_info_list)
node_idx += 1
##############################################################################
### Add edge info
##############################################################################
num_rotatable_bonds = len(rotatable_bond_info_list)
for edge_idx in range(num_rotatable_bonds):
rotatable_bond_info = rotatable_bond_info_list[edge_idx]
begin_atom_idx = rotatable_bond_info[0]
end_atom_idx = rotatable_bond_info[1]
begin_atom = mol.GetAtomWithIdx(begin_atom_idx)
begin_sdf_atom_idx = begin_atom.GetIntProp('sdf_atom_idx')
begin_atom_name = begin_atom.GetProp('atom_name')
end_atom = mol.GetAtomWithIdx(end_atom_idx)
end_sdf_atom_idx = end_atom.GetIntProp('sdf_atom_idx')
end_atom_name = end_atom.GetProp('atom_name')
begin_node_idx = None
end_node_idx = None
for node_idx in range(num_fragments):
atom_info_list = torsion_tree.nodes[node_idx]['atom_info_list']
for atom_info_dict in atom_info_list:
if atom_info_dict['atom_name'] == begin_atom_name:
begin_node_idx = node_idx
break
elif atom_info_dict['atom_name'] == end_atom_name:
end_node_idx = node_idx
break
if begin_node_idx is not None and end_node_idx is not None:
break
if begin_node_idx is None or end_node_idx is None:
raise ValueError('Bugs in edge assignment code!!')
torsion_tree.add_edge(begin_node_idx,
end_node_idx,
begin_node_idx=begin_node_idx,
end_node_idx=end_node_idx,
begin_sdf_atom_idx=begin_sdf_atom_idx,
end_sdf_atom_idx=end_sdf_atom_idx,
begin_atom_name=begin_atom_name,
end_atom_name=end_atom_name)
##############################################################################
self.torsion_tree = torsion_tree
self.mol = mol
def __deep_first_search__(self, node_idx):
if node_idx == 0:
self.pdbqt_atom_line_list.append('ROOT\n')
atom_info_list = self.torsion_tree.nodes[node_idx]['atom_info_list']
for atom_info_dict in atom_info_list:
atom_name = atom_info_dict['atom_name']
self.atom_idx_info_mapping_dict[atom_name] = self.pdbqt_atom_idx
atom_info_tuple = ('ATOM',
self.pdbqt_atom_idx,
atom_info_dict['atom_name'],
atom_info_dict['residue_name'],
atom_info_dict['chain_idx'],
atom_info_dict['residue_idx'],
atom_info_dict['x'],
atom_info_dict['y'],
atom_info_dict['z'],
1.0,
0.0,
atom_info_dict['charge'],
atom_info_dict['atom_type'])
self.pdbqt_atom_line_list.append(self.pdbqt_atom_line_format.format(*atom_info_tuple))
self.pdbqt_atom_idx += 1
self.pdbqt_atom_line_list.append('ENDROOT\n')
else:
atom_info_list = self.torsion_tree.nodes[node_idx]['atom_info_list']
for atom_info_dict in atom_info_list:
atom_name = atom_info_dict['atom_name']
if atom_name not in self.atom_idx_info_mapping_dict:
self.atom_idx_info_mapping_dict[atom_name] = self.pdbqt_atom_idx
atom_info_tuple = ('ATOM',
self.pdbqt_atom_idx,
atom_info_dict['atom_name'],
atom_info_dict['residue_name'],
atom_info_dict['chain_idx'],
atom_info_dict['residue_idx'],
atom_info_dict['x'],
atom_info_dict['y'],
atom_info_dict['z'],
1.0,
0.0,
atom_info_dict['charge'],
atom_info_dict['atom_type'])
self.pdbqt_atom_line_list.append(self.pdbqt_atom_line_format.format(*atom_info_tuple))
self.pdbqt_atom_idx += 1
self.visited_node_idx_set.add(node_idx)
neighbor_node_idx_list = list(self.torsion_tree.neighbors(node_idx))
for neighbor_node_idx in neighbor_node_idx_list:
if neighbor_node_idx not in self.visited_node_idx_set:
temp_pdbqt_atom_idx = self.pdbqt_atom_idx
atom_info_list = self.torsion_tree.nodes[neighbor_node_idx]['atom_info_list']
for atom_info_dict in atom_info_list:
atom_name = atom_info_dict['atom_name']
if atom_name not in self.atom_idx_info_mapping_dict:
self.atom_idx_info_mapping_dict[atom_name] = temp_pdbqt_atom_idx
temp_pdbqt_atom_idx += 1
edge_info = self.torsion_tree.edges[(node_idx, neighbor_node_idx)]
begin_node_idx = edge_info['begin_node_idx']
end_node_idx = edge_info['end_node_idx']
begin_atom_name = edge_info['begin_atom_name']
end_atom_name = edge_info['end_atom_name']
if begin_node_idx == node_idx:
parent_atom_name = begin_atom_name
offspring_atom_name = end_atom_name
else:
parent_atom_name = end_atom_name
offspring_atom_name = begin_atom_name
parent_atom_idx = self.atom_idx_info_mapping_dict[parent_atom_name]
offspring_atom_idx = self.atom_idx_info_mapping_dict[offspring_atom_name]
self.branch_info_list.append((parent_atom_name, str(parent_atom_idx), offspring_atom_name, str(offspring_atom_idx)))
self.pdbqt_atom_line_list.append(self.pdbqt_branch_line_format.format('BRANCH', parent_atom_idx, offspring_atom_idx))
self.__deep_first_search__(neighbor_node_idx)
self.pdbqt_atom_line_list.append(self.pdbqt_end_branch_line_format.format('ENDBRANCH', parent_atom_idx, offspring_atom_idx))
def write_pdbqt_file(self, out_file:str=''):
self.pdbqt_remark_line_list = []
self.pdbqt_atom_line_list = []
self.pdbqt_remark_torsion_line_format = '{:6s} {:^2d} {:1s} {:7s} {:6s} {:^7s} {:3s} {:^7s}\n'
self.pdbqt_atom_line_format = '{:4s} {:5d} {:^4s} {:3s} {:1s}{:4d} {:8.3f}{:8.3f}{:8.3f}{:6.2f}{:6.2f} {:6.3f} {:<2s}\n'
self.pdbqt_branch_line_format = '{:6s} {:3d} {:3d}\n'
self.pdbqt_end_branch_line_format = '{:9s} {:3d} {:3d}\n'
self.torsion_dof_line_format = '{:7s} {:d}'
## Prepare pdbqt atom lines
####################################################################################################
self.atom_idx_info_mapping_dict = {}
self.branch_info_list = []
self.visited_node_idx_set = set()
self.pdbqt_atom_idx = 1
self.__deep_first_search__(0)
self.num_torsions = len(self.branch_info_list)
self.pdbqt_atom_line_list.append(self.torsion_dof_line_format.format('TORSDOF', self.num_torsions))
####################################################################################################
## Prepare pdbqt remark lines
####################################################################################################
self.pdbqt_remark_line_list.append('REMARK ' + str(self.num_torsions) + ' active torsions:\n')
self.pdbqt_remark_line_list.append("REMARK status: ('A' for Active; 'I' for Inactive)\n")
for torsion_idx in range(self.num_torsions):
branch_info_tuple = self.branch_info_list[torsion_idx]
remark_torsion_info_tuple = ('REMARK',
torsion_idx+1,
'A',
'between',
'atoms:',
branch_info_tuple[0] + '_' + branch_info_tuple[1],
'and',
branch_info_tuple[2] + '_' + branch_info_tuple[3])
self.pdbqt_remark_line_list.append(self.pdbqt_remark_torsion_line_format.format(*remark_torsion_info_tuple))
####################################################################################################
self.pdbqt_line_list = self.pdbqt_remark_line_list + self.pdbqt_atom_line_list
with open(out_file, 'w') as f:
for pdbqt_line in self.pdbqt_line_list:
f.write(pdbqt_line)
def write_constraint_bpf_file(self, out_path:str=''):
self.core_bpf_remark_line_list = []
self.core_bpf_atom_line_list = []
self.core_bpf_atom_line_format = '{:8.3f}\t{:8.3f}\t{:8.3f}\t{:6.2f}\t{:6.2f}\t{:3s}\t{:<2s}\n'
self.core_bpf_remark_line_list.append('x y z Vset r type atom\n')
root_atom_info_list = self.torsion_tree.nodes[0]['atom_info_list']
for atom_info_dict in root_atom_info_list:
atom_info_tuple = (atom_info_dict['x'],
atom_info_dict['y'],
atom_info_dict['z'],
-1.2,
0.6,
'map',
atom_info_dict['atom_type'])
self.core_bpf_atom_line_list.append(self.core_bpf_atom_line_format.format(*atom_info_tuple))
self.core_bpf_line_list = self.core_bpf_remark_line_list + self.core_bpf_atom_line_list
os.makedirs(os.path.dirname(os.path.abspath(out_path)), exist_ok=True)
with open(out_path, 'w') as ligand_core_bpf_file:
for core_bpf_line in self.core_bpf_line_list:
ligand_core_bpf_file.write(core_bpf_line)
def get_sdf_torsion_tree_info(self) -> tuple[str]:
fragment_info_string = ''
torsion_info_string = ''
atom_info_string = ''
num_nodes = self.torsion_tree.number_of_nodes()
num_edges = self.torsion_tree.number_of_edges()
for node_idx in range(num_nodes):
atom_info_list = self.torsion_tree.nodes[node_idx]['atom_info_list']
for atom_info_dict in atom_info_list:
fragment_info_string += str(atom_info_dict['sdf_atom_idx'])
fragment_info_string += ' '
fragment_info_string = fragment_info_string[:-1]
fragment_info_string += '\n'
edge_key_list = list(self.torsion_tree.edges.keys())
for edge_idx in range(num_edges):
edge_key = edge_key_list[edge_idx]
edge_info_dict = self.torsion_tree.edges[edge_key]
begin_sdf_atom_idx = str(edge_info_dict['begin_sdf_atom_idx'])
end_sdf_atom_idx = str(edge_info_dict['end_sdf_atom_idx'])
begin_node_idx = str(edge_info_dict['begin_node_idx'])
end_node_idx = str(edge_info_dict['end_node_idx'])
torsion_info_string += f'{begin_sdf_atom_idx} {end_sdf_atom_idx} {begin_node_idx} {end_node_idx}'
torsion_info_string += '\n'
for atom in self.mol.GetAtoms():
sdf_atom_idx = str(atom.GetIntProp('sdf_atom_idx'))
charge = str(atom.GetDoubleProp('charge'))
atom_type = atom.GetProp('atom_type')
atom_info = str(sdf_atom_idx).ljust(3) + str(charge)[:10].ljust(10) + atom_type.ljust(2)
atom_info_string += atom_info
atom_info_string += '\n'
return fragment_info_string, torsion_info_string, atom_info_string
def calc_torsion_tree_info(mols:list):
for mol in mols:
rdkit_mol = mol.get_mol()
runner = TopologyBuilder(rdkit_mol)
runner.build_molecular_graph()
fragment_info_string, torsion_info_string, atom_info_string = runner.get_sdf_torsion_tree_info()
mol.add_prop("fragInfo", fragment_info_string)
mol.add_prop("torsionInfo", torsion_info_string)
mol.add_prop("atomInfo", atom_info_string)
return mols
def topogen(
mol:rdkit.Chem.rdchem.Mol
) -> Tuple[str, str, str, str]:
runner = TopologyBuilder(mol)
runner.build_molecular_graph()
fraginfo, torsioninfo, atominfo \
= runner.get_sdf_torsion_tree_info()
natoms = mol.GetNumAtoms()
fraginfo_all = " ".join([str(i) for i in range(1, 1+natoms)])
return fraginfo, torsioninfo, atominfo, fraginfo_all