-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathzdock.py
executable file
·418 lines (381 loc) · 18.4 KB
/
zdock.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
#! /usr/bin/python3
# Author: machunning
# Email: [email protected] | [email protected]
import os
import shutil
import argparse
from concurrent.futures import ProcessPoolExecutor, wait, ALL_COMPLETED
"""
mark_sur function
@param { string } command_path
@param { string } input_path
@param { string } filename
@param { string } output_path
@return { number } 0: success -1: fail
"""
def mark_sur(command_path, input_path, filename, output_path):
if not os.path.exists("%s/%s_m.pdb" % (output_path, filename[0:-4])):
try:
shutil.copy("%s/%s" % (input_path, filename), command_path)
os.system("cd %s && mark_sur %s %s_m.pdb" %
(command_path, filename, filename[0:-4]))
os.remove("%s/%s" % (command_path, filename))
shutil.move("%s/%s_m.pdb" %
(command_path, filename[0:-4]), output_path)
mark_sur_error(output_path, "%s_m.pdb" % filename[0:-4])
except BaseException as e:
f = open("error.log", "a+")
f.write("%s\n" % e)
f.close()
return 0
else:
return -1
#
"""
mark_sur function
执行部分pdb不会报错但生成了一个81字节的文件, 文件会影响zdock的运行
function_name: mark_sur_error
@param { string } output_path
@param { string } filename
"""
def mark_sur_error(output_path, filename):
if os.path.getsize("%s/%s" % (output_path, filename)) == 81:
f = open("error.log", "a+")
f.write("[error] %s is an empty file.\n" % filename)
f.close()
os.remove("%s/%s" % (output_path, filename))
"""
zdock function
@param { string } command_path
@param { string } input_path
@param { string } filename
@param { string } ligand_path
@param { string } ligand_name
@param { string } output_path
"""
def zdock(command_path, input_path, filename, ligand_path, ligand_name, output_path):
if not os.path.exists(output_path):
os.mkdir(output_path)
if not os.path.exists("%s/%s_%s.out" % (output_path, filename[0:-4], ligand_name[0:-6])):
try:
print("%s & %s start" % (ligand_name, filename))
os.system("%s/zdock -R %s/%s -L %s/%s -o %s/%s_%s.out"
% (command_path, input_path, filename, ligand_path,
ligand_name, output_path, filename[0:-4], ligand_name[0:-6]))
except BaseException as e:
f = open("error.log", "a+")
f.write('%s\n' % e)
f.close()
else:
print("%s_%s.out was created" % (filename[0:-4], ligand_name[0:-6]))
"""
create_pl function
@param { string } command_path
@param { string } input_path input file path + pdb name
@param { string } filename pdb_m_Ag**.out
@param { string } output_path
"""
def create_pl(command_path, input_path, filename, output_path):
if not os.path.exists("%s/%s" % (output_path, filename[-8:-4])):
os.mkdir("%s/%s" % (output_path, filename[-8:-4]))
new_path = "%s/%s/%s" % (output_path, filename[-8:-4], filename[0:-11])
if not os.path.exists(new_path):
os.mkdir(new_path)
if not os.path.exists("%s/create_lig" % new_path):
shutil.copy("%s/create_lig" % command_path, new_path)
if not os.path.exists("%s/%s" % (new_path, filename)):
shutil.copy("%s/%s" % (input_path, filename), new_path)
try:
os.system("cd %s && %s/create.pl %s 10" %
(new_path, command_path, filename))
print("create_pl %s successful!" % filename)
os.remove("%s/create_lig" % new_path)
os.remove("%s/%s" % (new_path, filename))
except BaseException as e:
f = open("error.log", "a+")
f.write('%s\n' % e)
f.close()
if __name__ == "__main__":
version = "v0.9.0"
# project root dir
project_path = os.getcwd()
# command file path
command_path = "%s/command" % project_path
# create argumentParser
parser = argparse.ArgumentParser()
parser.add_argument(
"-version", help="zdock_script version", action="store_true")
parser.add_argument("-mode", type=int, help="script mode", default=0)
parser.add_argument("-input", help="pdb input file(path)")
parser.add_argument("-output", help="pdb output file(path)")
parser.add_argument("-ligand", help="pdb ligand file(path)")
parser.add_argument("-multiProcessMode", type=int,
help="multi process mode", default=0)
args = parser.parse_args()
# print script version
if args.version:
print("zdock batch script For Linux %s" % version)
exit()
# Judging if input pdb is a file or a directory
if args.input:
if os.path.isdir(args.input):
input_path = {
"path": os.path.abspath(args.input),
"isDir": True
}
elif os.path.isfile(args.input):
input_path = {
"path": os.path.abspath(args.input),
"isDir": False
}
else:
print("ERROR: input argument could not find file or directory.")
exit()
else:
print("ERROR: Please enter an input argument!")
exit()
# Judging the output path
if args.output:
if os.path.exists(args.output) and os.path.isdir(args.output):
output_path = os.path.abspath(args.output)
else:
os.mkdir(args.output)
output_path = os.path.abspath(args.output)
else:
output_path = project_path
# Judging the ligand path
if args.ligand:
if os.path.isdir(args.ligand):
ligand_path = {
"path": os.path.abspath(args.ligand),
"isDir": True
}
elif os.path.isfile(args.ligand):
ligand_path = {
"path": os.path.abspath(args.ligand),
"isDir": False
}
else:
print("ERROR: ligand argument could not find file or directory.")
exit()
elif args.mode == 0 or args.mode == 2:
print("ERROR: The ligand argument is required")
exit()
# open process pool
if args.multiProcessMode:
process_pool = ProcessPoolExecutor(args.multiProcessMode)
print("input path: %s" % input_path["path"])
print("output path: %s" % output_path)
print("multiProcessMode: %s" % args.multiProcessMode)
# mode1: mark_sur all pdb
if args.mode == 1:
# input is a directory
if input_path["isDir"]:
for filename in os.listdir(input_path["path"]):
# Do not enable multiple processes mode
if args.multiProcessMode == 0:
mark_sur(command_path,
input_path["path"], filename, output_path)
# multiple processes mode
else:
process_pool.submit(
mark_sur, command_path, input_path["path"], filename, output_path)
# input is a file
elif not input_path["isDir"]:
mark_sur(command_path, os.path.dirname(input_path["path"]),
os.path.basename(input_path["path"]), output_path)
# mode2: zdock all pdb_m
if args.mode == 2:
print("ligand path: %s" % ligand_path["path"])
# input is a directory and ligand is a directory.
if input_path["isDir"] and ligand_path["isDir"]:
for ligand_name in os.listdir(ligand_path["path"]):
for filename in os.listdir(input_path["path"]):
# Do not enable multiple processes mode
if args.multiProcessMode == 0:
zdock(command_path, input_path["path"], filename,
ligand_path["path"], ligand_name,
"%s/%s" % (output_path, filename[0:-6]))
# multiple processes mode
else:
process_pool.submit(zdock, command_path, input_path["path"],
filename, ligand_path["path"], ligand_name,
"%s/%s" % (output_path, filename[0:-6]))
# input is a file and ligand is a directory.
if not input_path["isDir"] and ligand_path["isDir"]:
for ligand_name in os.listdir(ligand_path["path"]):
# Do not enable multiple processes mode
if args.multiProcessMode == 0:
zdock(command_path, os.path.dirname(input_path["path"]),
os.path.basename(
input_path["path"]), ligand_path["path"], ligand_name,
"%s/%s" % (output_path, os.path.basename(input_path["path"])[0:-6]))
# multiple processes mode
else:
process_pool.submit(zdock, command_path, os.path.dirname(input_path["path"]),
os.path.basename(
input_path["path"]), ligand_path["path"],
ligand_name, "%s/%s" % (output_path, os.path.basename(input_path["path"])[0:-6]))
# input is a directory and ligand is a file.
if input_path["isDir"] and not ligand_path["isDir"]:
for filename in os.listdir(input_path["path"]):
# Do not enable multiple processes mode
if args.multiProcessMode == 0:
zdock(command_path, input_path["path"], filename,
os.path.dirname(ligand_path["path"]),
os.path.basename(ligand_path["path"]),
"%s/%s" % (output_path, filename[0:-6]))
# multiple processes mode
else:
process_pool.submit(zdock, command_path, input_path["path"], filename,
os.path.dirname(ligand_path["path"]),
os.path.basename(ligand_path["path"]),
"%s/%s" % (output_path, filename[0:-6]))
# input is a file and ligand is a file.
if not input_path["isDir"] and not ligand_path["isDir"]:
zdock(command_path, os.path.dirname(input_path["path"]),
os.path.basename(input_path["path"]),
os.path.dirname(ligand_path["path"]),
os.path.basename(ligand_path["path"]),
"%s/%s" % (output_path, os.path.basename(input_path["path"])[0:-6]))
# step3: create_pl all pdb_m.out
if args.mode == 3:
# input is a directory
if input_path["isDir"]:
for dirname in os.listdir(input_path["path"]):
for filename in os.listdir("%s/%s" % (input_path["path"], dirname)):
if args.multiProcessMode == 0:
create_pl(command_path, "%s/%s" % (input_path["path"], dirname),
filename, output_path)
else:
process_pool.submit(create_pl, command_path,
"%s/%s" % (input_path["path"],
dirname),
filename, output_path)
# input is a file
if not input_path["isDir"]:
create_pl(command_path, os.path.dirname(input_path["path"]),
os.path.basename(input_path["path"]), output_path)
# step0: default mode
# Execute mark_sur, zdock, create_pl three steps on pdb
if args.mode == 0:
print("ligand path: %s" % ligand_path["path"])
mark_sur_pdb_path = "%s/mark_sur_pdb" % output_path
if not os.path.exists(mark_sur_pdb_path):
os.mkdir(mark_sur_pdb_path)
zdock_pdb_path = "%s/zdock_pdb" % output_path
if not os.path.exists(zdock_pdb_path):
os.mkdir(zdock_pdb_path)
create_pl_pdb = "%s/create_pl_pdb" % output_path
if not os.path.exists(create_pl_pdb):
os.mkdir(create_pl_pdb)
# input is a directory
if input_path["isDir"]:
# mark_sur pdb
# Do not enable multiple processes mode
if args.multiProcessMode == 0:
for filename in os.listdir(input_path["path"]):
mark_sur(command_path,
input_path["path"], filename, mark_sur_pdb_path)
# multiple processes mode
else:
all_task = [process_pool.submit(mark_sur, command_path,
input_path["path"], filename, mark_sur_pdb_path)
for filename in os.listdir(input_path["path"])]
wait(all_task, return_when=ALL_COMPLETED)
# zdock pdb
# ligand is a directory
if ligand_path["isDir"]:
# Do not enable multiple processes mode
if args.multiProcessMode == 0:
for ligand_name in os.listdir(ligand_path["path"]):
for filename in os.listdir(mark_sur_pdb_path):
zdock(command_path, mark_sur_pdb_path, filename,
ligand_path["path"], ligand_name,
"%s/%s" % (zdock_pdb_path, filename[0:-6]))
# multiple processes mode
else:
all_task = [process_pool.submit(zdock, command_path, mark_sur_pdb_path, filename,
ligand_path["path"], ligand_name,
"%s/%s" % (zdock_pdb_path, filename[0:-6]))
for ligand_name in os.listdir(ligand_path["path"])
for filename in os.listdir(mark_sur_pdb_path)]
wait(all_task, return_when=ALL_COMPLETED)
# ligand is a file
if not ligand_path["isDir"]:
# Do not enable multiple processes mode
if args.multiProcessMode == 0:
for filename in os.listdir(mark_sur_pdb_path):
zdock(command_path, mark_sur_pdb_path, filename,
os.path.dirname(ligand_path["path"]),
os.path.basename(ligand_path["path"]),
"%s/%s" % (zdock_pdb_path, filename[0:-6]))
# multiple processes mode
else:
all_task = [process_pool.submit(zdock, command_path, mark_sur_pdb_path, filename,
os.path.dirname(
ligand_path["path"]),
os.path.basename(
ligand_path["path"]),
"%s/%s" % (zdock_pdb_path, filename[0:-6]))
for filename in os.listdir(mark_sur_pdb_path)]
wait(all_task, return_when=ALL_COMPLETED)
# create_pl pdb
for dirname in os.listdir(zdock_pdb_path):
for filename in os.listdir("%s/%s" % (zdock_pdb_path, dirname)):
if args.multiProcessMode == 0:
create_pl(command_path, "%s/%s" % (zdock_pdb_path, dirname),
filename, create_pl_pdb)
else:
process_pool.submit(create_pl, command_path,
"%s/%s" % (zdock_pdb_path,
dirname),
filename, create_pl_pdb)
# input is a file
if not input_path["isDir"]:
# mark_sur pdb
mark_sur(command_path, os.path.dirname(input_path["path"]),
os.path.basename(input_path["path"]), mark_sur_pdb_path)
# zdock pdb
# ligand is a directory
if ligand_path["isDir"]:
# Do not enable multiple processes mode
if args.multiProcessMode == 0:
for ligand_name in os.listdir(ligand_path["path"]):
for filename in os.listdir(mark_sur_pdb_path):
zdock(command_path, mark_sur_pdb_path, filename,
ligand_path["path"], ligand_name,
"%s/%s" % (zdock_pdb_path, filename[0:-6]))
# multiple processes mode
else:
all_task = [process_pool.submit(zdock, command_path, mark_sur_pdb_path, filename,
ligand_path["path"], ligand_name,
"%s/%s" % (zdock_pdb_path, filename[0:-6]))
for ligand_name in os.listdir(ligand_path["path"])
for filename in os.listdir(mark_sur_pdb_path)]
wait(all_task, return_when=ALL_COMPLETED)
# ligand is a file
if not ligand_path["isDir"]:
for filename in os.listdir(mark_sur_pdb_path):
zdock(command_path, mark_sur_pdb_path, filename,
os.path.dirname(ligand_path["path"]),
os.path.basename(ligand_path["path"]),
"%s/%s" % (zdock_pdb_path, filename[0:-6]))
# create_pl pdb
for dirname in os.listdir(zdock_pdb_path):
for filename in os.listdir("%s/%s" % (zdock_pdb_path, dirname)):
if not ligand_path["isDir"]:
create_pl(command_path, "%s/%s" % (zdock_pdb_path, dirname),
filename, create_pl_pdb)
if ligand_path["isDir"]:
# Do not enable multiple processes mode
if args.multiProcessMode == 0:
create_pl(command_path, "%s/%s" % (zdock_pdb_path, dirname),
filename, create_pl_pdb)
# multiple processes mode
else:
process_pool.submit(create_pl, command_path,
"%s/%s" % (zdock_pdb_path,
dirname),
filename, create_pl_pdb)
# Script END