-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconverter.py
More file actions
39 lines (30 loc) · 1.18 KB
/
converter.py
File metadata and controls
39 lines (30 loc) · 1.18 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
import mdtraj as md
import os
import pickle
def convert_parm7_nc_to_pdb(parm7_file, nc_file, output_pdb_dir, token_file):
"""
Convert `.parm7` and `.nc` trajectory files to `.pdb` format based on `.pkl` token data.
"""
# Create the output directory if it doesn't exist
os.makedirs(output_pdb_dir, exist_ok=True)
# Load the trajectory using MDTraj
traj = md.load(nc_file, top=parm7_file)
# Load token data from the `.pkl` file
with open(token_file, 'rb') as f:
token_data = pickle.load(f)
# Extract and save only the frames specified in the `.pkl` file
for frame_idx in token_data.keys():
# Slice the trajectory for the current frame
frame = traj.slice(frame_idx, copy=True)
# Output file path
output_pdb_file = os.path.join(output_pdb_dir, f"frame_{frame_idx}.pdb")
# Save the frame as a PDB
frame.save(output_pdb_file)
print(f"Frame {frame_idx} saved as {output_pdb_file}")
print(f"Conversion of specified frames to PDB complete.")
# Example usage
parm7_file = ''
nc_file = ''
output_pdb_dir = ''
token_file = ''
convert_parm7_nc_to_pdb(parm7_file, nc_file, output_pdb_dir, token_file)