From 59916ed93e36788d4ce7ff129c686096df8c5960 Mon Sep 17 00:00:00 2001 From: Richard Gowers Date: Sun, 21 Aug 2016 10:13:52 +0100 Subject: [PATCH] Fixes for Issue #936 CONECT records to unknown entries (hopefully TER) are now ignored --- package/CHANGELOG | 2 ++ package/MDAnalysis/topology/PDBParser.py | 10 +++++-- testsuite/MDAnalysisTests/data/CONECT2TER.pdb | 12 ++++++++ testsuite/MDAnalysisTests/datafiles.py | 2 ++ .../MDAnalysisTests/topology/test_pdb.py | 30 +++++++++++++++++++ 5 files changed, 54 insertions(+), 2 deletions(-) create mode 100644 testsuite/MDAnalysisTests/data/CONECT2TER.pdb create mode 100644 testsuite/MDAnalysisTests/topology/test_pdb.py diff --git a/package/CHANGELOG b/package/CHANGELOG index e554652b123..44e145f6e20 100644 --- a/package/CHANGELOG +++ b/package/CHANGELOG @@ -44,6 +44,8 @@ Fixes defaults (#914) * Fixed same interRDF can be run twice (Issue #924) * Support for TPR files produced by Gromacs-2016 (Issue #932) + * Fixed parsing PDB files with CONECT records to TER entries (Issue #936) + Changes * Added protected variable _frame_index to to keep track of frame iteration number in AnalysisBase diff --git a/package/MDAnalysis/topology/PDBParser.py b/package/MDAnalysis/topology/PDBParser.py index 7a38420ea66..67134e0e067 100644 --- a/package/MDAnalysis/topology/PDBParser.py +++ b/package/MDAnalysis/topology/PDBParser.py @@ -180,8 +180,14 @@ def _parsebonds(self, atoms): for line in lines: atom, atoms = _parse_conect(line.strip()) for a in atoms: - bond = tuple([mapping[atom], mapping[a]]) - bonds.add(bond) + try: + bond = tuple([mapping[atom], mapping[a]]) + except KeyError: + # Bonds to TER records have no mapping + # Ignore these as they are not real atoms + pass + else: + bonds.add(bond) bonds = tuple(bonds) diff --git a/testsuite/MDAnalysisTests/data/CONECT2TER.pdb b/testsuite/MDAnalysisTests/data/CONECT2TER.pdb new file mode 100644 index 00000000000..cd56c7d6929 --- /dev/null +++ b/testsuite/MDAnalysisTests/data/CONECT2TER.pdb @@ -0,0 +1,12 @@ +HEADER ISSUE936 Test File +REMARK Contains a CONECT record to a TER entry +MODEL 1 +CRYST1 216.489 216.489 216.489 90.00 90.00 90.00 P 1 1 +ATOM 1 CA ABC A 1 111.252 98.373 98.187 +ATOM 2 CA ABC A 1 111.203 101.742 96.430 +ATOM 3 CA ABC A 1 107.607 102.968 96.316 +TER 4 ABC A 1 +CONECT 1 2 +CONECT 2 1 3 +CONECT 3 2 4 +ENDMDL diff --git a/testsuite/MDAnalysisTests/datafiles.py b/testsuite/MDAnalysisTests/datafiles.py index e4b83e30f25..328f26cd496 100644 --- a/testsuite/MDAnalysisTests/datafiles.py +++ b/testsuite/MDAnalysisTests/datafiles.py @@ -43,6 +43,7 @@ "PDB_multiframe", "PDB_helix", "PDB_conect", + "PDB_conect2TER", # Conect record to a TER entry (Issue 936) "XPDB_small", "PDB_full", # PDB 4E43 (full HEADER, TITLE, COMPND, REMARK, altloc) "ALIGN", # Various way to align atom names in PDB files @@ -172,6 +173,7 @@ PDB_multiframe = resource_filename(__name__, 'data/nmr_neopetrosiamide.pdb') PDB_helix = resource_filename(__name__, 'data/A6PA6_alpha.pdb') PDB_conect = resource_filename(__name__, 'data/conect_parsing.pdb') +PDB_conect2TER = resource_filename(__name__, 'data/CONECT2TER.pdb') GRO = resource_filename(__name__, 'data/adk_oplsaa.gro') GRO_velocity = resource_filename(__name__, 'data/sample_velocity_file.gro') diff --git a/testsuite/MDAnalysisTests/topology/test_pdb.py b/testsuite/MDAnalysisTests/topology/test_pdb.py new file mode 100644 index 00000000000..afd13bf5bce --- /dev/null +++ b/testsuite/MDAnalysisTests/topology/test_pdb.py @@ -0,0 +1,30 @@ +# -*- Mode: python; tab-width: 4; indent-tabs-mode:nil; coding:utf-8 -*- +# vim: tabstop=4 expandtab shiftwidth=4 softtabstop=4 fileencoding=utf-8 +# +# MDAnalysis --- http://www.MDAnalysis.org +# Copyright (c) 2006-2015 Naveen Michaud-Agrawal, Elizabeth J. Denning, Oliver Beckstein +# and contributors (see AUTHORS for the full list) +# +# Released under the GNU Public Licence, v2 or any higher version +# +# Please cite your use of MDAnalysis in published work: +# N. Michaud-Agrawal, E. J. Denning, T. B. Woolf, and O. Beckstein. +# MDAnalysis: A Toolkit for the Analysis of Molecular Dynamics Simulations. +# J. Comput. Chem. 32 (2011), 2319--2327, doi:10.1002/jcc.21787 +# + +from numpy.testing import assert_ +from MDAnalysisTests.datafiles import ( + PDB_conect2TER, +) + +import MDAnalysis as mda +from MDAnalysis.topology.PDBParser import PDBParser + + +def test_conect2ter(): + with PDBParser(PDB_conect2TER) as p: + struc = p.parse() + + assert_('bonds' in struc) + assert_(len(struc['bonds']) == 4)