ode.pl 12.4 KB
Newer Older
Thierry Martinez's avatar
Thierry Martinez committed
1 2 3
:- module(
  ode,
  [
4 5 6 7
    % Grammars
    oderef/1,
    op(1000, fx, init),
    % Commands
Thierry Martinez's avatar
Thierry Martinez committed
8 9 10 11 12 13 14 15 16 17 18 19
    new_ode_system/0,
    delete_ode_system/1,
    set_ode_system_name/1,
    list_ode_systems/0,
    select_ode_system/1,
    ode/1,
    add_ode/1,
    delete_ode/1,
    list_ode/0,
    ode_system/0,
    import_ode/1,
    import_reactions_from_ode_system/0,
Thierry Martinez's avatar
Thierry Martinez committed
20 21 22 23
    (init)/1,
    load_ode_system/1,
    add_ode_system/1,
    export_ode_system/1,
24
    % Public API
Thierry Martinez's avatar
Thierry Martinez committed
25
    new_ode_system/1,
Thierry Martinez's avatar
Thierry Martinez committed
26
    get_current_ode_system/1,
Thierry Martinez's avatar
Thierry Martinez committed
27 28 29 30 31 32 33
    set_current_ode_system/1,
    set_ode_system_name/2,
    import_ode/2,
    set_ode_initial_value/2,
    set_ode_initial_value/3,
    add_ode/2,
    import_reactions_from_ode_system/1,
Thierry Martinez's avatar
Thierry Martinez committed
34
    with_current_ode_system/1,
Thierry Martinez's avatar
ode  
Thierry Martinez committed
35
    edit_current_ode_system/1,
Thierry Martinez's avatar
Thierry Martinez committed
36
    all_odes/1,
Thierry Martinez's avatar
Thierry Martinez committed
37
    ode/2,
Thierry Martinez's avatar
Thierry Martinez committed
38
    ode/3,
39
    ode_add_expression_to_molecule/2
Thierry Martinez's avatar
Thierry Martinez committed
40 41 42
  ]).


43 44 45 46 47 48 49 50 51 52
:- devdoc('\\section{Grammars}').


:- grammar(oderef).


oderef(X) :-
  name(X).


Thierry Martinez's avatar
Thierry Martinez committed
53 54 55 56 57 58
:- devdoc('\\section{Commands}').


new_ode_system :-
  biocham_command,
  doc('creates an ODE system.'),
Thierry Martinez's avatar
Thierry Martinez committed
59
  new_ode_system(Id),
Thierry Martinez's avatar
Thierry Martinez committed
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
  set_current_ode_system(Id).


delete_ode_system(Name) :-
  biocham_command,
  type(Name, name),
  doc('deletes an ODE system.'),
  delete_items([kind: ode_system, key: Name]).


set_ode_system_name(Name) :-
  biocham_command,
  type(Name, name),
  doc('sets the name of the current ODE system.'),
  get_current_ode_system(Id),
Thierry Martinez's avatar
Thierry Martinez committed
75
  set_ode_system_name(Id, Name).
Thierry Martinez's avatar
Thierry Martinez committed
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


list_ode_systems :-
  biocham_command,
  doc('lists the ODE systems of the current model.'),
  list_items([kind: ode_system]).


select_ode_system(Name) :-
  biocham_command,
  type(Name, name),
  doc('selects an ODE system'),
  find_item([kind: ode_system, key: Name, id: Id]),
  set_current_ode_system(Id).


:- grammar(ode).


ode(d(X)/dt = E) :-
  name(X),
  arithmetic_expression(E).

ode(DX/dt = E) :-
  name(DX),
  arithmetic_expression(E).


add_ode(ODE) :-
  ode(ODE),
  get_current_ode_system(Id),
Thierry Martinez's avatar
ode  
Thierry Martinez committed
107
  !,
Thierry Martinez's avatar
Thierry Martinez committed
108
  add_ode(Id, ODE).
Thierry Martinez's avatar
Thierry Martinez committed
109 110 111 112 113


add_ode(ODEList) :-
  biocham_command(*),
  type(ODEList, '*'(ode)),
Thierry Martinez's avatar
ode  
Thierry Martinez committed
114 115
  doc('
    If there is a current ODE system, adds the given set of ODEs to it.
116 117 118
    If there is no current ODE system,
    imports the reactions inferred from the given set of ODEs.
  '),
Thierry Martinez's avatar
ode  
Thierry Martinez committed
119
  edit_current_ode_system((
Thierry Martinez's avatar
Thierry Martinez committed
120
    \+ (
Thierry Martinez's avatar
ode  
Thierry Martinez committed
121 122 123 124
      member(ODE, ODEList),
      \+ (
        add_ode(ODE)
      )
Thierry Martinez's avatar
Thierry Martinez committed
125
    )
Thierry Martinez's avatar
ode  
Thierry Martinez committed
126
  )).
Thierry Martinez's avatar
Thierry Martinez committed
127 128 129 130 131


import_reactions_from_ode_system :-
  biocham_command,
  doc('adds the reactions that match the current ODE system.'),
Thierry Martinez's avatar
Thierry Martinez committed
132 133
  get_current_ode_system(Id),
  import_reactions_from_ode_system(Id).
Thierry Martinez's avatar
Thierry Martinez committed
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148


delete_ode(ODERefList) :-
  biocham_command(*),
  type(ODERefList, '*'(oderef)),
  doc('removes the given set of ODEs from the current ODE system.'),
  \+ (
    member(Name, ODERefList),
    \+ (
      delete_single_ode(Name)
    )
  ).


list_ode :-
Thierry Martinez's avatar
Thierry Martinez committed
149
  biocham_command,
Thierry Martinez's avatar
Thierry Martinez committed
150 151
  doc('
    returns the set of ordinary differential equations
FAGES Francois's avatar
grrrr  
FAGES Francois committed
152
    and initial concentrations (one line per molecule)
FAGES Francois's avatar
merge  
FAGES Francois committed
153
    associated to the current model.
Thierry Martinez's avatar
Thierry Martinez committed
154 155 156 157
    \\begin{example}
  '),
  biocham_silent(clear_model),
  biocham(a => b),
Thierry Martinez's avatar
Thierry Martinez committed
158
  biocham(list_ode),
Thierry Martinez's avatar
Thierry Martinez committed
159 160 161
  doc('
    \\end{example}
  '),
Thierry Martinez's avatar
Thierry Martinez committed
162 163 164 165
  with_current_ode_system((
    get_current_ode_system(Id),
    list_items([parent: Id, kind: ode])
  )).
Thierry Martinez's avatar
Thierry Martinez committed
166 167 168 169


ode_system :-
  biocham_command,
170 171 172
  doc('
    builds the set of ODES corresponding to the reactions of the current model.
  '),
Thierry Martinez's avatar
Thierry Martinez committed
173 174 175
  delete_ode_system('ode_system'),
  new_ode_system,
  set_ode_system_name('ode_system'),
Thierry Martinez's avatar
Thierry Martinez committed
176 177 178 179 180 181 182 183
  with_clean(
    [ode:assoc/2],
    (
      ode:compute_ode,
      ode:simplify_ode,
      ode:put_ode_into_system
    )
  ).
Thierry Martinez's avatar
Thierry Martinez committed
184 185 186 187 188 189


import_ode(InputFile) :-
  biocham_command,
  type(InputFile, input_file),
  doc('imports a set of ODEs.'),
Thierry Martinez's avatar
Thierry Martinez committed
190 191 192
  import_ode(InputFile, Id),
  set_current_ode_system(Id).

Thierry Martinez's avatar
Thierry Martinez committed
193

Thierry Martinez's avatar
Thierry Martinez committed
194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211
init(InitList) :-
  biocham_command,
  type(InitList, '*'(name = arithmetic_expression)),
  doc('sets the initial value of a variable in the current set of ODEs.'),
  \+ (
    member(Variable = Value, InitList),
    \+ (
      set_ode_initial_value(Variable, Value)
    )
  ).


load_ode_system(InputFile) :-
  biocham_command,
  type(InputFile, input_file),
  doc('
    acts as \\command{load_biocham/1} but importing a model from an ODE system.
  '),
FAGES Francois's avatar
FAGES Francois committed
212
  models:load_all('ode', InputFile).
Thierry Martinez's avatar
Thierry Martinez committed
213 214 215 216 217 218 219 220


add_ode_system(InputFile) :-
  biocham_command,
  type(InputFile, input_file),
  doc('
    acts as \\command{add_biocham/1} but importing a model from an ODE system.
  '),
FAGES Francois's avatar
FAGES Francois committed
221
  models:load_all('ode', InputFile).
Thierry Martinez's avatar
Thierry Martinez committed
222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237


export_ode_system(OutputFile) :-
  biocham_command,
  type(OutputFile, output_file),
  doc('exports the current ODE system.'),
  with_current_ode_system((
    setup_call_cleanup(
      open(OutputFile, write, Stream),
      with_output_to(
        Stream,
        print_ode_system
      ),
      close(Stream)
    )
  )).
Thierry Martinez's avatar
Thierry Martinez committed
238 239 240 241

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


Thierry Martinez's avatar
Thierry Martinez committed
242 243 244 245
new_ode_system(Id) :-
  add_item([kind: ode_system, key: new_ode_system, id: Id]).


Thierry Martinez's avatar
Thierry Martinez committed
246 247 248 249
get_current_ode_system(Id) :-
  get_selection(current_model, current_ode_system, [Id]).


Thierry Martinez's avatar
Thierry Martinez committed
250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267
set_current_ode_system(Id) :-
  set_selection(current_model, current_ode_system, [Id]).


set_ode_system_name(Id, Name) :-
  replace_item(Id, ode_system, Name, Name).


import_ode(InputFile, Id) :-
  new_ode_system(Id),
  set_ode_system_name(Id, InputFile),
  setup_call_cleanup(
    open(InputFile, read, Stream),
    read_ode(Stream, Id),
    close(Stream)
  ).


Thierry Martinez's avatar
Thierry Martinez committed
268 269 270 271 272 273 274
all_odes(ODEs) :-
  get_current_ode_system(Id),
  all_items([parent: Id, kind: ode], ODEs).


ode(X, E) :-
  get_current_ode_system(Id),
Thierry Martinez's avatar
Thierry Martinez committed
275 276 277 278
  ode(Id, X, E).


ode(Id, X, E) :-
Thierry Martinez's avatar
Thierry Martinez committed
279 280 281 282 283 284 285 286 287
  (
    var(X)
  ->
    item([parent: Id, kind: ode, item: (d(X)/dt = E)])
  ;
    find_item([parent: Id, kind: ode, key: X, item: (d(X)/dt = E)])
  ).


Thierry Martinez's avatar
ode  
Thierry Martinez committed
288 289 290 291 292 293 294 295 296
ode_variables(Id, X) :-
  ode(Id, X, _).


ode_variables(Id, X) :-
  item([parent: Id, kind: initial_concentration, item: (init X = _)]),
  \+ item([parent: Id, kind: ode, key: X]).


Thierry Martinez's avatar
Thierry Martinez committed
297 298 299
set_ode_initial_value(Variable, Value) :-
  get_current_ode_system(Id),
  set_ode_initial_value(Id, Variable, Value).
Thierry Martinez's avatar
Thierry Martinez committed
300 301


Thierry Martinez's avatar
Thierry Martinez committed
302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322
set_ode_initial_value(Id, Variable, Value) :-
  change_item([parent: Id], initial_concentration, Variable, init Variable = Value).


add_ode(Id, ODE) :-
  (
    ODE = (d(X)/dt = _)
  ->
    Item = ODE
  ;
    ODE = (DX/dt = E),
    atom_concat(d, X, DX)
  ->
    Item = (d(X)/dt = E)
  ;
    throw(error(illformed_ode(ODE)))
  ),
  add_item([parent: Id, kind: ode, key: X, item: Item]).


import_reactions_from_ode_system(Id) :-
Thierry Martinez's avatar
ode  
Thierry Martinez committed
323 324 325 326 327 328 329 330 331
  check_cleaned(ode:assoc/2),
  enumerate_terms_in_odes(Id),
  add_terms_as_reactions(Id),
  clean(ode:assoc/2).

:- devdoc('\\section{Private predicates}').


enumerate_terms_in_odes(Id) :-
Thierry Martinez's avatar
Thierry Martinez committed
332 333 334
  \+ (
    ode(Id, X, E),
    \+ (
Thierry Martinez's avatar
ode  
Thierry Martinez committed
335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350
      enumerate_terms_in_ode(X, E)
    )
  ).


enumerate_terms_in_ode(X, E) :-
  \+ (
    non_decomposable_term_in_expression(Coefficient, Term, E),
    \+ (
      (
        retract(assoc(Term, Occurrences))
      ->
        true
      ;
        Occurrences = []
      ),
Thierry Martinez's avatar
Thierry Martinez committed
351
      (
Thierry Martinez's avatar
ode  
Thierry Martinez committed
352
        select((X, OtherCoefficient), Occurrences, OtherOccurrences)
Thierry Martinez's avatar
Thierry Martinez committed
353
      ->
Thierry Martinez's avatar
ode  
Thierry Martinez committed
354
        NewCoefficient is Coefficient + OtherCoefficient
Thierry Martinez's avatar
Thierry Martinez committed
355
      ;
Thierry Martinez's avatar
ode  
Thierry Martinez committed
356 357 358 359 360
        NewCoefficient = Coefficient,
        OtherOccurrences = Occurrences
      ),
      (
        NewCoefficient = 0
Thierry Martinez's avatar
Thierry Martinez committed
361
      ->
Thierry Martinez's avatar
ode  
Thierry Martinez committed
362 363 364 365 366
        NewOccurrences = OtherOccurrences
      ;
        NewOccurrences = [(X, NewCoefficient) | OtherOccurrences]
      ),
      assertz(assoc(Term, NewOccurrences))
Thierry Martinez's avatar
Thierry Martinez committed
367 368 369
    )
  ).

Thierry Martinez's avatar
Thierry Martinez committed
370

Thierry Martinez's avatar
ode  
Thierry Martinez committed
371 372 373
non_decomposable_term_in_expression(Coefficient, Term, E) :-
  additive_normal_form(E, ENormal),
  non_decomposable_term_in_additive_normal_form(Coefficient, Term, ENormal).
Thierry Martinez's avatar
Thierry Martinez committed
374 375


Thierry Martinez's avatar
ode  
Thierry Martinez committed
376 377 378 379 380 381 382
non_decomposable_term_in_additive_normal_form(Coefficient, Term, A + B) :-
  !,
  (
    non_decomposable_term_in_additive_normal_form(Coefficient, Term, A)
  ;
    non_decomposable_term_in_additive_normal_form(Coefficient, Term, B)
  ).
Thierry Martinez's avatar
Thierry Martinez committed
383

Thierry Martinez's avatar
ode  
Thierry Martinez committed
384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477
non_decomposable_term_in_additive_normal_form(Coefficient, Term, Coefficient * Term) :-
  number(Coefficient),
  !.

non_decomposable_term_in_additive_normal_form(1, Term, Term).


:- dynamic(reactant/2).


:- dynamic(product/2).


:- dynamic(inhibitor/2).


add_terms_as_reactions(Id) :-
  \+ (
    assoc(Term, Occurrences),
    \+ (
      add_term_as_reactions(Id, Term, Occurrences)
    )
  ).


add_term_as_reactions(Id, Term, Occurrences) :-
  check_cleaned(ode:reactant/2),
  check_cleaned(ode:product/2),
  check_cleaned(ode:inhibitor/2),
  \+ (
    member((X, Coefficient), Occurrences),
    \+ (
      (
        Coefficient < 0
      ->
        OppCoefficient is - Coefficient,
        assertz(reactant(X, OppCoefficient))
      ;
        assertz(product(X, Coefficient))
      )
    )
  ),
  \+ (
    ode_variables(Id, X),
    \+ (
      (
        \+ reactant(X, _),
        partial_has_pos_val(Term, X)
      ->
        assertz(reactant(X, 1)),
        (
          retract(product(X, OldCoefficient))
        ->
          true
        ;
          OldCoefficient = 0
        ),
        NewCoefficient is OldCoefficient + 1,
        assertz(product(X, NewCoefficient))
      ;
        true
      ),
      (
        partial_has_pos_val(- Term, X)
      ->
        assertz(inhibitor(X, 1))
      ;
        true
      )
    )
  ),
  findall(
    Coefficient * Object,
    reactant(Object, Coefficient),
    Reactants
  ),
  findall(
    Coefficient * Object,
    inhibitor(Object, Coefficient),
    _Inhibitors
  ),
  findall(
    Coefficient * Object,
    product(Object, Coefficient),
    Products
  ),
  add_reaction(Term, Reactants, Products, false),
  clean(ode:reactant/2),
  clean(ode:product/2),
  clean(ode:inhibitor/2).


partial_has_pos_val(Term, X) :-
  derivate(Term, X, DTerm),
Thierry Martinez's avatar
Thierry Martinez committed
478
  (
479
    always_negative(DTerm)
Thierry Martinez's avatar
Thierry Martinez committed
480
  ->
Thierry Martinez's avatar
ode  
Thierry Martinez committed
481
    fail
Thierry Martinez's avatar
Thierry Martinez committed
482 483 484 485 486
  ;
    true
  ).


Thierry Martinez's avatar
ode  
Thierry Martinez committed
487
:- dynamic(assoc/2).
Thierry Martinez's avatar
Thierry Martinez committed
488

Thierry Martinez's avatar
Thierry Martinez committed
489

Thierry Martinez's avatar
Thierry Martinez committed
490
compute_ode :-
491 492 493 494 495 496 497 498
  (
    is_reaction_model
  ->
    compute_ode_for_reaction_model
  ;
    is_influence_model
  ->
    compute_ode_for_influence_model
Thierry Martinez's avatar
Thierry Martinez committed
499
  ).
Thierry Martinez's avatar
Thierry Martinez committed
500 501 502 503


simplify_ode :-
  \+ (
Thierry Martinez's avatar
ode  
Thierry Martinez committed
504
    retract(assoc(Variable, Expression)),
Thierry Martinez's avatar
Thierry Martinez committed
505 506
    \+ (
      simplify(Expression, SimplifiedExpression),
Thierry Martinez's avatar
ode  
Thierry Martinez committed
507
      assertz(assoc(Variable, SimplifiedExpression))
Thierry Martinez's avatar
Thierry Martinez committed
508 509 510 511
    )
  ).


Thierry Martinez's avatar
Thierry Martinez committed
512
put_ode_into_system :-
Thierry Martinez's avatar
Thierry Martinez committed
513
  \+ (
Thierry Martinez's avatar
ode  
Thierry Martinez committed
514
    assoc(X, Expression),
Thierry Martinez's avatar
Thierry Martinez committed
515
    \+ (
Thierry Martinez's avatar
Thierry Martinez committed
516 517 518
      add_ode(d(X)/dt = Expression),
      get_initial_concentration(X, Concentration),
      set_ode_initial_value(X, Concentration)
Thierry Martinez's avatar
Thierry Martinez committed
519 520
    )
  ).
Thierry Martinez's avatar
Thierry Martinez committed
521 522


Thierry Martinez's avatar
Thierry Martinez committed
523 524 525
delete_single_ode(Name) :-
  get_current_ode_system(Id),
  delete_item([parent: Id, kind: ode, key: Name]).
Thierry Martinez's avatar
Thierry Martinez committed
526 527


Thierry Martinez's avatar
Thierry Martinez committed
528
read_ode(Stream, Id) :-
Thierry Martinez's avatar
Thierry Martinez committed
529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546
  repeat,
  read_line_to_codes(Stream, Codes),
  (
    Codes = end_of_file
  ->
    !
  ;
    read_term_from_codes(Codes, Expression, []),
    (
      Expression = end_of_file
    ->
      fail
    ;
      Expression = done
    ->
      !
    ;
      (
Thierry Martinez's avatar
Thierry Martinez committed
547
        Expression = (init X = E)
Thierry Martinez's avatar
Thierry Martinez committed
548
      ->
Thierry Martinez's avatar
Thierry Martinez committed
549
        set_ode_initial_value(Id, X, E)
Thierry Martinez's avatar
Thierry Martinez committed
550 551 552
      ;
        Expression = (_DX/dt = _E)
      ->
Thierry Martinez's avatar
Thierry Martinez committed
553
        add_ode(Id, Expression)
Thierry Martinez's avatar
Thierry Martinez committed
554 555 556 557 558 559 560
      )
    ->
      fail
    ;
      throw(error(syntax_error_in_ode_system(Codes, Expression)))
    )
  ).
Thierry Martinez's avatar
Thierry Martinez committed
561 562


Thierry Martinez's avatar
Thierry Martinez committed
563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597
monomial(Monomial, A + B) :-
  !,
  (
    monomial(Monomial, A)
  ;
    monomial(Monomial, B)
  ).

monomial(Monomial, A - B) :-
  !,
  (
    monomial(Monomial, A)
  ;
    monomial_opposite(Monomial, B)
  ).

monomial(A, A).


monomial_opposite(Monomial, A + B) :-
  !,
  (
    monomial_opposite(Monomial, A)
  ;
    monomial_opposite(Monomial, B)
  ).

monomial_opposite(Monomial, A - B) :-
  !,
  (
    monomial_opposite(Monomial, A)
  ;
    monomial(Monomial, B)
  ).

Thierry Martinez's avatar
ode  
Thierry Martinez committed
598
monomial_opposite(- A, A).
Thierry Martinez's avatar
Thierry Martinez committed
599 600 601 602 603 604 605


coefficient_species(Coefficient * Species, Coefficient, Species) :-
  !.

coefficient_species(Species, 1, Species).

Thierry Martinez's avatar
Thierry Martinez committed
606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629

models:add_file_suffix('ode', add_ode_system_file).


add_ode_system_file(InputFile) :-
  import_ode(InputFile),
  import_reactions_from_ode_system,
  get_current_ode_system(Id),
  delete_item(Id).


print_ode_system :-
  get_current_ode_system(Id),
  \+ (
    item([parent: Id, kind: initial_concentration, item: (init X = E)]),
    \+ (
      format('init ~p = ~p\n', [X, E])
    )
  ),
  \+ (
    item([parent: Id, kind: ode, item: (d(X)/dt = E)]),
    \+ (
      format('d~p/dt = ~p\n', [X, E])
    )
Thierry Martinez's avatar
Thierry Martinez committed
630
  ),
Thierry Martinez's avatar
Thierry Martinez committed
631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648
  format('done\n').


with_current_ode_system(Goal) :-
  (
    get_current_ode_system(_)
  ->
    Goal
  ;
    setup_call_cleanup(
      ode_system,
      Goal,
      (
        get_current_ode_system(Id),
        delete_item(Id)
      )
    )
  ).
Thierry Martinez's avatar
ode  
Thierry Martinez committed
649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666


edit_current_ode_system(Goal) :-
  (
    get_current_ode_system(_)
  ->
    Goal
  ;
    setup_call_cleanup(
      new_ode_system,
      Goal,
      (
        get_current_ode_system(Id),
        import_reactions_from_ode_system(Id),
        delete_item(Id)
      )
    )
  ).
667 668 669 670 671 672 673 674 675 676 677


ode_add_expression_to_molecule(NewExpression, Molecule) :-
  (
    retract(assoc(Molecule, Expression))
  ->
    true
  ;
    Expression = 0
  ),
  asserta(assoc(Molecule, Expression + NewExpression)).