bpp-core3  3.0.0
AbstractHmmTransitionMatrix.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 "../Matrix/MatrixTools.h"
6 #include "../Random/RandomTools.h"
7 #include "../VectorTools.h"
9 
10 using namespace bpp;
11 using namespace std;
12 
13 AbstractHmmTransitionMatrix::AbstractHmmTransitionMatrix(std::shared_ptr<const HmmStateAlphabet> alph, const string& prefix) :
14  alph_(alph),
15  pij_(static_cast<size_t>(alph->getNumberOfStates()), static_cast<size_t>(alph->getNumberOfStates())),
16  tmpmat_(static_cast<size_t>(alph->getNumberOfStates()), static_cast<size_t>(alph->getNumberOfStates())),
17  eqFreq_(static_cast<size_t>(alph->getNumberOfStates())),
18  upToDate_(false)
19 {}
20 
22  alph_(hptm.alph_),
23  pij_(hptm.pij_),
24  tmpmat_(hptm.tmpmat_),
25  eqFreq_(hptm.eqFreq_),
26  upToDate_(hptm.upToDate_)
27 {}
28 
30 {
31  alph_ = hptm.alph_;
32  pij_ = hptm.pij_;
33  tmpmat_ = hptm.tmpmat_;
34  eqFreq_ = hptm.eqFreq_;
35  upToDate_ = hptm.upToDate_;
36 
37  return *this;
38 }
39 
40 void AbstractHmmTransitionMatrix::setHmmStateAlphabet(std::shared_ptr<const HmmStateAlphabet> stateAlphabet)
41 {
42  if (stateAlphabet == nullptr)
43  throw HmmUnvalidAlphabetException("Null alphabet in AbstractHmmTransitionMatrix::setHmmStateAlphabet");
44 
45  alph_ = stateAlphabet;
46 }
47 
48 vector<size_t> AbstractHmmTransitionMatrix::sample(size_t size) const
49 {
50  vector<size_t> vres;
51  if (size == 0)
52  return vres;
53 
54  size_t nbStates = hmmStateAlphabet().getNumberOfStates();
55 
56  // update pij_
57  getPij();
58 
59  size_t sta = 0, stb;
61 
62  for (size_t i = 0; i < nbStates; ++i)
63  {
64  prob -= eqFreq_[i];
65  if (prob < 0)
66  {
67  sta = i;
68  break;
69  }
70  }
71 
72  vres.push_back(sta);
73 
74  for (size_t pos = 1; pos < size; pos++)
75  {
77 
78  const vector<double>& row = pij_.getRow(sta);
79 
80  for (size_t i = 0; i < nbStates; ++i)
81  {
82  prob -= row[i];
83  if (prob < 0)
84  {
85  stb = i;
86  break;
87  }
88  }
89  vres.push_back(stb);
90  sta = stb;
91  }
92  return vres;
93 }
AbstractHmmTransitionMatrix(std::shared_ptr< const HmmStateAlphabet > alph, const std::string &prefix="")
const HmmStateAlphabet & hmmStateAlphabet() const
STL namespace.
virtual const Matrix< double > & getPij() const =0
Get all transition probabilities as a matrix.
Partial implementation of HmmTransitionMatrix.
static double giveRandomNumberBetweenZeroAndEntry(double entry)
Get a double random value (between 0 and specified range).
Definition: RandomTools.h:78
std::shared_ptr< const HmmStateAlphabet > alph_
std::vector< size_t > sample(size_t size) const
sampling of a sequence of states. Starting point is sampled from the equilibrium distribution.
virtual size_t getNumberOfStates() const =0
const std::vector< Scalar > & getRow(size_t i) const
Definition: Matrix.h:159
void setHmmStateAlphabet(std::shared_ptr< const HmmStateAlphabet > stateAlphabet)
Set the new hidden state alphabet.
AbstractHmmTransitionMatrix & operator=(const AbstractHmmTransitionMatrix &hptm)
Exception thrown when an unvalid alphabet is specified.
Definition: HmmExceptions.h:35