forked from qusers/qligfep
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathprotprep.py
executable file
·630 lines (541 loc) · 28.3 KB
/
protprep.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
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
import argparse
from subprocess import check_output
import os
import collections
from time import gmtime, strftime
import numpy as np
import functions as f
import settings as s
import IO
class Run(object):
"""
Prepare a protein for usage in spherical boundary conditions.
"""
def __init__(self, prot, sphereradius, spherecenter, include, water,
preplocation, origin, noclean, *args, **kwargs):
self.prot = prot
self.radius = float(sphereradius)
self.center = spherecenter
self.include = include
self.water = water
self.noclean = noclean
self.PDB = {}
self.preplocation = preplocation
self.origin = origin
self.original_charges = {}
self.chains = []
self.log = {'INPUT':'protPREP.py -p {} -r {} -c {} -w={} -V={}'.format(prot,
sphereradius,
spherecenter,
water,
noclean
),
'CENTER':None,
'DECHARGE':[],
'CHARGE':[],
'TOTAL_CHARGE':0,
'CTERM':[],
'CYX':[],
'QRESN':{},
'PDB2Q':{},
'QRES_LIST':[],
'TIME':strftime("%Y-%m-%d %H:%M:%S", gmtime()),
}
def get_center_coordinates(self):
center = self.center
center = center.strip('[')
center = center.strip(']')
center = center.split(':')
with open(self.prot) as infile:
for line in infile:
if line.startswith(self.include):
line = IO.pdb_parse_in(line)
if center[0] == 'RESN':
if line[5] == center[2]:
if line[6] == int(center[1]) \
and line[2].strip() == 'CA':
self.center = [float(line[8]),
float(line[9]),
float(line[10])
]
elif center[0] == 'ATN':
if line[1] == int(center[1]):
self.center = [float(line[8]),
float(line[9]),
float(line[10])
]
elif len(center) == 3:
self.center = [float(center[0]),
float(center[1]),
float(center[2])
]
else:
print('Could not get center')
self.log['CENTER'] = '{} {} {}'.format(*self.center)
# The name of this function should be more general when the various inputs are there
def prepwizard_parse(self):
if self.origin == 'gromacs':
with open(self.prot) as infile, \
open(self.prot[:-4] + '_noH.pdb', 'w') as outfile:
for line in infile:
tmp = line
if line.startswith(self.include) == False:
continue
line = IO.pdb_parse_in(line)
if tmp[13] == 'H':# and line[4] != 'SOL':
write = False
else:
write = True
# Change residue name of waters
if line[4] == 'SOL':
line[4] = 'HOH'
if line[2] == 'OW':
line[2] = 'O'
coord1 = self.center
coord2 = [float(line[8]),
float(line[9]),
float(line[10])
]
write = f.euclidian_overlap(coord1, coord2, self.radius + 5)
if self.water != False:
line_out = IO.pdb_parse_out(line)
else:
continue
elif line[4] == 'ILE' and line[2] == 'CD':
line[2] = 'CD1'
line_out = IO.pdb_parse_out(line)
elif line[4] == 'CL-':
continue
line_out = IO.pdb_parse_out(line)
# Get the charges from the hydrogen connections
# NOTE: this might be more common and thus less lines of code might be
# needed, check when implementing MolProbity!!
if line[4] in IO.charged_res:
if line[2] in IO.charged_res[line[4]]:
if line[5] not in self.original_charges:
self.original_charges[line[5]] = {}
if line[2] not in IO.charged_res[line[4]]:
self.original_charges[line[5]][line[6]] = 'HIP'
else:
self.original_charges[line[5]][line[6]] = \
IO.charged_res[line[4]][line[2]]
if write == True:
outfile.write(line_out + '\n')
elif self.origin == 'maestro':
with open(self.prot) as infile, \
open(self.prot[:-4] + '_noH.pdb', 'w') as outfile:
for line in infile:
if line.startswith(self.include):
line = IO.pdb_parse_in(line)
if line[2][0] != 'H':
outline = IO.pdb_parse_out(line)
outfile.write(outline + '\n')
# Get the charges from the hydrogen connections
if line[4] in IO.charged_res:
if line[2] in IO.charged_res[line[4]]:
if line[5] not in self.original_charges:
self.original_charges[line[5]] = {}
if line[2] not in IO.charged_res[line[4]]:
self.original_charges[line[5]][line[6]] = 'HIP'
else:
self.original_charges[line[5]][line[6]] = IO.charged_res[line[4]][line[2]]
def readpdb(self):
i = 0
chain_ref = None
if self.origin == 'gromacs':
pdbfile = self.prot[:-4] + '_noH.pdb'
elif self.origin == 'maestro':
pdbfile = self.prot[:-4] + '_noH.pdb'
with open(pdbfile) as infile:
for line in infile:
if line.startswith(self.include):
header = IO.pdb_parse_in(line)
RES_ref = header[6] - 1
break
for line in infile:
line = IO.pdb_parse_in(line)
if not line[5] in self.chains:
self.chains.append(line[5])
# construct chain based container
self.PDB[line[5]] = {}
with open(pdbfile) as infile:
if self.water == True:
self.PDB['w'] = {}
for line in infile:
if line.startswith(self.include):
line = IO.pdb_parse_in(line)
chain = line[5]
RES = line[6]
if chain in self.original_charges:
if line[6] in self.original_charges[chain]:
line[4] = self.original_charges[chain][line[6]]
if self.water == True:
if line[4] == 'HOH':
self.PDB['w'][line[1]] = line
else:
self.PDB[line[5]][line[1]] = line
elif self.water == False:
if line[4] != 'HOH':
self.PDB[line[5]][line[1]] = line
if RES != RES_ref:
RES_ref = RES
#if line[4].strip() == 'HOH':
# continue
if line[4].strip() == 'SOL':
continue
else:
# check chain ID
if line[5] not in self.log['PDB2Q']:
self.log['PDB2Q'][line[5]] = {}
if line[5] not in self.log['QRESN']:
self.log['QRESN'][line[5]] = {}
i += 1
self.log['PDB2Q'][line[5]][i] = line[6]
self.log['QRESN'][line[5]][line[6]] = i
def decharge(self):
charged_res = {'GLU':['GLH', 'CD', -1],
'ASP':['ASH', 'CG', -1],
'ARG':['ARN', 'CZ', 1],
'LYS':['LYN', 'NZ', 1],
'HIP':['HID', 'CG', 1]
}
coord1 = self.center
decharge = {}
# Distance for decharging residues in boundary
rest_bound = float(self.radius) - 3.0
for chain in self.PDB:
for key in self.PDB[chain]:
at = self.PDB[chain][key]
if at[4] in charged_res:
if at[2].strip() == charged_res[at[4]][1]:
coord2 = [float(at[8]),
float(at[9]),
float(at[10])
]
if f.euclidian_overlap(coord1, coord2, rest_bound) == False:
if not at[5] in decharge:
decharge[at[5]] = [at[6]]
else:
decharge[at[5]].append(at[6])
outline = '{:<10}{:<10}{:<10}{:<10}'.format(
self.log['QRESN'][chain][at[6]],
at[6],
chain,
at[4])
self.log['DECHARGE'].append(outline)
# Check if the decharged residue is part of a salt bridge and
# neutralize this residue as well
for chain in self.PDB:
for key in self.PDB[chain]:
at = self.PDB[chain][key]
if chain not in decharge:
continue
if at[6] in decharge[chain] and at[2].strip() == charged_res[at[4]][1]:
coord1 = [float(at[8]),
float(at[9]),
float(at[10])
]
for chain2 in self.PDB:
for key2 in self.PDB[chain2]:
at_2 = self.PDB[chain2][key2]
if at_2[4] in charged_res:
if at_2[2].strip() == charged_res[at_2[4]][1]:
coord2 = [float(at_2[8]),
float(at_2[9]),
float(at_2[10])
]
if at == at_2:
continue
if at_2[6] in decharge[chain]:
continue
if at_2[6] in decharge[chain2]:
continue
if f.euclidian_overlap(coord1, coord2, 4.0) == True:
decharge[chain2].append(at_2[6])
outline = '{:<10}{:<10}{:<10}{:<10}'.format(
self.log['QRESN'][chain2][at_2[6]],
at_2[6],
chain2,
at_2[4])
self.log['DECHARGE'].append(outline)
# Get the charged residues in the sphere and the total charge of these residues in the sphere
for chain in self.PDB:
for key in self.PDB[chain]:
at = self.PDB[chain][key]
if chain not in decharge:
continue
if at[6] in decharge[chain]:
at[4] = charged_res[at[4]][0]
continue
else: # at[4] in charged_res:
if at[4] in charged_res:
if at[2].strip() == charged_res[at[4]][1]:
self.log['CHARGE'].append('{:<10}{:<10}{:<10}{:<10}'.format(
self.log['QRESN'][chain][at[6]],
at[6],
chain,
at[4]))
self.log['TOTAL_CHARGE'] += charged_res[at[4]][2]
def set_OXT(self):
## NOTE WARNING, ETC: Q AA codes are sometimes 3, sometimes 4, they MUST be updated to pdb
## standards ASAP!!!
decharged = ['LYN','ARN','GLH','ASH']
CTERM = []
remove = []
for chain in self.PDB:
for key in self.PDB[chain]:
at = self.PDB[chain][key]
if at[2].strip() == 'OXT':
CTERM.append((at[6],at[5]))
self.log['CTERM'].append('{} {}'.format(at[6], at[4]))
if self.origin == 'gromacs':
if at[2].strip() == 'O1':
CTERM.append((at[6],at[5]))
self.log['CTERM'].append('{} {}'.format(at[6], at[4]))
for cterm in CTERM:
for key in self.PDB[chain]:
at = self.PDB[chain][key]
#HOTFIX, check if this is ok
if at[4] in decharged:
if at[2] == 'O1':
self.PDB[chain][key][2] = 'O'
if at[2] == 'O2':
remove.append([chain,key])
else:
if at[6] == cterm[0] and at[5] == cterm[1]:
self.PDB[chain][key][4] = 'C' + at[4]
if self.origin == 'gromacs':
if at[2] == 'O1':
self.PDB[chain][key][2] = 'O'
if at[2] == 'O2':
self.PDB[chain][key][2] = 'OXT'
# Remove atoms from c or n terminal decharged residues
for at in remove:
del self.PDB[at[0]][at[1]]
def get_CYX(self):
cys = []
cyx = []
cys_bond = 2.2
cys_mat = []
i = 0
k = -1
# Reduce coordinate array
for chain in self.PDB:
for key in self.PDB[chain]:
at = self.PDB[chain][key]
if at[4] == 'CYS' or at[4] == 'CYX' and at[2].strip() == 'SG':
cys.append([at[6], (at[8], at[9], at[10])])
# Construct S-S bond matrix
for SG_1 in cys:
cys_list = [SG_1[0]]
for SG_2 in cys:
cys_list.append(f.euclidian_overlap(SG_1[1], SG_2[1], cys_bond))
cys_mat.append(cys_list)
# Fix to better handling
try:
total = len(cys_mat[0]) -1
for line in cys_mat:
k += 1
for j in range(i, total):
if cys_mat[i][j+1] == True and cys_mat[k][0] != cys_mat[j][0]:
cyx.append(cys_mat[k][0])
cyx.append(cys_mat[j][0])
outline = '{:<10}{:<10}{:<10}{:<10}{:<10}{:<10}'.format(self.log['QRESN'][chain][cys_mat[k][0]],
self.log['QRESN'][chain][cys_mat[j][0]],
cys_mat[k][0],
chain,
cys_mat[j][0],
chain)
self.log['CYX'].append(outline)
i += 1
for chain in self.PDB:
for key in self.PDB[chain]:
at = self.PDB[chain][key]
if at[6] in cyx and at[4] == 'CYS':
self.PDB[chain][key][4] = 'CYX'
except:
return None
def write_tmpPDB(self):
with open(self.prot[:-4] + '_tmp.pdb', 'w') as outfile:
for chain in self.PDB:
atom_numbers = sorted(list(self.PDB[chain].keys()))
for atom in atom_numbers:
outline = IO.pdb_parse_out(self.PDB[chain][atom]) + '\n'
outfile.write(outline)
if len(self.PDB) != 1:
outfile.write('GAP\n')
def write_qprep(self):
replacements = {'FF_LIB' : s.FF_DIR + '/OPLS2015.lib',
'FF_PRM' : s.FF_DIR + '/OPLS2015.prm',
'PROTPDB' : self.prot[:-4] + '_tmp.pdb',
'CENTER' : self.log['CENTER'],
'SPHERE' : '{:.1f}'.format(self.radius),
'SOLVENT' : '1 HOH'
}
with open (s.INPUT_DIR + '/qprep_protprep.inp') as infile, \
open ('qprep.inp', 'w') as outfile:
for line in infile:
line = IO.replace(line, replacements)
outfile.write(line)
if line[0:8] == '!addbond':
for line in self.log['CYX']:
line = line.split()
outline = 'addbond {}:SG {}:SG y\n'.format(*line)
outfile.write(outline)
def run_qprep(self):
qprep = s.LOCAL['QPREP']
options = ' < qprep.inp > qprep.out'
# Somehow Q is very annoying with this < > input style so had to implement
# another function that just calls os.system instead of using the preferred
# subprocess module....
IO.run_command(qprep, options, string = True)
##### THIS PART COMES AFTER THE TEMP .pdb IS WRITTEN ######
def write_pdb_out(self):
waters ={'HOH': ['O', 'H1', 'H2'],
'SOL': ['OW1', 'HW1', 'HW2']
}
waters_tokeep = []
with open('top_p.pdb') as infile:
for line in infile:
if line.startswith(self.include):
line = IO.pdb_parse_in(line)
if line[4].strip() in waters and \
line[2].strip() == waters[line[4].strip()][0]:
coord1 = self.center
coord2 = [float(line[8]),
float(line[9]),
float(line[10])
]
if f.euclidian_overlap(coord1, coord2, self.radius) == True:
waters_tokeep.append(line[6])
with open('top_p.pdb') as infile, \
open('water.pdb', 'w') as watout, \
open('protein.pdb', 'w') as protout:
for line in infile:
if line.startswith(self.include):
line = IO.pdb_parse_in(line)
if line[6] in waters_tokeep:
outline = IO.pdb_parse_out(line) + '\n'
watout.write(outline)
if line[4] not in waters:
outline = IO.pdb_parse_out(line) + '\n'
protout.write(outline)
if line[0:3] == 'TER' or line == 'GAP':
protout.write(line)
def write_log(self):
with open('protPREP.log', 'w') as outfile:
outfile.write('--------------------------------------------------------------------\n')
outfile.write('This file contains some output information regarding the preperation\n')
outfile.write('process of the protein for spherical boundary conditions in Q.\n\n')
outfile.write('The command line input was:\n')
outfile.write('{}\n\n'.format(self.log['INPUT']))
outfile.write('{:47}{:>21}\n'.format('Date:',
self.log['TIME']))
outfile.write('{:47}{:>21}\n'.format('Inputfile:',
self.prot))
outfile.write('{:41}{:>9.3f}{:>9.3f}{:>9.3f}\n'.format('Sphere center:',
self.center[0],
self.center[1],
self.center[2]))
outfile.write('{:47}{:>21.1f}\n'.format('Sphere radius:',
self.radius))
outfile.write('{:47}{:>21}\n'.format('Total charge in sphere:',
self.log['TOTAL_CHARGE']))
outfile.write('--------------------------------------------------------------------')
outfile.write('\nThe following residues have been decharged:\n')
outfile.write('{:10}{:10}{:10}{:10}\n'.format('Q_RESN', 'PDB_IN', 'CHAIN','RESNAME'))
for line in self.log['DECHARGE']:
outfile.write(line + '\n')
outfile.write('--------------------------------------------------------------------')
outfile.write('\nThe following charged residues are in the sphere:\n')
outfile.write('{:10}{:10}{:10}{:10}\n'.format('Q_RESN', 'PDB_IN', 'CHAIN','RESNAME'))
for line in self.log['CHARGE']:
outfile.write(line + '\n')
outfile.write('--------------------------------------------------------------------')
outfile.write('\n')
outfile.write('The following S-S bonds have been found:\n')
outfile.write('{:10}{:10}{:10}{:10}{:10}{:10}\n'.format(
'Q_CYS1', 'Q_CYS2', 'PDB_CYS1', 'CHAIN','PDB_CYS2','CHAIN'))
for line in self.log['CYX']:
outfile.write(line + '\n')
outfile.write('--------------------------------------------------------------------')
outfile.write('\n')
outfile.write('The following is a mapping of residue numbers in Q and the input \n')
outfile.write('pdbfile "{}":\n'.format(self.prot))
outfile.write('{:10}{:10}{:10}{:10}\n'.format('Q_RESN', 'PDB_IN', 'CHAIN','RESNAME'))
for chain in self.PDB:
for key in self.PDB[chain]:
if self.PDB[chain][key][2].strip() == 'CB':
PDB_resi = self.PDB[chain][key][6]
Q_resi = self.log['QRESN'][chain][PDB_resi]
resn = self.PDB[chain][key][4]
outfile.write('{:<10}{:<10}{:10}{:10}\n'.format(Q_resi,PDB_resi,chain,resn))
def cleanup(self):
if self.noclean == False:
os.remove(self.prot[:-4] + '_tmp.pdb')
os.remove(self.prot[:-4] + '_noH.pdb')
os.remove('qprep.inp')
os.remove('qprep.out')
os.remove('top_p.pdb')
os.remove('complexnotexcluded.pdb')
os.remove('tmp.top')
if __name__ == "__main__":
parser = argparse.ArgumentParser(
prog='protPREP',
formatter_class=argparse.RawDescriptionHelpFormatter,
description = ' == Generate FEP files for dual topology ligFEP == ')
parser.add_argument('-p', '--prot',
dest = "prot",
required = True,
help = "protein pdb file")
parser.add_argument('-r', '--sphereradius',
dest = "sphereradius",
required = True,
help = "radius of the sphere")
parser.add_argument('-w', '--nowater',
dest = "water",
required = False,
default = True,
action = 'store_false',
help = "set no water if crystal waters are NOT to be retained")
parser.add_argument('-c', '--spherecenter',
dest = "spherecenter",
required = True,
help = "center of the sphere, can be residue number (RESN:$)," \
"atomnumber (ATN:$) or explicit coordinates (X:Y:Z)")
parser.add_argument('--noclean',
dest = "noclean",
default = False,
action = 'store_true',
help = "If turned on, intermediate Q files will not be deleted")
parser.add_argument('-P', '--preplocation',
dest = "preplocation",
default = 'LOCAL',
help = "define this variable if you are setting up your system elsewhere")
parser.add_argument('-O', '--origin',
dest = "origin",
default = 'maestro',
choices = ['maestro', 'gromacs'],
help = "Use this flag to specficy with which program the .pdb file was written")
args = parser.parse_args()
run = Run(prot = args.prot,
sphereradius = args.sphereradius,
spherecenter = args.spherecenter,
water = args.water,
noclean = args.noclean,
preplocation = args.preplocation,
origin = args.origin,
include = ('ATOM','HETATM')
)
run.get_center_coordinates() # 00
run.prepwizard_parse() # 01
run.readpdb() # 02
run.decharge() # 03
run.set_OXT() # 04
run.get_CYX() # 05
run.write_tmpPDB() # 06
run.write_qprep() # 07
run.run_qprep() # 08
run.write_pdb_out() # 09
run.write_log() # 10
run.cleanup() # 11