Skip to content

Commit

Permalink
Resolve wrongly merging bond/angle/dihedral types (#4003)
Browse files Browse the repository at this point in the history
* Resolve wrongly merging bond/angle/dihedral types

* Add test for _removeDupes with lammps

* Added clarifying print statements

* Remove debugging if statements

* Update test_topologyobjects.py

* Update test_topologyobjects.py

* Add clarifying comments

* Fixed CHANGELOG

* Add test for _removeDupes with lammps

* Remove print statements

* Update CHANGELOG to remove dupliucate entry

---------

Co-authored-by: Hugo MacDermott-Opeskin <hugomacdermott@gmail.com>
  • Loading branch information
jaclark5 and hmacdope authored Jun 17, 2023
1 parent 58bbc44 commit 2d654ae
Show file tree
Hide file tree
Showing 5 changed files with 227 additions and 2 deletions.
3 changes: 2 additions & 1 deletion package/CHANGELOG
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,12 @@ The rules for this file:
* release numbers follow "Semantic Versioning" http://semver.org

------------------------------------------------------------------------------
??/??/?? IAlibay
??/??/?? IAlibay, jaclark5

* 2.6.0

Fixes
* Fix deletion of bond/angle/dihedral types (PR #4003, Issue #4001)

Enhancements

Expand Down
10 changes: 9 additions & 1 deletion package/MDAnalysis/core/topologyobjects.py
Original file line number Diff line number Diff line change
Expand Up @@ -445,7 +445,15 @@ def __init__(self, topologygroup):
except KeyError:
self.dict[btype] = [b]

self._removeDupes()
# Some force field types define bonds with a type
# (Ex. 1: 12 or 21), while others define with a tuple of atom types
# (Ex. 2: ("H", "O") or ("O", "H")). If the bond type is a tuple
# then the bond types in our second example are equivalent and one
# should be removed. If the bonds are defined as an integer then
# our first example would also be combined if `_removeDupes()`
# is run.
if self.dict and isinstance(list(self.dict.keys())[0], tuple):
self._removeDupes()

def _removeDupes(self):
"""Sorts through contents and makes sure that there are
Expand Down
10 changes: 10 additions & 0 deletions testsuite/MDAnalysisTests/core/test_topologyobjects.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@

import MDAnalysis as mda
from MDAnalysis.lib.distances import calc_bonds, calc_angles, calc_dihedrals
from MDAnalysisTests.datafiles import LAMMPSdata_many_bonds
from MDAnalysis.core.topologyobjects import (
TopologyGroup, TopologyObject, TopologyDict,
# TODO: the following items are not used
Expand Down Expand Up @@ -300,6 +301,15 @@ def test_bond_reversal(self, PSFDCD, b_td):
tg2 = b_td[b[::-1]]
assert tg1 == tg2

# This test will pass as long as `TopologyDict._removeDupes()` is
# not run. Otherwise bond type 12 and 21 will be seen as duplicates
# and combined.
def test_bond_no_reversal(self):
universe = mda.Universe(LAMMPSdata_many_bonds, format="DATA")
nbonds = 22
bondtypes = universe.atoms.bonds.types()
assert len(bondtypes) == nbonds

def test_angles_types(self, PSFDCD):
"""TopologyDict for angles"""
assert len(PSFDCD.atoms.angles.types()) == 130
Expand Down
204 changes: 204 additions & 0 deletions testsuite/MDAnalysisTests/data/lammps/a_lot_of_bond_types.data
Original file line number Diff line number Diff line change
@@ -0,0 +1,204 @@
LAMMPS data file generated by OpenBabel
28 atoms
27 bonds
44 angles
61 dihedrals
0 impropers
9 atom types
22 bond types
36 angle types
49 dihedral types
0 improper types
-6.91004 5.99665 xlo xhi
-6.91004 5.99665 ylo yhi
-6.91004 5.99665 zlo zhi



Masses

1 79.904 # Br
2 12.0107 # C
3 35.453 # Cl
4 18.9984 # F
5 1.00794 # H
6 14.0067 # N
7 15.9994 # O
8 30.9738 # P
9 32.065 # S


Atoms

1 1 2 0.00000 -1.23707 1.11411 -0.08956 # C
2 1 2 0.00000 -2.12077 -0.27264 0.08817 # C
3 1 2 0.00000 -1.74311 -1.09893 1.38852 # C
4 1 5 0.41000 -2.18526 -0.62143 2.28556 # H
5 1 4 0.00000 -2.25633 -2.37277 1.29938 # F
6 1 1 0.00000 0.09936 -1.55412 1.81908 # Br
7 1 9 0.00000 -4.07565 -0.30488 0.00221 # S
8 1 7 -0.82000 -5.81365 -0.48635 -0.01704 # O
9 1 1 0.00000 -6.41004 -0.99147 -1.70511 # Br
10 1 7 -0.82000 -3.96870 1.41535 0.18342 # O
11 1 6 0.00000 -4.98376 1.90832 -0.59681 # N
12 1 5 0.41000 -5.81819 2.06218 0.01623 # H
13 1 8 0.00000 -4.57772 3.50859 -1.33780 # P
14 1 5 0.41000 -5.78917 3.47542 -2.14332 # H
15 1 5 0.41000 -4.80980 4.20632 -0.08001 # H
16 1 1 0.00000 -4.02071 5.49665 -2.30091 # Br
17 1 7 -0.82000 -4.22298 -2.03748 -0.17558 # O
18 1 3 0.00000 -4.91536 -2.74247 1.23284 # Cl
19 1 3 0.00000 -1.67757 -1.28048 -1.34036 # Cl
20 1 8 0.00000 -1.35687 2.50643 1.21376 # P
21 1 4 0.00000 -2.70153 3.42513 0.73504 # F
22 1 7 -0.82000 -0.30576 3.58278 0.31747 # O
23 1 4 0.00000 0.91763 3.37723 0.77456 # F
24 1 7 -0.82000 -1.47682 1.68843 -1.35335 # O
25 1 5 0.41000 -2.39092 1.46822 -1.65330 # H
26 1 6 0.00000 0.21540 0.84629 -0.04018 # N
27 1 5 0.41000 0.62043 0.77000 0.92091 # H
28 1 4 0.00000 0.67813 -0.11355 -0.86675 # F


Bonds

1 6 2 1 # N: C
2 8 3 1 # O: C
3 9 3 4 # O: H
4 4 5 2 # F: N
5 7 2 6 # N: H
6 11 7 1 # P: C
7 15 7 8 # P: O
8 12 7 9 # P: F
9 5 10 8 # F: O
10 1 1 11 # C: C
11 21 12 11 # Cl: C
12 16 13 11 # S: C
13 17 13 14 # S: O
14 17 13 15 # S: O
15 17 13 16 # S: O
16 19 17 16 # Br: O
17 1 11 18 # C: C
18 18 19 18 # Br: C
19 3 20 18 # F: C
20 2 18 21 # C: H
21 10 15 22 # O: N
22 22 23 14 # Cl: O
23 14 24 22 # P: N
24 7 22 25 # N: H
25 20 26 24 # Br: P
26 13 24 27 # P: H
27 13 24 28 # P: H


Angles

1 15 3 1 2 # O: C: N
2 21 7 1 2 # P: C: N
3 12 2 1 11 # N: C: C
4 22 7 1 3 # P: C: O
5 14 3 1 11 # O: C: C
6 20 7 1 11 # P: C: C
7 7 5 2 1 # F: N: C
8 3 1 2 6 # C: N: H
9 8 5 2 6 # F: N: H
10 4 1 3 4 # C: O: H
11 17 8 7 1 # O: P: C
12 9 9 7 1 # F: P: C
13 10 9 7 8 # F: P: O
14 25 7 8 10 # P: O: F
15 34 12 11 1 # Cl: C: C
16 26 13 11 1 # S: C: C
17 1 18 11 1 # C: C: C
18 35 12 11 13 # Cl: C: S
19 34 12 11 18 # Cl: C: C
20 26 13 11 18 # S: C: C
21 18 14 13 11 # O: S: C
22 18 15 13 11 # O: S: C
23 18 16 13 11 # O: S: C
24 19 15 13 14 # O: S: O
25 19 16 13 14 # O: S: O
26 19 16 13 15 # O: S: O
27 36 23 14 13 # Cl: O: S
28 27 13 15 22 # S: O: N
29 31 17 16 13 # Br: O: S
30 28 19 18 11 # Br: C: C
31 5 20 18 11 # F: C: C
32 2 11 18 21 # C: C: H
33 29 19 18 20 # Br: C: F
34 30 19 18 21 # Br: C: H
35 6 20 18 21 # F: C: H
36 24 24 22 15 # P: N: O
37 16 15 22 25 # O: N: H
38 23 24 22 25 # P: N: H
39 33 26 24 22 # Br: P: N
40 13 22 24 27 # N: P: H
41 13 22 24 28 # N: P: H
42 32 26 24 27 # Br: P: H
43 32 26 24 28 # Br: P: H
44 11 28 24 27 # H: P: H


Dihedrals

1 6 5 1 2 3 # F: C: N: O
2 17 3 1 2 6 # O: C: N: H
3 25 7 1 2 5 # P: C: N: F
4 26 7 1 2 6 # P: C: N: H
5 5 5 1 2 11 # F: C: N: C
6 2 11 1 2 6 # C: C: N: H
7 14 2 1 3 4 # N: C: O: H
8 27 7 1 3 4 # P: C: O: H
9 3 11 1 3 4 # C: C: O: H
10 19 8 1 7 2 # O: C: P: N
11 8 9 1 7 2 # F: C: P: N
12 20 8 1 7 3 # O: C: P: O
13 9 9 1 7 3 # F: C: P: O
14 18 8 1 7 11 # O: C: P: C
15 7 9 1 7 11 # F: C: P: C
16 10 10 7 8 1 # F: P: O: C
17 11 10 7 8 9 # F: P: O: F
18 44 12 1 11 2 # Cl: C: C: N
19 30 13 1 11 2 # S: C: C: N
20 13 2 1 11 18 # N: C: C: C
21 45 12 1 11 3 # Cl: C: C: O
22 31 13 1 11 3 # S: C: C: O
23 16 3 1 11 18 # O: C: C: C
24 46 12 1 11 7 # Cl: C: C: P
25 32 13 1 11 7 # S: C: C: P
26 24 7 1 11 18 # P: C: C: C
27 21 14 11 13 1 # O: C: S: C
28 21 15 11 13 1 # O: C: S: C
29 21 16 11 13 1 # O: C: S: C
30 47 12 11 13 14 # Cl: C: S: O
31 47 12 11 13 15 # Cl: C: S: O
32 47 12 11 13 16 # Cl: C: S: O
33 21 14 11 13 18 # O: C: S: C
34 21 15 11 13 18 # O: C: S: C
35 21 16 11 13 18 # O: C: S: C
36 48 23 13 14 11 # Cl: S: O: C
37 49 23 13 14 15 # Cl: S: O: O
38 49 23 13 14 16 # Cl: S: O: O
39 15 22 13 15 11 # N: S: O: C
40 23 14 13 15 22 # O: S: O: N
41 23 16 13 15 22 # O: S: O: N
42 40 17 13 16 11 # Br: S: O: C
43 41 17 13 16 14 # Br: S: O: O
44 41 17 13 16 15 # Br: S: O: O
45 35 19 11 18 1 # Br: C: C: C
46 4 20 11 18 1 # F: C: C: C
47 1 1 11 18 21 # C: C: C: H
48 37 19 11 18 12 # Br: C: C:Cl
49 42 12 11 18 20 # Cl: C: C: F
50 43 12 11 18 21 # Cl: C: C: H
51 36 19 11 18 13 # Br: C: C: S
52 28 13 11 18 20 # S: C: C: F
53 29 13 11 18 21 # S: C: C: H
54 34 13 15 22 24 # S: O: N: P
55 33 13 15 22 25 # S: O: N: H
56 39 26 22 24 15 # Br: N: P: O
57 22 15 22 24 27 # O: N: P: H
58 22 15 22 24 28 # O: N: P: H
59 38 26 22 24 25 # Br: N: P: H
60 12 27 22 24 25 # H: N: P: H
61 12 28 22 24 25 # H: N: P: H
2 changes: 2 additions & 0 deletions testsuite/MDAnalysisTests/datafiles.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@
"LAMMPSdata2", "LAMMPSdcd2",
"LAMMPScnt", "LAMMPScnt2", # triclinic box
"LAMMPShyd", "LAMMPShyd2",
"LAMMPSdata_many_bonds",
"LAMMPSdata_deletedatoms", # with deleted atoms
"LAMMPSdata_triclinic", # lammpsdata file to test triclinic dimension parsing, albite with most atoms deleted
"LAMMPSdata_PairIJ", # lammps datafile with a PairIJ Coeffs section
Expand Down Expand Up @@ -526,6 +527,7 @@
LAMMPSDUMP_triclinic = (_data_ref / "lammps/albite_triclinic.dump").as_posix()
LAMMPSDUMP_image_vf = (_data_ref / "lammps/image_vf.lammpstrj").as_posix()
LAMMPS_image_vf = (_data_ref / "lammps/image_vf.data").as_posix()
LAMMPSdata_many_bonds = (_data_ref / "lammps/a_lot_of_bond_types.data").as_posix()

unordered_res = (_data_ref / "unordered_res.pdb").as_posix()

Expand Down

0 comments on commit 2d654ae

Please sign in to comment.