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

different atom number error message #2219

Merged
merged 4 commits into from
Apr 6, 2019
Merged
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
2 changes: 2 additions & 0 deletions package/AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,8 @@ Chronological list of authors
2019
- Ninad Bhat
- Fenil Suchak
- Yibo Zhang


External code
-------------
Expand Down
2 changes: 2 additions & 0 deletions package/CHANGELOG
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,8 @@ Fixes
Timestep.dimensions for all Universes and Readers (Issue #2190, PR #2213)
* Fixed analysis.nucleic.hydroxyl() function: correctly recognize the
2'-hydroxyl hydrogen H2'; gave wrong results (Issue #2218)
* A clear message that the .pdb universe cannot contain a different number of
atoms from frame to frame (Issue #1998, PR #2219)

Deprecations
* analysis.polymer.PersistenceLength no longer requires the perform_fit()
Expand Down
15 changes: 9 additions & 6 deletions package/MDAnalysis/coordinates/PDB.py
Original file line number Diff line number Diff line change
Expand Up @@ -393,14 +393,17 @@ def _read_frame(self, frame):
line[24:33], line[33:40],
line[40:47], line[47:54]]

# doing the conversion from list to array at the end is faster
self.ts.positions = tmp_buf

# check if atom number changed
if pos != self.n_atoms:
raise ValueError("Read an incorrect number of atoms\n"
"Expected {expected} got {actual}"
"".format(expected=self.n_atoms, actual=pos+1))
raise ValueError("Inconsistency in file '{}': The number of atoms "
"({}) in trajectory frame {} differs from the "
"number of atoms ({}) in the corresponding "
"topology.\nTrajectories with varying numbers of "
"atoms are currently not supported."
"".format(self.filename, pos, frame, self.n_atoms))

# doing the conversion from list to array at the end is faster
self.ts.positions = tmp_buf

if self.convert_units:
# both happen inplace
Expand Down
19 changes: 19 additions & 0 deletions testsuite/MDAnalysisTests/coordinates/test_pdb.py
Original file line number Diff line number Diff line change
Expand Up @@ -894,3 +894,22 @@ def test_write_pdb_zero_atoms():
with mda.Writer(outfile, ag.n_atoms) as w:
with pytest.raises(IndexError):
w.write(ag)


def test_atom_not_match(tmpdir):
# issue 1998
outfile = str(tmpdir.mkdir("PDBReader").join('test_atom_not_match' + ".pdb"))
u = mda.Universe(PSF, DCD)
# select two groups of atoms
protein = u.select_atoms("protein and name CA")
atoms = u.select_atoms(
'resid 1 or resid 10 or resid 100 or resid 1000 or resid 10000')
with mda.Writer(outfile, multiframe=True, n_atoms=10) as pdb:
# write these two groups of atoms to pdb
# Then the n_atoms will not match
pdb.write(protein)
pdb.write(atoms)
reader = mda.coordinates.PDB.PDBReader(outfile)
with pytest.raises(ValueError) as excinfo:
reader._read_frame(1)
assert 'Inconsistency in file' in str(excinfo.value)