From 55cce24003d4c0975b1bba387b9e6ac20638780e Mon Sep 17 00:00:00 2001 From: Rocco Meli Date: Tue, 24 Dec 2024 12:25:48 +0100 Subject: [PATCH] fmt selections (#4861) --- package/MDAnalysis/selections/__init__.py | 6 ++-- package/MDAnalysis/selections/base.py | 35 ++++++++++++++--------- package/MDAnalysis/selections/charmm.py | 16 +++++++---- package/MDAnalysis/selections/gromacs.py | 2 +- package/MDAnalysis/selections/jmol.py | 6 ++-- package/MDAnalysis/selections/pymol.py | 10 ++++--- package/MDAnalysis/selections/vmd.py | 2 +- package/pyproject.toml | 1 + 8 files changed, 48 insertions(+), 30 deletions(-) diff --git a/package/MDAnalysis/selections/__init__.py b/package/MDAnalysis/selections/__init__.py index 3ccecf7d0b..acb552b7ca 100644 --- a/package/MDAnalysis/selections/__init__.py +++ b/package/MDAnalysis/selections/__init__.py @@ -84,6 +84,8 @@ def get_writer(filename: str, defaultformat: str) -> base.SelectionWriterBase: try: return _SELECTION_WRITERS[format] except KeyError: - errmsg = (f"Writing as {format} is not implemented; only " - f"{ _SELECTION_WRITERS.keys()} will work.") + errmsg = ( + f"Writing as {format} is not implemented; only " + f"{ _SELECTION_WRITERS.keys()} will work." + ) raise NotImplementedError(errmsg) from None diff --git a/package/MDAnalysis/selections/base.py b/package/MDAnalysis/selections/base.py index eb55a73897..d9b49e516d 100644 --- a/package/MDAnalysis/selections/base.py +++ b/package/MDAnalysis/selections/base.py @@ -60,7 +60,7 @@ class _Selectionmeta(type): def __init__(cls, name, bases, classdict): type.__init__(type, name, bases, classdict) try: - fmt = util.asiterable(classdict['format']) + fmt = util.asiterable(classdict["format"]) except KeyError: pass else: @@ -97,17 +97,20 @@ class SelectionWriterBase(metaclass=_Selectionmeta): and closed with the :meth:`close` method or when exiting the `with` statement. """ + #: Name of the format. format = None #: Extension of output files. ext = None #: Special character to continue a line across a newline. - continuation = '' + continuation = "" #: Comment format string; should contain '%s' or ``None`` for no comments. commentfmt = None default_numterms = 8 - def __init__(self, filename, mode="w", numterms=None, preamble=None, **kwargs): + def __init__( + self, filename, mode="w", numterms=None, preamble=None, **kwargs + ): """Set up for writing to *filename*. Parameters @@ -126,8 +129,10 @@ def __init__(self, filename, mode="w", numterms=None, preamble=None, **kwargs): use as defaults for :meth:`write` """ self.filename = util.filename(filename, ext=self.ext) - if not mode in ('a', 'w'): - raise ValueError("mode must be one of 'w', 'a', not {0!r}".format(mode)) + if not mode in ("a", "w"): + raise ValueError( + "mode must be one of 'w', 'a', not {0!r}".format(mode) + ) self.mode = mode self._current_mode = mode[0] if numterms is None or numterms < 0: @@ -154,8 +159,8 @@ def comment(self, s): A newline is appended to non-empty strings. """ if self.commentfmt is None: - return '' - return self.commentfmt % s + '\n' + return "" + return self.commentfmt % s + "\n" def write_preamble(self): """Write a header, depending on the file format.""" @@ -188,7 +193,7 @@ def write(self, selection, number=None, name=None, frame=None, mode=None): frame = u.trajectory.ts.frame except AttributeError: frame = 1 # should catch cases when we are analyzing a single PDB (?) - name = name or self.otherargs.get('name', None) + name = name or self.otherargs.get("name", None) if name is None: if number is None: self.number += 1 @@ -203,13 +208,15 @@ def write(self, selection, number=None, name=None, frame=None, mode=None): out = self._outfile self._write_head(out, name=name) for iatom in range(0, len(selection.atoms), step): - line = selection_terms[iatom:iatom + step] + line = selection_terms[iatom : iatom + step] out.write(" ".join(line)) if len(line) == step and not iatom + step == len(selection.atoms): - out.write(' ' + self.continuation + '\n') - out.write(' ') # safe so that we don't have to put a space at the start of tail + out.write(" " + self.continuation + "\n") + out.write( + " " + ) # safe so that we don't have to put a space at the start of tail self._write_tail(out) - out.write('\n') # always terminate with newline + out.write("\n") # always terminate with newline def close(self): """Close the file @@ -234,11 +241,11 @@ def _translate(self, atoms, **kwargs): def _write_head(self, out, **kwargs): """Initial output to open file object *out*.""" - pass # pylint: disable=unnecessary-pass + pass # pylint: disable=unnecessary-pass def _write_tail(self, out, **kwargs): """Last output to open file object *out*.""" - pass # pylint: disable=unnecessary-pass + pass # pylint: disable=unnecessary-pass # Context manager support to match Coordinate writers # all file handles use a with block in their write method, so these do nothing special diff --git a/package/MDAnalysis/selections/charmm.py b/package/MDAnalysis/selections/charmm.py index 5f9b4b4b9b..62bbbd0497 100644 --- a/package/MDAnalysis/selections/charmm.py +++ b/package/MDAnalysis/selections/charmm.py @@ -1,5 +1,5 @@ # -*- Mode: python; tab-width: 4; indent-tabs-mode:nil; coding:utf-8 -*- -# vim: tabstop=4 expandtab shiftwidth=4 softtabstop=4 +# vim: tabstop=4 expandtab shiftwidth=4 softtabstop=4 # # MDAnalysis --- https://www.mdanalysis.org # Copyright (c) 2006-2017 The MDAnalysis Development Team and contributors @@ -45,20 +45,26 @@ class SelectionWriter(base.SelectionWriterBase): format = ["CHARMM", "str"] ext = "str" - continuation = '-' + continuation = "-" commentfmt = "! %s" - default_numterms = 4 # be conservative because CHARMM only reads 72 columns + default_numterms = ( + 4 # be conservative because CHARMM only reads 72 columns + ) def _translate(self, atoms, **kwargs): # CHARMM index is 1-based def _index(atom): return "BYNUM {0:d}".format((atom.index + 1)) - return base.join(atoms, ' .or.', _index) + return base.join(atoms, " .or.", _index) def _write_head(self, out, **kwargs): out.write(self.comment("MDAnalysis CHARMM selection")) - out.write("DEFINE {name!s} SELECT ".format(**kwargs) + self.continuation + '\n') + out.write( + "DEFINE {name!s} SELECT ".format(**kwargs) + + self.continuation + + "\n" + ) def _write_tail(self, out, **kwargs): out.write("END") diff --git a/package/MDAnalysis/selections/gromacs.py b/package/MDAnalysis/selections/gromacs.py index 3dc8ea7950..e5de251a4b 100644 --- a/package/MDAnalysis/selections/gromacs.py +++ b/package/MDAnalysis/selections/gromacs.py @@ -1,5 +1,5 @@ # -*- Mode: python; tab-width: 4; indent-tabs-mode:nil; coding:utf-8 -*- -# vim: tabstop=4 expandtab shiftwidth=4 softtabstop=4 +# vim: tabstop=4 expandtab shiftwidth=4 softtabstop=4 # # MDAnalysis --- https://www.mdanalysis.org # Copyright (c) 2006-2017 The MDAnalysis Development Team and contributors diff --git a/package/MDAnalysis/selections/jmol.py b/package/MDAnalysis/selections/jmol.py index 72462b97d2..ed29366884 100644 --- a/package/MDAnalysis/selections/jmol.py +++ b/package/MDAnalysis/selections/jmol.py @@ -1,5 +1,5 @@ # -*- Mode: python; tab-width: 4; indent-tabs-mode:nil; coding:utf-8 -*- -# vim: tabstop=4 expandtab shiftwidth=4 softtabstop=4 +# vim: tabstop=4 expandtab shiftwidth=4 softtabstop=4 # # MDAnalysis --- https://www.mdanalysis.org # Copyright (c) 2006-2017 The MDAnalysis Development Team and contributors @@ -46,14 +46,14 @@ class SelectionWriter(base.SelectionWriterBase): format = ["Jmol", "spt"] ext = "spt" default_numterms = None - commentfmt = '#' + commentfmt = "#" def _translate(self, atoms, **kwargs): # Jmol indexing is 0 based when using atom bitsets def _index(atom): return str(atom.index) - return base.join(atoms, ' ', _index) + return base.join(atoms, " ", _index) def _write_head(self, out, **kwargs): out.write("@~{name!s} ({{".format(**kwargs)) diff --git a/package/MDAnalysis/selections/pymol.py b/package/MDAnalysis/selections/pymol.py index 080d83817f..d528a1394b 100644 --- a/package/MDAnalysis/selections/pymol.py +++ b/package/MDAnalysis/selections/pymol.py @@ -1,5 +1,5 @@ # -*- Mode: python; tab-width: 4; indent-tabs-mode:nil; coding:utf-8 -*- -# vim: tabstop=4 expandtab shiftwidth=4 softtabstop=4 +# vim: tabstop=4 expandtab shiftwidth=4 softtabstop=4 # # MDAnalysis --- https://www.mdanalysis.org # Copyright (c) 2006-2017 The MDAnalysis Development Team and contributors @@ -46,7 +46,7 @@ class SelectionWriter(base.SelectionWriterBase): format = ["PyMol", "pml"] ext = "pml" - continuation = '\\' # quoted backslash! + continuation = "\\" # quoted backslash! commentfmt = "# %s" default_numterms = 6 @@ -55,8 +55,10 @@ def _translate(self, atoms, **kwargs): def _index(atom): return "index {0:d}".format((atom.index + 1)) - return base.join(atoms, ' |', _index) + return base.join(atoms, " |", _index) def _write_head(self, out, **kwargs): out.write(self.comment("MDAnalysis PyMol selection")) - out.write("select {name!s}, ".format(**kwargs) + self.continuation + '\n') + out.write( + "select {name!s}, ".format(**kwargs) + self.continuation + "\n" + ) diff --git a/package/MDAnalysis/selections/vmd.py b/package/MDAnalysis/selections/vmd.py index dc44916751..11081d6ebb 100644 --- a/package/MDAnalysis/selections/vmd.py +++ b/package/MDAnalysis/selections/vmd.py @@ -53,7 +53,7 @@ class SelectionWriter(base.SelectionWriterBase): format = "VMD" ext = "vmd" - continuation = '\\' + continuation = "\\" commentfmt = "# %s" def _write_head(self, out, **kwargs): diff --git a/package/pyproject.toml b/package/pyproject.toml index 7699f7c3b4..74598c1b04 100644 --- a/package/pyproject.toml +++ b/package/pyproject.toml @@ -136,6 +136,7 @@ tables\.py | MDAnalysis/lib/.*\.py^ | MDAnalysis/transformations/.*\.py | MDAnalysis/converters/.*\.py +| MDAnalysis/selections/.*\.py ) ''' extend-exclude = '''