ToPS
GeneralizedPairHiddenMarkovModel.hpp
00001 #ifndef GENERALIZED_PAIR_HIDDEN_MARKOV_MODEL
00002 #define GENERALIZED_PAIR_HIDDEN_MARKOV_MODEL
00003 
00004 #include "HiddenMarkovModel.hpp"
00005 #include "PairHiddenMarkovModel.hpp"
00006 #include "ProbabilisticModel.hpp"
00007 #include "PairDecodableModel.hpp"
00008 #include "ProbabilisticModelCreatorClient.hpp"
00009 #include "DecodableModel.hpp"
00010 #include "Sequence.hpp"
00011 #include "Alphabet.hpp"
00012 #include "ContextTree.hpp"
00013 #include "Symbol.hpp"
00014 
00015 #include "PairHiddenMarkovModelCreator.hpp"
00016 #include "util.hpp"
00017 #include <cstdarg>
00018 #include <vector>
00019 
00020 namespace tops{
00021 
00022   class GPHMMState: public PairDecodableState {
00023 
00024   private:
00025     int _maxEmissionSeq1, _maxEmissionSeq2, _minEmissionSeq1, _minEmissionSeq2;
00026     ProbabilisticModelPtr _duration;
00027 
00028   public:
00029     GPHMMState(){}
00030 
00031     GPHMMState(int id,
00032          SymbolPtr name,
00033          ProbabilisticModelPtr emission,
00034          DiscreteIIDModelPtr transitions,
00035          ProbabilisticModelPtr duration,
00036          IntVector iTransitions,
00037          IntVector oTransitions,
00038          int maxSeq1, int minSeq1,
00039          int maxSeq2, int minSeq2)
00040     {
00041       _id = id;
00042       _name = name;
00043       _emission = emission;
00044       _transitions = transitions;
00045       _duration = duration;
00046       _incomingTransitions = iTransitions;
00047       _outgoingTransitions = oTransitions;
00048       _maxEmissionSeq1 = maxSeq1;
00049       _minEmissionSeq1 = minSeq1;
00050       _maxEmissionSeq2 = maxSeq2;
00051       _minEmissionSeq2 = minSeq2;
00052     }
00053 
00054     int maxSeq1(){
00055       return _maxEmissionSeq1;
00056     }
00057 
00058     int minSeq1(){
00059       return _minEmissionSeq1;
00060     }
00061 
00062     int maxSeq2(){
00063       return _maxEmissionSeq2;
00064     }
00065     int minSeq2(){
00066       return _minEmissionSeq2;
00067     }
00068 
00069     ProbabilisticModelPtr duration(){
00070       return _duration;
00071     }
00072 
00073     /*double emission(Sequence &s1, Sequence &s2, int i, int j, int ne1, int ne2, int gap_id){
00074       if(maxEmissionSeq1 == 1 && maxEmissionSeq2 == 1)
00075   return log_probability_of_pair(s1[i-1], s2[j-1]);
00076       if(maxEmissionSeq1 == 1 && maxEmissionSeq2 == 0)
00077   return log_probability_of_pair(s1[i-1], gap_id);
00078       if(maxEmissionSeq1 == 1 && maxEmissionSeq2 == 1)
00079   return log_probability_of_pair(gap_id, s2[j-1]);
00080       vector<Matrix> f;
00081       return _emission->pairDecodable()->forward(sub_seq(s1, i-ne1-1, i-1),sub_seq(s1, i-ne1-1, i-1),f);
00082       }*/
00083 
00084   };
00085 
00086   typedef boost::shared_ptr <GPHMMState> GPHMMStatePtr;
00087 
00088   class GeneralizedPairHiddenMarkovModel : public PairDecodableModel{
00089 
00090   private:
00091     DiscreteIIDModelPtr _initialProbabilities;
00092     int _end_id, _gap_id;
00093 
00094   public:
00095 
00096     GeneralizedPairHiddenMarkovModel() {};
00097 
00098     virtual void initialize(const ProbabilisticModelParameters & par) ;
00099 
00100     virtual std::string model_name() const {
00101       return "GeneralizedPairHiddenMarkovModel";
00102     }
00103 
00104     virtual PairDecodableModel * pairDecodable() {
00105       return this;
00106     }
00107 
00108     void setInitialProbabilities(DiscreteIIDModelPtr init){
00109       _initialProbabilities = init;
00110     }
00111 
00112     DiscreteIIDModelPtr initialProbabilities(){
00113       return _initialProbabilities;
00114     }
00115 
00116     //Forward, Backward and Viterbi algorithms. We assume there are no cycles of silent states.
00117     double forward(const Sequence & seq1, const Sequence & seq2, vector<Matrix> &a);
00118 
00119     virtual double backward(const Sequence & seq1, const Sequence & seq2, vector<Matrix> &a);
00120 
00121     virtual double viterbi(const Sequence & seq1, const Sequence & seq2, Sequence & path, Sequence & al1, Sequence & al2, vector<Matrix> &a);
00122 
00123     /*virtual void posteriorProbabilities (const Sequence &seq1, const Sequence &seq2, vector<Matrix> & probabilities);
00124 
00125     virtual void generateSequence(Sequence &seq1, Sequence &seq2, Sequence &path);
00126 
00127     virtual void trainBaumWelch(SequenceList & sample1, SequenceList & sample2, int maxiterations, double diff_threshold);*/
00128 
00129     /*virtual ProbabilisticModelCreatorPtr getFactory() const {
00130       return GeneralizedPairHiddenMarkovModelCreatorPtr(new GeneralizedPairHiddenMarkovModelCreator());
00131       }*/
00132 
00133     //std::string str () const;
00134 
00135 
00136   };
00137   typedef boost::shared_ptr <GeneralizedPairHiddenMarkovModel> GeneralizedPairHiddenMarkovModelPtr;
00138 
00139 };
00140 #endif