influence_editor.pl 6.05 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,
11
    list_model_influences/0,
12
    influence/6,
13 14
    compute_ode_for_influence_model/0,
    print_influence/2
Thierry Martinez's avatar
Thierry Martinez committed
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
  ]
).


:- 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]).


34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
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
58 59


60
influence_model :-
Thierry Martinez's avatar
Thierry Martinez committed
61
  biocham_command,
62 63 64 65 66 67 68 69 70 71
  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
72 73 74 75 76 77 78 79 80 81 82 83 84 85


:- 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 :-
86 87 88 89 90
  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
91 92 93 94 95 96 97 98 99
  (
    is_influence_model
  ->
    true
  ;
    throw(error(not_an_influence_model, check_influence_model))
  ).


100 101 102 103 104 105
list_model_influences :-
  devdoc('
    lists all the influence rules in a loadable syntax
    (auxiliary predicate of list_model).
  '),
  \+ (
106
    item([no_inheritance, kind: influence, id: Id, item: Influence]),
107
    \+ (
108 109
      print_influence(Id, Influence),
      write('.\n')
110 111 112 113
    )
  ).


Thierry Martinez's avatar
Thierry Martinez committed
114 115 116 117
prolog:error_message(not_an_influence_model) -->
  ['Not an influence model'].


Thierry Martinez's avatar
Thierry Martinez committed
118 119 120 121 122
influence(InfluenceRule, Force, PositiveInputs, NegativeInputs, Sign, Output) :-
  devdoc(
    'builds or decomposes an influence rule.'
  ),
  once((
123
    Force = 'MA'(1),
Thierry Martinez's avatar
Thierry Martinez committed
124 125 126 127 128 129 130 131 132 133 134
    (
      var(InfluenceRule)
    ->
      true
    ;
      InfluenceRule \= (_ for _)
    ),
    BasicInfluenceRule = InfluenceRule
  ;
    InfluenceRule = (Force for BasicInfluenceRule)
  )),
Thierry Martinez's avatar
Thierry Martinez committed
135
  once((
Thierry Martinez's avatar
Thierry Martinez committed
136
    BasicInfluenceRule = (Inputs -> Output),
Thierry Martinez's avatar
Thierry Martinez committed
137 138
    Sign = +
  ;
Thierry Martinez's avatar
Thierry Martinez committed
139
    BasicInfluenceRule = (Inputs -< Output),
Thierry Martinez's avatar
Thierry Martinez committed
140 141 142 143 144
    Sign = -
  )),
  inputs(Inputs, PositiveInputs, NegativeInputs).


145 146 147 148 149 150 151 152 153 154 155 156 157
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)
    )
  ).


158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188
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
189 190 191
:- devdoc('\\section{Private predicates}').


192 193 194 195 196 197 198 199
add_coefficient(C, 1 * C).


sign(-, Force, - Force).

sign(+, Force, Force).


200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 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
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
251 252 253 254 255 256
inputs(PositiveInputEnum, PositiveInputList, []) :-
  (
    var(PositiveInputEnum)
  ->
    true
  ;
257 258
    PositiveInputEnum \= (_ / _),
    PositiveInputEnum \= (/ _)
Thierry Martinez's avatar
Thierry Martinez committed
259 260 261 262
  ),
  list_enumeration(PositiveInputList, PositiveInputEnum),
  !.

263
inputs(/ NegativeInputEnum, [], NegativeInputList) :-
Thierry Martinez's avatar
Thierry Martinez committed
264 265 266 267
  list_enumeration(NegativeInputList, NegativeInputEnum),
  !.

inputs(
268
  PositiveInputEnum / NegativeInputEnum,
Thierry Martinez's avatar
Thierry Martinez committed
269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285
  PositiveInputList,
  NegativeInputList
) :-
  list_enumeration(PositiveInputList, PositiveInputEnum),
  list_enumeration(NegativeInputList, NegativeInputEnum),
  !.


list_enumeration([], '_') :-
  !.

list_enumeration([H], H) :-
  H \= (_, _),
  !.

list_enumeration([H | List], (H, Enum)) :-
  list_enumeration(List, Enum).