sbml_qual_files.pl 6.57 KB
Newer Older
1 2 3 4 5
:- module(
  sbml_qual_files,
  [
    load_qual/1,
    add_qual/1,
6
    % API
7 8 9 10 11
    add_qual_file/1
  ]
).

:- use_module(library(sgml)).
Sylvain Soliman's avatar
Sylvain Soliman committed
12
:- use_module(library(xpath)).
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42

:- devdoc('\\section{Commands}').


load_qual(InputFile) :-
  biocham_command,
  type(InputFile, input_file),
  doc('
    acts as \\command{load_biocham/1} but importing influences and
    initial state (and only that!) from an SBML3qual .sbml file.
  '),
  load_all('sbml', InputFile).


add_qual(InputFile) :-
  biocham_command,
  type(InputFile, input_file),
  doc('
    acts as \\command{add_biocham/1} but importing influences and
    initial state (and only that!) from an SBML3qual .sbml file.
  '),
  add_all('sbml', InputFile).


:- devdoc('\\section{Public API}').


models:add_file_suffix('sbml', add_qual_file).


Sylvain Soliman's avatar
Sylvain Soliman committed
43 44 45
:- dynamic(qual_species/1).


46 47 48
add_qual_file(Filename) :-
  % FIXME should check for level=3 and qual:required=true in attributes of
  % sbml element
Sylvain Soliman's avatar
Sylvain Soliman committed
49 50 51 52
  load_xml(Filename, [element(sbml, _, Model)], [space(remove)]),
  retractall(qual_species(_)),
  forall(
    (
53
      xpath(Model, '//'('qual:qualitativeSpecies'),
Sylvain Soliman's avatar
Sylvain Soliman committed
54 55 56 57 58 59 60 61 62 63 64 65 66
        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(
67
    xpath(Model, '//'('qual:transition'),
Sylvain Soliman's avatar
Sylvain Soliman committed
68 69 70 71 72 73 74 75 76 77 78 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 104 105 106 107 108
      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], [])
  ),
109
  atom_bool(ResultLevel, AtomLevel),
Sylvain Soliman's avatar
Sylvain Soliman committed
110 111
  forall(
    function_terms(MathElt, Level),
112
    add_qual_influence(MathElt, OutputId, Level)
Sylvain Soliman's avatar
Sylvain Soliman committed
113
  ),
114 115 116 117 118 119 120 121 122
  findall(Cond, function_terms(and(Cond), _), Conds),
  forall(
    maplist(member, NonCond, Conds),
    (
      notlist(NonCond, NegatedCond),
      bool_simplify(and(NegatedCond), SimpleCond),
      add_qual_influence(SimpleCond, OutputId, AtomLevel)
    )
  ).
Sylvain Soliman's avatar
Sylvain Soliman committed
123 124 125 126 127 128 129

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),
130 131
  dnf(Bool, DBool),
  bool_simplify(DBool, SBool),
132 133 134 135 136 137 138 139 140 141
  (
    SBool = or(LBool)
  ->
    forall(
      member(OBool, LBool),
      assertz(function_terms(OBool, Result))
    )
  ;
    assertz(function_terms(SBool, Result))
  ),
Sylvain Soliman's avatar
Sylvain Soliman committed
142 143 144 145 146 147 148
  add_qual_functions(L, OutputId).

add_qual_functions([Default | L], OutputId) :-
  assertz(default_term(Default)),
  add_qual_functions(L, OutputId).


149 150 151 152 153 154 155 156 157 158
add_qual_influence(or(L), OutputId, Level) :-
  forall(member(X, L), add_qual_influence(X, OutputId, Level)).

add_qual_influence(and(L), OutputId, Level) :-
  pos_neg(L, Pos, Neg),
  level_arrow(Level, Arrow),
  influence_editor:inputs(Inputs, Pos, Neg),
  Influence =.. [Arrow, Inputs, OutputId],
  add_influence(Influence).

159 160
add_qual_influence(false, _, _).

161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181

level_arrow(Level, Arrow) :-
  (
    Level = 0
  ->
    Arrow = '-<'
  ;
    Arrow = '->'
  ).


pos_neg([], [], []).

pos_neg([not(A) | L], Pos, [A | Neg]) :-
  !,
  pos_neg(L, Pos, Neg).

pos_neg([A | L], [A | Pos], Neg) :-
  pos_neg(L, Pos, Neg).


Sylvain Soliman's avatar
Sylvain Soliman committed
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 207 208 209 210 211 212
% 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
  ).


213 214
bool_simplify(geq(_, 0), true) :-
  !.
Sylvain Soliman's avatar
Sylvain Soliman committed
215

216 217
bool_simplify(geq(X, 1), X) :-
  !.
Sylvain Soliman's avatar
Sylvain Soliman committed
218

219 220
bool_simplify(gt(X, _), X) :-
  !.
Sylvain Soliman's avatar
Sylvain Soliman committed
221

222 223
bool_simplify(leq(_, 1), true) :-
  !.
Sylvain Soliman's avatar
Sylvain Soliman committed
224

225 226
bool_simplify(leq(X, 0), not(X)) :-
  !.
Sylvain Soliman's avatar
Sylvain Soliman committed
227

228 229
bool_simplify(lt(X, _), not(X)) :-
  !.
Sylvain Soliman's avatar
Sylvain Soliman committed
230

231 232
bool_simplify(eq(X, 0), not(X)) :-
  !.
Sylvain Soliman's avatar
Sylvain Soliman committed
233

234 235
bool_simplify(eq(X, 1), X) :-
  !.
Sylvain Soliman's avatar
Sylvain Soliman committed
236

237
bool_simplify(and(L), And) :-
238
  !,
239 240 241
  maplist(bool_simplify, L, LL),
  list_to_set(LL, S),
  subtract(S, [true], LLL),
Sylvain Soliman's avatar
Sylvain Soliman committed
242
  (
243
    LLL = []
Sylvain Soliman's avatar
Sylvain Soliman committed
244
  ->
245
    And = true
Sylvain Soliman's avatar
Sylvain Soliman committed
246
  ;
247
    memberchk(false, LL)
Sylvain Soliman's avatar
Sylvain Soliman committed
248
  ->
249
    And = false
Sylvain Soliman's avatar
Sylvain Soliman committed
250
  ;
251 252
    member(X, LLL),
    member(not(X), LLL)
Sylvain Soliman's avatar
Sylvain Soliman committed
253 254 255
  ->
    And = false
  ;
256 257 258 259
    And = and(LLL)
  ).

bool_simplify(or(L), Or) :-
260
  !,
261 262 263 264 265
  maplist(bool_simplify, L, LL),
  list_to_set(LL, S),
  subtract(S, [false], LLL),
  (
    LLL = []
Sylvain Soliman's avatar
Sylvain Soliman committed
266
  ->
267
    Or = false
Sylvain Soliman's avatar
Sylvain Soliman committed
268
  ;
269
    memberchk(true, LL)
Sylvain Soliman's avatar
Sylvain Soliman committed
270
  ->
271
    Or = true
Sylvain Soliman's avatar
Sylvain Soliman committed
272
  ;
273 274 275 276 277 278 279 280 281 282
    member(X, LLL),
    member(not(X), LLL)
  ->
    Or = false
  ;
    LLL = [Or]
  ->
    true
  ;
    Or = or(LLL)
Sylvain Soliman's avatar
Sylvain Soliman committed
283 284
  ).

285 286
bool_simplify(X, X).

287 288 289 290 291

dnf(and(X, Y), And) :-
  !,
  dnf(X, XX),
  dnf(Y, YY),
Sylvain Soliman's avatar
Sylvain Soliman committed
292
  (
293 294
    XX = and(LX),
    YY = and(LY)
Sylvain Soliman's avatar
Sylvain Soliman committed
295
  ->
296 297
    append(LX, LY, LXY),
    And = and(LXY)
Sylvain Soliman's avatar
Sylvain Soliman committed
298
  ;
299 300
    XX = and(LX),
    YY = or(LY)
Sylvain Soliman's avatar
Sylvain Soliman committed
301
  ->
302 303
    findall(and([ELY | LX]), member(ELY, LY), LAnd),
    And = or(LAnd)
Sylvain Soliman's avatar
Sylvain Soliman committed
304
  ;
305 306
    XX = or(LX),
    YY = and(LY)
Sylvain Soliman's avatar
Sylvain Soliman committed
307
  ->
308 309 310 311 312 313 314 315
    findall(and([ELX | LY]), member(ELX, LX), LAnd),
    And = or(LAnd)
  ;
    XX = or(LX),
    YY = or(LY)
  ->
    findall(and([ELX, ELY]), (member(ELX, LX), member(ELY, LY)), LAnd),
    And = or(LAnd)
Sylvain Soliman's avatar
Sylvain Soliman committed
316
  ;
317 318 319 320 321 322 323 324 325 326
    throw(error('could not convert to DNF'))
  ).

dnf(or(X, Y), Or) :-
  !,
  dnf(X, XX),
  dnf(Y, YY),
  (
    XX = or(LX),
    YY = or(LY)
Sylvain Soliman's avatar
Sylvain Soliman committed
327
  ->
328 329
    append(LX, LY, LXY),
    Or = or(LXY)
Sylvain Soliman's avatar
Sylvain Soliman committed
330
  ;
331 332
    XX = and(_LX),
    YY = or(LY)
Sylvain Soliman's avatar
Sylvain Soliman committed
333
  ->
334
    Or = or([XX | LY])
Sylvain Soliman's avatar
Sylvain Soliman committed
335
  ;
336 337 338 339 340 341 342 343 344 345 346
    XX = or(LX),
    YY = and(_LY)
  ->
    Or = or([YY | LX])
  ;
    XX = and(_LX),
    YY = and(_LY)
  ->
    Or = or([XX, YY])
  ;
    throw(error('could not convert to DNF'))
Sylvain Soliman's avatar
Sylvain Soliman committed
347
  ).
348 349

dnf(X, and([X])).
350 351 352 353 354 355 356 357 358 359


notlist([], []).

notlist([not(H) | T], [H | NotT]) :-
  !,
  notlist(T, NotT).

notlist([H | T], [not(H) | NotT]) :-
  notlist(T, NotT).