/ghmm-0.7.0/tests/shmm_viterbi_test.c

http://github.com/jcnossen/hmmgenefinder · C · 77 lines · 49 code · 16 blank · 12 comment · 12 complexity · b91159309cd8e2c2e3e9b8ea6b1390e1 MD5 · raw file

  1. /*******************************************************************************
  2. author : Bernd Wichern
  3. filename : /zpr/bspk/src/hmm/ghmm/tests/shmm_viterbi_test.c
  4. created : TIME: 12:52:37 DATE: Tue 05. June 2001
  5. last-modified: TIME: 15:33:06 DATE: Tue 05. June 2001
  6. $Id: shmm_viterbi_test.c 201 2002-02-22 23:48:44Z achim $
  7. *******************************************************************************/
  8. #include <string.h>
  9. #include <ghmm/smodel.h>
  10. #include <ghmm/sequence.h>
  11. #include <ghmm/sviterbi.h>
  12. #include <ghmm/mes.h>
  13. static int viterbi_test(char* argv[]);
  14. /*============================================================================*/
  15. static int viterbi_test(char* argv[]) {
  16. #define CUR_PROC "viterbi_test"
  17. FILE *outfile = NULL;
  18. sequence_d_t **sqd = NULL;
  19. smodel** smo = NULL;
  20. char outfilename[256];
  21. int smo_number, sqd_number, *state_seq = NULL, model, t, cnt = 0;
  22. long i, j;
  23. double log_p;
  24. /* read array of models */
  25. smo = smodel_read(argv[1], &smo_number);
  26. if (!(smo)) {mes_proc(); goto STOP;}
  27. /* read sequences */
  28. sqd = sequence_d_read(argv[2], &sqd_number);
  29. if (!sqd) {mes_proc(); goto STOP;}
  30. strcpy(outfilename, argv[3]);
  31. if(!(outfile = mes_fopen(outfilename, "wt"))) {mes_proc(); goto STOP;}
  32. /* calculate viterbi path for every possible sequence-model combination */
  33. for (model = 0; model < smo_number; model++) {
  34. for (i = 0; i < sqd_number; i++) {
  35. for (j = 0; j < sqd[i]->seq_number; j++) {
  36. cnt++;
  37. state_seq = sviterbi(smo[model], sqd[i]->seq[j], sqd[i]->seq_len[j],
  38. &log_p);
  39. if (state_seq == NULL) {mes_proc(); goto STOP;}
  40. fprintf(outfile, "%d %ld (Seq.ID %d): logp %.4f\t", model, i,
  41. (int) sqd[i]->seq_id[j], log_p);
  42. for (t = 0; t < sqd[i]->seq_len[j]; t++)
  43. fprintf(outfile, "%2d ", state_seq[t]);
  44. fprintf(outfile, "\n");
  45. m_free(state_seq);
  46. if (!cnt%100) printf("%d\n", cnt);
  47. }
  48. }
  49. }
  50. STOP:
  51. return -1;
  52. # undef CUR_PROC
  53. }
  54. /*============================================================================*/
  55. int main(int argc, char* argv[]) {
  56. if (argc != 4) {
  57. printf("Usage: shmm_viterbi_test <Model File> <Sequence File> <Output File> \n");
  58. exit(1);
  59. }
  60. return viterbi_test(argv);
  61. }