bpp-phyl  2.2.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>
51 #include <Bpp/Numeric/Random/RandomTools.h>
52 #include <Bpp/Numeric/VectorTools.h>
53 
54 // From Seqlib:
55 #include <Bpp/Seq/Alphabet/Alphabet.h>
56 #include <Bpp/Seq/Alphabet/NucleicAlphabet.h>
57 
58 // From the STL:
59 #include <vector>
60 #include <map>
61 #include <algorithm>
62 #include <memory>
63 
64 namespace bpp
65 {
105  public AbstractParameterAliasable
106 {
107 protected:
111  const Alphabet* alphabet_;
112 
113  size_t nbStates_;
114 
118  std::vector<SubstitutionModel*> modelSet_;
119 
120 private:
124  std::auto_ptr<FrequenciesSet> rootFrequencies_;
125 
129  mutable std::map<int, size_t> nodeToModel_;
130  mutable std::map<size_t, std::vector<int> > modelToNodes_;
131 
139  std::vector<ParameterList> modelParameters_;
140 
142 
143 public:
150  SubstitutionModelSet(const Alphabet* alpha):
151  AbstractParameterAliasable(""),
152  alphabet_(alpha),
153  nbStates_(0),
154  modelSet_(),
155  rootFrequencies_(0),
156  nodeToModel_(),
157  modelToNodes_(),
159  stationarity_(true)
160  {
161  }
162 
170  SubstitutionModelSet(const Alphabet* alpha, FrequenciesSet* rootFreqs):
171  AbstractParameterAliasable(""),
172  alphabet_(alpha),
173  nbStates_(0),
174  modelSet_(),
175  rootFrequencies_(0),
176  nodeToModel_(),
177  modelToNodes_(),
179  stationarity_(true)
180  {
181  setRootFrequencies(rootFreqs);
182  }
183 
189  void clear();
190 
197  void setRootFrequencies(FrequenciesSet* rootFreqs);
198 
200 
202 
204  {
205  for (size_t i = 0; i < modelSet_.size(); i++) { delete modelSet_[i]; }
206  }
207 
208 #ifndef NO_VIRTUAL_COV
210 #else
211  Clonable*
212 #endif
213  clone() const { return new SubstitutionModelSet(*this); }
214 
215 public:
222  size_t getNumberOfStates() const throw (Exception)
223  {
224  return nbStates_;
225  }
226 
232  virtual void fireParameterChanged(const ParameterList& parameters);
233 
237  size_t getNumberOfModels() const { return modelSet_.size(); }
238 
243  bool hasMixedSubstitutionModel() const;
244 
251  const SubstitutionModel* getModel(size_t i) const throw (IndexOutOfBoundsException)
252  {
253  if (i > modelSet_.size()) throw IndexOutOfBoundsException("SubstitutionModelSet::getNumberOfModels().", 0, modelSet_.size() - 1, i);
254  return modelSet_[i];
255  }
256 
257  SubstitutionModel* getModel(size_t i) throw (IndexOutOfBoundsException)
258  {
259  if (i > modelSet_.size()) throw IndexOutOfBoundsException("SubstitutionModelSet::getNumberOfModels().", 0, modelSet_.size() - 1, i);
260  return modelSet_[i];
261  }
262 
270  size_t getModelIndexForNode(int nodeId) const throw (Exception)
271  {
272  std::map<int, size_t>::iterator i = nodeToModel_.find(nodeId);
273  if (i == nodeToModel_.end())
274  throw Exception("SubstitutionModelSet::getModelIndexForNode(). No model associated to node with id " + TextTools::toString(nodeId));
275  return i->second;
276  }
277 
285  const SubstitutionModel* getModelForNode(int nodeId) const throw (Exception)
286  {
287  std::map<int, size_t>::const_iterator i = nodeToModel_.find(nodeId);
288  if (i == nodeToModel_.end())
289  throw Exception("SubstitutionModelSet::getModelForNode(). No model associated to node with id " + TextTools::toString(nodeId));
290  return modelSet_[i->second];
291  }
292  SubstitutionModel* getModelForNode(int nodeId) throw (Exception)
293  {
294  std::map<int, size_t>::iterator i = nodeToModel_.find(nodeId);
295  if (i == nodeToModel_.end())
296  throw Exception("SubstitutionModelSet::getModelForNode(). No model associated to node with id " + TextTools::toString(nodeId));
297  return modelSet_[i->second];
298  }
299 
307  const std::vector<int>& getNodesWithModel(size_t i) const throw (IndexOutOfBoundsException)
308  {
309  if (i >= modelSet_.size()) throw IndexOutOfBoundsException("SubstitutionModelSet::getNodesWithModel().", i, 0, modelSet_.size());
310  return modelToNodes_[i];
311  }
312 
319  std::vector<int> getNodesWithParameter(const std::string& name) const throw (ParameterNotFoundException);
320 
337  void addModel(SubstitutionModel* model, const std::vector<int>& nodesId);//, const std::vector<std::string>& newParams) throw (Exception);
338 
349  void replaceModel(size_t modelIndex, SubstitutionModel* model) throw (Exception);
350 
351  void listModelNames(std::ostream& out = std::cout) const;
352 
356  const FrequenciesSet* getRootFrequenciesSet() const { return rootFrequencies_.get(); }
357 
361  std::vector<double> getRootFrequencies() const
362  {
363  if (stationarity_)
364  return modelSet_[0]->getFrequencies();
365  else
366  return rootFrequencies_->getFrequencies();
367  }
368 
374  ParameterList getRootFrequenciesParameters() const
375  {
376  if (stationarity_)
377  return ParameterList();
378  else
379  return rootFrequencies_->getParameters();
380  }
381 
389  ParameterList getNodeParameters() const
390  {
391  ParameterList pl;
392  for (size_t i = stationarity_ ? 0 : rootFrequencies_->getNumberOfParameters();
393  i < getNumberOfParameters(); i++)
394  {
395  pl.addParameter(getParameter_(i));
396  }
397  return pl;
398  }
399 
408  ParameterList getModelParameters(size_t modelIndex) const;
409 
410  const Alphabet* getAlphabet() const { return alphabet_; }
411 
417  virtual const std::vector<int>& getAlphabetStates() const {
418  return getModel(0)->getAlphabetStates();
419  }
420 
421  virtual const StateMap& getStateMap() const {
422  return getModel(0)->getStateMap();
423  }
424 
425  virtual std::vector<size_t> getModelStates(int code) const {
426  return getModel(0)->getModelStates(code);
427  }
428 
429  virtual std::vector<size_t> getModelStates(const std::string& code) const {
430  return getModel(0)->getModelStates(code);
431  }
432 
437  virtual int getAlphabetStateAsInt(size_t index) const {
438  return getModel(0)->getAlphabetStateAsInt(index);
439  }
440 
445  virtual std::string getAlphabetStateAsChar(size_t index) const {
446  return getModel(0)->getAlphabetStateAsChar(index);
447  }
448 
461  bool isFullySetUpFor(const Tree& tree, bool throwEx = true) const throw (Exception)
462  {
463  return checkOrphanModels(throwEx)
464  // && checkOrphanParameters(throwEx)
465  && checkOrphanNodes(tree, throwEx)
466  && checkUnknownNodes(tree, throwEx);
467  }
468 
469 protected:
474  {
475  if (!stationarity_)
476  rootFrequencies_->matchParametersValues(getParameters());
477  }
478 
484  bool checkOrphanModels(bool throwEx) const throw (Exception);
485 
486  bool checkOrphanNodes(const Tree& tree, bool throwEx) const throw (Exception);
487 
488  bool checkUnknownNodes(const Tree& tree, bool throwEx) const throw (Exception);
490 };
491 } // end of namespace bpp.
492 
493 #endif // _SUBSTITUTIONMODELSET_H_
494 
bool isFullySetUpFor(const Tree &tree, bool throwEx=true) const
Check if the model set is fully specified for a given tree.
std::map< size_t, std::vector< int > > modelToNodes_
Substitution models manager for non-homogeneous / non-reversible models of evolution.
Interface for all substitution models.
const SubstitutionModel * getModelForNode(int nodeId) const
Get the model associated to a particular node id.
virtual std::vector< size_t > getModelStates(int code) const =0
Get the state in the model corresponding to a particular state in the alphabet.
ParameterList getRootFrequenciesParameters() const
Get the parameters corresponding to the root frequencies.
virtual int getAlphabetStateAsInt(size_t index) const =0
const SubstitutionModel * getModel(size_t i) const
Get one model from the set knowing its index.
SubstitutionModelSet(const Alphabet *alpha, FrequenciesSet *rootFreqs)
Create a model set according to the specified alphabet and root frequencies. Stationarity is not assu...
virtual const StateMap & getStateMap() const =0
virtual const std::vector< int > & getAlphabetStates() const
virtual const std::vector< int > & getAlphabetStates() const =0
ParameterList getModelParameters(size_t modelIndex) const
Get the parameters attached to a Model.
void setRootFrequencies(FrequenciesSet *rootFreqs)
Sets a given FrequenciesSet for root frequencies.
size_t getModelIndexForNode(int nodeId) const
Get the index in the set of the model associated to a particular node id.
std::vector< double > getRootFrequencies() const
void listModelNames(std::ostream &out=std::cout) const
virtual std::string getAlphabetStateAsChar(size_t index) const
Interface for phylogenetic tree objects.
Definition: Tree.h:148
Parametrize a set of state frequencies.
virtual const StateMap & getStateMap() const
virtual std::vector< size_t > getModelStates(const std::string &code) const
SubstitutionModel * getModelForNode(int nodeId)
void addModel(SubstitutionModel *model, const std::vector< int > &nodesId)
Add a new model to the set, and set relationships with nodes and params.
std::map< int, size_t > nodeToModel_
Contains for each node in a tree the index of the corresponding model in modelSet_.
ParameterList getNodeParameters() const
Get the parameters corresponding attached to the nodes of the tree.
std::vector< ParameterList > modelParameters_
Parameters for each model in the set.
const Alphabet * getAlphabet() const
const FrequenciesSet * getRootFrequenciesSet() const
void clear()
Resets all the information contained in this object.
void replaceModel(size_t modelIndex, SubstitutionModel *model)
Replace a model in the set, and all corresponding parameters. The replaced model deleted.
std::auto_ptr< FrequenciesSet > rootFrequencies_
Root frequencies.
virtual std::string getAlphabetStateAsChar(size_t index) const =0
virtual int getAlphabetStateAsInt(size_t index) const
std::vector< SubstitutionModel * > modelSet_
Contains all models used in this tree.
size_t getNumberOfStates() const
Get the number of states associated to this model set.
bool checkOrphanModels(bool throwEx) const
virtual void fireParameterChanged(const ParameterList &parameters)
const std::vector< int > & getNodesWithModel(size_t i) const
Get a list of nodes id for which the given model is associated.
SubstitutionModel * getModel(size_t i)
SubstitutionModelSet & operator=(const SubstitutionModelSet &set)
SubstitutionModelSet(const Alphabet *alpha)
Create a model set according to the specified alphabet. Stationarity is assumed.
SubstitutionModelSet * clone() const
Map the states of a given alphabet which have a model state.
Definition: StateMap.h:58
std::vector< int > getNodesWithParameter(const std::string &name) const
const Alphabet * alphabet_
A pointer toward the common alphabet to all models in the set.
bool checkOrphanNodes(const Tree &tree, bool throwEx) const
bool checkUnknownNodes(const Tree &tree, bool throwEx) const
virtual std::vector< size_t > getModelStates(int code) const