bpp-phyl3  3.0.0
FrequencySet.h
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: The Bio++ Development Group
2 //
3 // SPDX-License-Identifier: CECILL-2.1
4 
5 #ifndef BPP_PHYL_MODEL_FREQUENCYSET_FREQUENCYSET_H
6 #define BPP_PHYL_MODEL_FREQUENCYSET_FREQUENCYSET_H
7 
8 
9 #include "../StateMap.h"
10 
11 // From bpp-core:
16 
17 // From bpp-seq:
19 
20 namespace bpp
21 {
28  public virtual ParameterAliasable
29 {
30 public:
32 
33 public:
37  virtual std::shared_ptr<const Alphabet> getAlphabet() const = 0;
38 
42  virtual const Alphabet& alphabet() const = 0;
43 
47  virtual const StateMapInterface& stateMap() const = 0;
48 
52  virtual std::shared_ptr<const StateMapInterface> getStateMap() const = 0;
53 
57  virtual const Vdouble& getFrequencies() const = 0;
58 
62  virtual const std::map<int, double> getAlphabetStatesFrequencies() const = 0;
63 
71  virtual void setFrequencies(const std::vector<double>& frequencies) = 0;
72 
80  virtual void setFrequenciesFromAlphabetStatesFrequencies(const std::map<int, double>& frequencies) = 0;
81 
82  virtual std::string getName() const = 0;
83 
87  virtual size_t getNumberOfFrequencies() const = 0;
88 
89 public:
90  static std::shared_ptr<IntervalConstraint> FREQUENCE_CONSTRAINT_SMALL;
91  static std::shared_ptr<IntervalConstraint> FREQUENCE_CONSTRAINT_MILLI;
92  static std::shared_ptr<IntervalConstraint> FREQUENCE_CONSTRAINT_CENTI;
93 };
94 
100  public virtual FrequencySetInterface,
102 {
103 private:
104  std::shared_ptr<const Alphabet> alphabet_;
105  std::shared_ptr<const StateMapInterface> stateMap_;
106  std::vector<double> freq_;
107  std::string name_;
108 
109 public:
111  std::shared_ptr<const StateMapInterface> stateMap,
112  const std::string& prefix,
113  const std::string& name) :
117  freq_(stateMap->getNumberOfModelStates()),
118  name_(name)
119  {}
120 
123  alphabet_(af.alphabet_),
124  stateMap_(af.stateMap_),
125  freq_(af.freq_),
126  name_(af.name_)
127  {}
128 
130  {
132  alphabet_ = af.alphabet_;
133  stateMap_ = af.stateMap_;
134  freq_ = af.freq_;
135  name_ = af.name_;
136  return *this;
137  }
138 
139 public:
140  std::shared_ptr<const Alphabet> getAlphabet() const override { return alphabet_; }
141 
142  const Alphabet& alphabet() const override { return *alphabet_; }
143 
144  const StateMapInterface& stateMap() const override { return *stateMap_; }
145 
146  std::shared_ptr<const StateMapInterface> getStateMap() const override { return stateMap_; }
147 
148  const Vdouble& getFrequencies() const override { return freq_; }
149 
150  const std::map<int, double> getAlphabetStatesFrequencies() const override;
151 
161  void setFrequenciesFromAlphabetStatesFrequencies(const std::map<int, double>& frequencies) override;
162 
163  size_t getNumberOfFrequencies() const override { return freq_.size(); }
164 
165  std::string getName() const override { return name_; }
166 
167  void normalize()
168  {
169  double x = 0;
170  for (auto f : freq_)
171  {
172  x += f;
173  }
174  freq_ /= x;
175  }
176 
177 protected:
178  std::vector<double>& getFrequencies_() { return freq_; }
179  double& getFreq_(size_t i) { return freq_[i]; }
180  const double& getFreq_(size_t i) const { return freq_[i]; }
181  void setFrequencies_(const std::vector<double>& frequencies) { freq_ = frequencies; }
182 };
183 
184 
197  public AbstractFrequencySet
198 {
199 private:
204 
205 public:
211  std::shared_ptr<const StateMapInterface> stateMap,
212  bool allowNullFreqs = false,
213  unsigned short method = 1,
214  const std::string& name = "Full");
215 
217  std::shared_ptr<const StateMapInterface> stateMap,
218  const std::vector<double>& initFreqs,
219  bool allowNullFreqs = false,
220  unsigned short method = 1,
221  const std::string& name = "Full");
222 
223  FullFrequencySet* clone() const override
224  {
225  return new FullFrequencySet(*this);
226  }
227 
228 public:
229  void setFrequencies(const std::vector<double>& frequencies) override;
230 
231  unsigned short getMethod() const { return sFreq_.getMethod();}
232 
233  void setNamespace(const std::string& nameSpace) override;
234 
235 protected:
236  void fireParameterChanged(const ParameterList& parameters) override;
237 
238 private:
239  void updateFreq_();
240 };
241 
242 class TransitionModelInterface;
243 
251  public AbstractFrequencySet
252 {
253 private:
254  std::shared_ptr<TransitionModelInterface> model_;
255 
256 public:
257  FromModelFrequencySet(std::shared_ptr<TransitionModelInterface> model);
258 
260 
262 
263  FromModelFrequencySet* clone() const override { return new FromModelFrequencySet(*this); }
264 
265  virtual ~FromModelFrequencySet();
266 
267 public:
269  {
270  return *model_;
271  }
272 
273  std::shared_ptr<const TransitionModelInterface> getModel() const
274  {
275  return model_;
276  }
277 
278  void setFrequencies(const std::vector<double>& frequencies) override;
279 
280  void fireParameterChanged(const ParameterList& pl) override;
281 
282  void setNamespace(const std::string& name) override;
283 };
284 
285 
294  public AbstractFrequencySet
295 {
296 private:
297  std::unique_ptr<FrequencySetInterface> freqSet_;
298  std::vector<double> rateFreqs_;
299 
300 public:
302  std::unique_ptr<FrequencySetInterface> freqSet,
303  const std::vector<double>& rateFreqs);
304 
306  AbstractFrequencySet(mmfs),
307  freqSet_(mmfs.freqSet_->clone()),
308  rateFreqs_(mmfs.rateFreqs_)
309  {}
310 
312  {
314  freqSet_.reset(mmfs.freqSet_->clone());
315  rateFreqs_ = mmfs.rateFreqs_;
316  return *this;
317  }
318 
319  MarkovModulatedFrequencySet* clone() const override { return new MarkovModulatedFrequencySet(*this); }
320 
322 
323 public:
324  void setFrequencies(const std::vector<double>& frequencies) override
325  {
326  // Just forward this method to the sequence state frequencies set. This may change in the future...
327  freqSet_->setFrequencies(frequencies);
328  }
329 
330  void fireParameterChanged(const ParameterList& pl) override
331  {
332  freqSet_->matchParametersValues(pl);
334  }
335 
337 };
338 
339 
346  public AbstractFrequencySet
347 {
348 public:
358  std::shared_ptr<const StateMapInterface> stateMap,
359  const std::vector<double>& initFreqs,
360  const std::string& name = "Fixed");
361 
369  std::shared_ptr<const StateMapInterface> stateMap,
370  const std::string& name = "Fixed");
371 
372  FixedFrequencySet* clone() const override { return new FixedFrequencySet(*this); }
373 
374 public:
375  void setFrequencies(const std::vector<double>& frequencies) override;
376 };
377 
378 
385  public AbstractFrequencySet
386 {
387 private:
388  std::string path_;
389  size_t nCol_;
390 
391 public:
393  std::shared_ptr<const StateMapInterface> stateMap,
394  const std::string& path,
395  size_t nCol = 1);
396 
397  UserFrequencySet(const UserFrequencySet& fmfs);
398 
400 
401  UserFrequencySet* clone() const override { return new UserFrequencySet(*this); }
402 
403  virtual ~UserFrequencySet(){}
404 
405 public:
406  const std::string& getPath() const { return path_; }
407 
408  size_t getColumnNumber() const
409  {
410  return nCol_;
411  }
412 
413  void setFrequencies(const std::vector<double>& frequencies) override;
414 
415 protected:
416  void readFromFile_();
417 };
418 } // end of namespace bpp.
419 #endif // BPP_PHYL_MODEL_FREQUENCYSET_FREQUENCYSET_H
Basic implementation of the FrequencySet interface.
Definition: FrequencySet.h:102
std::shared_ptr< const Alphabet > alphabet_
Definition: FrequencySet.h:104
const Alphabet & alphabet() const override
Definition: FrequencySet.h:142
void setFrequencies_(const std::vector< double > &frequencies)
Definition: FrequencySet.h:181
const std::map< int, double > getAlphabetStatesFrequencies() const override
std::shared_ptr< const Alphabet > getAlphabet() const override
Definition: FrequencySet.h:140
std::shared_ptr< const StateMapInterface > stateMap_
Definition: FrequencySet.h:105
void setFrequenciesFromAlphabetStatesFrequencies(const std::map< int, double > &frequencies) override
Set the Frequencies from the one of the map which keys match with a letter of the Alphabet....
size_t getNumberOfFrequencies() const override
Definition: FrequencySet.h:163
std::shared_ptr< const StateMapInterface > getStateMap() const override
Definition: FrequencySet.h:146
double & getFreq_(size_t i)
Definition: FrequencySet.h:179
AbstractFrequencySet(const AbstractFrequencySet &af)
Definition: FrequencySet.h:121
std::vector< double > freq_
Definition: FrequencySet.h:106
std::vector< double > & getFrequencies_()
Definition: FrequencySet.h:178
const double & getFreq_(size_t i) const
Definition: FrequencySet.h:180
std::string getName() const override
Definition: FrequencySet.h:165
AbstractFrequencySet & operator=(const AbstractFrequencySet &af)
Definition: FrequencySet.h:129
AbstractFrequencySet(std::shared_ptr< const StateMapInterface > stateMap, const std::string &prefix, const std::string &name)
Definition: FrequencySet.h:110
const Vdouble & getFrequencies() const override
Definition: FrequencySet.h:148
const StateMapInterface & stateMap() const override
Definition: FrequencySet.h:144
AbstractParameterAliasable & operator=(const AbstractParameterAliasable &ap)
FrequencySet useful for homogeneous and stationary models.
Definition: FrequencySet.h:347
void setFrequencies(const std::vector< double > &frequencies) override
Set the parameters in order to match a given set of frequencies.
FixedFrequencySet(std::shared_ptr< const StateMapInterface > stateMap, const std::vector< double > &initFreqs, const std::string &name="Fixed")
Construction with user-defined frequencies on the states of the model.
FixedFrequencySet * clone() const override
Definition: FrequencySet.h:372
Parametrize a set of state frequencies.
Definition: FrequencySet.h:29
virtual const Vdouble & getFrequencies() const =0
static std::shared_ptr< IntervalConstraint > FREQUENCE_CONSTRAINT_SMALL
Definition: FrequencySet.h:90
virtual std::shared_ptr< const Alphabet > getAlphabet() const =0
virtual void setFrequenciesFromAlphabetStatesFrequencies(const std::map< int, double > &frequencies)=0
Set the Frequencies from the one of the map which keys match with a letter of the Alphabet....
virtual const StateMapInterface & stateMap() const =0
FrequencySetInterface * clone() const =0
static std::shared_ptr< IntervalConstraint > FREQUENCE_CONSTRAINT_MILLI
Definition: FrequencySet.h:91
virtual std::string getName() const =0
virtual const std::map< int, double > getAlphabetStatesFrequencies() const =0
virtual const Alphabet & alphabet() const =0
virtual std::shared_ptr< const StateMapInterface > getStateMap() const =0
virtual size_t getNumberOfFrequencies() const =0
virtual void setFrequencies(const std::vector< double > &frequencies)=0
Set the parameters in order to match a given set of frequencies.
static std::shared_ptr< IntervalConstraint > FREQUENCE_CONSTRAINT_CENTI
Definition: FrequencySet.h:92
FrequencySet defined from the equilibrium distribution of a given model.
Definition: FrequencySet.h:252
const TransitionModelInterface & model() const
Definition: FrequencySet.h:268
void fireParameterChanged(const ParameterList &pl) override
FromModelFrequencySet & operator=(const FromModelFrequencySet &fmfs)
std::shared_ptr< const TransitionModelInterface > getModel() const
Definition: FrequencySet.h:273
FromModelFrequencySet * clone() const override
Definition: FrequencySet.h:263
void setFrequencies(const std::vector< double > &frequencies) override
Set the parameters in order to match a given set of frequencies.
void setNamespace(const std::string &name) override
std::shared_ptr< TransitionModelInterface > model_
Definition: FrequencySet.h:254
FromModelFrequencySet(std::shared_ptr< TransitionModelInterface > model)
A generic FrequencySet allowing for the estimation of all frequencies.
Definition: FrequencySet.h:198
void fireParameterChanged(const ParameterList &parameters) override
Simplex sFreq_
Simplex to handle the probabilities and the parameters.
Definition: FrequencySet.h:203
unsigned short getMethod() const
Definition: FrequencySet.h:231
FullFrequencySet(std::shared_ptr< const StateMapInterface > stateMap, bool allowNullFreqs=false, unsigned short method=1, const std::string &name="Full")
Construction with uniform frequencies on the states of the alphabet.
void setNamespace(const std::string &nameSpace) override
FullFrequencySet * clone() const override
Definition: FrequencySet.h:223
void setFrequencies(const std::vector< double > &frequencies) override
Set the parameters in order to match a given set of frequencies.
FrequencySet to be used with a Markov-modulated substitution model.
Definition: FrequencySet.h:295
void setFrequencies(const std::vector< double > &frequencies) override
Set the parameters in order to match a given set of frequencies.
Definition: FrequencySet.h:324
MarkovModulatedFrequencySet(std::unique_ptr< FrequencySetInterface > freqSet, const std::vector< double > &rateFreqs)
std::unique_ptr< FrequencySetInterface > freqSet_
Definition: FrequencySet.h:297
MarkovModulatedFrequencySet & operator=(const MarkovModulatedFrequencySet &mmfs)
Definition: FrequencySet.h:311
void fireParameterChanged(const ParameterList &pl) override
Definition: FrequencySet.h:330
const FrequencySetInterface & getStatesFrequencySet() const
Definition: FrequencySet.h:336
MarkovModulatedFrequencySet(const MarkovModulatedFrequencySet &mmfs)
Definition: FrequencySet.h:305
std::vector< double > rateFreqs_
Definition: FrequencySet.h:298
MarkovModulatedFrequencySet * clone() const override
Definition: FrequencySet.h:319
unsigned short getMethod() const
Map the states of a given alphabet which have a model state.
Definition: StateMap.h:25
Interface for all transition models.
FrequencySet to be read in a file. More specifically, a frequency set is read in a column of a given ...
Definition: FrequencySet.h:386
void setFrequencies(const std::vector< double > &frequencies) override
Set the parameters in order to match a given set of frequencies.
virtual ~UserFrequencySet()
Definition: FrequencySet.h:403
UserFrequencySet & operator=(const UserFrequencySet &fmfs)
UserFrequencySet(std::shared_ptr< const StateMapInterface > stateMap, const std::string &path, size_t nCol=1)
User.
UserFrequencySet * clone() const override
Definition: FrequencySet.h:401
size_t getColumnNumber() const
Definition: FrequencySet.h:408
const std::string & getPath() const
Definition: FrequencySet.h:406
static std::vector< T > kroneckerMult(const std::vector< T > &v1, const std::vector< T > &v2)
Defines the basic types of data flow nodes.
std::vector< double > Vdouble