This repository was archived by the owner on Mar 7, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgenerate_features.py
373 lines (277 loc) · 8.52 KB
/
generate_features.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
import argparse
import os
import time
from glob import glob
from pathlib import Path
import pandas as pd
import progressbar
from biopandas.mol2 import PandasMol2
from biopandas.pdb import PandasPdb
from scipy import spatial
atom_types = {
"OE1": "O",
"HB1": "H",
"SD": "S",
"HE": "H",
"1HD1": "H",
"1HG1": "H",
"2HD2": "H",
"1HD2": "H",
"CE3": "C",
"OH": "O",
"CZ": "C",
"HG2": "H",
"HN2": "H",
"NZ": "N",
"HN1": "H",
"3HD2": "H",
"CD2": "C",
"2HH2": "H",
"HH2": "H",
"O": "O",
"2HD1": "H",
"ND1": "N",
"HH": "H",
"1HE2": "H",
"HB": "H",
"NH2": "N",
"3HG1": "H",
"ND2": "N",
"CZ3": "C",
"HA2": "H",
"OG": "O",
"CG2": "C",
"CE": "C",
"SG": "S",
"NE": "N",
"CG": "C",
"CB": "C",
"HG1": "H",
"NH1": "N",
"2HE2": "H",
"3HD1": "H",
"1HH2": "H",
"HD2": "H",
"HD1": "H",
"NE1": "N",
"HB2": "H",
"HA": "H",
"3HG2": "H",
"HN3": "H",
"HE1": "H",
"CD": "C",
"HZ3": "H",
"OD1": "O",
"N": "N",
"H": "H",
"HA1": "H",
"2HH1": "H",
"NE2": "N",
"CE2": "C",
"C": "C",
"OD2": "O",
"2HG1": "H",
"CD1": "C",
"HE3": "H",
"1HH1": "H",
"2HG2": "H",
"HB3": "H",
"CE1": "C",
"OXT": "O",
"CH2": "C",
"1HG2": "H",
"HZ1": "H",
"OG1": "O",
"HZ2": "H",
"CA": "C",
"CG1": "C",
"HE2": "H",
"CZ2": "C",
"OE2": "O",
"HG": "H",
"HZ": "H",
}
amino_acid_groups = [
["ARG", "LYS", "ASP", "GLU"],
["GLN", "ASN", "HIS", "SER", "THR", "CYS"],
["TRP", "TYR", "MET"],
["ILE", "LEU", "PHE", "VAL", "PRO", "GLY", "ALA"],
]
elements = ["H", "C", "N", "O", "S", "P", "F", "Cl", "Br", "I"]
def generate_columns_name(elements, amino_acid_groups):
"""
Generate feature names which used as columns in .csv file.
Parameters:
elements (list): Elements
amino_acid_groups (list): Classification of amino acids
Returns:
columns_name(dict): Feature names which used as columns in .csv file.
"""
columns_name = {}
i = 0
for ligand_element in elements:
for protein_element in elements:
for index, amino_acid_group in enumerate(amino_acid_groups):
columns_name[i] = (
ligand_element + "_" + protein_element + "_" + str(index)
)
i += 1
return columns_name
def ligand_specific_element_coordinates(ligand, element, complex=False):
"""
Returns coordinates of ligand's atoms belong to specific element.
Parameters:
ligand (pandas dataframe):Pandas dataframe object extrated from mol2 file of ligand.
element (str): Element name in string format.
complex (bool): Use when ligand and protein are in a single pdb file.
Returns:
coordinates(numpy array):Numpy array contains ligand coordinates.
"""
if complex:
coordinates = ligand[ligand["element_symbol"] == element].loc[
:, ["x_coord", "y_coord", "z_coord"]
]
else:
coordinates = ligand[ligand["element_symbol"] == element].loc[
:, ["x", "y", "z"]
]
return coordinates.to_numpy()
def protein_specific_element_coordinates(protein, element, amino_acid_group):
"""
Returns coordinates of protein's atoms belong to specific element.
Parameters:
protein (pandas dataframe):Pandas dataframe object extrated from pdb file of protein.
element (str): Element name in string format.
Returns:
coordinates(numpy array):Numpy array contains protein coordinates.
"""
protein = protein[protein["residue_name"].isin(amino_acid_group)]
coordinates = protein[protein["element_symbol"] == element].loc[
:, ["x_coord", "y_coord", "z_coord"]
]
return coordinates.to_numpy()
def weighting_sum(distances, cutoff, exp):
"""
Returns generated feature.
Parameters:
distances (numpy array):Distances between ligand and protein atoms.
cutoff (float): Distance cutoff.
exp (float): Weighting exponent
Returns:
feature(float): Generated feature.
"""
selected_distances = distances[distances < cutoff]
feature = sum(list(map(lambda x: 1.0 / (x ** exp), selected_distances)))
return feature
def generate_features(path, exp=2, cutoff=4.5, complex=False, filename="file.csv"):
"""
Returns coordinates of ligand's atoms belong to specific element.
Parameters:
path (str):Path of structures files in string.
cutoff (float): Distance cutoff.
exp (float): Weighting exponent
complex (bool): Use when ligand and protein are in a single pdb file.
filename (str): Filename of generated cvs file.
Returns:
df(csv):Return csv file of generated features.
"""
data = {}
entries = Path(path)
numbers = len(os.listdir(path))
bar = progressbar.ProgressBar(maxval=numbers).start()
for num, entry in enumerate(entries.iterdir()):
bar.update(num + 1)
features = []
if complex:
complex_file = glob(str(entry))
pdf = PandasPdb().read_pdb(complex_file[0])
pmol = pdf.df["HETATM"]
ppdb = pdf.df["ATOM"]
ppdb["element_symbol"] = ppdb["atom_name"].map(atom_types)
else:
ligand_file = glob(str(entry) + "//*.mol2")
protein_file = glob(str(entry) + "//*.pdb")
try:
pmol = PandasMol2().read_mol2(ligand_file[0])
except Exception:
print(f"Error in file structure {str(entry.name)}")
continue
pmol = pmol.df
pmol["element_symbol"] = pmol["atom_type"].apply(lambda x: x.split(".")[0])
ppdb = PandasPdb().read_pdb(protein_file[0])
ppdb = ppdb.df["ATOM"]
ppdb["element_symbol"] = ppdb["atom_name"].map(atom_types)
for ligand_element in elements:
for protein_element in elements:
for amino_acid_group in amino_acid_groups:
ligand_coords = ligand_specific_element_coordinates(
pmol, ligand_element, complex
)
protein_coords = protein_specific_element_coordinates(
ppdb, protein_element, amino_acid_group
)
distances = spatial.distance.cdist(
ligand_coords, protein_coords
).ravel()
features.append(weighting_sum(distances, cutoff, exp))
if complex:
name = entry.name
else:
name = entry.name
data[name] = features
columns_name = generate_columns_name(elements, amino_acid_groups)
df = pd.DataFrame(data).transpose().rename(columns=columns_name)
return df.to_csv(filename)
if __name__ == "__main__":
start = time.time()
print("\n")
print("Job is started.")
print("------------------------------")
parser = argparse.ArgumentParser(
description="Generating features for a set of given structures."
)
parser.add_argument(
"-d", "--directory", help="Directory of structures files.", required=True
)
parser.add_argument(
"-n",
"--exp",
type=float,
default=2,
help="Exponent is used in weighting factor.",
)
parser.add_argument(
"-o",
"--cutoff",
type=float,
default=4.5,
help="Cutoff distance is used during feature engeineering.",
)
parser.add_argument(
"-c",
"--complex",
type=bool,
default=False,
help="Indicating input files are complex or not.",
)
parser.add_argument(
"-f", "--filename", default="file.csv", help="Name of output csv file."
)
args = parser.parse_args()
print("Input parameters:")
print(f"Exponent: {args.exp}")
print(f"Complex: {args.complex}")
print(f"Cutoff: {args.cutoff}")
print(f"Filename: {args.filename}")
print("------------------------------")
print("Please wait until all features are generated...")
generate_features(
args.directory, args.exp, args.cutoff, args.complex, args.filename
)
end = time.time()
seconds = end - start
m, s = divmod(seconds, 60)
h, m = divmod(m, 60)
print("------------------------------")
print(f"Job is done at {h} hours, {m} minutes and {s:.2f} seconds!")
print(f"{args.filename} is created.")