Coverage for cvpack/torsion_similarity.py: 100%

19 statements  

« prev     ^ index     » next       coverage.py v7.16.2, created at 2026-10-21 18:13 +0000

1""" 

2.. class:: TorsionSimilarity 

3 :platform: Linux, MacOS, Windows 

4 :synopsis: The degree of similarity between pairs of torsion angles. 

5 

6.. classauthor:: Charlles Abreu <craabreu@gmail.com> 

7 

8""" 

9 

10from __future__ import annotations 

11 

12import openmm 

13from numpy.typing import ArrayLike 

14from openmm import unit as mmunit 

15 

16from .collective_variable import CollectiveVariable 

17 

18 

19class TorsionSimilarity(CollectiveVariable, openmm.CustomCompoundBondForce): 

20 r""" 

21 The degree of similarity between `n` pairs of torsion angles: 

22 

23 .. math:: 

24 

25 s({\bf r}) = \frac{n}{2} + \frac{1}{2} \sum_{i=1}^n \cos\Big( 

26 \phi^{\rm 1st}_i({\bf r}) - \phi^{\rm 2nd}_i({\bf r}) 

27 \Big) 

28 

29 where :math:`\phi^{\rm kth}_i` is the torsion angle at position :math:`i` in the 

30 :math:`k`-th list. 

31 

32 .. note:: 

33 

34 In `PLUMED <https://www.plumed.org/doc-v2.8/user-doc/html/_colvar.html>`_, this 

35 collective variable is called ``DIHCOR``. 

36 

37 Parameters 

38 ---------- 

39 firstList 

40 A list of :math:`n` tuples of four atom indices defining the first torsion 

41 angle in each pair. 

42 secondList 

43 A list of :math:`n` tuples of four atom indices defining the second torsion 

44 angle in each pair. 

45 pbc 

46 Whether to use periodic boundary conditions in distance calculations. 

47 name 

48 The name of the collective variable. 

49 

50 Raises 

51 ------ 

52 ValueError 

53 If firstList and secondList have different lengths 

54 

55 Example 

56 ------- 

57 >>> import cvpack 

58 >>> import mdtraj 

59 >>> import openmm 

60 >>> import openmm.unit as mmunit 

61 >>> from openmmtools import testsystems 

62 >>> model = testsystems.LysozymeImplicit() 

63 >>> traj = mdtraj.Trajectory( 

64 ... model.positions, mdtraj.Topology.from_openmm(model.topology) 

65 ... ) 

66 >>> phi_atoms, _ = mdtraj.compute_phi(traj) 

67 >>> valid_atoms = traj.top.select("resid 59 to 79 and backbone") 

68 >>> phi_atoms = [ 

69 ... phi 

70 ... for phi in phi_atoms 

71 ... if all(atom in valid_atoms for atom in phi) 

72 ... ] 

73 >>> torsion_similarity = cvpack.TorsionSimilarity( 

74 ... phi_atoms[1:], phi_atoms[:-1] 

75 ... ) 

76 >>> torsion_similarity.addToSystem(model.system) 

77 >>> integrator = openmm.VerletIntegrator(0) 

78 >>> platform = openmm.Platform.getPlatformByName("Reference") 

79 >>> context = openmm.Context(model.system, integrator, platform) 

80 >>> context.setPositions(model.positions) 

81 >>> torsion_similarity.getValue(context) 

82 18.659... dimensionless 

83 """ 

84 

85 def __init__( 

86 self, 

87 firstList: ArrayLike, 

88 secondList: ArrayLike, 

89 pbc: bool = False, 

90 name: str = "torsion_similarity", 

91 ) -> None: 

92 firstList = [list(map(int, first)) for first in firstList] 

93 secondList = [list(map(int, second)) for second in secondList] 

94 if len(firstList) != len(secondList): 

95 raise ValueError("firstList and secondList must have the same length") 

96 function = "0.5*(1 + cos(delta))" 

97 definition = "delta = dihedral(p1, p2, p3, p4) - dihedral(p5, p6, p7, p8)" 

98 super().__init__(8, f"{function}; {definition}") 

99 for first, second in zip(firstList, secondList): 

100 self.addBond([*first, *second], []) 

101 self.setUsesPeriodicBoundaryConditions(pbc) 

102 self._registerCV( 

103 name, 

104 mmunit.dimensionless, 

105 firstList=firstList, 

106 secondList=secondList, 

107 pbc=pbc, 

108 ) 

109 

110 

111TorsionSimilarity.registerTag("!cvpack.TorsionSimilarity")