OpenMS
HiddenMarkovModel.h
Go to the documentation of this file.
1 // Copyright (c) 2002-2023, The OpenMS Team -- EKU Tuebingen, ETH Zurich, and FU Berlin
2 // SPDX-License-Identifier: BSD-3-Clause
3 //
4 // --------------------------------------------------------------------------
5 // $Maintainer: Timo Sachsenberg $
6 // $Authors: Andreas Bertsch $
7 // --------------------------------------------------------------------------
8 
9 
10 #pragma once
11 
12 #include <vector>
13 #include <set>
14 
15 #include <OpenMS/CONCEPT/Types.h>
18 
19 #include <utility>
20 #include <map>
21 
22 namespace OpenMS
23 {
27  class OPENMS_DLLAPI HMMState
28  {
29 public:
30 
36 
38  HMMState(const HMMState & state);
39 
41  HMMState(const String & name, bool hidden = true);
42 
44  virtual ~HMMState();
46 
49 
54  void setName(const String & name);
55 
57  const String & getName() const;
58 
60  void setHidden(bool hidden);
61 
63  bool isHidden() const;
64 
67 
70 
72  void addSuccessorState(HMMState * state);
73 
76 
78  const std::set<HMMState *> & getPredecessorStates() const;
79 
81  const std::set<HMMState *> & getSuccessorStates() const;
83 
84 protected:
85 
87  bool hidden_;
88 
91 
93  std::set<HMMState *> pre_states_;
94 
96  std::set<HMMState *> succ_states_;
97  };
98 
99 
107  class OPENMS_DLLAPI HiddenMarkovModel
108  {
109 public:
110 
116 
119 
123 
126 
135  void writeGraphMLFile(const String & filename);
136 
138  void write(std::ostream & out) const;
139 
141  double getTransitionProbability(const String & s1, const String & s2) const;
142 
144  void setTransitionProbability(const String & s1, const String & s2, double prob);
145 
148 
150  void addNewState(HMMState * state);
151 
153  void addNewState(const String & name);
154 
156  void addSynonymTransition(const String & name1, const String & name2, const String & synonym1, const String & synonym2);
157 
159  void evaluate();
160 
162  void train();
163 
165  void setInitialTransitionProbability(const String & state, double prob);
166 
169 
171  void setTrainingEmissionProbability(const String & state, double prob);
172 
175 
177  void enableTransition(const String & s1, const String & s2);
178 
180  void disableTransition(const String & s1, const String & s2);
181 
184 
186  void calculateEmissionProbabilities(std::map<HMMState *, double> & emission_probs);
187 
189  void dump();
190 
192  void forwardDump();
193 
195  //void buildSynonyms();
196 
199 
201  HMMState * getState(const String & name);
202 
204  const HMMState * getState(const String & name) const;
205 
207  void clear();
208 
210  void setPseudoCounts(double pseudo_counts);
211 
213  double getPseudoCounts() const;
214 
215  void setVariableModifications(const StringList & modifications);
217 
218 protected:
219 
222 
225 
227  void setTrainingEmissionProbability_(HMMState * state, double prob);
228 
230  void setTransitionProbability_(HMMState * s1, HMMState * s2, double prob);
231 
233  double getTransitionProbability_(HMMState * s1, HMMState * s2) const;
234 
235 
238 
241 
244 
247 
248 private:
249 
250  // transition probs
251  std::map<HMMState *, std::map<HMMState *, double> > trans_;
252 
253  // transition prob counts
254  std::map<HMMState *, std::map<HMMState *, double> > count_trans_;
255 
256  std::map<HMMState *, std::map<HMMState *, std::vector<double> > > count_trans_all_;
257 
258  // all transition probs of all training steps (for model checking)
259  std::map<HMMState *, std::map<HMMState *, std::vector<double> > > train_count_trans_all_;
260 
261  // number of training steps of the transitions
262  std::map<HMMState *, std::map<HMMState *, Size> > training_steps_count_;
263 
264  // forward variables
265  std::map<HMMState *, double> forward_;
266 
267  // backward variables
268  std::map<HMMState *, double> backward_;
269 
270  // name to state Mapping
271  std::map<String, HMMState *> name_to_state_;
272 
273  // emission probabilities
274  std::map<HMMState *, double> train_emission_prob_;
275 
276  // initial transition probabilities
277  std::map<HMMState *, double> init_prob_;
278 
279  // all states of the HMM
280  std::set<HMMState *> states_;
281 
282  // trained transitions
283  std::set<std::pair<HMMState *, HMMState *> > trained_trans_;
284 
285  // synonym transitions Mapping
286  std::map<String, std::map<String, std::pair<String, String> > > synonym_trans_names_;
287 
288  // synonym transitions
289  std::map<HMMState *, std::map<HMMState *, std::pair<HMMState *, HMMState *> > > synonym_trans_;
290 
291  // transitions which are enabled
292  std::map<HMMState *, std::set<HMMState *> > enabled_trans_;
293 
294  // pseudocounts used in this instance
296 
297  // copy all the stuff from one HMM to this
298  void copy_(const HiddenMarkovModel & source);
299 
301  };
302 }
Hidden Markov Model State class for the Hidden Markov Model.
Definition: HiddenMarkovModel.h:28
void deletePredecessorState(HMMState *state)
deletes the given predecessor state from the list
std::set< HMMState * > succ_states_
Definition: HiddenMarkovModel.h:96
void addPredecessorState(HMMState *state)
adds the given predecessor state to the list
HMMState & operator=(const HMMState &)
HMMState(const HMMState &state)
copy constructor
bool isHidden() const
returns true if the state is hidden
String name_
Definition: HiddenMarkovModel.h:90
void setName(const String &name)
sets the name of the state
const std::set< HMMState * > & getPredecessorStates() const
returns the predecessor states of the state
std::set< HMMState * > pre_states_
Definition: HiddenMarkovModel.h:93
HMMState()
default constructor
HMMState(const String &name, bool hidden=true)
constructor with name and visibility option
void addSuccessorState(HMMState *state)
add the given successor state to the list
const String & getName() const
returns the name of the state
const std::set< HMMState * > & getSuccessorStates() const
return the successor states of the state
void setHidden(bool hidden)
sets the hidden property to the state
bool hidden_
Definition: HiddenMarkovModel.h:87
void deleteSuccessorState(HMMState *state)
deletes the given successor state from the list
virtual ~HMMState()
destructor
Hidden Markov Model implementation of PILIS.
Definition: HiddenMarkovModel.h:108
void clearInitialTransitionProbabilities()
clears the initial probabilities
void setTransitionProbability(const String &s1, const String &s2, double prob)
sets the transition probability of the given state names to prob
void setTransitionProbability_(HMMState *s1, HMMState *s2, double prob)
sets the transition probability of the given states to prob
void enableTransition(const String &s1, const String &s2)
enables a transition; adds s1 to the predecessor list of s2 and s2 to the successor list of s1
void estimateUntrainedTransitions()
builds a synonyms structure, needed when synonyms are used
std::map< HMMState *, double > forward_
Definition: HiddenMarkovModel.h:265
HiddenMarkovModel & operator=(const HiddenMarkovModel &)
assignment operator
StringList var_modifications_
Definition: HiddenMarkovModel.h:300
void disableTransitions()
disables all transitions
void addNewState(HMMState *state)
registers a new state to the HMM
void clearTrainingEmissionProbabilities()
clear the emission probabilities
std::map< HMMState *, double > train_emission_prob_
Definition: HiddenMarkovModel.h:274
void setPseudoCounts(double pseudo_counts)
sets the pseudo count that are added instead of zero
void disableTransition(const String &s1, const String &s2)
disables the transition; deletes the nodes from the predecessor/successor list respectively
std::set< std::pair< HMMState *, HMMState * > > trained_trans_
Definition: HiddenMarkovModel.h:283
void disableTransition_(HMMState *s1, HMMState *s2)
disables the transition; deletes the nodes from the predecessor/successor list respectively
void train()
trains the HMM; initial probabilities and emission probabilities of the emitting states should be set
virtual ~HiddenMarkovModel()
destructor
void evaluate()
evaluate the HMM, estimates the transition probabilities from the training
double getBackwardVariable_(HMMState *)
returns the backward variable
std::map< String, std::map< String, std::pair< String, String > > > synonym_trans_names_
Definition: HiddenMarkovModel.h:286
void calculateEmissionProbabilities(std::map< HMMState *, double > &emission_probs)
calculates the emission probabilities of the HMM (of course only of the non-hidden states)
void write(std::ostream &out) const
writes the HMM into an outstream
std::map< HMMState *, std::map< HMMState *, double > > count_trans_
Definition: HiddenMarkovModel.h:254
std::map< HMMState *, std::map< HMMState *, Size > > training_steps_count_
Definition: HiddenMarkovModel.h:262
void addNewState(const String &name)
registers a new state to the HMM
std::map< String, HMMState * > name_to_state_
Definition: HiddenMarkovModel.h:271
std::map< HMMState *, std::map< HMMState *, std::vector< double > > > train_count_trans_all_
Definition: HiddenMarkovModel.h:259
void copy_(const HiddenMarkovModel &source)
double getTransitionProbability(const String &s1, const String &s2) const
returns the transition probability of the given state names
void calculateForwardPart_()
performs the forward algorithm
double getPseudoCounts() const
returns the pseudo counts
void enableTransition_(HMMState *s1, HMMState *s2)
enables a transition; adds s1 to the predecessor list of s2 and s2 to the successor list of s1
HiddenMarkovModel(const HiddenMarkovModel &hmm_new)
copy constructor
const HMMState * getState(const String &name) const
returns the state with the given name
double pseudo_counts_
Definition: HiddenMarkovModel.h:295
void setInitialTransitionProbability(const String &state, double prob)
sets the initial transition probability of the given state to prob
HMMState * getState(const String &name)
returns the state with the given name
std::map< HMMState *, double > backward_
Definition: HiddenMarkovModel.h:268
HiddenMarkovModel()
default constructor
std::map< HMMState *, std::map< HMMState *, std::pair< HMMState *, HMMState * > > > synonym_trans_
Definition: HiddenMarkovModel.h:289
std::map< HMMState *, std::set< HMMState * > > enabled_trans_
Definition: HiddenMarkovModel.h:292
double getTransitionProbability_(HMMState *s1, HMMState *s2) const
returns the transition probability of the given states
std::set< HMMState * > states_
Definition: HiddenMarkovModel.h:280
void clear()
clears all data
void addSynonymTransition(const String &name1, const String &name2, const String &synonym1, const String &synonym2)
add a new synonym transition to the given state names
std::map< HMMState *, std::map< HMMState *, std::vector< double > > > count_trans_all_
Definition: HiddenMarkovModel.h:256
void dump()
writes some stats to cerr
void forwardDump()
writes some info of the forward "matrix" to cerr
void calculateBackwardPart_()
performs the backward algorithm
double getForwardVariable_(HMMState *)
returns the forward variable
void writeGraphMLFile(const String &filename)
writes the HMM into a file in GraphML format
Size getNumberOfStates() const
return the number of states
std::map< HMMState *, std::map< HMMState *, double > > trans_
Definition: HiddenMarkovModel.h:251
void setTrainingEmissionProbability(const String &state, double prob)
sets the emission probability of the given state to prob
void setTrainingEmissionProbability_(HMMState *state, double prob)
sets the emission probability of the given state to prob
void setVariableModifications(const StringList &modifications)
std::map< HMMState *, double > init_prob_
Definition: HiddenMarkovModel.h:277
A more convenient string class.
Definition: String.h:34
size_t Size
Size type e.g. used as variable which can hold result of size()
Definition: Types.h:101
std::vector< String > StringList
Vector of String.
Definition: ListUtils.h:44
Main OpenMS namespace.
Definition: FeatureDeconvolution.h:22