-
Notifications
You must be signed in to change notification settings - Fork 0
/
extract_sdf_from_dlg.py
139 lines (116 loc) · 3.21 KB
/
extract_sdf_from_dlg.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
#!/usr/bin/env python3
from rdkit import Chem
import re
import sys
import os
FEATURES = [
'version 0.1.0 : Start',
'version 0.2.0 : deal with error occured on `Chem.MolFromPDBBlock`',
]
VERSION = FEATURES[-1].split(':')[0].replace('version',' ').strip()
Usage = """
Usage: AutoDock dock-dlg file process:
./this.script.py dock.dlg
"""
if len(sys.argv) != 2:
print(Usage)
exit()
File = sys.argv[1]
if os.path.splitext(File)[1].lower() != '.dlg':
print(Usage)
print('Fatal: only dlg file is supported')
exit()
Pat_origin = re.compile(r'INPUT-LIGAND-PDBQT: (.+)(?:\n|\r\n?)')
Pat_models = re.compile(r'DOCKED: MODEL.*?DOCKED: ENDMDL',re.DOTALL)
Autodock_atom_types = {
'H' : ['H','HD','HS'],
'C' : ['C','A'],
'N' : ['N','NA','NS'],
'O' : ['O','OA','OS'],
'F' : ['F'],
'Mg': ['Mg','MG'],
'P' : ['P'],
'S' : ['S','SA'],
'Cl': ['Cl','CL'],
'Br': ['Br','BR'],
'Ca': ['Ca','CA'],
'Mn': ['Mn','MN'],
'Fe': ['Fe','FE'],
'Zn': ['Zn','ZN'],
'I' : ['I'],
}
Contents = None
with open(File,'rt') as f:
Contents = f.read()
if not Contents:
print('Fatal: File Contents: no inputs')
exit()
# 01234567890123456789012345678901234567890123456789012345678901234567890123456789
#'HETATM 1 C1 AQ4 A 999 29.552 -1.989 53.496 1.00 82.82 0.018 C '
def func_pdbqt2pdb(lines):
prolines = []
ebinding = []
for l in lines:
if 'Free Energy of Binding' in l:
g = l.split()
if len(g) > 8:
try:
v = float(g[7])
except ValueError:
pass
else:
ebinding.append(v)
if len(l) >= 78 and l[:4] in ['HETA','ATOM']:
at = l[77:79].strip()
ra = None
for k,v in Autodock_atom_types.items():
if at in v:
ra = k
break
if not ra:
print(f'Error: Atomtype not defined: {l}')
continue
new = l[0:12] + ' {:2} '.format(ra) + l[16:54] + ' '*26
prolines.append(new)
if not ebinding:
ebinding = [0.0, ]
elif len(ebinding) > 1:
print('Warning: multiple binding values')
return prolines,min(ebinding)
full = []
titles = []
origin = Pat_origin.findall(Contents)
if not origin:
print('Fatal: no found original structure')
exit()
oripdb,e = func_pdbqt2pdb(origin)
if not oripdb:
print('Error: no found original structure')
full.append(oripdb)
titles.append('Original')
models = Pat_models.findall(Contents)
if not models:
print('Fatal: No models found')
exit()
for m in models:
m = m.replace('DOCKED: ','').split('\n')
t,e = func_pdbqt2pdb(m)
if not t:
print('Error: error on found model')
continue
full.append(t)
titles.append('BindingEnergy: {:}'.format(e))
base = 'out-' + os.path.splitext(File)[0] + '.sdf'
w = Chem.SDWriter(base)
for m,t in zip(full,titles):
mol = Chem.MolFromPDBBlock('\n'.join(m))
if not mol:
print('\nError:')
for i in m:
print(i)
print('\n')
continue
mol.SetProp('_Name',t)
w.write(mol)
w.close()
print('DONE')