IO¶
The IO module handles reading and writing of various molecular file formats.
Factory Functions¶
Data file reader factory functions.
This module provides convenient factory functions for creating various data file readers. All functions return Frame objects by populating an optional frame parameter.
_ensure_frame ¶
_ensure_frame(frame)
Ensure a Frame object exists.
Source code in src/molpy/io/readers.py
15 16 17 18 19 20 21 | |
read_amber_ac ¶
read_amber_ac(file, frame=None)
Read AC file and return a Frame object.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
file
|
PathLike
|
Path to AC file |
required |
frame
|
Any
|
Optional existing Frame to populate |
None
|
Returns:
| Type | Description |
|---|---|
Any
|
Populated Frame object |
Source code in src/molpy/io/readers.py
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 | |
read_amber_inpcrd ¶
read_amber_inpcrd(inpcrd, frame=None)
Read AMBER inpcrd file and return a Frame object.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
inpcrd
|
PathLike
|
Path to AMBER inpcrd file |
required |
frame
|
Any
|
Optional existing Frame to populate |
None
|
Returns:
| Type | Description |
|---|---|
Any
|
Populated Frame object |
Source code in src/molpy/io/readers.py
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 | |
read_amber_prmtop ¶
read_amber_prmtop(prmtop, inpcrd=None, frame=None)
Read AMBER prmtop and optional inpcrd files.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
prmtop
|
PathLike
|
Path to AMBER prmtop file |
required |
inpcrd
|
PathLike | None
|
Optional path to AMBER inpcrd file |
None
|
frame
|
Any
|
Optional existing Frame to populate |
None
|
Returns:
| Type | Description |
|---|---|
Any
|
Tuple of (Frame, ForceField) |
Source code in src/molpy/io/readers.py
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 | |
read_gro ¶
read_gro(file, frame=None)
Read GROMACS gro file and return a Frame object.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
file
|
PathLike
|
Path to gro file |
required |
frame
|
Any
|
Optional existing Frame to populate |
None
|
Returns:
| Type | Description |
|---|---|
Any
|
Populated Frame object |
Source code in src/molpy/io/readers.py
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 | |
read_h5 ¶
read_h5(file, frame=None)
Read HDF5 file and return a Frame object.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
file
|
PathLike
|
Path to HDF5 file |
required |
frame
|
Any
|
Optional existing Frame to populate |
None
|
Returns:
| Type | Description |
|---|---|
Any
|
Populated Frame object |
Examples:
>>> frame = read_h5("structure.h5")
>>> frame["atoms"]["x"]
array([0., 1., 2.])
Source code in src/molpy/io/readers.py
189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 | |
read_h5_trajectory ¶
read_h5_trajectory(file)
Read HDF5 trajectory file and return a trajectory reader.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
file
|
PathLike
|
Path to HDF5 trajectory file |
required |
Returns:
| Type | Description |
|---|---|
Any
|
HDF5TrajectoryReader object |
Examples:
>>> reader = read_h5_trajectory("trajectory.h5")
>>> frame = reader.read_frame(0)
>>> for frame in reader:
... process(frame)
Source code in src/molpy/io/readers.py
339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 | |
read_lammps_data ¶
read_lammps_data(file, atom_style, frame=None)
Read LAMMPS data file and return a Frame object.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
file
|
PathLike
|
Path to LAMMPS data file |
required |
atom_style
|
str
|
LAMMPS atom style (e.g., 'full', 'atomic') |
required |
frame
|
Any
|
Optional existing Frame to populate |
None
|
Returns:
| Type | Description |
|---|---|
Any
|
Populated Frame object |
Source code in src/molpy/io/readers.py
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 | |
read_lammps_forcefield ¶
read_lammps_forcefield(scripts, forcefield=None)
Read LAMMPS force field file and return a ForceField object.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
scripts
|
PathLike | list[PathLike]
|
Path or list of paths to LAMMPS force field scripts |
required |
forcefield
|
Any
|
Optional existing ForceField to populate |
None
|
Returns:
| Type | Description |
|---|---|
Any
|
Populated ForceField object |
Source code in src/molpy/io/readers.py
217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 | |
read_lammps_molecule ¶
read_lammps_molecule(file, frame=None)
Read LAMMPS molecule file and return a Frame object.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
file
|
PathLike
|
Path to LAMMPS molecule file |
required |
frame
|
Any
|
Optional existing Frame to populate |
None
|
Returns:
| Type | Description |
|---|---|
Any
|
Populated Frame object |
Source code in src/molpy/io/readers.py
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 | |
read_lammps_trajectory ¶
read_lammps_trajectory(traj, frame=None)
Read LAMMPS trajectory file and return a trajectory reader.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
traj
|
PathLike
|
Path to LAMMPS trajectory file |
required |
frame
|
Any
|
Optional reference Frame for topology |
None
|
Returns:
| Type | Description |
|---|---|
Any
|
LammpsTrajectoryReader object |
Source code in src/molpy/io/readers.py
308 309 310 311 312 313 314 315 316 317 318 319 320 321 | |
read_mol2 ¶
read_mol2(file, frame=None)
Read mol2 file and return a Frame object.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
file
|
PathLike
|
Path to mol2 file |
required |
frame
|
Any
|
Optional existing Frame to populate |
None
|
Returns:
| Type | Description |
|---|---|
Any
|
Populated Frame object |
Source code in src/molpy/io/readers.py
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 | |
read_pdb ¶
read_pdb(file, frame=None)
Read PDB file and return a Frame object.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
file
|
PathLike
|
Path to PDB file |
required |
frame
|
Any
|
Optional existing Frame to populate |
None
|
Returns:
| Type | Description |
|---|---|
Any
|
Populated Frame object |
Source code in src/molpy/io/readers.py
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 | |
read_top ¶
read_top(file, forcefield=None)
Read GROMACS topology file and return a ForceField object.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
file
|
PathLike
|
Path to GROMACS .top file |
required |
forcefield
|
Any
|
Optional existing ForceField to populate |
None
|
Returns:
| Type | Description |
|---|---|
Any
|
Populated ForceField object |
Source code in src/molpy/io/readers.py
281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 | |
read_xml_forcefield ¶
read_xml_forcefield(file)
Read XML force field file and return a ForceField object.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
file
|
PathLike
|
Path to XML force field file |
required |
Returns:
| Type | Description |
|---|---|
Any
|
ForceField object |
Source code in src/molpy/io/readers.py
236 237 238 239 240 241 242 243 244 245 246 247 248 | |
read_xsf ¶
read_xsf(file, frame=None)
Read XSF file and return a Frame object.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
file
|
PathLike
|
Path to XSF file |
required |
frame
|
Any
|
Optional existing Frame to populate |
None
|
Returns:
| Type | Description |
|---|---|
Any
|
Populated Frame object |
Source code in src/molpy/io/readers.py
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 | |
read_xyz ¶
read_xyz(file, frame=None)
Read XYZ file and return a Frame object.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
file
|
PathLike
|
Path to XYZ file |
required |
frame
|
Any
|
Optional existing Frame to populate |
None
|
Returns:
| Type | Description |
|---|---|
Any
|
Populated Frame object |
Source code in src/molpy/io/readers.py
171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 | |
read_xyz_trajectory ¶
read_xyz_trajectory(file)
Read XYZ trajectory file and return a trajectory reader.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
file
|
PathLike
|
Path to XYZ trajectory file |
required |
Returns:
| Type | Description |
|---|---|
Any
|
XYZTrajectoryReader object |
Source code in src/molpy/io/readers.py
324 325 326 327 328 329 330 331 332 333 334 335 336 | |
Data file writer factory functions.
This module provides convenient factory functions for creating various data file writers. All functions write Frame or ForceField objects to files.
write_gro ¶
write_gro(file, frame)
Write a Frame object to a GROMACS GRO file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
file
|
PathLike
|
Output file path |
required |
frame
|
Any
|
Frame object to write |
required |
Source code in src/molpy/io/writers.py
48 49 50 51 52 53 54 55 56 57 58 59 | |
write_h5 ¶
write_h5(file, frame, compression='gzip', compression_opts=4)
Write a Frame object to an HDF5 file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
file
|
PathLike
|
Output file path |
required |
frame
|
Any
|
Frame object to write |
required |
compression
|
str | None
|
Compression algorithm (None, 'gzip', 'lzf', 'szip'). Defaults to 'gzip'. |
'gzip'
|
compression_opts
|
int
|
Compression level (for gzip: 0-9). Defaults to 4. |
4
|
Examples:
>>> frame = Frame(blocks={"atoms": {"x": [0, 1, 2], "y": [0, 0, 0]}})
>>> write_h5("structure.h5", frame)
Source code in src/molpy/io/writers.py
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 | |
write_h5_trajectory ¶
write_h5_trajectory(file, frames, compression='gzip', compression_opts=4)
Write frames to an HDF5 trajectory file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
file
|
PathLike
|
Output file path |
required |
frames
|
list
|
List of Frame objects to write |
required |
compression
|
str | None
|
Compression algorithm (None, 'gzip', 'lzf', 'szip'). Defaults to 'gzip'. |
'gzip'
|
compression_opts
|
int
|
Compression level (for gzip: 0-9). Defaults to 4. |
4
|
Examples:
>>> frames = [frame0, frame1, frame2]
>>> write_h5_trajectory("trajectory.h5", frames)
Source code in src/molpy/io/writers.py
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 | |
write_lammps_data ¶
write_lammps_data(file, frame, atom_style='full')
Write a Frame object to a LAMMPS data file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
file
|
PathLike
|
Output file path |
required |
frame
|
Any
|
Frame object to write |
required |
atom_style
|
str
|
LAMMPS atom style (default: 'full') |
'full'
|
Source code in src/molpy/io/writers.py
19 20 21 22 23 24 25 26 27 28 29 30 31 | |
write_lammps_forcefield ¶
write_lammps_forcefield(file, forcefield, precision=6)
Write a ForceField object to a LAMMPS force field file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
file
|
PathLike
|
Output file path |
required |
forcefield
|
Any
|
ForceField object to write |
required |
precision
|
int
|
Number of decimal places for floating point values |
6
|
Source code in src/molpy/io/writers.py
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 | |
write_lammps_molecule ¶
write_lammps_molecule(file, frame, format_type='native')
Write a Frame object to a LAMMPS molecule file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
file
|
PathLike
|
Output file path |
required |
frame
|
Any
|
Frame object to write |
required |
format_type
|
str
|
Format type (default: 'native') |
'native'
|
Source code in src/molpy/io/writers.py
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 | |
write_lammps_system ¶
write_lammps_system(workdir, frame, forcefield)
Write a complete LAMMPS system (data + forcefield) to a directory.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
workdir
|
PathLike
|
Output directory path |
required |
frame
|
Any
|
Frame object containing structure |
required |
forcefield
|
Any
|
ForceField object containing parameters |
required |
Source code in src/molpy/io/writers.py
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 | |
write_lammps_trajectory ¶
write_lammps_trajectory(file, frames, atom_style='full')
Write frames to a LAMMPS trajectory file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
file
|
PathLike
|
Output file path |
required |
frames
|
list
|
List of Frame objects to write |
required |
atom_style
|
str
|
LAMMPS atom style (default: 'full') |
'full'
|
Source code in src/molpy/io/writers.py
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 | |
write_pdb ¶
write_pdb(file, frame)
Write a Frame object to a PDB file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
file
|
PathLike
|
Output file path |
required |
frame
|
Any
|
Frame object to write |
required |
Source code in src/molpy/io/writers.py
34 35 36 37 38 39 40 41 42 43 44 45 | |
write_xsf ¶
write_xsf(file, frame)
Write a Frame object to an XSF file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
file
|
PathLike
|
Output file path |
required |
frame
|
Any
|
Frame object to write |
required |
Source code in src/molpy/io/writers.py
62 63 64 65 66 67 68 69 70 71 72 73 | |
write_xyz_trajectory ¶
write_xyz_trajectory(file, frames)
Write frames to an XYZ trajectory file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
file
|
PathLike
|
Output file path |
required |
frames
|
list
|
List of Frame objects to write |
required |
Source code in src/molpy/io/writers.py
167 168 169 170 171 172 173 174 175 176 177 178 179 | |
Data Modules¶
Base¶
DataReader ¶
DataReader(path, **open_kwargs)
Bases: FileBase, ABC
Base class for data file readers.
Source code in src/molpy/io/data/base.py
47 48 | |
read
abstractmethod
¶
read(frame=None)
Populate / update a Frame from the underlying file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
frame
|
Frame | None
|
Optional existing Frame to populate. If None, creates a new one. |
None
|
Returns:
| Type | Description |
|---|---|
Frame
|
The populated Frame object |
Source code in src/molpy/io/data/base.py
68 69 70 71 72 73 74 75 76 77 78 79 | |
read_lines ¶
read_lines()
Return all lines at once.
Source code in src/molpy/io/data/base.py
63 64 65 | |
DataWriter ¶
DataWriter(path, **open_kwargs)
Bases: FileBase, ABC
Base class for data file writers.
Source code in src/molpy/io/data/base.py
88 89 | |
write
abstractmethod
¶
write(frame)
Serialize frame into the underlying file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
frame
|
Frame
|
Frame object to write |
required |
Source code in src/molpy/io/data/base.py
91 92 93 94 95 96 97 98 99 | |
FileBase ¶
FileBase(path, mode, **open_kwargs)
Bases: ABC
Common logic for Context-manager + lazy file handle.
Source code in src/molpy/io/data/base.py
17 18 19 20 21 | |
AC¶
Antechamber (AC) file format reader.
Reads Antechamber .ac files containing atom and bond information with force field types and charges.
AcReader ¶
AcReader(file)
Bases: DataReader
Reader for Antechamber .ac format files.
Parses ATOM and BOND sections from Antechamber output files, extracting coordinates, charges, atom types, and connectivity.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
file
|
str | Path
|
Path to .ac file |
required |
Source code in src/molpy/io/data/ac.py
26 27 28 | |
assign_atomic_numbers ¶
assign_atomic_numbers(atoms)
Assign atomic numbers by guessing from atom names/types.
Source code in src/molpy/io/data/ac.py
109 110 111 112 113 114 115 116 117 | |
read ¶
read(frame)
Read .ac file and populate Frame with atoms and bonds.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
frame
|
Frame
|
Frame to populate |
required |
Returns:
| Type | Description |
|---|---|
Frame
|
Frame with atoms and bonds data |
Source code in src/molpy/io/data/ac.py
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 | |
Amber¶
AmberInpcrdReader ¶
AmberInpcrdReader(file, **kwargs)
Bases: DataReader
Reader for AMBER ASCII *.inpcrd (old-style) coordinate files.
- Coordinates: 12.7/12.8 format, 6 numbers per line
- Optional velocities section (same length as coordinates)
- Optional final box line (3-6 floats)
Source code in src/molpy/io/data/amber.py
36 37 | |
GRO¶
GroReader ¶
GroReader(file)
Bases: DataReader
Robust GROMACS GRO file reader.
Features: - Parses GRO format atoms with proper field extraction - Handles box information (orthogonal and triclinic) - Robust error handling for malformed files - Proper coordinate precision preservation
Source code in src/molpy/io/data/gro.py
24 25 26 | |
assign_numbers ¶
assign_numbers(atoms)
Assign atomic numbers based on atom names.
Source code in src/molpy/io/data/gro.py
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 | |
read ¶
read(frame=None)
Read GRO file and populate frame.
Source code in src/molpy/io/data/gro.py
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 | |
sanitizer
staticmethod
¶
sanitizer(line)
Clean line while preserving GRO format.
Source code in src/molpy/io/data/gro.py
28 29 30 31 | |
GroWriter ¶
GroWriter(path)
Bases: DataWriter
GROMACS GRO file writer.
Features: - Writes properly formatted GRO files - Handles orthogonal and triclinic boxes - Supports velocity information - Maintains precision for coordinates
Source code in src/molpy/io/data/gro.py
265 266 267 | |
write ¶
write(frame)
Write frame to GRO file.
Source code in src/molpy/io/data/gro.py
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 | |
H5¶
HDF5 file format support for Frame objects.
This module provides reading and writing of Frame objects to/from HDF5 format using h5py. The HDF5 format is efficient for storing large molecular datasets and supports compression and chunking.
HDF5 Structure:¶
/ # Root group ├── blocks/ # Group containing all data blocks │ ├── atoms/ # Block group (e.g., "atoms") │ │ ├── x # Dataset (variable) │ │ ├── y # Dataset │ │ └── z # Dataset │ └── bonds/ # Another block group │ ├── i # Dataset │ └── j # Dataset └── metadata/ # Group containing metadata ├── timestep # Attribute or dataset └── ... # Other metadata
HDF5Reader ¶
HDF5Reader(path, **open_kwargs)
Read Frame objects from HDF5 files.
The HDF5 file structure should follow the format: - /blocks/{block_name}/{variable_name} for data arrays - /metadata/ for frame metadata
Examples:
>>> reader = HDF5Reader("frame.h5")
>>> frame = reader.read()
>>> frame["atoms"]["x"]
array([0., 1., 2.])
Initialize HDF5 reader.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
PathLike
|
Path to HDF5 file |
required |
**open_kwargs
|
Any
|
Additional arguments passed to h5py.File |
{}
|
Source code in src/molpy/io/data/h5.py
396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 | |
read ¶
read(frame=None)
Read Frame from HDF5 file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
frame
|
Frame | None
|
Optional existing Frame to populate. If None, creates a new one. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
Frame |
Frame
|
Populated Frame object with blocks and metadata from HDF5 file. |
Source code in src/molpy/io/data/h5.py
423 424 425 426 427 428 429 430 431 432 433 | |
HDF5Writer ¶
HDF5Writer(path, compression='gzip', compression_opts=4, **open_kwargs)
Write Frame objects to HDF5 files.
The HDF5 file structure follows: - /blocks/{block_name}/{variable_name} for data arrays - /metadata/ for frame metadata
Examples:
>>> frame = Frame(blocks={"atoms": {"x": [0, 1, 2], "y": [0, 0, 0]}})
>>> writer = HDF5Writer("frame.h5")
>>> writer.write(frame)
Initialize HDF5 writer.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
PathLike
|
Path to output HDF5 file |
required |
compression
|
str | None
|
Compression algorithm (None, 'gzip', 'lzf', 'szip'). Defaults to 'gzip'. |
'gzip'
|
compression_opts
|
int
|
Compression level (for gzip: 0-9). Defaults to 4. |
4
|
**open_kwargs
|
Any
|
Additional arguments passed to h5py.File |
{}
|
Source code in src/molpy/io/data/h5.py
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 | |
write ¶
write(frame)
Write Frame to HDF5 file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
frame
|
Frame
|
Frame object to write. |
required |
Raises:
| Type | Description |
|---|---|
ValueError
|
If frame is empty (no blocks). |
Source code in src/molpy/io/data/h5.py
487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 | |
frame_to_h5_group ¶
frame_to_h5_group(frame, h5_group, compression='gzip', compression_opts=4)
Write a Frame to an HDF5 group.
This function can be used to write a Frame to any HDF5 group, making it reusable for both single Frame files and trajectory files.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
frame
|
Frame
|
Frame object to write |
required |
h5_group
|
Group
|
HDF5 group to write to |
required |
compression
|
str | None
|
Compression algorithm (None, 'gzip', 'lzf', 'szip'). Defaults to 'gzip'. |
'gzip'
|
compression_opts
|
int
|
Compression level (for gzip: 0-9). Defaults to 4. |
4
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If frame is empty (no blocks). |
Source code in src/molpy/io/data/h5.py
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 | |
h5_group_to_frame ¶
h5_group_to_frame(h5_group, frame=None)
Read a Frame from an HDF5 group.
This function can be used to read a Frame from any HDF5 group, making it reusable for both single Frame files and trajectory files.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
h5_group
|
Group
|
HDF5 group to read from |
required |
frame
|
Frame | None
|
Optional existing Frame to populate. If None, creates a new one. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
Frame |
Frame
|
Populated Frame object with blocks and metadata from HDF5 group. |
Source code in src/molpy/io/data/h5.py
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 | |
LAMMPS¶
Modern LAMMPS data file I/O using Block.from_csv.
This module provides a clean, imperative approach to reading and writing LAMMPS data files using the Block.from_csv functionality.
LammpsDataReader ¶
LammpsDataReader(path, atom_style='full')
Bases: DataReader
Modern LAMMPS data file reader using Block.from_csv.
Source code in src/molpy/io/data/lammps.py
26 27 28 | |
read ¶
read(frame=None)
Read LAMMPS data file into a Frame.
Source code in src/molpy/io/data/lammps.py
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 | |
LammpsDataWriter ¶
LammpsDataWriter(path, atom_style='full')
Bases: DataWriter
Modern LAMMPS data file writer using Block.to_csv approach.
Important Requirements: - Atoms in the frame must have an 'id' field. This field is required to map atom indices to atom IDs for LAMMPS output. - Connectivity data (bonds, angles, dihedrals) in the frame uses atom indices (0-based from to_frame()). The writer automatically converts these indices to atom IDs using the index->ID mapping from the atoms 'id' field.
Frame Structure: - Atoms: Must include 'id' field. Other required fields depend on atom_style. - Bonds/Angles/Dihedrals: Use atom indices in 'atomi', 'atomj', 'atomk', 'atoml' (from to_frame()). These are 0-based indices that will be converted to 1-based atom IDs.
Source code in src/molpy/io/data/lammps.py
511 512 513 | |
write ¶
write(frame)
Write Frame to LAMMPS data file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
frame
|
Frame
|
Frame containing atoms and optionally bonds/angles/dihedrals. Atoms must have 'id' field. |
required |
Raises:
| Type | Description |
|---|---|
ValueError
|
If atoms are missing 'id' field. |
Source code in src/molpy/io/data/lammps.py
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 574 | |
LAMMPS molecule file I/O.
This module provides readers and writers for LAMMPS molecule template files, supporting both native format and JSON format as described in the LAMMPS documentation.
LammpsMoleculeReader ¶
LammpsMoleculeReader(path)
Bases: DataReader
LAMMPS molecule file reader supporting both native and JSON formats.
Source code in src/molpy/io/data/lammps_molecule.py
23 24 25 | |
read ¶
read(frame=None)
Read LAMMPS molecule file into a Frame.
Source code in src/molpy/io/data/lammps_molecule.py
27 28 29 30 31 32 33 34 | |
LammpsMoleculeWriter ¶
LammpsMoleculeWriter(path, format_type='native')
Bases: DataWriter
LAMMPS molecule file writer supporting both native and JSON formats.
Source code in src/molpy/io/data/lammps_molecule.py
619 620 621 622 623 624 625 626 627 | |
write ¶
write(frame)
Write Frame to LAMMPS molecule file.
Source code in src/molpy/io/data/lammps_molecule.py
629 630 631 632 633 634 | |
Mol2¶
Mol2Reader ¶
Mol2Reader(file)
Bases: DataReader
Robust MOL2 file reader following TRIPOS MOL2 format specification.
Features: - Parses MOLECULE, ATOM, BOND, and SUBSTRUCTURE sections - Handles various atom types and bond types - Robust error handling for malformed files - Supports partial files with missing sections - Assigns atomic numbers from atom names/types
Source code in src/molpy/io/data/mol2.py
23 24 25 26 | |
read ¶
read(frame)
Read MOL2 file and populate frame.
Source code in src/molpy/io/data/mol2.py
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 | |
sanitizer
staticmethod
¶
sanitizer(line)
Clean up line by stripping whitespace.
Source code in src/molpy/io/data/mol2.py
28 29 30 31 | |
PDB¶
PDBReader ¶
PDBReader(file, **kwargs)
Bases: DataReader
Minimal-yet-robust PDB reader.
- ATOM / HETATM parsed per PDB v3.3 fixed columns
- CRYST1 -> frame.box
- CONECT -> bond list
Source code in src/molpy/io/data/pdb.py
93 94 | |
PDBWriter ¶
PDBWriter(path)
Bases: DataWriter
Robust PDB file writer that creates properly formatted PDB files.
Features: - Writes ATOM/HETATM records with proper formatting - Handles missing fields with sensible defaults - Writes CRYST1 records from box information - Writes CONECT records for bonds - Ensures PDB format compliance
Source code in src/molpy/io/data/pdb.py
235 236 | |
write ¶
write(frame)
Write frame to PDB file.
Required fields in frame["atoms"]: - x, y, z: coordinates (float, required) - id: atom ID (int, optional, defaults to index+1)
Optional fields in frame["atoms"]: - name: atom name (str) - resName: residue name (str) - element: element symbol (str) - resSeq: residue sequence number (int) - chainID: chain identifier (str) - occupancy: occupancy (float) - tempFactor: temperature factor (float)
Optional metadata: - elements: space-separated string of element symbols (one per atom) - name: frame name (str) - box: Box object for CRYST1 record
Raises:
| Type | Description |
|---|---|
ValueError
|
If required fields (x, y, z) are missing or contain None |
Source code in src/molpy/io/data/pdb.py
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 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 | |
Top¶
GROMACS topology file reader for Frame objects.
This module provides a reader for GROMACS topology files that extracts structural information (atoms, bonds, angles, dihedrals, pairs) and creates Frame objects with Block containers.
TopReader ¶
TopReader(file, **open_kwargs)
Bases: DataReader
Read GROMACS topology files and create Frame objects.
This reader parses GROMACS .top files and extracts structural information from sections like [atoms], [bonds], [angles], [dihedrals], and [pairs].
Examples:
>>> reader = TopReader("molecule.top")
>>> frame = reader.read()
>>> frame["atoms"] # Block with atom data
>>> frame["bonds"] # Block with bond data
Initialize GROMACS topology reader.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
file
|
PathLike
|
Path to GROMACS .top file |
required |
**open_kwargs
|
Additional arguments passed to file open |
{}
|
Source code in src/molpy/io/data/top.py
33 34 35 36 37 38 39 40 41 | |
read ¶
read(frame=None)
Read GROMACS topology file and populate Frame.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
frame
|
Frame | None
|
Optional existing Frame to populate. If None, creates a new one. |
None
|
Returns:
| Type | Description |
|---|---|
Frame
|
Frame object with atoms, bonds, angles, dihedrals, and pairs blocks. |
Source code in src/molpy/io/data/top.py
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 | |
XSF¶
XSF (XCrySDen Structure File) format reader and writer.
XSF is a format for crystal structure visualization, supporting both periodic and non-periodic structures. It can contain atomic coordinates, unit cell parameters, and other structural information.
XsfReader ¶
XsfReader(file)
Bases: DataReader
Parse an XSF file into a Frame.
XSF format supports both crystal structures (with unit cell) and molecular structures. The format can contain: - CRYSTAL or MOLECULE keyword - PRIMVEC or CONVVEC for unit cell vectors - PRIMCOORD for atomic coordinates - Optional comment lines starting with #
Source code in src/molpy/io/data/xsf.py
33 34 35 | |
read ¶
read(frame=None)
Read XSF file and return Frame.
Returns¶
Frame Frame with: - frame containing atomic data - box: unit cell for CRYSTAL, Free Box for MOLECULE
Source code in src/molpy/io/data/xsf.py
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 | |
XsfWriter ¶
XsfWriter(file)
Bases: DataWriter
Write Frame to XSF format.
Features: - Supports both CRYSTAL and MOLECULE structures - Writes PRIMVEC for unit cell vectors - Writes PRIMCOORD for atomic coordinates - Automatically determines structure type based on presence of box
Source code in src/molpy/io/data/xsf.py
200 201 202 | |
write ¶
write(frame)
Write Frame to XSF file.
Parameters¶
frame : Frame Frame containing atomic data and optional box information
Source code in src/molpy/io/data/xsf.py
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 | |
XYZ¶
XYZReader ¶
XYZReader(path, **open_kwargs)
Bases: DataReader
Parse an XYZ file (single model) into an :class:Frame.
Supports both standard XYZ and extended XYZ (extxyz) formats.
Standard XYZ Format¶
1. integer `N` - number of atoms
2. comment line - stored in frame.metadata
3. N lines: `symbol x y z`
Extended XYZ Format¶
1. integer `N` - number of atoms
2. comment line with key=value pairs (e.g., Properties=species:S:1:pos:R:3)
3. N lines with columns defined by Properties specification
Source code in src/molpy/io/data/base.py
47 48 | |
read ¶
read(frame=None)
Parameters¶
frame Optional frame to populate; if None, a new one is created.
Returns¶
Frame
Frame with:
* block "atoms":
- element -> (N,) y -> (N,) float array
- z -> (N,) float array
- number -> (N,) int array (atomic numbers)
- additional columns from Properties if extxyz
* metadata from comment line
Source code in src/molpy/io/data/xyz.py
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 | |
Trajectory Modules¶
Base¶
BaseTrajectoryReader ¶
BaseTrajectoryReader(fpath)
Bases: ABC, Iterable['Frame']
Base class for trajectory file readers that act as lazy-loading iterators.
This class provides memory-mapped file reading and directly returns Frame objects without loading everything into memory. Supports reading from multiple files.
Implements Iterable[Frame] for lazy iteration over frames.
Initialize the trajectory reader.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
fpath
|
PathLike | list[PathLike]
|
Path to trajectory file or list of paths to multiple trajectory files |
required |
Source code in src/molpy/io/trajectory/base.py
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 | |
close ¶
close()
Close all memory-mapped files.
Source code in src/molpy/io/trajectory/base.py
70 71 72 73 74 75 | |
read_all ¶
read_all()
Read all frames from the trajectory file.
Source code in src/molpy/io/trajectory/base.py
143 144 145 | |
read_frame ¶
read_frame(index)
Read a specific frame from the trajectory file(s).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
index
|
int
|
Global frame index to read |
required |
Returns:
| Type | Description |
|---|---|
Frame
|
The Frame object |
Source code in src/molpy/io/trajectory/base.py
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 | |
read_frames ¶
read_frames(indices)
Read multiple frames from the trajectory file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
indices
|
list[int]
|
list of frame indices to read |
required |
Returns:
| Type | Description |
|---|---|
list[Frame]
|
list of Frame objects |
Source code in src/molpy/io/trajectory/base.py
116 117 118 119 120 121 122 123 124 125 126 | |
read_range ¶
read_range(start, stop, step=1)
Read a range of frames from the trajectory file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
start
|
int
|
Starting frame index |
required |
stop
|
int
|
Stopping frame index (exclusive) |
required |
step
|
int
|
Step size |
1
|
Returns:
| Type | Description |
|---|---|
list[Frame]
|
list of Frame objects |
Source code in src/molpy/io/trajectory/base.py
128 129 130 131 132 133 134 135 136 137 138 139 140 141 | |
FrameLocation ¶
Bases: NamedTuple
Location information for a frame.
TrajectoryWriter ¶
TrajectoryWriter(fpath)
Bases: ABC
Base class for all trajectory file writers.
Source code in src/molpy/io/trajectory/base.py
340 341 342 | |
write_frame
abstractmethod
¶
write_frame(frame)
Write a single frame to the file.
Source code in src/molpy/io/trajectory/base.py
350 351 352 353 | |
H5¶
HDF5 trajectory file format support.
This module provides reading and writing of Trajectory objects to/from HDF5 format using h5py. The HDF5 format is efficient for storing large trajectory datasets and supports compression and chunking.
HDF5 Trajectory Structure:¶
/ # Root group ├── frames/ # Group containing all frames │ ├── 0/ # Frame 0 │ │ ├── blocks/ # Data blocks (same structure as single Frame) │ │ └── metadata/ # Frame metadata │ ├── 1/ # Frame 1 │ │ ├── blocks/ │ │ └── metadata/ │ └── ... ├── n_frames # Attribute: total number of frames └── metadata/ # Optional trajectory-level metadata
HDF5TrajectoryReader ¶
HDF5TrajectoryReader(path, **open_kwargs)
Read Trajectory objects from HDF5 files.
The HDF5 file structure should follow: - /frames/{frame_index}/blocks/ for data blocks - /frames/{frame_index}/metadata/ for frame metadata - /n_frames attribute for total frame count
Examples:
>>> reader = HDF5TrajectoryReader("trajectory.h5")
>>> frame = reader.read_frame(0)
>>> for frame in reader:
... process(frame)
Initialize HDF5 trajectory reader.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
PathLike
|
Path to HDF5 trajectory file |
required |
**open_kwargs
|
Additional arguments passed to h5py.File |
{}
|
Source code in src/molpy/io/trajectory/h5.py
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 | |
read_frame ¶
read_frame(index)
Read a specific frame from the trajectory.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
index
|
int
|
Frame index (0-based) |
required |
Returns:
| Type | Description |
|---|---|
Frame
|
Frame object |
Raises:
| Type | Description |
|---|---|
IndexError
|
If index is out of range |
Source code in src/molpy/io/trajectory/h5.py
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 | |
HDF5TrajectoryWriter ¶
HDF5TrajectoryWriter(path, compression='gzip', compression_opts=4, **open_kwargs)
Bases: TrajectoryWriter
Write Trajectory objects to HDF5 files.
The HDF5 file structure follows: - /frames/{frame_index}/blocks/ for data blocks - /frames/{frame_index}/metadata/ for frame metadata - /n_frames attribute for total frame count
Examples:
>>> writer = HDF5TrajectoryWriter("trajectory.h5")
>>> writer.write_frame(frame0)
>>> writer.write_frame(frame1)
>>> writer.close()
Initialize HDF5 trajectory writer.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
PathLike
|
Path to output HDF5 file |
required |
compression
|
str | None
|
Compression algorithm (None, 'gzip', 'lzf', 'szip'). Defaults to 'gzip'. |
'gzip'
|
compression_opts
|
int
|
Compression level (for gzip: 0-9). Defaults to 4. |
4
|
**open_kwargs
|
Additional arguments passed to h5py.File |
{}
|
Source code in src/molpy/io/trajectory/h5.py
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 | |
close ¶
close()
Close the HDF5 file.
Source code in src/molpy/io/trajectory/h5.py
308 309 310 311 312 | |
write_frame ¶
write_frame(frame)
Write a single frame to the trajectory file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
frame
|
Frame
|
Frame object to write |
required |
Source code in src/molpy/io/trajectory/h5.py
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 | |
LAMMPS¶
LammpsTrajectoryReader ¶
LammpsTrajectoryReader(fpath, frame=None, extra_type_mappings=None)
Bases: BaseTrajectoryReader
Reader for LAMMPS trajectory files, supporting multiple files.
Source code in src/molpy/io/trajectory/lammps.py
53 54 55 56 57 58 59 60 61 62 63 | |
LammpsTrajectoryWriter ¶
LammpsTrajectoryWriter(fpath, atom_style='full')
Bases: TrajectoryWriter
Writer for LAMMPS trajectory files.
Source code in src/molpy/io/trajectory/lammps.py
178 179 180 | |
write_frame ¶
write_frame(frame, timestep=None)
Write a single frame to the file.
Source code in src/molpy/io/trajectory/lammps.py
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 | |
XYZ¶
XYZTrajectoryReader ¶
XYZTrajectoryReader(fpath)
Bases: BaseTrajectoryReader
Reader for XYZ trajectory files.
Source code in src/molpy/io/trajectory/base.py
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 | |
XYZTrajectoryWriter ¶
XYZTrajectoryWriter(fpath)
Bases: TrajectoryWriter
Writer for XYZ trajectory files.
Source code in src/molpy/io/trajectory/xyz.py
161 162 163 | |
close ¶
close()
Close the file.
Source code in src/molpy/io/trajectory/xyz.py
196 197 198 199 | |
write_frame ¶
write_frame(frame)
Write a single frame to the XYZ file.
Source code in src/molpy/io/trajectory/xyz.py
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 | |
write_traj ¶
write_traj(trajectory)
Write multiple frames to the XYZ file.
Source code in src/molpy/io/trajectory/xyz.py
191 192 193 194 | |