Skip to content

Commit 6d9320f

Browse files
committed
correctly read and write box lengths and angles from PDB files
1 parent dc68757 commit 6d9320f

File tree

2 files changed

+16
-3
lines changed

2 files changed

+16
-3
lines changed

moleculekit/readers.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1259,8 +1259,17 @@ def _fix_formal_charge(val):
12591259
if tempfile:
12601260
os.unlink(filename)
12611261

1262+
box = None
1263+
boxangles = None
1264+
if "a" in crystalinfo and "b" in crystalinfo and "c" in crystalinfo:
1265+
box = np.array([crystalinfo["a"], crystalinfo["b"], crystalinfo["c"]])
1266+
if "alpha" in crystalinfo and "beta" in crystalinfo and "gamma" in crystalinfo:
1267+
boxangles = np.array(
1268+
[crystalinfo["alpha"], crystalinfo["beta"], crystalinfo["gamma"]]
1269+
)
1270+
12621271
topo.crystalinfo = crystalinfo
1263-
traj = Trajectory(coords=coords)
1272+
traj = Trajectory(coords=coords, box=box, boxangles=boxangles)
12641273
return MolFactory.construct(
12651274
topo,
12661275
traj,

moleculekit/writers.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,10 +131,14 @@ def PDBwrite(mol, filename, frames=None, writebonds=True, mode="pdb"):
131131

132132
checkTruncations(mol)
133133
box = None
134+
boxangles = None
134135
if mol.numFrames != 0:
135136
coords = np.atleast_3d(mol.coords[:, :, frames])
136137
if hasattr(mol, "box") and mol.box.shape[1] != 0:
137138
box = mol.box[:, frames[0]]
139+
boxangles = [90, 90, 90]
140+
if hasattr(mol, "boxangles") and mol.boxangles.shape[1] != 0:
141+
boxangles = mol.boxangles[:, frames[0]]
138142
else: # If Molecule only contains topology, PDB requires some coordinates so give it zeros
139143
coords = np.zeros((mol.numAtoms, 3, 1), dtype=np.float32)
140144

@@ -166,10 +170,10 @@ def PDBwrite(mol, filename, frames=None, writebonds=True, mode="pdb"):
166170
else:
167171
fh = open(filename, "w", encoding="ascii")
168172

169-
if box is not None and not np.all(mol.box == 0):
173+
if box is not None and not np.all(box == 0):
170174
fh.write(
171175
"CRYST1%9.3f%9.3f%9.3f%7.2f%7.2f%7.2f P 1 1 \n"
172-
% (box[0], box[1], box[2], 90, 90, 90)
176+
% (box[0], box[1], box[2], boxangles[0], boxangles[1], boxangles[2])
173177
)
174178

175179
if mode == "pdb":

0 commit comments

Comments
 (0)