-
Notifications
You must be signed in to change notification settings - Fork 0
/
docking_dna.py
executable file
·550 lines (483 loc) · 18.2 KB
/
docking_dna.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
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
#!/usr/bin/env python
import argparse
import os
import multiprocessing
import subprocess
import shutil
import glob
import json
from utils import logger
""" Script configuration """
tmp_folder_name = '.tmp'
uploads_folder_name = 'uploads'
json_results_file_name = '.results.json'
ini_file_script = 'prepare_ini_file.py'
pydock_bin = 'pydock3'
sampling_script = 'run_ftdock.sh'
scoring_script = 'parallel_scoring.py'
models_dest_folder = 'models'
models_prefix = 'mug_'
results_csv_file = 'result.csv'
mock_folder_dna = "/home/user/bin/mug/mock/3mfk"
mock_folder_protein = "/home/user/bin/mug/mock/3mfk_monomers"
top_models = 10
""" End of configuration """
class CommandLineParser(object):
"""Parses command line"""
@staticmethod
def valid_file(file_name):
if not os.path.exists(file_name):
raise argparse.ArgumentTypeError("The file does not exist")
return file_name
@staticmethod
def valid_integer_number(ivalue):
try:
ivalue = int(ivalue)
except:
raise argparse.ArgumentTypeError("%s is an invalid value" % ivalue)
if ivalue <= 0:
raise argparse.ArgumentTypeError("%s is an invalid value" % ivalue)
return ivalue
class cd:
"""Context manager for changing the current working directory"""
def __init__(self, new_path):
self.new_path = os.path.expanduser(new_path)
def __enter__(self):
self.saved_path = os.getcwd()
os.chdir(self.new_path)
def __exit__(self, etype, value, traceback):
os.chdir(self.saved_path)
def read_config(config_json_file):
data = None
receptor_id = None
ligand_id = None
project_path = None
project_name = None
models = None
scoring = None
try:
with open(config_json_file) as data_file:
data = json.load(data_file)
if data['input_files'][0]['name'] == 'ligand':
ligand_id = data['input_files'][0]['value']
receptor_id = data['input_files'][1]['value']
else:
ligand_id = data['input_files'][1]['value']
receptor_id = data['input_files'][0]['value']
for argument in data['arguments']:
if argument['name'] == 'execution':
project_path = argument['value']
project_name = os.path.basename(project_path)
if argument['name'] == 'models':
models = argument['value']
if argument['name'] == 'scoring':
scoring = argument['value']
except Exception, e:
logger.error('Error reading config JSON: %s' % str(e))
return receptor_id, ligand_id, project_path, project_name, int(models), scoring
def read_metadata(metadata_json_file):
metadata = {}
try:
with open(metadata_json_file) as data_file:
data = json.load(data_file)
for argument in data:
metadata[argument['_id']] = argument
except Exception, e:
logger.error('Error reading metadata JSON: %s' % str(e))
return metadata
def get_top_from_ene(ene_file, top=10):
"""Parses the top models conformations from a .ene file"""
top_list = []
line_count = 0
with open(ene_file) as input_file:
for line in input_file:
line_count += 1
if line_count > 2:
fields = line.split()
conf = fields[0]
top_list.append(conf)
if line_count == top+2:
return top_list
return top_list
def ene_to_csv(ene_file, csv_file, top=100, has_header=True):
"""Energy file to CSV file format"""
with open(ene_file) as input_file:
with open(csv_file, 'w') as output_file:
lines_to_write=top
if has_header:
lines_to_write += 1
line_count = 0
for line in input_file:
if line and line[0] not in ['#', '-'] and line_count <= lines_to_write:
output_file.write((','.join([field.strip() for field in line.split()])) + os.linesep)
line_count += 1
def prepare_workspace(project_path, log_file):
"""Prepares the workspace"""
logger.progress("Preparing workspace", status="RUNNING")
# Create project path if required
if not os.path.exists(project_path):
os.makedirs(project_path)
# Create temporal working path if required
tmp_path = os.path.join(project_path, tmp_folder_name)
if not os.path.exists(tmp_path):
os.makedirs(tmp_path)
# Calculate the uploads path
source_data_path = os.path.join(project_path, uploads_folder_name)
# Calculate the results path
results_path = project_path
logger.progress("Preparing workspace", status="DONE")
return source_data_path, tmp_path, results_path
def setup_molecules(working_path, receptor_pdb, ligand_pdb, project_name):
with cd(working_path):
logger.progress("Setup", status="RUNNING")
command = "%s %s %s %s" % (ini_file_script, project_name, receptor_pdb, ligand_pdb)
process = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE)
#print 'Waiting for pid %s' % str(process.pid)
process.wait()
command = "%s %s setup > setup.log" % (pydock_bin, project_name)
process = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE)
#print 'Waiting for pid %s' % str(process.pid)
process.wait()
logger.progress("Setup", status="DONE")
return os.path.join(working_path, "%s_rec.pdb" % project_name), os.path.join(working_path, "%s_lig.pdb" % project_name)
def sampling(working_path, receptor_pdb, ligand_pdb, project_name, num_cores, mock=False, mock_folder=""):
with cd(working_path):
logger.progress("Sampling", status="RUNNING")
if not mock:
command = "%s %s %s %s %s" % (sampling_script, project_name, receptor_pdb, ligand_pdb, str(num_cores))
process = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE)
#print 'Waiting for pid %s' % str(process.pid)
process.wait()
command = "%s %s rotftdock" % (pydock_bin, project_name)
process = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE)
#print 'Waiting for pid %s' % str(process.pid)
process.wait()
else:
shutil.copy2(os.path.join(mock_folder, '3mfk.ftdock'), os.path.join(working_path, "%s.ftdock" % project_name))
shutil.copy2(os.path.join(mock_folder, '3mfk.rot'), os.path.join(working_path, "%s.rot" % project_name))
logger.progress("Sampling", status="DONE")
return os.path.join(working_path, "%s.ftdock" % project_name)
def scoring(working_path, project_name, num_cores, scoring_module="dockser", mock=False, mock_folder=""):
with cd(working_path):
logger.progress("Scoring", status="RUNNING")
if not mock:
command = "%s %s %s %s" % (scoring_script, project_name, str(num_cores), scoring_module)
process = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE)
#print 'Waiting for pid %s' % str(process.pid)
process.wait()
else:
shutil.copy2(os.path.join(mock_folder, '3mfk.ene'), os.path.join(working_path, "%s.ene" % project_name))
logger.progress("Scoring", status="DONE")
return os.path.join(working_path, "%s.ene" % project_name)
def create_top_structures(working_path, models_refix, project_name, top, file_name):
with cd(working_path):
with open(file_name, 'w') as output:
num_model = 1
for conf in top:
try:
pdb_file_name = "%s%s_%s.pdb" % (models_prefix, project_name, conf)
with open(pdb_file_name) as input_pdb:
output.write('MODEL %d\n' % num_model)
for line in input_pdb:
output.write(line)
output.write('ENDMDL\n')
num_model += 1
except IOError:
pass
def generate_models(working_path, project_name, num_models):
with cd(working_path):
logger.progress("Generating models", status="RUNNING")
command = "%s %s makePDB 1 %s %s.ene %s" % (pydock_bin, project_name, str(num_models), project_name, models_prefix)
#print 'Generating structures: %s' % str(command)
process = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE)
#print 'Waiting for pid %s' % str(process.pid)
process.wait()
# Keep the top
top = get_top_from_ene("%s.ene" % project_name, top=top_models)
#print 'Top structures are: %s' % str(top)
create_top_structures(working_path, models_prefix, project_name, top, 'top_structures.pdb')
# Create top 10
for i in range(top_models):
shutil.copy2("%s%s_%s.pdb" % (models_prefix, project_name, top[0]) , 'top_%d.pdb' % (i+1))
models_path = os.path.join(working_path, models_dest_folder)
if not os.path.exists(models_path):
os.makedirs(models_path)
else:
shutil.rmtree(models_path)
models = glob.glob("%s*.pdb" % models_prefix)
for model in models:
shutil.move(model, models_path)
logger.progress("Generating models", status="DONE")
return True
def clean_workspace(working_path, project_name):
"""Cleans the workspace from temporal folder and scratch files"""
with cd(working_path):
logger.progress("Cleaning", status="RUNNING")
# Remove scoring temporal folders
temp_folders = glob.glob('tmp_pyDock*')
for folder in temp_folders:
try:
shutil.rmtree(folder)
except:
pass
# Remove scratch sampling files
scratch_files = glob.glob('scratch*')
for scratch_file in scratch_files:
try:
os.remove(scratch_file)
except:
pass
# Remove specific files
try:
os.remove('%s.ftdock.log' % project_name)
except:
pass
logger.progress("Cleaning", status="DONE")
def create_compress_results(working_path, project_name):
with cd(working_path):
to_move = glob.glob('*')
if not os.path.exists(project_name):
os.makedirs(project_name)
for thing in to_move:
try:
shutil.move(thing, project_name)
except:
pass
command = "tar zcf %s.tgz %s" % (project_name, project_name)
process = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE)
process.wait()
return "%s.tgz" % project_name
def prepare_results(working_path, results_path, project_name, num_models):
with cd(working_path):
# Clean workspace from temporal results
clean_workspace(working_path, project_name)
# Move top PDB to results folder
try:
shutil.move('top_structures.pdb', results_path)
except:
pass
for i in range(top_models):
try:
shutil.move('top_%d.pdb' % (i+1), results_path)
except:
pass
# Create CSV file
ene_file = "%s.ene" % project_name
csv_file = os.path.join(results_path, results_csv_file)
ene_to_csv(ene_file, csv_file, top=num_models)
# Create compress file
tgz_file = create_compress_results(working_path, project_name)
try:
shutil.move(tgz_file, results_path)
except:
pass
def mark_as_complete(results_path, project_name):
json_file_name = os.path.join(results_path, json_results_file_name)
with open(json_file_name, 'w') as output:
content = """
{
"output_files": [
{
"name": "top_structures",
"source_id": [
""
],
"taxon_id": "",
"meta_data": {
},
"file_path": "%s/top_structures.pdb"
},
{
"name": "results",
"source_id": [
""
],
"taxon_id": "",
"meta_data": {
},
"file_path": "%s/%s.tgz"
},
{
"name": "energy_table",
"source_id": [
""
],
"taxon_id": "",
"meta_data": {
},
"file_path": "%s/%s"
},
{
"name": "top10",
"source_id": [
""
],
"taxon_id": "",
"meta_data": {
},
"file_path": "%s/top_1.pdb"
},
{
"name": "top10",
"source_id": [
""
],
"taxon_id": "",
"meta_data": {
},
"file_path": "%s/top_2.pdb"
},
{
"name": "top10",
"source_id": [
""
],
"taxon_id": "",
"meta_data": {
},
"file_path": "%s/top_3.pdb"
},
{
"name": "top10",
"source_id": [
""
],
"taxon_id": "",
"meta_data": {
},
"file_path": "%s/top_4.pdb"
},
{
"name": "top10",
"source_id": [
""
],
"taxon_id": "",
"meta_data": {
},
"file_path": "%s/top_5.pdb"
},
{
"name": "top10",
"source_id": [
""
],
"taxon_id": "",
"meta_data": {
},
"file_path": "%s/top_6.pdb"
},
{
"name": "top10",
"source_id": [
""
],
"taxon_id": "",
"meta_data": {
},
"file_path": "%s/top_7.pdb"
},
{
"name": "top10",
"source_id": [
""
],
"taxon_id": "",
"meta_data": {
},
"file_path": "%s/top_8.pdb"
},
{
"name": "top10",
"source_id": [
""
],
"taxon_id": "",
"meta_data": {
},
"file_path": "%s/top_9.pdb"
},
{
"name": "top10",
"source_id": [
""
],
"taxon_id": "",
"meta_data": {
},
"file_path": "%s/top_10.pdb"
}
]
}
""" % (results_path, results_path, project_name, results_path, results_csv_file, results_path, results_path, results_path, results_path, results_path, results_path, results_path, results_path, results_path, results_path)
output.write(content)
return json_file_name
def check_output(file_name):
"""Check if file exists and contains actual data"""
try:
if os.stat(file_name).st_size > 0:
return True
else:
# Empty file
return False
except OSError:
# No file
return False
def run_pipeline(args, num_cores):
# Prepare all required parameters to run the pipeline
receptor_id, ligand_id, project_path, project_name, num_models, scoring_function = read_config(args.config)
metadata = read_metadata(args.in_metadata)
receptor_pdb_file = metadata[receptor_id]['file_path']
ligand_pdb_file = metadata[ligand_id]['file_path']
# Log file
log_file = args.log_file
# Prepare workspace and get the relevant paths for the pipeline
source_data_path, working_path, results_path = prepare_workspace(project_path, log_file)
# Setup molecules
receptor_pdb, ligand_pdb = setup_molecules(working_path, receptor_pdb_file, ligand_pdb_file, project_name)
# Activate mocks if required
mocking = False
mock_folder = ""
rec_file = os.path.basename(metadata[receptor_id]['file_path'])
lig_file = os.path.basename(metadata[ligand_id]['file_path'])
if rec_file == '3mfk_homodimer.pdb' and lig_file == '3mfk_dna.pdb':
mocking = True
mock_folder = mock_folder_dna
logger.info("Mocking Protein-DNA: %s" % mock_folder)
if (rec_file == '3mfk_monomer2.pdb' and lig_file == '3mfk_monomer1.pdb') or (rec_file == '3mfk_monomer1.pdb' and lig_file == '3mfk_monomer2.pdb'):
mocking = True
mock_folder = mock_folder_protein
logger.info("Mocking Protein-Protein: %s" % mock_folder)
# Sampling step
sampling_output_file = sampling(working_path, receptor_pdb, ligand_pdb, project_name, num_cores, mocking, mock_folder)
if not check_output(sampling_output_file):
logger.error('Sampling process, FTDock output file not found')
raise SystemExit
# Energetic scoring step
scoring_output_file = scoring(working_path, project_name, num_cores, scoring_function, mocking, mock_folder)
if not check_output(scoring_output_file):
logger.error('Scoring process, energy table file not found')
raise SystemExit
# Generating models step
generate_models(working_path, project_name, num_models)
# Prepare results and cleaning step
prepare_results(working_path, results_path, project_name, num_models)
# Finishing the pipeline
json_file_name = mark_as_complete(results_path, project_name)
if __name__ == "__main__":
# Parse command line
parser = argparse.ArgumentParser(prog="docking_dna")
# Config file
parser.add_argument("--config", help="Configuration JSON file",
type=CommandLineParser.valid_file, metavar="config", required=True)
# Metadata
parser.add_argument("--in_metadata", help="Project metadata", metavar="in_metadata", required=True)
# Output metadata
parser.add_argument("--out_metadata", help="Output metadata", metavar="output_metadata", required=True)
# Log file
parser.add_argument("--log_file", help="Log file", metavar="log_file", required=True)
args = parser.parse_args()
# Number of cores available
num_cores = multiprocessing.cpu_count()
# Protein-DNA docking pipeline
run_pipeline(args, num_cores)