bpp-phyl3  3.0.0
ModelPath.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 "ModelPath.h"
6 
7 using namespace bpp;
8 using namespace std;
9 
13 
15  mModPath_(hn.mModPath_),
16  proba_(hn.proba_)
17 {}
18 
19 
21 {
22  mModPath_ = hn.mModPath_;
23  proba_ = hn.proba_;
24 
25  return *this;
26 }
27 
28 void ModelPath::setModel(std::shared_ptr<MixedTransitionModelInterface> mMod, const Vuint& vnS)
29 {
30  if (vnS.size() == 0)
31  return;
32  mModPath_[mMod] = PathNode();
33  mModPath_[mMod].insertN(vnS);
34 
35  if (mModPath_[mMod].back() >= mMod->getNumberOfModels())
36  throw IndexOutOfBoundsException("ModelPath::setModel. Bad submodel number in mixed model", mModPath_[mMod].back(), 0, mMod->getNumberOfModels() - 1);
37 }
38 
39 void ModelPath::changeModel(shared_ptr<MixedTransitionModelInterface> mMod1,
40  shared_ptr<MixedTransitionModelInterface> mMod2)
41 {
42  if (mModPath_.find(mMod1) == mModPath_.end())
43  throw Exception("ModelPath::changeModel : Unknown model " + mMod1->getName());
44 
45  if (leadMod_ == mMod1)
46  leadMod_ = mMod2;
47 
48  const auto& np = mModPath_[mMod1];
49  setModel(mMod2, np);
50  mModPath_.erase(mMod1);
51 }
52 
53 void ModelPath::addToModel(std::shared_ptr<MixedTransitionModelInterface> mMod, const Vuint& vnS)
54 {
55  if (mModPath_.find(mMod) == mModPath_.end())
56  mModPath_[mMod] = PathNode();
57 
58  mModPath_[mMod].insertN(vnS);
59 
60  if (mModPath_.size() > 0 && mModPath_[mMod].back() >= mMod->getNumberOfModels())
61  throw IndexOutOfBoundsException("ModelPath::addToModel. Bad submodel number in mixed model", mModPath_[mMod].back(), 0, mMod->getNumberOfModels() - 1);
62 }
63 
64 bool ModelPath::operator<=(const ModelPath& hn) const
65 {
66  const auto& mpath2 = hn.mModPath_;
67 
68  for (const auto& ipath : mModPath_)
69  {
70  if (mpath2.find(ipath.first) != mpath2.end() &&
71  !(ipath.second <= mpath2.at(ipath.first)))
72  return false;
73  }
74 
75  return true;
76 }
77 
78 bool ModelPath::operator>=(const ModelPath& hn) const
79 {
80  return hn <= *this;
81 }
82 
83 bool ModelPath::intersects(const ModelPath& hn) const
84 {
85  const auto& mpath2 = hn.mModPath_;
86 
87  for (const auto& ipath : mModPath_)
88  {
89  if (mpath2.find(ipath.first) == mpath2.end() ||
90  ipath.second.intersects(mpath2.at(ipath.first)))
91  return true;
92  }
93 
94  for (const auto& ipath : mpath2)
95  {
96  if (mModPath_.find(ipath.first) == mModPath_.end())
97  return true;
98  }
99 
100  return false;
101 }
102 
104 {
105  const auto& mpath2 = hn.mModPath_;
106 
107  for (const auto& ipath : mpath2)
108  {
109  addToModel(ipath.first, ipath.second);
110  }
111 
112  return *this;
113 }
114 
116 {
117  const auto& mpath2 = hn.mModPath_;
118 
119  for (const auto& ipath : mpath2)
120  {
121  if (mModPath_.find(ipath.first) != mModPath_.end())
122  {
123  mModPath_[ipath.first] -= ipath.second;
124  if (mModPath_[ipath.first].size() == 0)
125  mModPath_.erase(ipath.first);
126  }
127  }
128 
129  return *this;
130 }
131 
132 vector< shared_ptr<MixedTransitionModelInterface>> ModelPath::getModels() const
133 {
134  vector< shared_ptr<MixedTransitionModelInterface>> models;
135 
136  std::transform(
137  mModPath_.begin(),
138  mModPath_.end(),
139  std::back_inserter(models),
140  [](const map<shared_ptr<MixedTransitionModelInterface>, PathNode>::value_type& pair){
141  return pair.first;
142  });
143 
144  return models;
145 }
146 
147 std::string ModelPath::toString() const
148 {
149  std::string output;
150  bool deb = true;
151  for (const auto& mod : mModPath_)
152  {
153  if (!deb)
154  output += "&";
155 
156  auto model = mod.first;
157 
158  if (dynamic_cast<const AbstractBiblioMixedTransitionModel*>(model.get()) == nullptr)
159  {
160  std::string name = "";
161  auto pMS = dynamic_cast<const MixtureOfTransitionModels*>(model.get());
162  if (pMS)
163  {
164  name = "Mixture[";
165  bool com = false;
166  for (auto nb:mod.second)
167  {
168  name = name + (com ? ", " : "") + pMS->AbstractMixedTransitionModel::nModel(nb - 1).getName();
169  }
170  name += "]";
171  }
172  else
173  {
174  auto pMT = dynamic_cast<const MixtureOfATransitionModel*>(model.get());
175  name = "MixedModel";
176 
177  const TransitionModelInterface& eM = pMT->model(0);
178 
179  name += "." + eM.getName() + "[" + mod.second.to_string() + "]";
180  }
181  output += name;
182  }
183  else
184  output += model->getName() + "[" + mod.second.to_string() + "]";
185 
186  deb = false;
187  }
188  return output;
189 }
190 
191 /**********************************************************/
192 /******************** NODE ********************************/
193 /***********************************************************/
194 
196 {
197  for (const auto& it2 : vn)
198  {
199  vector<uint>::const_iterator it(begin());
200 
201  for ( ; it != end(); it++)
202  {
203  if (*it >= it2)
204  break;
205  }
206  if (it == end())
207  push_back(it2);
208  else if (*it != it2)
209  insert(it, it2);
210  }
211 }
212 
214 {
215  erase(std::remove_if(begin(), end(),
216  [vn](const uint x) -> bool {
217  return std::find(vn.begin(), vn.end(), x) != vn.end();
218  }), end());
219 }
220 
222 {
223  vector<uint>::const_iterator it2(n.begin());
224 
225  for (const auto& it : *this)
226  {
227  while (it2 != n.end() && (*it2 < it))
228  it2++;
229  if (it2 == n.end() || (*it2 > it))
230  return false;
231  }
232  return true;
233 }
234 
236 {
237  return n <= *this;
238 }
239 
241 {
242  vector<uint>::const_iterator it2(n.begin());
243 
244  for (const auto& it : *this)
245  {
246  while (it2 != n.end() && (*it2 < it))
247  it2++;
248 
249  if (it2 == n.end())
250  return false;
251  if (*it2 == it)
252  return true;
253  }
254  return false;
255 }
256 
258 {
259  std::string output;
260  bool deb = true;
261  for (auto ind:*this)
262  {
263  if (!deb)
264  output += ",";
265  output += std::to_string(ind + 1);
266  deb = false;
267  }
268  return output;
269 }
Abstract class for mixture models based on the bibliography.
virtual std::string getName() const =0
Get the name of the model.
Transition models defined as a mixture of nested substitution models.
Transition models defined as a mixture of several substitution models.
std::string getName() const override
Get the name of the model.
A vector<int> where all elements are different and in INCREASING ORDER. So inclusion should be done t...
Definition: ModelPath.h:30
void insertN(const Vuint &vn)
Insert elements.
Definition: ModelPath.cpp:195
bool operator>=(const PathNode &) const
checks if this PathNode includes another one.
Definition: ModelPath.cpp:235
bool intersects(const PathNode &) const
checks if this PathNode intersects another one.
Definition: ModelPath.cpp:240
void removeN(const Vuint &vn)
Remove elements.
Definition: ModelPath.cpp:213
bool operator<=(const PathNode &) const
checks if this PathNode is included in another one.
Definition: ModelPath.cpp:221
std::string to_string() const
Output.
Definition: ModelPath.cpp:257
Organization of submodels in mixed substitution models in a path. See class ModelScenario for a thoro...
Definition: ModelPath.h:22
ModelPath & operator-=(const ModelPath &)
Remove from the PathNodes of this object the matching ones of the ModelPath.
Definition: ModelPath.cpp:115
std::shared_ptr< MixedTransitionModelInterface > leadMod_
Definition: ModelPath.h:108
bool intersects(const ModelPath &) const
checks if this ModelPath intersects another one. Which means that one submodel explicitly declared in...
Definition: ModelPath.cpp:83
std::string toString() const
string description
Definition: ModelPath.cpp:147
std::map< std::shared_ptr< MixedTransitionModelInterface >, PathNode > mModPath_
Definition: ModelPath.h:103
void changeModel(std::shared_ptr< MixedTransitionModelInterface > mMod1, std::shared_ptr< MixedTransitionModelInterface > mMod2)
change from a model to another
Definition: ModelPath.cpp:39
ModelPath & operator=(const ModelPath &)
Definition: ModelPath.cpp:20
std::vector< std::shared_ptr< MixedTransitionModelInterface > > getModels() const
gets the MixedTransitionModel used in the ModelPath
Definition: ModelPath.cpp:132
void setModel(std::shared_ptr< MixedTransitionModelInterface > mMod, const Vuint &vnS)
sets submodel numbers in the mixed model. Checks if all the numbers are valid.
Definition: ModelPath.cpp:28
ModelPath & operator+=(const ModelPath &)
Cumulates the PathNodes of the given ModelPath into this one.
Definition: ModelPath.cpp:103
void addToModel(std::shared_ptr< MixedTransitionModelInterface > mMod, const Vuint &vnS)
adds submodel numbers to the mixed model. Checks if all the numbers are valid.
Definition: ModelPath.cpp:53
bool operator<=(const ModelPath &) const
checks if this ModelPath is included in another one. Which means that all submodels of this path are ...
Definition: ModelPath.cpp:64
double proba_
probability of this ModelPath.
Definition: ModelPath.h:113
bool operator>=(const ModelPath &) const
checks if this ModelPath includes another one.
Definition: ModelPath.cpp:78
Interface for all transition models.
Defines the basic types of data flow nodes.
std::string to_string(const NoDimension &)
std::vector< unsigned int > Vuint