-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProtein_Refinement.py
347 lines (308 loc) · 12.4 KB
/
Protein_Refinement.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
import argparse
from Methods.distance_file_gen import gen_distance_file
from Methods.atoms_reordination import atoms_re_ordination
from Methods.spectral_projected_gradient import protein_spg
from Methods.pdf_file_gen import write_pdb_file
from Methods.utils import *
from Methods.obj import *
from LOG import os_display_call
from time import time
parser = argparse.ArgumentParser(description="A protein refinement method...")
parser.add_argument("filename", type=str, help="Input protein name")
parser.add_argument(
"--distance_overwrite",
default=False,
help="Enable the overwrite option for the distance file generator function (default: False)",
)
parser.add_argument(
"--multi_start",
default=False,
help="Enable the multi start option. (default: False)",
)
parser.add_argument(
"--convex_relax",
default=False,
help="Indicates whether the starting point originates from convex relaxation (default: False)",
)
parser.add_argument(
"--local_dir",
default="C:\\Users\\viniv\\Documents\\protein_tests\\Nodes",
help="main directory from Testes/Nodes folder (default: .\\Documents\\protein_tests\\Nodes)",
)
parser.add_argument(
"--debug_mode",
default=False,
help="Enables the option for debug mode in the spectral gradient method",
)
args = parser.parse_args()
# protein filename:
filename = args.filename
# local directory:
local_dir = args.local_dir
# main PDB file directory:
dir_pdb = local_dir + f"\\Teste {filename}\\{filename}.txt"
# Start LOG list:
log = []
try:
# open PDB file (as an np-array):
pdb = np.genfromtxt(dir_pdb, dtype="str")
print(":: PDB file read complete! Initiating distance file generation")
# num_atom_init :: Initial number of atoms
num_atom_init = int(len(pdb[:, 1]))
# log archive
log.append(":: PDB file read complete! Initiating distance file generation")
except FileNotFoundError:
print(":: PDB file not found!")
pdb, num_atom_init = [], 0
print(":: The process was interrupted")
# log archive
log.append(":: PDB file not found!")
log.append(":: The process was interrupted")
exit()
# distance file generator -- generates the distance file, if it doesn't exists, and returns it's directory:
try:
raid_d = gen_distance_file(
pdb, filename, local_dir, overwrite=args.distance_overwrite
)
print(":: Process completed successfully, waiting for data to be read...")
# open distance file (as an np-array):
distancias = np.genfromtxt(raid_d, dtype="str")
print(f":: distance file dist_{filename} read complete!")
# index vectors (for atom pairs [u, v]):
u, v = (
np.array(distancias[:, 0], dtype="int"),
np.array(distancias[:, 1], dtype="int"),
)
# it starts from zero on python
u = u - np.ones(len(u), dtype="int")
v = v - np.ones(len(v), dtype="int")
# lower and upper bounds vectors:
lb, ub = (
np.array(distancias[:, 8], dtype="float"),
np.array(distancias[:, 9], dtype="float"),
)
prop_dist = 0
for k in range(len(u)):
if int(distancias[k][-1]) != 0:
prop_dist += 1
prop_dist = int(prop_dist)
# Log archive
log.append(":: Process completed successfully, waiting for data to be read...")
log.append(f":: distance file dist_{filename} read complete!")
except OSError as err:
print(f":: Distance file generator found an error {err}")
print(":: The process was interrupted")
distancias, u, v, lb, ub, prop_dist = [], [], [], [], [], 0
# log archive
log.append(f":: Distance file generator found an error {err}")
log.append(":: The process was interrupted")
exit()
# Adjust variables:
Noise, TOL, N, M, w = 1e-1, 1e-6, 2000, 15, np.ones(len(lb))
# Noise :: Degree of disturbance of the expected solution;
# TOL :: tolerance for the SPG;
# N :: maximum accepted number of iterations;
# M :: non monotone parameter of GLL line search;
# w :: weight vector
# Pre-initialization:
# Choice/re-ordination of the available atoms in accordance with the distance data file
solution, total_atoms_ord, string_ord, atoms = atoms_re_ordination(pdb, distancias)
solution = centralizar(solution)
log.append(string_ord)
# Initial point origin:
if args.convex_relax:
print(":: Initial point --Originated from convex relaxation")
log.append(":: Initial point --Originated from convex relaxation")
try:
# directory to initial point file (convex relax solution)
raid = local_dir + f"\\Teste {filename}\\relax_scaled_{filename}.txt"
xi = np.genfromtxt(raid)
xi = centralizar(xi)
print(f":: relax_scaled_{filename} file read complete.")
log.append(f":: relax_scaled_{filename} file read complete.")
# directory to initial point file (non scaled relax solution)
raid = local_dir + f"\\Teste {filename}\\relax_non_scaled_{filename}.txt"
non_scaled = np.genfromtxt(raid)
non_scaled = centralizar(non_scaled)
print(f":: relax_non_scaled_{filename} file read complete.")
log.append(f":: relax_non_scaled_{filename} file read complete.")
# Starting point from convex relaxation:
# calculating the objective function value for the non scaled initial point (stress)
dist_non_scaled = dist_matrix_projection(len(u), u, v, lb, ub, non_scaled)
fo_non_scaled = stress(non_scaled, dist_non_scaled, u, v, w)
# calculating the objective function value for the scaled initial point (stress)
dist_non_scaled = dist_matrix_projection(len(u), u, v, lb, ub, xi)
fo_scaled = stress(xi, dist_non_scaled, u, v, w)
except FileNotFoundError:
print(
f":: File relax_scaled_{filename} or relax_non_scaled_{filename} not found --convex_relax: False"
)
log.append(
f":: File relax_scaled_{filename} or relax_non_scaled_{filename} not found --convex_relax: False"
)
args.convex_relax = False
fo_scaled, fo_non_scaled, xi = 0, 0, []
else:
print(":: Generating initial point file using a perturbed expected solution.")
log.append(":: Generating initial point file using a perturbed expected solution.")
xi = Noise * np.asarray([np.random.normal(0, 1, len(solution)) for i in range(3)]).T
xi = xi + solution
xi = np.array(xi)
xi = centralizar(xi)
fo_non_scaled, fo_scaled = 0, 0
# Initiate solver:
if args.multi_start:
data = {}
try:
print(f":: Multi-start option --Enable {10}x times")
log.append(f":: Multi-start option --Enable {10}x times")
print(f":: maximum iterations: {N}, tol: {TOL} and memory: {M}")
log.append(f":: maximum iterations: {N}, tol: {TOL} and memory: {M}")
# multi start option enable, initial distance vector will be set as in multi-start definition
yi = dist_matrix_projection(int(len(u)), u, v, lb, ub, xi)
to = time()
out = protein_spg(
stress,
grad_stress,
xi,
yi,
[u, v, w, lb, ub, TOL, N, M],
debug_mode=args.debug_mode,
)
elapsed_time = time() - to
fo = stress(xi, yi, u, v, w)
if args.debug_mode:
X = out[0]
if check_solution_dimension(X, solution):
print(
":: Solution found and expected solution have different number of atoms!"
)
log.append(
":: Solution found and expected solution have different number of atoms!"
)
print(f":: ({0}) Solution found!")
data[0] = (out, elapsed_time, fo)
for i in range(1, 10):
yi = dist_matrix_projection(int(len(u)), u, v, lb, ub, xi, multistart=True)
to = time()
out = protein_spg(
stress,
grad_stress,
xi,
yi,
[u, v, w, lb, ub, TOL, N, M],
debug_mode=args.debug_mode,
)
elapsed_time = time() - to
fo = stress(xi, yi, u, v, w)
if args.debug_mode:
X = out[0]
if check_solution_dimension(X, solution):
print(
":: Solution found and expected solution have different number of atoms!"
)
log.append(
":: Solution found and expected solution have different number of atoms!"
)
print(f":: ({i}) Solution found!")
data[i] = (out, elapsed_time, fo)
except Exception as ee:
print(
f":: Attempt for protein_spg --Multi-start --Enable failed with --bad error: {ee}"
)
print(":: The process was interrupted!")
log.append(
f":: Attempt for protein_spg --Multi-start --Enable failed with --bad error: {ee}"
)
log.append(":: The process was interrupted!")
exit()
out = []
fo, elapsed_time = 0.0, 0.0
# parameter initialization for data output:
ops = (xi, solution, u, v, lb, ub)
main = (
filename,
num_atom_init,
total_atoms_ord,
len(u),
prop_dist,
args.convex_relax,
fo_non_scaled,
fo_scaled,
ops,
)
os_display_call(log, local_dir, main, data, multistart=True)
else:
# multi start option not enable, initial distance vector will be the standard
yi = dist_matrix_projection(int(len(u)), u, v, lb, ub, xi)
if args.debug_mode:
print(":: Debug mode --True")
log.append(":: Debug mode --True")
try:
print(f":: maximum iterations: {N}, tol: {TOL} and memory: {M}")
log.append(f":: maximum iterations: {N}, tol: {TOL} and memory: {M}")
to = time()
out = protein_spg(
stress,
grad_stress,
xi,
yi,
[u, v, w, lb, ub, TOL, N, M],
debug_mode=args.debug_mode,
)
elapsed_time = time() - to
fo = stress(xi, yi, u, v, w)
log.append(out[-1])
print(":: Solution found!")
log.append(":: Solution found!")
if args.debug_mode:
X = out[0]
if check_solution_dimension(X, solution):
print(
":: Solution found and expected solution have different number of atoms!"
)
log.append(
":: Solution found and expected solution have different number of atoms!"
)
data = (out, elapsed_time, fo)
except Exception as err:
print(f":: Attempt for protein_spg failed with --bad error: {err}")
print(":: The process was interrupted!")
log.append(f":: Attempt for protein_spg failed with --bad error: {err}")
log.append(":: The process was interrupted!")
exit()
out = []
fo, elapsed_time = 0.0, 0.0
data = (out, elapsed_time, fo)
ops = (xi, solution, u, v, lb, ub)
main = (
filename,
num_atom_init,
total_atoms_ord,
len(u),
prop_dist,
args.convex_relax,
fo_non_scaled,
fo_scaled,
ops
)
os_display_call(log, local_dir, main, data)
# ----------------------------------------------------------
# Output the data to a file
# ----------------------------------------------------------
atoms = np.array(atoms)
def raid_gen(arch):
return local_dir + f"\\Teste {filename}\\{arch}_{filename}.pdb"
# export solution pdb file:
# output PDB file format -- ATOM i atom amino A res x1 x2 x3 0.00 0.00 atom[0]:
write_pdb_file(raid_gen("Sol"), out[0], atoms[:, 1], atoms[:, 3], atoms[:, 2])
# export original solution pdb file -- only if it dos not already exists
try:
with open(raid_gen("Orig")) as f:
print(f"Orig_{filename} already exists!")
except FileNotFoundError:
print(f"Creating a new Orig_{filename}")
write_pdb_file(raid_gen("Orig"), solution, atoms[:, 1], atoms[:, 3], atoms[:, 2])
# export initial point
write_pdb_file(raid_gen("Ponto"), xi, atoms[:, 1], atoms[:, 3], atoms[:, 2])
# ----------------------------------------------------------