Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036 #ifndef OPENMS_ANALYSIS_ID_HIDDENMARKOVMODEL_H
00037 #define OPENMS_ANALYSIS_ID_HIDDENMARKOVMODEL_H
00038
00039 #include <vector>
00040 #include <set>
00041
00042 #include <OpenMS/DATASTRUCTURES/Map.h>
00043 #include <OpenMS/CONCEPT/Types.h>
00044 #include <OpenMS/DATASTRUCTURES/StringList.h>
00045 #include <OpenMS/DATASTRUCTURES/String.h>
00046
00047 #include <utility>
00048
00049 namespace OpenMS
00050 {
00054 class OPENMS_DLLAPI HMMState
00055 {
00056 public:
00057
00061
00062 HMMState();
00063
00065 HMMState(const HMMState & state);
00066
00068 HMMState(const String & name, bool hidden = true);
00069
00071 virtual ~HMMState();
00073
00075 HMMState & operator=(const HMMState &);
00076
00080
00081 void setName(const String & name);
00082
00084 const String & getName() const;
00085
00087 void setHidden(bool hidden);
00088
00090 bool isHidden() const;
00091
00093 void addPredecessorState(HMMState * state);
00094
00096 void deletePredecessorState(HMMState * state);
00097
00099 void addSuccessorState(HMMState * state);
00100
00102 void deleteSuccessorState(HMMState * state);
00103
00105 const std::set<HMMState *> & getPredecessorStates() const;
00106
00108 const std::set<HMMState *> & getSuccessorStates() const;
00110
00111 protected:
00112
00114 bool hidden_;
00115
00117 String name_;
00118
00120 std::set<HMMState *> pre_states_;
00121
00123 std::set<HMMState *> succ_states_;
00124 };
00125
00126
00134 class OPENMS_DLLAPI HiddenMarkovModel
00135 {
00136 public:
00137
00141
00142 HiddenMarkovModel();
00143
00145 HiddenMarkovModel(const HiddenMarkovModel & hmm_new);
00146
00148 virtual ~HiddenMarkovModel();
00150
00152 HiddenMarkovModel & operator=(const HiddenMarkovModel &);
00153
00162 void writeGraphMLFile(const String & filename);
00163
00165 void write(std::ostream & out) const;
00166
00168 DoubleReal getTransitionProbability(const String & s1, const String & s2) const;
00169
00171 void setTransitionProbability(const String & s1, const String & s2, DoubleReal prob);
00172
00174 Size getNumberOfStates() const;
00175
00177 void addNewState(HMMState * state);
00178
00180 void addNewState(const String & name);
00181
00183 void addSynonymTransition(const String & name1, const String & name2, const String & synonym1, const String & synonym2);
00184
00186 void evaluate();
00187
00189 void train();
00190
00192 void setInitialTransitionProbability(const String & state, DoubleReal prob);
00193
00195 void clearInitialTransitionProbabilities();
00196
00198 void setTrainingEmissionProbability(const String & state, DoubleReal prob);
00199
00201 void clearTrainingEmissionProbabilities();
00202
00204 void enableTransition(const String & s1, const String & s2);
00205
00207 void disableTransition(const String & s1, const String & s2);
00208
00210 void disableTransitions();
00211
00213 void calculateEmissionProbabilities(Map<HMMState *, DoubleReal> & emission_probs);
00214
00216 void dump();
00217
00219 void forwardDump();
00220
00222
00223
00225 void estimateUntrainedTransitions();
00226
00228 HMMState * getState(const String & name);
00229
00231 const HMMState * getState(const String & name) const;
00232
00234 void clear();
00235
00237 void setPseudoCounts(DoubleReal pseudo_counts);
00238
00240 DoubleReal getPseudoCounts() const;
00241
00242 void setVariableModifications(const StringList & modifications);
00244
00245 protected:
00246
00248 void disableTransition_(HMMState * s1, HMMState * s2);
00249
00251 void enableTransition_(HMMState * s1, HMMState * s2);
00252
00254 void setTrainingEmissionProbability_(HMMState * state, DoubleReal prob);
00255
00257 void setTransitionProbability_(HMMState * s1, HMMState * s2, DoubleReal prob);
00258
00260 DoubleReal getTransitionProbability_(HMMState * s1, HMMState * s2) const;
00261
00262
00264 void calculateForwardPart_();
00265
00267 void calculateBackwardPart_();
00268
00270 DoubleReal getForwardVariable_(HMMState *);
00271
00273 DoubleReal getBackwardVariable_(HMMState *);
00274
00275 private:
00276
00277
00278 Map<HMMState *, Map<HMMState *, DoubleReal> > trans_;
00279
00280
00281 Map<HMMState *, Map<HMMState *, DoubleReal> > count_trans_;
00282
00283 Map<HMMState *, Map<HMMState *, std::vector<DoubleReal> > > count_trans_all_;
00284
00285
00286 Map<HMMState *, Map<HMMState *, std::vector<DoubleReal> > > train_count_trans_all_;
00287
00288
00289 Map<HMMState *, Map<HMMState *, Size> > training_steps_count_;
00290
00291
00292 Map<HMMState *, DoubleReal> forward_;
00293
00294
00295 Map<HMMState *, DoubleReal> backward_;
00296
00297
00298 Map<String, HMMState *> name_to_state_;
00299
00300
00301 Map<HMMState *, DoubleReal> train_emission_prob_;
00302
00303
00304 Map<HMMState *, DoubleReal> init_prob_;
00305
00306
00307 std::set<HMMState *> states_;
00308
00309
00310 std::set<std::pair<HMMState *, HMMState *> > trained_trans_;
00311
00312
00313 Map<String, Map<String, std::pair<String, String> > > synonym_trans_names_;
00314
00315
00316 Map<HMMState *, Map<HMMState *, std::pair<HMMState *, HMMState *> > > synonym_trans_;
00317
00318
00319 Map<HMMState *, std::set<HMMState *> > enabled_trans_;
00320
00321
00322 DoubleReal pseudo_counts_;
00323
00324
00325 void copy_(const HiddenMarkovModel & source);
00326
00327 StringList var_modifications_;
00328 };
00329 }
00330 #endif