Commit 352c1175 authored by Sylvain Soliman's avatar Sylvain Soliman

Some SBML-qual parsing

parent 4719c472
......@@ -8,6 +8,7 @@
).
:- use_module(library(sgml)).
:- use_module(library(xpath)).
:- devdoc('\\section{Commands}').
......@@ -38,9 +39,192 @@ add_qual(InputFile) :-
models:add_file_suffix('sbml', add_qual_file).
:- dynamic(qual_species/1).
add_qual_file(Filename) :-
% FIXME should check for level=3 and qual:required=true in attributes of
% sbml element
load_xml_file(Filename, [element(sbml, _, T)]),
length(T, N),
write(T),nl, write(N), nl.
load_xml(Filename, [element(sbml, _, Model)], [space(remove)]),
retractall(qual_species(_)),
forall(
(
xpath(Model, //'qual:qualitativeSpecies',
element('qual:qualitativeSpecies', Attributes, [])),
member('qual:id'=Id, Attributes)
),
(
member('qual:initialLevel'=Level, Attributes)
->
assertz(qual_species(Id)),
present(Id, Level)
;
assertz(qual_species(Id))
)
),
forall(
xpath(Model, //'qual:transition',
element('qual:transition', _, Transition)),
add_qual_transition(Transition)
).
:- dynamic(function_terms/2).
:- dynamic(default_term/1).
add_qual_transition(Transition) :-
retractall(function_terms(_, _)),
retractall(default_term(_)),
(
member(element('qual:listOfOutputs', [], Outputs), Transition)
->
add_qual_transition(Outputs, Transition)
;
true
).
add_qual_transition([], _Transition).
add_qual_transition([element('qual:output', Output, []) | ListOfOutputs], Transition) :-
member('qual:qualitativeSpecies'=OutputId, Output),
member(element('qual:listOfFunctionTerms', [], Functions), Transition),
(
% already computed for another output
default_term(_)
->
add_qual_functions([], OutputId)
;
add_qual_functions(Functions, OutputId)
),
add_qual_transition(ListOfOutputs, Transition).
add_qual_functions([], OutputId) :-
default_term(
element('qual:defaultTerm', ['qual:resultLevel'=ResultLevel], [])
),
forall(
function_terms(MathElt, Level),
format('if ~w then ~w -> ~w~n', [MathElt, OutputId, Level])
),
format('otherwise ~w -> ~w~n', [OutputId, ResultLevel]).
add_qual_functions([Function | L], OutputId) :-
Function = element('qual:functionTerm', ['qual:resultLevel'=Level], [Maths]),
!,
atom_bool(Level, Result),
Maths = element(math, _NameSpace, [MathElt]),
mathml_to_bool(MathElt, Bool),
bool_simplify(Bool, SBool),
assertz(function_terms(SBool, Result)),
add_qual_functions(L, OutputId).
add_qual_functions([Default | L], OutputId) :-
assertz(default_term(Default)),
add_qual_functions(L, OutputId).
% FIXME mathml operators like and/or/etc. might be N-ary
mathml_to_bool(element(apply, [], [element(Functor, [], []), X, Y]), B) :-
mathml_to_bool(X, XX),
mathml_to_bool(Y, YY),
B =.. [Functor, XX, YY].
mathml_to_bool(element(ci, [], [Id]), Bool) :-
(
qual_species(Id)
->
Bool = Id
;
% Id is a threshold, so we consider it equal to 1
Bool = 1
).
mathml_to_bool(element(cn, [type=integer], [Int]), Bool) :-
atom_bool(Int, Bool).
atom_bool(Atom, Bool) :-
atom_number(Atom, Int),
(
Int > 0
->
Bool = 1
;
Bool = 0
).
bool_simplify(geq(_, 0), true).
bool_simplify(geq(X, 1), X).
bool_simplify(gt(X, _), X).
bool_simplify(leq(_, 1), true).
bool_simplify(leq(X, 0), not(X)).
bool_simplify(lt(X, _), not(X)).
bool_simplify(eq(X, 0), not(X)).
bool_simplify(eq(X, 1), X).
bool_simplify(and(X, Y), And) :-
bool_simplify(X, XX),
bool_simplify(Y, YY),
(
XX = true
->
And = YY
;
YY = true
->
And = XX
;
(XX = false; YY = false)
->
And = false
;
XX = YY
->
And = XX
;
XX = Z,
YY = not(Z)
->
And = false
;
And = and(XX, YY)
).
bool_simplify(or(X, Y), Or) :-
bool_simplify(X, XX),
bool_simplify(Y, YY),
(
XX = false
->
Or = YY
;
YY = false
->
Or = XX
;
(XX = true; YY = true)
->
Or = true
;
XX = YY
->
Or = XX
;
XX = Z,
YY = not(Z)
->
Or = true
;
Or = or(XX, YY)
).
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment