StochHMM  v0.34
Flexible Hidden Markov Model C++ Library and Application
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
state.h
Go to the documentation of this file.
1 //state.h
2 //Copyright (c) 2007-2012 Paul C Lott
3 //University of California, Davis
4 //Genome and Biomedical Sciences Facility
5 //UC Davis Genome Center
6 //Ian Korf Lab
7 //Website: www.korflab.ucdavis.edu
8 //Email: lottpaul@gmail.com
9 //
10 //Permission is hereby granted, free of charge, to any person obtaining a copy of
11 //this software and associated documentation files (the "Software"), to deal in
12 //the Software without restriction, including without limitation the rights to
13 //use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
14 //the Software, and to permit persons to whom the Software is furnished to do so,
15 //subject to the following conditions:
16 //
17 //The above copyright notice and this permission notice shall be included in all
18 //copies or substantial portions of the Software.
19 //
20 //THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 //IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
22 //FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
23 //COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
24 //IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
25 //CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 
27 
28 #ifndef STATE_H
29 #define STATE_H
30 
31 #include <string>
32 #include <vector>
33 #include "text.h"
34 #include "emm.h"
35 #include "transitions.h"
36 #include <stdint.h>
37 #include <stdlib.h>
38 #include <bitset>
39 
40 #ifndef SIZE_MAX
41 #define SIZE_MAX ((size_t)-1)
42 #endif
43 
44 //Define size of bitset. If not set in makefile then will use default 1024.
45 #ifndef STATE_MAX
46 #define STATE_MAX 1024
47 #endif
48 
49 namespace StochHMM{
50 
51  class transition;
52 
53  class state{
54  public:
55  state();
56  //state(int);
57  state(std::string&,stringList&,tracks&,weights*, StateFuncs*); //!Create state from string
58  ~state();
59 
60  friend class model;
61 
62  //ACCESSOR
63 
64  //!Get the states integer index value
65  //! \return integer
66  inline size_t getIterator(){return stateIterator;};
67 
68  //!Get the name of the state
69  //! \return std::string Name of state
70  inline std::string& getName(){return name;};
71 
72  //!Get the GFF tag to use for the state
73  //! \results std::string GFF tag
74  inline std::string& getGFF(){return gff;};
75 
76  //!Get the Label used for the state
77  //! \result std::string
78  inline std::string& getLabel(){return label;};
79 
80  //!Get all transition defined for the state
81  //!\return std::vector<transitions*>* Pointer to all transitions in state
82  inline std::vector<transition*>* getTransitions(){return transi;};
83 
84  //!Get all states that this state has transitions to
85  //! \return std::vector<state*>* Pointer to all state that are transitioned to
86  inline std::bitset<STATE_MAX>* getTo(){return &to;};
87 
88 
89 
90  //!Get all states that transition to this state
91  //!\return std::vector<state*>* Pointer to all state that transition to this state
92  inline std::bitset<STATE_MAX>* getFrom(){return &from;};
93 
94 
95  //TODO: Check that undefined return values are NULL
96  //!Get transition at index
97  //! \param iter Index to get transition for
98  //! \return transition* pointer to the transition
99  inline transition* getTrans(size_t iter){return (*transi)[iter];};
100 
101  //!Get the ending transition
102  //! \return transition* Pointer to ending transition
103  //! \If not defined return value should be NULL
104  inline transition* getEnding(){return endi;};
105 
106  //!Get the emission defined at index
107  //!\param iter Index of emm to get
108  //!\results emm* pointer to emission
109  inline emm* getEmission(size_t iter){return emission[iter];};
110 
111  double get_emission_prob(sequences& seqs, size_t iter); //get emission for given (position)
112  double get_transition_prob(sequences& seqs, size_t to, size_t iter); //get transition (position,from or too)
113  double getEndTrans();
114 
115  void print();
116  std::string stringify();
117 
118  //MUTATORS
119  bool parse(std::string&,stringList&,tracks&,weights*,StateFuncs*);
120 
121  //!Add the transition to the state
122  //!\param trans Pointer to transition to add to the state
123  inline void addTransition(transition* trans){transi->push_back(trans);};
124 
125  //!Set the ending transition to a given transition
126  //!\param trans Pointer to transition to be used as ending transition
127  inline void setEndingTransition(transition* trans){endi=trans;};
128 
129  //!Add emission to the state
130  //!\param em Pointer to the emission to be added
131  inline void addEmission(emm* em){emission.push_back(em);};
132 
133  //!Set the name of the state
134  //!\param txt Name of the state
135  inline void setName(std::string& txt){name=txt;};
136 
137  //!Set the GFF Tag for the state
138  inline void setGFF(std::string& txt){gff=txt;};
139 
140  //!Set the Label for the state
141  inline void setLabel(std::string& txt){label=txt;};
142 
143  //!Add state that this state transitions to
144  inline void addToState(state* st){to[st->getIterator()]=1;};
145 
146  //!Add state that transitions to this state
147  inline void addFromState(state* st){from[st->getIterator()]=1;};
148 
149  //!Set the index value to be used for the state
150  inline void setIter(size_t val){stateIterator=val;};
151 
152  bool hasComplexEmission();
153  bool hasComplexTransition();
154 
155  bool hasSimpleEmission();
156  bool hasSimpleTransition();
157 
158  bool isSimple();
159 
160  void checkLabels(std::set<std::string>& ,std::set<std::string>& ,std::set<std::string>& );
161 
162  void _finalizeTransitions(std::map<std::string,state*>& state_index);
163 
164  private:
165  std::string name; /* State name */
166  std::string gff ; /* State features description */
167  std::string label; /* State feature path label */
168 
169  std::vector<transition*>* transi;
171 
172  // Track Emissions //
173  std::vector<emm*> emission;
174 
175 
176  //Linking State Information (These are assigned at model finalization)
177  size_t stateIterator; //index of state in HMM
178  std::bitset<STATE_MAX> to;
179  std::bitset<STATE_MAX> from;
180 
181  bool _parseHeader(std::string&);
182  bool _parseTransition(std::string&,stringList&, tracks&, weights* , StateFuncs*);
183  bool _parseEmission(std::string&,stringList&, tracks&, weights*, StateFuncs*);
184  };
185 
186 }
187 #endif /*STATE_H*/