Home  · Classes  · Annotated Classes  · Modules  · Members  · Namespaces  · Related Pages

HiddenMarkovModel.h

Go to the documentation of this file.
00001 // --------------------------------------------------------------------------
00002 //                   OpenMS -- Open-Source Mass Spectrometry
00003 // --------------------------------------------------------------------------
00004 // Copyright The OpenMS Team -- Eberhard Karls University Tuebingen,
00005 // ETH Zurich, and Freie Universitaet Berlin 2002-2012.
00006 //
00007 // This software is released under a three-clause BSD license:
00008 //  * Redistributions of source code must retain the above copyright
00009 //    notice, this list of conditions and the following disclaimer.
00010 //  * Redistributions in binary form must reproduce the above copyright
00011 //    notice, this list of conditions and the following disclaimer in the
00012 //    documentation and/or other materials provided with the distribution.
00013 //  * Neither the name of any author or any participating institution
00014 //    may be used to endorse or promote products derived from this software
00015 //    without specific prior written permission.
00016 // For a full list of authors, refer to the file AUTHORS.
00017 // --------------------------------------------------------------------------
00018 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00019 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00020 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00021 // ARE DISCLAIMED. IN NO EVENT SHALL ANY OF THE AUTHORS OR THE CONTRIBUTING
00022 // INSTITUTIONS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
00023 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
00024 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
00025 // OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
00026 // WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
00027 // OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
00028 // ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00029 //
00030 // --------------------------------------------------------------------------
00031 // $Maintainer: Andreas Bertsch $
00032 // $Authors: Andreas Bertsch $
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     //void buildSynonyms();
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     // transition probs
00278     Map<HMMState *, Map<HMMState *, DoubleReal> > trans_;
00279 
00280     // transition prob counts
00281     Map<HMMState *, Map<HMMState *, DoubleReal> > count_trans_;
00282 
00283     Map<HMMState *, Map<HMMState *, std::vector<DoubleReal> > > count_trans_all_;
00284 
00285     // all transition probs of all training steps (for model checking)
00286     Map<HMMState *, Map<HMMState *, std::vector<DoubleReal> > > train_count_trans_all_;
00287 
00288     // number of training steps of the transitions
00289     Map<HMMState *, Map<HMMState *, Size> > training_steps_count_;
00290 
00291     // forward variables
00292     Map<HMMState *, DoubleReal> forward_;
00293 
00294     // backward variables
00295     Map<HMMState *, DoubleReal> backward_;
00296 
00297     // name to state Mapping
00298     Map<String, HMMState *> name_to_state_;
00299 
00300     // emission probabilities
00301     Map<HMMState *, DoubleReal> train_emission_prob_;
00302 
00303     // initial transition probabilities
00304     Map<HMMState *, DoubleReal> init_prob_;
00305 
00306     // all states of the HMM
00307     std::set<HMMState *> states_;
00308 
00309     // trained transitions
00310     std::set<std::pair<HMMState *, HMMState *> > trained_trans_;
00311 
00312     // synonym transitions Mapping
00313     Map<String, Map<String, std::pair<String, String> > > synonym_trans_names_;
00314 
00315     // synonym transitions
00316     Map<HMMState *, Map<HMMState *, std::pair<HMMState *, HMMState *> > > synonym_trans_;
00317 
00318     // transitions which are enabled
00319     Map<HMMState *, std::set<HMMState *> > enabled_trans_;
00320 
00321     // pseudocounts used in this instance
00322     DoubleReal pseudo_counts_;
00323 
00324     // copy all the stuff from one HMM to this
00325     void copy_(const HiddenMarkovModel & source);
00326 
00327     StringList var_modifications_;
00328   };
00329 }
00330 #endif

OpenMS / TOPP release 1.10.0 Documentation generated on Thu Mar 7 2013 09:42:40 using doxygen 1.7.1