/ghmm-0.7.0/ghmm/sdmodel.h

http://github.com/jcnossen/hmmgenefinder · C Header · 283 lines · 67 code · 38 blank · 178 comment · 0 complexity · a9b3093f39fbf9e2f06f85b2903ecd31 MD5 · raw file

  1. /*******************************************************************************
  2. *
  3. * This file is part of the General Hidden Markov Model Library,
  4. * GHMM version __VERSION__, see http://ghmm.org
  5. *
  6. * Filename: ghmm/ghmm/sdmodel.h
  7. * Authors: Wasinee Rungsarityotin, Benjamin Georgi
  8. *
  9. * Copyright (C) 1998-2004 Alexander Schliep
  10. * Copyright (C) 1998-2001 ZAIK/ZPR, Universitaet zu Koeln
  11. * Copyright (C) 2002-2004 Max-Planck-Institut fuer Molekulare Genetik,
  12. * Berlin
  13. *
  14. * Contact: schliep@ghmm.org
  15. *
  16. * This library is free software; you can redistribute it and/or
  17. * modify it under the terms of the GNU Library General Public
  18. * License as published by the Free Software Foundation; either
  19. * version 2 of the License, or (at your option) any later version.
  20. *
  21. * This library is distributed in the hope that it will be useful,
  22. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  23. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  24. * Library General Public License for more details.
  25. *
  26. * You should have received a copy of the GNU Library General Public
  27. * License along with this library; if not, write to the Free
  28. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  29. *
  30. *
  31. * This file is version $Revision: 1191 $
  32. * from $Date: 2005-06-21 11:56:12 +0200 (Tue, 21 Jun 2005) $
  33. * last change by $Author: cic99 $.
  34. *
  35. *******************************************************************************/
  36. #ifndef SDMODEL_H
  37. #define SDMODEL_H
  38. #ifdef __cplusplus
  39. extern "C" {
  40. #endif
  41. /**@name HMM-Modell */
  42. /*@{ (Doc++-Group: model) */
  43. /** @name state
  44. The basic structure, keeps all parameters that belong to a state.
  45. */
  46. struct sdstate {
  47. /** Initial probability */
  48. double pi;
  49. /** Output probability */
  50. double *b;
  51. /** ID of the following state */
  52. int *out_id;
  53. /** ID of the previous state */
  54. int *in_id;
  55. /** transition probs to successor states. It is a
  56. matrix in case of mult. transition matrices (COS > 1)*/
  57. double **out_a;
  58. /** transition probs from predecessor states. It is a
  59. matrix in case of mult. transition matrices (COS > 1) */
  60. double **in_a;
  61. /** Transition probability to a successor
  62. double *out_a; */
  63. /** Transition probablity to a precursor
  64. double *in_a;*/
  65. /** Number of successor states */
  66. int out_states;
  67. /** Number of precursor states */
  68. int in_states;
  69. /** if fix == 1 --> b stays fix during the training */
  70. int fix;
  71. char *label;
  72. /* XXX Specific variable for ProfileHMM to count the number of
  73. match states. Not elegant solution.
  74. WS: if 1 then counts me, 0 don't count me */
  75. int countme;
  76. };
  77. typedef struct sdstate sdstate;
  78. /** @name model
  79. The complete HMM. Contains all parameters, that define a HMM.
  80. */
  81. struct sdmodel {
  82. /** Number of states */
  83. int N;
  84. /** Number of outputs */
  85. int M;
  86. /** smodel includes continuous model with one transition matrix
  87. (cos is set to 1) and an extension for models with several matrices
  88. (cos is set to a positive integer value > 1).*/
  89. int cos;
  90. /** Vector of the states */
  91. sdstate *s;
  92. /** Prior for the a priori probability for the model.
  93. A value of -1 indicates that no prior is defined. */
  94. double prior;
  95. /** Contains bit flags for various model extensions such as
  96. kSilentStates, kTiedEmissions (see ghmm.h for a complete list)
  97. */
  98. /** pointer to class function */
  99. int (*get_class) (int *, int);
  100. /*int (*get_class)(const double*,int,double*); */
  101. /** Contains bit flags for various model extensions such as
  102. kSilentStates, kTiedEmissions (see ghmm.h for a complete list)
  103. */
  104. int model_type;
  105. /** Flag variables for each state indicating whether it is emitting
  106. or not.
  107. Note: silent != NULL iff (model_type & kSilentStates) == 1 */
  108. int *silent;
  109. /*AS*/ int topo_order_length;
  110. /*WR*/ int *topo_order;
  111. /*WR*/};
  112. typedef struct sdmodel sdmodel;
  113. #ifdef __cplusplus
  114. }
  115. #endif
  116. /*
  117. Important: The inclusion of sequence.h ist not done before this point in
  118. order to avoid error by compiling.
  119. */
  120. #include <ghmm/sequence.h>
  121. #include <ghmm/scanner.h>
  122. #ifdef __cplusplus
  123. extern "C" {
  124. #endif
  125. /** Frees the memory of a model.
  126. @return 0 for succes; -1 for error
  127. @param mo: pointer to a model */
  128. int sdmodel_free (sdmodel ** mo);
  129. int sdmodel_initSilentStates (sdmodel * mo);
  130. /**
  131. Produces sequences to a given model. All memory that is needed for the
  132. sequences is allocated inside the function. It is possible to define
  133. the length of the sequences global (global_len > 0) or it can be set
  134. inside the function, when a final state in the model is reach (a state
  135. with no output). If the model has no final state, the sequences will
  136. have length MAX_SEQ_LEN.
  137. @return pointer to an array of sequences
  138. @param mo: model
  139. @param seed: initial parameter for the random value generator
  140. (an integer). If seed == 0, then the random value
  141. generator is not initialized.
  142. @param global_len: length of sequences (=0: automatically via final states)
  143. @param seq_number: number of sequences
  144. @param T_max: maximal number of consecutive silent states in model (used to
  145. identify silent circles).
  146. */
  147. sequence_t *sdmodel_generate_sequences (sdmodel * mo, int seed,
  148. int global_len, long seq_number,
  149. int Tmax);
  150. /**
  151. Copies a given model. Allocates the necessary memory.
  152. @return copy of the model
  153. @param mo: model to copy */
  154. sdmodel *sdmodel_copy (const sdmodel * mo);
  155. /** Utility for converting between single discrete model and switching model */
  156. model *sdmodel_to_model (const sdmodel * mo, int kclass);
  157. /** */
  158. void model_to_sdmodel (const model * mo, sdmodel * smo, int klass);
  159. /**
  160. Writes a model in matrix format.
  161. @param file: output file
  162. @param mo: model
  163. */
  164. void sdmodel_print (FILE * file, sdmodel * mo);
  165. /**
  166. Writes transition matrix of a model.
  167. @param file: output file
  168. @param mo: model
  169. @param tab: format: leading tabs
  170. @param separator: format: seperator for columns
  171. @param ending: format: end of a row
  172. */
  173. void sdmodel_Ak_print (FILE * file, sdmodel * mo, int k, char *tab,
  174. char *separator, char *ending);
  175. /**
  176. Writes output matrix of a model.
  177. @param file: output file
  178. @param mo: model
  179. @param tab: format: leading tabs
  180. @param separator: format: seperator for columns
  181. @param ending: format: end of a row
  182. */
  183. void sdmodel_B_print (FILE * file, sdmodel * mo, char *tab, char *separator,
  184. char *ending);
  185. /**
  186. Writes initial allocation vector of a matrix.
  187. @param file: output file
  188. @param mo: model
  189. @param tab: format: leading Tabs
  190. @param separator: format: seperator for columns
  191. @param ending: format: end of a row
  192. */
  193. void sdmodel_Pi_print (FILE * file, sdmodel * mo, char *tab,
  194. char *separator, char *ending);
  195. /*============================================================================*/
  196. /** sdviterbi is working for switching discrete model
  197. * sdmodel_topo_ordering -- need to be implemented with DFS (as in model_util.c)
  198. *============================================================================
  199. **/
  200. void sdmodel_topo_ordering (sdmodel * mo);
  201. int *sdviterbi (sdmodel * mo, int *o, int len, double *log_p);
  202. /** Forward-Algorithm.
  203. Calculates alpha[t][i], scaling factors scale[t] and log( P(O|lambda) ) for
  204. a given double sequence and a given model.
  205. @param smo model
  206. @param O sequence
  207. @param length: length of sequence
  208. @param alpha: alpha[t][i]
  209. @param scale: a reference for double type, scale factors
  210. @param log\_p: a reference for double type, log likelihood log( P(O|lambda) )
  211. @return 0 for success, -1 for error
  212. */
  213. int sdfoba_forward (sdmodel * mo, const int *O, int len, double **alpha,
  214. double *scale, double *log_p);
  215. /** Descale
  216. descales the alpha matrix from the forward algorithm
  217. @param alpha: alpha matrix from forward
  218. @param scale: scale vector from forward
  219. @param t: number of timesteps
  220. @param n: number of states
  221. @param newalpha: unscaled alpha matrix
  222. @return 0 for success, -1 for error
  223. */
  224. int sdfoba_descale (double **alpha, double *scale, int t, int n,
  225. double **newalpha);
  226. /**
  227. Calculates the sum log( P( O | lambda ) ).
  228. Sequences, that can not be generated from the given model, are neglected.
  229. @return log(P)
  230. @param mo model
  231. @param sq sequences
  232. */
  233. double sdmodel_likelihood (sdmodel * mo, sequence_t * sq);
  234. /**
  235. Writes the parameters of a model sorted by states.
  236. Is not very concise.
  237. @param file: output file
  238. @param mo: model
  239. */
  240. void sdmodel_states_print (FILE * file, sdmodel * mo);
  241. #ifdef __cplusplus
  242. }
  243. #endif
  244. #endif
  245. /*@} (Doc++-Group: model) */