influence_editor.pl 6.31 KB
Newer Older
Thierry Martinez's avatar
Thierry Martinez committed
1 2 3 4 5 6
:- module(
  influence_editor,
  [
    % Commands
    add_influence/1,
    list_influences/0,
7
    influence_model/0,
Thierry Martinez's avatar
Thierry Martinez committed
8 9 10
    % Public API
    is_influence_model/0,
    check_influence_model/0,
Thierry Martinez's avatar
Thierry Martinez committed
11
    with_influence_model/1,
12
    list_model_influences/0,
13
    influence/6,
14 15
    compute_ode_for_influence_model/0,
    print_influence/2
Thierry Martinez's avatar
Thierry Martinez committed
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
  ]
).


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


add_influence(Influence) :-
  biocham_command,
  type(Influence, influence),
  doc('
    adds influence rules to the current set of influences.
    This command is implicit: influence rules can be added directly in
    influence models.
  '),
  check_influence_model,
  add_item([kind: influence, item: Influence]).


35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
list_influences :-
  biocham_command,
  doc('
    lists the current set of influence rules.
    If the current model is a reaction model,
    the influence rules are inferred from the reaction model
    (see \\command{influence_model/0}).'),
  (
    is_influence_model
  ->
    list_items([kind: influence])
  ;
    is_reaction_model
  ->
    single_model(ReactionModel),
    setup_call_cleanup(
      new_model(InfluenceModel),
      (
        influence_model(ReactionModel, InfluenceModel),
        list_items([parent: InfluenceModel, kind: influence, no_numbering])
      ),
      delete_item(InfluenceModel)
    )
  ).
Thierry Martinez's avatar
Thierry Martinez committed
59 60


61
influence_model :-
Thierry Martinez's avatar
Thierry Martinez committed
62
  biocham_command,
63 64 65 66 67 68 69 70 71 72
  doc('
    creates a new influence model by inferring
    the influences between all molecular objects of the current reaction model
  '),
  check_reaction_model,
  single_model(ReactionModel),
  new_model,
  set_model_name(influence_model),
  single_model(InfluenceModel),
  influence_model(ReactionModel, InfluenceModel).
Thierry Martinez's avatar
Thierry Martinez committed
73 74 75 76 77 78 79 80 81 82 83 84 85 86


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


is_influence_model :-
   devdoc('
     succeeds if the current model is an influence model
     (i.e., does not contain any reaction rules).
   '),
   \+ item([kind: reaction]).


check_influence_model :-
87 88 89 90 91
  devdoc('
     succeeds if the current model is an influence model
     (i.e., does not contain any reaction rules)
     and throws an exception otherwise.
  '),
Thierry Martinez's avatar
Thierry Martinez committed
92 93 94 95 96 97 98 99 100
  (
    is_influence_model
  ->
    true
  ;
    throw(error(not_an_influence_model, check_influence_model))
  ).


Thierry Martinez's avatar
Thierry Martinez committed
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
with_influence_model(G) :-
  (
    is_influence_model
  ->
    G
  ;
    single_model(ReactionModel),
    setup_call_cleanup(
      new_model(InfluenceModel),
      (
        influence_model(ReactionModel, InfluenceModel),
        set_current_models([InfluenceModel]),
        G
      ),
      (
        delete_item(InfluenceModel),
        set_current_models([ReactionModel])
      )
    )
  ).


123 124 125 126 127 128
list_model_influences :-
  devdoc('
    lists all the influence rules in a loadable syntax
    (auxiliary predicate of list_model).
  '),
  \+ (
129
    item([no_inheritance, kind: influence, id: Id, item: Influence]),
130
    \+ (
131 132
      print_influence(Id, Influence),
      write('.\n')
133 134 135 136
    )
  ).


Thierry Martinez's avatar
Thierry Martinez committed
137 138 139 140
prolog:error_message(not_an_influence_model) -->
  ['Not an influence model'].


Thierry Martinez's avatar
Thierry Martinez committed
141 142 143 144 145
influence(InfluenceRule, Force, PositiveInputs, NegativeInputs, Sign, Output) :-
  devdoc(
    'builds or decomposes an influence rule.'
  ),
  once((
146
    Force = 'MA'(1),
Thierry Martinez's avatar
Thierry Martinez committed
147 148 149 150 151 152 153 154 155 156 157
    (
      var(InfluenceRule)
    ->
      true
    ;
      InfluenceRule \= (_ for _)
    ),
    BasicInfluenceRule = InfluenceRule
  ;
    InfluenceRule = (Force for BasicInfluenceRule)
  )),
Thierry Martinez's avatar
Thierry Martinez committed
158
  once((
Thierry Martinez's avatar
Thierry Martinez committed
159
    BasicInfluenceRule = (Inputs -> Output),
Thierry Martinez's avatar
Thierry Martinez committed
160 161
    Sign = +
  ;
Thierry Martinez's avatar
Thierry Martinez committed
162
    BasicInfluenceRule = (Inputs -< Output),
Thierry Martinez's avatar
Thierry Martinez committed
163 164 165 166 167
    Sign = -
  )),
  inputs(Inputs, PositiveInputs, NegativeInputs).


168 169 170 171 172 173 174 175 176 177 178 179 180
compute_ode_for_influence_model :-
  \+ (
    item([kind: influence, item: Item]),
    influence(Item, Force, PositiveInputs, _NegativeInputs, Sign, Output),
    \+ (
      maplist(add_coefficient, PositiveInputs, PositiveInputsWithCoefficient),
      kinetics(PositiveInputsWithCoefficient, Force, ForceExpression),
      sign(Sign, ForceExpression, SignedForce),
      ode_add_expression_to_molecule(SignedForce, Output)
    )
  ).


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 207 208 209 210 211
print_influence(_Id, Item) :-
  (
    Item = (Force for BasicInfluence)
  ->
    format('~w for ', [Force])
  ;
    BasicInfluence = Item
  ),
  (
    BasicInfluence = (Inputs -> Output)
  ->
    Arrow = '->'
  ;
    BasicInfluence = (Inputs -< Output)
  ->
    Arrow = '-<'
  ),
  (
    Inputs = (PositiveInputs / NegativeInputs)
  ->
    format('~w / ~w', [PositiveInputs, NegativeInputs])
  ;
    Inputs = (/ NegativeInputs)
  ->
    format('/ ~w', [NegativeInputs])
  ;
    write(Inputs)
  ),
  format(' ~w ~w', [Arrow, Output]).


Thierry Martinez's avatar
Thierry Martinez committed
212 213 214
:- devdoc('\\section{Private predicates}').


215 216 217 218 219 220 221 222
add_coefficient(C, 1 * C).


sign(-, Force, - Force).

sign(+, Force, Force).


223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273
influence_model(ReactionModel, InfluenceModel) :-
  \+ (
    item([parent: ReactionModel, kind: reaction, item: Reaction]),
    reaction(Reaction, _Kinetics, Reactants, Products),
    \+ (
      substract_list(Reactants, Products, Difference),
      create_influences(Reactants, Difference, InfluenceModel)
    )
  ).


substract_list([], Difference, Difference).

substract_list([Coefficient0 * Object | Tail], Products, Difference) :-
  (
    select(Coefficient1 * Object, Products, Others)
  ->
    Coefficient is Coefficient1 - Coefficient0,
    (
      Coefficient = 0
    ->
      substract_list(Tail, Others, Difference)
    ;
      substract_list(Tail, [Coefficient * Object | Others], Difference)
    )
  ;
    Coefficient is - Coefficient0,
    substract_list(Tail, [Coefficient * Object | Products], Difference)
  ).


create_influences(Reactants, Difference, InfluenceModel) :-
  \+ (
    member(_ * Input, Reactants),
    member(Coefficient * Output, Difference),
    \+ (
      (
        Coefficient > 0
      ->
        add_item(
          [parent: InfluenceModel, kind: influence, item: (Input -> Output)]
        )
      ;
        add_item(
          [parent: InfluenceModel, kind: influence, item: (Input -< Output)]
        )
      )
    )
  ).


Thierry Martinez's avatar
Thierry Martinez committed
274 275 276 277 278 279
inputs(PositiveInputEnum, PositiveInputList, []) :-
  (
    var(PositiveInputEnum)
  ->
    true
  ;
280 281
    PositiveInputEnum \= (_ / _),
    PositiveInputEnum \= (/ _)
Thierry Martinez's avatar
Thierry Martinez committed
282 283 284 285
  ),
  list_enumeration(PositiveInputList, PositiveInputEnum),
  !.

286
inputs(/ NegativeInputEnum, [], NegativeInputList) :-
Thierry Martinez's avatar
Thierry Martinez committed
287 288 289 290
  list_enumeration(NegativeInputList, NegativeInputEnum),
  !.

inputs(
291
  PositiveInputEnum / NegativeInputEnum,
Thierry Martinez's avatar
Thierry Martinez committed
292 293 294 295 296 297
  PositiveInputList,
  NegativeInputList
) :-
  list_enumeration(PositiveInputList, PositiveInputEnum),
  list_enumeration(NegativeInputList, NegativeInputEnum),
  !.