bpp-phyl3  3.0.0
FrequencySet.cpp
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: The Bio++ Development Group
2 //
3 // SPDX-License-Identifier: CECILL-2.1
4 
7 
8 #include "FrequencySet.h"
9 
10 // From bpp-phyl
11 #include "../SubstitutionModel.h"
12 #include "../AbstractBiblioSubstitutionModel.h"
13 
14 
15 using namespace bpp;
16 
17 #include <cmath>
18 using namespace std;
19 
20 std::shared_ptr<IntervalConstraint> FrequencySetInterface::FREQUENCE_CONSTRAINT_MILLI(new IntervalConstraint(NumConstants::MILLI(), 1 - NumConstants::MILLI(), false, false));
21 std::shared_ptr<IntervalConstraint> FrequencySetInterface::FREQUENCE_CONSTRAINT_CENTI(new IntervalConstraint(NumConstants::CENTI(), 1 - NumConstants::CENTI(), false, false));
22 std::shared_ptr<IntervalConstraint> FrequencySetInterface::FREQUENCE_CONSTRAINT_SMALL(new IntervalConstraint(NumConstants::SMALL(), 1 - NumConstants::SMALL(), false, false));
23 
24 // ///////////////////////////////////////
25 // AbstractFrequencySet
26 
27 
29 {
30  size_t s = stateMap_->getNumberOfModelStates();
31  vector<double> freq(s);
32  double x = 0;
33  for (size_t i = 0; i < s; ++i)
34  {
35  map<int, double>::const_iterator it = frequencies.find(stateMap_->getAlphabetStateAsInt(i));
36  if (it != frequencies.end())
37  freq[i] = it->second;
38  else
39  freq[i] = 0;
40  x += freq[i];
41  }
42 
43  if (x != 0)
44  for (size_t i = 0; i < s; ++i)
45  {
46  freq[i] /= x;
47  }
48  else
49  for (size_t i = 0; i < s; ++i)
50  {
51  freq[i] = 1.0 / (double)s;
52  }
53 
54  setFrequencies(freq);
55 }
56 
57 const std::map<int, double> AbstractFrequencySet::getAlphabetStatesFrequencies() const
58 {
59  map<int, double> fmap;
60  for (size_t i = 0; i < stateMap_->getNumberOfModelStates(); ++i)
61  {
62  fmap[stateMap_->getAlphabetStateAsInt(i)] += freq_[i];
63  }
64  return fmap;
65 }
66 
67 // ////////////////////////////
68 // FullFrequencySet
69 
71  std::shared_ptr<const StateMapInterface> stateMap,
72  bool allowNullFreqs,
73  unsigned short method,
74  const string& name) :
76  stateMap,
77  "Full.",
78  name),
79  sFreq_(stateMap->getNumberOfModelStates(), method, allowNullFreqs, "Full.")
80 {
81  vector<double> vd;
82  double r = 1. / static_cast<double>(stateMap->getNumberOfModelStates());
83 
84  for (size_t i = 0; i < stateMap->getNumberOfModelStates(); i++)
85  {
86  vd.push_back(r);
87  }
88 
91  updateFreq_();
92 }
93 
95  std::shared_ptr<const StateMapInterface> stateMap,
96  const vector<double>& initFreqs,
97  bool allowNullFreqs,
98  unsigned short method,
99  const string& name) :
101  stateMap,
102  "Full.",
103  name),
104  sFreq_(stateMap->getNumberOfModelStates(), method, allowNullFreqs, "Full.")
105 {
106  sFreq_.setFrequencies(initFreqs);
108  updateFreq_();
109 }
110 
111 void FullFrequencySet::setFrequencies(const vector<double>& frequencies)
112 {
113  sFreq_.setFrequencies(frequencies);
115 
116  updateFreq_();
117 }
118 
119 void FullFrequencySet::setNamespace(const std::string& nameSpace)
120 {
121  sFreq_.setNamespace(nameSpace);
123 }
124 
126 {
127  sFreq_.matchParametersValues(parameters);
128  updateFreq_();
129 }
130 
132 {
133  for (size_t i = 0; i < getAlphabet()->getSize(); i++)
134  {
135  getFreq_(i) = sFreq_.prob(i);
136  }
137 }
138 
139 // ///////////////////////////////////////////
140 // / FixedFrequencySet
141 
143  std::shared_ptr<const StateMapInterface> stateMap,
144  const vector<double>& initFreqs,
145  const string& name) :
147  stateMap,
148  "Fixed.",
149  name)
150 {
151  if (stateMap->getNumberOfModelStates() != initFreqs.size())
152  throw Exception("FixedFrequencySet::constructor. size of init vector does not match the number of states in the model.");
153  setFrequencies(initFreqs);
154 }
155 
157  std::shared_ptr<const StateMapInterface> stateMap,
158  const string& name) :
160  stateMap,
161  "Fixed.",
162  name)
163 {
164  size_t n = stateMap->getNumberOfModelStates();
165  setFrequencies_(std::vector<double>(n, 1. / (double)n));
166 }
167 
168 void FixedFrequencySet::setFrequencies(const vector<double>& frequencies)
169 {
170  if (frequencies.size() != getNumberOfFrequencies())
171  throw DimensionException("FixedFrequencySet::setFrequencies", frequencies.size(), getNumberOfFrequencies());
172  double sum = 0.0;
173  for (size_t i = 0; i < frequencies.size(); i++)
174  {
175  sum += frequencies[i];
176  }
177  if (fabs(1. - sum) > 0.00001)
178  throw Exception("FixedFrequencySet::setFrequencies. Frequencies sum must equal 1 (sum = " + TextTools::toString(sum) + ").");
179  setFrequencies_(frequencies);
180 }
181 
183  unique_ptr<FrequencySetInterface> freqSet,
184  const std::vector<double>& rateFreqs) :
186  make_shared<MarkovModulatedStateMap>(freqSet->stateMap(), static_cast<unsigned int>(rateFreqs.size())),
187  "MarkovModulated.",
188  "MarkovModulated." + freqSet->getName()),
189  freqSet_(std::move(freqSet)),
190  rateFreqs_(rateFreqs)
191 {
192  freqSet_->setNamespace(std::string("MarkovModulated.") + freqSet_->getNamespace());
193  addParameters_(freqSet_->getParameters());
194  setFrequencies_(VectorTools::kroneckerMult(rateFreqs, freqSet_->getFrequencies()));
195 }
196 
199 
200 
202  AbstractFrequencySet(fmfs),
203  model_(fmfs.model_->clone())
204 {}
205 
207 {
209  model_.reset(fmfs.model_->clone());
210  return *this;
211 }
212 
214 
216  std::shared_ptr<TransitionModelInterface> model) :
217  AbstractFrequencySet(model->getStateMap(), "FromModel." + (model ? model->getNamespace() : ""), "FromModel"),
218  model_(model)
219 {
220  model_->setNamespace(getNamespace());
221  addParameters_(model_->getParameters());
222  setFrequencies_(model_->getFrequencies());
223 }
224 
225 
226 void FromModelFrequencySet::setNamespace(const std::string& name)
227 {
229  model_->setNamespace(name);
230 }
231 
232 
233 void FromModelFrequencySet::setFrequencies(const std::vector<double>& frequencies)
234 {
235  std::map<int, double> freq;
236  for (size_t i = 0; i < getNumberOfFrequencies(); ++i)
237  {
238  freq[stateMap().getAlphabetStateAsInt(i)] += frequencies[i];
239  }
240  model_->setFreq(freq);
241  matchParametersValues(model_->getParameters());
242 }
243 
245 {
246  model_->matchParametersValues(pl);
247  setFrequencies_(model_->getFrequencies());
248 }
249 
250 
253 
255  std::shared_ptr<const StateMapInterface> stateMap,
256  const std::string& path,
257  size_t nCol) :
259  stateMap,
260  "Empirical.",
261  "Empirical"),
262  path_(path),
263  nCol_(nCol)
264 {
265  readFromFile_();
266 }
267 
269  AbstractFrequencySet(fmfs),
270  path_(fmfs.path_),
271  nCol_(fmfs.nCol_)
272 {}
273 
275 {
277  path_ = fmfs.path_;
278  nCol_ = fmfs.nCol_;
279  return *this;
280 }
281 
283 {
284  if (!FileTools::fileExists(path_.c_str()))
285  throw Exception("UserFrequencySet::readFromFile. Frequencies file not found : " + path_);
286 
287  ifstream in(path_.c_str(), ios::in);
288 
289  // Read profile:
290 
291  for (unsigned int i = 0; i < getAlphabet()->getSize(); i++)
292  {
293  if (!in)
294  throw Exception("UserFrequencySet::readFromFile. Missing frequencies in file : " + path_);
295 
296  string line = FileTools::getNextLine(in);
297  StringTokenizer st(line);
298  double s(0);
299  for (unsigned int j = 0; j < nCol_; j++)
300  {
301  if (!st.hasMoreToken())
302  throw Exception("UserFrequencySet::readFromFile. Missing frequencies for column " + TextTools::toString(nCol_) + " in line " + TextTools::toString(i));
303  s = TextTools::toDouble(st.nextToken());
304  }
305  getFreq_(i) = s;
306  }
307 
308  double sf = VectorTools::sum(getFrequencies_());
309  if (fabs(sf - 1) > 0.000001)
310  {
311  ApplicationTools::displayMessage("WARNING!!! Frequencies sum to " + TextTools::toString(sf) + ", frequencies have been scaled.");
312  sf *= 1. / sf;
313  }
314 
315  // Closing stream:
316  in.close();
317 }
318 
319 void UserFrequencySet::setFrequencies(const vector<double>& frequencies)
320 {
321  if (frequencies.size() != getNumberOfFrequencies())
322  throw DimensionException("UserFrequencySet::setFrequencies", frequencies.size(), getNumberOfFrequencies());
323  double sum = 0.0;
324  for (size_t i = 0; i < frequencies.size(); i++)
325  {
326  sum += frequencies[i];
327  }
328  if (fabs(1. - sum) > 0.00001)
329  throw Exception("FixedFrequencySet::setFrequencies. Frequencies sum must equal 1 (sum = " + TextTools::toString(sum) + ").");
330  setFrequencies_(frequencies);
331 }
Basic implementation of the FrequencySet interface.
Definition: FrequencySet.h:102
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
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
double & getFreq_(size_t i)
Definition: FrequencySet.h:179
std::vector< double > & getFrequencies_()
Definition: FrequencySet.h:178
AbstractFrequencySet & operator=(const AbstractFrequencySet &af)
Definition: FrequencySet.h:129
const StateMapInterface & stateMap() const override
Definition: FrequencySet.h:144
void addParameters_(const ParameterList &parameters)
void setNamespace(const std::string &prefix)
void setParametersValues(const ParameterList &parameters) override
bool matchParametersValues(const ParameterList &parameters) override
std::string getNamespace() const override
const ParameterList & getParameters() const override
static void displayMessage(const std::string &text)
static std::string getNextLine(std::istream &in)
static bool fileExists(const std::string &filename)
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.
static std::shared_ptr< IntervalConstraint > FREQUENCE_CONSTRAINT_SMALL
Definition: FrequencySet.h:90
static std::shared_ptr< IntervalConstraint > FREQUENCE_CONSTRAINT_MILLI
Definition: FrequencySet.h:91
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
void fireParameterChanged(const ParameterList &pl) override
FromModelFrequencySet & operator=(const FromModelFrequencySet &fmfs)
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)
void fireParameterChanged(const ParameterList &parameters) override
Simplex sFreq_
Simplex to handle the probabilities and the parameters.
Definition: FrequencySet.h:203
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
void setFrequencies(const std::vector< double > &frequencies) override
Set the parameters in order to match a given set of frequencies.
MarkovModulatedFrequencySet(std::unique_ptr< FrequencySetInterface > freqSet, const std::vector< double > &rateFreqs)
std::unique_ptr< FrequencySetInterface > freqSet_
Definition: FrequencySet.h:297
This class implements a state map for Markov modulated models.
Definition: StateMap.h:192
static double MILLI()
static double CENTI()
static double SMALL()
double prob(size_t i) const
void setFrequencies(const std::vector< double > &)
virtual size_t getNumberOfModelStates() const =0
virtual int getAlphabetStateAsInt(size_t index) const =0
const std::string & nextToken()
bool hasMoreToken() const
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.
UserFrequencySet & operator=(const UserFrequencySet &fmfs)
UserFrequencySet(std::shared_ptr< const StateMapInterface > stateMap, const std::string &path, size_t nCol=1)
User.
static T sum(const std::vector< T > &v1)
static std::vector< T > kroneckerMult(const std::vector< T > &v1, const std::vector< T > &v2)
double toDouble(const std::string &s, char dec='.', char scientificNotation='e')
std::string toString(T t)
Defines the basic types of data flow nodes.