Skip to content

Commit

Permalink
fix: catch zero volume PDB box and warn
Browse files Browse the repository at this point in the history
  • Loading branch information
jag1g13 committed Aug 13, 2021
1 parent ba0e5ab commit 61a5b80
Show file tree
Hide file tree
Showing 3 changed files with 704 additions and 6 deletions.
28 changes: 25 additions & 3 deletions pycgtool/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,29 @@ def __init__(self, msg=None):
super(NonMatchingSystemError, self).__init__(msg)


def try_load_traj(filepath: PathLike, **kwargs) -> mdtraj.Trajectory:
"""Load a trajectory, if a PDB fails with zero box volume disable volume check and try again."""
filepath = pathlib.Path(filepath)

try:
return mdtraj.load(str(filepath), **kwargs)

except FloatingPointError:
# PDB file loading checks density using the simulation box
# This can fail if the box volume is zero
if filepath.suffix.lower() == '.pdb':
kwargs.pop('top', None) # Can't specify a topology for `load_pdb`
trajectory = mdtraj.load_pdb(str(filepath), no_boxchk=True, **kwargs)
logger.warning(
'Unitcell has zero volume - periodic boundaries will not be accounted for. '
'If the molecule is split by a periodic boundary, results will be incorrect.'
)

return trajectory

raise


class Frame:
"""Load and store data from a simulation trajectory."""
def __init__(self,
Expand All @@ -51,15 +74,14 @@ def __init__(self,
if topology_file is not None:
try:
logging.info('Loading topology file')
self._trajectory = mdtraj.load(str(topology_file))
self._trajectory = try_load_traj(topology_file)
self._topology = self._trajectory.topology
logging.info('Finished loading topology file')

if trajectory_file is not None:
try:
logging.info('Loading trajectory file - this may take a while')
self._trajectory = mdtraj.load(str(trajectory_file),
top=self._topology)
self._trajectory = try_load_traj(trajectory_file, top=self._topology)
self._trajectory = self._slice_trajectory(frame_start, frame_end)
logging.info(
'Finished loading trajectory file - loaded %d frames',
Expand Down
Loading

0 comments on commit 61a5b80

Please sign in to comment.