IMP logo
IMP Reference Guide  2.6.1
The Integrative Modeling Platform
SAXSMultiStateModelScore.h
Go to the documentation of this file.
1 /**
2  * \file IMP/multi_state/SAXSMultiStateModelScore.h
3  * \brief
4  *
5  * \authors Dina Schneidman
6  * Copyright 2007-2016 IMP Inventors. All rights reserved.
7  *
8  */
9 
10 #ifndef IMPMULTI_STATE_SAXS_MULTI_STATE_MODEL_SCORE_H
11 #define IMPMULTI_STATE_SAXS_MULTI_STATE_MODEL_SCORE_H
12 
13 #include "MultiStateModelScore.h"
14 #include "MultiStateModel.h"
15 
16 #include <IMP/saxs/Profile.h>
18 #include <IMP/Object.h>
19 
20 #include <vector>
21 
22 IMPMULTISTATE_BEGIN_NAMESPACE
23 
24 /** Score multi-state models against SAXS profiles */
25 template <typename ScoringFunctionT>
27 public:
28 
29  /* if c1_c2_approximate=true, get_score will return approximate score
30  based on average c1/c2
31  min_weight_threshold, multi-state models with one or more weights below
32  min_weight_threshold will get a negative score
33  */
35  const saxs::Profile* exp_profile,
36  bool c1_c2_approximate,
37  double min_c1 = 0.99, double max_c1 = 1.05,
38  double min_c2 = -0.5, double max_c2 = 2.0);
39 
40  double get_score(const MultiStateModel& m) const;
41 
42  double get_score(const MultiStateModel& m,
43  Vector<double>& weights) const;
44 
45  saxs::WeightedFitParameters get_fit_parameters(MultiStateModel& m) const;
46 
47  saxs::WeightedFitParameters get_fit_parameters() const;
48 
49  void write_fit_file(MultiStateModel& m,
51  const std::string fit_file_name) const;
52 
53  std::string get_state_name(unsigned int id) const { return profiles_[id]->get_name(); }
54 
55  std::string get_dataset_name() const { return exp_profile_->get_name(); }
56 
57  double get_average_c1() const { return average_c1_; }
58  double get_average_c2() const { return average_c2_; }
59 
60  void set_average_c1_c2(const Vector<saxs::WeightedFitParameters>& fps);
61 
62 private:
63  void resample(const saxs::Profile* exp_profile,
64  const saxs::Profiles& profiles,
65  saxs::Profiles& resampled_profiles);
66 
67  void set_average_c1_c2(saxs::WeightedProfileFitter<ScoringFunctionT>* score,
68  const saxs::Profiles& profiles);
69 
70 private:
71  // input profiles
72  const saxs::Profiles profiles_;
74 
75  // resampled on experimental profile q's
76  saxs::Profiles resampled_profiles_;
77 
78  // scoring with exp_profile_
80 
81  double min_c1_, max_c1_, min_c2_, max_c2_;
82  double average_c1_, average_c2_;
83 
84  // approximate c1/c2 at get_score(), do accurate fitting at get_fit_parameters()
85  bool c1_c2_approximate_;
86 
87  // do not perform any c1/c2 fitting
88  bool c1_c2_no_fitting_;
89 };
90 
91 template <typename ScoringFunctionT>
93  const saxs::Profiles& profiles,
94  const saxs::Profile* exp_profile,
95  bool c1_c2_approximate,
96  double min_c1, double max_c1,
97  double min_c2, double max_c2) :
98  profiles_(profiles), exp_profile_(exp_profile),
99  min_c1_(min_c1), max_c1_(max_c1), min_c2_(min_c2), max_c2_(max_c2),
100  c1_c2_approximate_(c1_c2_approximate), c1_c2_no_fitting_(false) {
101 
102  if(profiles_.size() < 1) {
103  IMP_THROW("SAXSMultiStateModelScore - please provide at least one profile"
104  << std::endl, IOException);
105  }
106 
107  // resample all models profiles
108  resample(exp_profile_, profiles_, resampled_profiles_);
109 
110  // init scoring object
111  score_ = new saxs::WeightedProfileFitter<ScoringFunctionT>(exp_profile_);
112 
113  // compute average c1/c2
114  set_average_c1_c2(score_, resampled_profiles_);
115 }
116 
117 template <typename ScoringFunctionT>
118 void SAXSMultiStateModelScore<ScoringFunctionT>::resample(
119  const saxs::Profile* exp_profile,
120  const saxs::Profiles& profiles,
121  saxs::Profiles& resampled_profiles) {
122 
123  resampled_profiles.reserve(profiles.size());
124  for(unsigned int i=0; i<profiles.size(); i++) {
125  saxs::Profile *resampled_profile =
126  new saxs::Profile(exp_profile->get_min_q(), exp_profile->get_max_q(),
127  exp_profile->get_delta_q());
128  profiles[i]->resample(exp_profile, resampled_profile);
129  resampled_profiles.push_back(resampled_profile);
130  if(!profiles[i]->is_partial_profile()) c1_c2_no_fitting_ = true;
131  }
132 }
133 
134 template <typename ScoringFunctionT>
135 void SAXSMultiStateModelScore<ScoringFunctionT>::set_average_c1_c2(
136  saxs::WeightedProfileFitter<ScoringFunctionT>* score,
137  const saxs::Profiles& profiles) {
138  if(c1_c2_no_fitting_) return;
139  average_c1_ = 0.0;
140  average_c2_ = 0.0;
141  saxs::ProfilesTemp profiles_temp(1);
142  for(unsigned int i=0; i<profiles.size(); i++) {
143  profiles_temp[0] = profiles[i];
144  saxs::WeightedFitParameters fp =
145  score->fit_profile(profiles_temp, min_c1_, max_c1_, min_c2_, max_c2_);
146  average_c1_ += fp.get_c1();
147  average_c2_ += fp.get_c2();
148  }
149  average_c1_ /= profiles.size();
150  average_c2_ /= profiles.size();
151 }
152 
153 template <typename ScoringFunctionT>
154 void SAXSMultiStateModelScore<ScoringFunctionT>::set_average_c1_c2(
155  const Vector<saxs::WeightedFitParameters>& fps) {
156  if(c1_c2_no_fitting_) return;
157  double c1 = 0.0;
158  double c2 = 0.0;
159  for(unsigned int i=0; i<fps.size(); i++) {
160  c1 += fps[i].get_c1();
161  c2 += fps[i].get_c2();
162  }
163  c1 /= fps.size();
164  c2 /= fps.size();
165 
166  average_c1_ = c1;
167  average_c2_ = c2;
168 }
169 
170 
171 template <typename ScoringFunctionT>
172 double SAXSMultiStateModelScore<ScoringFunctionT>::get_score(const MultiStateModel& m,
173  Vector<double>& weights) const {
174  const Vector<unsigned int>& states = m.get_states();
175  saxs::ProfilesTemp profiles(states.size());
176  for(unsigned int i=0; i<states.size(); i++) {
177  profiles[i] = resampled_profiles_[states[i]];
178  if(c1_c2_approximate_ && !c1_c2_no_fitting_)
179  profiles[i]->sum_partial_profiles(average_c1_, average_c2_);
180  }
181 
182  double chi;
183  if(c1_c2_approximate_ || c1_c2_no_fitting_) { // just score calculation
184  chi = score_->compute_score(profiles, weights);
185  } else { // optimize c1/c2 fit and score
186  saxs::WeightedFitParameters fp =
187  score_->fit_profile(profiles, min_c1_, max_c1_, min_c2_, max_c2_);
188  chi = fp.get_chi();
189  }
190 
191  return chi;
192 }
193 
194 template <typename ScoringFunctionT>
195 double SAXSMultiStateModelScore<ScoringFunctionT>::get_score(const MultiStateModel& m) const {
196  Vector<double> weights;
197  return get_score(m, weights);
198 }
199 
200 
201 template <typename ScoringFunctionT>
202 saxs::WeightedFitParameters
203  SAXSMultiStateModelScore<ScoringFunctionT>::get_fit_parameters(MultiStateModel& m) const {
204 
205  if(c1_c2_no_fitting_) {
206  Vector<double> weights;
207  double s = get_score(m, weights);
208  saxs::WeightedFitParameters wfp(s, 1.0, 0.0, weights);
209  return wfp;
210  }
211 
212  const Vector<unsigned int>& states = m.get_states();
213  saxs::ProfilesTemp profiles(states.size());
214  for(unsigned int i=0; i<states.size(); i++)
215  profiles[i] = resampled_profiles_[states[i]];
216 
217  saxs::WeightedFitParameters fp =
218  score_->fit_profile(profiles, min_c1_, max_c1_, min_c2_, max_c2_);
219  m.set_score(fp.get_chi());
220 
221  return fp;
222 }
223 
224 template <typename ScoringFunctionT>
225 saxs::WeightedFitParameters
226  SAXSMultiStateModelScore<ScoringFunctionT>::get_fit_parameters() const {
227 
228  if(c1_c2_no_fitting_) {
229  Vector<double> weights;
230  double s = score_->compute_score(resampled_profiles_, weights);
231  saxs::WeightedFitParameters wfp(s, 1.0, 0.0, weights);
232  return wfp;
233  }
234 
235  saxs::WeightedFitParameters fp = score_->fit_profile(resampled_profiles_,
236  min_c1_, max_c1_,
237  min_c2_, max_c2_);
238  return fp;
239 }
240 
241 template <typename ScoringFunctionT>
242 void SAXSMultiStateModelScore<ScoringFunctionT>::write_fit_file(MultiStateModel& m,
243  const saxs::WeightedFitParameters& fp,
244  const std::string fit_file_name) const {
245 
246  const Vector<unsigned int>& states = m.get_states();
247  saxs::ProfilesTemp profiles(states.size());
248  for(unsigned int i=0; i<states.size(); i++)
249  profiles[i] = resampled_profiles_[states[i]];
250  score_->write_fit_file(profiles, fp, fit_file_name);
251 }
252 
253 IMPMULTISTATE_END_NAMESPACE
254 
255 #endif /* IMPMULTI_STATE_SAXS_MULTI_STATE_MODEL_SCORE_H */
IMP::Vector< IMP::Pointer< Profile > > Profiles
Definition: Profile.h:295
base class for MultiStateModel scoring classes
Fitting of multiple profiles to the experimental one. The weights of the profiles are computed analyt...
Base class for MultiStateModel scoring classes.
An input/output exception.
Definition: exception.h:174
Fitting of multiple profiles to the experimental one.
Keep track of multiple states.
IMP::Vector< IMP::WeakPointer< Profile > > ProfilesTemp
Definition: Profile.h:295
#define IMP_THROW(message, exception_name)
Throw an exception with a message.
Definition: check_macros.h:50
Parameters of a weighted fit, from WeightedProfileFitter.
A shared base class to help in debugging and things.
Keep track of multiple states.
A class for profile storing and computation.