IMP  2.1.0
The Integrative Modeling Platform
bond_decorators.h
Go to the documentation of this file.
1 /**
2  * \file IMP/atom/bond_decorators.h \brief Contains decorators for a bond
3  *
4  * Copyright 2007-2013 IMP Inventors. All rights reserved.
5  *
6  */
7 
8 #ifndef IMPATOM_BOND_DECORATORS_H
9 #define IMPATOM_BOND_DECORATORS_H
10 
11 #include <IMP/atom/atom_config.h>
12 #include <IMP/core/internal/graph_base.h>
13 #include "internal/bond_helpers.h"
16 #include <IMP/kernel/Particle.h>
17 #include <IMP/kernel/Model.h>
18 #include <IMP/Decorator.h>
19 #include <IMP/core/XYZ.h>
20 
21 #include <IMP/kernel/internal/IndexingIterator.h>
22 IMPATOM_BEGIN_NAMESPACE
23 
24 class Bond;
25 class Bonded;
26 
27 /** \defgroup bond Creating and restraining bonds
28  A set of classes and functions for manipulating bonds.
29  */
30 
31 //! A decorator for wrapping a particle representing a molecular bond
32 /**
33  As with Atom, the types of bonds will eventually be run-time
34  expandible.
35 
36  \ingroup bond
37  \see Bonded
38  \see IMP::atom::get_internal_bonds()
39  */
40 class IMPATOMEXPORT Bond : public Decorator {
41  friend class Bonded;
42 
43  public:
45 
47  return IMP::core::internal::graph_is_edge(m->get_particle(pi),
48  internal::get_bond_data().graph_);
49  }
50 
51  //! The types a bond can have right now
52  enum Type {
53  UNKNOWN = -1,
54  NONBIOLOGICAL,
55  SINGLE = 1,
56  DOUBLE = 2,
57  TRIPLE = 3,
58  HYDROGEN,
59  SALT,
60  PEPTIDE,
61  AMIDE,
62  AROMATIC
63  };
64 
65  //! Get the atom i of the bond
66  /** \param[in] i 0 or 1
67  \return Bonded for the atom in question
68  */
69  Bonded get_bonded(unsigned int i) const;
70 
71  IMP_DECORATOR_GET_SET_OPT(type, internal::get_bond_data().type_, Int, Int,
72  UNKNOWN);
73 
74  IMP_DECORATOR_GET_SET_OPT(order, internal::get_bond_data().order_, Int, Int,
75  1);
76 
77  IMP_DECORATOR_GET_SET_OPT(length, internal::get_bond_data().length_, Float,
78  Float, -1);
79  IMP_DECORATOR_GET_SET_OPT(stiffness, internal::get_bond_data().stiffness_,
80  Float, Float, -1);
81 
82  static FloatKey get_length_key() { return internal::get_bond_data().length_; }
83 };
84 
85 //! A decorator for a particle which has bonds.
86 /** \ingroup bond
87  \see Bond
88  */
89 class IMPATOMEXPORT Bonded : public Decorator {
90  struct GetBond {
91  typedef Bond result_type;
92  kernel::Particle *d_;
93  GetBond() : d_(nullptr) {}
94  GetBond(kernel::Particle *d) : d_(d) {}
95  Bond operator()(unsigned int i) const;
96  bool operator==(const GetBond &o) const { return d_ == o.d_; }
97  };
98  struct GetBonded {
99  typedef Bonded result_type;
100  kernel::Particle *d_;
101  GetBonded() : d_(nullptr) {}
102  GetBonded(kernel::Particle *d) : d_(d) {}
103  Bonded operator()(unsigned int i) const;
104  bool operator==(const GetBonded &o) const { return d_ == o.d_; }
105  };
106  static void do_setup_particle(kernel::Model *m, kernel::ParticleIndex pi) {
107  graph_initialize_node(m->get_particle(pi),
108  internal::get_bond_data().graph_);
109  }
110 
111  public:
114 
115  static bool get_is_setup(kernel::Model *m, kernel::ParticleIndex pi) {
116  return IMP::core::internal::graph_is_node(m->get_particle(pi),
117  internal::get_bond_data().graph_);
118  }
119 
120  /** */
121  unsigned int get_number_of_bonds() const {
122  return graph_get_number_of_edges(get_particle(),
123  internal::get_bond_data().graph_);
124  }
125  /** \deprecated_at{2.1} Use get_bond_indexes() instead. */
126  IMPATOM_DEPRECATED_FUNCTION_DECL(2.1)
127  kernel::ParticleIndexes get_bonds() const {
128  IMPATOM_DEPRECATED_FUNCTION_DEF(2.1, "Use get_bond_indexes().");
129  return get_bond_indexes();
130  }
131 
132  kernel::ParticleIndexes get_bond_indexes() const {
133  return graph_get_edges(get_particle(), internal::get_bond_data().graph_);
134  }
135 
136  //! Get a Bond of the ith bond
137  /** \return decorator of the ith child, or throw an exception if there
138  is no such bond
139  */
140  Bond get_bond(unsigned int i) const {
141  kernel::Particle *p =
142  graph_get_edge(get_particle(), i, internal::get_bond_data().graph_);
143  return Bond(p);
144  }
145 
146  //! Get a Bonded of the ith bonded particle
147  /** \return decorator of the ith child, or throw an exception if there
148  is no such bond
149 
150  \note I don't entirely like having this here as it duplicates
151  functionality available otherwise, however it is such a fundamental
152  operation and kind of a pain to write. It also means that we
153  could later pull the edge endpoints into the vertex if
154  desired.
155  */
156  Bonded get_bonded(unsigned int i) const {
157  kernel::Particle *p =
158  graph_get_edge(get_particle(), i, internal::get_bond_data().graph_);
159  Bond bd(p);
160  if (bd.get_bonded(0) == *this)
161  return bd.get_bonded(1);
162  else
163  return bd.get_bonded(0);
164  }
165 
166 /** @name Iterate through the bonds
167  @{
168 */
169 #ifdef IMP_DOXYGEN
170  class BondIterator;
171 #else
172  typedef IMP::internal::IndexingIterator<GetBond> BondIterator;
173 #endif
174 #ifndef SWIG
175  BondIterator bonds_begin() const {
176  return BondIterator(GetBond(get_particle()), 0);
177  }
178  BondIterator bonds_end() const {
179  return BondIterator(GetBond(get_particle()), get_number_of_bonds());
180  }
181 #endif
182 /** @} */
183 /** @name Iterate through the bondeds
184  @{
185 */
186 #ifdef IMP_DOXYGEN
187  class BondedIterator;
188 #else
189  typedef IMP::internal::IndexingIterator<GetBonded> BondedIterator;
190 #endif
191 #ifndef SWIG
192  BondedIterator bondeds_begin() const {
193  return BondedIterator(GetBonded(get_particle()), 0);
194  }
195  BondedIterator bondeds_end() const {
196  return BondedIterator(GetBonded(get_particle()), get_number_of_bonds());
197  }
198 #endif
199  /** @} */
200 };
201 
202 IMP_DECORATORS(Bonded, Bondeds, kernel::ParticlesTemp);
203 IMP_DECORATORS(Bond, Bonds, kernel::ParticlesTemp);
204 
205 inline Bonded Bond::get_bonded(unsigned int i) const {
206  kernel::Particle *p =
207  graph_get_node(get_particle(), i, internal::get_bond_data().graph_);
208  return Bonded(p);
209 }
210 
211 #ifndef IMP_DOXYGEN
212 inline Bond Bonded::GetBond::operator()(unsigned int i) const {
213  return Bonded(d_).get_bond(i);
214 }
215 inline Bonded Bonded::GetBonded::operator()(unsigned int i) const {
216  return Bonded(d_).get_bonded(i);
217 }
218 #endif
219 
220 //! Connect the two wrapped particles by a bond.
221 /** \param[in] a The first kernel::Particle as a Bonded
222  \param[in] b The second kernel::Particle as a Bonded
223  \param[in] t The type to use for the bond
224  \return Bond of the bond kernel::Particle.
225 
226  \ingroup bond
227  See Bond
228  See Bonded
229  */
230 IMPATOMEXPORT Bond create_bond(Bonded a, Bonded b, Int t);
231 
232 //! Connect the two wrapped particles by a custom bond.
233 /** \param[in] a The first kernel::Particle as a Bonded
234  \param[in] b The second kernel::Particle as a Bonded
235  \param[in] length The length of the bond.
236  \param[in] stiffness The stiffness of the bond.
237  \return Bond of the bond kernel::Particle.
238 
239  \ingroup bond
240  See Bond
241  See Bonded
242  */
243 IMPATOMEXPORT inline Bond create_custom_bond(Bonded a, Bonded b, Float length,
244  Float stiffness = -1) {
245  IMP_INTERNAL_CHECK(length >= 0, "Length must be positive");
246  Bond bd = create_bond(a, b, Bond::NONBIOLOGICAL);
247  bd.set_length(length);
248  bd.get_particle()->set_name(std::string("bond ") +
249  a.get_particle()->get_name() + " and " +
250  b.get_particle()->get_name());
251  if (stiffness >= 0) bd.set_stiffness(stiffness);
252  return bd;
253 }
254 
255 //! Connect the two wrapped particles by a custom bond.
256 /** Create a bond by copying the information from the othr bond
257 
258  \ingroup bond
259  See Bond
260  See Bonded
261  */
262 IMPATOMEXPORT inline Bond create_bond(Bonded a, Bonded b, Bond o) {
263  Bond bd = create_bond(a, b, o.get_type());
264  if (o.get_length() > 0) bd.set_length(o.get_length());
265  bd.get_particle()->set_name(std::string("bond ") +
266  a.get_particle()->get_name() + " and " +
267  b.get_particle()->get_name());
268  if (o.get_stiffness() >= 0) bd.set_stiffness(o.get_stiffness());
269  return bd;
270 }
271 
272 //! Destroy the bond connecting to particles.
273 /** \param[in] b The bond.
274  \ingroup bond
275  See Bond
276  See Bonded
277  */
278 IMPATOMEXPORT void destroy_bond(Bond b);
279 
280 //! Get the bond between two particles.
281 /** Bond() is returned if the particles are not bonded.
282  \ingroup bond
283  See Bond
284  See Bonded
285  */
286 IMPATOMEXPORT Bond get_bond(Bonded a, Bonded b);
287 
288 /** \class BondGeometry
289  \brief Display an Bond particle as a segment.
290 
291  \class BondsGeometry
292  \brief Display an IMP::SingletonContainer of Bond particles
293  as segments.
294 */
295 IMP_PARTICLE_GEOMETRY(Bond, Bond, {
296  atom::Bonded ep0 = d.get_bonded(0);
297  core::XYZ epi0(ep0.get_particle());
298  atom::Bonded ep1 = d.get_bonded(1);
299  core::XYZ epi1(ep1.get_particle());
300  algebra::Segment3D s(epi0.get_coordinates(), epi1.get_coordinates());
302  ret.push_back(g);
303 });
304 
305 IMPATOM_END_NAMESPACE
306 
307 #endif /* IMPATOM_BOND_DECORATORS_H */
Import IMP/kernel/Decorator.h in the namespace.
Particle * get_particle() const
Particle * get_particle(ParticleIndex p) const
The base class for geometry.
A decorator for a particle which has bonds.
#define IMP_DECORATOR_METHODS(Name, Parent)
#define IMP_DECORATOR_GET_SET_OPT(name, AttributeKey, Type, ReturnType, default_value)
Define methods for getting and setting an optional simple field.
Type
The types a bond can have right now.
Bond get_bond(unsigned int i) const
Get a Bond of the ith bond.
void destroy_bond(Bond b)
Destroy the bond connecting to particles.
Simple xyz decorator.
Bond create_custom_bond(Bonded a, Bonded b, Float length, Float stiffness=-1)
Connect the two wrapped particles by a custom bond.
Implement geometry for the basic shapes from IMP.algebra.
A decorator for wrapping a particle representing a molecular bond.
static bool get_is_setup(Particle *p)
Return true if the particle can be cast to the decorator.
Bond create_bond(Bonded a, Bonded b, Int t)
Connect the two wrapped particles by a bond.
#define IMP_DECORATOR_SETUP_0(Name)
#define IMP_INTERNAL_CHECK(expr, message)
An assertion to check for internal errors in IMP. An IMP::ErrorException will be thrown.
Represent an XYZR particle with a sphere.
A decorator for a particle with x,y,z coordinates.
Definition: XYZ.h:32
Bonded get_bonded(unsigned int i) const
Get the atom i of the bond.
Class to handle individual model particles.
Storage of a model, its restraints, constraints and particles.
Classes to handle individual model particles.
double Float
Basic floating-point value (could be float, double...)
Definition: base/types.h:20
Bond get_bond(Bonded a, Bonded b)
Get the bond between two particles.
int Int
Basic integer value.
Definition: base/types.h:35
Bonded get_bonded(unsigned int i) const
Get a Bonded of the ith bonded particle.
#define IMP_DECORATORS(Name, PluralName, Parent)
Define the types for storing sets of decorators.
Class for storing model, its restraints, constraints, and particles.