IMP  2.2.0
The Integrative Modeling Platform
MSConnectivityRestraint.h
Go to the documentation of this file.
1 /**
2  * \file IMP/core/MSConnectivityRestraint.h
3  * \brief Mass Spec Connectivity restraint.
4  *
5  * Restrict max distance between at least one pair of particles of any
6  * two distinct types. It also handles multiple copies of the same particles.
7  *
8  * Copyright 2007-2014 IMP Inventors. All rights reserved.
9  *
10  */
11 
12 #ifndef IMPCORE_MS_CONNECTIVITY_RESTRAINT_H
13 #define IMPCORE_MS_CONNECTIVITY_RESTRAINT_H
14 
15 #include <vector>
16 #include <string>
17 #include <IMP/core/core_config.h>
18 #include "DistanceRestraint.h"
19 
20 #include <IMP/SingletonContainer.h>
21 #include <IMP/kernel/Restraint.h>
22 #include <IMP/PairScore.h>
23 
24 IMPCORE_BEGIN_NAMESPACE
25 
26 // Hopefully this would "fix" the problem with friend access
27 // in older versions of gcc
28 #ifdef __GNUC__
29 #define IMP_GCC_VERSION \
30  (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__)
31 #if IMP_GCC_VERSION >= 40100
32 #define IMPCORE_FRIEND_IS_OK
33 #endif
34 #else
35 // assume that other compilers are fine
36 #define IMPCORE_FRIEND_IS_OK
37 #endif
38 
39 //! Ensure that a set of particles remains connected with one another.
40 /** The restraint implements ambiguous connectivity. That is, it takes
41  several particles including multiple copies and ensures that they remain
42  connected, but allows how they are connected to change. If you wish
43  to restrain the connectivity of sets of
44  particles (i.e. each protein is represented using a set of balls)
45  use an appropriate PairScore which calls a Refiner (such
46  as ClosePairsPairScore).
47 
48  \include ms_connectivity_restraint.py
49 
50  More precisely, the restraint scores by computing the MST on the complete
51  graph connecting all the particles. The edge weights are given by
52  the value of the PairScore for the two endpoints of the edge.
53  */
54 class IMPCOREEXPORT MSConnectivityRestraint : public kernel::Restraint {
57  double eps_;
58 
59  public:
60  //! Use the given PairScore
61  /** If sc is nullptr, a ListSingletonContainer is created internally.
62  eps is set to 0.1 by default.
63  */
64  MSConnectivityRestraint(Model *m, PairScore *ps, double eps = 0.1);
65  /** @name kernel::Particles to be connected
66 
67  The following methods are used to manipulate the list of particles
68  that are to be connected. Each particle should have all the
69  attributes expected by the PairScore used.
70 
71  Ideally, one should pass a singleton container instead. These
72  can only be used if none is passed.
73  */
74  /*@{*/
75  unsigned int add_type(const kernel::ParticlesTemp &ps);
76  unsigned int add_composite(const Ints &components);
77  unsigned int add_composite(const Ints &components, unsigned int parent);
78  // void add_particle(kernel::Particle *p);
79  // void add_particles(const kernel::Particles &ps);
80  // void set_particles(const kernel::Particles &ps);
81  /*@}*/
82 
83  //! Return the set of pairs which are connected by the restraint
84  /** This set of pairs reflects the current configuration at the time of
85  the get_connected_pairs() call, not the set at the time of the last
86  evaluate() call.
87  */
88  kernel::ParticlePairsTemp get_connected_pairs() const;
89 
90  //! Return the pair score used for scoring
91  PairScore *get_pair_score() const { return ps_; }
92 
94 
95  double unprotected_evaluate(IMP::kernel::DerivativeAccumulator *accum) const;
96 
98 
100 
101 #ifdef IMPCORE_FRIEND_IS_OK
102  private:
103 #endif
104 
105 #ifndef IMP_DOXYGEN
106  class ParticleMatrix {
107  public:
108  class ParticleData {
109  public:
110  ParticleData(kernel::Particle *p, unsigned int id)
111  : particle_(p), id_(id) {}
112 
113  kernel::Particle *get_particle() const { return particle_; }
114 
115  unsigned int get_id() const { return id_; }
116 
117  private:
118  kernel::Particle *particle_;
119  unsigned int id_;
120  };
121 
122  ParticleMatrix(unsigned int number_of_classes)
123  : protein_by_class_(number_of_classes),
124  min_distance_(std::numeric_limits<double>::max()),
125  max_distance_(0),
126  current_id_(0) {}
127 
128  ParticleMatrix()
129  : min_distance_(std::numeric_limits<double>::max()),
130  max_distance_(0),
131  current_id_(0) {}
132 
133  void resize(unsigned int number_of_classes) {
134  protein_by_class_.resize(number_of_classes);
135  }
136 
137  unsigned int add_particle(kernel::Particle *p, unsigned int id);
138  unsigned int add_type(const kernel::ParticlesTemp &ps);
139  void create_distance_matrix(const PairScore *ps);
140  void clear_particles() {
141  particles_.clear();
142  for (unsigned int i = 0; i < protein_by_class_.size(); ++i)
143  protein_by_class_[i].clear();
144  }
145  unsigned int size() const { return particles_.size(); }
146  unsigned int get_number_of_classes() const {
147  return protein_by_class_.size();
148  }
149  double get_distance(unsigned int p1, unsigned int p2) const {
150  return dist_matrix_[p1 * size() + p2];
151  }
152  Ints const &get_ordered_neighbors(unsigned int p) const {
153  return order_[p];
154  }
155  ParticleData const &get_particle(unsigned int p) const {
156  return particles_[p];
157  }
158  Ints const &get_all_proteins_in_class(unsigned int id) const {
159  return protein_by_class_[id];
160  }
161  double max_distance() const { return max_distance_; }
162  double min_distance() const { return min_distance_; }
163 
164  private:
165  class DistCompare {
166  public:
167  DistCompare(unsigned int source, ParticleMatrix const &parent)
168  : parent_(parent), source_(source) {}
169 
170  bool operator()(unsigned int p1, unsigned int p2) const {
171  return parent_.get_distance(source_, p1) <
172  parent_.get_distance(source_, p2);
173  }
174 
175  private:
176  ParticleMatrix const &parent_;
177  unsigned int source_;
178  };
179 
180  base::Vector<ParticleData> particles_;
181  Floats dist_matrix_;
182  base::Vector<Ints> order_;
183  base::Vector<Ints> protein_by_class_;
184  double min_distance_;
185  double max_distance_;
186  unsigned int current_id_;
187  };
188 
189  class ExperimentalTree {
190  public:
191  ExperimentalTree() : root_(-1), finalized_(false) {}
192 
193  void connect(unsigned int parent, unsigned int child);
194  void finalize();
195  unsigned int add_composite(const Ints &components);
196  unsigned int add_composite(const Ints &components, unsigned int parent);
197 
198  class Node {
199  public:
200  Node() : visited_(false) {}
201  unsigned int get_number_of_parents() const { return parents_.size(); }
202  bool is_root() const { return get_number_of_parents() == 0; }
203  unsigned int get_number_of_children() const { return children_.size(); }
204  bool is_leaf() const { return get_number_of_children() == 0; }
205  unsigned int get_parent(unsigned int idx) const { return parents_[idx]; }
206  unsigned int get_child(unsigned int idx) const { return children_[idx]; }
207  typedef base::Vector<std::pair<unsigned int, int> > Label;
208  const Label &get_label() const { return label_; }
209 
210  base::Vector<unsigned int> parents_;
211  base::Vector<unsigned int> children_;
212  Label label_;
213  bool visited_;
214  };
215 
216  bool find_cycle(unsigned int node_index);
217  bool is_consistent(unsigned int node_index) const;
218 
219  const Node *get_node(unsigned int index) const { return &nodes_[index]; }
220  unsigned int get_number_of_nodes() const { return nodes_.size(); }
221  unsigned int get_root() const { return root_; }
222  void desc_to_label(const Ints &components, Node::Label &label);
223 
224  base::Vector<Node> nodes_;
225  unsigned int root_;
226  bool finalized_;
227  };
228 #endif // IMP_DOXYGEN
229 
230  ParticleMatrix particle_matrix_;
231  mutable ExperimentalTree tree_;
232 
233  friend class MSConnectivityScore;
234 };
235 
236 IMPCORE_END_NAMESPACE
237 
238 #endif /* IMPCORE_MS_CONNECTIVITY_RESTRAINT_H */
Class for adding derivatives from restraints to the model.
Distance restraint between two particles.
IMP::kernel::PairScore PairScore
Import IMP/kernel/SingletonContainer.h in the namespace.
Abstract score function.
Hierarchy get_root(Hierarchy h)
Return the root of the hierarchy.
double get_distance(const Plane3D &pln, const Vector3D &p)
Return the distance between a plane and a point in 3D.
Definition: Plane3D.h:67
Abstract base class for all restraints.
PairScore * get_pair_score() const
Return the pair score used for scoring.
Import IMP/kernel/PairScore.h in the namespace.
A restraint is a term in an IMP ScoringFunction.
#define IMP_OBJECT_METHODS(Name)
Define the basic things needed by any Object.
Class to handle individual model particles.
virtual Restraints do_create_current_decomposition() const
IMP::base::Vector< Float > Floats
Standard way to pass a bunch of Float values.
Definition: base/types.h:47
IMP::kernel::Particle Particle
virtual ModelObjectsTemp do_get_inputs() const =0
void add_particle(RMF::FileHandle fh, kernel::Particle *hs)
Ensure that a set of particles remains connected with one another.
Class for storing model, its restraints, constraints, and particles.
Definition: kernel/Model.h:72
IMP::base::Vector< Int > Ints
Standard way to pass a bunch of Int values.
Definition: base/types.h:49