IMP  2.0.1
The Integrative Modeling Platform
Statistical.h
Go to the documentation of this file.
1 /**
2  * \file IMP/score_functor/Statistical.h
3  * \brief A Score on the distance between a pair of particles.
4  *
5  * Copyright 2007-2013 IMP Inventors. All rights reserved.
6  */
7 
8 #ifndef IMPSCORE_FUNCTOR_STATISTICAL_H
9 #define IMPSCORE_FUNCTOR_STATISTICAL_H
10 
11 #include <IMP/score_functor/score_functor_config.h>
12 #include "Score.h"
13 #include <IMP/Model.h>
14 #include "internal/PMFTable.h"
15 #include <IMP/algebra/utility.h>
16 IMPSCOREFUNCTOR_BEGIN_NAMESPACE
17 
18 /** Create a pairwise statistical potential from a file. The Keys passed
19  as a template argument are used to determine how to map the names of
20  the types as described in the loaded file to indexes. That is, if
21  atom::ResidueKey is passed as the Keys, the potential will expect a file
22  which has one line for each pair of residue names.
23 
24  The expected file format is:
25 \verbatim
26  bin_width number_a number_b [offset]
27  key_0a key_0b bin0 bin1 bin2...
28  key_1a key_1b bin0 bin1 bin2...
29 \endverbatim
30 
31  The order of the lines (after the first one) does not matter.
32  The bin_width is how much distance is allocated per bin (the distance used
33  is that between the points). number_a and number_b are the numbers of
34  particle types (number_b should only be specified if BIPARTITE is set).
35 
36  \note The values read in the file are for bins. That is, the first bin
37  is from offset to offset+width. The second is offset+width to offset+
38  2width.
39  As a result, when interpolation is used, the function achieves the
40  bin value at the center of the bin.
41 
42  \param[in] Key is an IMP::Key which maps between names and indices
43  \param[in] BIPARTITE If true, the two sets of particles being stored are
44  different (e.g. a protein atom and a ligand atom), otherwise they are
45  assumed to both be the same. Appropriate values must be provided
46  in the file.
47  \param[in] INTERPOLATE If true, even the scores without derivatives are
48  spline interpolated. If false, only the evaluation of derivatives is
49  interpolated with a spline.
50 */
51 template <class Key, bool BIPARTITE, bool INTERPOLATE, bool SPARSE=false>
52 class Statistical: public Score {
53  internal::PMFTable<BIPARTITE, INTERPOLATE, SPARSE> table_;
54  double threshold_;
55  IntKey key_;
56 public:
57  /** \param[in] k The attribute to use for determining the particle types
58  \param[in] threshold The maximum distance to score
59  \param[in] data_file Where to load the file from.
60  */
62  double threshold,
63  base::TextInput data_file):
64  table_(0),
65  threshold_(threshold), key_(k){
66  IMP_USAGE_CHECK(!BIPARTITE,
67  "Constructor can only be used for non-bipartite scores.");
68  table_.template initialize<Key>(data_file);
69  }
70  /** \param[in] k The attribute to use for determining the particle types
71  \param[in] threshold The maximum distance to score
72  \param[in] data_file Where to load the file from.
73  \param[in] shift The offset for the types of the second set of types.
74  eg, if the score is on protein and ligand atoms, the ligand atom types
75  start with the value shift.
76  */
78  double threshold,
79  base::TextInput data_file,
80  unsigned int shift):
81  table_(shift),
82  threshold_(threshold), key_(k){
83  IMP_USAGE_CHECK(BIPARTITE,
84  "Constructor can only be used for bipartite scores.");
85  table_.template initialize<Key>(data_file);
86  }
87 
88  // depend on get_is_trivially_zero
89  double get_score(Model *m, const base::Array<2, ParticleIndex>& pp,
90  double distance) const {
91  if (distance >= threshold_ || distance < 0.001) {
92  return 0;
93  }
94  int pt= m->get_attribute(key_, pp[0]);
95  int lt= m->get_attribute(key_, pp[1]);
96  if (pt==-1 || lt==-1) return 0;
97  return table_.get_score(pt, lt, distance);
98  }
99  DerivativePair get_score_and_derivative(Model *m,
100  const base::Array<2,
101  ParticleIndex>&pp,
102  double distance) const {
103  if (distance >= threshold_ || distance < 0.001) {
104  return DerivativePair(0,0);
105  }
106  int pt= m->get_attribute(key_, pp[0]);
107  int lt= m->get_attribute(key_, pp[1]);
108  if (pt==-1 || lt==-1) return DerivativePair(0,0);
109  return table_.get_score_with_derivative(pt,
110  lt, distance);
111  }
112  double get_maximum_range(Model *,
113  const base::Array<2, ParticleIndex>& ) const {
114  return std::min(threshold_, table_.get_max());
115  }
116  bool get_is_trivially_zero(Model *m,
117  const base::Array<2, ParticleIndex>& p,
118  double squared_distance) const {
119  return squared_distance > algebra::get_squared(get_maximum_range(m,p));
120  }
121 };
122 
123 IMPSCOREFUNCTOR_END_NAMESPACE
124 
125 #endif /* IMPSCORE_FUNCTOR_STATISTICAL_H */