OpenMS
ProductModel.h
Go to the documentation of this file.
1 // --------------------------------------------------------------------------
2 // OpenMS -- Open-Source Mass Spectrometry
3 // --------------------------------------------------------------------------
4 // Copyright The OpenMS Team -- Eberhard Karls University Tuebingen,
5 // ETH Zurich, and Freie Universitaet Berlin 2002-2023.
6 //
7 // This software is released under a three-clause BSD license:
8 // * Redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above copyright
11 // notice, this list of conditions and the following disclaimer in the
12 // documentation and/or other materials provided with the distribution.
13 // * Neither the name of any author or any participating institution
14 // may be used to endorse or promote products derived from this software
15 // without specific prior written permission.
16 // For a full list of authors, refer to the file AUTHORS.
17 // --------------------------------------------------------------------------
18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 // ARE DISCLAIMED. IN NO EVENT SHALL ANY OF THE AUTHORS OR THE CONTRIBUTING
22 // INSTITUTIONS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
25 // OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
26 // WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
27 // OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
28 // ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 //
30 // --------------------------------------------------------------------------
31 // $Maintainer: Timo Sachsenberg $
32 // $Authors: $
33 // --------------------------------------------------------------------------
34 
35 
36 #pragma once
37 
40 #include <OpenMS/KERNEL/Peak2D.h>
43 
44 namespace OpenMS
45 {
46 
59  template <UInt D>
60  class ProductModel;
61 
63  template <>
64  class OPENMS_DLLAPI ProductModel<2>:
65  public BaseModel<2>
66  {
67 public:
68 
70  enum {D = 2};
71 
72  typedef double IntensityType;
75 
78  BaseModel<D>(),
79  distributions_(D, nullptr)
80  {
81  this->setName(this->getProductName());
82 
83  //Register model info
84  for (UInt dim = 0; dim < D; ++dim)
85  {
87  this->subsections_.push_back(name);
88  this->defaults_.setValue(name, "GaussModel", "Name of the model used for this dimension");
89  }
90 
91  //defaults
92  this->defaults_.setValue("intensity_scaling", 1.0, "Scaling factor used to adjust the model distribution to the intensities of the data");
93  this->defaultsToParam_();
94  }
95 
97  ProductModel(const ProductModel & source) :
98  BaseModel<D>(source),
99  distributions_(D, nullptr),
100  scale_(source.scale_)
101  {
102  for (UInt dim = 0; dim < D; ++dim)
103  {
104  // clone source model
105  if (source.distributions_[dim])
106  {
107  ModelDescription<1> desc(source.distributions_[dim]);
108  setModel(dim, desc.createModel());
109  }
110  }
111  updateMembers_();
112  }
113 
115  ~ProductModel() override
116  {
117  for (Size dim = 0; dim < D; ++dim)
118  {
119  delete distributions_[dim];
120  }
121  }
122 
124  virtual ProductModel & operator=(const ProductModel & source)
125  {
126  if (&source == this) return *this;
127 
128  BaseModel<D>::operator=(source);
129  scale_ = source.scale_;
130 
131  for (UInt dim = 0; dim < D; ++dim)
132  {
133  if (source.distributions_[dim])
134  {
135  // clone source model
136  ModelDescription<1> desc(source.distributions_[dim]);
137  setModel(dim, desc.createModel());
138  }
139  else
140  {
141  distributions_[dim] = nullptr;
142  }
143  }
144  updateMembers_();
145 
146  return *this;
147  }
148 
150  IntensityType getIntensity(const PositionType & pos) const override
151  {
152  IntensityType intens(scale_);
153  for (UInt dim = 0; dim < D; ++dim)
154  {
155  if (distributions_[dim] == nullptr)
156  {
157  throw Exception::BaseException(__FILE__, __LINE__, OPENMS_PRETTY_FUNCTION, String("ProductModel: model for dimension ") + dim + " not set.", "");
158  }
159  intens *= distributions_[dim]->getIntensity(pos[dim]);
160  }
161  return intens;
162  }
163 
165  static BaseModel<D> * create()
166  {
167  return new ProductModel<D>();
168  }
169 
171  static const String getProductName()
172  {
173  return String("ProductModel") + D + "D";
174  }
175 
184  {
185  OPENMS_PRECONDITION(dim < D, "ProductModel<D>:getModel(Position): index overflow!");
186  if (dist == nullptr || dist == distributions_[dim])
187  {
188  return *this;
189  }
190 
191  delete distributions_[dim];
192  distributions_[dim] = dist;
193 
194  // Update model info
196  this->param_.removeAll(name + ':');
197  this->param_.insert(name + ':', distributions_[dim]->getParameters());
198  this->param_.setValue(name, distributions_[dim]->getName());
199 
200  return *this;
201  }
202 
204  {
205  OPENMS_PRECONDITION(dim < D, "ProductModel<D>:getModel(Position): index overflow!");
206  return distributions_[dim];
207  }
208 
211  {
212  return scale_;
213  }
214 
217  {
218  this->setCutOff(this->getCutOff() / scale_); // remove scaling from cutoff
219  scale_ = scale;
220  this->param_.setValue("intensity_scaling", scale);
221  this->setCutOff(this->getCutOff() * scale_); // scale cutoff
222  }
223 
225  void getSamples(SamplesType & cont) const override
226  {
227  cont.clear();
228  typedef BaseModel<1>::SamplesType Samples1D;
229  std::vector<Samples1D> samples(D);
230  // get samples for each dimension
231  for (Size dim = 0; dim < D; ++dim)
232  {
233  distributions_[dim]->getSamples(samples[dim]);
234  }
235 
237  std::vector<UInt> i(D, 0); // index vector
238 
239  while (i[D - 1] < samples[D - 1].size())
240  {
241  for (UInt dim = 0; dim < D; ++dim)
242  {
243  peak.getPosition()[dim] = samples[dim][i[dim]].getPosition()[0];
244  }
245  fillIntensity(peak);
246  cont.push_back(peak);
247 
248  ++i[0];
249  for (Size dim = 0; dim < D - 1; ++dim)
250  {
251  if (i[dim] >= samples[dim].size())
252  {
253  i[dim] = 0;
254  ++i[dim + 1];
255  }
256  }
257  }
258  return;
259  }
260 
261 protected:
262  void updateMembers_() override
263  {
265  scale_ = (double)(this->param_.getValue("intensity_scaling"));
266  for (UInt dim = 0; dim < D; ++dim)
267  {
269  if (this->param_.exists(name))
270  {
271  delete distributions_[dim];
272  distributions_[dim] = Factory<BaseModel<1> >::create(this->param_.getValue(name).toString());
273  Param copy = this->param_.copy(name + ":", true);
274  distributions_[dim]->setParameters(copy);
275  if (distributions_[dim]->getName().hasSubstring("IsotopeModel"))
276  {
277  static_cast<IsotopeModel *>(distributions_[dim])->setSamples(static_cast<IsotopeModel *>(distributions_[dim])->getFormula());
278  }
279  }
280  }
281  }
282 
283  std::vector<BaseModel<1> *> distributions_;
285  };
286 }
287 
Abstract base class for all D-dimensional models.
Definition: BaseModel.h:51
DPeak< D >::Type PeakType
Definition: BaseModel.h:56
BaseModel & operator=(const BaseModel &source)
assignment operator
Definition: BaseModel.h:77
std::vector< PeakType > SamplesType
Definition: BaseModel.h:57
void updateMembers_() override
This method is used to update extra member variables at the end of the setParameters() method.
Definition: BaseModel.h:152
Representation of a coordinate in D-dimensional space.
Definition: DPosition.h:55
Exception base class.
Definition: Exception.h:91
Returns FactoryProduct* based on the name of the desired concrete FactoryProduct.
Definition: Factory.h:63
Isotope distribution approximated using linear interpolation.
Definition: IsotopeModel.h:60
EmpiricalFormula getFormula()
return the Averagine peptide formula (mass calculated from mean mass and charge – use ....
Stores the name and parameters of a model.
Definition: ModelDescription.h:54
BaseModel< D > * createModel()
Definition: ModelDescription.h:96
Management and storage of parameters / INI files.
Definition: Param.h:70
Param copy(const std::string &prefix, bool remove_prefix=false) const
Returns a new Param object containing all entries that start with prefix.
static char const * shortDimensionName(UInt const dim)
Short name of the dimension (abbreviated form)
IntensityType getScale() const
return the intensity scaling factor
Definition: ProductModel.h:210
std::vector< BaseModel< 1 > * > distributions_
Definition: ProductModel.h:283
IntensityType scale_
Definition: ProductModel.h:284
DPosition< D > PositionType
Definition: ProductModel.h:73
BaseModel< D >::SamplesType SamplesType
Definition: ProductModel.h:74
ProductModel()
Default constructor.
Definition: ProductModel.h:77
void setScale(IntensityType scale)
set the intensity scaling factor
Definition: ProductModel.h:216
static BaseModel< D > * create()
create new ProductModel object (needed by Factory)
Definition: ProductModel.h:165
virtual ProductModel & operator=(const ProductModel &source)
assignment operator
Definition: ProductModel.h:124
BaseModel< 1 > * getModel(UInt dim) const
Definition: ProductModel.h:203
IntensityType getIntensity(const PositionType &pos) const override
intensity equals product of intensities in each dimension
Definition: ProductModel.h:150
ProductModel & setModel(UInt dim, BaseModel< 1 > *dist)
set model dist for dimension dim
Definition: ProductModel.h:183
~ProductModel() override
destructor
Definition: ProductModel.h:115
ProductModel(const ProductModel &source)
copy constructor
Definition: ProductModel.h:97
void getSamples(SamplesType &cont) const override
get reasonable set of samples from the model (i.e. for printing)
Definition: ProductModel.h:225
void updateMembers_() override
This method is used to update extra member variables at the end of the setParameters() method.
Definition: ProductModel.h:262
double IntensityType
Definition: ProductModel.h:72
static const String getProductName()
Returns the name of the model.
Definition: ProductModel.h:171
A more convenient string class.
Definition: String.h:60
unsigned int UInt
Unsigned integer type.
Definition: Types.h:94
size_t Size
Size type e.g. used as variable which can hold result of size()
Definition: Types.h:127
#define OPENMS_PRECONDITION(condition, message)
Precondition macro.
Definition: openms/include/OpenMS/CONCEPT/Macros.h:120
Class for product models i.e. models with D independent dimensions.
Definition: ProductModel.h:60
static bool hasSubstring(const String &this_s, const String &string)
Definition: StringUtilsSimple.h:137
Main OpenMS namespace.
Definition: FeatureDeconvolution.h:48