IMP logo
IMP Reference Guide  develop.d36d3deffb,2026/11/09
The Integrative Modeling Platform
CHARMMParameters.h
Go to the documentation of this file.
1 /**
2  * \file IMP/atom/CHARMMParameters.h
3  * \brief access to CHARMM force field parameters
4  *
5  * Copyright 2007-2026 IMP Inventors. All rights reserved.
6  *
7  */
8 
9 #ifndef IMPATOM_CHARMM_PARAMETERS_H
10 #define IMPATOM_CHARMM_PARAMETERS_H
11 
12 #include "internal/charmm_helpers.h"
13 #include "ForceFieldParameters.h"
14 #include "charmm_topology.h"
15 #include "atom_macros.h"
16 #include <cereal/access.hpp>
17 #include <IMP/file.h>
18 
19 #include <array>
20 #include <map>
21 #include <string>
22 // swig is being dumb
23 #ifdef SWIG
24 IMPKERNEL_BEGIN_NAMESPACE
25 class VersionInfo;
26 IMPKERNEL_END_NAMESPACE
27 #endif
28 
29 IMPATOM_BEGIN_NAMESPACE
30 
31 //! The parameters for a CHARMM bond or angle.
33  double force_constant;
34  double ideal;
37  out << "force constant: " << force_constant << "; ideal value: " << ideal;
38  });
39 
40 private:
41  friend class cereal::access;
42 
43  template<class Archive> void serialize(Archive &ar) {
44  ar(force_constant, ideal);
45  }
46 };
47 
49 
50 //! The parameters for a CHARMM dihedral or improper.
52  double force_constant;
53  int multiplicity;
54  double ideal;
57  out << "force constant: " << force_constant
58  << "; multiplicity: " << multiplicity << "; ideal value: " << ideal;
59  });
60 
61 private:
62  friend class cereal::access;
63 
64  template<class Archive> void serialize(Archive &ar) {
65  ar(force_constant, multiplicity, ideal);
66  }
67 };
68 
70 
71 class CHARMMTopology;
72 
73 //! CHARMM force field parameters.
74 /** This class reads in topology and parameter files in CHARMM format and
75  stores the information.
76 
77  It does not actually evaluate the force field itself - there are other
78  classes that use the parameters provided by this class to do that. For
79  example, the LennardJonesTypedPairScore or CoulombPairScore evaluate the
80  nonbond terms of the CHARMM force field, while
81  CHARMMStereochemistryRestraint (or BondSingletonScore, AngleSingletonScore,
82  DihedralSingletonScore and ImproperSingletonScore) cover the bond terms.
83 
84  Typically, the create_topology() method is used to create a new
85  CHARMMTopology object for a given Hierarchy; that object can then be
86  used to assign atomic radii, bonds, etc.
87  */
88 class IMPATOMEXPORT CHARMMParameters : public ForceFieldParameters {
89  std::map<std::string, Element> atom_type_to_element_;
90  std::map<ResidueType, Pointer<CHARMMIdealResidueTopology>>
91  residue_topologies_;
92  std::map<std::string, Pointer<CHARMMPatch>> patches_;
93  std::map<internal::CHARMMBondNames, CHARMMBondParameters> bond_parameters_;
94  std::map<internal::CHARMMAngleNames, CHARMMBondParameters> angle_parameters_;
95 
96  typedef Vector<std::pair<internal::CHARMMDihedralNames,
98  DihedralParameters dihedral_parameters_;
99  DihedralParameters improper_parameters_;
100 
101  DihedralParameters::const_iterator find_dihedral(
102  DihedralParameters::const_iterator begin,
103  DihedralParameters::const_iterator end,
104  const internal::CHARMMDihedralNames &dihedral,
105  bool allow_wildcards) const;
106 
107  CHARMMDihedralParametersList find_dihedral_parameters(
108  const internal::CHARMMDihedralNames &types) const;
109 
110  // Results of get_dihedral_parameters(), since searching the list of
111  // parameters is slow, and a system typically contains only a small number
112  // of unique combinations of types. The parameters do not change once the
113  // parameter file has been read, so the cache never needs to be invalidated.
114  // Keys are the atom types in forward or reverse order, whichever sorts
115  // first, since the same parameters match in either order.
116  mutable std::map<std::array<std::string, 4>, CHARMMDihedralParametersList>
117  dihedral_parameters_cache_;
118 
119  public:
120  //! Construction with CHARMM topology (and optionally parameters) file.
121  /** For addition of atom types, the topology file alone is enough;
122  for adding bonds and radii, both files are needed.
123 
124  Atom and residue naming in the topology file differs slightly between
125  CHARMM and PDB. If translate_names_to_pdb is set to true, some simple
126  translations are done to map CHARMM-style names to PDB-style for
127  standard amino acids and some other commonly-used residues and patches.
128  (This translation has already been done for the topology files included
129  with IMP and MODELLER, so it is only needed for topology files taken
130  from CHARMM itself or other sources.) The modifications are as follows:
131  - CHARMM HSD (unprotonated histidine) is mapped to PDB HIS.
132  - CD1 and CD2 atoms in LEU are swapped.
133  - OT1 and OT2 in CTER are mapped to O and OXT.
134  - CHARMM hydrogen names are mapped to PDB equivalents.
135  - CHARMM NTER, GLYP and CTER residues are modified slightly to avoid
136  removing the HN, HN and O atoms respectively, and adding excess bonds
137  to these atoms.
138  */
139  CHARMMParameters(TextInput topology_file_name,
140  TextInput par_file_name = TextInput(),
141  bool translate_names_to_pdb = false);
142 
143  /** \name Residue topology
144 
145  The class stores the topology of each residue type as defined in the
146  topology file, as a set of CHARMMIdealResidueTopology objects.
147  */
148  /**@{*/
149  void add_residue_topology(CHARMMIdealResidueTopology *res) {
150  res->set_was_used(true);
151  residue_topologies_.insert(
152  std::make_pair(ResidueType(res->get_type()), res));
153  }
154 
155  CHARMMIdealResidueTopology *get_residue_topology(ResidueType type) const {
156  std::map<ResidueType,
157  Pointer<CHARMMIdealResidueTopology>>::const_iterator it =
158  residue_topologies_.find(type);
159  if (it != residue_topologies_.end()) {
160  return it->second;
161  } else {
162  IMP_THROW("Residue " << type << " does not exist", ValueException);
163  }
164  }
165 
166  /** \name Patches
167 
168  The class stores patches as defined in the topology file, as a set of
169  CHARMMPatch objects.
170  */
171  /**@{*/
172  void add_patch(CHARMMPatch *patch) {
173  patch->set_was_used(true);
174  patches_.insert(std::make_pair(patch->get_type(), patch));
175  }
176 #if 0
177  // return of non const ref values is not allowed
178  CHARMMPatch &get_patch(std::string name) {
179  std::map<std::string, CHARMMPatch>::iterator it = patches_.find(name);
180  if (it != patches_.end()) {
181  return it->second;
182  } else {
183  IMP_THROW("Patch " << name << " does not exist", ValueException);
184  }
185  }
186 #endif
187 
188  CHARMMPatch *get_patch(std::string name) const {
189  std::map<std::string, Pointer<CHARMMPatch>>::const_iterator it =
190  patches_.find(name);
191  if (it != patches_.end()) {
192  return it->second;
193  } else {
194  IMP_THROW("Patch " << name << " does not exist", ValueException);
195  }
196  }
197  /**@}*/
198 
199  //! Create topology that corresponds to the primary sequence of the Hierarchy.
200  /** Residues are placed in different segments if they are deemed to be
201  "disconnected", i.e. they have different Chains as parents, they are
202  in different Fragments and the fragments are not consecutive (the last
203  residue index of the first Fragment and the first index of the second
204  Fragment are not consecutive), or their ancestors are different
205  (e.g. one Residue lives in a Chain and another does not have a Chain
206  parent) */
207  CHARMMTopology *create_topology(Hierarchy hierarchy) const;
208 
209  //! Get bond parameters for the bond between the two given CHARMM atom types.
210  /** The atom types may match in any order.
211  \throws IndexException if no parameters are present.
212  */
213  const CHARMMBondParameters &get_bond_parameters(std::string type1,
214  std::string type2) const {
215  internal::CHARMMBondNames types = internal::CHARMMBondNames(type1, type2);
216  if (bond_parameters_.find(types) != bond_parameters_.end()) {
217  return bond_parameters_.find(types)->second;
218  } else {
219  IMP_THROW("No CHARMM parameters found for bond " << type1 << "-" << type2,
221  }
222  }
223 
224  //! Get parameters for the angle between the three given CHARMM atom types.
225  /** The atom types may match in either forward or reverse order.
226  \throws IndexException if no parameters are present.
227  */
228  const CHARMMBondParameters &get_angle_parameters(std::string type1,
229  std::string type2,
230  std::string type3) const {
231  internal::CHARMMAngleNames types =
232  internal::CHARMMAngleNames(type1, type2, type3);
233  if (angle_parameters_.find(types) != angle_parameters_.end()) {
234  return angle_parameters_.find(types)->second;
235  } else {
236  IMP_THROW("No CHARMM parameters found for angle " << type1 << "-" << type2
237  << "-" << type3,
239  }
240  }
241 
242  //! Get parameters for the dihedral between the four given CHARMM atom types.
243  /** The atom types may match in either forward or reverse order. When
244  looking for a match in the library, wildcards are considered; an atom
245  type of X in the library will match any atom type. The most specific
246  match from the library is returned.
247 
248  Multiple sets of parameters can be specified for the same combination
249  of atom types in the library, in which case all of them are returned.
250 
251  \throws IndexException if no parameters are present.
252  */
253  CHARMMDihedralParametersList get_dihedral_parameters(
254  std::string type1, std::string type2, std::string type3,
255  std::string type4) const;
256 
257  //! Get parameters for the improper between the four given CHARMM atom types.
258  /** The atom types may match in either forward or reverse order. When
259  looking for a match in the library, wildcards are considered; an atom
260  type of X in the library will match any atom type. The most specific
261  match from the library is returned.
262 
263  \throws IndexException if no parameters are present.
264  */
266  std::string type1, std::string type2, std::string type3,
267  std::string type4) const {
268  internal::CHARMMDihedralNames types =
269  internal::CHARMMDihedralNames(type1, type2, type3, type4);
270  // Return just the first match; wildcards are OK
271  DihedralParameters::const_iterator it = find_dihedral(
272  improper_parameters_.begin(), improper_parameters_.end(), types, true);
273  if (it != improper_parameters_.end()) {
274  return it->second;
275  } else {
276  IMP_THROW("No CHARMM parameters found for improper "
277  << type1 << "-" << type2 << "-" << type3 << "-" << type4,
279  }
280  }
281 
282  //! Auto-generate Angle particles from the passed list of Bond particles.
283  /** The angles consist of all unique pairs of bonds which share an
284  endpoint. If no parameters are found for an angle, it is simply
285  created without those parameters.
286 
287  The list of newly-created Angle particles can be passed to a
288  StereochemistryPairFilter to exclude 1-3 interactions from the
289  nonbonded list, or to an AngleSingletonScore to score each angle.
290 
291  \return a list of the newly-created Angle particles.
292 
293  \see CHARMMTopology::add_bonds().
294  */
295  Particles create_angles(Particles bonds) const;
296 
297  //! Auto-generate Dihedral particles from the passed list of Bond particles.
298  /** The dihedrals consist of all unique triples of bonds which form
299  dihedrals. If no parameters are found for a dihedral, it is simply
300  created without those parameters; if multiple sets of parameters are
301  found, multiple copies of the dihedral are created, each with one set
302  of parameters.
303 
304  The list of newly-created Dihedral particles can be passed to a
305  StereochemistryPairFilter to exclude 1-4 interactions from the
306  nonbonded list, or to a DihedralSingletonScore to score each dihedral.
307 
308  If dihedrals are explicitly listed in the CHARMM topology file, they
309  can be created if desired by calling CHARMMTopology::add_dihedrals()
310  rather than this function.
311 
312  \return a list of the newly-created Dihedral particles.
313 
314  \see CHARMMTopology::add_bonds().
315  */
316  Particles create_dihedrals(Particles bonds) const;
317 
319 
320  private:
321  virtual String get_force_field_atom_type(Atom atom) const override;
322 
323  void read_parameter_file(TextInput input_file);
324  // read topology file
325  void read_topology_file(TextInput input_file,
326  bool translate_names_to_pdb);
327 
328  void add_angle(Particle *p1, Particle *p2,
329  Particle *p3, Particles &ps) const;
330  void add_dihedral(Particle *p1, Particle *p2,
331  Particle *p3, Particle *p4,
332  Particles &ps) const;
333 
334  ResidueType parse_residue_line(const String &line,
335  bool translate_names_to_pdb);
336  void parse_atom_line(const String &line, ResidueType curr_res_type,
337  CHARMMResidueTopologyBase *residue,
338  bool translate_names_to_pdb);
339  void parse_bond_line(const String &line, ResidueType curr_res_type,
340  CHARMMResidueTopologyBase *residue,
341  bool translate_names_to_pdb);
342 
343  void parse_nonbonded_parameters_line(String line);
344  void parse_bonds_parameters_line(String line);
345  void parse_angles_parameters_line(String line);
346  void parse_dihedrals_parameters_line(String line, DihedralParameters &param);
347  WarningContext warn_context_;
348 };
349 
351 
352 /** The default CHARMM parameters support normal amino acid
353  and nucleic acid residues and the atoms found in them.
354  To use CHARMM with heterogens or non-standard residues,
355  a different CHARMM parameters file must be used.
356 
357  No hydrogen parameters are read.
358 
359  \see get_all_atom_CHARMM_parameters();
360 */
362 
363 /** The default CHARMM parameters support normal amino acid
364  and nucleic acid residues and the atoms found in them.
365  To use CHARMM with heterogens or non-standard residues,
366  a different CHARMM parameters file must be used.
367 
368  \see get_heavy_atom_CHARMM_parameters()
369 */
371 
372 IMPATOM_END_NAMESPACE
373 
374 #endif /* IMPATOM_CHARMM_PARAMETERS_H */
The ideal topology of a single residue.
CHARMMParameters * get_heavy_atom_CHARMM_parameters()
Storage and access to force field.
#define IMP_SHOWABLE_INLINE(Name, how_to_show)
Declare the methods needed by an object that can be printed.
The parameters for a CHARMM bond or angle.
#define IMP_OBJECT_METHODS(Name)
Define the basic things needed by any Object.
Definition: object_macros.h:25
The parameters for a CHARMM dihedral or improper.
force field base class
Handling of file input/output.
const CHARMMBondParameters & get_angle_parameters(std::string type1, std::string type2, std::string type3) const
Get parameters for the angle between the three given CHARMM atom types.
A more IMP-like version of the std::vector.
Definition: Vector.h:50
const CHARMMBondParameters & get_bond_parameters(std::string type1, std::string type2) const
Get bond parameters for the bond between the two given CHARMM atom types.
Warnings with the same key within the context are only output once.
A smart pointer to a reference counted object.
Definition: Pointer.h:87
CHARMM force field parameters.
Classes for handling CHARMM-style topology.
const CHARMMDihedralParameters & get_improper_parameters(std::string type1, std::string type2, std::string type3, std::string type4) const
Get parameters for the improper between the four given CHARMM atom types.
#define IMP_VALUES(Name, PluralName)
Define the type for storing sets of values.
Definition: value_macros.h:23
The standard decorator for manipulating molecular structures.
Base class for all CHARMM residue-based topology.
A decorator for a particle representing an atom.
Definition: atom/Atom.h:238
The type for a residue.
An exception for a request for an invalid member of a container.
Definition: exception.h:156
A CHARMM patch residue.
#define IMP_OBJECTS(Name, PluralName)
Define the types for storing lists of object pointers.
Definition: object_macros.h:44
#define IMP_THROW(message, exception_name)
Throw an exception with a message.
Definition: check_macros.h:50
Macros for maintaining molecular hierarchies.
Class to handle individual particles of a Model object.
Definition: Particle.h:45
The topology of a complete CHARMM model.
CHARMMParameters * get_all_atom_CHARMM_parameters()
An exception for an invalid value being passed to IMP.
Definition: exception.h:136
std::string String
Basic string value.
Definition: types.h:43
void set_was_used(bool tf) const