bpp-phyl3  3.0.0
ModelScenario.cpp
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: The Bio++ Development Group
2 //
3 // SPDX-License-Identifier: CECILL-2.1
4 
5 #include <numeric>
6 
7 #include "ModelScenario.h"
8 
9 using namespace bpp;
10 using namespace std;
11 
13 {
14  ModelPath nhn;
15  for (const auto& mp : vModelPaths_)
16  {
17  nhn += *mp;
18  }
19 
20  auto rest = make_shared<ModelPath> ();
21  auto models = nhn.getModels();
22  for (const auto& model:models)
23  {
24  Vuint v((uint)model->getNumberOfModels());
25  iota(v.begin(), v.end(), 0);
26  rest->setModel(model, v);
27  }
28 
29  (*rest) -= nhn;
30 
31  if (rest->size() != 0)
32  {
33  addModelPath(rest);
34  return true;
35  }
36 
37  return false;
38 }
39 
40 void ModelScenario::changeModel(shared_ptr<MixedTransitionModelInterface> m1,
41  shared_ptr<MixedTransitionModelInterface> m2)
42 {
43  for (auto& mp: vModelPaths_)
44  {
45  mp->changeModel(m1, m2);
46  }
47 }
48 
50 {
51  ModelPath tthn;
52 
53  size_t nhn = getNumberOfModelPaths();
54  for (size_t i = 0; i < nhn; i++)
55  {
56  if (tthn.intersects(*getModelPath(i)))
57  return false;
58  else
59  tthn += (*getModelPath(i));
60  }
61 
62  return true;
63 }
64 
65 // void ModelScenario::fireParameterChanged(const ParameterList& parameters)
66 // {
67 // SubstitutionModelSet::fireParameterChanged(parameters);
68 
69 // // should be restricted only when probability related parameters are changed
70 // computeModelPathsProbabilities();
71 // }
72 
73 
75 {
76  return;
77 
78  size_t nbh = getNumberOfModelPaths();
79 
80  if (nbh == 0)
81  return;
82 
83  // Compute the probabilities of the hypernodes from the lead mixed
84  // model of the first ModelPath
85 
86  shared_ptr<MixedTransitionModelInterface> pfSM(vModelPaths_[0]->getLeadModel());
87 
88  if (pfSM == 0)
89  throw Exception("ModelScenario::computeModelPathsProbabilities: missing lead Model.");
90 
91  for (size_t nh = 0; nh < nbh; nh++)
92  {
93  ModelPath& h = *getModelPath(nh);
94  if (h.hasModel(pfSM))
95  {
96  const ModelPath::PathNode& fnd = h.getPathNode(pfSM);
97 
98  double fprob = 0;
99  for (const auto& fn:fnd)
100  {
101  fprob += pfSM->getNProbability(static_cast<size_t>(fn));
102  }
103 
104  h.setProbability(fprob);
105  }
106  else
107  throw Exception("ModelScenario::computeModelPathsProbabilities : reference model " + pfSM->getName() + " is missing in ModelPath " + TextTools::toString(nh));
108  }
109 
110  // Sets the new probabilities & rates of the mixmodels
111 
112  auto models = getModels();
113 
114  for (auto model:getModels())
115  {
116  if (model != pfSM)
117  {
118  for (size_t nh = 0; nh < nbh; nh++)
119  {
120  ModelPath& h = *getModelPath(nh);
121  if (!h.hasModel(model))
122  throw Exception("ModelScenario::computeModelPathsProbabilities : reference model " + model->getName() + " is missing in ModelPath " + TextTools::toString(nh));
123 
124  const ModelPath::PathNode& fnd = h.getPathNode(model);
125  double prob = 0;
126  for (auto& fn:fnd)
127  {
128  prob += model->getNProbability(static_cast<size_t>(fn));
129  }
130 
131  // sets the real probabilities
132  for (auto& fn:fnd)
133  {
134  model->setNProbability(static_cast<size_t>(fn), h.getProbability() * model->getNProbability(static_cast<size_t>(fn)) / prob);
135  }
136  }
137 
138  // normalizes Vrates with the real probabilities
139 
140  model->normalizeVRates();
141  }
142 
143  // sets the conditional probabilities
144 
145  for (size_t nh = 0; nh < nbh; nh++)
146  {
147  ModelPath& h = *getModelPath(nh);
148  const ModelPath::PathNode& fnd = h.getPathNode(model);
149  for (auto& fn:fnd)
150  {
151  model->setNProbability(static_cast<size_t>(fn),
152  model->getNProbability(static_cast<size_t>(fn)) / h.getProbability());
153  }
154  }
155  }
156 }
157 
159 {
160  string output;
161 
162  for (const auto& mp :vModelPaths_)
163  {
164  output += "<" + mp->toString() + ">";
165  }
166 
167  return output;
168 }
169 
170 vector<shared_ptr<MixedTransitionModelInterface>> ModelScenario::getModels() const
171 {
172  vector<shared_ptr<MixedTransitionModelInterface>> models, models2;
173 
174  for (const auto& mp : vModelPaths_)
175  {
176  auto vmodel = mp->getModels();
177  for (auto& model:vmodel)
178  {
179  if (find(models.begin(), models.end(), model) == models.end())
180  models.push_back(model);
181  else if (find(models2.begin(), models2.end(), model) == models2.end())
182  models2.push_back(model);
183  }
184  }
185 
186  return models2; // return only models found in several paths
187 }
188 
189 // double ModelScenario::getModelPathProbability(const ModelPath& hn) const
190 // {
191 // auto models= hn.getModels();
192 
193 // double fprob = 1;
194 // for (auto& model:models)
195 // {
196 // double x = 0;
197 // for (const auto fn:hn.getPathNode(model))
198 // x += model->getNProbability(static_cast<size_t>(fn));
199 
200 // fprob *= x;
201 // }
202 
203 // return fprob;
204 // }
A vector<int> where all elements are different and in INCREASING ORDER. So inclusion should be done t...
Definition: ModelPath.h:30
Organization of submodels in mixed substitution models in a path. See class ModelScenario for a thoro...
Definition: ModelPath.h:22
bool intersects(const ModelPath &) const
checks if this ModelPath intersects another one. Which means that one submodel explicitly declared in...
Definition: ModelPath.cpp:83
const PathNode & getPathNode(std::shared_ptr< MixedTransitionModelInterface > mMod) const
gets the pathnode associated with a model
Definition: ModelPath.h:251
double getProbability() const
returns the probability
Definition: ModelPath.h:225
void setProbability(double x)
sets the probability
Definition: ModelPath.h:230
std::vector< std::shared_ptr< MixedTransitionModelInterface > > getModels() const
gets the MixedTransitionModel used in the ModelPath
Definition: ModelPath.cpp:132
bool hasModel(std::shared_ptr< MixedTransitionModelInterface > mMod) const
checks if there is a pathnode associated with a model
Definition: ModelPath.h:235
void computeModelPathsProbabilities()
compute the probabilities in all the ModelPaths
std::vector< std::shared_ptr< MixedTransitionModelInterface > > getModels() const
return models found in several paths
void changeModel(std::shared_ptr< MixedTransitionModelInterface > m1, std::shared_ptr< MixedTransitionModelInterface > m2)
bool hasExclusivePaths() const
Checks if all the path (ie hypernodes) are exclusive.
std::string toString() const
string description
std::string toString(T t)
Defines the basic types of data flow nodes.
std::vector< unsigned int > Vuint