annotate toolboxes/FullBNT-1.0.7/bnt/examples/dynamic/HHMM/Motif/learn_motif_hhmm.m @ 0:cc4b1211e677 tip

initial commit to HG from Changeset: 646 (e263d8a21543) added further path and more save "camirversion.m"
author Daniel Wolff
date Fri, 19 Aug 2016 13:07:06 +0200
parents
children
rev   line source
Daniel@0 1
Daniel@0 2 seed = 0;
Daniel@0 3 rand('state', seed);
Daniel@0 4 randn('state', seed);
Daniel@0 5
Daniel@0 6 chars = ['a', 'c', 'g', 't'];
Daniel@0 7 motif = 'accca';
Daniel@0 8 motif_length = length(motif);
Daniel@0 9 motif_code = zeros(1, motif_length);
Daniel@0 10 for i=1:motif_length
Daniel@0 11 motif_code(i) = find(chars == motif(i));
Daniel@0 12 end
Daniel@0 13
Daniel@0 14 [bnet_init, Qnodes, Fnodes, Onode] = mk_motif_hhmm('motif_length', length(motif));
Daniel@0 15 %[bnet_init, Qnodes, Fnodes, Onode] = mk_motif_hhmm('motif_pattern', motif);
Daniel@0 16 ss = bnet_init.nnodes_per_slice;
Daniel@0 17
Daniel@0 18
Daniel@0 19
Daniel@0 20 % We generate a training set by creating uniform sequences,
Daniel@0 21 % and inserting a single motif at a random location.
Daniel@0 22 ntrain = 100;
Daniel@0 23 T = 20;
Daniel@0 24 cases = cell(1, ntrain);
Daniel@0 25
Daniel@0 26 if 1
Daniel@0 27 % uniform background
Daniel@0 28 background_dist = normalise(ones(1, length(chars)));
Daniel@0 29 end
Daniel@0 30 if 0
Daniel@0 31 % use a constant background
Daniel@0 32 background_dist = zeros(1, length(chars));
Daniel@0 33 m = find(chars=='t');
Daniel@0 34 background_dist(m) = 1.0;
Daniel@0 35 end
Daniel@0 36 if 0
Daniel@0 37 % use a background skewed away from the motif
Daniel@0 38 p = 0.01; q = (1-(2*p))/2;
Daniel@0 39 background_dist = [p p q q];
Daniel@0 40 end
Daniel@0 41
Daniel@0 42 unif_pos = normalise(ones(1, T-length(motif)));
Daniel@0 43 cases = cell(1, ntrain);
Daniel@0 44 data = zeros(1,T);
Daniel@0 45 for i=1:ntrain
Daniel@0 46 data = sample_discrete(background_dist, 1, T);
Daniel@0 47 L = sample_discrete(unif_pos, 1, 1);
Daniel@0 48 data(L:L+length(motif)-1) = motif_code;
Daniel@0 49 cases{i} = cell(ss, T);
Daniel@0 50 cases{i}(Onode,:) = num2cell(data);
Daniel@0 51 end
Daniel@0 52 disp('sample training cases')
Daniel@0 53 for i=1:5
Daniel@0 54 chars(cell2num(cases{i}(Onode,:)))
Daniel@0 55 end
Daniel@0 56
Daniel@0 57 engine_init = hmm_inf_engine(bnet_init);
Daniel@0 58
Daniel@0 59 [bnet_learned, LL, engine_learned] = ...
Daniel@0 60 learn_params_dbn_em(engine_init, cases, 'max_iter', 100, 'thresh', 1e-2);
Daniel@0 61 % 'anneal', 1, 'anneal_rate', 0.7);
Daniel@0 62
Daniel@0 63 % extract the learned motif profile
Daniel@0 64 eclass = bnet_learned.equiv_class;
Daniel@0 65 CPDO=struct(bnet_learned.CPD{eclass(Onode,1)});
Daniel@0 66 fprintf('columns = chars, rows = states\n');
Daniel@0 67 profile_learned = squeeze(CPDO.CPT(2,:,:))
Daniel@0 68 [m,ndx] = max(profile_learned, [], 2);
Daniel@0 69 map_motif_learned = chars(ndx)
Daniel@0 70 back_learned = squeeze(CPDO.CPT(1,1,:))'
Daniel@0 71 %map_back_learned = chars(argmax(back_learned))
Daniel@0 72
Daniel@0 73 CPDO_init = struct(bnet_init.CPD{eclass(Onode,1)});
Daniel@0 74 profile_init = squeeze(CPDO_init.CPT(2,:,:));
Daniel@0 75 back_init = squeeze(CPDO_init.CPT(1,1,:))';