molpy.io¶
MolPy IO Module - Unified interface for molecular file I/O.
This module provides a clean, organized interface for reading and writing various molecular file formats. It supports: - Data files (PDB, XYZ, LAMMPS, GROMACS, AMBER, etc.) - Force field files (LAMMPS, XML, AMBER prmtop, GROMACS top) - Trajectory files (LAMMPS dump, XYZ)
Design Principles:¶
- Reader/Writer Pattern: Each format has dedicated Reader and Writer classes
- Factory Functions: Convenient read_xxx/write_xxx functions for simple usage
- Lazy Imports: Dependencies loaded only when needed
- Unified Interface: All readers have read(), all writers have write()
Basic Usage:¶
# Reading data files
from molpy.io import read_pdb, read_lammps_data
frame = read_pdb("structure.pdb")
frame = read_lammps_data("data.lammps", atom_style="full")
# Writing data files
from molpy.io import write_pdb, write_lammps_data
write_pdb("output.pdb", frame)
write_lammps_data("output.data", frame, atom_style="full")
# Reading force fields
from molpy.io import read_xml_forcefield, read_lammps_forcefield
ff = read_xml_forcefield("oplsaa.xml")
ff = read_lammps_forcefield("forcefield.in")
# Reading trajectories
from molpy.io import read_lammps_trajectory, read_xyz_trajectory
traj = read_lammps_trajectory("dump.lammpstrj")
for frame in traj:
process(frame)
read_amber ¶
read_amber(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
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 | |
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_system ¶
read_amber_system(prmtop, inpcrd=None, system=None)
Read AMBER prmtop and optional inpcrd files (legacy function).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
prmtop
|
PathLike
|
Path to AMBER prmtop file |
required |
inpcrd
|
PathLike | None
|
Optional path to AMBER inpcrd file |
None
|
system
|
Any
|
Optional FrameSystem (unused, kept for compatibility) |
None
|
Returns:
| Type | Description |
|---|---|
Any
|
Tuple of (Frame, ForceField) |
Note
For new code, prefer using read_amber() directly.
Source code in src/molpy/io/__init__.py
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 | |
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_gromacs_system ¶
read_gromacs_system(gro_file, top_file=None, system=None)
Read GROMACS structure and optional topology files.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
gro_file
|
PathLike
|
Path to GROMACS .gro file |
required |
top_file
|
PathLike | None
|
Optional path to GROMACS .top file |
None
|
system
|
Any
|
Optional FrameSystem (unused, kept for compatibility) |
None
|
Returns:
| Type | Description |
|---|---|
Any
|
Frame if no topology, or tuple of (Frame, ForceField) if topology provided |
Note
For new code, prefer using read_gro() and read_top() separately.
Source code in src/molpy/io/__init__.py
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 | |
read_lammps ¶
read_lammps(data, scripts=None, frame=None, atomstyle='full')
Read LAMMPS data and optional force field files.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
PathLike
|
Path to LAMMPS data file |
required |
scripts
|
PathLike | list[PathLike] | None
|
Optional path(s) to LAMMPS force field scripts |
None
|
frame
|
Any
|
Optional existing Frame to populate |
None
|
atomstyle
|
str
|
LAMMPS atom style (default: 'full') |
'full'
|
Returns:
| Type | Description |
|---|---|
Any
|
Frame object (force field is loaded but returned separately if needed) |
Note
For new code, prefer using read_lammps_data() and read_lammps_forcefield() separately.
Source code in src/molpy/io/__init__.py
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 | |
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
194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 | |
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
285 286 287 288 289 290 291 292 293 294 295 296 297 298 | |
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
258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 | |
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
213 214 215 216 217 218 219 220 221 222 223 224 225 | |
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
301 302 303 304 305 306 307 308 309 310 311 312 313 | |
write_lammps ¶
write_lammps(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
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 | |
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
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 | |
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
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 | |
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
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 | |
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
48 49 50 51 52 53 54 55 56 57 58 59 | |
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
125 126 127 128 129 130 131 132 133 134 135 136 137 | |