Skip to content

Commit

Permalink
Fix #2692 (#2694)
Browse files Browse the repository at this point in the history
- Use user-provided remark in XYZWriter and add MDAnalysis version if not
  • Loading branch information
RMeli authored Jun 7, 2020
1 parent 8c06037 commit e835b5b
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 9 deletions.
1 change: 1 addition & 0 deletions package/CHANGELOG
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ mm/dd/yy richardjgowers, kain88-de, lilyminium, p-j-smith, bdice, joaomcteixeira
* 0.21.0

Fixes
* Use user-provided `remark` in `XYZWriter` (Issue #2692)
* Added more informative error messages about topology attributes
(Issue #2565)
* Made NoDataError a subclass of ValueError *and* AttributeError
Expand Down
37 changes: 28 additions & 9 deletions package/MDAnalysis/coordinates/XYZ.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
XYZ File format
---------------
Definiton used by the :class:`XYZReader` and :class:`XYZWriter` (and
Definition used by the :class:`XYZReader` and :class:`XYZWriter` (and
the `VMD xyzplugin`_ from whence the definition was taken)::
[ comment line ] !! NOT IMPLEMENTED !! DO NOT INCLUDE
Expand Down Expand Up @@ -146,16 +146,18 @@ def __init__(self, filename, n_atoms=None, atoms=None, convert_units=True,
writing [``True``]
remark: str (optional)
single line of text ("molecule name"). By default writes MDAnalysis
version
version and frame
.. versionchanged:: 1.0.0
Removed :code:`default_remark` variable (Issue #2692).
"""
self.filename = filename
self.remark = remark
self.n_atoms = n_atoms
self.convert_units = convert_units

self.atomnames = self._get_atoms_elements_or_names(atoms)
default_remark = "Written by {0} (release {1})".format(
self.__class__.__name__, __version__)
self.remark = default_remark if remark is None else remark

# can also be gz, bz2
self._xyz = util.anyopen(self.filename, 'wt')

Expand Down Expand Up @@ -190,8 +192,8 @@ def close(self):
def write(self, obj):
"""Write object `obj` at current trajectory frame to file.
Atom elements (or names) in the output are taken from the `obj` or default
to the value of the `atoms` keyword supplied to the
Atom elements (or names) in the output are taken from the `obj` or
default to the value of the `atoms` keyword supplied to the
:class:`XYZWriter` constructor.
Parameters
Expand Down Expand Up @@ -229,7 +231,13 @@ def write(self, obj):
self.write_next_timestep(ts)

def write_next_timestep(self, ts=None):
"""Write coordinate information in *ts* to the trajectory"""
"""
Write coordinate information in *ts* to the trajectory
.. versionchanged:: 1.0.0
Print out :code:`remark` if present, otherwise use generic one
(Issue #2692).
"""
if ts is None:
if not hasattr(self, 'ts'):
raise NoDataError('XYZWriter: no coordinate data to write to '
Expand Down Expand Up @@ -259,8 +267,19 @@ def write_next_timestep(self, ts=None):
else:
coordinates = ts.positions

# Write number of atoms
self._xyz.write("{0:d}\n".format(ts.n_atoms))
self._xyz.write("frame {0}\n".format(ts.frame))

# Write remark
if self.remark is None:
remark = "frame {} | Written by MDAnalysis {} (release {})\n".format(
ts.frame, self.__class__.__name__, __version__)

self._xyz.write(remark)
else:
self._xyz.write(self.remark.strip() + "\n")

# Write content
for atom, (x, y, z) in zip(self.atomnames, coordinates):
self._xyz.write("{0!s:>8} {1:10.5f} {2:10.5f} {3:10.5f}\n"
"".format(atom, x, y, z))
Expand Down
20 changes: 20 additions & 0 deletions testsuite/MDAnalysisTests/coordinates/test_xyz.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
BaseWriterTest)
from MDAnalysisTests import make_Universe

from MDAnalysis import __version__

class XYZReference(BaseReference):
def __init__(self):
Expand Down Expand Up @@ -106,6 +107,25 @@ def test_no_conversion(self, ref, reader, tmpdir):
w.write(ts)
self._check_copy(outfile, ref, reader)

@pytest.mark.parametrize("remarkout, remarkin",
[
2 * ["Curstom Remark"],
2 * [""],
[None, "frame 0 | Written by MDAnalysis XYZWriter (release {0})".format(__version__)],
]
)
def test_remark(self, remarkout, remarkin, ref, tmpdir):
u = mda.Universe(ref.topology, ref.trajectory)
outfile = "write-remark.xyz"

with tmpdir.as_cwd():
u.atoms.write(outfile, remark=remarkout)

with open(outfile, "r") as xyzout:
lines = xyzout.readlines()

assert lines[1].strip() == remarkin


class XYZ_BZ_Reference(XYZReference):
def __init__(self):
Expand Down

0 comments on commit e835b5b

Please sign in to comment.