OpenMS  2.8.0
HiddenMarkovModel.h
Go to the documentation of this file.
1 // --------------------------------------------------------------------------
2 // OpenMS -- Open-Source Mass Spectrometry
3 // --------------------------------------------------------------------------
4 // Copyright The OpenMS Team -- Eberhard Karls University Tuebingen,
5 // ETH Zurich, and Freie Universitaet Berlin 2002-2021.
6 //
7 // This software is released under a three-clause BSD license:
8 // * Redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above copyright
11 // notice, this list of conditions and the following disclaimer in the
12 // documentation and/or other materials provided with the distribution.
13 // * Neither the name of any author or any participating institution
14 // may be used to endorse or promote products derived from this software
15 // without specific prior written permission.
16 // For a full list of authors, refer to the file AUTHORS.
17 // --------------------------------------------------------------------------
18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 // ARE DISCLAIMED. IN NO EVENT SHALL ANY OF THE AUTHORS OR THE CONTRIBUTING
22 // INSTITUTIONS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
25 // OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
26 // WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
27 // OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
28 // ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 //
30 // --------------------------------------------------------------------------
31 // $Maintainer: Timo Sachsenberg $
32 // $Authors: Andreas Bertsch $
33 // --------------------------------------------------------------------------
34 
35 
36 #pragma once
37 
38 #include <vector>
39 #include <set>
40 
42 #include <OpenMS/CONCEPT/Types.h>
45 
46 #include <utility>
47 
48 namespace OpenMS
49 {
53  class OPENMS_DLLAPI HMMState
54  {
55 public:
56 
62 
64  HMMState(const HMMState & state);
65 
67  HMMState(const String & name, bool hidden = true);
68 
70  virtual ~HMMState();
72 
75 
80  void setName(const String & name);
81 
83  const String & getName() const;
84 
86  void setHidden(bool hidden);
87 
89  bool isHidden() const;
90 
93 
96 
98  void addSuccessorState(HMMState * state);
99 
102 
104  const std::set<HMMState *> & getPredecessorStates() const;
105 
107  const std::set<HMMState *> & getSuccessorStates() const;
109 
110 protected:
111 
113  bool hidden_;
114 
117 
119  std::set<HMMState *> pre_states_;
120 
122  std::set<HMMState *> succ_states_;
123  };
124 
125 
133  class OPENMS_DLLAPI HiddenMarkovModel
134  {
135 public:
136 
142 
145 
149 
152 
161  void writeGraphMLFile(const String & filename);
162 
164  void write(std::ostream & out) const;
165 
167  double getTransitionProbability(const String & s1, const String & s2) const;
168 
170  void setTransitionProbability(const String & s1, const String & s2, double prob);
171 
174 
176  void addNewState(HMMState * state);
177 
179  void addNewState(const String & name);
180 
182  void addSynonymTransition(const String & name1, const String & name2, const String & synonym1, const String & synonym2);
183 
185  void evaluate();
186 
188  void train();
189 
191  void setInitialTransitionProbability(const String & state, double prob);
192 
195 
197  void setTrainingEmissionProbability(const String & state, double prob);
198 
201 
203  void enableTransition(const String & s1, const String & s2);
204 
206  void disableTransition(const String & s1, const String & s2);
207 
210 
213 
215  void dump();
216 
218  void forwardDump();
219 
221  //void buildSynonyms();
222 
225 
227  HMMState * getState(const String & name);
228 
230  const HMMState * getState(const String & name) const;
231 
233  void clear();
234 
236  void setPseudoCounts(double pseudo_counts);
237 
239  double getPseudoCounts() const;
240 
241  void setVariableModifications(const StringList & modifications);
243 
244 protected:
245 
248 
251 
253  void setTrainingEmissionProbability_(HMMState * state, double prob);
254 
256  void setTransitionProbability_(HMMState * s1, HMMState * s2, double prob);
257 
259  double getTransitionProbability_(HMMState * s1, HMMState * s2) const;
260 
261 
264 
267 
270 
273 
274 private:
275 
276  // transition probs
278 
279  // transition prob counts
281 
283 
284  // all transition probs of all training steps (for model checking)
286 
287  // number of training steps of the transitions
289 
290  // forward variables
292 
293  // backward variables
295 
296  // name to state Mapping
298 
299  // emission probabilities
301 
302  // initial transition probabilities
304 
305  // all states of the HMM
306  std::set<HMMState *> states_;
307 
308  // trained transitions
309  std::set<std::pair<HMMState *, HMMState *> > trained_trans_;
310 
311  // synonym transitions Mapping
313 
314  // synonym transitions
316 
317  // transitions which are enabled
319 
320  // pseudocounts used in this instance
322 
323  // copy all the stuff from one HMM to this
324  void copy_(const HiddenMarkovModel & source);
325 
327  };
328 }
Hidden Markov Model State class for the Hidden Markov Model.
Definition: HiddenMarkovModel.h:54
void deletePredecessorState(HMMState *state)
deletes the given predecessor state from the list
std::set< HMMState * > succ_states_
Definition: HiddenMarkovModel.h:122
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:116
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:119
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:113
void deleteSuccessorState(HMMState *state)
deletes the given successor state from the list
virtual ~HMMState()
destructor
Hidden Markov Model implementation of PILIS.
Definition: HiddenMarkovModel.h:134
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
Map< HMMState *, Map< HMMState *, double > > count_trans_
Definition: HiddenMarkovModel.h:280
HiddenMarkovModel & operator=(const HiddenMarkovModel &)
assignment operator
StringList var_modifications_
Definition: HiddenMarkovModel.h:326
void disableTransitions()
disables all transitions
void addNewState(HMMState *state)
registers a new state to the HMM
void clearTrainingEmissionProbabilities()
clear the emission probabilities
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:309
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
Map< HMMState *, Map< HMMState *, double > > trans_
Definition: HiddenMarkovModel.h:277
void evaluate()
evaluate the HMM, estimates the transition probabilities from the training
Map< HMMState *, double > train_emission_prob_
Definition: HiddenMarkovModel.h:300
double getBackwardVariable_(HMMState *)
returns the backward variable
void write(std::ostream &out) const
writes the HMM into an outstream
Map< HMMState *, Map< HMMState *, std::pair< HMMState *, HMMState * > > > synonym_trans_
Definition: HiddenMarkovModel.h:315
void addNewState(const String &name)
registers a new state to the HMM
Map< HMMState *, Map< HMMState *, std::vector< double > > > train_count_trans_all_
Definition: HiddenMarkovModel.h:285
void copy_(const HiddenMarkovModel &source)
double getTransitionProbability(const String &s1, const String &s2) const
returns the transition probability of the given state names
Map< HMMState *, double > backward_
Definition: HiddenMarkovModel.h:294
void calculateForwardPart_()
performs the forward algorithm
double getPseudoCounts() const
returns the pseudo counts
Map< String, HMMState * > name_to_state_
Definition: HiddenMarkovModel.h:297
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:321
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
Map< String, Map< String, std::pair< String, String > > > synonym_trans_names_
Definition: HiddenMarkovModel.h:312
HiddenMarkovModel()
default constructor
Map< HMMState *, Map< HMMState *, Size > > training_steps_count_
Definition: HiddenMarkovModel.h:288
double getTransitionProbability_(HMMState *s1, HMMState *s2) const
returns the transition probability of the given states
std::set< HMMState * > states_
Definition: HiddenMarkovModel.h:306
Map< HMMState *, Map< HMMState *, std::vector< double > > > count_trans_all_
Definition: HiddenMarkovModel.h:282
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
void dump()
writes some stats to cerr
void forwardDump()
writes some info of the forward "matrix" to cerr
Map< HMMState *, std::set< HMMState * > > enabled_trans_
Definition: HiddenMarkovModel.h:318
Map< HMMState *, double > init_prob_
Definition: HiddenMarkovModel.h:303
void calculateBackwardPart_()
performs the backward algorithm
double getForwardVariable_(HMMState *)
returns the forward variable
void calculateEmissionProbabilities(Map< HMMState *, double > &emission_probs)
calculates the emission probabilities of the HMM (of course only of the non-hidden states)
void writeGraphMLFile(const String &filename)
writes the HMM into a file in GraphML format
Size getNumberOfStates() const
return the number of states
Map< HMMState *, double > forward_
Definition: HiddenMarkovModel.h:291
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)
Map class based on the STL map (containing several convenience functions)
Definition: Map.h:52
A more convenient string class.
Definition: String.h:60
size_t Size
Size type e.g. used as variable which can hold result of size()
Definition: Types.h:127
std::vector< String > StringList
Vector of String.
Definition: ListUtils.h:70
Main OpenMS namespace.
Definition: FeatureDeconvolution.h:47