Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make CRD EXT format optional #3635

Merged
merged 25 commits into from
Apr 18, 2022
Merged
Show file tree
Hide file tree
Changes from 22 commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
6d6a55e
CRD extended format an option for small systems
mdpoleto Apr 13, 2022
9c64f40
make CRD EXT format optional
mdpoleto Apr 13, 2022
beb6159
make CRD EXT format optional
mdpoleto Apr 13, 2022
1aec9f8
make CRD EXT format optional
mdpoleto Apr 13, 2022
ca4b46b
Update package/MDAnalysis/coordinates/CRD.py
mdpoleto Apr 14, 2022
6437458
Update package/MDAnalysis/coordinates/CRD.py
mdpoleto Apr 14, 2022
a2d0353
Update package/MDAnalysis/coordinates/CRD.py
mdpoleto Apr 14, 2022
53494d5
Update package/MDAnalysis/coordinates/CRD.py
mdpoleto Apr 14, 2022
430c4b8
Simplifying CRD extended code
mdpoleto Apr 14, 2022
6a4f04b
Include tests for CRD ext format
mdpoleto Apr 14, 2022
714945e
Include tests for CRD ext format
mdpoleto Apr 14, 2022
f304346
Include tests for CRD ext format
mdpoleto Apr 15, 2022
cf061f1
Improve tests for CRD EXT format
mdpoleto Apr 15, 2022
1419ab9
to be overwritten
mdpoleto Apr 15, 2022
b10ec16
Merge branch 'develop' of https://github.com/MDAnalysis/mdanalysis in…
mdpoleto Apr 15, 2022
eedd551
Merge branch 'MDAnalysis-develop' into issue-3605
mdpoleto Apr 15, 2022
c3cf075
update AUTHORS
mdpoleto Apr 15, 2022
9077a6d
Update package/MDAnalysis/coordinates/CRD.py
mdpoleto Apr 16, 2022
4765c8f
Add test to read EXT format
mdpoleto Apr 16, 2022
b11b6c4
Improve test to read EXT format
mdpoleto Apr 16, 2022
53c93e3
Merge branch 'develop' of https://github.com/MDAnalysis/mdanalysis in…
mdpoleto Apr 18, 2022
30ffb41
Update AUTHORS
mdpoleto Apr 18, 2022
276b0b7
Update AUTHORS and CRD read tests
mdpoleto Apr 18, 2022
04879c3
Update AUTHORS and CRD read tests
mdpoleto Apr 18, 2022
2d3819e
Apply suggestions from code review
orbeckst Apr 18, 2022
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
2 changes: 2 additions & 0 deletions package/AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,9 @@ Chronological list of authors
- Sukeerti T
- Robot Jelly
- Mark Verma
- Marcelo D. Poleto

>>>>>>> c0a4f610656baf08294d01ebff2fafc859887cfc
mdpoleto marked this conversation as resolved.
Show resolved Hide resolved

External code
-------------
Expand Down
2 changes: 2 additions & 0 deletions package/CHANGELOG
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ Enhancements
monatomic ion charges or edge cases with nitrogen, sulfur, phosphorus and
conjugated systems should now have correctly assigned bond orders and
charges.
* CRD extended format can now be explicitly requested with the `extended`
mdpoleto marked this conversation as resolved.
Show resolved Hide resolved
keyword (Issue #3605)

Changes
* ITPParser no longer adds an angle for water molecules that have the SETTLE
Expand Down
18 changes: 16 additions & 2 deletions package/MDAnalysis/coordinates/CRD.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,9 @@ class CRDWriter(base.WriterBase):

.. versionchanged:: 0.11.0
Frames now 0-based instead of 1-based
.. versionchanged:: 2.2.0
CRD extended format can now be explicitly requested with the
`extended` keyword
"""
format = 'CRD'
units = {'time': None, 'length': 'Angstrom'}
Expand All @@ -151,11 +154,21 @@ def __init__(self, filename, **kwargs):
----------
filename : str or :class:`~MDAnalysis.lib.util.NamedStream`
name of the output file or a stream

extended : bool (optional)
By default, noextended CRD format is used [``False``].
However, extended CRD format can be forced by
specifying `extended` ``=True``.
orbeckst marked this conversation as resolved.
Show resolved Hide resolved

.. versionadded:: 2.2.0
"""

self.filename = util.filename(filename, ext='crd')
self.crd = None

# account for explicit crd format, if requested
self.extended = kwargs.pop("extended", False)

def write(self, selection, frame=None):
"""Write selection at current trajectory frame to file.

Expand All @@ -182,14 +195,15 @@ def write(self, selection, frame=None):
except AttributeError:
frame = 0 # should catch cases when we are analyzing a single PDB (?)


atoms = selection.atoms # make sure to use atoms (Issue 46)
coor = atoms.positions # can write from selection == Universe (Issue 49)

n_atoms = len(atoms)
# Detect which format string we're using to output (EXT or not)
# *len refers to how to truncate various things,
# depending on output format!
if n_atoms > 99999:
if self.extended or n_atoms > 99999:
at_fmt = self.fmt['ATOM_EXT']
serial_len = 10
resid_len = 8
Expand Down Expand Up @@ -238,7 +252,7 @@ def write(self, selection, frame=None):
crd.write("*\n")

# Write NUMATOMS
if n_atoms > 99999:
if self.extended or n_atoms > 99999:
crd.write(self.fmt['NUMATOMS_EXT'].format(n_atoms))
else:
crd.write(self.fmt['NUMATOMS'].format(n_atoms))
Expand Down
40 changes: 34 additions & 6 deletions testsuite/MDAnalysisTests/coordinates/test_crd.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
)

import MDAnalysis as mda
import os
import numpy as np

from MDAnalysisTests.datafiles import CRD
from MDAnalysisTests import make_Universe
Expand All @@ -40,7 +42,8 @@ def u(self):

@pytest.fixture()
def outfile(self, tmpdir):
return str(tmpdir) + '/out.crd'
#return str(tmpdir) + '/out.crd'
return os.path.join(str(tmpdir), 'test.crd')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why this change? iirc tmpdir is a Path object so should work ok as is?

Copy link
Contributor Author

@mdpoleto mdpoleto Apr 16, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From what I understood from @tylerjereddy 's comment, using this construct is preferred since it does not leave artifacts behind. See full discussion at: #3635 (comment)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was indeed what @tylerjereddy suggested.

Maybe @richardjgowers was thinking to use

return str(tmpdir / 'test.crd')

We do need a str for Universe as we cannot use py.path or pathfile objects yet.

By the way, please remove commented out code.


def test_write_atoms(self, u, outfile):
# Test that written file when read gives same coordinates
Expand All @@ -66,11 +69,36 @@ def CRD_iter(fn):
for ref, other in zip(CRD_iter(CRD), CRD_iter(outfile)):
assert ref == other

def test_write_EXT(self):
# TODO: Write tests that use EXT output format
# Must have *lots* of atoms, maybe fake the system
# to make tests faster
pass
def test_write_EXT(self, u, outfile):
# Use the `extended` keyword to force the EXT format
u.atoms.write(outfile, extended=True)

with open(outfile, 'r') as inf:
format_line = inf.readlines()[2]
assert 'EXT' in format_line, "EXT format expected"
orbeckst marked this conversation as resolved.
Show resolved Hide resolved

def test_read_EXT(self, u, outfile):
mdpoleto marked this conversation as resolved.
Show resolved Hide resolved
# Read EXT format and check atom positions
u.atoms.write(outfile, extended=True)

u2 = mda.Universe(outfile)

sel1 = u.select_atoms('all')
sel2 = u2.select_atoms('all')

# Rounding floats since EXT format support more decimals
orbeckst marked this conversation as resolved.
Show resolved Hide resolved
cog1 = np.around(sel1.center_of_geometry(),6)
cog2 = np.around(sel2.center_of_geometry(),6)
mdpoleto marked this conversation as resolved.
Show resolved Hide resolved

assert_equal(len(u.atoms), len(u2.atoms)), 'Equal number of '\
'atoms expected in both CRD formats'
assert_equal(len(u.atoms.residues),
len(u2.atoms.residues)), 'Equal number of residues expected in'\
'both CRD formats'
assert_equal(len(u.atoms.segments),
len(u2.atoms.segments)), 'Equal number of segments expected in'\
'both CRD formats'
assert_equal(cog1, cog2), 'Same centroid expected for both CRD formats'
mdpoleto marked this conversation as resolved.
Show resolved Hide resolved


class TestCRDWriterMissingAttrs(object):
Expand Down