-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathgenerate_json.py
More file actions
388 lines (315 loc) · 14 KB
/
generate_json.py
File metadata and controls
388 lines (315 loc) · 14 KB
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
import re
import sys
import argparse
import math
from typing import Optional, Sequence
import pprint
sys.path.append("/proj/kuhl_lab/evopro2/")
#from evopro2.user_inputs.inputs import FileArgumentParser
from objects.chemical import to1letter, to3letter, alphabet
from utils.parsing_utils import get_coordinates_pdb_extended
def get_coordinates_pdb_extended(pdb, fil = False):
chains = []
residues = {}
residueindices = {}
if fil:
with open(pdb,"r") as f:
pdb_split = f.read().split("\n")
else:
pdb_split = pdb.split("\n")
i=0
pdb_split = [x for x in pdb_split if x]
for lin in pdb_split:
x = lin[30:38].strip(' ')
y = lin[38:46].strip(' ')
z = lin[46:54].strip(' ')
l = lin.strip().split()
if l and ('ATOM' in l[0] or 'HETATM' in l[0]):
resid = lin[21] + '_' + lin[17:20].strip() + "_" + lin[22:26].strip()
atominfo = (l[1], l[2], (x, y, z))
if lin[21] not in chains:
chains.append(lin[21])
if resid not in residues:
residues[resid] = [atominfo]
else:
residues[resid].append(atominfo)
if resid not in residueindices:
residueindices[resid] = i
i = i+1
return chains, residues, residueindices
class FileArgumentParser(argparse.ArgumentParser):
"""Overwrites default ArgumentParser to better handle flag files."""
def convert_arg_line_to_args(self, arg_line: str) -> Optional[Sequence[str]]:
""" Read from files where each line contains a flag and its value, e.g.
'--flag value'. Also safely ignores comments denoted with '#' and
empty lines.
"""
# Remove any comments from the line
arg_line = arg_line.split('#')[0]
# Escape if the line is empty
if not arg_line:
return None
# Separate flag and values
split_line = arg_line.strip().split(' ')
# If there is actually a value, return the flag value pair
if len(split_line) > 1:
return [split_line[0], ' '.join(split_line[1:])]
# Return just flag if there is no value
else:
return split_line
import math
def find_all(a_str, sub):
start = 0
while True:
start = a_str.find(sub, start)
if start == -1: return
yield start
start += len(sub)
#TODO: add support for parsing SMILES strings/other small mol format
def parse_seqfile(filename):
ids = {}
chains = {}
with open(filename, "r") as f:
i=0
for lin in f:
l = lin.strip().split(":")
#dict chainid: [type, sequence]
chains[l[0]] = [l[1],l[2]]
for chain in chains:
reslist = list(chains[chain][-1])
for aa, i in zip(reslist, range(len(reslist))):
resid = chain + str(i+1)
extended_resid = chain + "_" + to3letter[aa] + "_" + str(i+1)
ids[resid] = (extended_resid, chain, i+1)
return ids, chains
def parse_pdbfile(filename):
chains, residues, _ = get_coordinates_pdb_extended(filename, fil=True)
pdbids = {}
chain_seqs = {}
for chain in chains:
chain_seqs[chain]=[]
res_index_chain = 1
chain_test = chains[0]
for residue in residues:
num_id = int(residue.split("_")[-1])
chain = residue.split("_")[0]
if chain != chain_test:
res_index_chain = 1
chain_test = chain
pdbid = chain+str(num_id)
pdbids[pdbid] = (residue, chain, res_index_chain)
aa = to1letter[residue.split("_")[1]]
chain_seqs[chain].append(aa)
res_index_chain += 1
for chain in chains:
chain_seqs[chain] = "".join([x for x in chain_seqs[chain] if x is not None])
for chain_id in chain_seqs:
sequence = chain_seqs[chain_id]
type = "prot"
if "a" in sequence or "t" in sequence or "c" in sequence or "g" in sequence:
type = "dna"
elif "b" in sequence or "u" in sequence or "d" in sequence or "h" in sequence:
type = "rna"
chain_seqs[chain_id] = [type, sequence]
return pdbids, chain_seqs
def generate_json(pdbids, chain_seqs, mut_res, opf, default, symmetric_res):
if "all" in default:
if default == "all":
include = alphabet
else:
omit = list(default.split("-")[1])
include = [x for x in alphabet if x not in omit]
default = "".join(include)
mutable = []
if "~" in "".join(mut_res):
try:
fixed_res = []
fixed_seqs = [seq.strip("~") for seq in mut_res]
#print(fixed_seqs)
for c in chain_seqs:
for fixed in fixed_seqs:
if fixed in chain_seqs[c][-1]:
indices = list(find_all(chain_seqs[c][-1], fixed))
for i in indices:
for j in range(i, len(fixed)+i):
fixed_res.append(pdbids[str(c) + str(j+1)])
for pdbid in pdbids:
if pdbids[pdbid] not in fixed_res:
mutable.append({"chain":pdbids[pdbid][1], "resid": pdbids[pdbid][2], "WTAA": to1letter[pdbids[pdbid][0].split("_")[1]], "MutTo": default})
except:
raise ValueError("Invalid specification. Try using the less than sign after the residue ID.")
else:
for resind in mut_res:
if "*" in resind:
try:
chain = resind.split("*")[0]
for pdbid in pdbids:
if pdbid.startswith(chain):
mutable.append({"chain":pdbids[pdbid][1], "resid": pdbids[pdbid][2], "WTAA": to1letter[pdbids[pdbid][0].split("_")[1]], "MutTo": default})
except:
raise ValueError("Invalid specification. Try using the asterisk after the chain ID.")
#starts with residue number specified (inclusive)
elif "<G" in resind:
residue = resind.split("<")[0]
chain = re.split('(\d+)', residue)[0]
num_id = int(re.split('(\d+)', residue)[1])
chain_seq = chain_seqs[chain][-1]
for i in range(num_id-1, len(chain_seq)):
if chain_seq[i] == "G":
mutable.append({"chain":chain, "resid": i+1, "WTAA": "G", "MutTo": default})
else:
break
elif "<" in resind:
try:
residue = resind.split("<")[0]
chain = re.split('(\d+)', residue)[0]
num_id = int(re.split('(\d+)', residue)[1])
for pdbid in pdbids:
chain_compare = re.split('(\d+)', pdbid)[0]
num_id_compare = int(re.split('(\d+)', pdbid)[1])
if chain == chain_compare and num_id<=num_id_compare:
mutable.append({"chain":pdbids[pdbid][1], "resid": pdbids[pdbid][2], "WTAA": to1letter[pdbids[pdbid][0].split("_")[1]], "MutTo": default})
except:
raise ValueError("Invalid specification. Try using the less than sign after the residue ID.")
elif resind in pdbids:
mutable.append({"chain":pdbids[resind][1], "resid": pdbids[resind][2], "WTAA": to1letter[pdbids[resind][0].split("_")[1]], "MutTo": default})
symmetric = []
for symmetry in symmetric_res:
values = list(symmetry.values())
#print(values)
for tied_pos in zip(*values):
skip_tie = False
sym_res = []
for pos in tied_pos:
res_id = pos[0] + str(pos[1])
if pdbids[res_id][0] == 'X':
skip_tie = True
break
else:
sym_res.append( pdbids[res_id][1] + str(pdbids[res_id][2]) )
if not skip_tie:
symmetric.append(sym_res)
chain_seqs_mod = {}
for chain in chain_seqs:
chain_seqs_mod[chain] = {"sequence":list(chain_seqs[chain][-1]), "type":chain_seqs[chain][0]}
dictionary = {"chains" : chain_seqs_mod, "designable": mutable, "symmetric": symmetric}
# write json to file with human-friendly formatting
#jsonobj = json.dumps(dictionary, indent = 4)
jsonobj = pprint.pformat(dictionary, compact=True, sort_dicts=False).replace("'",'"')
with open(opf, "w") as outfile:
outfile.write(jsonobj)
def parse_mutres_input(mutresstring):
mutres_temp = mutresstring.strip().split(",")
mutres_temp = [x.strip() for x in mutres_temp if x]
mutres = []
for elem in mutres_temp:
if "-" not in elem:
mutres.append(elem)
else:
start, finish = elem.split("-")
chain = re.split('(\d+)', start)[0]
s = int(re.split('(\d+)', start)[1])
f = int(re.split('(\d+)', finish)[1])
for i in range(s, f+1):
mutres.append(chain + str(i))
return mutres
def _check_res_validity(res_item):
split_item = [item for item in re.split('(\d+)', res_item) if item]
if len(split_item) != 2:
raise ValueError(f'Unable to parse residue: {res_item}.')
return (split_item[0], int(split_item[1]))
def _check_range_validity(range_item):
split_range = range_item.split('-')
if len(split_range) != 2:
raise ValueError(f'Unable to parse residue range: {range_item}')
start_item, finish_item = split_range[0], split_range[1]
s_chain, s_idx = _check_res_validity(start_item)
f_chain, f_idx = _check_res_validity(finish_item)
if s_chain != f_chain:
raise ValueError(f'Residue ranges cannot span multiple chains: {range_item}')
if s_idx >= f_idx:
raise ValueError(f'Residue range starting index must be smaller than the ending index: '
f'{range_item}')
res_range = []
for i in range(s_idx, f_idx + 1):
res_range.append( (s_chain, i) )
return res_range
def _check_range_validity_asterisk(range_item, pdbids):
res_range = []
chain = range_item.split("*")[0]
for pdbid in pdbids:
chain_id = re.split('(\d+)', pdbid)[0]
if chain == chain_id:
res_range.append( (chain_id, pdbids[pdbid][2]) )
if len(res_range) == 0:
raise ValueError(f'Unable to parse residue range: {range_item}')
return res_range
def _check_symmetry_validity(symmetric_item, pdbids):
split_item = symmetric_item.split(':')
symmetry_dict = {}
for subitem in split_item:
if '-' in subitem:
res_range = _check_range_validity(subitem)
symmetry_dict[subitem] = res_range
elif '*' in subitem:
res_range = _check_range_validity_asterisk(subitem, pdbids)
symmetry_dict[subitem] = res_range
else:
res_ch, res_idx = _check_res_validity(subitem)
symmetry_dict[subitem] = [(res_ch, res_idx)]
item_lens = [len(symmetry_dict[key]) for key in symmetry_dict]
if math.floor(sum([l == item_lens[0] for l in item_lens])/len(item_lens)) != 1:
raise ValueError(f'Tied residues and residue ranges must be of the same '
f'size for forcing symmetry: {symmetric_item}')
return symmetry_dict
def parse_symmetric_res(symmetric_str, pdbids):
symmetric_str = [s for s in symmetric_str.strip().split(",") if s]
#print(symmetric_str)
symmetric_res = []
for item in symmetric_str:
if ":" not in item:
raise ValueError(f'No colon detected in symmetric res: {item}.')
symmetry_dict = _check_symmetry_validity(item, pdbids)
symmetric_res.append(symmetry_dict)
#print(symmetric_res)
return symmetric_res
def getPDBParser() -> FileArgumentParser:
"""Gets an FileArgumentParser with necessary arguments to run generate_json"""
parser = FileArgumentParser(description='Script that can take a PDB and PDB residue numbers'
' and convert to json file format for input to EvoPro',
fromfile_prefix_chars='@')
parser.add_argument('--pdb_file',
default=None,
type=str,
help='Path to and name of PDB file to extract chains and sequences.')
parser.add_argument('--sequence_file',
default=None,
type=str,
help='Path to and name of text file to extract chains and sequences. Only provide if there is no PDB file.')
parser.add_argument('--mut_res',
default='',
type=str,
help='PDB chain and residue numbers to mutate, separated by commas')
parser.add_argument('--default_mutres_setting',
default='all',
type=str,
help='Default setting for residues to mutate. Individual ones can be changed manually. Default is all')
parser.add_argument('--output',
default='',
type=str,
help='path and name of output json file')
parser.add_argument('--symmetric_res',
default='',
type=str,
help='PDB chain and residue numbers to force symmetry separated by a colon')
return parser
if __name__=="__main__":
parser = getPDBParser()
args = parser.parse_args(sys.argv[1:])
if args.sequence_file:
pdbids, chain_seqs = parse_seqfile(args.sequence_file)
else:
pdbids, chain_seqs = parse_pdbfile(args.pdb_file)
symres = parse_symmetric_res(args.symmetric_res, pdbids)
mutres = parse_mutres_input(args.mut_res)
generate_json(pdbids, chain_seqs, mutres, args.output, args.default_mutres_setting, symres)