Commit 3808e576 authored by Thierry Martinez's avatar Thierry Martinez

Parameters, macros, etc.

parent 86d866a1
......@@ -4,11 +4,11 @@ all: biocham biocham_debug tests doc
.PHONY: tests doc
biocham: $(MODULES) Makefile
biocham: $(MODULES) toc.org Makefile
echo $(MODULES)
swipl -o biocham --goal=start --toplevel=toplevel -c $(MODULES)
biocham_debug: $(MODULES) Makefile
biocham_debug: $(MODULES) toc.org Makefile
swipl -o biocham_debug --goal=initialize -c $(MODULES)
tests: biocham_tests
......
:- module(
conservation_laws,
[
]
).
......@@ -8,13 +8,19 @@
).
filename('.'(Prefix, Suffix), Filename) :-
filename(Filename, ExpandedFilename) :-
flat_filename(Filename, FlatFilename),
expand_file_name(FlatFilename, List),
member(ExpandedFilename, List).
flat_filename('.'(Prefix, Suffix), Filename) :-
!,
filename(Prefix, PrefixAtom),
filename(Suffix, SuffixAtom),
flat_filename(Prefix, PrefixAtom),
flat_filename(Suffix, SuffixAtom),
format(atom(Filename), '~a.~a', [PrefixAtom, SuffixAtom]).
filename(Filename, Filename).
flat_filename(Filename, Filename).
chop_suffix(Filename, Suffix) :-
......
:- module(
initial_state,
[
get_initial_state/2
get_initial_state/2,
list_initial_state/0,
clear_initial_state/0,
present/1,
present/2,
absent/1,
undefined/1
]
).
:- dynamic(initial_state/2).
get_initial_state(Molecule, State) :-
get_initial_state(Object, Concentration) :-
(
initial_state(Molecule, State)
item(
[model: current_model, kind: initial_state],
present(Object)
)
->
Concentration = 1
;
item(
[model: current_model, kind: initial_state],
present(Object, Concentration)
)
->
true
;
Concentration = 0
).
list_initial_state :-
biocham_command,
doc('
lists the objects which are present (including their initial concentration)
and absent from the initial state.'),
list_items([model: current_model, kind: initial_state]).
clear_initial_state :-
biocham_command,
doc('
makes undefined all objects possibly present or absent in the initial
state.'),
\+ (
item([model: current_model, kind: initial_state, id: Id], _),
\+ (
delete_item(Id)
)
).
present(Object) :-
biocham_command,
type(Object, object),
doc('\\argument{Object} is made present in the initial state.'),
undefined_no_error(Object),
add_item(initial_state, present(Object)).
present(Object, Concentration) :-
biocham_command,
type(Object, object),
type(Concentration, concentration),
doc('
\\argument{Object} is initialized with the given initial concentration.
An initial value equal to 0 means absent.'),
undefined_no_error(Object),
add_item(initial_state, present(Object, Concentration)).
absent(Object) :-
biocham_command,
type(Object, object),
doc('\\argument{Object} is made absent in the initial state.'),
catch(
undefined(Object),
error(unknown_initial_state(_)),
true
),
add_item(initial_state, absent(Object)).
undefined_no_error(Object) :-
catch(
undefined(Object),
error(unknown_initial_state(_)),
true
).
undefined(Object) :-
biocham_command,
type(Object, object),
doc('
\\argument{Object} is made possibly present or absent in the initial
state.'),
(
(
item(
[model: current_model, kind: initial_state, id: Id],
present(Object)
)
;
item(
[model: current_model, kind: initial_state, id: Id],
present(Object, _Concentration)
)
;
item(
[model: current_model, kind: initial_state, id: Id],
absent(Object)
)
)
->
delete_item(Id)
;
State = 0
throw(error(unknown_initial_state(Object)))
).
/*
list_initial_state .
lists the objects which are present (including their initial concentration) and absent from the initial state.
• list_all_initial_states .
same as above but lists undefined objects as well.
• clear_initial_state .
makes undefined all objects possibly present or absent in the initial state. Also deletes all parameters and macros.
• present(object_pattern) .
• present(set_of_object_patterns) .
all objects (appearing in the instances of the current set of rules) and matching one of the given object patterns are made present in the initial state.
Example 24 present({cycA, cdk1, cycE~?}). makes cycA, cdk1 and all modified forms of cycE present in the initial state.
• present(object_pattern,concentration) .
• present(object_pattern,parameter) .
initializes the given objects with the given initial concentration, or sets it to be equal to the value of the given parameter. An initial value equal to 0 means absent.
Example 25
present(MAPK,0.3). present(CycE, k).
makes present two molecules with concentration, respectively 0.3 and the value of parameter k.
• absent(object_pattern) . • absent(set_of_object_patterns) .
makes all objects (appearing in the instances of the current set of rules) and matching one of the given object patterns, absent from the initial state. Same as present with initial concentration equal to 0.
• undefined(object_pattern) . • undefined(set_of_object_patterns) .
makes all objects (appearing in the instances of the current set of rules) and matching one of the given object patterns, possibly present or absent in the initial state.
• make_present_not_absent .
makes all objects (appearing in the instances of the current set of rules) which are not declared absent, present in the initial state.
• make_absent_not_present .
makes all objects (appearing in the instances of the current set of rules) which are not declared present, absent in the initial state.
• set_initial_state(state) .
• set_initial_state(set_of_states) .
*/
:- module(
macros,
[
macro/2,
macro/1,
list_macros/0,
delete_macro/1
]
).
macro(Macro, Value) :-
biocham_command,
type(Macro, macro),
doc('sets the value of \\argument{macro} to \\argument{Value}.'),
catch(
delete_macro(Macro),
error(undefined_macro(_)),
true
),
add_item(macro, macro(Macro, Value)).
macro(Macro) :-
biocham_command,
type(Macro, macro),
doc('shows the expression associated to the given macro.'),
find_macro(Macro, _Id, Expression),
write(Expression),
nl.
find_macro(Macro, Id, Expression) :-
(
item([model: current_model, kind: macro, id: Id], macro(Macro, Expression))
->
true
;
throw(error(undefined_macro(Macro)))
).
list_macros :-
biocham_command,
doc('lists all known macros.'),
list_items([model: current_model, kind: macro]).
delete_macro(Macro) :-
biocham_command,
type(Macro, macro),
doc('deletes a macro.'),
find_macro(Macro, Id, _Expression),
delete_item(Id).
......@@ -4,9 +4,9 @@
load/1,
load_biocham/1,
new_model/0,
add_item/3,
add_item/2,
list_items/1,
remove_item/1,
delete_item/1,
item/2
]
).
......@@ -85,12 +85,23 @@ add_biocham(InputFile) :-
\\emph{added} to the current set of rules.
The commands contained in the file are executed
(with the file directory as current directory).'),
filename(InputFile, Filename),
automatic_suffix(Filename, '.bc', FilenameBc),
setup_call_cleanup(
open(FilenameBc, read, Stream),
load_biocham_stream(Stream),
close(Stream)
\+ (
filename(InputFile, Filename),
\+ (
automatic_suffix(Filename, '.bc', FilenameBc),
file_directory_name(FilenameBc, FileDirectory),
setup_call_cleanup(
(
working_directory(FileDirectory, PreviousDirectory),
open(FilenameBc, read, Stream)
),
load_biocham_stream(Stream),
(
close(Stream),
working_directory(_, PreviousDirectory)
)
)
)
).
......@@ -148,16 +159,16 @@ not_fresh :-
retractall(fresh).
add_item(Model, Kind, Item) :-
single_model(Model, ModelId),
add_item(Kind, Item) :-
single_model(Model),
not_fresh,
create_item_id(Id),
assertz(item(Id, ModelId, Kind, Item)).
assertz(item(Id, Model, Kind, Item)).
single_model(Model) :-
(
current_model([Model])
nb_getval(current_models, [Model])
->
true
;
......@@ -229,7 +240,7 @@ list_items(Options) :-
).
remove_item(Id) :-
delete_item(Id) :-
retract(item(Id, _Model, _Kind, _Item)).
......
:- module(
parameters,
[
parameter/2,
parameter/1,
list_parameters/0,
delete_parameter/1
]
).
parameter(Parameter, Value) :-
biocham_command,
type(Parameter, parameter),
type(Value, float),
doc('sets the value of \\argument{Parameter} to \\argument{Value}.'),
(
find_parameter(Parameter, Id, _Value)
->
delete_item(Id)
;
true
),
add_item(parameter, parameter(Parameter, Value)).
parameter(Parameter) :-
biocham_command,
type(Parameter, parameter),
doc('shows the value of \\argument{Parameter}.'),
find_parameter(Parameter, _Id, Value),
write(Value),
nl.
find_parameter(Parameter, Id, Value) :-
(
item(
[model: current_model, kind: macro, id: Id],
parameter(Parameter, Value)
)
->
true
;
throw(error(undefined_parameter(Parameter)))
).
list_parameters :-
biocham_command,
doc('shows the values of all known parameters.'),
list_items([model: current_model, kind: parameter]).
delete_parameter(Parameter) :-
biocham_command,
type(Parameter, parameter),
doc('deletes a parameter'),
find_parameter(Parameter, Id, _Value),
delete_item(Id).
......@@ -11,20 +11,21 @@ add_rule(Rule) :-
biocham_command,
type(Rule, reaction),
doc('adds reaction rules to the current set of rules.'),
add_item(current_model, rule, Rule).
simplify_rule(Rule, SimplifiedRule),
add_item(rule, SimplifiedRule).
list_rules :-
biocham_command,
doc('lists the current set of rules.'),
list_items([model: current_model, kind: rule]).
rule(Item, Kinetics, Reactants, Products) :-
rule(Item, Kinetics, Reactants, Products, Reversible) :-
(
Item = (PairKinetics for Body)
Item = (Kinetics for Body)
->
true
;
PairKinetics = 'MA'(1),
Kinetics = 'MA'(1),
Body = Item
),
(
......@@ -46,28 +47,31 @@ rule(Item, Kinetics, Reactants, Products) :-
->
Reversible = yes
),
solution(Left, LeftMolecules),
solution(Right, RightMolecules),
solution(Catalyst, CatalystMolecules),
append(CatalystMolecules, LeftMolecules, LeftMoleculesWithCatalyst),
append(CatalystMolecules, RightMolecules, RightMoleculesWithCatalyst),
solution_to_list(Left, LeftMolecules),
solution_to_list(Right, RightMolecules),
solution_to_list(Catalyst, CatalystMolecules),
append(CatalystMolecules, LeftMolecules, Reactants),
append(CatalystMolecules, RightMolecules, Products).
rule(Item, Kinetics, Reactants, Products) :-
rule(Item, PairKinetics, LeftMolecules, RightMolecules, Reversible),
(
Reversible = yes
->
pair_kinetics(PairKinetics, ForwardKinetics, BackwardKinetics),
(
Kinetics = ForwardKinetics,
Reactants = LeftMoleculesWithCatalyst,
Products = RightMoleculesWithCatalyst
Reactants = LeftMolecules,
Products = RightMolecules
;
Kinetics = BackwardKinetics,
Reactants = RightMoleculesWithCatalyst,
Products = LeftMoleculesWithCatalyst
Reactants = RightMolecules,
Products = LeftMolecules
)
;
Kinetics = PairKinetics,
Reactants = LeftMoleculesWithCatalyst,
Products = RightMoleculesWithCatalyst
Reactants = LeftMolecules,
Products = RightMolecules
).
pair_kinetics(PairKinetics, ForwardKinetics, BackwardKinetics) :-
......@@ -80,17 +84,139 @@ pair_kinetics(PairKinetics, ForwardKinetics, BackwardKinetics) :-
BackwardKinetics = PairKinetics
).
solution('_', []) :-
solution_to_list('_', []) :-
!.
solution(A + B, Solution) :-
solution_to_list(A + B, Solution) :-
!,
solution(A, SolutionA),
solution(B, SolutionB),
solution_to_list(A, SolutionA),
solution_to_list(B, SolutionB),
append(SolutionA, SolutionB, Solution).
solution(Coefficient * Object, Solution) :-
solution_to_list(Coefficient * Object, Solution) :-
!,
Solution = [Coefficient * Object].
solution(Object, [1 * Object]).
solution_to_list(Object, [1 * Object]).
list_to_solution([], '_').
list_to_solution([Head | Tail], Solution) :-
list_to_solution(Tail, Head, Solution).
list_to_solution([], CoefficientObject, Object) :-
coefficient_object(CoefficientObject, Object).
list_to_solution([Head | Tail], CoefficientObject, Solution) :-
coefficient_object(CoefficientObject, Object),
Solution = Object + SolutionTail,
list_to_solution(Tail, Head, SolutionTail).
coefficient_object(1 * Object, Object) :-
!.
coefficient_object(CoefficientObject, CoefficientObject).
simplify_rule(Rule, SimplifiedRule) :-
rule(Rule, Kinetics, LeftMolecules, RightMolecules, Reversible),
simplify_kinetics(Kinetics, KineticsSimplified),
simplify_solution(LeftMolecules, LeftMoleculesSimplified),
simplify_solution(RightMolecules, RightMoleculesSimplified),
simplify_catalyst(
LeftMoleculesSimplified, RightMoleculesSimplified, Left, Catalyst, Right
),
build_rule(
KineticsSimplified, Left, Catalyst, Right, Reversible, SimplifiedRule
).
simplify_kinetics(Kinetics, KineticsSimplified) :-
(
Kinetics = (LeftKinetics, RightKinetics)
->
simplify(LeftKinetics, LeftKineticsSimplified),
simplify(RightKinetics, RightKineticsSimplified),
(
LeftKineticsSimplified = RightKineticsSimplified
->
KineticsSimplified = LeftKineticsSimplified
;
KineticsSimplified = (LeftKineticsSimplified, RightKineticsSimplified)
)
;
simplify(Kinetics, KineticsSimplified)
).
simplify_solution([], []).
simplify_solution([Coefficient * Object | Tail], SimplifiedSolution) :-
collect_object(Tail, Object, TailCoefficient, Others),
simplify(Coefficient + TailCoefficient, SimplifiedCoefficient),
(
SimplifiedCoefficient = 0
->
simplify_solution(Others, SimplifiedCoefficient)
;
SimplifiedSolution = [SimplifiedCoefficient * Object | SimplifiedTail],
simplify_solution(Tail, SimplifiedTail)
).
collect_object([], _Object, 0, []).
collect_object([Coefficient * Object | Tail], Object, Sum, Others) :-
!,
Sum = Coefficient + TailCoefficient,
collect_object(Tail, Object, TailCoefficient, Others).
collect_object([CoefficientObject | Tail], Object, Sum, Others) :-
Others = [CoefficientObject | OthersTail],
collect_object(Tail, Object, Sum, OthersTail).
simplify_catalyst([], Right, [], [], Right) :-
simplify_catalyst([Head | Tail], Right, NewLeft, Catalyst, NewRight) :-
(
select(Head, Right, OthersRight)
->
Catalyst = [Head | CatalystTail],
simplify_catalyst(Tail, Right, NewLeft, CatalystTail, NewRight)
build_rule(Kinetics, Left, Catalyst, Right, Reversible, Rule) :-
(
Kinetics = 'MA'(1)
->
Rule = Body
;
Rule = Kinetics for Body
),
list_to_solution(Left, LeftSolution),
list_to_solution(Catalyst, CatalystSolution),
list_to_solution(Right, RightSolution),
(
Reversible = yes
->
(
CatalystSolution = '_'
->
Body = (LeftSolution <=> RightSolution)
;
Body = (LeftSolution <=[ CatalystSolution ]=> RightSolution)
)
;
(
CatalystSolution = '_'
->
Body = (LeftSolution => RightSolution)
;
Body = (LeftSolution =[ CatalystSolution ]=> RightSolution)
)
).
......@@ -10,6 +10,7 @@
- counters.pl
- filename.pl
- doc.pl
- types.pl
* Syntax of Biocham Reaction Systems
** Biochemical objects
- objects.pl
......@@ -26,6 +27,13 @@
** Listing and defining initial states, molecules and locations
*** Initial state
- initial_state.pl
** Listing and declaring parameters, macros and invariants
*** Parameters
- parameters.pl
*** Macros
- macros.pl
*** Algebraic invariants, conservation laws and P-invariants
- conservation_laws.pl
** Simulations
*** ODE and stochastic simulations
- numerical_simulation.pl
:- module(units,
[
infer/2
]
).
infer(A + B, AUnit) :-
infer(A, AUnit),
infer(B, BUnit),
check_homogeneous(AUnit, BUnit).
infer(A - B, AUnit) :-
infer(A, AUnit),
infer(B, BUnit),
check_homogeneous(AUnit, BUnit).
infer(A * B, Unit) :-
infer(A, AUnit),
infer(B, BUnit),
product_unit(AUnit, BUnit, Unit).
infer(A / B, Unit) :-
infer(A, AUnit),
infer(B, BUnit),
inverse_unit(BUnit, BUnitInv),
product_unit(AUnit, BUnitInv, Unit).
infer(A ^ B, Unit) :-
infer(A, AUnit),
infer(B, BUnit),
inverse_unit(BUnit, BUnitInv),
product_unit(AUnit, BUnitInv, Unit).
check_homogeneous(AUnit, BUnit) :-
(
AUnit = BUnit
->
true
;
throw(error(not_homogeneous(A, AUnit, B, BUnit)))
).
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