IMP  2.0.1
The Integrative Modeling Platform
TypedPairScore.h
Go to the documentation of this file.
1 /**
2  * \file IMP/core/TypedPairScore.h
3  * \brief Delegate to another PairScore depending on particle types.
4  *
5  * Copyright 2007-2013 IMP Inventors. All rights reserved.
6  */
7 
8 #ifndef IMPCORE_TYPED_PAIR_SCORE_H
9 #define IMPCORE_TYPED_PAIR_SCORE_H
10 
11 #include <IMP/core/core_config.h>
12 #include <IMP/PairScore.h>
13 #include <IMP/pair_macros.h>
14 
15 IMPCORE_BEGIN_NAMESPACE
16 
17 //! Delegate to another PairScore depending on particle types.
18 /** Each particle is given an integer type, which is used at evaluate time
19  to select a PairScore to use for a given pair of particles. (The ordering
20  of the particles does not matter.) After creating the object, call
21  set_pair_score() to tell it the PairScore objects you want to use for each
22  pair of particle types.
23 
24  \note For efficiency, you should probably use the
25  container::PredicatePairRestraint instead.
26  */
27 class IMPCOREEXPORT TypedPairScore : public PairScore
28 {
29  // The key used for the particle types.
30  IntKey typekey_;
31  typedef std::map<std::pair<Int,Int>,
33  // Mapping from particle types to PairScores.
34  ScoreMap score_map_;
35  // Whether to throw an exception for invalid particle types.
36  bool allow_invalid_types_;
37 
38  PairScore *get_pair_score(const ParticlePair &pp) const;
39 public:
40  //! Constructor.
41  /** \param[in] typekey The IntKey used to denote the type of each particle.
42  \param[in] allow_invalid_types Desired behavior for particle pairs that
43  have types not covered by set_pair_score(). If true, the score
44  returned for these pairs is zero. If false, evaluate() raises
45  a ValueException.
46  */
47  TypedPairScore(IntKey typekey, bool allow_invalid_types=true);
48 
49  //! Set the particle's type.
50  /** At evaluate time, if a given particle does not have the typekey
51  attribute, this method is called. Here it does nothing, but it could
52  be overridden in a subclass to automatically set the type of a particle,
53  e.g. from other particle attributes such as an atom or residue name.
54  */
55  virtual void set_particle_type(Particle *) const {}
56 
57  //! Set the PairScore to delegate to for a given pair of particle types.
58  /** \param[in] ps PairScore to use at evaluate time.
59  \param[in] atype First particle type.
60  \param[in] btype Second particle type.
61  */
62  void set_pair_score(PairScore *ps, Int atype, Int btype) {
63  score_map_[std::pair<Int,Int>(std::min(atype, btype),
64  std::max(atype, btype))]
66  }
67 
69 };
70 
71 IMPCORE_END_NAMESPACE
72 
73 #endif /* IMPCORE_TYPED_PAIR_SCORE_H */