Skip to content

PDBWriter incompatible with StringIO if atom coordinates go out of range #2512

@Ragzouken

Description

@Ragzouken

Expected behavior

Assuming that PDBWriter is supposed to support a StringIO object as a valid destination, I expect that attempting to write a PDB with atom coordinates out of range will raise the "PDB files must have coordinate values between [...]" exception.

Actual behavior

The check attempts to os.remove the StringIO object and does not catch the resulting exception, so the coordinate exception never gets raised.

~/narupa-protocols/narupa-protocol/python-libraries/narupa-ase/examples/nglclient.py in mda_to_pdb_str(universe)
     54 def mda_to_pdb_str(universe: mda.Universe):
     55     with StringIO() as str_io, mda.coordinates.PDB.PDBWriter(str_io) as writer:
---> 56         writer.write(universe.atoms)
     57         pdb = str_io.getvalue()
     58     return pdb
~/anaconda3/envs/narupa-dev/lib/python3.7/site-packages/MDAnalysis/coordinates/PDB.py in write(self, obj)
    765         # write_all_timesteps() to dump everything in one go, or do the
    766         # traditional loop over frames
--> 767         self.write_next_timestep(self.ts, multiframe=self._multiframe)
    768         self._write_pdb_bonds()
    769         # END record is written when file is being close()d
~/anaconda3/envs/narupa-dev/lib/python3.7/site-packages/MDAnalysis/coordinates/PDB.py in write_next_timestep(self, ts, **kwargs)
    839                 raise NoDataError("PBDWriter: no coordinate data to write to "
    840                                   "trajectory file")
--> 841         self._check_pdb_coordinates()
    842         self._write_timestep(ts, **kwargs)
    843 
~/anaconda3/envs/narupa-dev/lib/python3.7/site-packages/MDAnalysis/coordinates/PDB.py in _check_pdb_coordinates(self)
    670             self.close()
    671             try:
--> 672                 os.remove(self.filename)
    673             except OSError as err:
    674                 if err.errno == errno.ENOENT:
TypeError: remove: path should be string, bytes or os.PathLike, not _io.StringIO

Code to reproduce the behavior

Show us how to reproduce the failiure. If you can, use trajectory files from the test data.

import io
import MDAnalysis as mda
from MDAnalysis.tests.datafiles import PDB

u = mda.Universe(PDB)
u.atoms.translate([-9999, -9999, -9999])

with io.StringIO() as str_io, mda.coordinates.PDB.PDBWriter(str_io) as writer:
    #writer.filename = ""  # uncomment to avoid issue
    writer.write(u.atoms)

Currently version of MDAnalysis

  • Which version are you using? (run python -c "import MDAnalysis as mda; print(mda.__version__)")
    0.20.1
  • Which version of Python (python -V)?
    Python 3.7.3
  • Which operating system?
    Windows 10

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions