model_correction.pl 5.01 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
/*
Implement the automatic correction of models loaded from a sbml file
Coder: M. Hemery
*/

:- module(
  correction,
  [
  % Commands
  correct_model/0
  % API
  ]
).

% Only for separate compilation/linting
:- use_module(arithmetic_rules).
:- use_module(doc).
:- use_module(formal_derivation).
:- use_module(reaction_rules).
:- use_module(util).

correct_model :-
  biocham_command,
  doc(''),
  single_model(ModelId),
26
  setof(
27
28
29
30
    Molecule,
    identifier_kind(ModelId, Molecule, object),
    List_Molecule
  ),
31
  % First loop that add/delete/split reactions
32
33
34
  reactions_with_species(List_Molecule, List_Reaction),
  forall(
    member(Reaction, List_Reaction),
35
36
37
38
    (
      remove_null_kinetic(Reaction),!
    ;
      split_bidirectional_reaction(Reaction),!
39
40
    ;
      reverse_reaction(Reaction),!
41
42
43
    ;
      true
    )
44
45
46
47
48
49
  ),
  % Second loop to add catalyst/inhibitor
  reactions_with_species(List_Molecule, Sec_List_Reaction),
  forall(
    member(Reaction, Sec_List_Reaction),
    correct_modifiers(List_Molecule, Reaction)
50
51
52
53
54
55
56
57
  ).

%! remove_null_kinetic(+Reaction)
%
% Detect and remove reactions with identically null kinetics

remove_null_kinetic(Expression for Reaction) :-
  (
58
59
    ode:substitute_functions(Expression, Expr),
    is_null(Expr)
60
61
  ->
    delete_reaction(Expression for Reaction)
62
63
64
65
66
67
68
69
  ).

%! split_bidirectional_reaction(+Reaction)
%
% Split a bidirection reaction in two

split_bidirectional_reaction(Expression for Reactant => Product) :-
  (
70
71
    ode:substitute_functions(Expression, Expr),
    distribute(Expr, DistExpr),
72
73
74
    DistExpr = Up - Down,
    models:formal_product(P, Reactant => Product),
    present_in_kinetics(P, Down)
75
76
77
78
  ->
    delete_reaction(Expression for Reactant => Product),
    add_reaction(Up for Reactant => Product),
    add_reaction(Down for Product => Reactant)
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
  ).

%! reverse_reaction(+Reaction)
%
% Reverse a reaction if its kinetic is negative

reverse_reaction(Expression for Reactant => Product) :-
  (
    ode:substitute_functions(Expression, - RevExpr)
  ->
    delete_reaction(Expression for Reactant => Product),
    add_reaction(RevExpr for Product => Reactant)
  ).


%! correct_modifiers(+List_Molecule, +Reaction)
%
% Ensure that the modifiers are correctly formulated

correct_modifiers(List_Molecule, Expression_raw for Reaction) :-
  ode:substitute_functions(Expression_raw, Expression),
  detect_modifiers(List_Molecule, Expression, Catalysts, Inhibitors),
  (
    append(Catalysts, Inhibitors, [])
  ->
104
    true
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
  ;
    add_catalysts(Catalysts, Reaction, ReactionTempo),
    add_inhibitors(Inhibitors, ReactionTempo, NewReaction),
    delete_reaction(Expression_raw for Reaction),
    add_reaction(Expression_raw for NewReaction)
  ).


detect_modifiers([], _Expression, [], []).

detect_modifiers([Mol|TailM], Expression, NewC, NewI) :-
  derivate(Expression, Mol, Deriv),
  (
    is_null(Deriv)
  ->
    NewC = TailC,
    NewI = TailI
  ;
    always_negative(Deriv)
  ->
    NewC = TailC,
    NewI = [Mol|TailI]
  ;
    NewC = [Mol|TailC],
    NewI = TailI
  ),
  detect_modifiers(TailM, Expression, TailC, TailI).

add_catalysts([], Reaction, Reaction).
add_catalysts([Head|Tail], Reaction, NewReaction) :-
  models:formal_reactant(Head, Reaction),
  !,
  add_catalysts(Tail, Reaction, NewReaction). 
add_catalysts([Head|Tail], Reactants => Products, NewReactants => NProducts+Head) :-
  add_catalysts(Tail, Reactants => Products, NReactants => NProducts),
  (
    NReactants = Reac / Inhib
  ->
    NewReactants = Reac+Head / Inhib
  ;
    NewReactants = Reactants+Head
  ).

148
% TODO -- remove Head from Reactants & Products if necessary
149
150
151
152
153
add_inhibitors([], Reaction, Reaction).
add_inhibitors([Head|Tail], Reaction, NewReaction) :-
  models:formal_inhibitor(Head, Reaction),
  !,
  add_inhibitors(Tail, Reaction, NewReaction). 
Mathieu Hemery's avatar
Mathieu Hemery committed
154
155
156
157
158
159
160
161
162
add_inhibitors([Head|Tail], Reactants => Products, NewReactants => NProducts) :-
  add_inhibitors(Tail, Reactants => Products, ReactantsTempo => ProductsTempo),
  (
    remove_molecule(ReactantsTempo, Head, NReactants),
    remove_molecule(ProductsTempo, Head, NProducts)
  ;
    NReactants = ReactantsTempo,
    NProducts = ProductsTempo
  ),
163
164
165
166
167
  (
    NReactants = Reac / Inhib
  ->
    NewReactants = Reac / (Head,Inhib)
  ;
Mathieu Hemery's avatar
Mathieu Hemery committed
168
    NewReactants = NReactants / Head
169
  ).
Mathieu Hemery's avatar
Mathieu Hemery committed
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206

% remove_molecule(Species, Molecule, Without)

remove_molecule(Molecule, Molecule, '_') :- !.
remove_molecule(P*Molecule, Molecule, Result) :- 
  (
    P = 1
  ->
    Result = '_'
  ;
    P = 2
  ->
    Result = Molecule
  ;
    Pm is P-1,
    Result = Pm*Molecule
  ),!.
remove_molecule(Species+Molecule, Molecule, Species) :- !.
remove_molecule(Species+P*Molecule, Molecule, Result) :-
  (
    P = 1
  ->
    Result = Species
  ;
    P = 2
  ->
    Result = Species+Molecule
  ;
    Pm is P-1,
    Result = Species+Pm*Molecule
  ),!.
remove_molecule(Molecule+Other, Molecule, Result) :- !,
  remove_molecule(Other+Molecule, Molecule, Result).
remove_molecule(P*Molecule+Other, Molecule, Result) :- !,
  remove_molecule(Other+P*Molecule, Molecule, Result).
remove_molecule(Species+Other, Molecule, Without+Other) :-
  remove_molecule(Species, Molecule, Without).