IMP logo
IMP Reference Guide  2.22.0
The Integrative Modeling Platform
AV.h
Go to the documentation of this file.
1 /**
2  * \file IMP/bff/AV.h
3  * \brief Simple Accessible Volume decorator.
4  *
5  * \authors Thomas-Otavio Peulen
6  * Copyright 2007-2022 IMP Inventors. All rights reserved.
7  *
8  */
9 #ifndef IMPBFF_AV_H
10 #define IMPBFF_AV_H
11 
12 #include <IMP/bff/bff_config.h>
13 
14 #include <IMP/decorator_macros.h>
15 #include <IMP/Decorator.h>
16 #include <IMP/algebra/Vector3D.h>
18 #include <IMP/core/XYZ.h>
19 #include <IMP/log.h>
20 #include <IMP/Value.h>
21 #include <IMP/value_macros.h>
22 
23 
24 #include <IMP/bff/PathMap.h>
25 
26 #include <string>
27 #include <cmath>
28 #include <vector>
29 #include <limits>
30 #include <iostream> // std::cout, std::cout, std::flush
31 
32 #include <IMP/bff/internal/json.h>
33 #include <IMP/bff/internal/InverseSampler.h>
34 // requires C++14
35 // #include <boost/histogram.hpp> // make_histogram, regular, weight, indexed
36 #include <IMP/bff/internal/Histogram.h>
37 
38 IMPBFF_BEGIN_NAMESPACE
39 
40 
41 /// Different types of distances between two accessible volumes
42 typedef enum{
43  DYE_PAIR_DISTANCE_E, /// Mean FRET averaged distance R_E
44  DYE_PAIR_DISTANCE_MEAN, /// Mean distance <R_DA>
45  DYE_PAIR_DISTANCE_MP, /// Distance between AV mean positions
46  DYE_PAIR_EFFICIENCY, /// Mean FRET efficiency
47  DYE_PAIR_DISTANCE_DISTRIBUTION, /// Distance distribution
48  DYE_PAIR_XYZ_DISTANCE /// Distance between XYZ of dye particles
50 
51 
52 
53 /// Container for experimental distance measurement
54 class IMPBFFEXPORT AVPairDistanceMeasurement : public Value {
55 
56 public:
57 
58  double distance = -1;
59  double error_neg = -1;
60  double error_pos = -1;
61  double forster_radius = 52.0;
62  int distance_type = IMP::bff::DYE_PAIR_DISTANCE_MEAN;
63  std::string position_1;
64  std::string position_2;
65 
66  /// Get a JSON string
67  std::string get_json();
68 
69  /**
70  @brief Score a model distance against the experimental distance.
71  @param model The model distance to be scored.
72  @return The score of the model distance.
73  */
74  double score_model(double model);
75 
77  { out << "AVPairDistanceMeasurement"; });
78 };
80 
81 
82 //! A decorator for a particle with accessible volume (AV).
83 /** Using the decorator one can get and set AV
84 parameters and modify derivatives.
85 
86  AV must have IMP.Hierarchy parent with XYZ -> is labeling site
87  AV coordinates = AV mean position
88 
89 \ingroup helper
90 \ingroup decorators
91 \include AV_Decorator.py
92 
93 */
94 class IMPBFFEXPORT AV : public IMP::core::Gaussian {
95 
96 private:
97 
98  Pointer<PathMap> av_map_;
99 
100 protected:
101 
102  IMP::algebra::VectorD<9> get_parameter() const {
104  v[0] = get_linker_length();
105  v[1] = get_radius1();
106  v[2] = get_radius2();
107  v[3] = get_radius3();
108  v[4] = get_linker_width();
109  v[5] = get_allowed_sphere_radius();
110  v[6] = get_contact_volume_thickness();
111  v[7] = get_contact_volume_trapped_fraction();
112  v[8] = get_simulation_grid_resolution();
113  return v;
114  }
115 
116 public:
117 
118  /**
119  @brief Creates a path map header.
120  @return The created path map header.
121  */
122  IMP::bff::PathMapHeader create_path_map_header();
123 
124  /**
125  * @brief Initializes the path map.
126  *
127  * This function initializes the path map used by the AV system. The path map is a
128  * data structure that stores information about the available paths or routes in the system.
129  *
130  * @return void
131  */
132  void init_path_map();
133 
134  /**
135  * @brief Get the FloatKey object for a specific AV feature.
136  *
137  * This function returns a FloatKey object representing a specific AV feature
138  * based on the given index.
139  *
140  * @param i The index of the AV feature.
141  * @return The FloatKey object for the AV feature at the given index.
142  *
143  * @note The valid range for the index is 0 to 8.
144  * @note If the index is out of range, an error message will be generated.
145  */
146  static FloatKey get_av_key(unsigned int i){
147  IMP_USAGE_CHECK(i < 9, "Out of range av feature");
148  static const FloatKey k[] = {
149  FloatKey("linker_length"),
150  FloatKey("radius1"),
151  FloatKey("radius2"),
152  FloatKey("radius3"),
153  FloatKey("linker_width"),
154  FloatKey("allowed_sphere_radius"),
155  FloatKey("contact_volume_thickness"),
156  FloatKey("contact_volume_trapped_fraction"),
157  FloatKey("simulation_grid_resolution")
158  };
159  return k[i];
160  }
161 
162  /**
163  * @brief Sets up the attributes for a particle in a model for AV (Anisotropic Volume) calculations.
164  *
165  * This function sets up the attributes for a particle in a model for AV calculations.
166  * The attributes include linker length, radii, linker width, allowed sphere radius,
167  * contact volume thickness, contact volume trapped fraction, and simulation grid resolution.
168  * It also sets up a particle attribute to store the source particle index.
169  *
170  * @param m The model in which the particle resides.
171  * @param pi The index of the particle to set up.
172  * @param pi_source The index of the source particle.
173  * @param linker_length The length of the linker.
174  * @param radii The radii of the particle in the x, y, and z directions.
175  * @param linker_width The width of the linker.
176  * @param allowed_sphere_radius The radius of the allowed sphere.
177  * @param contact_volume_thickness The thickness of the contact volume.
178  * @param contact_volume_trapped_fraction The fraction of the contact volume that is trapped.
179  * @param simulation_grid_resolution The resolution of the simulation grid.
180  */
182  ParticleIndex pi_source,
183  double linker_length = 20.0,
184  const algebra::Vector3D radii = algebra::Vector3D(3.5, 0, 0),
185  double linker_width = 0.5,
186  double allowed_sphere_radius = 1.5,
187  double contact_volume_thickness = 0.0,
188  double contact_volume_trapped_fraction = -1,
189  double simulation_grid_resolution = 1.5) {
190  if (!IMP::core::Gaussian::get_is_setup(m, pi)) {
192  }
193  m->add_attribute(get_av_key(0), pi, linker_length);
194  m->add_attribute(get_av_key(1), pi, radii[0]);
195  m->add_attribute(get_av_key(2), pi, radii[1]);
196  m->add_attribute(get_av_key(3), pi, radii[2]);
197  m->add_attribute(get_av_key(4), pi, linker_width);
198  m->add_attribute(get_av_key(5), pi, allowed_sphere_radius);
199  m->add_attribute(get_av_key(6), pi, contact_volume_thickness);
200  m->add_attribute(get_av_key(7), pi, contact_volume_trapped_fraction);
201  m->add_attribute(get_av_key(8), pi, simulation_grid_resolution);
202  m->add_attribute(get_particle_key(0), pi, pi_source);
203  }
204 
205  /**
206  * @brief Get the particle key for the specified index.
207  * @param i The index of the particle key.
208  * @return The particle key at the specified index.
209  */
210  static ParticleIndexKey get_particle_key(unsigned int i) {
211  static const ParticleIndexKey k[1] = {
212  ParticleIndexKey("Source particle")
213  };
214  return k[i];
215  }
216 
217  /**
218  * @brief Get the particle index of the AV object.
219  * @return The particle index of the AV object.
220  */
223  }
224 
225  /**
226  * @brief Get the particle index of the AV object at the specified index.
227  * @param i The index of the AV object.
228  * @return The particle index of the AV object at the specified index.
229  */
230  ParticleIndex get_particle_index(unsigned int i) const {
231  return get_model()->get_attribute(get_particle_key(i), get_particle_index());
232  }
233 
234  /**
235  * @brief Get the particle pointer of the AV object.
236  * @return The particle pointer of the AV object.
237  */
239  return Decorator::get_particle();
240  }
241 
242  /**
243  * @brief Get the particle pointer of the AV object at the specified index.
244  * @param i The index of the AV object.
245  * @return The particle pointer of the AV object at the specified index.
246  */
247  Particle* get_particle(unsigned int i) const {
249  }
251 
252  /** Setup the particle with unspecified AV - uses default values. */
254  IMP_DECORATOR_GET_SET(linker_length, get_av_key(0), Float, Float);
255  IMP_DECORATOR_GET_SET(radius1, get_av_key(1), Float, Float);
256  IMP_DECORATOR_GET_SET(radius2, get_av_key(2), Float, Float);
257  IMP_DECORATOR_GET_SET(radius3, get_av_key(3), Float, Float);
258  IMP_DECORATOR_GET_SET(linker_width, get_av_key(4), Float, Float);
259  IMP_DECORATOR_GET_SET(allowed_sphere_radius, get_av_key(5), Float, Float);
260  IMP_DECORATOR_GET_SET(contact_volume_thickness, get_av_key(6), Float, Float);
261  IMP_DECORATOR_GET_SET(contact_volume_trapped_fraction, get_av_key(7), Float, Float);
262  IMP_DECORATOR_GET_SET(simulation_grid_resolution, get_av_key(8), Float, Float);
263 
264 
265  /**
266  * @brief Returns the radii of an object.
267  *
268  * This function returns the radii of an object as a 3D vector. The radii are obtained by calling the functions get_radius1(), get_radius2(), and get_radius3() and storing the values in a Vector3D object.
269  *
270  * @return A Vector3D object representing the radii of the object.
271  */
273  return IMP::algebra::Vector3D({get_radius1(), get_radius3(), get_radius3()});
274  }
275 
276  //! Get whether the coordinates are optimized
277  /** \return true only if all of them are optimized.
278  */
280  return
281  get_particle()->get_is_optimized(get_av_key(0)) &&
282  get_particle()->get_is_optimized(get_av_key(1)) &&
283  get_particle()->get_is_optimized(get_av_key(2)) &&
284  get_particle()->get_is_optimized(get_av_key(3)) &&
285  get_particle()->get_is_optimized(get_av_key(6)) &&
286  get_particle()->get_is_optimized(get_av_key(7));
287  }
288 
289  //! Set whether the coordinates are optimized
290  void set_av_parameters_are_optimized(bool tf) const {
291  get_particle()->set_is_optimized(get_av_key(0), tf);
292  get_particle()->set_is_optimized(get_av_key(1), tf);
293  get_particle()->set_is_optimized(get_av_key(2), tf);
294  get_particle()->set_is_optimized(get_av_key(3), tf);
295  get_particle()->set_is_optimized(get_av_key(6), tf);
296  get_particle()->set_is_optimized(get_av_key(7), tf);
297  }
298 
299  /**
300  * @brief Sets the AV parameter using a JSON object.
301  *
302  * This function takes a JSON object as input and sets the AV parameter accordingly.
303  *
304  * @param j The JSON object containing the AV parameter.
305  */
306  void set_av_parameter(const nlohmann::json &j);
307 
308 
309  //! Get the vector of derivatives accumulated by add_to_derivatives().
310  /** Somewhat suspect based on wanting a Point/Vector differentiation
311  but we don't have points */
313  return get_model()->get_coordinate_derivatives(get_particle_index());
314  }
315 
316  static bool get_is_setup(Model *m, ParticleIndex pi) {
317  return m->get_has_attribute(get_av_key(2), pi);
318  }
319 
320  /**
321  * @brief Get the PathMap associated with the AV object.
322  * @return A pointer to the PathMap object.
323  */
324  IMP::bff::PathMap* get_map() const;
325 
326  /**
327  * @brief Resample the AV object.
328  * @param shift_xyz Flag indicating whether to shift the XYZ coordinates.
329  */
330  void resample(bool shift_xyz=true);
331 
332  /**
333  * @brief Get the mean position of the AV object.
334  * @return The mean position as a Vector3D.
335  */
336  IMP::algebra::Vector3D get_mean_position(bool include_source=true) const;
337 
338  /**
339  * @brief Get the source coordinates of the AV object.
340  * @return The source coordinates as a Vector3D.
341  */
342  IMP::algebra::Vector3D get_source_coordinates() const;
343 
344  /**
345  * @brief Get the source particle of the AV object.
346  * @return A pointer to the source Particle object.
347  */
348  Particle* get_source() const;
349 
350 };
351 
352 IMP_DECORATORS(AV, AVs, ParticlesTemp);
353 
354 /**
355  * @brief Computes the FRET efficiency given the distance and Forster radius.
356  * @tparam T The type of the distance.
357  * @param distance The distance between the two volumes.
358  * @param forster_radius The Forster radius.
359  * @return The FRET efficiency.
360  */
361 template<typename T>
362 T inline fret_efficiency(T distance, double forster_radius){
363  double rda_r0_6 = std::pow(distance / forster_radius, 6.0);
364  return 1. / (1. + rda_r0_6);
365 }
366 
367 /**
368  * @brief Computes the distance between two volumes given the FRET efficiency and Forster radius.
369  * @tparam T The type of the distance.
370  * @param fret_efficiency The FRET efficiency.
371  * @param forster_radius The Forster radius.
372  * @return The distance between the two volumes.
373  */
374 template<typename T>
375 T inline distance_fret(double fret_efficiency, double forster_radius){
376  return forster_radius * std::pow(1. / fret_efficiency - 1.0, 1. / 6.);
377 }
378 
379 /**
380  * @brief Computes the distance to another accessible volume.
381  * @param a The first accessible volume.
382  * @param b The second accessible volume.
383  * @param forster_radius The Forster radius.
384  * @param distance_type The type of distance to compute.
385  * @param n_samples The number of samples to use for distance computation.
386  * @return The distance between the two accessible volumes.
387  */
388 IMPBFFEXPORT double av_distance(
389  const AV& a,
390  const AV& b,
391  double forster_radius = 52.0,
392  int distance_type = DYE_PAIR_DISTANCE_MEAN,
393  int n_samples = 10000
394 );
395 
396 // Draw random points in AV. Returns (x,y,z,d) vector
397 IMPBFFEXPORT std::vector<double> av_random_points(
398  const AV& av1,
399  int n_samples=10000
400 );
401 
402 //! Random sampling over AV/AV distances
403 IMPBFFEXPORT std::vector<double> av_random_distances(
404  const AV& av1,
405  const AV& av2,
406  int n_samples=10000
407 );
408 
409 
410 //! Compute the distance to another accessible volume
411 IMPBFFEXPORT std::vector<double> av_distance_distribution(
412  const AV& av1,
413  const AV& av2,
414  std::vector<double> axis,
415  //double start, double stop, int n_bins, // for boost histogram
416  int n_samples=10000
417 );
418 
419 
420 /**
421  * @brief Find the particle index of a labeling site.
422  *
423  * This function searches for the particle index of a labeling site in a given hierarchy.
424  *
425  * @param[in] hier The hierarchy in which the labeling site is searched.
426  * @param[in] json_str The JSON string containing the FPS.json position (optional).
427  * @param[in] json_data The JSON data containing the FPS.json position (optional).
428  *
429  * @return The particle index of the labeling site.
430  *
431  * @note If both json_str and json_data are provided, json_str will be used.
432  */
434  const IMP::core::Hierarchy& hier,
435  std::string json_str = "",
436  const nlohmann::json &json_data = nlohmann::json()
437 );
438 
439 
440 IMPBFF_END_NAMESPACE
441 
442 #endif /* IMPBFF_AV_H */
Particle * get_particle(ParticleIndex p) const
Get the particle from an index.
Definition: Model.h:489
The base class for decorators.
static void do_setup_particle(Model *m, ParticleIndex pi, ParticleIndex pi_source, double linker_length=20.0, const algebra::Vector3D radii=algebra::Vector3D(3.5, 0, 0), double linker_width=0.5, double allowed_sphere_radius=1.5, double contact_volume_thickness=0.0, double contact_volume_trapped_fraction=-1, double simulation_grid_resolution=1.5)
Sets up the attributes for a particle in a model for AV (Anisotropic Volume) calculations.
Definition: AV.h:181
#define IMP_DECORATOR_GET_SET(name, AttributeKey, Type, ReturnType)
Define methods for getting and setting a particular simple field.
Distance between AV mean positions.
Definition: AV.h:46
A decorator for a particle with accessible volume (AV).
Definition: AV.h:94
ParticleIndex get_particle_index() const
Returns the particle index decorated by this decorator.
Definition: Decorator.h:211
static Gaussian setup_particle(Model *m, ParticleIndex pi)
Definition: core/Gaussian.h:65
#define IMP_SHOWABLE_INLINE(Name, how_to_show)
Declare the methods needed by an object that can be printed.
Key< 0 > FloatKey
The type used to identify float attributes in the Particles.
Definition: base_types.h:32
algebra::Vector3D get_derivatives() const
Get the vector of derivatives accumulated by add_to_derivatives().
Definition: AV.h:312
ParticleIndex get_particle_index(unsigned int i) const
Get the particle index of the AV object at the specified index.
Definition: AV.h:230
#define IMP_DECORATOR_SETUP_1(Name, FirstArgumentType, first_argument_name)
Model * get_model() const
Returns the Model containing the particle.
Definition: Decorator.h:214
Container for experimental distance measurement.
Definition: AV.h:54
Mean FRET efficiency.
Definition: AV.h:47
Mean distance <R_DA>
Definition: AV.h:45
bool get_parameters_are_optimized() const
Get whether the coordinates are optimized.
Definition: AV.h:279
double av_distance(const AV &a, const AV &b, double forster_radius=52.0, int distance_type=DYE_PAIR_DISTANCE_MEAN, int n_samples=10000)
Computes the distance to another accessible volume.
A more IMP-like version of the std::vector.
Definition: Vector.h:50
Class to search path on grids.
Definition: PathMap.h:45
Simple XYZ decorator.
A smart pointer to a reference counted object.
Definition: Pointer.h:87
std::vector< double > av_distance_distribution(const AV &av1, const AV &av2, std::vector< double > axis, int n_samples=10000)
Compute the distance to another accessible volume.
Class for storing model, its restraints, constraints, and particles.
Definition: Model.h:86
DyePairMeasures
Different types of distances between two accessible volumes.
Definition: AV.h:42
Base class for a simple primitive-like type.
Definition: Value.h:23
void set_av_parameters_are_optimized(bool tf) const
Set whether the coordinates are optimized.
Definition: AV.h:290
#define IMP_VALUES(Name, PluralName)
Define the type for storing sets of values.
Definition: value_macros.h:23
T fret_efficiency(T distance, double forster_radius)
Computes the FRET efficiency given the distance and Forster radius.
Definition: AV.h:362
Class to search path on grids.
Key< 3 > ParticleIndexKey
The type used to identify a particle attribute in the Particles.
Definition: base_types.h:44
void add_attribute(TypeKey attribute_key, ParticleIndex particle, Type value)
add particle attribute with the specified key and initial value
A Cartesian vector in D-dimensions.
Definition: VectorD.h:39
Particle * get_particle() const
Get the particle pointer of the AV object.
Definition: AV.h:238
Distance distribution.
Definition: AV.h:48
Helper macros for implementing Decorators.
IMP::algebra::Vector3D get_radii()
Returns the radii of an object.
Definition: AV.h:272
static ParticleIndexKey get_particle_key(unsigned int i)
Get the particle key for the specified index.
Definition: AV.h:210
static FloatKey get_av_key(unsigned int i)
Get the FloatKey object for a specific AV feature.
Definition: AV.h:146
Simple 3D transformation class.
Particle * get_particle() const
Returns the particle decorated by this decorator.
Definition: Decorator.h:194
#define IMP_DECORATOR_METHODS(Name, Parent)
Base class for a simple primitive-like type.
VectorD< 3 > Vector3D
Definition: VectorD.h:408
double Float
Basic floating-point value (could be float, double...)
Definition: types.h:19
ParticleIndex get_particle_index() const
Get the particle index of the AV object.
Definition: AV.h:221
Simple 3D vector class.
Class to handle individual particles of a Model object.
Definition: Particle.h:43
#define IMP_USAGE_CHECK(expr, message)
A runtime test for incorrect usage of a class or method.
Definition: check_macros.h:168
Macros to help in implementing Value objects.
#define IMP_DECORATORS(Name, PluralName, Parent)
Define the types for storing sets of decorators.
T distance_fret(double fret_efficiency, double forster_radius)
Computes the distance between two volumes given the FRET efficiency and Forster radius.
Definition: AV.h:375
A decorator for helping deal with a generalized hierarchy.
std::vector< double > av_random_distances(const AV &av1, const AV &av2, int n_samples=10000)
Random sampling over AV/AV distances.
bool get_has_attribute(TypeKey attribute_key, ParticleIndex particle) const
return true if particle has attribute with the specified key
Particle * get_particle(unsigned int i) const
Get the particle pointer of the AV object at the specified index.
Definition: AV.h:247
Logging and error reporting support.
IMP::ParticleIndex search_labeling_site(const IMP::core::Hierarchy &hier, std::string json_str="", const nlohmann::json &json_data=nlohmann::json())
Find the particle index of a labeling site.
Mean FRET averaged distance R_E.
Definition: AV.h:44
Type get_attribute(TypeKey attribute_key, ParticleIndex particle)
get the value of the particle attribute with the specified key