bpp-phyl  2.2.0
FrequenciesSet.h
Go to the documentation of this file.
1 //
2 // File: FrequenciesSet.h
3 // Created by: Bastien Boussau
4 // Julien Dutheil
5 // Created on: Tue Aug 21 2007
6 //
7 
8 /*
9  Copyright or (c) or Copr. Bio++ Development Team, (November 16, 2004)
10 
11  This software is a computer program whose purpose is to provide classes
12  for phylogenetic data analysis.
13 
14  This software is governed by the CeCILL license under French law and
15  abiding by the rules of distribution of free software. You can use,
16  modify and/ or redistribute the software under the terms of the CeCILL
17  license as circulated by CEA, CNRS and INRIA at the following URL
18  "http://www.cecill.info".
19 
20  As a counterpart to the access to the source code and rights to copy,
21  modify and redistribute granted by the license, users are provided only
22  with a limited warranty and the software's author, the holder of the
23  economic rights, and the successive licensors have only limited
24  liability.
25 
26  In this respect, the user's attention is drawn to the risks associated
27  with loading, using, modifying and/or developing or reproducing the
28  software by the user in light of its specific status of free software,
29  that may mean that it is complicated to manipulate, and that also
30  therefore means that it is reserved for developers and experienced
31  professionals having in-depth computer knowledge. Users are therefore
32  encouraged to load and test the software's suitability as regards their
33  requirements in conditions enabling the security of their systems and/or
34  data to be ensured and, more generally, to use and operate it in the
35  same conditions as regards security.
36 
37  The fact that you are presently reading this means that you have had
38  knowledge of the CeCILL license and that you accept its terms.
39  */
40 
41 #ifndef _FREQUENCIESSET_H_
42 #define _FREQUENCIESSET_H_
43 
44 #include "../StateMap.h"
45 
46 // From bpp-core:
47 #include <Bpp/Numeric/Parametrizable.h>
48 #include <Bpp/Numeric/AbstractParametrizable.h>
49 #include <Bpp/Numeric/VectorTools.h>
50 #include <Bpp/Numeric/Prob/Simplex.h>
51 
52 // From bpp-seq:
53 #include <Bpp/Seq/Alphabet/Alphabet.h>
54 
55 namespace bpp
56 {
63  public virtual Parametrizable
64 {
65 public:
66 #ifndef NO_VIRTUAL_COV
67  FrequenciesSet* clone() const = 0;
68 #endif
69 
70 public:
74  virtual const Alphabet* getAlphabet() const = 0;
75 
79  virtual const StateMap& getStateMap() const = 0;
80 
84  virtual const std::vector<double> getFrequencies() const = 0;
85 
89  virtual const std::map<int, double> getAlphabetStatesFrequencies() const = 0;
90 
98  virtual void setFrequencies(const std::vector<double>& frequencies) = 0;
99 
107  virtual void setFrequenciesFromAlphabetStatesFrequencies(const std::map<int, double>& frequencies) = 0;
108 
109  virtual std::string getName() const = 0;
110 
114  virtual size_t getNumberOfFrequencies() const = 0;
115 
116 public:
117  static IntervalConstraint FREQUENCE_CONSTRAINT_SMALL;
118  static IntervalConstraint FREQUENCE_CONSTRAINT_MILLI;
119 };
120 
126  public virtual FrequenciesSet,
127  public AbstractParametrizable
128 {
129 private:
130  const Alphabet* alphabet_;
131  std::auto_ptr<StateMap> stateMap_;
132  std::vector<double> freq_;
133  std::string name_;
134 
135 public:
136  AbstractFrequenciesSet(StateMap* stateMap, const std::string& prefix, const std::string& name) :
137  AbstractParametrizable(prefix),
138  alphabet_(stateMap->getAlphabet()),
139  stateMap_(stateMap),
140  freq_(stateMap->getNumberOfModelStates()),
141  name_(name)
142  {}
143 
144 #ifndef NO_VIRTUAL_COV
146 #else
147  Clonable*
148 #endif
149  clone() const = 0;
150 
152  AbstractParametrizable(af),
153  alphabet_(af.alphabet_),
154  stateMap_(af.stateMap_->clone()),
155  freq_(af.freq_),
156  name_(af.name_)
157  {}
158 
160  {
161  AbstractParametrizable::operator=(af);
162  alphabet_ = af.alphabet_;
163  stateMap_.reset(af.stateMap_->clone());
164  freq_ = af.freq_;
165  name_ = af.name_;
166  return *this;
167  }
168 
169 public:
170  const Alphabet* getAlphabet() const { return alphabet_; }
171 
172  const StateMap& getStateMap() const { return *stateMap_; }
173 
174  const std::vector<double> getFrequencies() const { return freq_; }
175 
176  const std::map<int, double> getAlphabetStatesFrequencies() const;
177 
187  void setFrequenciesFromAlphabetStatesFrequencies(const std::map<int, double>& frequencies);
188 
189  size_t getNumberOfFrequencies() const { return freq_.size(); }
190 
191  std::string getName() const { return(name_); }
192 
193  void normalize()
194  {
195  double x = 0;
196  for (size_t i = 0; i < freq_.size(); i++)
197  x += freq_[i];
198  freq_ /= x;
199  }
200 
201 protected:
202  std::vector<double>& getFrequencies_() { return freq_; }
203  double& getFreq_(size_t i) { return freq_[i]; }
204  const double& getFreq_(size_t i) const { return freq_[i]; }
205  void setFrequencies_(const std::vector<double>& frequencies) { freq_ = frequencies; }
206 };
207 
222 {
223 private:
227  Simplex sFreq_;
228 
229 public:
234  FullFrequenciesSet(StateMap* stateMap, bool allowNullFreqs = false, unsigned short method = 1, const std::string& name = "Full.");
235  FullFrequenciesSet(StateMap* stateMap, const std::vector<double>& initFreqs, bool allowNullFreqs = false, unsigned short method = 1, const std::string& name = "Full.");
236 
237  FullFrequenciesSet* clone() const { return new FullFrequenciesSet(*this); }
238 
239 public:
240  void setFrequencies(const std::vector<double>& frequencies);
241 
242  unsigned short getMethod() const { return sFreq_.getMethod();}
243 
244  void setNamespace(const std::string& nameSpace);
245 
246 protected:
247  void fireParameterChanged(const ParameterList& parameters);
248 
249 private:
250  void updateFreq_();
251 };
252 
262 {
263 private:
265  std::vector<double> rateFreqs_;
266 
267 public:
268  MarkovModulatedFrequenciesSet(FrequenciesSet* freqSet, const std::vector<double>& rateFreqs);
269 
272  freqSet_(mmfs.freqSet_->clone()),
273  rateFreqs_(mmfs.rateFreqs_)
274  {}
275 
277  {
279  freqSet_ = mmfs.freqSet_->clone();
280  rateFreqs_ = mmfs.rateFreqs_;
281  return *this;
282  }
283 
285 
287 
288 public:
289  void setFrequencies(const std::vector<double>& frequencies)
290  {
291  // Just forward this method to the sequence state frequencies set. This may change in the future...
292  freqSet_->setFrequencies(frequencies);
293  }
294 
295  void fireParameterChanged(const ParameterList& pl)
296  {
297  freqSet_->matchParametersValues(pl);
298  setFrequencies_(VectorTools::kroneckerMult(rateFreqs_, freqSet_->getFrequencies()));
299  }
300 
301  const FrequenciesSet& getStatesFrequenciesSet() const { return *freqSet_; }
302 
303 };
304 
305 
313 {
314 public:
315 
324  FixedFrequenciesSet(StateMap* stateMap, const std::vector<double>& initFreqs, const std::string& name = "Fixed") throw (Exception);
325 
332  FixedFrequenciesSet(StateMap* stateMap, const std::string& name = "Fixed");
333 
334  FixedFrequenciesSet* clone() const { return new FixedFrequenciesSet(*this); }
335 
336 public:
337  void setFrequencies(const std::vector<double>& frequencies);
338 
339 protected:
340  void fireParameterChanged(const ParameterList& parameters) {}
341 };
342 
343 
344 } // end of namespace bpp.
345 
346 #endif // _FREQUENCIESSET_H_
347 
348 
FullFrequenciesSet * clone() const
MarkovModulatedFrequenciesSet(FrequenciesSet *freqSet, const std::vector< double > &rateFreqs)
FrequenciesSet useful for homogeneous and stationary models.
FixedFrequenciesSet(StateMap *stateMap, const std::vector< double > &initFreqs, const std::string &name="Fixed")
Construction with user-defined frequencies on the states of the model.
virtual const Alphabet * getAlphabet() const =0
const double & getFreq_(size_t i) const
unsigned short getMethod() const
virtual void setFrequencies(const std::vector< double > &frequencies)=0
Set the parameters in order to match a given set of frequencies.
void setFrequencies(const std::vector< double > &frequencies)
Set the parameters in order to match a given set of frequencies.
void setFrequencies_(const std::vector< double > &frequencies)
void setFrequencies(const std::vector< double > &frequencies)
Set the parameters in order to match a given set of frequencies.
AbstractFrequenciesSet(StateMap *stateMap, const std::string &prefix, const std::string &name)
virtual const std::vector< double > getFrequencies() const =0
static IntervalConstraint FREQUENCE_CONSTRAINT_MILLI
void setNamespace(const std::string &nameSpace)
FullFrequenciesSet(StateMap *stateMap, bool allowNullFreqs=false, unsigned short method=1, const std::string &name="Full.")
Construction with uniform frequencies on the states of the alphabet.
FrequenciesSet * clone() const =0
MarkovModulatedFrequenciesSet & operator=(const MarkovModulatedFrequenciesSet &mmfs)
Parametrize a set of state frequencies.
const std::map< int, double > getAlphabetStatesFrequencies() const
FrequenciesSet to be used with a Markov-modulated substitution model.
static IntervalConstraint FREQUENCE_CONSTRAINT_SMALL
virtual size_t getNumberOfFrequencies() const =0
AbstractFrequenciesSet(const AbstractFrequenciesSet &af)
A generic FrequenciesSet allowing for the estimation of all frequencies.
virtual std::string getName() const =0
void fireParameterChanged(const ParameterList &pl)
const Alphabet * getAlphabet() const
FixedFrequenciesSet * clone() const
MarkovModulatedFrequenciesSet(const MarkovModulatedFrequenciesSet &mmfs)
size_t getNumberOfFrequencies() const
std::vector< double > & getFrequencies_()
const StateMap & getStateMap() const
double & getFreq_(size_t i)
MarkovModulatedFrequenciesSet * clone() const
virtual const std::map< int, double > getAlphabetStatesFrequencies() const =0
Simplex sFreq_
Simplex to handle the probabilities and the parameters.
std::auto_ptr< StateMap > stateMap_
std::vector< double > rateFreqs_
void setFrequencies(const std::vector< double > &frequencies)
Set the parameters in order to match a given set of frequencies.
void setFrequenciesFromAlphabetStatesFrequencies(const std::map< int, double > &frequencies)
Set the Frequencies from the one of the map which keys match with a letter of the Alphabet...
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...
std::vector< double > freq_
void fireParameterChanged(const ParameterList &parameters)
const std::vector< double > getFrequencies() const
virtual const StateMap & getStateMap() const =0
Map the states of a given alphabet which have a model state.
Definition: StateMap.h:58
std::string getName() const
AbstractFrequenciesSet & operator=(const AbstractFrequenciesSet &af)
const FrequenciesSet & getStatesFrequenciesSet() const
void fireParameterChanged(const ParameterList &parameters)
Basic implementation of the FrequenciesSet interface.
AbstractFrequenciesSet * clone() const =0