-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy path01_prepare_get_json.py
More file actions
371 lines (313 loc) · 11.7 KB
/
01_prepare_get_json.py
File metadata and controls
371 lines (313 loc) · 11.7 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
from Bio import PDB
from Bio.PDB import Structure, Model
from Bio.PDB import PDBParser, MMCIFIO
import os
import pandas as pd
from tqdm import tqdm
import multiprocessing as mp
from pathlib import Path
import math
import shutil
import argparse
from concurrent.futures import ThreadPoolExecutor, as_completed
import json
# Dictionary for converting three-letter amino acid codes to single-letter codes
protein_letters_3to1 = {
"ALA": "A",
"CYS": "C",
"ASP": "D",
"GLU": "E",
"PHE": "F",
"GLY": "G",
"HIS": "H",
"ILE": "I",
"LYS": "K",
"LEU": "L",
"MET": "M",
"ASN": "N",
"PRO": "P",
"GLN": "Q",
"ARG": "R",
"SER": "S",
"THR": "T",
"VAL": "V",
"TRP": "W",
"TYR": "Y",
"MSE": "M",
}
def split_by_total_length(df, num_jobs):
"""
Split the dataframe into num_jobs batches based on the sum of 'total_length':
1. Ensures each batch has a similar total sequence length sum for load balancing.
2. Groups samples with similar lengths together.
"""
# Sort by total_length in descending order (Greedy approach for load balancing)
df = df.sort_values("total_length", ascending=False).reset_index(
drop=True
)
total_sum = df["total_length"].sum()
target_sum = total_sum / num_jobs
groups = []
current_group = []
current_sum = 0
for _, row in df.iterrows():
val = int(row["total_length"])
# If adding the current row exceeds the target sum, start a new group
# Constraint: ensures we don't create too many small groups early on
if current_sum + val > target_sum and len(groups) < num_jobs - 2:
groups.append(pd.DataFrame(current_group))
current_group = [row]
current_sum = val
else:
current_group.append(row)
current_sum += val
# Handle the final batch splitting to reach the exact num_jobs count
len_last_group = len(current_group)
index = int(len_last_group / 2.2)
groups.append(pd.DataFrame(current_group[:index]))
groups.append(pd.DataFrame(current_group[index:]))
return groups
def format_msa_sequence(sequence):
"""Format a raw sequence into a basic MSA query string."""
return f">query\n{sequence}\n"
def get_chain_sequences_from_row(row):
"""Extract all non-empty chain sequences from a dataframe row."""
chain_sequences = []
chain_columns = [
col
for col in row.index
if col.startswith("chain_") and col.endswith("_seq")
]
for col in chain_columns:
if pd.notna(row[col]) and row[col] != "":
chain_id = col.split("_")[1]
chain_sequences.append((chain_id, row[col]))
return chain_sequences
def get_sequence_from_chain(chain):
"""Convert Biopython chain object to a single-letter amino acid sequence."""
sequence = ""
for residue in chain:
# ' ' indicates a standard residue (not heteroatoms)
if residue.id[0] == " ":
resname = residue.get_resname().upper()
sequence += protein_letters_3to1.get(resname, "X")
return sequence
def process_single_pdb(args):
"""
Process a single PDB file:
1. Extract sequences for each chain.
2. Save each chain as a separate .cif file for AlphaFold3 template compatibility.
"""
input_pdb, output_dir_cif = args
try:
parser = PDB.PDBParser(QUIET=True)
structure = parser.get_structure("structure", input_pdb)
base_name = os.path.splitext(os.path.basename(input_pdb))[0]
chain_sequences = {}
merged_sequence = ""
# Iterate through the first model (index 0) of the PDB
for chain in structure[0]:
chain_id = chain.id
sequence = get_sequence_from_chain(chain)
chain_sequences[chain_id] = sequence
merged_sequence += sequence
# Create a new structure object containing only this specific chain
new_structure = Structure.Structure("new_structure")
new_model = Model.Model(0)
new_structure.add(new_model)
new_model.add(chain.copy())
# Save as MMCIF format
cif_io = MMCIFIO()
cif_io.set_structure(new_structure)
cif_output = os.path.join(
output_dir_cif, f"{base_name}_chain_{chain_id}.cif"
)
cif_io.save(cif_output)
return base_name, chain_sequences, len(merged_sequence)
except Exception as e:
print(f"Error processing {input_pdb}: {str(e)}")
return None, None, None
def generate_json_files(tasks):
"""Generate AlphaFold3 formatted JSON input files from sequence data."""
row, cif_dir, output_dir = tasks
complex_name = row["complex"]
chain_sequences = get_chain_sequences_from_row(row)
if not chain_sequences:
print(f"⚠️ Warning: No valid chain sequences for {complex_name}")
return None
sequences = []
for chain_id, sequence in chain_sequences:
cif_filename = f"{complex_name}_chain_{chain_id}.cif"
cif_path = os.path.join(cif_dir, cif_filename)
if not os.path.exists(cif_path):
print(f"⚠️ Warning: {cif_filename} not found")
continue
# Build the AlphaFold3 JSON structure for the protein component
sequences.append(
{
"protein": {
"id": chain_id,
"sequence": sequence,
"modifications": [],
"unpairedMsa": format_msa_sequence(sequence),
"pairedMsa": format_msa_sequence(sequence),
"templates": [
{
"mmcifPath": cif_path,
"queryIndices": list(range(len(sequence))),
"templateIndices": list(range(len(sequence))),
}
],
}
}
)
if not sequences:
print(f"⚠️ Warning: No valid sequence data for {complex_name}")
return None
json_data = {
"dialect": "alphafold3",
"version": 1,
"name": complex_name,
"sequences": sequences,
"modelSeeds": [10],
"bondedAtomPairs": None,
"userCCD": None,
}
output_filename = f"{complex_name}.json"
output_path = os.path.join(output_dir, output_filename)
with open(output_path, "w") as f:
json.dump(json_data, f, indent=2)
return output_filename
def get_seq_main():
parser = argparse.ArgumentParser()
parser.add_argument("--input_dir", type=str, default="input_pdbs")
parser.add_argument(
"--output_dir_cif", type=str, default="single_chain_cif"
)
parser.add_argument("--save_csv", type=str, default="seq.csv")
parser.add_argument("--output_dir_json", type=str, default="json")
parser.add_argument("--batch_dir", type=str, default="batch")
parser.add_argument(
"--num_workers",
type=int,
default=None,
help="Process count (default CPU-4)",
)
parser.add_argument(
"--num_jobs",
type=int,
default=None,
help="Number of batch groups to create",
)
args = parser.parse_args()
# Environment Setup
num_workers = (
args.num_workers if args.num_workers else mp.cpu_count() - 4
)
num_jobs = args.num_jobs
os.makedirs(args.output_dir_cif, exist_ok=True)
os.makedirs(args.output_dir_json, exist_ok=True)
print(f"Input Directory: {args.input_dir}")
print(f"CIF Output Directory: {args.output_dir_cif}")
print(f"JSON Output Directory: {args.output_dir_json}")
# Phase 1: Parallel PDB Processing (Extract sequences and split chains)
pdb_files = list(Path(args.input_dir).glob("*.pdb"))
process_args = [(str(f), args.output_dir_cif) for f in pdb_files]
sequences_dict = {}
with mp.Pool(processes=num_workers) as pool:
results = list(
tqdm(
pool.imap(process_single_pdb, process_args),
total=len(pdb_files),
desc="Processing PDBs",
)
)
# Consolidate results into sequence metadata
for base_name, chain_sequences, length in results:
if base_name is not None:
sequences_dict[base_name] = {
"sequences": chain_sequences,
"length": length,
}
# Aggregate all unique chain IDs across all structures for CSV alignment
all_chain_ids = set()
for entry in sequences_dict.values():
all_chain_ids.update(entry["sequences"].keys())
def chain_sort_key(chain_id):
return str(chain_id)
all_chain_ids = sorted(list(all_chain_ids), key=chain_sort_key)
# Prepare DataFrame rows
rows = []
for complex_name, entry in sequences_dict.items():
chain_data = entry["sequences"]
row = {"complex": complex_name, "total_length": entry["length"]}
for chain_id in all_chain_ids:
row[f"chain_{chain_id}_seq"] = chain_data.get(chain_id, "")
rows.append(row)
df = pd.DataFrame(rows)
# Reorder columns: ID, Length, then specific chain sequences
cols = ["complex", "total_length"] + [
c for c in df.columns if c not in ["complex", "total_length"]
]
df = df[cols]
df.to_csv(args.save_csv, index=False)
print(f"\n✅ Sequence info saved to {args.save_csv}")
# Phase 2: Parallel JSON Generation
json_tasks = [
(r, args.output_dir_cif, args.output_dir_json)
for _, r in df.iterrows()
]
with mp.Pool(processes=num_workers) as pool:
json_results = list(
tqdm(
pool.imap(generate_json_files, json_tasks),
total=len(json_tasks),
desc="Generating JSONs",
)
)
success_count = sum(1 for r in json_results if r)
print(
f"\n✅ Parallel JSON generation complete: {success_count} files created"
)
# Phase 3: Batch Partitioning and Symlinking
batch_json_root = os.path.join(args.batch_dir, "json")
batch_pdb_root = os.path.join(args.batch_dir, "pdb")
# Use the length-based splitting logic to balance workloads across jobs
df_shuffled = df.sample(frac=1).reset_index(drop=True)
subs = split_by_total_length(df_shuffled, num_jobs)
print(
f"📦 Total samples: {len(df_shuffled)}, splitting into {num_jobs} batches."
)
for i, sub in enumerate(subs):
if sub.empty:
print(f"🔹 Batch {i} is empty, skipping.")
continue
max_len = sub["total_length"].max()
batch_name = f"batch_{i}_{max_len}"
bd_json = os.path.join(batch_json_root, batch_name)
bd_pdb = os.path.join(batch_pdb_root, batch_name)
os.makedirs(bd_json, exist_ok=True)
os.makedirs(bd_pdb, exist_ok=True)
count = 0
for _, r in sub.iterrows():
cid = r["complex"]
# Create symbolic links to save space while organizing files into batch folders
for ext, src_dir, dest_dir in [
(".pdb", args.input_dir, bd_pdb),
(".json", args.output_dir_json, bd_json),
]:
src = os.path.join(src_dir, f"{cid}{ext}")
if os.path.exists(src):
dest_path = os.path.join(
dest_dir, os.path.basename(src)
)
if os.path.exists(dest_path):
os.remove(dest_path)
os.symlink(src, dest_path)
count += 1
print(f"✅ {batch_name}: contains {count} complexes")
print(
f"\n📊 Summary: Processed {len(df)} complexes across {len(os.listdir(batch_json_root))} batches."
)
if __name__ == "__main__":
get_seq_main()