IMP logo
bond_decorators.h
Go to the documentation of this file.
1 /**
2  * \file atom/bond_decorators.h \brief Contains decorators for a bond
3  *
4  * Copyright 2007-2012 IMP Inventors. All rights reserved.
5  *
6  */
7 
8 #ifndef IMPATOM_BOND_DECORATORS_H
9 #define IMPATOM_BOND_DECORATORS_H
10 
11 #include "atom_config.h"
12 #include <IMP/core/internal/graph_base.h>
13 #include "internal/bond_helpers.h"
16 #include <IMP/Particle.h>
17 #include <IMP/Model.h>
18 #include <IMP/Decorator.h>
19 #include <IMP/core/XYZ.h>
20 
21 #include <IMP/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 {
42  friend class Bonded;
43 public:
45 
46  //! Return true if the particle is a bond.
47  static bool particle_is_instance(Particle *p) {
48  return IMP::core::internal::graph_is_edge(p,
49  internal::get_bond_data().graph_);
50  }
51 
52  //! The types a bond can have right now
53  enum Type {UNKNOWN=-1,
54  NONBIOLOGICAL, SINGLE=1, DOUBLE=2, TRIPLE=3, HYDROGEN,
55  SALT, PEPTIDE,
56  AMIDE, AROMATIC
57  };
58 
59  //! Get the atom i of the bond
60  /** \param[in] i 0 or 1
61  \return Bonded for the atom in question
62  */
63  Bonded get_bonded(unsigned int i) const ;
64 
66  internal::get_bond_data().type_, Int, Int,
67  UNKNOWN);
68 
70  internal::get_bond_data().order_,
71  Int, Int, 1);
72 
74  internal::get_bond_data().length_, Float,
75  Float, -1);
76  IMP_DECORATOR_GET_SET_OPT(stiffness,
77  internal::get_bond_data().stiffness_,
78  Float,
79  Float, -1);
80 
81  static FloatKey get_length_key() {
82  return internal::get_bond_data().length_;
83  }
84 };
85 
86 
87 
88 //! A decorator for a particle which has bonds.
89 /** \ingroup bond
90  \see Bond
91  */
92 class IMPATOMEXPORT Bonded: public Decorator
93 {
94  struct GetBond {
95  typedef Bond result_type;
96  Particle* d_;
97  GetBond():d_(nullptr){}
98  GetBond(Particle* d): d_(d){}
99  Bond operator()(unsigned int i) const;
100  bool operator==(const GetBond &o) const {
101  return d_== o.d_;
102  }
103  };
104  struct GetBonded {
105  typedef Bonded result_type;
106  Particle* d_;
107  GetBonded():d_(nullptr){}
108  GetBonded(Particle* d): d_(d){}
109  Bonded operator()(unsigned int i) const;
110  bool operator==(const GetBonded &o) const {
111  return d_== o.d_;
112  }
113  };
114 public:
115  IMP_DECORATOR(Bonded, Decorator);
116  //! return true if it is a bonded particle
117  static bool particle_is_instance(Particle *p) {
118  return IMP::core::internal::graph_is_node(p,
119  internal::get_bond_data().graph_);
120  }
121 
122  static Bonded setup_particle(Particle *p) {
123  graph_initialize_node(p, internal::get_bond_data().graph_);
124  return Bonded(p);
125  }
126 
127  /** */
128  unsigned int get_number_of_bonds() const {
129  return graph_get_number_of_edges(get_particle(),
130  internal::get_bond_data().graph_);
131  }
132 
133 
134  //! Get a Bond of the ith bond
135  /** \return decorator of the ith child, or throw an exception if there
136  is no such bond
137  */
138  Bond get_bond(unsigned int i) const {
139  Particle *p= graph_get_edge(get_particle(), i,
140  internal::get_bond_data().graph_);
141  return Bond(p);
142  }
143 
144  //! Get a Bonded of the ith bonded particle
145  /** \return decorator of the ith child, or throw an exception if there
146  is no such bond
147 
148  \note I don't entirely like having this here as it duplicates
149  functionality available otherwise, however it is such a fundamental
150  operation and kind of a pain to write. It also means that we
151  could later pull the edge endpoints into the vertex if
152  desired.
153  */
154  Bonded get_bonded(unsigned int i) const {
155  Particle *p= graph_get_edge(get_particle(), i,
156  internal::get_bond_data().graph_);
157  Bond bd(p);
158  if (bd.get_bonded(0) == *this) return bd.get_bonded(1);
159  else return bd.get_bonded(0);
160  }
161 
162  /** @name Iterate through the bonds
163  @{
164  */
165 #ifdef IMP_DOXYGEN
166  class BondIterator;
167 #else
168  typedef IMP::internal::IndexingIterator<GetBond> BondIterator;
169 #endif
170 #ifndef SWIG
171  BondIterator bonds_begin() const {
172  return BondIterator(GetBond(get_particle()), 0);
173  }
174  BondIterator bonds_end() const {
175  return BondIterator(GetBond(get_particle()), get_number_of_bonds());
176  }
177 #endif
178  /** @} */
179  /** @name Iterate through the bondeds
180  @{
181  */
182 #ifdef IMP_DOXYGEN
183  class BondedIterator;
184 #else
185  typedef IMP::internal::IndexingIterator<GetBonded> BondedIterator;
186 #endif
187 #ifndef SWIG
188  BondedIterator bondeds_begin() const {
189  return BondedIterator(GetBonded(get_particle()), 0);
190  }
191  BondedIterator bondeds_end() const {
192  return BondedIterator(GetBonded(get_particle()), get_number_of_bonds());
193  }
194 #endif
195  /** @} */
196 };
197 
198 IMP_DECORATORS(Bonded,Bondeds, ParticlesTemp);
199 IMP_DECORATORS(Bond,Bonds, ParticlesTemp);
200 
201 
202 inline Bonded Bond::get_bonded(unsigned int i) const
203 {
204  Particle *p= graph_get_node(get_particle(), i,
205  internal::get_bond_data().graph_);
206  return Bonded(p);
207 }
208 
209 #ifndef IMP_DOXYGEN
210 inline Bond Bonded::GetBond::operator()(unsigned int i)
211  const {
212  return Bonded(d_).get_bond(i);
213 }
214 inline Bonded Bonded::GetBonded::operator()(unsigned int i)
215  const {
216  return Bonded(d_).get_bonded(i);
217 }
218 #endif
219 
221 //! Connect the two wrapped particles by a bond.
222 /** \param[in] a The first Particle as a Bonded
223  \param[in] b The second Particle as a Bonded
224  \param[in] t The type to use for the bond
225  \return Bond of the bond Particle.
226 
227  \ingroup bond
228  \relatesalso Bond
229  \relatesalso Bonded
230  */
231 IMPATOMEXPORT
232 Bond create_bond(Bonded a, Bonded b, Int t);
233 
234 
235 //! Connect the two wrapped particles by a custom bond.
236 /** \param[in] a The first Particle as a Bonded
237  \param[in] b The second Particle as a Bonded
238  \param[in] length The length of the bond.
239  \param[in] stiffness The stiffness of the bond.
240  \return Bond of the bond Particle.
241 
242  \ingroup bond
243  \relatesalso Bond
244  \relatesalso Bonded
245  */
246 IMPATOMEXPORT
248  Float length, Float stiffness=-1) {
249  IMP_INTERNAL_CHECK(length>=0, "Length must be positive");
250  Bond bd=create_bond(a,b, Bond::NONBIOLOGICAL);
251  bd.set_length(length);
252  bd.get_particle()->set_name(std::string("bond ")+
253  a.get_particle()->get_name()
254  + " and " + b.get_particle()->get_name());
255  if (stiffness >=0) bd.set_stiffness(stiffness);
256  return bd;
257 }
258 
259 
260 //! Connect the two wrapped particles by a custom bond.
261 /** Create a bond by copying the information from the othr bond
262 
263  \ingroup bond
264  \relatesalso Bond
265  \relatesalso Bonded
266  */
267 IMPATOMEXPORT
268 inline Bond create_bond(Bonded a, Bonded b,
269  Bond o) {
270  Bond bd=create_bond(a,b, o.get_type());
271  if (o.get_length() > 0) bd.set_length(o.get_length());
272  bd.get_particle()->set_name(std::string("bond ")+
273  a.get_particle()->get_name()
274  + " and " + b.get_particle()->get_name());
275  if (o.get_stiffness() >=0) bd.set_stiffness(o.get_stiffness());
276  return bd;
277 }
278 
279 //! Destroy the bond connecting to particles.
280 /** \param[in] b The bond.
281  \ingroup bond
282  \relatesalso Bond
283  \relatesalso Bonded
284  */
285 IMPATOMEXPORT
286 void destroy_bond(Bond b);
287 
288 //! Get the bond between two particles.
289 /** Bond() is returned if the particles are not bonded.
290  \ingroup bond
291  \relatesalso Bond
292  \relatesalso Bonded
293  */
294 IMPATOMEXPORT
295 Bond get_bond(Bonded a, Bonded b);
296 
297 
298 
299 
300 /** \class BondGeometry
301  \brief Display an Bond particle as a segment.
302 
303  \class BondsGeometry
304  \brief Display an IMP::SingletonContainer of Bond particles
305  as segments.
306 */
307 IMP_PARTICLE_GEOMETRY(Bond, Bond,{
308  atom::Bonded ep0= d.get_bonded(0);
309  core::XYZ epi0(ep0.get_particle());
310  atom::Bonded ep1= d.get_bonded(1);
311  core::XYZ epi1(ep1.get_particle());
312  algebra::Segment3D s(epi0.get_coordinates(),
313  epi1.get_coordinates());
315  ret.push_back(g);
316  });
317 
318 IMPATOM_END_NAMESPACE
319 
320 #endif /* IMPATOM_BOND_DECORATORS_H */

Generated on Tue May 22 2012 23:33:12 for IMP by doxygen 1.8.1