Skip to content

Commit e52212b

Browse files
committed
Add PDBWriter.dump_single
1 parent d544695 commit e52212b

File tree

3 files changed

+45
-12
lines changed

3 files changed

+45
-12
lines changed

CHANGELOG.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,14 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## [Unreleased]
99

10+
### Added
11+
12+
- Add a `stride` option to the trajectory writers.
13+
- Add method `dump_single` method to `PDBWriter` to write one-off file with a single snapshot.
14+
1015
### Changed
1116

1217
- By default, run only 100 optimization steps in `build_random_cell`.
13-
- Add a `stride` option to the trajectory writers.
1418

1519
## [0.0.0] - 2024-10-06
1620

src/tinyff/trajectory.py

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import os
2828
from functools import partial
2929
from glob import glob
30+
from typing import TextIO
3031

3132
import attrs
3233
import numpy as np
@@ -83,19 +84,27 @@ def dump(self, atpos: ArrayLike, cell_lengths: ArrayLike):
8384

8485
def dump_each(self, atpos: ArrayLike, cell_lengths: ArrayLike):
8586
"""Write a snapshot to the PDB file without considering `self.stride`."""
87+
with open(self.path_pdb, "a") as fh:
88+
self._dump_low(fh, atpos, cell_lengths)
89+
90+
def dump_single(self, path_pdb: str, atpos: ArrayLike, cell_lengths: ArrayLike):
91+
"""Dump a single snapshot with the same settings to a different file."""
92+
with open(path_pdb, "w") as fh:
93+
self._dump_low(fh, atpos, cell_lengths)
94+
95+
def _dump_low(self, fh: TextIO, atpos: ArrayLike, cell_lengths: ArrayLike):
8696
atpos = parse_atpos(atpos, len(self.atnums))
8797
cell_lengths = parse_cell_lengths(cell_lengths)
88-
with open(self.path_pdb, "a") as fh:
89-
a, b, c = cell_lengths * self.to_angstrom
90-
print(f"CRYST1{a:9.3f}{b:9.3f}{c:9.3f} 90.00 90.00 90.00 P 1 1", file=fh)
91-
for i, (x, y, z) in enumerate(atpos * self.to_angstrom):
92-
symbol = SYMBOLS[self.atnums[i] - 1]
93-
print(
94-
f"HETATM{i+1:5d} {symbol:2s} ATM 1 {x:8.3f}{y:8.3f}{z:8.3f}"
95-
f" 1.00 1.00 {symbol:2s}",
96-
file=fh,
97-
)
98-
print("END", file=fh)
98+
a, b, c = cell_lengths * self.to_angstrom
99+
print(f"CRYST1{a:9.3f}{b:9.3f}{c:9.3f} 90.00 90.00 90.00 P 1 1", file=fh)
100+
for i, (x, y, z) in enumerate(atpos * self.to_angstrom):
101+
symbol = SYMBOLS[self.atnums[i] - 1]
102+
print(
103+
f"HETATM{i+1:5d} {symbol:2s} ATM 1 {x:8.3f}{y:8.3f}{z:8.3f}"
104+
f" 1.00 1.00 {symbol:2s}",
105+
file=fh,
106+
)
107+
print("END", file=fh)
99108

100109

101110
@attrs.define

tests/test_trajectory.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,26 @@ def test_pdb_writer_stride(tmpdir):
7979
assert fh.read().count("CRYST1") == 3
8080

8181

82+
PDB_REF_SINGLE = """\
83+
CRYST1 10.000 10.000 10.000 90.00 90.00 90.00 P 1 1
84+
HETATM 1 Ar ATM 1 3.000 4.000 5.000 1.00 1.00 Ar
85+
HETATM 2 Ar ATM 1 6.000 7.000 3.142 1.00 1.00 Ar
86+
END
87+
"""
88+
89+
90+
def test_pdb_writer_single(tmpdir):
91+
cell_length = 10.0
92+
atpos = np.array([[3.0, 4.0, 5.0], [6.0, 7.0, 3.1415]])
93+
path_pdb = os.path.join(tmpdir, "test.pdb")
94+
path_other = os.path.join(tmpdir, "other.pdb")
95+
pdb_writer = PDBWriter(path_pdb, to_angstrom=1.0, atnums=[18] * len(atpos), stride=5)
96+
pdb_writer.dump_single(path_other, atpos, cell_length)
97+
98+
with open(path_other) as fh:
99+
assert fh.read() == PDB_REF_SINGLE
100+
101+
82102
def test_npy_traj(tmpdir):
83103
traj_atpos = np.array([[[1.2, 1.3], [4.9, 3.1]], [[0.7, 8.1], [-7.9, 0.5]]])
84104
traj_pressure = np.array([5.0, 4.0])

0 commit comments

Comments
 (0)