bpp-phyl3  3.0.0
SubstitutionProcessCollection.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_SUBSTITUTIONPROCESSCOLLECTION_H
6 #define BPP_PHYL_LIKELIHOOD_SUBSTITUTIONPROCESSCOLLECTION_H
7 
8 // From bpp-core:
9 #include <Bpp/Exceptions.h>
14 
15 // From bpp-seq:
18 
19 #include "../Model/FrequencySet/FrequencySet.h"
20 #include "../Model/SubstitutionModel.h"
22 #include "SubstitutionProcess.h"
24 
25 
26 namespace bpp
27 {
48  public virtual Clonable,
50 {
51 private:
56 
61  std::map<size_t, std::vector<size_t>> mModelToSubPro_;
62 
67 
72  std::map<size_t, std::vector<size_t>> mFreqToSubPro_;
73 
78 
86  std::map<size_t, std::vector<size_t>> mVConstDist_;
87 
92  std::map<size_t, std::vector<size_t>> mDistToSubPro_;
93 
98 
103  std::map<size_t, std::vector<size_t>> mTreeToSubPro_;
104 
108  std::map<size_t, std::shared_ptr<ModelScenario>> mModelScenario_;
109 
113  std::map<size_t, std::shared_ptr<SubstitutionProcessCollectionMember>> mSubProcess_; // Need a specific deleter because the destructor is private.
114 
115 public:
121  modelColl_(),
122  mModelToSubPro_(),
123  freqColl_(),
124  mFreqToSubPro_(),
125  distColl_(),
126  mVConstDist_(),
127  mDistToSubPro_(),
128  treeColl_(),
129  mTreeToSubPro_(),
130  mSubProcess_()
131  {}
132 
133 
135 
137 
139  {
140  clear();
141  }
142 
144 
145 
151  void clear();
152 
169  void addParametrizable(std::shared_ptr<Parametrizable> parametrizable, size_t parametrizableIndex, bool withParameters = true);
170 
171  void replaceParametrizable(std::shared_ptr<Parametrizable> parametrizable, size_t parametrizableIndex, bool withParameters = true);
172 
181  void addModel(std::shared_ptr<BranchModelInterface> model, size_t modelIndex)
182  {
183  addParametrizable(model, modelIndex);
184  }
185 
186  void replaceModel(std::shared_ptr<BranchModelInterface> model, size_t modelIndex)
187  {
188  replaceParametrizable(model, modelIndex);
189  }
190 
191  void addFrequencies(std::shared_ptr<FrequencySetInterface> frequencies, size_t frequenciesIndex)
192  {
193  addParametrizable(frequencies, frequenciesIndex);
194  }
195 
196  void replaceFrequencies(std::shared_ptr<FrequencySetInterface> frequencies, size_t frequenciesIndex)
197  {
198  addParametrizable(frequencies, frequenciesIndex);
199  }
200 
201  void addDistribution(std::shared_ptr<DiscreteDistributionInterface> distribution, size_t distributionIndex)
202  {
203  addParametrizable(distribution, distributionIndex, (distributionIndex < 10000));
204 
205  if (distributionIndex >= 10000)
206  mVConstDist_[distributionIndex / 10000 - 1].push_back(distributionIndex % 10000);
207  }
208 
209 
210  void addTree(std::shared_ptr<ParametrizablePhyloTree> tree, size_t treeIndex)
211  {
212  addParametrizable(tree, treeIndex);
213  }
214 
215  void replaceTree(std::shared_ptr<ParametrizablePhyloTree> tree, size_t treeIndex)
216  {
217  replaceParametrizable(tree, treeIndex);
218  }
219 
220  void addScenario(std::shared_ptr<ModelScenario> scen, size_t scenIndex)
221  {
222  mModelScenario_[scenIndex] = scen;
223  }
224 
231  BranchModelInterface& model(size_t modelIndex)
232  {
233  return dynamic_cast<BranchModelInterface&>(*modelColl_[modelIndex]);
234  }
235 
236  const BranchModelInterface& model(size_t modelIndex) const
237  {
238  return dynamic_cast<const BranchModelInterface&>(*modelColl_[modelIndex]);
239  }
240 
241  std::shared_ptr<BranchModelInterface> getModel(size_t modelIndex)
242  {
243  return std::dynamic_pointer_cast<BranchModelInterface>(modelColl_[modelIndex]);
244  }
245 
246  std::shared_ptr<const BranchModelInterface> getModel(size_t modelIndex) const
247  {
248  return std::dynamic_pointer_cast<const BranchModelInterface>(modelColl_[modelIndex]);
249  }
250 
257  size_t getModelIndex(std::shared_ptr<BranchModelInterface> model) const
258  {
259  if (modelColl_.hasObject(model))
260  return modelColl_.getFirstKey(model);
261  else
262  return 0;
263  }
264 
270  bool hasModel(std::shared_ptr<BranchModelInterface> model) const
271  {
272  return modelColl_.hasObject(model);
273  }
274 
281  FrequencySetInterface& frequencySet(size_t frequenciesIndex)
282  {
283  return *freqColl_[frequenciesIndex];
284  }
285 
286  const FrequencySetInterface& frequencySet(size_t frequenciesIndex) const
287  {
288  return *(std::dynamic_pointer_cast<const FrequencySetInterface>(freqColl_[frequenciesIndex]));
289  }
290 
291  std::shared_ptr<FrequencySetInterface> getFrequencySet(size_t frequenciesIndex)
292  {
293  return freqColl_[frequenciesIndex];
294  }
295 
296  std::shared_ptr<const FrequencySetInterface> getFrequencySet(size_t frequenciesIndex) const
297  {
298  return freqColl_[frequenciesIndex];
299  }
300 
307  std::shared_ptr<DiscreteDistributionInterface> getRateDistribution(size_t distributionIndex)
308  {
309  return distColl_[distributionIndex];
310  }
311 
312  std::shared_ptr<const DiscreteDistributionInterface> getRateDistribution(size_t distributionIndex) const
313  {
314  return distColl_[distributionIndex];
315  }
316 
318  {
319  return *distColl_[distributionIndex];
320  }
321 
322  const DiscreteDistributionInterface& rateDistribution(size_t distributionIndex) const
323  {
324  return *distColl_[distributionIndex];
325  }
326 
333  ParametrizablePhyloTree& tree(size_t treeIndex)
334  {
335  return dynamic_cast<ParametrizablePhyloTree&>(*treeColl_[treeIndex]);
336  }
337 
338  const ParametrizablePhyloTree& tree(size_t treeIndex) const
339  {
340  return dynamic_cast<const ParametrizablePhyloTree&>(*treeColl_[treeIndex]);
341  }
342 
343  std::shared_ptr<ParametrizablePhyloTree> getTree(size_t treeIndex)
344  {
345  return std::dynamic_pointer_cast<ParametrizablePhyloTree>(treeColl_[treeIndex]);
346  }
347 
348  std::shared_ptr<const ParametrizablePhyloTree> getTree(size_t treeIndex) const
349  {
350  return std::dynamic_pointer_cast<const ParametrizablePhyloTree>(treeColl_[treeIndex]);
351  }
352 
358  bool hasModelScenario(size_t numPath) const
359  {
360  return mModelScenario_.find(numPath) != mModelScenario_.end();
361  }
362 
369  std::shared_ptr<const ModelScenario> getModelScenario(size_t numPath) const
370  {
371  if (!hasModelScenario(numPath))
372  return 0;
373  else
374  return mModelScenario_.at(numPath);
375  }
376 
377  std::shared_ptr<ModelScenario> getModelScenario(size_t numPath)
378  {
379  if (!hasModelScenario(numPath))
380  return 0;
381  else
382  return mModelScenario_.at(numPath);
383  }
384 
385  std::vector<size_t> getScenarioNumbers() const
386  {
387  std::vector<size_t> vkeys;
388 
389  for (const auto& it:mModelScenario_)
390  {
391  vkeys.push_back(it.first);
392  }
393 
394  return vkeys;
395  }
396 
397 
402  std::vector<size_t> getModelNumbers() const
403  {
404  return modelColl_.keys();
405  }
406 
407  std::vector<size_t> getFrequenciesNumbers() const
408  {
409  return freqColl_.keys();
410  }
411 
412  std::vector<size_t> getRateDistributionNumbers() const
413  {
414  return distColl_.keys();
415  }
416 
417  std::vector<size_t> getTreeNumbers() const
418  {
419  return treeColl_.keys();
420  }
421 
422  std::vector<size_t> getSubstitutionProcessNumbers() const
423  {
424  std::vector<size_t> vn;
425 
426  for (auto it = mSubProcess_.begin(); it != mSubProcess_.end(); it++)
427  {
428  vn.push_back(it->first);
429  }
430 
431  return vn;
432  }
433 
434  bool hasModelNumber(size_t n) const
435  {
436  return modelColl_.hasObject(n);
437  }
438 
439  bool hasFrequenciesNumber(size_t n) const
440  {
441  return freqColl_.hasObject(n);
442  }
443 
444  bool hasDistributionNumber(size_t n) const
445  {
446  return distColl_.hasObject(n);
447  }
448 
449  bool hasTreeNumber(size_t n) const
450  {
451  return treeColl_.hasObject(n);
452  }
453 
454  bool hasSubstitutionProcessNumber(size_t n) const
455  {
456  return mSubProcess_.find(n) != mSubProcess_.end();
457  }
458 
473  void fireParameterChanged(const ParameterList& parameters);
474 
475  void setNamespace(const std::string& prefix);
476 
477  void aliasParameters(const std::string& p1, const std::string& p2);
478 
479  void unaliasParameters(const std::string& p1, const std::string& p2);
480 
481  void aliasParameters(std::map<std::string, std::string>& unparsedParams, bool verbose);
482 
487  /*
488  * @brief Method to add a SubstitutionProcess.
489  *
490  * @param nProc the number of the Substitution process
491  * @param mModBr a map associating numbers of models (from the collection) and numbers of branches
492  * @param nTree the number of a Tree (from the collection)
493  * @param nRate the number of a Distribution Rate (from the collection)
494  * @param nFreq the number of a FrequencySet for the root (from the collection)
495  *
496  * @throw an Exception if the built SubstitutionModelSet is not complete or well built.
497  *
498  */
499  void addSubstitutionProcess(size_t nProc, std::map<size_t, std::vector<unsigned int>> mModBr, size_t nTree, size_t nRate, size_t nFreq);
500 
501  /*
502  * @brief Method to add a SubstitutionProcess.
503  *
504  * @param nProc the number of the Substitution process
505  * @param mModBr a map associating numbers of models (from the collection) and numbers of branches
506  * @param nTree the number of a Tree (from the collection)
507  * @param nRate the number of a Distribution Rate (from the collection)
508  *
509  * @throw an Exception if the built SubstitutionModelSet is not complete or well built.
510  *
511  */
512  void addSubstitutionProcess(size_t nProc, std::map<size_t, std::vector<unsigned int>> mModBr, size_t nTree, size_t nRate);
513 
514  /*
515  * @brief Method to add a one per branch SubstitutionProcess. A new
516  * model is created for each branch.
517  *
518  * @param nProc the number of the Substitution process
519  * @param nMod the number of Transition Model which is cloned on
520  * all branches.
521  * @param nTree the number of a Tree (from the collection)
522  * @param nRate the number of a Distribution Rate (from the
523  * collection)
524  * @param nFreq the number of a FrequencySet for the root (from
525  * the collection)
526  * @param sharedParameterNames the vector of the names of parameters
527  * of the model that are shared among all the models.
528  *
529  */
530  void addOnePerBranchSubstitutionProcess(size_t nProc, size_t nMod, size_t nTree, size_t nRate, size_t nFreq, const std::vector<std::string>& sharedParameterNames);
531 
532  /*
533  * @brief Method to add a one per branch SubstitutionProcess. A new
534  * model is created for each branch.
535  *
536  * @param nProc the number of the Substitution process
537  * @param nMod the number of Transition Model which is cloned on
538  * all branches.
539  * @param nTree the number of a Tree (from the collection)
540  * @param nRate the number of a Distribution Rate (from the
541  * collection)
542  * @param nFreq the number of a FrequencySet for the root (from
543  * the collection)
544  * @param sharedParameterNames the vector of the names of parameters
545  * of the model that are shared among all the models.
546  *
547  */
548  void addOnePerBranchSubstitutionProcess(size_t nProc, size_t nMod, size_t nTree, size_t nRate, const std::vector<std::string>& sharedParameterNames);
549 
553  size_t getNumberOfSubstitutionProcess() const { return mSubProcess_.size(); }
554 
556  {
557  return *mSubProcess_[i];
558  }
559 
561  {
562  const auto it = mSubProcess_.find(i);
563 
564  return *(it->second);
565  }
566 
567  std::shared_ptr<SubstitutionProcessCollectionMember> getSubstitutionProcess(size_t i)
568  {
569  return mSubProcess_[i];
570  }
571 
572  std::shared_ptr<const SubstitutionProcessCollectionMember> getSubstitutionProcess(size_t i) const
573  {
574  const auto it = mSubProcess_.find(i);
575 
576  return it->second;
577  }
578 
583  /*
584  * @brief Get the branch lengths parameters.
585  *
586  * @return A ParameterList with all branch lengths.
587  */
588  ParameterList getBranchLengthParameters(bool independent) const
589  {
590  ParameterList pl = treeColl_.getParameters();
591 
592  if (independent)
594  else
595  return pl;
596  }
597 
598  ParameterList getBranchLengthParameters(size_t nTree, bool independent) const
599  {
600  ParameterList pl = treeColl_.getParametersForObject(nTree);
601 
602  if (independent)
604  else
605  return pl;
606  }
607 
613  ParameterList getSubstitutionProcessParameters(size_t nProc, bool independent) const
614  {
615  ParameterList pl;
616 
617  if (mSubProcess_.find(nProc) != mSubProcess_.end())
618  pl = mSubProcess_.find(nProc)->second->getParameters();
619  else
620  return ParameterList();
621 
622  if (independent)
624  else
625  return pl;
626  }
627 
629 
635  ParameterList getSubstitutionModelParameters(size_t nMod, bool independent) const
636  {
637  ParameterList pl = modelColl_.getParametersForObject(nMod);
638 
639  if (independent)
641  else
642  return pl;
643  }
644 
646  {
647  ParameterList pl = modelColl_.getParameters();
648 
649  if (independent)
651  else
652  return pl;
653  }
654 
655 
663 
669  ParameterList getRateDistributionParameters(size_t nRate, bool independent) const
670  {
671  ParameterList pl = distColl_.getParametersForObject(nRate);
672 
673  if (independent)
675  else
676  return pl;
677  }
678 
680  {
681  ParameterList pl = distColl_.getParameters();
682 
683  if (independent)
685  else
686  return pl;
687  }
688 
694  ParameterList getRootFrequenciesParameters(size_t nFreq, bool independent) const
695  {
696  ParameterList pl = freqColl_.getParametersForObject(nFreq);
697 
698  if (independent)
700  else
701  return pl;
702  }
703 
705  {
706  ParameterList pl = freqColl_.getParameters();
707 
708  if (independent)
710  else
711  return pl;
712  }
713 };
714 } // end of namespace bpp.
715 #endif // BPP_PHYL_LIKELIHOOD_SUBSTITUTIONPROCESSCOLLECTION_H
const ParameterList & getIndependentParameters() const
Interface for all Branch models.
Parametrize a set of state frequencies.
Definition: FrequencySet.h:29
virtual ParameterList getCommonParametersWith(const ParameterList &params) const
PhyloTree with Parametrizable Phylo Branches. They SHARE their branch length parameters.
Collection of Substitution Process, which owns all the necessary objects: Substitution models,...
std::vector< size_t > getRateDistributionNumbers() const
ParametrizableCollection< DiscreteDistributionInterface > distColl_
std::shared_ptr< const ParametrizablePhyloTree > getTree(size_t treeIndex) const
std::shared_ptr< const BranchModelInterface > getModel(size_t modelIndex) const
ParameterList getRootFrequenciesParameters(size_t nFreq, bool independent) const
Get the parameters associated to the root frequencies(s).
std::map< size_t, std::shared_ptr< SubstitutionProcessCollectionMember > > mSubProcess_
FrequencySetInterface & frequencySet(size_t frequenciesIndex)
Get a FrequencySet from the collection.
SubstitutionProcessCollection & operator=(const SubstitutionProcessCollection &set)
DiscreteDistributionInterface & rateDistribution(size_t distributionIndex)
ParametrizableCollection< BranchModelInterface > modelColl_
std::vector< size_t > getSubstitutionProcessNumbers() const
std::vector< size_t > getModelNumbers() const
Get the numbers of the specified objects from the collections.
std::map< size_t, std::vector< size_t > > mDistToSubPro_
std::map< size_t, std::shared_ptr< ModelScenario > > mModelScenario_
SubstitutionProcessCollection * clone() const
void replaceFrequencies(std::shared_ptr< FrequencySetInterface > frequencies, size_t frequenciesIndex)
void addScenario(std::shared_ptr< ModelScenario > scen, size_t scenIndex)
std::map< size_t, std::vector< size_t > > mModelToSubPro_
void replaceTree(std::shared_ptr< ParametrizablePhyloTree > tree, size_t treeIndex)
size_t getNumberOfSubstitutionProcess() const
Methods to retrieve Substitution Process.
const BranchModelInterface & model(size_t modelIndex) const
const SubstitutionProcessCollectionMember & substitutionProcess(size_t i) const
ParameterList getNonDerivableParameters() const
Get the Non-derivable parameters.
void fireParameterChanged(const ParameterList &parameters)
AbstractParameterAliasable functions, redirected towards the process members.
std::shared_ptr< const ModelScenario > getModelScenario(size_t numPath) const
Get a ModelScenario from the set.
const DiscreteDistributionInterface & rateDistribution(size_t distributionIndex) const
ParameterList getRateDistributionParameters(size_t nRate, bool independent) const
Get the parameters associated to the rate distribution(s).
std::shared_ptr< FrequencySetInterface > getFrequencySet(size_t frequenciesIndex)
size_t getModelIndex(std::shared_ptr< BranchModelInterface > model) const
Return the number of a BranchModel in the collection.
BranchModelInterface & model(size_t modelIndex)
Get a BranchModel from the collection.
std::shared_ptr< const FrequencySetInterface > getFrequencySet(size_t frequenciesIndex) const
void addFrequencies(std::shared_ptr< FrequencySetInterface > frequencies, size_t frequenciesIndex)
void clear()
Resets all the information contained in this object.
SubstitutionProcessCollectionMember & substitutionProcess(size_t i)
std::shared_ptr< DiscreteDistributionInterface > getRateDistribution(size_t distributionIndex)
Get a DiscreteDistribution from the collection.
void replaceParametrizable(std::shared_ptr< Parametrizable > parametrizable, size_t parametrizableIndex, bool withParameters=true)
ParametrizableCollection< FrequencySetInterface > freqColl_
std::vector< size_t > getScenarioNumbers() const
ParameterList getBranchLengthParameters(size_t nTree, bool independent) const
ParameterList getSubstitutionModelParameters(size_t nMod, bool independent) const
Get the parameters associated to substitution model(s).
void addOnePerBranchSubstitutionProcess(size_t nProc, size_t nMod, size_t nTree, size_t nRate, size_t nFreq, const std::vector< std::string > &sharedParameterNames)
ParameterList getSubstitutionProcessParameters(size_t nProc, bool independent) const
Get the parameters associated to substitution process(es).
void aliasParameters(const std::string &p1, const std::string &p2)
ParameterList getRateDistributionParameters(bool independent) const
ParameterList getRootFrequenciesParameters(bool independent) const
std::vector< size_t > getFrequenciesNumbers() const
bool hasModelScenario(size_t numPath) const
checks if the set has a ModelScenario
std::map< size_t, std::vector< size_t > > mTreeToSubPro_
std::shared_ptr< const SubstitutionProcessCollectionMember > getSubstitutionProcess(size_t i) const
void unaliasParameters(const std::string &p1, const std::string &p2)
std::shared_ptr< BranchModelInterface > getModel(size_t modelIndex)
bool hasModel(std::shared_ptr< BranchModelInterface > model) const
Return if a BranchModel is in the collection.
ParameterList getBranchLengthParameters(bool independent) const
Methods to retrieve parameters.
SubstitutionProcessCollection()
Create empty collections.
ParametrizableCollection< ParametrizablePhyloTree > treeColl_
std::shared_ptr< const DiscreteDistributionInterface > getRateDistribution(size_t distributionIndex) const
void replaceModel(std::shared_ptr< BranchModelInterface > model, size_t modelIndex)
ParameterList getSubstitutionModelParameters(bool independent) const
void addDistribution(std::shared_ptr< DiscreteDistributionInterface > distribution, size_t distributionIndex)
void addModel(std::shared_ptr< BranchModelInterface > model, size_t modelIndex)
specific methods to add specific objects.
void addParametrizable(std::shared_ptr< Parametrizable > parametrizable, size_t parametrizableIndex, bool withParameters=true)
Add a new parametrizable to the matching collection with a given number.
void addSubstitutionProcess(size_t nProc, std::map< size_t, std::vector< unsigned int >> mModBr, size_t nTree, size_t nRate, size_t nFreq)
const ParametrizablePhyloTree & tree(size_t treeIndex) const
const FrequencySetInterface & frequencySet(size_t frequenciesIndex) const
std::shared_ptr< SubstitutionProcessCollectionMember > getSubstitutionProcess(size_t i)
std::map< size_t, std::vector< size_t > > mVConstDist_
std::shared_ptr< ParametrizablePhyloTree > getTree(size_t treeIndex)
void addTree(std::shared_ptr< ParametrizablePhyloTree > tree, size_t treeIndex)
std::shared_ptr< ModelScenario > getModelScenario(size_t numPath)
std::map< size_t, std::vector< size_t > > mFreqToSubPro_
ParametrizablePhyloTree & tree(size_t treeIndex)
Get a tree from the set.
Defines the basic types of data flow nodes.