OpenMS  2.4.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-2018.
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 
60  HMMState();
62 
64  HMMState(const HMMState & state);
65 
67  HMMState(const String & name, bool hidden = true);
68 
70  virtual ~HMMState();
72 
74  HMMState & operator=(const HMMState &);
75 
79  void setName(const String & name);
81 
83  const String & getName() const;
84 
86  void setHidden(bool hidden);
87 
89  bool isHidden() const;
90 
92  void addPredecessorState(HMMState * state);
93 
95  void deletePredecessorState(HMMState * state);
96 
98  void addSuccessorState(HMMState * state);
99 
101  void deleteSuccessorState(HMMState * state);
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 
144  HiddenMarkovModel(const HiddenMarkovModel & hmm_new);
145 
147  virtual ~HiddenMarkovModel();
149 
151  HiddenMarkovModel & operator=(const HiddenMarkovModel &);
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 
173  Size getNumberOfStates() const;
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 
194  void clearInitialTransitionProbabilities();
195 
197  void setTrainingEmissionProbability(const String & state, double prob);
198 
200  void clearTrainingEmissionProbabilities();
201 
203  void enableTransition(const String & s1, const String & s2);
204 
206  void disableTransition(const String & s1, const String & s2);
207 
209  void disableTransitions();
210 
212  void calculateEmissionProbabilities(Map<HMMState *, double> & emission_probs);
213 
215  void dump();
216 
218  void forwardDump();
219 
221  //void buildSynonyms();
222 
224  void estimateUntrainedTransitions();
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 
247  void disableTransition_(HMMState * s1, HMMState * s2);
248 
250  void enableTransition_(HMMState * s1, HMMState * s2);
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 
263  void calculateForwardPart_();
264 
266  void calculateBackwardPart_();
267 
269  double getForwardVariable_(HMMState *);
270 
272  double getBackwardVariable_(HMMState *);
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 }
Map< HMMState *, double > train_emission_prob_
Definition: HiddenMarkovModel.h:300
double pseudo_counts_
Definition: HiddenMarkovModel.h:321
Map< HMMState *, double > backward_
Definition: HiddenMarkovModel.h:294
StringList var_modifications_
Definition: HiddenMarkovModel.h:326
A more convenient string class.
Definition: String.h:57
Map< HMMState *, Map< HMMState *, double > > trans_
Definition: HiddenMarkovModel.h:277
bool hidden_
Definition: HiddenMarkovModel.h:113
std::set< HMMState * > succ_states_
Definition: HiddenMarkovModel.h:122
std::set< HMMState * > pre_states_
Definition: HiddenMarkovModel.h:119
Map< HMMState *, Map< HMMState *, std::pair< HMMState *, HMMState * > > > synonym_trans_
Definition: HiddenMarkovModel.h:315
std::set< HMMState * > states_
Definition: HiddenMarkovModel.h:306
Map< String, Map< String, std::pair< String, String > > > synonym_trans_names_
Definition: HiddenMarkovModel.h:312
Main OpenMS namespace.
Definition: FeatureDeconvolution.h:46
String name_
Definition: HiddenMarkovModel.h:116
std::set< std::pair< HMMState *, HMMState * > > trained_trans_
Definition: HiddenMarkovModel.h:309
Hidden Markov Model State class for the Hidden Markov Model.
Definition: HiddenMarkovModel.h:53
Map< String, HMMState * > name_to_state_
Definition: HiddenMarkovModel.h:297
Map< HMMState *, Map< HMMState *, double > > count_trans_
Definition: HiddenMarkovModel.h:280
Map< HMMState *, double > forward_
Definition: HiddenMarkovModel.h:291
Map< HMMState *, Map< HMMState *, std::vector< double > > > train_count_trans_all_
Definition: HiddenMarkovModel.h:285
std::vector< String > StringList
Vector of String.
Definition: ListUtils.h:73
Map< HMMState *, std::set< HMMState * > > enabled_trans_
Definition: HiddenMarkovModel.h:318
size_t Size
Size type e.g. used as variable which can hold result of size()
Definition: Types.h:127
Hidden Markov Model implementation of PILIS.
Definition: HiddenMarkovModel.h:133
Map class based on the STL map (containing several convenience functions)
Definition: Map.h:50
Map< HMMState *, double > init_prob_
Definition: HiddenMarkovModel.h:303
Map< HMMState *, Map< HMMState *, std::vector< double > > > count_trans_all_
Definition: HiddenMarkovModel.h:282
Map< HMMState *, Map< HMMState *, Size > > training_steps_count_
Definition: HiddenMarkovModel.h:288