Mercurial > hg > camir-aes2014
comparison toolboxes/FullBNT-1.0.7/bnt/examples/dynamic/HHMM/Motif/mk_motif_hhmm.m @ 0:e9a9cd732c1e tip
first hg version after svn
author | wolffd |
---|---|
date | Tue, 10 Feb 2015 15:05:51 +0000 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:e9a9cd732c1e |
---|---|
1 function [bnet, Qnodes, Fnodes, Onode] = mk_motif_hhmm(varargin) | |
2 % [bnet, Qnodes, Fnodes, Onode] = mk_motif_hhmm(...) | |
3 % | |
4 % Make the following HHMM | |
5 % | |
6 % S2 <----------------------> S1 | |
7 % | | | |
8 % | | | |
9 % M1 -> M2 -> M3 -> end B1 -> end | |
10 % | |
11 % where Mi represents the i'th letter in the motif | |
12 % and B is the background state. | |
13 % Si chooses between running the motif or the background. | |
14 % The Si and B states have self loops (not shown). | |
15 % | |
16 % The transition params are defined to respect the above topology. | |
17 % The background is uniform; each motif state has a random obs. distribution. | |
18 % | |
19 % Optional params: | |
20 % motif_length - required, unless we specify motif_pattern | |
21 % motif_pattern - if specified, we make the motif submodel deterministically | |
22 % emit this pattern | |
23 % background - if specified, we make the background submodel | |
24 % deterministically emit this (makes the motif easier to see!) | |
25 | |
26 | |
27 args = varargin; | |
28 nargs = length(args); | |
29 | |
30 % extract pattern, if any | |
31 motif_pattern = []; | |
32 for i=1:2:nargs | |
33 switch args{i}, | |
34 case 'motif_pattern', motif_pattern = args{i+1}; | |
35 end | |
36 end | |
37 | |
38 % set defaults | |
39 motif_length = length(motif_pattern); | |
40 background_char = []; | |
41 | |
42 % get params | |
43 for i=1:2:nargs | |
44 switch args{i}, | |
45 case 'motif_length', motif_length = args{i+1}; | |
46 case 'background', background_char = args{i+1}; | |
47 end | |
48 end | |
49 | |
50 | |
51 chars = ['a', 'c', 'g', 't']; | |
52 Osize = length(chars); | |
53 | |
54 Qsize = [2 motif_length]; | |
55 Qnodes = 1:2; | |
56 D = 2; | |
57 transprob = cell(1,D); | |
58 termprob = cell(1,D); | |
59 startprob = cell(1,D); | |
60 | |
61 % startprob{d}(k,j), startprob{1}(1,j) | |
62 % transprob{d}(i,k,j), transprob{1}(i,j) | |
63 % termprob{d}(k,j) | |
64 | |
65 | |
66 % LEVEL 1 | |
67 | |
68 startprob{1} = zeros(1, 2); | |
69 startprob{1} = [1 0]; % always start in the background model | |
70 | |
71 % When in the background state, we stay there with high prob | |
72 % When in the motif state, we immediately return to the background state. | |
73 transprob{1} = [0.8 0.2; | |
74 1.0 0.0]; | |
75 | |
76 | |
77 % LEVEL 2 | |
78 startprob{2} = 'leftstart'; % both submodels start in substate 1 | |
79 transprob{2} = zeros(motif_length, 2, motif_length); | |
80 termprob{2} = zeros(2, motif_length); | |
81 | |
82 % In the background model, we only use state 1. | |
83 transprob{2}(1,1,1) = 1; % self loop | |
84 termprob{2}(1,1) = 0.2; % prob transition to end state | |
85 | |
86 % Motif model | |
87 transprob{2}(:,2,:) = mk_leftright_transmat(motif_length, 0); % no self loops | |
88 termprob{2}(2,end) = 1.0; % last state immediately terminates | |
89 | |
90 | |
91 % OBS LEVEl | |
92 | |
93 obsprob = zeros([Qsize Osize]); | |
94 if isempty(background_char) | |
95 % uniform background model | |
96 %obsprob(1,1,:) = normalise(ones(Osize,1)); | |
97 obsprob(1,1,:) = normalise(rand(Osize,1)); | |
98 else | |
99 % deterministic background model (easy to see!) | |
100 m = find(chars==background_char); | |
101 obsprob(1,1,m) = 1.0; | |
102 end | |
103 | |
104 if ~isempty(motif_pattern) | |
105 % initialise with true motif (cheating) | |
106 for i=1:motif_length | |
107 m = find(chars == motif_pattern(i)); | |
108 obsprob(2,i,m) = 1.0; | |
109 end | |
110 else | |
111 obsprob(2,:,:) = mk_stochastic(rand(motif_length, Osize)); | |
112 end | |
113 | |
114 if 0 | |
115 Oargs = {'CPT', obsprob}; | |
116 else | |
117 % We use a minent prior for the emission distribution for the states in the motif model | |
118 % (but not the background model). This encourages nearly deterministic distributions. | |
119 % We create an index matrix (where M = motif length) | |
120 % [2 1 | |
121 % 2 2 | |
122 % ... | |
123 % 2 M] | |
124 % and then convert this to a list of integers, which | |
125 % specifies when to use the minent prior (Q1=2 specifies motif model). | |
126 M = motif_length; | |
127 ndx = [2*ones(M,1) (1:M)']; | |
128 pcases = subv2ind([2 motif_length], ndx); | |
129 Oargs = {'CPT', obsprob, 'prior_type', 'entropic', 'entropic_pcases', pcases}; | |
130 end | |
131 | |
132 | |
133 | |
134 [bnet, Qnodes, Fnodes, Onode] = mk_hhmm('Qsizes', Qsize, 'Osize', Osize, 'discrete_obs', 1, ... | |
135 'Oargs', Oargs, 'Ops', Qnodes(1:2), ... | |
136 'startprob', startprob, 'transprob', transprob, 'termprob', termprob); | |
137 |