kinetics.pl 3.81 KB
Newer Older
Thierry Martinez's avatar
Thierry Martinez committed
1 2 3
:- module(
  kinetics,
  [
FAGES Francois's avatar
FAGES Francois committed
4
    kinetics/4,
5 6
    add_coefficient/2,
    present_in_kinetics/2
Thierry Martinez's avatar
Thierry Martinez committed
7 8 9
  ]
).

Mathieu Hemery's avatar
Mathieu Hemery committed
10 11 12
:- use_module(objects).
:- use_module(reaction_editor).

FAGES Francois's avatar
FAGES Francois committed
13 14
kinetics(Reactants, Inhibitors, Kinetics, Value) :-
  eval_kinetics(Reactants, Inhibitors, Kinetics, ValueToSimplify),
Thierry Martinez's avatar
ode  
Thierry Martinez committed
15 16 17
  simplify(ValueToSimplify, Value).


FAGES Francois's avatar
FAGES Francois committed
18
eval_kinetics(Reactants, _Inhibitors, single_reactant, Value) :-
Thierry Martinez's avatar
Thierry Martinez committed
19
  !,
Thierry Martinez's avatar
Thierry Martinez committed
20
  (
Thierry Martinez's avatar
Thierry Martinez committed
21
    Reactants = [Value]
Thierry Martinez's avatar
Thierry Martinez committed
22
  ->
Thierry Martinez's avatar
Thierry Martinez committed
23
    true
24
  ;
Thierry Martinez's avatar
Thierry Martinez committed
25
    throw(error(not_single_reactant_reaction))
Thierry Martinez's avatar
Thierry Martinez committed
26 27
  ).

FAGES Francois's avatar
FAGES Francois committed
28
eval_kinetics(Reactants, Inhibitors, product(Pattern in List, Expression), Value) :-
Thierry Martinez's avatar
Thierry Martinez committed
29 30 31 32 33 34
  !,
  (
    Pattern = S * O
  ->
    true
  ;
FAGES Francois's avatar
FAGES Francois committed
35
    S=1, O=Pattern %throw(error(invalid_pattern))
Thierry Martinez's avatar
Thierry Martinez committed
36 37 38
  ),
  (
    List = [reactants]
FAGES Francois's avatar
FAGES Francois committed
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
  ->
    make_product(Reactants, S, O, Expression, Value)
  ;
    (List = [inhibitors]
     ->
         maplist(add_coefficient, Inhibitors, CoeffInhibitors),
         make_product(CoeffInhibitors, S, O, Expression, Value)
     ;
     throw(error(invalid_list))
    )
  ).
  

eval_kinetics(Reactants, Inhibitors, sum(Pattern in List, Expression), Value) :-
  !,
  (
    Pattern = S * O
Thierry Martinez's avatar
Thierry Martinez committed
56 57 58
  ->
    true
  ;
FAGES Francois's avatar
FAGES Francois committed
59
    S=1, O=Pattern %throw(error(invalid_pattern))
Thierry Martinez's avatar
Thierry Martinez committed
60
  ),
FAGES Francois's avatar
FAGES Francois committed
61 62 63 64 65 66 67 68 69 70 71 72 73 74
  (
    List = [reactants]
  ->
    make_sum(Reactants, S, O, Expression, Value)
  ;
    (List = [inhibitors]
     ->
         maplist(add_coefficient, Inhibitors, CoeffInhibitors),
         make_sum(CoeffInhibitors, S, O, Expression, Value)
     ;
     throw(error(invalid_list))
    )
  ).
  
Thierry Martinez's avatar
Thierry Martinez committed
75

FAGES Francois's avatar
FAGES Francois committed
76
eval_kinetics(Reactants, Inhibitors, FunctionApplication, Expression) :-
Thierry Martinez's avatar
Thierry Martinez committed
77
  function_apply(FunctionApplication, NewBody),
Thierry Martinez's avatar
Thierry Martinez committed
78
  !,
FAGES Francois's avatar
FAGES Francois committed
79
  eval_kinetics(Reactants, Inhibitors, NewBody, Expression).
Thierry Martinez's avatar
Thierry Martinez committed
80

FAGES Francois's avatar
FAGES Francois committed
81 82
eval_kinetics(Reactants, Inhibitors, Callable, Expression) :-
  term_morphism(kinetics:eval_kinetics(Reactants, Inhibitors), Callable, Expression).
Thierry Martinez's avatar
Thierry Martinez committed
83 84


Thierry Martinez's avatar
Thierry Martinez committed
85
make_product([], _S, _O, _Expression, 1).
Thierry Martinez's avatar
Thierry Martinez committed
86

Thierry Martinez's avatar
Thierry Martinez committed
87 88 89 90
make_product([RS * RO | Tail], S, O, Expression, Product) :-
  substitute([S, O], [RS, RO], Expression, SubstitutedExpression),
  Product = SubstitutedExpression * TailProduct,
  make_product(Tail, S, O, Expression, TailProduct).
Thierry Martinez's avatar
Thierry Martinez committed
91 92


FAGES Francois's avatar
FAGES Francois committed
93 94 95 96 97 98 99 100 101
make_sum([], _S, _O, _Expression, 1).

make_sum([RS * RO | Tail], S, O, Expression, Sum) :-
  substitute([S, O], [RS, RO], Expression, SubstitutedExpression),
  Sum = SubstitutedExpression + TailSum,
  make_sum(Tail, S, O, Expression, TailSum).

add_coefficient(C, 1 * C).

102 103 104 105 106 107 108 109 110 111 112 113 114
%! present_in_kinetics(+Molecule, +Kinetics)
%
% Test if the Molecule (or whatever actually) is present in the kinetics

present_in_kinetics(Molecule, Molecule) :- !.

present_in_kinetics(Molecule, Kinetics) :-
  Kinetics =.. [_Functor|Decomposed],
  present_in_kinetics_sr(Molecule, Decomposed).

present_in_kinetics_sr(Molecule, [Head|Tail]) :-
  present_in_kinetics(Molecule, Head),!;
  present_in_kinetics_sr(Molecule, Tail).
FAGES Francois's avatar
FAGES Francois committed
115 116


FAGES Francois's avatar
CTL  
FAGES Francois committed
117
:-doc('Useful abbreviations for mass action law kinetics (with inhibitors), Michaelis-Menten kinetics, Hill kinetics (with inhibitors).').
FAGES Francois's avatar
FAGES Francois committed
118 119 120 121

:- initial('function MA(k) = k * product(S * M in [reactants], M ^ S)').

:- initial('function MAI(k) = k * product(S * M in [reactants], M ^ S) / (1 + sum(M in [inhibitors], M))').
Thierry Martinez's avatar
Thierry Martinez committed
122 123 124 125 126


:- initial('
  function MM(Vm, Km) = Vm * single_reactant / (Km + single_reactant)
').
127 128 129
%:- initial('
%  function MM(Vm, Km) = Vm * product(S * M in [reactants], M ^ S) / (Km + product(S * M in [reactants], M ^ S))
%').
Thierry Martinez's avatar
Thierry Martinez committed
130 131 132


:- initial('
FAGES Francois's avatar
CTL  
FAGES Francois committed
133
  function Hill(Vm, Km, n) =
Thierry Martinez's avatar
Thierry Martinez committed
134 135
    Vm * single_reactant ^ n / (Km ^ n + single_reactant ^ n).
').
136 137 138 139
%:- initial('
%  function Hill(Vm, Km, n) =
%    Vm * product(S * M in [reactants], M ^ (S * n)) / (Km ^ n + product(S * M in [reactants], M ^ (S * n))).
%').
FAGES Francois's avatar
FAGES Francois committed
140

FAGES Francois's avatar
CTL  
FAGES Francois committed
141
:- initial('function HillI(Vm, Km, n) = Vm * single_reactant ^ n / (Km ^ n + single_reactant ^ n + sum(M in [inhibitors], M ^ n)).').
142
%:- initial('function HillI(Vm, Km, n) = Vm * product(S * M in [reactants], M ^ (S * n)) / (Km ^ n + product(S * M in [reactants], M ^ (S * n)) + sum(M in [inhibitors], M ^ n))').
FAGES Francois's avatar
FAGES Francois committed
143