ToPS
PairDecodableModel.hpp
00001 #ifndef PAIR_DECODABLE_MODEL_HPP
00002 #define PAIR_DECODABLE_MODEL_HPP
00003 
00004 
00005 #include "ProbabilisticModel.hpp"
00006 #include "Symbol.hpp"
00007 #include "Sequence.hpp"
00008 #include "util.hpp"
00009 #include <boost/shared_ptr.hpp>
00010 
00011 namespace tops {
00012 
00013   class PairDecodableState {
00014 
00015   protected:
00016 
00017     int _id;
00018     SymbolPtr _name;
00019     IntVector _incomingTransitions, _outgoingTransitions;
00020     DiscreteIIDModelPtr _transitions;
00021     ProbabilisticModelPtr _emission;
00022 
00023   public:
00024 
00025     void not_implemented(std::string method){
00026       cerr << "The state type " << state_type() << " does not implement the method: " << method << endl;
00027       exit(-1);
00028     }
00029 
00030     virtual std::string state_type(){
00031       return "PairDecodableState";
00032     }
00033 
00034     virtual int eSeq1(){
00035       not_implemented("int eSeq1()");
00036       return 0;
00037     }
00038 
00039     virtual int eSeq2(){
00040       not_implemented("int eSeq2()");
00041       return 0;
00042     }
00043 
00044     virtual int maxSeq1(){
00045       not_implemented("int maxSeq1()");
00046       return 0;
00047     }
00048 
00049     virtual int minSeq1(){
00050       not_implemented("int minSeq1()");
00051       return 0;
00052     }
00053 
00054     virtual int maxSeq2(){
00055       not_implemented("int maxSeq2()");
00056       return 0;
00057     }
00058 
00059     virtual int minSeq2(){
00060       not_implemented("int minSeq2()");
00061       return 0;
00062     }
00063 
00064     vector<int> iTransitions() const{
00065       return _incomingTransitions;
00066     }
00067 
00068     int getITransId(int i){
00069       return _incomingTransitions[i];
00070     }
00071 
00072     vector<int> oTransitions(){
00073       return _outgoingTransitions;
00074     }
00075 
00076     int getOTransId(int i){
00077       return _outgoingTransitions[i];
00078     }
00079 
00080     DiscreteIIDModelPtr transitions() {
00081       return _transitions;
00082     }
00083 
00084     virtual ProbabilisticModelPtr emission(){
00085       return _emission;
00086     }
00087 
00088     virtual ProbabilisticModelPtr duration(){
00089       not_implemented("ProbabilisticModelPtr duration()");
00090       ProbabilisticModelPtr a = ProbabilisticModelPtr(new ProbabilisticModel());
00091       return a;
00092     }
00093 
00094     SymbolPtr getName() const {
00095       return _name;
00096     }
00097 
00098     int getId() {
00099       return _id;
00100     }
00101   };
00102 
00103   typedef boost::shared_ptr <PairDecodableState> PairDecodableStatePtr;
00104 
00106   class PairDecodableModel : public ProbabilisticModel  {
00107 
00108   protected:
00109     std::vector<PairDecodableStatePtr> _states;
00110     AlphabetPtr _state_names;
00111 
00112   public:
00113     PairDecodableModel(){};
00114 
00115     void not_implemented(std::string method) const{
00116       cerr << "The model " << model_name() << " does not implement the method: " << method << endl;
00117       exit(-1);
00118     }
00119 
00120     void setStates(std::vector<PairDecodableStatePtr> states, AlphabetPtr state_names){
00121       _states = states;
00122       _state_names = state_names;
00123     }
00124 
00125     void setObservationSymbols(AlphabetPtr obs){
00126       tops::ProbabilisticModel::setAlphabet(obs);
00127     }
00128 
00129     PairDecodableStatePtr getState(int id) const {
00130       return _states[id];
00131     }
00132 
00133     AlphabetPtr getStateNames() const  {
00134       return _state_names;
00135     }
00136 
00137     virtual PairDecodableModel * pairDecodable() {
00138       return this;
00139     }
00140 
00141     virtual std::string model_name() const {
00142       return "PairDecodableModel";
00143     }
00144 
00145     virtual string getStateName(int state) const{
00146       return _state_names->getSymbol(state)->name();
00147     }
00148 
00149     virtual void setSilentStates(std::vector<PairDecodableStatePtr> states){
00150       not_implemented("void setSilentStates(std::vector<PairDecodableStatePtr> states)");
00151     }
00152 
00153     virtual int getSilentId(int k) const {
00154       not_implemented("int getSilentId(int k) const");
00155       return 0;
00156     }
00157 
00158     virtual void silentStatesSort(vector<PairDecodableStatePtr> silStates){
00159       not_implemented("void silentStatesSort(vector<PairDecodableStatePtr> silStates)");
00160     }
00161 
00163     //All the methods below should be pure virtual methods in this class.
00164     virtual void initialize(const ProbabilisticModelParameters & par) = 0;
00165 
00166     std::string str () const{
00167       not_implemented("std::string str() const");
00168       return "";
00169     }
00170 
00171     virtual double forward(const Sequence & seq1, const Sequence & seq2, vector<Matrix> &a){
00172       not_implemented("double forward(const Sequence & seq1, const Sequence & seq2, vector<Matrix> &a)");
00173       return 0.0;
00174     }
00175 
00176     virtual double backward(const Sequence & seq1, const Sequence & seq2, vector<Matrix> &a){
00177       not_implemented("double backward(const Sequence & seq1, const Sequence & seq2, vector<Matrix> &a)");
00178       return 0.0;
00179     }
00180 
00181     virtual double viterbi(const Sequence & seq1, const Sequence & seq2, Sequence & path, Sequence & al1, Sequence & al2, vector<Matrix> &a){
00182       not_implemented("double viterbi(const Sequence & seq1, const Sequence & seq2, Sequence & path, Sequence & al1, Sequence & al2, vector<Matrix> &a)");
00183       return 0.0;
00184     }
00185 
00186     virtual void posteriorProbabilities (const Sequence &seq1, const Sequence &seq2, vector<Matrix> & probabilities){
00187       not_implemented("void posteriorProbabilities (const Sequence &seq1, const Sequence &seq2, vector<Matrix> & probabilities)");
00188     }
00189 
00190     virtual void generateSequence(Sequence &seq1, Sequence &seq2, Sequence &path){
00191       not_implemented("void generateSequence(Sequence &seq1, Sequence &seq2, Sequence &path)");
00192     }
00193 
00194     virtual void trainBaumWelch(SequenceList & sample1, SequenceList & sample2, int maxiterations, double diff_threshold){
00195       not_implemented("void trainBaumWelch(SequenceList & sample1, SequenceList & sample2, int maxiterations, double diff_threshold)");
00196     }
00197 
00198     virtual ProbabilisticModelCreatorPtr getFactory() const {
00199       not_implemented("ProbabilisticModelCreatorPtr getFactory() const");
00200       ProbabilisticModelCreatorPtr a;
00201       return a;
00202     }
00203 
00204   };
00205 
00206   typedef boost::shared_ptr <PairDecodableModel> PairDecodableModelPtr;
00207 }
00208 #endif