-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdocking_preparation.py
283 lines (237 loc) · 11.8 KB
/
docking_preparation.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
#!/usr/bin/env python
'''
Generate binding sites by doing an MCS alignment of project data to available crystal structures.
'''
########################################################################################################################
import argparse
import pandas as pd
from ccdc import io, protein, entry
from pathlib import Path
from rdkit import Chem
from ccdc_roche.python.los_descriptors import _cut_out_binding_site_by_distance
########################################################################################################################
def parse_args():
"""Define and parse the arguments to the script."""
parser = argparse.ArgumentParser(
description='Execute Line of sight contact scripts.',
formatter_class=argparse.ArgumentDefaultsHelpFormatter # To display default values in help message.
)
parser.add_argument(
'-t',
'--target',
help='Target name.',
default='pde-10'
)
return parser.parse_args()
def _get_alignment_templates(target='pde-10'):
structure_quality = pd.read_csv(f'{target}_structure_quality.csv')
high_quality_strucs = structure_quality[(structure_quality['RESOLUTION'] <= 2.5) &
(structure_quality['DENSITYFLAGS'] <= 5) &
(structure_quality['LIGCLASHES'] == 0) &
(structure_quality['LIGSYMCONTACT'] == 0)]['STRUCID'].to_list()
assayed_structures = list((Path('') / Path(target) / Path('tmp_2d_sdf')).glob('*.sdf'))
ligand3d_sdf = list((Path('') / Path(target) / Path('tmp_aligned_3d_sdf')).glob('*.sdf'))
with io.EntryWriter('ligand_templates_for_mcs.sdf') as wri:
for assayed_sdf in assayed_structures:
pdb_id = assayed_sdf.stem.split('_')[0]
if pdb_id not in high_quality_strucs:
continue
if len(pdb_id) == 4:
ligand3D = [f for f in ligand3d_sdf if pdb_id in str(f.stem)][0]
else:
with io.EntryReader(str(assayed_sdf)) as rdr:
e = rdr[0]
ligname = e.identifier.split('-')[0]
ligand3D = [f for f in ligand3d_sdf if ligname in str(f)]
if len(ligand3D) < 1:
continue
else:
ligand3D = ligand3D[0]
with io.EntryReader(str(ligand3D)) as rdr:
for e in rdr:
e.attributes['$File'] = str(ligand3D.resolve())
e.attributes['STRUCID'] = pdb_id
wri.write(e)
def _get_pdb_ligname_df(target='pde-10'):
pdb_files = list((Path('') / Path(target) / Path('tmp_aligned_for_MOE')).glob('*.pdb'))
sdf_files = list((Path('') / Path(target) / Path('tmp_2d_sdf')).glob('*.sdf'))
ligand3d_sdf = list(
(Path('') / Path(target) / Path('tmp_aligned_3d_sdf')).glob('*.sdf'))
structure_quality = Path(f'{target}_structure_quality.csv')
high_quality_strucs = []
if structure_quality.is_file():
structure_quality = pd.read_csv(f'{target}_structure_quality.csv')
high_quality_strucs = structure_quality[(structure_quality['RESOLUTION'] <= 2.5) &
(structure_quality['DENSITYFLAGS'] <= 5) &
(structure_quality['LIGCLASHES'] == 0) &
(structure_quality['LIGSYMCONTACT'] == 0)]['STRUCID'].to_list()
structure_dicts = []
for sdf_file in sdf_files:
strucid = sdf_file.stem.split('_')[0]
with io.EntryReader(str(sdf_file)) as rdr:
full_lname = rdr[0].identifier
if full_lname.startswith('RO'):
lname = full_lname.split('-')[0]
else:
lname = full_lname
pdb_file = [f for f in pdb_files if f.stem.startswith(strucid)][0]
template_file = None
for f in ligand3d_sdf:
if f.stem.startswith('RO') and f.stem.startswith(lname):
template_file = f
break
elif not f.stem.startswith('RO') and f.stem.startswith(strucid):
template_file = f
break
structure_dicts.append({'ligand': lname, 'full_ligand_name': full_lname, 'pdb_file': str(pdb_file),
'template_file': template_file, 'strucid': strucid})
df = pd.DataFrame(structure_dicts)
if high_quality_strucs:
df['is_high_quality'] = df['strucid'].isin(high_quality_strucs)
else:
df['is_high_quality'] = True
df.to_csv('pdb_ligand.csv', index=False)
def _sanitize_pdb_files(target='pde-10'):
pdb_files = list((Path('/rf_scoring') / Path(target) / Path('tmp_aligned_for_MOE')).glob('*.pdb'))
sanitized_path = Path('/rf_scoring') / Path(target) / Path('tmp_aligned_for_MOE_sanitized')
structure_quality = pd.read_csv(f'{target}_structure_quality.csv')
high_quality_strucs = structure_quality[(structure_quality['RESOLUTION'] <= 2.5) &
(structure_quality['DENSITYFLAGS'] <= 5) &
(structure_quality['LIGCLASHES'] == 0) &
(structure_quality['LIGSYMCONTACT'] == 0)
]['STRUCID'].to_list()
if not sanitized_path.is_dir():
sanitized_path.mkdir()
for pdb_file in pdb_files:
pdb_id = pdb_file.stem.split('_')[0].split('.')[0]
if pdb_id not in high_quality_strucs:
continue
m = Chem.MolFromPDBFile(str(pdb_file), removeHs=False)
if not m:
print('File cannot be read by RDKit: ', pdb_file)
with io.EntryReader(str(pdb_file)) as rdr:
p = protein.Protein.from_entry(rdr[0])
p.remove_unknown_atoms()
p.kekulize()
p.standardise_aromatic_bonds()
for lig in p.ligands:
if 'PO4' in lig.identifier:
p.remove_ligand(lig.identifier)
m = Chem.MolFromMol2Block(p.to_string('mol2'))
sanitized_file = sanitized_path / Path(pdb_file.name.replace('.pdb', '_sanitized.pdb'))
with open(sanitized_file, 'w') as outfile:
writer = Chem.rdmolfiles.PDBWriter(outfile)
writer.write(m)
writer.close()
def _get_binding_site_residues(target='pde-10'):
sanitized_path = Path('/rf_scoring') / Path(target) / Path(
'tmp_aligned_for_MOE_sanitized')
structure_quality = pd.read_csv(f'{target}_structure_quality.csv')
high_quality_strucs = structure_quality[(structure_quality['RESOLUTION'] <= 2.5) &
(structure_quality['DENSITYFLAGS'] <= 5) &
(structure_quality['LIGCLASHES'] == 0) &
(structure_quality['LIGSYMCONTACT'] == 0)]
binding_site_residues = []
strucids = []
for index, row in high_quality_strucs.iterrows():
strucid = row['STRUCID']
pdb_file = list(Path(sanitized_path).glob(f'*{strucid}*_sanitized.pdb'))
if pdb_file:
pdb_file = str(pdb_file[0])
ligname = row['HETMOLRES'][:3]
with io.EntryReader(pdb_file) as rdr:
for e in rdr:
p = protein.Protein.from_entry(e)
ligands = [c for c in p.components if c.atoms[-1].residue_label[:3] == ligname]
if not ligands:
continue
if len(ligands) != 1:
continue
else:
ligand = ligands[0]
if not 9 < len(ligand.atoms) < 500: # kick out fragments and covalently bound ligands
continue
for lig in p.ligands:
p.remove_ligand(lig.identifier)
for a in p.atoms:
if a.residue_label[:3] == ligname:
p.remove_atoms([a])
p = _cut_out_binding_site_by_distance(p, ligand)
for residue in p.residues:
binding_site_residues.append(residue.identifier)
strucids.append(strucid)
binding_site = pd.DataFrame()
binding_site['residue_identifier'] = binding_site_residues
binding_site['strucid'] = strucids
binding_site = binding_site.drop_duplicates('residue_identifier')
binding_site = binding_site['residue_identifier'].str.split(':', expand=True).drop_duplicates(subset=1).rename(
columns={0: 'chain_label', 1: 'residue_identifier'})
binding_site.to_csv('binding_site_residues.csv', index=False)
def _export_ligands(target='pde-10'):
protein_out = Path('tmp_aligned_for_MOE_sanitized')
ligand_out = Path('tmp_aligned_3d_sdf_sanitized/single_files')
if not protein_out.is_dir():
protein_out.mkdir()
pdb_ligand_df = pd.read_csv('pdb_ligand.csv')
# ligand_ids = list(pdb_ligand_df['ligand'].unique())
protein_files = Path('final_pdb_files').glob('*.pdb')
for cnt, protein_file in enumerate(protein_files):
protein_file_str = str(protein_file)
strucid = protein_file.stem
# if strucid not in ['1jnhn'] and cnt != 0:
# continue
ligand_name = pdb_ligand_df[pdb_ligand_df['strucid'] == strucid]['ligand'].values[0]
ccdc_protein = protein.Protein.from_file(protein_file_str)
ligands = []
for lig in ccdc_protein.ligands:
for id in ['LIG', 'L0R', 'UNL', '5M9']:
if id in lig.identifier:
ligands.append(lig)
ligand = ligands[0].components[0].copy()
print('removing...')
for r in ccdc_protein.residues:
if r.chain_identifier != 'A' and r.atoms[0].protein_atom_type == 'Amino_acid':
ccdc_protein.remove_residue(r.identifier)
for lig in ccdc_protein.ligands:
ccdc_protein.remove_ligand(lig.identifier)
ccdc_protein.add_ligand(ligand)
ccdc_protein.remove_unknown_atoms()
ccdc_protein.remove_all_metals()
ccdc_protein.identifier = strucid
# if cnt == 0:
# template_protein_chain = ccdc_protein.copy().chains[0]
#
# else:
# transformed_protein = ccdc_protein.copy()
# transformation_matrix = protein.Protein.ChainSuperposition().superpose(template_protein_chain, transformed_protein.chains[0])[1]
# for cnt, a in enumerate(transformed_protein.atoms):
# a.displacement_parameters = ccdc_protein.atoms[cnt].displacement_parameters
# ligand.transform(transformation_matrix)
print('writing...')
with io.MoleculeWriter(protein_out / protein_file.name) as w:
w.write(ccdc_protein)
ligand_entry = entry.Entry.from_molecule(ligand)
ligand_entry.attributes['strucid'] = strucid
ligand_entry.attributes['SRN'] = ligand_name
ligand_entry.identifier = ligand_name
ligands.append(ligand_entry)
if not ligand_out.is_dir():
ligand_out.mkdir()
with io.EntryWriter(ligand_out / f'{strucid}_{ligand_name}.sdf') as w:
w.write(ligand_entry)
return
def main():
# args = parse_args()
parse_args()
_export_ligands()
# print('Getting alignment templates...')
# _get_alignment_templates(args.target)
# print('Assigning ligand names to PDB files...')
# _get_pdb_ligname_df(args.target)
# print('Sanitizing PDB files...')
# _sanitize_pdb_files(args.target)
# print('Getting bindingsite residues...')
# _get_binding_site_residues(args.target)
print('finished')
if __name__ == '__main__':
main()