Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 15 additions & 3 deletions package/MDAnalysis/core/groups.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@
_TOPOLOGY_ATTRNAMES,
)
from ..lib import util
from ..lib.log import ProgressBar
from ..lib.util import (
cached,
warn_if_not_unique,
Expand Down Expand Up @@ -3824,6 +3825,7 @@ def write(
file_format=None,
filenamefmt="{trjname}_{frame}",
frames=None,
verbose=False,
**kwargs,
):
"""Write `AtomGroup` to a file.
Expand Down Expand Up @@ -3868,15 +3870,21 @@ def write(
By default, `frames` is set to ``None`` and only the current frame
is written. If `frames` is set to "all", then all the frame from
trajectory are written.
verbose : bool, optional
Whether to display a progress bar when writing multiple frames
(i.e. when `frames` is not ``None``). If ``True``, a progress bar
is displayed while writing the frames (see
:class:`MDAnalysis.lib.log.ProgressBar`); if ``False`` (the
default), no progress bar is displayed.

.. versionadded:: 2.12.0

.. versionchanged:: 0.9.0 Merged with write_selection. This method can
now write both selections out.
.. versionchanged:: 0.19.0
Can write multiframe trajectories with the 'frames' argument.
"""
# TODO: Add a 'verbose' option alongside 'frames'.

"""
# check that AtomGroup actually has any atoms (Issue #434)
if len(self.atoms) == 0:
raise IndexError("Cannot write an AtomGroup with 0 atoms")
Expand Down Expand Up @@ -3946,7 +3954,11 @@ def write(
else:
current_frame = trj.ts.frame
try:
for _ in trj_frames:
for _ in ProgressBar(
trj_frames,
verbose=verbose,
desc="Writing frames",
):
w.write(self.atoms)
finally:
trj[current_frame]
Expand Down
30 changes: 30 additions & 0 deletions testsuite/MDAnalysisTests/core/test_atomgroup.py
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,36 @@ def test_bogus_kwarg_pdb(self, u, tmpdir):
with pytest.raises(TypeError):
u.atoms.write("dummy.pdb", bogus="what?")

def test_write_frames_verbose(self, u, tmpdir):
# writing multiple frames with verbose=True should write the
# requested frames correctly
destination = str(tmpdir / "test.dcd")
frames = [2, 3, 3, 1]
u.atoms.write(destination, frames=frames, verbose=True)

u_new = mda.Universe(destination, to_guess=())
new_positions = np.stack(
[ts.positions.copy() for ts in u_new.trajectory]
)
ref_positions = np.stack(
[ts.positions.copy() for ts in u.trajectory[frames]]
)

assert_array_almost_equal(new_positions, ref_positions)

def test_write_frames_verbose_progress(self, u, tmpdir, capsys):
# verbose=True should display a progress bar with the description
# "Writing frames" on stderr, while the default should be silent
destination = str(tmpdir / "test.dcd")
u.atoms.write(destination, frames=[1, 2, 3])
captured = capsys.readouterr()
assert "Writing frames" not in captured.err
assert captured.out == ""

u.atoms.write(destination, frames=[1, 2, 3], verbose=True)
captured = capsys.readouterr()
assert "Writing frames" in captured.err


class _WriteAtoms(object):
"""Set up the standard AdK system in implicit solvent."""
Expand Down
Loading