-
Notifications
You must be signed in to change notification settings - Fork 1
/
NbX_feature_prep.py
348 lines (236 loc) · 12.9 KB
/
NbX_feature_prep.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
# Import library
from pyrosetta import *
import os
import glob
import pandas as pd
import re
from pyrosetta.rosetta.protocols.rosetta_scripts import *
from pyrosetta.rosetta.protocols.antibody import *
from pyrosetta.rosetta.protocols.antibody.design import *
from pyrosetta.rosetta.utility import *
from pyrosetta.rosetta.protocols.analysis import InterfaceAnalyzerMover
from biopandas.pdb import PandasPdb
from argparse import ArgumentParser
# Init
init()
parser = ArgumentParser()
parser.add_argument('--antigen_chain', type=str)
parser.add_argument('--antibody_chain', type=str)
parser.add_argument('--native', type=str)
args = parser.parse_args()
### Define path to python
path_to_python = "/home/cltam/anaconda3/envs/nbx/bin/python"
### Define path to DockQ
path_to_dockq = "/data/cltam/script/DockQ/"
### Define correct pose .pdb name
if args.native != None:
correct_pose_pdb = args.native
### Define path to FoldX
path_to_foldx = "/data/cltam/script/FoldX/foldx_20221231"
### Define antigen chain and antibody chain
antigen_chain = args.antigen_chain
antibody_chain = args.antibody_chain
### Define desired epitope to be targeted (i.e. positive epitope)
epitope_positive_start_residue = 9998
epitope_positive_end_residue = 9999
### Define epitope to be avoided (i.e. negative epitope)
epitope_negative_start_residue = 9998
epitope_negative_end_residue = 9999
### Define CDR start residue and end residue
CDR1_start_residue = 24
CDR1_end_residue = 42
CDR2_start_residue = 57
CDR2_end_residue = 69
CDR3_start_residue = 107
CDR3_end_residue = 138
### Define a function that create a list with chain and resdiue numbers
def create_chain_res_list(chain, start_res, end_res):
empty_list = []
for i in range(start_res, end_res+1):
empty_list.append(chain+str(i))
return empty_list
### Define a function that extract records from a dataframe according to match in a certain column
def extract_record(df, col_name, match):
return df.loc[df[col_name] == match]
### Define a function that multiplies residue count with aadescriptors and sum the product for 20 aa
def multiply_residue_count_with_aadescriptor_sum(residue_count_df, aa_descriptor_df, chain):
result = pd.DataFrame(0, index = range(1), columns=aa_descriptor_df.columns)
for i in range(residue_count_df.shape[1]):
multiply_df = pd.DataFrame(residue_count_df.iloc[:,i:i+1].values * aa_descriptor_df.iloc[i:i+1,:].values)
result = pd.DataFrame(result.values + multiply_df.values)
new_col_name = []
for elem2 in aa_descriptor_df.iloc[i:i+1,:].columns:
new_col_name.append("chain" + chain + "_" + elem2)
result.columns = new_col_name
return result
### Create lists containing every residues of every CDRs for counting
CDR1 = create_chain_res_list(antibody_chain,CDR1_start_residue,CDR1_end_residue)
CDR2 = create_chain_res_list(antibody_chain,CDR2_start_residue,CDR2_end_residue)
CDR3 = create_chain_res_list(antibody_chain,CDR3_start_residue,CDR3_end_residue)
epitope_positive = create_chain_res_list(antigen_chain,epitope_positive_start_residue,epitope_positive_end_residue)
epitope_negative = create_chain_res_list(antigen_chain,epitope_negative_start_residue,epitope_negative_end_residue)
### Create lists containing wild card for antigen and antibody to count all interacting residues
antigen_chain_wildcard = re.compile("." + antigen_chain + ".+")
antibody_chain_wildcard = re.compile("." + antibody_chain + ".+")
### Define residue list for counting individual residue
residue_list = ['G','A','V','P','I','L','F','W','S','T','C','E','D','Q','N','H','K','R','Y','M']
residue_list.sort()
### Create empty dataframe to collect all features from all .pdb
all_pdb_summary_df = pd.DataFrame()
### Collect all .pdb paths in the current directory into a list, sort the list
filepath_list = []
for filepath in glob.iglob('./*.pdb'):
filepath_list.append(filepath)
filepath_list.sort()
### Loop through all *.pdb in current directory
for filepath in filepath_list:
# try:
### Grep ATOM
os.system(str("grep 'ATOM' " + filepath + " > " + filepath[2:-4] + "_grep-ATOM.pdb"))
### Calculate DockQ score
if args.native != None:
os.system(str(path_to_python + ' ' + path_to_dockq + "DockQ.py" + ' ' + filepath[2:-4] + "_grep-ATOM.pdb" + ' ' + correct_pose_pdb + ' > ' + filepath[2:-4] + "_grep-ATOM.pdb" + '.DockQ'))
filein = open(filepath[2:-4] + "_grep-ATOM.pdb" + '.DockQ', 'r', encoding='utf-8')
readline = filein.readlines()
dockq = []
for i in readline[-5:]:
dockq.append(i.replace('\n',''))
dockq_df = pd.DataFrame([sub.split(" ") for sub in dockq])
dockq_df = dockq_df.set_index(dockq_df.columns[0])
dockq_df = dockq_df.iloc[:,0:1].T
dockq_df.reset_index(drop=True, inplace=True)
# Import a pose to PyRosetta
pose = pose_from_pdb(filepath[2:-4] + "_grep-ATOM.pdb")
# Run PyRosetta InterfaceAnalyzerMover
iam = InterfaceAnalyzerMover()
iam.set_pack_separated(True)
iam.apply(pose)
score = pose.scores
score_df = pd.DataFrame.from_dict(score, orient='index')
score_df_transposed = score_df.T
score_df_transposed = score_df_transposed.iloc[:,:20]
### Run FoldX AnalyzeComplex
os.system(str(path_to_foldx + " --command=AnalyseComplex --pdb=" + filepath[2:-4] + "_grep-ATOM.pdb" + " --analyseComplexChains=A,H"))
### Collect results from FoldX AnalyzeComplex
filein = open('Indiv_energies_' + filepath[2:-4] + "_grep-ATOM" + '_AC.fxout', 'r', encoding='utf-8')
readline = filein.readlines()
energy1 = readline[-1].replace('\n','')
energy1_split = energy1.split('\t')
energy2 = readline[-2].replace('\n','')
energy2_split = energy2.split('\t')
energy1_df = pd.DataFrame([energy1_split])
energy2_df = pd.DataFrame([energy2_split])
group1 = energy1_split[1]
group2 = energy2_split[1]
header = readline[-3].replace('\n','')
header_split = header.split('\t')
header_split_concat1 = ['chain' + group1 + ' - ' + elem for elem in header_split]
header_split_concat2 = ['chain' + group2 + ' - ' + elem for elem in header_split]
energy1_df.columns = header_split_concat1
energy2_df.columns = header_split_concat2
FoldX_AnalyzeComplex_energy_df = pd.concat([energy2_df.iloc[:,1:], energy1_df.iloc[:,1:]], axis = 1)
FoldX_AnalyzeComplex_energy_df.pop('chainH - Group')
FoldX_AnalyzeComplex_energy_df.pop('chainA - Group')
### Read interface residue output from FoldX AnalyzeComplex
filein = open('Interface_Residues_' + filepath[2:-4] + "_grep-ATOM" + '_AC.fxout', 'r', encoding='utf-8')
readline = filein.readlines()
interface_residue = readline[-1].replace('\n','').split('\t')
### Count interacting residues in 1) CDR1, 2) CDR2, 3) CDR3, 4) positive epitope, 5) negative epitope and 6) paratope
interface_residue_reduced = [i[1:] for i in interface_residue]
CDR1_count = len(set(interface_residue_reduced).intersection(CDR1))
CDR2_count = len(set(interface_residue_reduced).intersection(CDR2))
CDR3_count = len(set(interface_residue_reduced).intersection(CDR3))
epitope_positive_count = len(set(interface_residue_reduced).intersection(epitope_positive))
epitope_negative_count = len(set(interface_residue_reduced).intersection(epitope_negative))
antigen_count = len(list(filter(antigen_chain_wildcard.match,interface_residue)))
antibody_count = len(list(filter(antibody_chain_wildcard.match,interface_residue)))
### convert .pdb to dataframe by biopandas
ppdb = PandasPdb()
ppdb.read_pdb(filepath[2:-4] + "_grep-ATOM.pdb")
pdb_df = extract_record(ppdb.df['ATOM'], 'chain_id', antibody_chain)
### count the number of residues
residue_set = set()
for i in pdb_df['residue_number']:
residue_set.add(i)
### count full length of CDR1, CDR2 and CDR3
CDR1_full_list = []
CDR2_full_list = []
CDR3_full_list = []
for i in residue_set:
if i >= CDR1_start_residue and i <= CDR1_end_residue:
CDR1_full_list.append(i)
CDR1_full_number = len(CDR1_full_list)
for i in residue_set:
if i >= CDR2_start_residue and i <= CDR2_end_residue:
CDR2_full_list.append(i)
CDR2_full_number = len(CDR2_full_list)
for i in residue_set:
if i >= CDR3_start_residue and i <= CDR3_end_residue:
CDR3_full_list.append(i)
CDR3_full_number = len(CDR3_full_list)
### Create a dataframe to collect all the epitope and CDR count results
epitope_CDR_count_df = pd.DataFrame(
{
'epitope_positive_count' : epitope_positive_count,
'epitope_negative_count' : epitope_negative_count,
'CDR1_count': CDR1_count,
'CDR2_count': CDR2_count,
'CDR3_count': CDR3_count,
'antigen_total_count' : antigen_count,
'antibody_total_count' : antibody_count,
'CDR1_FL' : int(CDR1_full_number),
'CDR2_FL' : int(CDR2_full_number),
'CDR3_FL' : int(CDR3_full_number)
}, index = [0])
epitope_CDR_count_df.reset_index(drop=True, inplace=True)
### Calculate ratio over total counts
epitope_CDR_count_df['epitope_positive_count/antigen_total'] = epitope_CDR_count_df['epitope_positive_count']/epitope_CDR_count_df['antigen_total_count']
epitope_CDR_count_df['epitope_negative_count/antigen_total'] = epitope_CDR_count_df['epitope_negative_count']/epitope_CDR_count_df['antigen_total_count']
epitope_CDR_count_df['CDRs_count/antibody_total'] = (epitope_CDR_count_df['CDR1_count'] + epitope_CDR_count_df['CDR2_count'] + epitope_CDR_count_df['CDR3_count']) /epitope_CDR_count_df['antibody_total_count']
epitope_CDR_count_df['CDR1_count/CDR1_FL'] = epitope_CDR_count_df['CDR1_count']/epitope_CDR_count_df['CDR1_FL']
epitope_CDR_count_df['CDR2_count/CDR2_FL'] = epitope_CDR_count_df['CDR2_count']/epitope_CDR_count_df['CDR2_FL']
epitope_CDR_count_df['CDR3_count/CDR3_FL'] = epitope_CDR_count_df['CDR3_count']/epitope_CDR_count_df['CDR3_FL']
antigen_individual_residue_count_df = pd.DataFrame()
for i in residue_list:
r = re.compile(i + antigen_chain + ".+")
residue_count = len(list(filter(r.match,interface_residue)))
col_name = 'chain' + antigen_chain + '_' + i
antigen_individual_residue_count_df[col_name] = [residue_count]
antibody_individual_residue_count_df = pd.DataFrame()
for i in residue_list:
r = re.compile(i + antibody_chain + ".+")
residue_count = len(list(filter(r.match,interface_residue)))
col_name = 'chain' + antibody_chain + '_' + i
antibody_individual_residue_count_df[col_name] = [residue_count]
### Multiply the individual interface residue counts with aadescriptors
aa_descriptor = pd.read_csv('aaDescriptors.csv', index_col=0)
antigen_aadescriptor_df = multiply_residue_count_with_aadescriptor_sum(antigen_individual_residue_count_df, aa_descriptor, antigen_chain)
antibody_aadescriptor_df = multiply_residue_count_with_aadescriptor_sum(antibody_individual_residue_count_df, aa_descriptor, antibody_chain)
if args.native != None:
each_pdb_summary_df = pd.concat([dockq_df,
epitope_CDR_count_df,
antigen_individual_residue_count_df,
antibody_individual_residue_count_df,
score_df_transposed,
FoldX_AnalyzeComplex_energy_df,
antigen_aadescriptor_df,
antibody_aadescriptor_df],
axis = 1)
else:
each_pdb_summary_df = pd.concat([epitope_CDR_count_df,
antigen_individual_residue_count_df,
antibody_individual_residue_count_df,
score_df_transposed,
FoldX_AnalyzeComplex_energy_df,
antigen_aadescriptor_df,
antibody_aadescriptor_df],
axis = 1)
each_pdb_summary_df.insert(0, 'PDB', filepath[2:-4])
# Append results to the empty dataframe
all_pdb_summary_df = all_pdb_summary_df.append(each_pdb_summary_df)
# Delete files from FoldX AnalyzeComplex
os.system(str("rm -rf *" + filepath[2:-4] + "_grep-ATOM*"))
# except:
# os.system(str("rm -rf *" + filepath[2:-4] + "_grep-ATOM*"))
### Save the summary feature dataframe to a .csv
all_pdb_summary_df.to_csv('NbX_feature.csv', index=False)