2007-07-13 01:52:54 +01:00
|
|
|
%
|
|
|
|
% distribution
|
|
|
|
%
|
|
|
|
|
2008-03-13 14:38:02 +00:00
|
|
|
:- module(clpbn_dist,
|
|
|
|
[
|
|
|
|
dist/1,
|
|
|
|
dist/3,
|
|
|
|
dists/1,
|
|
|
|
dist_new_table/2,
|
|
|
|
get_dist/4,
|
|
|
|
get_dist_matrix/5,
|
|
|
|
get_dist_domain/2,
|
|
|
|
get_dist_params/2,
|
|
|
|
get_dist_domain_size/2,
|
|
|
|
get_dist_tparams/2,
|
|
|
|
get_evidence_position/3,
|
|
|
|
get_evidence_from_position/3,
|
|
|
|
dist_to_term/2,
|
|
|
|
empty_dist/2,
|
|
|
|
dist_new_table/2
|
2007-07-13 01:52:54 +01:00
|
|
|
]).
|
|
|
|
|
2008-02-12 17:03:59 +00:00
|
|
|
:- use_module(library(lists),[is_list/1,nth0/3]).
|
2007-07-13 01:52:54 +01:00
|
|
|
|
2007-12-05 12:17:25 +00:00
|
|
|
:- use_module(library(matrix),
|
|
|
|
[matrix_new/4,
|
2008-03-13 14:38:02 +00:00
|
|
|
matrix_new/3,
|
|
|
|
matrix_to_list/2,
|
2007-12-05 12:17:25 +00:00
|
|
|
matrix_to_logs/1]).
|
2007-11-16 14:58:41 +00:00
|
|
|
|
2007-07-13 01:52:54 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
:- mode dist(+, -).
|
|
|
|
|
|
|
|
:- mode get_dist(+, -, -, -).
|
|
|
|
|
|
|
|
:- mode get_dist_params(+, -).
|
|
|
|
|
|
|
|
:- mode get_dist_domain_size(+, -).
|
|
|
|
|
|
|
|
:- mode get_dist_domain(+, -).
|
|
|
|
|
|
|
|
:- mode get_dist_nparams(+, -).
|
|
|
|
|
|
|
|
:- mode dist(?).
|
|
|
|
|
|
|
|
:- mode dist_to_term(+,-).
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*******************************************
|
|
|
|
|
|
|
|
store stuff in a DB of the form:
|
|
|
|
db(Id, CPT, Type, Domain, CPTSize, DSize)
|
|
|
|
|
|
|
|
where Id is the id,
|
|
|
|
cptsize is the table size or -1,
|
|
|
|
DSize is the domain size,
|
|
|
|
Type is
|
|
|
|
tab for tabular
|
|
|
|
trans for HMMs
|
|
|
|
continuous
|
|
|
|
Domain is
|
|
|
|
a list of values
|
|
|
|
bool for [t,f]
|
|
|
|
aminoacids for [a,c,d,e,f,g,h,i,k,l,m,n,p,q,r,s,t,v,w,y]
|
|
|
|
dna for [a,c,g,t]
|
|
|
|
rna for [a,c,g,u]
|
|
|
|
reals
|
|
|
|
|
|
|
|
|
|
|
|
********************************************/
|
|
|
|
|
2007-11-20 15:51:39 +00:00
|
|
|
:- dynamic id/1.
|
2007-07-13 01:52:54 +01:00
|
|
|
|
|
|
|
id(1).
|
|
|
|
|
|
|
|
new_id(Id) :-
|
|
|
|
retract(id(Id)),
|
|
|
|
Id1 is Id+1,
|
|
|
|
assert(id(Id1)).
|
|
|
|
|
2007-08-06 15:55:43 +01:00
|
|
|
dists(X) :- id(X1), X is X1-1.
|
2007-07-13 01:52:54 +01:00
|
|
|
|
|
|
|
dist(V, Id, Parents) :-
|
|
|
|
var(V), !,
|
|
|
|
freeze(V, dist(V, Id, Parents)).
|
|
|
|
dist(p(Type, CPT, Parents), Id, FParents) :-
|
|
|
|
when(
|
|
|
|
(ground(Type), ground(CPT))
|
|
|
|
,
|
|
|
|
distribution(Type, CPT, Id, Parents, FParents)
|
|
|
|
).
|
|
|
|
dist(p(Type, CPT), Id, FParents) :-
|
|
|
|
when(
|
|
|
|
(ground(Type), ground(CPT))
|
|
|
|
,
|
|
|
|
distribution(Type, CPT, Id, [], FParents)
|
|
|
|
).
|
|
|
|
|
|
|
|
distribution(bool, trans(CPT), Id, Parents, FParents) :-
|
|
|
|
is_list(CPT), !,
|
|
|
|
compress_hmm_table(CPT, Parents, Tab, FParents),
|
2008-03-13 14:38:02 +00:00
|
|
|
add_dist([t,f], trans, Tab, ParentsId).
|
2007-07-13 01:52:54 +01:00
|
|
|
distribution(bool, CPT, Id, Parents, Parents) :-
|
|
|
|
is_list(CPT), !,
|
|
|
|
add_dist([t,f], tab, CPT, Id).
|
|
|
|
distribution(aminoacids, trans(CPT), Id, Parents, FParents) :-
|
|
|
|
is_list(CPT), !,
|
|
|
|
compress_hmm_table(CPT, Parents, Tab, FParents),
|
2008-03-13 14:38:02 +00:00
|
|
|
add_dist([a,c,d,e,f,g,h,i,k,l,m,n,p,q,r,s,t,v,w,y], trans, Tab, FParents, Id).
|
2007-07-13 01:52:54 +01:00
|
|
|
distribution(aminoacids, CPT, Id, Parents, Parents) :-
|
|
|
|
is_list(CPT), !,
|
2008-03-13 14:38:02 +00:00
|
|
|
add_dist([a,c,d,e,f,g,h,i,k,l,m,n,p,q,r,s,t,v,w,y], tab, CPT, Parents, Id).
|
2007-07-13 01:52:54 +01:00
|
|
|
distribution(dna, trans(CPT), Id, Parents, FParents) :-
|
|
|
|
is_list(CPT), !,
|
|
|
|
compress_hmm_table(CPT, Parents, Tab, FParents),
|
2008-03-13 14:38:02 +00:00
|
|
|
add_dist([a,c,g,t], trans, Tab, FParents, Id).
|
2007-07-13 01:52:54 +01:00
|
|
|
distribution(dna, CPT, Id, Parents, Parents) :-
|
|
|
|
is_list(CPT), !,
|
|
|
|
add_dist([a,c,g,t], tab, CPT, Id).
|
|
|
|
distribution(rna, trans(CPT), Id, Parents, FParents) :-
|
|
|
|
is_list(CPT), !,
|
2008-03-13 14:38:02 +00:00
|
|
|
compress_hmm_table(CPT, Parents, Tab, FParents, FParents),
|
2007-07-13 01:52:54 +01:00
|
|
|
add_dist([a,c,g,u], trans, Tab, Id).
|
|
|
|
distribution(rna, CPT, Id, Parents, Parents) :-
|
|
|
|
is_list(CPT), !,
|
2008-03-13 14:38:02 +00:00
|
|
|
add_dist([a,c,g,u], tab, CPT, Parents, Id).
|
2007-07-13 01:52:54 +01:00
|
|
|
distribution(Domain, trans(CPT), Id, Parents, FParents) :-
|
|
|
|
is_list(Domain),
|
|
|
|
is_list(CPT), !,
|
|
|
|
compress_hmm_table(CPT, Parents, Tab, FParents),
|
2008-03-13 14:38:02 +00:00
|
|
|
add_dist(Domain, trans, Tab, FParents, Id).
|
2007-07-13 01:52:54 +01:00
|
|
|
distribution(Domain, CPT, Id, Parents, Parents) :-
|
|
|
|
is_list(Domain),
|
|
|
|
is_list(CPT), !,
|
2008-03-13 14:38:02 +00:00
|
|
|
add_dist(Domain, tab, CPT, Parents, Id).
|
2007-07-13 01:52:54 +01:00
|
|
|
|
2008-03-13 14:38:02 +00:00
|
|
|
add_dist(Domain, Type, CPT, _, Id) :-
|
2007-11-20 15:51:39 +00:00
|
|
|
recorded(clpbn_dist_db, db(Id, CPT, Type, Domain, _, _), _), !.
|
2008-03-13 14:38:02 +00:00
|
|
|
add_dist(Domain, Type, CPT, PSizes, Id) :-
|
2007-07-13 01:52:54 +01:00
|
|
|
length(CPT, CPTSize),
|
|
|
|
length(Domain, DSize),
|
|
|
|
new_id(Id),
|
2008-03-13 14:38:02 +00:00
|
|
|
record_parent_sizes(Parents, Id, PSizes, [DSize|PSizes]),
|
2007-11-20 15:51:39 +00:00
|
|
|
recordz(clpbn_dist_db,db(Id, CPT, Type, Domain, CPTSize, DSize),_).
|
2007-07-13 01:52:54 +01:00
|
|
|
|
2008-03-13 14:38:02 +00:00
|
|
|
|
|
|
|
record_parent_sizes([], Id, [], DSizes) :-
|
|
|
|
recordz(clpbn_dist_psizes,db(Id, DSizes),_).
|
|
|
|
record_parent_sizes([P|Parents], Id, [Size|Sizes], DSizes) :-
|
|
|
|
clpbn:get_atts(P,dist(Dist,_)),
|
|
|
|
get_dist_domain_size(Dist, DSize),
|
|
|
|
record_parent_sizes(Parents, Id, Sizes, DSizes).
|
|
|
|
|
2007-07-13 01:52:54 +01:00
|
|
|
%
|
|
|
|
% Often, * is used to code empty in HMMs.
|
|
|
|
%
|
|
|
|
compress_hmm_table([], [], [], []).
|
|
|
|
compress_hmm_table([*|L],[_|Parents],NL,NParents) :- !,
|
|
|
|
compress_hmm_table(L,Parents,NL,NParents).
|
|
|
|
compress_hmm_table([Prob|L],[P|Parents],[Prob|NL],[P|NParents]) :-
|
|
|
|
compress_hmm_table(L,Parents,NL,NParents).
|
|
|
|
|
|
|
|
dist(Id) :-
|
2007-11-20 15:51:39 +00:00
|
|
|
recorded(clpbn_dist_db, db(Id, _, _, _, _, _), _).
|
2007-07-13 01:52:54 +01:00
|
|
|
|
|
|
|
get_dist(Id, Type, Domain, Tab) :-
|
2007-11-20 15:51:39 +00:00
|
|
|
recorded(clpbn_dist_db, db(Id, Tab, Type, Domain, _, _), _).
|
2007-07-13 01:52:54 +01:00
|
|
|
|
2007-11-16 14:58:41 +00:00
|
|
|
get_dist_matrix(Id, Parents, Type, Domain, Mat) :-
|
2007-11-20 15:51:39 +00:00
|
|
|
recorded(clpbn_dist_db, db(Id, Tab, Type, Domain, _, DomainSize), _),
|
2007-11-16 14:58:41 +00:00
|
|
|
get_dsizes(Parents, Sizes, []),
|
2007-12-05 12:17:25 +00:00
|
|
|
matrix_new(floats, [DomainSize|Sizes], Tab, Mat),
|
|
|
|
matrix_to_logs(Mat).
|
2007-11-16 14:58:41 +00:00
|
|
|
|
|
|
|
get_dsizes([], Sizes, Sizes).
|
|
|
|
get_dsizes([P|Parents], [Sz|Sizes], Sizes0) :-
|
|
|
|
clpbn:get_atts(P,dist(Dist,_)),
|
|
|
|
get_dist_domain_size(Dist, Sz),
|
|
|
|
get_dsizes(Parents, Sizes, Sizes0).
|
|
|
|
|
|
|
|
|
2007-07-13 01:52:54 +01:00
|
|
|
get_dist_params(Id, Parms) :-
|
2007-11-20 15:51:39 +00:00
|
|
|
recorded(clpbn_dist_db, db(Id, Parms, _, _, _, _), _).
|
2007-07-13 01:52:54 +01:00
|
|
|
|
|
|
|
get_dist_domain_size(Id, DSize) :-
|
2007-11-20 15:51:39 +00:00
|
|
|
recorded(clpbn_dist_db, db(Id, _, _, _, _, DSize), _).
|
2007-07-13 01:52:54 +01:00
|
|
|
|
|
|
|
get_dist_domain(Id, Domain) :-
|
2007-11-20 15:51:39 +00:00
|
|
|
recorded(clpbn_dist_db, db(Id, _, _, Domain, _, _), _).
|
2007-07-13 01:52:54 +01:00
|
|
|
|
|
|
|
get_dist_nparams(Id, NParms) :-
|
2007-11-20 15:51:39 +00:00
|
|
|
recorded(clpbn_dist_db, db(Id, _, _, _, NParms, _), _).
|
2007-07-13 01:52:54 +01:00
|
|
|
|
2008-02-12 17:03:59 +00:00
|
|
|
get_evidence_position(El, Id, Pos) :-
|
|
|
|
recorded(clpbn_dist_db, db(Id, _, _, Domain, _, _), _),
|
2008-02-13 11:57:46 +00:00
|
|
|
nth0(Pos, Domain, El), !.
|
2008-02-12 17:03:59 +00:00
|
|
|
get_evidence_position(El, Id, Pos) :-
|
2008-02-13 11:57:46 +00:00
|
|
|
recorded(clpbn_dist_db, db(Id, _, _, _, _, _), _), !,
|
|
|
|
throw(error(domain_error(evidence,Id),get_evidence_position(El, Id, Pos))).
|
2008-02-12 17:03:59 +00:00
|
|
|
get_evidence_position(El, Id, Pos) :-
|
2008-02-13 11:57:46 +00:00
|
|
|
throw(error(domain_error(no_distribution,Id),get_evidence_position(El, Id, Pos))).
|
2008-02-12 17:03:59 +00:00
|
|
|
|
|
|
|
get_evidence_from_position(El, Id, Pos) :-
|
|
|
|
recorded(clpbn_dist_db, db(Id, _, _, Domain, _, _), _),
|
2008-02-13 11:57:46 +00:00
|
|
|
nth0(Pos, Domain, El), !.
|
2008-02-12 17:03:59 +00:00
|
|
|
get_evidence_from_position(El, Id, Pos) :-
|
2008-02-13 11:57:46 +00:00
|
|
|
recorded(clpbn_dist_db, db(Id, _, _, _, _, _), _), !,
|
|
|
|
throw(error(domain_error(evidence,Id),get_evidence_from_position(El, Id, Pos))).
|
2008-02-12 17:03:59 +00:00
|
|
|
get_evidence_from_position(El, Id, Pos) :-
|
2008-02-13 11:57:46 +00:00
|
|
|
throw(error(domain_error(no_distribution,Id),get_evidence_from_position(El, Id, Pos))).
|
2008-02-12 17:03:59 +00:00
|
|
|
|
2007-07-13 01:52:54 +01:00
|
|
|
dist_to_term(_Id,_Term).
|
2008-03-13 14:38:02 +00:00
|
|
|
|
|
|
|
empty_dist(Dist, TAB) :-
|
|
|
|
recorded(clpbn_dist_psizes,db(Dist, DSizes),_),
|
|
|
|
matrix_new(floats, DSizes, TAB).
|
|
|
|
|
|
|
|
dist_new_table(Id, NewMAT) :-
|
|
|
|
matrix_to_list(NewMat, List),
|
|
|
|
recorded(clpbn_dist_db, db(Id, _, A, B, C, D), R),
|
|
|
|
erase(R),
|
|
|
|
recorda(clpbn_dist_db, db(Id, List, A, B, C, D), R),
|
|
|
|
fail.
|
|
|
|
dist_new_table(_, _).
|
|
|
|
|
|
|
|
|