diff --git a/package/MDAnalysis/core/groups.py b/package/MDAnalysis/core/groups.py index af084926cd..ca4a4600b1 100644 --- a/package/MDAnalysis/core/groups.py +++ b/package/MDAnalysis/core/groups.py @@ -106,6 +106,7 @@ _TOPOLOGY_ATTRNAMES, ) from ..lib import util +from ..lib.log import ProgressBar from ..lib.util import ( cached, warn_if_not_unique, @@ -3824,6 +3825,7 @@ def write( file_format=None, filenamefmt="{trjname}_{frame}", frames=None, + verbose=False, **kwargs, ): """Write `AtomGroup` to a file. @@ -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") @@ -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] diff --git a/testsuite/MDAnalysisTests/core/test_atomgroup.py b/testsuite/MDAnalysisTests/core/test_atomgroup.py index 0808511343..7bbd47bad6 100644 --- a/testsuite/MDAnalysisTests/core/test_atomgroup.py +++ b/testsuite/MDAnalysisTests/core/test_atomgroup.py @@ -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."""