bpp-phyl3  3.0.0
SubstitutionProcessCollectionMember.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_LIKELIHOOD_SUBSTITUTIONPROCESSCOLLECTIONMEMBER_H
6 #define BPP_PHYL_LIKELIHOOD_SUBSTITUTIONPROCESSCOLLECTIONMEMBER_H
7 
8 
10 
11 namespace bpp
12 {
19 class SubstitutionProcessCollection;
20 
23 {
24 private:
30 
34  size_t nProc_;
35 
36 private:
41  void updateParameters();
42 
43 private:
48  std::map<unsigned int, size_t> nodeToModel_;
49  std::map<size_t, std::vector<unsigned int>> modelToNodes_;
50 
54  size_t nTree_;
55 
59  size_t nDist_;
60 
64  size_t nRoot_;
65 
69  size_t nPath_;
70 
71 private:
72  /*
73  * @brief Constructors are only accessible through a SubstitutionProcessCollection.
74  */
75 
86  SubstitutionProcessCollection* pSubProColl,
87  size_t nProc,
88  size_t nTree,
89  size_t nDist) :
91  pSubProColl_(pSubProColl),
92  nProc_(nProc),
93  nodeToModel_(),
94  modelToNodes_(),
95  nTree_(nTree),
96  nDist_(nDist),
97  nRoot_(0),
98  nPath_(0)
99  {
101  }
102 
109  nProc_(set.nProc_),
112  nTree_(set.nTree_),
113  nDist_(set.nDist_),
114  nRoot_(set.nRoot_),
115  nPath_(set.nPath_)
116  {}
117 
119  {
122  nProc_ = set.nProc_;
123 
126  nTree_ = set.nTree_;
127  nDist_ = set.nDist_;
128  nRoot_ = set.nRoot_;
129  nPath_ = set.nPath_;
130 
131  return *this;
132  }
133 
135 
136  struct Deleter
137  {
139  {
140  delete sm;
141  }
142  };
143 
145 
146 public:
148  {
149  if (!pSubProColl_)
150  throw NullPointerException("SubstitutionProcessCollectionMember::collection(). Member is not associated to any collection.");
151  return *pSubProColl_;
152  }
153 
155  {
156  if (!pSubProColl_)
157  throw NullPointerException("SubstitutionProcessCollectionMember::collection(). Member is not associated to any collection.");
158  return *pSubProColl_;
159  }
160 
161  void clear()
162  {
163  nodeToModel_.clear();
164  modelToNodes_.clear();
165  nRoot_ = 0;
166  }
167 
169  {
170  return pSubProColl_;
171  }
172 
174  {
175  return pSubProColl_;
176  }
177 
178  const StateMapInterface& stateMap() const override
179  {
180  return model(modelToNodes_.begin()->first).stateMap();
181  }
182 
183  std::shared_ptr<const StateMapInterface> getStateMap() const override
184  {
185  return model(modelToNodes_.begin()->first).getStateMap();
186  }
187 
191  size_t getNumberOfProcesses() const
192  {
193  return nProc_;
194  }
195 
199  size_t getNumberOfModels() const override { return modelToNodes_.size(); }
200 
204  bool hasMixedTransitionModel() const;
205 
209  bool isStationary() const
210  {
211  return nRoot_ == 0;
212  }
213 
214  const BranchModelInterface& model(size_t n) const override;
215 
216  std::shared_ptr<const BranchModelInterface> getModel(size_t n) const override;
217 
218  std::shared_ptr<BranchModelInterface> getModel(size_t n);
219 
220  std::vector<size_t> getModelNumbers() const override;
221 
229  size_t getModelNumberForNode(unsigned int nodeId) const override
230  {
231  auto i = nodeToModel_.find(nodeId);
232  if (i == nodeToModel_.end())
233  throw Exception("SubstitutionProcessCollectionMember::getModelNumberForNode(). No model associated to node with id " + TextTools::toString(nodeId));
234  return i->second;
235  }
236 
244  std::shared_ptr<const BranchModelInterface> getModelForNode(unsigned int nodeId) const override
245  {
246  std::map<unsigned int, size_t>::const_iterator i = nodeToModel_.find(nodeId);
247  if (i == nodeToModel_.end())
248  throw Exception("SubstitutionProcessCollectionMember::getModelForNode(). No model associated to node with id " + TextTools::toString(nodeId));
249  return getModel(i->second);
250  }
251 
259  const std::vector<unsigned int> getNodesWithModel(size_t i) const override
260  {
261  const auto it = modelToNodes_.find(i);
262  if (it == modelToNodes_.end())
263  throw Exception("SubstitutionProcessCollectionMember::getNodesWithModel(). No nodes associated with model " + TextTools::toString(i));
264 
265  return it->second;
266  }
267 
275  void addModel(size_t numModel, const std::vector<unsigned int>& nodesId);
276 
280  const DiscreteDistributionInterface& rateDistribution() const override;
281 
283 
284  std::shared_ptr<const DiscreteDistributionInterface> getRateDistribution() const override;
285 
286  std::shared_ptr<DiscreteDistributionInterface> getRateDistribution() override;
287 
288  const size_t getRateDistributionNumber() const { return nDist_; }
289 
295  void setRootFrequencies(size_t numFreq);
296 
297  const size_t getRootFrequenciesNumber() const { return nRoot_; }
298 
299  bool hasRootFrequencySet() const override { return !isStationary(); }
300 
301  const FrequencySetInterface& rootFrequencySet() const override;
302 
304 
305  std::shared_ptr<const FrequencySetInterface> getRootFrequencySet() const override;
306 
307  std::shared_ptr<FrequencySetInterface> getRootFrequencySet() override;
308 
309  /*
310  * @brief Set the Set of Model Path
311  * @param numPath the index of the frequencies in the collection.
312  *
313  * This method checks that the models defined in the process match
314  * those of the model paths.
315  */
316  void setModelScenario(size_t numPath);
317 
318  const size_t getModelScenarioNumber() const { return nPath_; }
319 
320  std::shared_ptr<const ModelScenario> getModelScenario() const override;
321 
322  std::shared_ptr<ModelScenario> getModelScenario();
323 
327  bool matchParametersValues(const ParameterList& parameters) override;
328 
339  bool isFullySetUp(bool throwEx = true) const
340  {
341  return checkOrphanNodes(throwEx)
342  && checkUnknownNodes(throwEx);
343  }
344 
345 protected:
351  bool checkOrphanNodes(bool throwEx) const;
352 
353  bool checkUnknownNodes(bool throwEx) const;
356 public:
360  const std::vector<double>& getRootFrequencies() const override;
361 
365  const ParametrizablePhyloTree& parametrizablePhyloTree() const override;
366 
367  std::shared_ptr<const ParametrizablePhyloTree> getParametrizablePhyloTree() const override;
368 
370 
371  std::shared_ptr<ParametrizablePhyloTree> getParametrizablePhyloTree();
372 
373 public:
374  /*
375  * @brief set a new tree number from the collection
376  *
377  * @param nTree number of the tree
378  * @param check if models assignment on branches is correct
379  *
380  */
381 
382  void setTreeNumber(size_t nTree, bool check = true);
383 
384  size_t getTreeNumber() const { return nTree_; }
385 
386  const BranchModelInterface& model(unsigned int nodeId, size_t classIndex) const override
387  {
388  return model(nodeToModel_.at(nodeId));
389  }
390 
391  std::shared_ptr<const BranchModelInterface> getModel(unsigned int nodeId, size_t classIndex) const override
392  {
393  return getModel(nodeToModel_.at(nodeId));
394  }
395 
396 
400  ParameterList getSubstitutionModelParameters(bool independent) const override;
401 
405  ParameterList getRateDistributionParameters(bool independent) const override;
406 
410  ParameterList getBranchLengthParameters(bool independent) const override;
411 
415  ParameterList getRootFrequenciesParameters(bool independent) const override;
416 
420  ParameterList getNonDerivableParameters() const override;
421 
422  double getProbabilityForModel(size_t classIndex) const override;
423 
424  Vdouble getClassProbabilities() const override;
425 
426  double getRateForModel(size_t classIndex) const override;
427 
429 };
430 } // end of namespace bpp.
431 #endif // BPP_PHYL_LIKELIHOOD_SUBSTITUTIONPROCESSCOLLECTIONMEMBER_H
AbstractParameterAliasable & operator=(const AbstractParameterAliasable &ap)
A partial implementation of the SubstitutionProcess interface.
Interface for all Branch models.
virtual std::shared_ptr< const StateMapInterface > getStateMap() const =0
virtual const StateMapInterface & stateMap() const =0
Parametrize a set of state frequencies.
Definition: FrequencySet.h:29
PhyloTree with Parametrizable Phylo Branches. They SHARE their branch length parameters.
Map the states of a given alphabet which have a model state.
Definition: StateMap.h:25
void updateParameters()
sets the parameters as the independent parameters on the objects
const BranchModelInterface & model(unsigned int nodeId, size_t classIndex) const override
Get the substitution model corresponding to a certain branch, site pattern, and model class.
std::vector< size_t > getModelNumbers() const override
std::shared_ptr< const DiscreteDistributionInterface > getRateDistribution() const override
Get a pointer to the rate distribution (or null if there is no rate distribution).
const DiscreteDistributionInterface & rateDistribution() const override
Get the rate distribution.
size_t nPath_
the number of the set of model path, if needed.
SubstitutionProcessCollectionMember(SubstitutionProcessCollection *pSubProColl, size_t nProc, size_t nTree, size_t nDist)
Create a model set belonging to the specified SubstitutionProcessCollection. Stationarity is assumed.
size_t nRoot_
The number of the root frequencies (0 if the process is stationary).
bool isFullySetUp(bool throwEx=true) const
Check if the model set is fully specified for a given tree.
const FrequencySetInterface & rootFrequencySet() const override
size_t nProc_
The number of the process in the collection.
std::map< size_t, std::vector< unsigned int > > modelToNodes_
const SubstitutionProcessCollection * getCollection() const
ParameterList getBranchLengthParameters(bool independent) const override
Get the parameters of the tree.
const StateMapInterface & stateMap() const override
double getRateForModel(size_t classIndex) const override
bool matchParametersValues(const ParameterList &parameters) override
AbsractParametrizable interface.
const BranchModelInterface & model(size_t n) const override
std::shared_ptr< const ModelScenario > getModelScenario() const override
Get the Model Scenario associated with this process, in case there are mixture models involved.
size_t nTree_
The number of the tree: 0 means no assigned tree.
const std::vector< unsigned int > getNodesWithModel(size_t i) const override
Get a list of nodes id for which the given model is associated.
SubstitutionProcessCollectionMember(const SubstitutionProcessCollectionMember &set)
Resets all the information contained in this object.
void addModel(size_t numModel, const std::vector< unsigned int > &nodesId)
Add a new model to the set, and set relationships with nodes.
std::shared_ptr< const FrequencySetInterface > getRootFrequencySet() const override
std::shared_ptr< const BranchModelInterface > getModel(size_t n) const override
std::shared_ptr< const StateMapInterface > getStateMap() const override
ParameterList getSubstitutionModelParameters(bool independent) const override
Get the parameters of the substitution models.
const std::vector< double > & getRootFrequencies() const override
ParameterList getRateDistributionParameters(bool independent) const override
Get the parameters of the rate distribution.
std::shared_ptr< const ParametrizablePhyloTree > getParametrizablePhyloTree() const override
size_t nDist_
The number of the rate distribution.
const ParametrizablePhyloTree & parametrizablePhyloTree() const override
std::map< unsigned int, size_t > nodeToModel_
Contains for each node in a tree the index of the corresponding model in modelSet_.
std::shared_ptr< const BranchModelInterface > getModelForNode(unsigned int nodeId) const override
Get the model associated to a particular node id.
void setRootFrequencies(size_t numFreq)
Set the root Frequencies Set.
SubstitutionProcessCollectionMember & operator=(const SubstitutionProcessCollectionMember &set)
ParameterList getNonDerivableParameters() const override
get all NonDerivable parameters.
const SubstitutionProcessCollection & collection() const
ParameterList getRootFrequenciesParameters(bool independent) const override
Get the parameters of the root frequencies set.
SubstitutionProcessCollection * pSubProColl_
A pointer towards the collection the SubstitutionProcessCollectionMember belongs to.
size_t getModelNumberForNode(unsigned int nodeId) const override
Get the number of the model associated to a particular node id.
SubstitutionProcessCollectionMember * clone() const override
std::shared_ptr< const BranchModelInterface > getModel(unsigned int nodeId, size_t classIndex) const override
Get the substitution model corresponding to a certain branch, site pattern, and model class.
double getProbabilityForModel(size_t classIndex) const override
Collection of Substitution Process, which owns all the necessary objects: Substitution models,...
std::string toString(T t)
Defines the basic types of data flow nodes.
std::vector< double > Vdouble
void operator()(SubstitutionProcessCollectionMember *sm) const