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 2
/
Copy pathgenerate_features.py
303 lines (223 loc) · 6.91 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
import os
import time
import argparse
import progressbar
from glob import glob
from pathlib import Path
import numpy as np
import pandas as pd
from scipy import spatial
from biopandas.pdb import PandasPdb
from biopandas.mol2 import PandasMol2
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):
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):
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):
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):
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, complex=False, filename="file.csv"):
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)
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, 12.0, 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(
"-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("Inputs")
print(f"Exponent: {args.exp}")
print(f"Complex: {args.complex}")
print(f"Filename: {args.filename}")
print("------------------------------")
print("Please wait until all features are generated...")
generate_features(args.directory, args.exp, 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.")