-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprepare_script_af3_multichain_multi_template.py
573 lines (456 loc) · 18.9 KB
/
prepare_script_af3_multichain_multi_template.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
"""
author: nabin
This script prepares input for running AlphaFold3.
1. Standard Input for AF3.
2. Input without using any templates.
3. Input using own template.
Runing AF3:
docker run -it \
--volume /AF3/alphafold3/af_inputs:/root/af_input \
--volume /AF3/alphafold3/af_outputs:/root/af_output \
--volume /AF3/alphafold3/model_parameters:/root/models \
--volume /bmlfast/databases:/root/public_databases \
--gpus all \
alphafold3 \
python run_alphafold.py \
--json_path=/root/af_input/40352.json \
--model_dir=/root/models \
--output_dir=/root/af_output
Note: Replace [40352.json] with the input filename for AF3. This script prepares that.
"""
from Bio import SeqIO, pairwise2
from Bio.PDB.MMCIFParser import MMCIFParser
from Bio.PDB.PDBParser import PDBParser
from Bio.SeqUtils import seq1
import json
import os
from Bio.PDB import PDBParser, MMCIFIO
import os
no_temp_seq_list = list()
from Bio import PDB
restype_3to1 = {
'ALA': 'A',
'ARG': 'R',
'ASN': 'N',
'ASP': 'D',
'CYS': 'C',
'GLN': 'Q',
'GLU': 'E',
'GLY': 'G',
'HIS': 'H',
'ILE': 'I',
'LEU': 'L',
'LYS': 'K',
'MET': 'M',
'PHE': 'F',
'PRO': 'P',
'SER': 'S',
'THR': 'T',
'TRP': 'W',
'TYR': 'Y',
'VAL': 'V',
'UNK' : 'U',
}
def extract_seq(pdb_file, atomic_chain_seq_file, atomic_seq_file):
chain_seq_dict = dict()
parser = PDB.PDBParser()
pdb_map = pdb_file
struct = parser.get_structure("CA", pdb_map)
for model in struct:
for chain in model:
for residue in chain:
for atom in residue:
if atom.get_name() == "CA":
chain_id = chain.id
try:
amino_name = restype_3to1[residue.resname]
if chain_id in chain_seq_dict:
chain_seq_dict[chain_id].append(amino_name)
else:
chain_seq_dict[chain_id] = [amino_name]
except KeyError:
pass
with open(atomic_chain_seq_file, 'w') as a_c:
for k,v in chain_seq_dict.items():
print(f">pdb2seq|Chains {k}", file=a_c)
result = ''.join(v)
print(result, file=a_c)
all_seq = list()
with open(atomic_seq_file, 'w') as a_s:
print(">pdb2seq|Chains A", file=a_s)
for k,v in chain_seq_dict.items():
result = ''.join(v)
all_seq.append(result)
final_result = ''.join(all_seq)
print(final_result,file=a_s)
def extract_sequences_from_fasta(fasta_file):
"""
Extract sequences and chain IDs from a multi-chain FASTA file.
"""
sequences = {}
with open(fasta_file, 'r') as f:
lines = f.readlines()
current_chain = None
for line in lines:
if line.startswith(">"):
current_chain = line.split("|")[1].strip().split()[1]
sequences[current_chain] = ""
else:
sequences[current_chain] += line.strip()
return sequences
def extract_sequences_from_structure_multichain(structure_file):
"""
Extract sequences from a multi-chain structure file (mmCIF or PDB).
"""
if structure_file.endswith(".cif"):
parser = MMCIFParser()
elif structure_file.endswith(".pdb"):
parser = PDBParser()
else:
raise ValueError("Invalid file format. Use .cif or .pdb.")
structure = parser.get_structure("template", structure_file)
chain_sequences = {}
for chain in structure.get_chains():
chain_id = chain.id
sequence = ""
for residue in chain.get_residues():
if residue.has_id("CA"): # Only include residues with CA (amino acids)
sequence += seq1(residue.get_resname(), custom_map={"UNK": "X"})
chain_sequences[chain_id] = sequence
return chain_sequences
def align_sequences(query_sequence, template_sequence):
"""
Align the query sequence with the template sequence and get indices for AlphaFold3.
"""
alignments = pairwise2.align.globalxx(query_sequence, template_sequence)
best_alignment = alignments[0]
query_indices = []
template_indices = []
query_pos = 0
template_pos = 0
for q, t in zip(best_alignment.seqA, best_alignment.seqB):
if q != "-" and t != "-":
query_indices.append(query_pos)
template_indices.append(template_pos)
if q != "-":
query_pos += 1
if t != "-":
template_pos += 1
return query_indices, template_indices
def align_sequences_multichain(query_sequences, template_sequences, protein_id):
"""
Align multi-chain query sequences with template sequences and get indices for AlphaFold3.
"""
alignment_data = {}
# print("Query Sequences: ", query_sequences)
# print("Template Sequences: ", template_sequences)
for chain_id, query_sequence in query_sequences.items():
if chain_id in template_sequences:
template_sequence = template_sequences[chain_id]
alignments = pairwise2.align.globalxx(query_sequence, template_sequence)
best_alignment = alignments[0]
query_indices = []
template_indices = []
query_pos = 0
template_pos = 0
for q, t in zip(best_alignment.seqA, best_alignment.seqB):
if q != "-" and t != "-":
query_indices.append(query_pos)
template_indices.append(template_pos)
if q != "-":
query_pos += 1
if t != "-":
template_pos += 1
alignment_data[chain_id] = (query_indices, template_indices)
else:
print(f"Warning: No template sequence found for chain {chain_id}. Skipping alignment.")
alignment_data[chain_id] = ([], [])
no_temp_seq_list.append(protein_id)
return alignment_data
def pdb2cif(template_pdb, template_cif):
# Load the PDB file
parser = PDBParser(QUIET=True)
structure = parser.get_structure('structure', template_pdb)
# Write the initial mmCIF file
io = MMCIFIO()
io.set_structure(structure)
io.save(template_cif)
release_date = "1999-01-01" # change release date. this date is safe and AF3 includes it.
# Need below for AF3 to find the release date. The cutoff is ~ 2021
metadata_entries = f"""
#
_pdbx_database_status.recvd_initial_deposition_date {release_date}
_struct_ref_seq.seq_release_date {release_date}
_pdbx_audit_revision_history.revision_date {release_date}
#
"""
# Append the release date to the mmCIF file
with open(template_cif, "a") as cif_file:
cif_file.write(metadata_entries)
def extract_chain_from_structure(template_path, chain_id):
"""
Extract the specified chain from a structure file and return its contents as a string.
"""
from Bio.PDB import MMCIFParser, PDBIO, Select
class ChainSelect(Select):
def __init__(self, chain_id):
self.chain_id = chain_id
def accept_chain(self, chain):
return chain.id == self.chain_id
parser = MMCIFParser(QUIET=True)
structure = parser.get_structure("template", template_path)
output_file = f"{os.path.splitext(template_path)[0]}_chain{chain_id}.cif"
# io = PDBIO()
io = MMCIFIO()
io.set_structure(structure)
io.save(output_file, select=ChainSelect(chain_id))
release_date = "1999-01-01" # change release date. this date is safe and AF3 includes it.
# Need below for AF3 to find the release date. The cutoff is ~ 2021
metadata_entries = f"""
#
_pdbx_database_status.recvd_initial_deposition_date {release_date}
_struct_ref_seq.seq_release_date {release_date}
_pdbx_audit_revision_history.revision_date {release_date}
#
"""
# Append the release date to the mmCIF file
with open(output_file, "a") as cif_file:
cif_file.write(metadata_entries)
with open(output_file, "r") as file:
chain_content = file.read()
return chain_content
def generate_json_cryo2struct_multichain_2(query_sequences, alignment_data_1, alignment_data_2, template_path_1, template_path_2):
"""
Generate JSON for multi-chain sequences with template mappings in AlphaFold3 format.
Parameters:
query_sequences: dict of {chain_id: query_sequence}
alignment_data: dict of {chain_id: (query_indices, template_indices)}
template_path: Path to the template structure file.
Returns:
dict: JSON structure for AlphaFold3 input.
"""
name_ = os.path.basename(template_path_1).split("_")[0]
name_ = f"{name_}_cryo2struct_multi_template"
sequence_entries = []
for chain_id, query_sequence in query_sequences.items():
query_indices_1, template_indices_1 = alignment_data_1.get(chain_id, ([], []))
query_indices_2, template_indices_2 = alignment_data_2.get(chain_id, ([], []))
# Extract chain-specific template structure
try:
chain_template_content_1 = extract_chain_from_structure(template_path_1, chain_id)
chain_template_content_2 = extract_chain_from_structure(template_path_2, chain_id)
except Exception as e:
print(f"Error extracting chain {chain_id} from template: {e}")
chain_template_content_1 = ""
chain_template_content_2 = ""
sequence_entries.append({
"protein": {
"id": chain_id,
"sequence": query_sequence,
"pairedMsa": "",
"unpairedMsa": "",
"templates": [
{
"mmcif": chain_template_content_1.strip(),
"queryIndices": query_indices_1,
"templateIndices": template_indices_1
},
{
"mmcif": chain_template_content_2.strip(),
"queryIndices": query_indices_2,
"templateIndices": template_indices_2
}
]
}
})
data = {
"name": name_,
"modelSeeds": [1],
"sequences": sequence_entries,
"dialect": "alphafold3",
"version": 1
}
return data
def generate_json_cryo2struct(query_sequence, query_indices, template_indices, template_path):
"""
Generate JSON with template mappings.
"""
with open(template_path, "r") as cif_file:
mmcif_contents = cif_file.read()
mmcif_contents_inline = mmcif_contents.strip()
name_ = os.path.basename(template_path).split("_")[0]
name_ = f"{name_}_cryo2struct"
data = {
"name": name_,
"modelSeeds": [1],
"sequences": [
{
"protein": {
"id": "A",
"sequence": query_sequence,
"pairedMsa": "",
"unpairedMsa":"",
"templates": [
{
"mmcif": mmcif_contents_inline,
"queryIndices": query_indices,
"templateIndices": template_indices
}
]
}
}
],
"dialect": "alphafold3",
"version": 1
}
return data
def generate_json_cryo2struct_multichain(query_sequences, alignment_data, template_path):
"""
Generate JSON for multi-chain sequences with template mappings in AlphaFold3 format.
"""
with open(template_path, "r") as cif_file:
mmcif_contents = cif_file.read()
mmcif_contents_inline = mmcif_contents.strip()
name_ = os.path.basename(template_path).split("_")[0]
name_ = f"{name_}_cryo2struct"
sequence_entries = []
for chain_id, query_sequence in query_sequences.items():
query_indices, template_indices = alignment_data.get(chain_id, ([], []))
sequence_entries.append({
"protein": {
"id": chain_id,
"sequence": query_sequence,
"pairedMsa": "",
"unpairedMsa": "",
"templates": [
{
"mmcif": mmcif_contents_inline,
"queryIndices": query_indices,
"templateIndices": template_indices
}
]
}
})
data = {
"name": name_,
"modelSeeds": [1],
"sequences": sequence_entries,
"dialect": "alphafold3",
"version": 1
}
return data
def generate_json_no_template(sequences, template_path):
"""
Generate JSON for multi-chain sequences in AlphaFold3 format.
"""
name_ = os.path.basename(template_path).split("_")[0]
name_ = f"{name_}_no_template"
sequence_entries = [
{"protein": {"id": chain_id, "sequence": seq, "pairedMsa": "", "unpairedMsa": "", "templates": []}}
for chain_id, seq in sequences.items()
]
data = {
"name": name_,
"modelSeeds": [1],
"sequences": sequence_entries,
"dialect": "alphafold3",
"version": 1
}
return data
def generate_json(sequences, template_path):
"""
Generate JSON for multi-chain sequences in AlphaFold3 format.
"""
name_ = os.path.basename(template_path).split("_")[0]
name_ = f"{name_}_standard"
sequence_entries = [
{"protein": {"id": chain_id, "sequence": seq}}
for chain_id, seq in sequences.items()
]
data = {
"name": name_,
"modelSeeds": [1],
"sequences": sequence_entries,
"dialect": "alphafold3",
"version": 1
}
return data
def main_multi(query_fasta_path, template_structure_1, template_structure_2, output_json_path, output_json_path_cryo2struct, output_json_path_no_template, protein_id):
print("Extracting sequences from FASTA...")
sequences = extract_sequences_from_fasta(query_fasta_path)
print(f"Extracted sequences for chains: {', '.join(sequences.keys())}")
print("Extracting template sequences from structure...")
template_sequences_1 = extract_sequences_from_structure_multichain(template_structure_1)
print(f"Extracted template sequences for chains: {', '.join(template_sequences_1.keys())}")
template_sequences_2 = extract_sequences_from_structure_multichain(template_structure_2)
print(f"Extracted template sequences for chains: {', '.join(template_sequences_2.keys())}")
# print("Generating Standard JSON for multi-chain...")
json_data = generate_json(sequences, template_structure_1)
with open(output_json_path, "w") as outfile:
json.dump(json_data, outfile, indent=2)
print(f"JSON saved to {output_json_path}")
print("Generating No Template JSON for multi-chain...")
json_data = generate_json_no_template(sequences, template_structure_1)
with open(output_json_path_no_template, "w") as outfile:
json.dump(json_data, outfile, indent=2)
print(f"JSON saved to {output_json_path_no_template}")
alignment_data_1 = align_sequences_multichain(sequences, template_sequences_1, protein_id)
alignment_data_2 = align_sequences_multichain(sequences, template_sequences_2, protein_id)
print("Generating Cryo2Struct JSON for multi-chain...")
json_data_cryo2struct = generate_json_cryo2struct_multichain_2(sequences, alignment_data_1, alignment_data_2, template_structure_1, template_structure_2)
with open(output_json_path_cryo2struct, "w") as outfile:
json.dump(json_data_cryo2struct, outfile, indent=2)
print(f"JSON saved to {output_json_path_cryo2struct}")
if __name__ == "__main__":
no_template_list = list()
scripts_dir = "/AF3/alphafold3/af_inputs_multichain_multitemplate"
os.makedirs(scripts_dir, exist_ok=True)
data_dir = "/data"
data_dir_list_all = os.listdir(data_dir)
data_dir_list = data_dir_list_all
for protein_id in data_dir_list:
print("#"*50)
print("Processing -> ", protein_id)
print("#"*50)
protein_dir = f"{data_dir}/{protein_id}"
template_structure_pdb_1 = f"{protein_dir}/{protein_id}_cryo2struct_atomic_chain_conf_score_lmd.pdb"
template_structure_1 = f"{protein_dir}/{protein_id}_cryo2struct_lmd.cif"
template_structure_pdb_2 = f"{protein_dir}/{protein_id}_cryo2struct_atomic_chain_conf_score.pdb"
template_structure_2 = f"{protein_dir}/{protein_id}_cryo2struct.cif"
if os.path.exists(template_structure_pdb_2) and os.path.exists(template_structure_pdb_1):
if os.path.exists(template_structure_1):
os.remove(template_structure_1)
if os.path.exists(template_structure_2):
os.remove(template_structure_2)
# convert pdb to cif. AF3 requires template in .cif format.
pdb2cif(template_pdb=template_structure_pdb_1, template_cif=template_structure_1)
pdb2cif(template_pdb=template_structure_pdb_2, template_cif=template_structure_2)
fasta_file = [p for p in os.listdir(protein_dir) if p.endswith(".fasta")]
fasta_file.sort()
fasta_file_name = fasta_file[0].split(".")[0]
protein_file = f"{protein_dir}/{fasta_file_name}.pdb"
# print("Protein File: ", protein_file)
atomic_chain_seq_file = f"{protein_dir}/atomic_seq_chain.fasta"
atomic_seq_file = f"{protein_dir}/atomic_seq.fasta"
if os.path.exists(atomic_chain_seq_file):
os.remove(atomic_chain_seq_file)
if os.path.exists(atomic_seq_file):
os.remove(atomic_seq_file)
extract_seq(protein_file, atomic_chain_seq_file, atomic_seq_file)
output_json_path = f"{scripts_dir}/{protein_id}.json"
output_json_path_cryo2struct = f"{scripts_dir}/{protein_id}_cryo2struct_multi_template.json"
output_json_path_no_template = f"{scripts_dir}/{protein_id}_no_template.json"
if os.path.exists(output_json_path):
os.remove(output_json_path)
if os.path.exists(output_json_path_cryo2struct):
os.remove(output_json_path_cryo2struct)
if os.path.exists(output_json_path_no_template):
os.remove(output_json_path_no_template)
main_multi(atomic_chain_seq_file, template_structure_1, template_structure_2, output_json_path, output_json_path_cryo2struct, output_json_path_no_template, protein_id)
print("Done -> ", protein_id)
else:
print("No template structure found for ", protein_id)
no_template_list.append(protein_id)
print("No template found for: ", no_template_list)
print("No template sequence found for: ", set(no_temp_seq_list))
print("Total Data: ", len(data_dir_list))