From 04056e35ccaee5343718b532ff88995fc000e1d9 Mon Sep 17 00:00:00 2001 From: Fenil Suchak Date: Sat, 16 Mar 2019 18:38:05 +0530 Subject: [PATCH 01/13] fix1 --- package/MDAnalysis/core/groups.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/package/MDAnalysis/core/groups.py b/package/MDAnalysis/core/groups.py index f8788e6aca6..37e988d7030 100644 --- a/package/MDAnalysis/core/groups.py +++ b/package/MDAnalysis/core/groups.py @@ -3086,7 +3086,8 @@ def write(self, filename=None, file_format="PDB", format = os.path.splitext(filename)[1][1:] # strip initial dot! format = format or file_format format = format.strip().upper() - + if format == 'GZ': + format = filename.split('.')[-2] writer = get_writer_for(filename, format=format, multiframe=multiframe) except (ValueError, TypeError): pass From e7d816456555090d15483f66965e08898f1f1fb2 Mon Sep 17 00:00:00 2001 From: Fenil Suchak Date: Tue, 19 Mar 2019 12:16:33 +0530 Subject: [PATCH 02/13] removed redundant format --- package/MDAnalysis/core/groups.py | 10 +--------- 1 file changed, 1 insertion(+), 9 deletions(-) diff --git a/package/MDAnalysis/core/groups.py b/package/MDAnalysis/core/groups.py index 37e988d7030..a8a885d204b 100644 --- a/package/MDAnalysis/core/groups.py +++ b/package/MDAnalysis/core/groups.py @@ -3080,15 +3080,7 @@ def write(self, filename=None, file_format="PDB", # Try and select a Class using get_ methods (becomes `writer`) # Once (and if!) class is selected, use it in with block try: - # format keyword works differently in get_writer and get_selection_writer - # here it overrides everything, in get_sel it is just a default - # apply sparingly here! - format = os.path.splitext(filename)[1][1:] # strip initial dot! - format = format or file_format - format = format.strip().upper() - if format == 'GZ': - format = filename.split('.')[-2] - writer = get_writer_for(filename, format=format, multiframe=multiframe) + writer = get_writer_for(filename, format=None, multiframe=multiframe) except (ValueError, TypeError): pass else: From 62601a372140d9c4cc8033d60ab03d393e30ca65 Mon Sep 17 00:00:00 2001 From: Fenil Suchak Date: Thu, 21 Mar 2019 13:00:56 +0530 Subject: [PATCH 03/13] file_format_precedence --- package/MDAnalysis/core/groups.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/package/MDAnalysis/core/groups.py b/package/MDAnalysis/core/groups.py index a8a885d204b..110284f4425 100644 --- a/package/MDAnalysis/core/groups.py +++ b/package/MDAnalysis/core/groups.py @@ -2975,7 +2975,7 @@ def improper(self): "improper only makes sense for a group with exactly 4 atoms") return topologyobjects.ImproperDihedral(self.ix, self.universe) - def write(self, filename=None, file_format="PDB", + def write(self, filename=None, file_format=None, filenamefmt="{trjname}_{frame}", frames=None, **kwargs): """Write `AtomGroup` to a file. @@ -3080,7 +3080,11 @@ def write(self, filename=None, file_format="PDB", # Try and select a Class using get_ methods (becomes `writer`) # Once (and if!) class is selected, use it in with block try: - writer = get_writer_for(filename, format=None, multiframe=multiframe) + if file_format is None: #If this case then get_writer_for guesses format from filename + pass + else: + format = file_format + writer = get_writer_for(filename, format=format, multiframe=multiframe) except (ValueError, TypeError): pass else: From 1b23c61974190db7a72a2e28328f9664279cc897 Mon Sep 17 00:00:00 2001 From: Fenil Suchak Date: Thu, 21 Mar 2019 22:11:45 +0530 Subject: [PATCH 04/13] fixed changes due to file_format being none --- package/MDAnalysis/core/groups.py | 2 +- package/MDAnalysis/lib/util.py | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/package/MDAnalysis/core/groups.py b/package/MDAnalysis/core/groups.py index 110284f4425..0119f5d9d82 100644 --- a/package/MDAnalysis/core/groups.py +++ b/package/MDAnalysis/core/groups.py @@ -3054,7 +3054,7 @@ def write(self, filename=None, file_format=None, if filename is None: trjname, ext = os.path.splitext(os.path.basename(trj.filename)) filename = filenamefmt.format(trjname=trjname, frame=trj.frame) - filename = util.filename(filename, ext=file_format.lower(), keep=True) + filename = util.filename(filename, ext=file_format, keep=True) # Some writer behave differently when they are given a "multiframe" # argument. It is the case of the PDB writer tht writes models when diff --git a/package/MDAnalysis/lib/util.py b/package/MDAnalysis/lib/util.py index 28c9457198c..18627e859d1 100644 --- a/package/MDAnalysis/lib/util.py +++ b/package/MDAnalysis/lib/util.py @@ -249,6 +249,7 @@ def filename(name, ext=None, keep=False): Also permits :class:`NamedStream` to pass through. """ if ext is not None: + ext = ext.lower() if not ext.startswith(os.path.extsep): ext = os.path.extsep + ext root, origext = os.path.splitext(name) From 94ccf13f14f81cf59458d87d008c5e28775847d1 Mon Sep 17 00:00:00 2001 From: Fenil Suchak Date: Fri, 22 Mar 2019 20:15:33 +0530 Subject: [PATCH 05/13] fixed redundant --- package/MDAnalysis/core/groups.py | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/package/MDAnalysis/core/groups.py b/package/MDAnalysis/core/groups.py index 0119f5d9d82..205e5b41e81 100644 --- a/package/MDAnalysis/core/groups.py +++ b/package/MDAnalysis/core/groups.py @@ -3080,11 +3080,7 @@ def write(self, filename=None, file_format=None, # Try and select a Class using get_ methods (becomes `writer`) # Once (and if!) class is selected, use it in with block try: - if file_format is None: #If this case then get_writer_for guesses format from filename - pass - else: - format = file_format - writer = get_writer_for(filename, format=format, multiframe=multiframe) + writer = get_writer_for(filename, format=file_format, multiframe=multiframe) except (ValueError, TypeError): pass else: From 70a314be119b6da78fd65aafa38ac82309f4eb12 Mon Sep 17 00:00:00 2001 From: Fenil Suchak Date: Sun, 24 Mar 2019 14:54:14 +0530 Subject: [PATCH 06/13] fixed util.filename by default PDB --- package/MDAnalysis/core/groups.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package/MDAnalysis/core/groups.py b/package/MDAnalysis/core/groups.py index 205e5b41e81..4ac718db785 100644 --- a/package/MDAnalysis/core/groups.py +++ b/package/MDAnalysis/core/groups.py @@ -3054,7 +3054,7 @@ def write(self, filename=None, file_format=None, if filename is None: trjname, ext = os.path.splitext(os.path.basename(trj.filename)) filename = filenamefmt.format(trjname=trjname, frame=trj.frame) - filename = util.filename(filename, ext=file_format, keep=True) + filename = util.filename(filename, ext='PDB', keep=True) # Some writer behave differently when they are given a "multiframe" # argument. It is the case of the PDB writer tht writes models when From a7186173ee8c48cc6dc160468efd53793f02bb74 Mon Sep 17 00:00:00 2001 From: Fenil Suchak Date: Fri, 29 Mar 2019 15:01:23 +0530 Subject: [PATCH 07/13] some logical fix --- package/MDAnalysis/core/groups.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/package/MDAnalysis/core/groups.py b/package/MDAnalysis/core/groups.py index 4ac718db785..0a7c773eb53 100644 --- a/package/MDAnalysis/core/groups.py +++ b/package/MDAnalysis/core/groups.py @@ -3054,8 +3054,7 @@ def write(self, filename=None, file_format=None, if filename is None: trjname, ext = os.path.splitext(os.path.basename(trj.filename)) filename = filenamefmt.format(trjname=trjname, frame=trj.frame) - filename = util.filename(filename, ext='PDB', keep=True) - + filename = util.filename(filename, ext= file_format or 'PDB', keep=True) # Some writer behave differently when they are given a "multiframe" # argument. It is the case of the PDB writer tht writes models when # "multiframe" is True. @@ -3099,7 +3098,7 @@ def write(self, filename=None, file_format=None, try: # here `file_format` is only used as default, # anything pulled off `filename` will be used preferentially - writer = get_selection_writer_for(filename, file_format) + writer = get_selection_writer_for(filename, file_format or 'PDB') except (TypeError, NotImplementedError): pass else: From cc4a01389aa20340026379af56989e0a846b51db Mon Sep 17 00:00:00 2001 From: Fenil Suchak Date: Fri, 29 Mar 2019 15:36:16 +0530 Subject: [PATCH 08/13] GRO Zipping --- package/MDAnalysis/coordinates/GRO.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package/MDAnalysis/coordinates/GRO.py b/package/MDAnalysis/coordinates/GRO.py index 98e753b4303..39bdf697294 100644 --- a/package/MDAnalysis/coordinates/GRO.py +++ b/package/MDAnalysis/coordinates/GRO.py @@ -326,7 +326,7 @@ def __init__(self, filename, convert_units=None, n_atoms=None, **kwargs): w.write(u.atoms) """ - self.filename = util.filename(filename, ext='gro') + self.filename = util.filename(filename, ext='gro', keep = True) self.n_atoms = n_atoms self.reindex = kwargs.pop('reindex', True) From 92634f0d9ccd0a00acee3389cf99de662faf69f5 Mon Sep 17 00:00:00 2001 From: Fenil Suchak Date: Tue, 2 Apr 2019 22:31:26 +0530 Subject: [PATCH 09/13] tests_added --- .../MDAnalysisTests/core/test_atomgroup.py | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/testsuite/MDAnalysisTests/core/test_atomgroup.py b/testsuite/MDAnalysisTests/core/test_atomgroup.py index c2f223bf2ec..70bdec90d1f 100644 --- a/testsuite/MDAnalysisTests/core/test_atomgroup.py +++ b/testsuite/MDAnalysisTests/core/test_atomgroup.py @@ -167,7 +167,27 @@ def test_write_frame_none(self, u, tmpdir, extension): u.atoms.positions[None, ...], new_positions, decimal=2 ) + @pytest.mark.parametrize('extension', ('xtc', 'dcd', 'pdb', 'xyz')) + def test_compressed_write_frame_none(self, u, tmpdir, extension): + for ext in ('.gz', '.bz2'): + destination = str(tmpdir / 'test.' + extension + ext) + u.atoms.write(destination, frames=None) + u_new = mda.Universe(destination) + new_positions = np.stack([ts.positions for ts in u_new.trajectory]) + assert_array_almost_equal( + u.atoms.positions[None, ...], new_positions, decimal=2 + ) + def test_write_frames_all(self, u, tmpdir): + for ext in ('.gz', '.bz2'): + destination = str(tmpdir / 'test.dcd') + ext + u.atoms.write(destination, frames='all') + u_new = mda.Universe(destination) + ref_positions = np.stack([ts.positions for ts in u.trajectory]) + new_positions = np.stack([ts.positions for ts in u_new.trajectory]) + assert_array_almost_equal(new_positions, ref_positions) + + def test_compressed_rite_frames_all(self, u, tmpdir): destination = str(tmpdir / 'test.dcd') u.atoms.write(destination, frames='all') u_new = mda.Universe(destination) @@ -238,6 +258,16 @@ def test_write_atoms(self, universe, outfile): err_msg=("atom coordinate mismatch between original and {0!s} file" "".format(self.ext))) + def test_compressed_write_atoms(self, universe, outfile): + for compressed_ext in ('.gz', '.bz2'): + universe.atoms.write(outfile + compressed_ext) + u2 = self.universe_from_tmp(outfile + compressed_ext) + assert_almost_equal( + universe.atoms.positions, u2.atoms.positions, + self.precision, + err_msg=("atom coordinate mismatch between original and {0!s} file" + "".format(self.ext))) + def test_write_empty_atomgroup(self, universe, outfile): sel = universe.select_atoms('name doesntexist') with pytest.raises(IndexError): From 755e246eb4ee74b2217a93467ec8606ef31c0881 Mon Sep 17 00:00:00 2001 From: Fenil Suchak Date: Wed, 3 Apr 2019 10:13:18 +0530 Subject: [PATCH 10/13] made_changes --- package/MDAnalysis/coordinates/GRO.py | 2 +- package/MDAnalysis/core/groups.py | 7 +++++-- testsuite/MDAnalysisTests/core/test_atomgroup.py | 6 +++--- 3 files changed, 9 insertions(+), 6 deletions(-) diff --git a/package/MDAnalysis/coordinates/GRO.py b/package/MDAnalysis/coordinates/GRO.py index 39bdf697294..3303f28cbff 100644 --- a/package/MDAnalysis/coordinates/GRO.py +++ b/package/MDAnalysis/coordinates/GRO.py @@ -326,7 +326,7 @@ def __init__(self, filename, convert_units=None, n_atoms=None, **kwargs): w.write(u.atoms) """ - self.filename = util.filename(filename, ext='gro', keep = True) + self.filename = util.filename(filename, ext='gro', keep=True) self.n_atoms = n_atoms self.reindex = kwargs.pop('reindex', True) diff --git a/package/MDAnalysis/core/groups.py b/package/MDAnalysis/core/groups.py index 0a7c773eb53..e9dcfecc458 100644 --- a/package/MDAnalysis/core/groups.py +++ b/package/MDAnalysis/core/groups.py @@ -3054,7 +3054,9 @@ def write(self, filename=None, file_format=None, if filename is None: trjname, ext = os.path.splitext(os.path.basename(trj.filename)) filename = filenamefmt.format(trjname=trjname, frame=trj.frame) - filename = util.filename(filename, ext= file_format or 'PDB', keep=True) + filename = util.filename(filename, + ext=file_format if file_format is not None else 'PDB', + keep=True) # Some writer behave differently when they are given a "multiframe" # argument. It is the case of the PDB writer tht writes models when # "multiframe" is True. @@ -3098,7 +3100,8 @@ def write(self, filename=None, file_format=None, try: # here `file_format` is only used as default, # anything pulled off `filename` will be used preferentially - writer = get_selection_writer_for(filename, file_format or 'PDB') + writer = get_selection_writer_for(filename, + file_format if file_format is not None else 'PDB') except (TypeError, NotImplementedError): pass else: diff --git a/testsuite/MDAnalysisTests/core/test_atomgroup.py b/testsuite/MDAnalysisTests/core/test_atomgroup.py index 70bdec90d1f..a532d850c72 100644 --- a/testsuite/MDAnalysisTests/core/test_atomgroup.py +++ b/testsuite/MDAnalysisTests/core/test_atomgroup.py @@ -156,7 +156,7 @@ def test_write_frame_iterator(self, u, tmpdir, frames): assert_array_almost_equal(new_positions, ref_positions) - @pytest.mark.parametrize('extension', ('xtc', 'dcd', 'pdb', 'xyz')) + @pytest.mark.parametrize('extension', ('xtc', 'dcd', 'pdb', 'xyz', 'PDB')) def test_write_frame_none(self, u, tmpdir, extension): destination = str(tmpdir / 'test.' + extension) u.atoms.write(destination, frames=None) @@ -167,7 +167,7 @@ def test_write_frame_none(self, u, tmpdir, extension): u.atoms.positions[None, ...], new_positions, decimal=2 ) - @pytest.mark.parametrize('extension', ('xtc', 'dcd', 'pdb', 'xyz')) + @pytest.mark.parametrize('extension', ('xtc', 'dcd', 'pdb', 'xyz', 'PDB')) def test_compressed_write_frame_none(self, u, tmpdir, extension): for ext in ('.gz', '.bz2'): destination = str(tmpdir / 'test.' + extension + ext) @@ -187,7 +187,7 @@ def test_write_frames_all(self, u, tmpdir): new_positions = np.stack([ts.positions for ts in u_new.trajectory]) assert_array_almost_equal(new_positions, ref_positions) - def test_compressed_rite_frames_all(self, u, tmpdir): + def test_compressed_write_frames_all(self, u, tmpdir): destination = str(tmpdir / 'test.dcd') u.atoms.write(destination, frames='all') u_new = mda.Universe(destination) From 28298b1165be8eac710422fbacb09824b1f7a283 Mon Sep 17 00:00:00 2001 From: Fenil Suchak Date: Wed, 3 Apr 2019 10:27:07 +0530 Subject: [PATCH 11/13] changes_fixed --- testsuite/MDAnalysisTests/core/test_atomgroup.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/testsuite/MDAnalysisTests/core/test_atomgroup.py b/testsuite/MDAnalysisTests/core/test_atomgroup.py index a532d850c72..617cdb7b959 100644 --- a/testsuite/MDAnalysisTests/core/test_atomgroup.py +++ b/testsuite/MDAnalysisTests/core/test_atomgroup.py @@ -178,7 +178,7 @@ def test_compressed_write_frame_none(self, u, tmpdir, extension): u.atoms.positions[None, ...], new_positions, decimal=2 ) - def test_write_frames_all(self, u, tmpdir): + def test_compressed_write_frames_all(self, u, tmpdir): for ext in ('.gz', '.bz2'): destination = str(tmpdir / 'test.dcd') + ext u.atoms.write(destination, frames='all') @@ -187,7 +187,7 @@ def test_write_frames_all(self, u, tmpdir): new_positions = np.stack([ts.positions for ts in u_new.trajectory]) assert_array_almost_equal(new_positions, ref_positions) - def test_compressed_write_frames_all(self, u, tmpdir): + def test_write_frames_all(self, u, tmpdir): destination = str(tmpdir / 'test.dcd') u.atoms.write(destination, frames='all') u_new = mda.Universe(destination) From d84232f484195c9c70cdfa363b9d4edaa8da7ff1 Mon Sep 17 00:00:00 2001 From: Fenil Suchak Date: Wed, 3 Apr 2019 10:53:03 +0530 Subject: [PATCH 12/13] added changelog and authors --- package/AUTHORS | 1 + package/CHANGELOG | 4 ++++ 2 files changed, 5 insertions(+) diff --git a/package/AUTHORS b/package/AUTHORS index 8ba4857455f..8056f7f4f9f 100644 --- a/package/AUTHORS +++ b/package/AUTHORS @@ -117,6 +117,7 @@ Chronological list of authors - Daniele Padula 2019 - Ninad Bhat + - Fenil Suchak External code ------------- diff --git a/package/CHANGELOG b/package/CHANGELOG index a283ff2fed5..1a44d656fcd 100644 --- a/package/CHANGELOG +++ b/package/CHANGELOG @@ -49,6 +49,8 @@ Enhancements distance_array. (Issue #2103, PR #2209) * updated analysis.distances.contact_matrix to use capped_distance. (Issue #2102, PR #2215) + * added functionality to write files in compressed form(gz,bz2). (Issue #2216, + PR #2221) Changes * added official support for Python 3.7 (PR #1963) @@ -58,6 +60,8 @@ Changes invalid, an all-zero array or zero is returned, respectively. (Issue #2200, PR #2201) * changed fudge_factor in guess_bonds to deal with new vdw radii (#2138, PR #2142) + * changed ag.write() so that preference is given to file_format over filename + (Issue #2216,PR #2221) Fixes * fixed the segmentation fault in capped_distances (Issue #2164, PR #2169) From 941e3cd794d13852a76393f2ce9c51c7c35d753e Mon Sep 17 00:00:00 2001 From: Fenil Suchak Date: Thu, 4 Apr 2019 16:20:54 +0530 Subject: [PATCH 13/13] updated changelog --- package/CHANGELOG | 2 -- 1 file changed, 2 deletions(-) diff --git a/package/CHANGELOG b/package/CHANGELOG index 1a44d656fcd..12fca2b7c43 100644 --- a/package/CHANGELOG +++ b/package/CHANGELOG @@ -60,8 +60,6 @@ Changes invalid, an all-zero array or zero is returned, respectively. (Issue #2200, PR #2201) * changed fudge_factor in guess_bonds to deal with new vdw radii (#2138, PR #2142) - * changed ag.write() so that preference is given to file_format over filename - (Issue #2216,PR #2221) Fixes * fixed the segmentation fault in capped_distances (Issue #2164, PR #2169)