bpp-phyl  2.4.0
SubstitutionModelSet.h
Go to the documentation of this file.
1 //
2 // File: SubstitutionModelSet.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 _SUBSTITUTIONMODELSET_H_
42 #define _SUBSTITUTIONMODELSET_H_
43 
44 
45 #include "../Tree.h"
46 #include "SubstitutionModel.h"
49 
50 #include <Bpp/Exceptions.h>
53 
54 // From Seqlib:
57 
58 // From the STL:
59 #include <vector>
60 #include <map>
61 #include <algorithm>
62 #include <memory>
63 #include <typeinfo>
64 
65 namespace bpp
66 {
107 {
108 protected:
113 
114  size_t nbStates_;
115 
119  std::vector<TransitionModel*> modelSet_;
120 
121 private:
125  std::unique_ptr<FrequenciesSet> rootFrequencies_;
126 
130  mutable std::map<int, size_t> nodeToModel_;
131  mutable std::map<size_t, std::vector<int> > modelToNodes_;
132 
140  std::vector<ParameterList> modelParameters_;
141 
143 
144 public:
153  alphabet_(alpha),
154  nbStates_(0),
155  modelSet_(),
156  rootFrequencies_(),
157  nodeToModel_(),
158  modelToNodes_(),
159  modelParameters_(),
160  stationarity_(true)
161  {
162  }
163 
171  SubstitutionModelSet(const Alphabet* alpha, FrequenciesSet* rootFreqs):
173  alphabet_(alpha),
174  nbStates_(0),
175  modelSet_(),
176  rootFrequencies_(),
177  nodeToModel_(),
178  modelToNodes_(),
179  modelParameters_(),
180  stationarity_(true)
181  {
182  setRootFrequencies(rootFreqs);
183  }
184 
189  void clear();
190 
191  bool isStationary() const
192  {
193  return stationarity_;
194  }
195 
201  void setRootFrequencies(FrequenciesSet* rootFreqs);
202 
204 
206 
208  {
209  for (auto& model : modelSet_)
210  {
211  delete model;
212  }
213  }
214 
215  SubstitutionModelSet* clone() const { return new SubstitutionModelSet(*this); }
216 
217 public:
224  size_t getNumberOfStates() const
225  {
226  return nbStates_;
227  }
228 
234  virtual void fireParameterChanged(const ParameterList& parameters);
235 
239  size_t getNumberOfModels() const { return modelSet_.size(); }
240 
245  bool hasMixedSubstitutionModel() const;
246 
253  const TransitionModel* getModel(size_t i) const
254  {
255  if (i >= modelSet_.size()) throw IndexOutOfBoundsException("SubstitutionModelSet::getModel(i).", 0, modelSet_.size() - 1, i);
256  return modelSet_[i];
257  }
258 
260  {
261  if (i >= modelSet_.size()) throw IndexOutOfBoundsException("SubstitutionModelSet::getModel(i).", 0, modelSet_.size() - 1, i);
262  return modelSet_[i];
263  }
264 
269  {
270  try
271  {
272  return dynamic_cast<const SubstitutionModel*>(getModel(i));
273  }
274  catch (std::bad_cast& bc)
275  {
276  throw Exception("SubstitutionModelSet::getSubstitutionModel : model is not a sustitution model " + getModel(i)->getName());
277  }
278  }
279 
280 
282  {
283  try
284  {
285  return dynamic_cast<SubstitutionModel*>(getModel(i));
286  }
287  catch (std::bad_cast& bc)
288  {
289  throw Exception("SubstitutionModelSet::getSubstitutionModel : model is not a sustitution model " + getModel(i)->getName());
290  }
291  }
292 
293  /*
294  * @brief check if has only markovian substitution models
295  *
296  */
297 
299  {
300  for (const auto& mod : modelSet_)
301  if (dynamic_cast<const SubstitutionModel*>(mod)==0)
302  return false;
303 
304  return true;
305  }
306 
314  size_t getModelIndexForNode(int nodeId) const
315  {
316  std::map<int, size_t>::iterator i = nodeToModel_.find(nodeId);
317  if (i == nodeToModel_.end())
318  throw Exception("SubstitutionModelSet::getModelIndexForNode(). No model associated to node with id " + TextTools::toString(nodeId));
319  return i->second;
320  }
321 
329  const TransitionModel* getModelForNode(int nodeId) const
330  {
331  std::map<int, size_t>::const_iterator i = nodeToModel_.find(nodeId);
332  if (i == nodeToModel_.end())
333  throw Exception("SubstitutionModelSet::getModelForNode(). No model associated to node with id " + TextTools::toString(nodeId));
334  return modelSet_[i->second];
335  }
337  {
338  std::map<int, size_t>::iterator i = nodeToModel_.find(nodeId);
339  if (i == nodeToModel_.end())
340  throw Exception("SubstitutionModelSet::getModelForNode(). No model associated to node with id " + TextTools::toString(nodeId));
341  return modelSet_[i->second];
342  }
343 
345  {
346  return dynamic_cast<const SubstitutionModel*>(getModelForNode(nodeId));
347  }
348 
350  {
351  return dynamic_cast<SubstitutionModel*>(getModelForNode(nodeId));
352  }
353 
361  const std::vector<int>& getNodesWithModel(size_t i) const
362  {
363  if (i >= modelSet_.size()) throw IndexOutOfBoundsException("SubstitutionModelSet::getNodesWithModel().", i, 0, modelSet_.size());
364  return modelToNodes_[i];
365  }
366 
373  std::vector<int> getNodesWithParameter(const std::string& name) const;
374 
391  void addModel(TransitionModel* model, const std::vector<int>& nodesId);//, const std::vector<std::string>& newParams);
392 
403  void replaceModel(size_t modelIndex, TransitionModel* model);
404 
405  void listModelNames(std::ostream& out = std::cout) const;
406 
410  const FrequenciesSet* getRootFrequenciesSet() const { return rootFrequencies_.get(); }
411 
415  std::vector<double> getRootFrequencies() const
416  {
417  if (stationarity_)
418  return modelSet_[0]->getFrequencies();
419  else
420  return rootFrequencies_->getFrequencies();
421  }
422 
429  {
430  if (stationarity_)
431  return ParameterList();
432  else
433  return rootFrequencies_->getParameters();
434  }
435 
444  {
445  ParameterList pl;
446  for (size_t i = stationarity_ ? 0 : rootFrequencies_->getNumberOfParameters();
447  i < getNumberOfParameters(); i++)
448  {
450  }
451  return pl;
452  }
453 
462  ParameterList getModelParameters(size_t modelIndex) const;
463 
464  const Alphabet* getAlphabet() const { return alphabet_; }
465 
471  virtual const std::vector<int>& getAlphabetStates() const {
472  return getModel(0)->getAlphabetStates();
473  }
474 
475  virtual const StateMap& getStateMap() const {
476  return getModel(0)->getStateMap();
477  }
478 
479  virtual std::vector<size_t> getModelStates(int code) const {
480  return getModel(0)->getModelStates(code);
481  }
482 
483  virtual std::vector<size_t> getModelStates(const std::string& code) const {
484  return getModel(0)->getModelStates(code);
485  }
486 
491  virtual int getAlphabetStateAsInt(size_t index) const {
492  return getModel(0)->getAlphabetStateAsInt(index);
493  }
494 
499  virtual std::string getAlphabetStateAsChar(size_t index) const {
500  return getModel(0)->getAlphabetStateAsChar(index);
501  }
502 
515  bool isFullySetUpFor(const Tree& tree, bool throwEx = true) const
516  {
517  return checkOrphanModels(throwEx)
518  // && checkOrphanParameters(throwEx)
519  && checkOrphanNodes(tree, throwEx)
520  && checkUnknownNodes(tree, throwEx);
521  }
522 
523 protected:
528  {
529  if (!stationarity_)
530  rootFrequencies_->matchParametersValues(getParameters());
531  }
532 
538  bool checkOrphanModels(bool throwEx) const;
539 
540  bool checkOrphanNodes(const Tree& tree, bool throwEx) const;
541 
542  bool checkUnknownNodes(const Tree& tree, bool throwEx) const;
544 };
545 } // end of namespace bpp.
546 
547 #endif // _SUBSTITUTIONMODELSET_H_
548 
SubstitutionModel * getSubstitutionModel(size_t i)
std::map< size_t, std::vector< int > > modelToNodes_
SubstitutionModelSet * clone() const
Substitution models manager for non-homogeneous / non-reversible models of evolution.
Interface for all substitution models.
SubstitutionModelSet(const Alphabet *alpha, FrequenciesSet *rootFreqs)
Create a model set according to the specified alphabet and root frequencies. Stationarity is not assu...
size_t getNumberOfStates() const
Get the number of states associated to this model set.
virtual std::string getAlphabetStateAsChar(size_t index) const
void setRootFrequencies(FrequenciesSet *rootFreqs)
Sets a given FrequenciesSet for root frequencies.
const TransitionModel * getModelForNode(int nodeId) const
Get the model associated to a particular node id.
void listModelNames(std::ostream &out=std::cout) const
virtual int getAlphabetStateAsInt(size_t index) const
const FrequenciesSet * getRootFrequenciesSet() const
Interface for phylogenetic tree objects.
Definition: Tree.h:148
const TransitionModel * getModel(size_t i) const
Get one model from the set knowing its index.
ParameterList getRootFrequenciesParameters() const
Get the parameters corresponding to the root frequencies.
virtual std::string getAlphabetStateAsChar(size_t index) const =0
SubstitutionModel * getSubstitutionModelForNode(int nodeId)
std::map< int, size_t > nodeToModel_
Contains for each node in a tree the index of the corresponding model in modelSet_.
virtual std::vector< size_t > getModelStates(int code) const =0
Get the state in the model corresponding to a particular state in the alphabet.
bool isFullySetUpFor(const Tree &tree, bool throwEx=true) const
Check if the model set is fully specified for a given tree.
const Alphabet * getAlphabet() const
std::vector< ParameterList > modelParameters_
Parameters for each model in the set.
ParameterList getNodeParameters() const
Get the parameters corresponding attached to the nodes of the tree.
size_t getNumberOfParameters() const
void replaceModel(size_t modelIndex, TransitionModel *model)
Replace a model in the set, and all corresponding parameters. The replaced model deleted.
void clear()
Resets all the information contained in this object.
bool checkOrphanNodes(const Tree &tree, bool throwEx) const
virtual const StateMap & getStateMap() const
virtual const std::vector< int > & getAlphabetStates() const =0
virtual std::vector< size_t > getModelStates(int code) const
virtual int getAlphabetStateAsInt(size_t index) const =0
std::unique_ptr< FrequenciesSet > rootFrequencies_
Root frequencies.
bool checkUnknownNodes(const Tree &tree, bool throwEx) const
virtual void addParameter(const Parameter &param)
const SubstitutionModel * getSubstitutionModel(size_t i) const
Return a markovian substitution model (or null)
TransitionModel * getModel(size_t i)
virtual void fireParameterChanged(const ParameterList &parameters)
virtual const StateMap & getStateMap() const =0
const SubstitutionModel * getSubstitutionModelForNode(int nodeId) const
std::vector< double > getRootFrequencies() const
ParameterList getModelParameters(size_t modelIndex) const
Get the parameters attached to a Model.
const ParameterList & getParameters() const
Parameter & getParameter_(const std::string &name)
void addModel(TransitionModel *model, const std::vector< int > &nodesId)
Add a new model to the set, and set relationships with nodes and params.
virtual const std::vector< int > & getAlphabetStates() const
std::string toString(T t)
TransitionModel * getModelForNode(int nodeId)
SubstitutionModelSet & operator=(const SubstitutionModelSet &set)
SubstitutionModelSet(const Alphabet *alpha)
Create a model set according to the specified alphabet. Stationarity is assumed.
Map the states of a given alphabet which have a model state.
Definition: StateMap.h:58
size_t getModelIndexForNode(int nodeId) const
Get the index in the set of the model associated to a particular node id.
std::vector< int > getNodesWithParameter(const std::string &name) const
virtual std::vector< size_t > getModelStates(const std::string &code) const
bool checkOrphanModels(bool throwEx) const
const Alphabet * alphabet_
A pointer toward the common alphabet to all models in the set.
std::vector< TransitionModel * > modelSet_
Contains all models used in this tree.
const std::vector< int > & getNodesWithModel(size_t i) const
Get a list of nodes id for which the given model is associated.