forked from oxpig/AEV-PLIG
-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate_pdbbind_graphs.py
414 lines (322 loc) · 14.1 KB
/
generate_pdbbind_graphs.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
import pandas as pd
import pickle
import torch
import torchani
import torchani_mod
import qcelemental as qcel
import numpy as np
from tqdm import tqdm
import os
from rdkit import Chem
def elements_to_atomicnums(elements):
"""
Convert element symbols to atomic numbers.
Parameters
----------
elements: Iterable
Iterable object with lement symbols
Returns
-------
np.ndarray
Array of atomic numbers
"""
atomicnums = np.zeros(len(elements), dtype=int)
for idx, e in enumerate(elements):
atomicnums[idx] = qcel.periodictable.to_Z(e)
return atomicnums
def LoadMolasDF(mol):
# This function converts the input ligand (.MOL file) into a pandas DataFrame with the ligand atom position in 3D (X,Y,Z)
# load the ligand
m = mol
atoms = []
for atom in m.GetAtoms():
if atom.GetSymbol() != "H": # Include only non-hydrogen atoms
entry = [int(atom.GetIdx())]
entry.append(str(atom.GetSymbol()))
pos = m.GetConformer().GetAtomPosition(atom.GetIdx())
entry.append(float("{0:.4f}".format(pos.x)))
entry.append(float("{0:.4f}".format(pos.y)))
entry.append(float("{0:.4f}".format(pos.z)))
atoms.append(entry)
df = pd.DataFrame(atoms)
df.columns = ["ATOM_INDEX","ATOM_TYPE","X","Y","Z"]
return df
def LoadPDBasDF(PDB, atom_keys):
# This function converts a protein PDB file into a pandas DataFrame with the protein atom position in 3D (X,Y,Z)
prot_atoms = []
f = open(PDB)
for i in f:
if i[:4] == "ATOM":
# Include only non-hydrogen atoms
if (len(i[12:16].replace(" ","")) < 4 and i[12:16].replace(" ","")[0] != "H") or (len(i[12:16].replace(" ","")) == 4 and i[12:16].replace(" ","")[1] != "H" and i[12:16].replace(" ","")[0] != "H"):
prot_atoms.append([int(i[6:11]),
i[17:20]+"-"+i[12:16].replace(" ",""),
float(i[30:38]),
float(i[38:46]),
float(i[46:54])
])
f.close()
df = pd.DataFrame(prot_atoms, columns=["ATOM_INDEX","PDB_ATOM","X","Y","Z"])
df = df.merge(atom_keys, left_on='PDB_ATOM', right_on='PDB_ATOM')[["ATOM_INDEX", "ATOM_TYPE", "X", "Y", "Z"]].sort_values(by="ATOM_INDEX").reset_index(drop=True)
if list(df["ATOM_TYPE"].isna()).count(True) > 0:
print("WARNING: Protein contains unsupported atom types. Only supported atom-type pairs are counted.")
return df
def GetMolAEVs_extended(protein_path, mol, atom_keys, radial_coefs, atom_map):
"""
Calculate AEVs for molecule atoms based on protein atoms in a complex.
Hydrogens in both the molecule and protein are ignored and behind the
curtains molecule atoms are encoded as carbon to use torchani_mod code.
Parameters
----------
protein_path: string, path to protein file (.pdb)
mol_path: string, path to molecule file (.mol2)
atom_keys: dataframe of atom types in proteins
radial_coefs: list of pytorch tensors of RcR, EtaR, RsR
angular_coefs: list of pytorch tensors of RcA, Zeta, TsA, EtaA, RsA
Returns
-------
Tensor of shape [1, A, L], where A is the number of atoms and L is the
appropriate length of AEV.
Todo
----
Assert that RcR > RcA
"""
# Protein and ligand structure are loaded as pandas DataFrame
Target = LoadPDBasDF(protein_path, atom_keys)
Ligand = LoadMolasDF(mol)
# Define AEV coeficients
# Radial coefficients
RcR = radial_coefs[0]
EtaR = radial_coefs[1]
RsR = radial_coefs[2]
# Angular coefficients (Ga)
RcA = 2.0
Zeta = torch.tensor([1.0])
TsA = torch.tensor([1.0]) # Angular shift in GA
EtaA = torch.tensor([1.0])
RsA = torch.tensor([1.0]) # Radial shift in GA
# Reduce size of Target df to what we need based on radial cutoff RcR
distance_cutoff = RcR + 0.1
for i in ["X","Y","Z"]:
Target = Target[Target[i] < float(Ligand[i].max())+distance_cutoff]
Target = Target[Target[i] > float(Ligand[i].min())-distance_cutoff]
Target = Target.merge(atom_map, on='ATOM_TYPE', how='left')
# Create tensors of atomic numbers and coordinates of molecule atoms and
# protein atoms combined. Encode molecule atoms as hydrogen
mol_len = torch.tensor(len(Ligand))
atomicnums = np.append(np.ones(mol_len)*6, Target["ATOM_NR"])
atomicnums = torch.tensor(atomicnums, dtype=torch.int64)
atomicnums = atomicnums.unsqueeze(0)
coordinates = pd.concat([Ligand[['X','Y','Z']], Target[['X','Y','Z']]])
coordinates = torch.tensor(coordinates.values)
coordinates = coordinates.unsqueeze(0)
# Use torchani_mod to calculate AEVs
atom_symbols = []
for i in range(1, 23):
atom_symbols.append(qcel.periodictable.to_symbol(i))
AEVC = torchani_mod.AEVComputer(RcR, RcA, EtaR, RsR,
EtaA, Zeta, RsA, TsA, len(atom_symbols))
SC = torchani.SpeciesConverter(atom_symbols)
sc = SC((atomicnums, coordinates))
aev = AEVC.forward((sc.species, sc.coordinates), mol_len)
# find indices of columns to keep
# keep all radial terms
n = len(atom_symbols)
n_rad_sub = len(EtaR)*len(RsR)
indices = list(np.arange(n*n_rad_sub))
return Ligand, aev.aevs.squeeze(0)[:mol_len,indices]
def GetMolAEVs_rad(protein_path, mol, atom_keys, radial_coefs):
"""
Calculate AEVs for molecule atoms based on protein atoms in a complex.
Hydrogens in both the molecule and protein are ignored and behind the
curtains molecule atoms are encoded as carbon to use torchani_mod code.
Parameters
----------
protein_path: string, path to protein file (.pdb)
mol_path: string, path to molecule file (.mol2)
atom_keys: dataframe of atom types in proteins
radial_coefs: list of pytorch tensors of RcR, EtaR, RsR
angular_coefs: list of pytorch tensors of RcA, Zeta, TsA, EtaA, RsA
Returns
-------
Tensor of shape [1, A, L], where A is the number of atoms and L is the
appropriate length of AEV.
Todo
----
Assert that RcR > RcA
"""
# Protein and ligand structure are loaded as pandas DataFrame
Target = LoadPDBasDF(protein_path, atom_keys)
Ligand = LoadMolasDF(mol)
# Define AEV coeficients
# Radial coefficients
RcR = radial_coefs[0]
EtaR = radial_coefs[1]
RsR = radial_coefs[2]
# Angular coefficients (Ga)
RcA = 2.0
Zeta = torch.tensor([1.0])
TsA = torch.tensor([1.0]) # Angular shift in GA
EtaA = torch.tensor([1.0])
RsA = torch.tensor([1.0]) # Radial shift in GA
# Reduce size of Target df to what we need based on radial cutoff RcR
distance_cutoff = RcR + 0.1
for i in ["X","Y","Z"]:
Target = Target[Target[i] < float(Ligand[i].max())+distance_cutoff]
Target = Target[Target[i] > float(Ligand[i].min())-distance_cutoff]
# Instead of all these different atom types we only use atom symbol
Target["ATOM_SYMBOL"] = [i.split(";")[0] for i in Target["ATOM_TYPE"]]
# Create tensors of atomic numbers and coordinates of molecule atoms and
# protein atoms combined. Encode molecule atoms as hydrogen
mol_len = torch.tensor(len(Ligand))
atomicnums = np.append(np.ones(mol_len)*6, elements_to_atomicnums(Target['ATOM_SYMBOL']))
atomicnums = torch.tensor(atomicnums, dtype=torch.int64)
atomicnums = atomicnums.unsqueeze(0)
coordinates = pd.concat([Ligand[['X','Y','Z']], Target[['X','Y','Z']]])
coordinates = torch.tensor(coordinates.values)
coordinates = coordinates.unsqueeze(0)
# Use torchani_mod to calculate AEVs
atom_symbols = ['C','N','O','S']
AEVC = torchani_mod.AEVComputer(RcR, RcA, EtaR, RsR,
EtaA, Zeta, RsA, TsA, len(atom_symbols))
SC = torchani.SpeciesConverter(atom_symbols)
sc = SC((atomicnums, coordinates))
aev = AEVC.forward((sc.species, sc.coordinates), mol_len)
# find indices of columns to keep
# keep all radial terms
n = len(atom_symbols)
n_rad_sub = len(EtaR)*len(RsR)
indices = list(np.arange(n*n_rad_sub))
return Ligand, aev.aevs.squeeze(0)[:mol_len,indices]
def one_of_k_encoding(x, allowable_set):
"""
taken from https://github.com/thinng/GraphDTA
function which one hot encodes x w.r.t. allowable_set and x has to be in allowable_set
x:
element from allowable_set
allowable_set: list
list of elements x is from
"""
if x not in allowable_set:
raise Exception("input {0} not in allowable set{1}:".format(x, allowable_set))
return list(map(lambda s: x == s, allowable_set))
def atom_features(atom, features=["atom_symbol",
"num_heavy_atoms",
"total_num_Hs",
"explicit_valence",
"is_aromatic",
"is_in_ring"]):
# Computes the ligand atom features for graph node construction
# The standard features are the following:
# atom_symbol = one hot encoding of atom symbol
# num_heavy_atoms = # of heavy atom neighbors
# total_num_Hs = # number of hydrogen atom neighbors
# explicit_valence = explicit valence of the atom
# is_aromatic = boolean 1 - aromatic, 0 - not aromatic
# is_in_ring = boolean 1 - is in ring, 0 - is not in ring
feature_list = []
if "atom_symbol" in features:
feature_list.extend(one_of_k_encoding(atom.GetSymbol(),['F', 'N', 'Cl', 'O', 'Br', 'C', 'B', 'P', 'I', 'S']))
if "num_heavy_atoms" in features:
feature_list.append(len([x.GetSymbol() for x in atom.GetNeighbors() if x.GetSymbol() != "H"]))
if "total_num_Hs" in features:
feature_list.append(len([x.GetSymbol() for x in atom.GetNeighbors() if x.GetSymbol() == "H"]))
if "explicit_valence" in features: #-NEW ADDITION FOR PLIG
feature_list.append(atom.GetExplicitValence())
if "is_aromatic" in features:
if atom.GetIsAromatic():
feature_list.append(1)
else:
feature_list.append(0)
if "is_in_ring" in features:
if atom.IsInRing():
feature_list.append(1)
else:
feature_list.append(0)
return np.array(feature_list)
def mol_to_graph(mol, mol_df, aevs, extra_features=["atom_symbol",
"num_heavy_atoms",
"total_num_Hs",
"explicit_valence",
"is_aromatic",
"is_in_ring"]):
features = []
heavy_atom_index = []
idx_to_idx = {}
counter = 0
# Generate nodes
for atom in mol.GetAtoms():
if atom.GetSymbol() != "H": # Include only non-hydrogen atoms
idx_to_idx[atom.GetIdx()] = counter
aev_idx = mol_df[mol_df['ATOM_INDEX'] == atom.GetIdx()].index
heavy_atom_index.append(atom.GetIdx())
feature = np.append(atom_features(atom), aevs[aev_idx,:])
features.append(feature)
counter += 1
#Generate edges
edges = []
for bond in mol.GetBonds():
idx1 = bond.GetBeginAtomIdx()
idx2 = bond.GetEndAtomIdx()
if idx1 in heavy_atom_index and idx2 in heavy_atom_index:
bond_type = one_of_k_encoding(bond.GetBondType(),[1,12,2,3])
bond_type = [float(b) for b in bond_type]
edge1 = [idx_to_idx[idx1], idx_to_idx[idx2]]
edge1.extend(bond_type)
edge2 = [idx_to_idx[idx2], idx_to_idx[idx1]]
edge2.extend(bond_type)
edges.append(edge1)
edges.append(edge2)
df = pd.DataFrame(edges, columns=['atom1', 'atom2', 'single', 'aromatic', 'double', 'triple'])
df = df.sort_values(by=['atom1','atom2'])
edge_index = df[['atom1','atom2']].to_numpy().tolist()
edge_attr = df[['single','aromatic','double','triple']].to_numpy().tolist()
return len(mol_df), features, edge_index, edge_attr
'''
Load data
'''
data = pd.read_csv("data/pdbbind_processed.csv", index_col=0)
'''
Generate for all complexes: ANI-2x with 22 atom types. Only 2-atom interactions
'''
print("The number of data points is ", len(data))
atom_keys = pd.read_csv("data/PDB_Atom_Keys.csv", sep=",")
atom_map = pd.DataFrame(pd.unique(atom_keys["ATOM_TYPE"]))
atom_map[1] = list(np.arange(len(atom_map)) + 1)
atom_map = atom_map.rename(columns={0:"ATOM_TYPE", 1:"ATOM_NR"})
# Radial coefficients: ANI-2x
RcR = 5.1 # Radial cutoff
EtaR = torch.tensor([19.7]) # Radial decay
RsR = torch.tensor([0.80, 1.07, 1.34, 1.61, 1.88, 2.14, 2.41, 2.68,
2.95, 3.22, 3.49, 3.76, 4.03, 4.29, 4.56, 4.83]) # Radial shift
radial_coefs = [RcR, EtaR, RsR]
mol_graphs = {}
failed_list = []
failed_after_reading = []
for i, pdb in tqdm(enumerate(data["PDB_code"])):
if data["refined"][i]:
folder = "data/pdbbind/refined-set/"
else:
folder = "data/pdbbind/general-set/"
mol_path = os.path.join(folder, pdb, f'{pdb}_ligand.mol2')
mol = Chem.MolFromMol2File(mol_path)
if mol is None:
print("can't read molecule structure:", pdb)
failed_list.append(pdb)
continue
else:
mol = Chem.AddHs(mol, addCoords=True)
try:
protein_path = os.path.join(folder, pdb, f'{pdb}_protein.pdb')
mol_df, aevs = GetMolAEVs_extended(protein_path, mol, atom_keys, radial_coefs, atom_map)
graph = mol_to_graph(mol, mol_df, aevs)
mol_graphs[pdb] = graph
except ValueError as e:
print(e)
failed_after_reading.append(pdb)
continue
print(len(failed_list), len(failed_after_reading))
#save the graphs to use as input for the GNN models
output_file_graphs = "data/pdbbind.pickle"
with open(output_file_graphs, 'wb') as handle:
pickle.dump(mol_graphs, handle, protocol=pickle.HIGHEST_PROTOCOL)