-
Notifications
You must be signed in to change notification settings - Fork 0
/
exporter.py
946 lines (839 loc) · 39.9 KB
/
exporter.py
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
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
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
148
149
150
151
152
153
154
155
156
157
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
189
190
191
192
193
194
195
196
197
198
199
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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
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
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
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
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
#! /usr/bin/env python3
# I, Robert Rozanski, the copyright holder of this work, release this work into the public domain. This applies worldwide. In some countries this may not be legally possible; if so: I grant anyone the right to use this work for any purpose, without any conditions, unless such conditions are required by law.
import mnm_repr
import exp_repr
from exp_repr import DetectionEntity, LocalisationEntity, DetectionActivity, AdamTwoFactorExperiment
from mnm_repr import Activity, Condition
def export_entities(entities):
strings = []
for ent in entities:
if isinstance(ent, mnm_repr.Gene):
strings.append("\ngene(%s,%s)." %(ent.ID, ent.version))
elif isinstance(ent, mnm_repr.Metabolite):
strings.append("\nmetabolite(%s,%s)." %(ent.ID, ent.version))
elif isinstance(ent, mnm_repr.Protein):
strings.append("\nprotein(%s,%s)." %(ent.ID, ent.version))
elif isinstance(ent, mnm_repr.Complex):
strings.append("\ncomplex(%s,%s)." %(ent.ID, ent.version))
else:
raise TypeError("export_entities: entity type not recognised:%s" % type(ent))
if ent.properties == frozenset([]):
continue
for prop in ent.properties:
if isinstance(prop, mnm_repr.Catalyses):
strings.append("\ncatalyses(%s,%s,%s)." % (ent.ID, ent.version, prop.activity.ID))
elif isinstance(prop, mnm_repr.Transports):
strings.append("\ntransports(%s,%s,%s)." % (ent.ID, ent.version, prop.activity.ID))
else:
raise TypeError("export_entities: property type not recognised:%s" % type(prop))
return strings
def export_compartments(compartments):
comps = ";".join([comp.ID for comp in compartments])
return [comps.join(['\ncompartment(', ').'])]
def export_activities(activities):
strings = []
for act in activities:
if act.reversibility == False:
if isinstance(act, mnm_repr.Growth):
strings.append('\ngrowth(%s).' % act.ID)
elif isinstance(act, mnm_repr.Expression):
strings.append('\nexpression(%s).' % act.ID)
elif isinstance(act, mnm_repr.Reaction):
strings.append('\nreaction(%s).' % act.ID)
elif isinstance(act, mnm_repr.Transport):
strings.append('\ntransport(%s).' % act.ID)
elif isinstance(act, mnm_repr.ComplexFormation):
strings.append('\ncomplex_formation(%s).' % act.ID)
else:
raise TypeError("export_activities: activity type not recognised: %s" % type(act))
elif act.reversibility == True:
strings.append('\nreverse(%s,%s_rev).' % (act.ID,act.ID))
if isinstance(act, mnm_repr.Growth):
strings.append('\ngrowth(%s).' % act.ID)
strings.append('\ngrowth(%s_rev).' % act.ID)
elif isinstance(act, mnm_repr.Expression):
strings.append('\nexpression(%s).' % act.ID)
strings.append('\nexpression(%s_rev).' % act.ID)
elif isinstance(act, mnm_repr.Reaction):
strings.append('\nreaction(%s).' % act.ID)
strings.append('\nreaction(%s_rev).' % act.ID)
elif isinstance(act, mnm_repr.Transport):
strings.append('\ntransport(%s).' % act.ID)
strings.append('\ntransport(%s_rev).' % act.ID)
elif isinstance(act, mnm_repr.ComplexFormation):
strings.append('\ncomplex_formation(%s).' % act.ID)
strings.append('\ncomplex_formation(%s_rev).' % act.ID)
else:
raise TypeError("export_activities: activity type not recognised: %s" % type(act))
else:
raise ValueError("export_activities: activity's reversibility status not specified")
for act in activities:
strings.extend(export_activity(act))
return strings
def export_activity(activity):
strings = []
if activity.reversibility == False:
for req in activity.required_conditions:
strings.extend(export_required_condition(req, activity))
for change in activity.changes:
strings.extend(export_change(change, activity))
elif activity.reversibility == True:
for req in activity.required_conditions:
strings.extend(export_required_condition(req, activity))
for change in activity.changes:
strings.extend(export_change(change, activity))
# export reverse version
for change in list(activity.changes) + [x for x in activity.required_conditions if (not isinstance(x, mnm_repr.PresentEntity))]:
strings.extend(export_required_condition(change, activity, '_rev'))
for req in [x for x in activity.required_conditions if isinstance(x, mnm_repr.PresentEntity)]:
strings.extend(export_change(req, activity, '_rev'))
else:
raise ValueError("export_activity: activity's reversibility status not specified")
return strings
def export_required_condition(req, activity, suffix=''):
strings = []
if isinstance(req, mnm_repr.PresentEntity):
strings.append('\nsubstrate(%s,%s,%s,%s%s).' % (req.entity.ID, req.entity.version, req.compartment.ID, activity.ID, suffix))
elif isinstance(req, mnm_repr.PresentCatalyst):
strings.append('\nenz_required(%s%s).' % (activity.ID, suffix))
strings.append('\nenz_compartment(%s,%s%s).' % (req.compartment.ID, activity.ID, suffix))
elif isinstance(req, mnm_repr.PresentTransporter):
strings.append('\ntransp_required(%s%s).' % (activity.ID, suffix))
strings.append('\ntransp_compartment(%s,%s%s).' % (req.compartment.ID, activity.ID, suffix))
else:
raise TypeError("export_activity: requirement type not recognised:%s" % type(req))
return strings
def export_change(change, activity, suffix=''):
return ['\nproduct(%s,%s,%s,%s%s).' % (change.entity.ID, change.entity.version, change.compartment.ID, activity.ID, suffix)]
def export_results(results):
strings = []
for result in results:
ID = result.ID
out = result.outcome
if isinstance(result.exp_description.experiment_type, exp_repr.ReconstructionTransporterRequired):
act = result.exp_description.experiment_type.transport_activity_id
trp = result.exp_description.experiment_type.transporter_id
strings.append('\nresult(%s, experiment(transp_reconstruction_exp, %s, %s), %s).' % (ID, act, trp, out))
elif isinstance(result.exp_description.experiment_type, exp_repr.ReconstructionEnzReaction):
act = result.exp_description.experiment_type.reaction_id
enz = result.exp_description.experiment_type.enzyme_id
strings.append('\nresult(%s, experiment(enz_reconstruction_exp, %s, %s), %s).' % (ID, act, enz, out))
elif isinstance(result.exp_description.experiment_type, exp_repr.ReconstructionActivity):
act = result.exp_description.experiment_type.activity_id
strings.append('\nresult(%s, experiment(basic_reconstruction_exp, %s), %s).' % (ID, act, out))
elif isinstance(result.exp_description.experiment_type, exp_repr.AdamTwoFactorExperiment):
gene = result.exp_description.experiment_type.gene_id
met = result.exp_description.experiment_type.metabolite_id
strings.append('\nresult(%s, experiment(adam_two_factor_exp, %s, %s), %s).' % (ID, gene, met, out))
elif isinstance(result.exp_description.experiment_type, exp_repr.DetectionActivity):
act = result.exp_description.experiment_type.activity_id
strings.append('\nresult(%s, experiment(detection_activity_exp, %s), %s).' % (ID, act, out))
elif isinstance(result.exp_description.experiment_type, exp_repr.LocalisationEntity):
ent = result.exp_description.experiment_type.entity_id
comp = result.exp_description.experiment_type.compartment_id
strings.append('\nresult(%s, experiment(localisation_entity_exp, %s, %s), %s).' % (ID, ent, comp, out))
elif isinstance(result.exp_description.experiment_type, exp_repr.DetectionEntity):
ent = result.exp_description.experiment_type.entity_id
strings.append('\nresult(%s, experiment(detection_entity_exp, %s), %s).' % (ID, ent, out))
else:
raise TypeError('export_results: result type not recognised:%s' % type(result))
return strings
def export_models(models_results):
strings = []
# model().
joined_models = ';'.join([x.ID for x in models_results.keys()])
strings.append(joined_models.join(['\nmodel(', ').']))
# specification:
for model in models_results.keys():
strings.extend(export_model_specification(model))
return strings
def export_model_specification(model):
strings = []
# setup
for cond in model.setup_conditions:
strings.append('\nadded_to_model(setup_present(%s,%s,%s),%s).' % (cond.entity.ID, cond.entity.version, cond.compartment.ID, model.ID))
# activities
for act in model.intermediate_activities:
strings.append('\nadded_to_model(%s,%s).' % (act.ID, model.ID))
return strings
def export_termination_conds_revision(base_model):
strings = []
for cond in base_model.termination_conditions:
strings.append('\n#example synthesizable(%s, %s, %s, %s).' % (cond.entity.ID, cond.entity.version, cond.compartment.ID, base_model.ID))
# base model shouldn't have inactive activities
# or more than one version of any entity (would restrict derived models too, so OK)
strings.append('\n#example not not_clean_model(%s).' % base_model.ID)
return strings
def export_relevancy_results_revision(models_results):
strings = []
for model in models_results.keys():
for res in models_results[model]:
strings.append('\nrelevant(%s, %s).' % (res.ID, model.ID))
strings.append('\n#example not inconsistent(%s, %s).' % (model.ID, res.ID))
return strings
def export_termination_conds_consistency(base_model):
strings = []
for cond in base_model.termination_conditions:
strings.append('\n:- not synthesizable(%s, %s, %s, %s).' % (cond.entity.ID, cond.entity.version, cond.compartment.ID, base_model.ID))
# base model shouldn't have inactive activities
# or more than one version of any entity (would restrict derived models too, so OK)
strings.append('\n:- not_clean_model(%s).' % base_model.ID)
return strings
def export_relevancy_results_consistency(models_results, base_model):
strings = []
for model in models_results.keys():
for res in models_results[model]:
if res in base_model.ignored_results:
continue
else:
strings.append('\nrelevant(%s, %s).' % (res.ID, model.ID))
strings.append('\n:- inconsistent(%s, %s).' % (model.ID, res.ID))
return strings
def export_force_new_model(base_model, external_models):
strings = []
for model in external_models:
# external model specification
strings.append('\nexternal_model(%s).' % model.ID)
for activity in model.intermediate_activities:
strings.append('\nin_model(%s,%s).' % (activity.ID, model.ID))
# constraint
strings.append('\n#example different(%s, %s).' % (base_model.ID, model.ID))
return strings
def export_add_activities(activities):
return ['\n#modeh add(%s) =%s @1.' % (act.ID, act.add_cost) for act in activities]
def export_remove_activities(activities):
return ['\n#modeh remove(%s) =%s @1.' % (act.ID, act.remove_cost) for act in activities]
def export_ignore_results(results):
return ['\n#modeh ignored(%s) =%s @2.' % (result.ID, result.exp_description.experiment_type.ignoring_penalty) for result in results]
def models_rules(max_number_activities):
return [
'\n%%% catalysis/ transport of reversed activities:',
'\ncatalyses(Entity, Version, ReverseActivity) :-',
'\n catalyses(Entity, Version, BaseActivity),',
'\n reverse(BaseActivity, ReverseActivity).',
'\n',
'\ntransports(Entity, Version, ReverseActivity) :-',
'\n transports(Entity, Version, BaseActivity),',
'\n reverse(BaseActivity, ReverseActivity).',
'\n',
'\n%%% automatically adding reverse activities to models:',
'\nadded_to_model(ActivityRev, Model) :-',
'\n added_to_model(BaseActivity, Model),',
'\n reverse(BaseActivity, ActivityRev).',
'\n',
'\n%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%',
'\n%%%%% model specification rules %%%%%',
'\n%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%',
'\nactivity(Activity) :- reaction(Activity).',
'\nactivity(Activity) :- transport(Activity).',
'\nactivity(Activity) :- expression(Activity).',
'\nactivity(Activity) :- complex_formation(Activity).',
'\nactivity(Activity) :- growth(Activity).',
'\n',
'\nentity(Entity, Version) :- metabolite(Entity, Version).',
'\nentity(Entity, Version) :- complex(Entity, Version).',
'\nentity(Entity, Version) :- protein(Entity, Version).',
'\nentity(Entity, Version) :- gene(Entity, Version).',
'\n',
'\nconnected_compartments(Compartment2,Compartment1) :- connected_compartments(Compartment1,Compartment2).',
'\nconnected_compartments(Compartment,Compartment) :- compartment(Compartment).',
'\n',
'\nin_model(ActivityOrSetup, Model) :-',
'\n added_to_model(ActivityOrSetup, Model),',
'\n not removed(ActivityOrSetup, Model).',
'\n',
'\ninitially_present(Entity, Version, Compartment, Model) :-',
'\n in_model(setup_present(Entity, Version, Compartment), Model).',
'\n',
'\n%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%',
'\n%%%%% synthesizable Entity rules %%%%%',
'\n%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%',
'\nsynthesizable(Ent, Version, Compartment, Model) :-',
'\n activity(Activity),',
'\n product(Ent, Version, Compartment, Activity),',
'\n active(Activity, Model).',
'\n',
'\nactive(Activity, Model) :-',
'\n activity(Activity),',
'\n in_model(Activity, Model),',
'\n not eliminated(Activity, Model).',
'\n',
'\neliminated(Activity, Model) :-',
'\n eliminated(Activity, Model, Int),',
'\n iteration(Int).',
'\n',
'\n%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%',
'\n%%%%% activities elimination rules %%%%%',
'\n%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%',
'\n',
'\n% iterations counter',
'\niteration(0).',
'\niteration(1).',
'\n',
'\niteration(Int+2) :-',
'\n Int < %s,' % max_number_activities,
'\n iteration(Int),',
'\n eliminated(Activity, Model, Int+1),',
'\n not eliminated(Activity, Model, Int),',
'\n in_model(Activity, Model),',
'\n activity(Activity),',
'\n model(Model).',
'\n',
'\n% reachability of one specie from another going only from products to substrates (i.e. back).',
'\npath_back_from_to(EntFrom, VerFrom, CompFrom, EntTo, VerTo, CompTo, Model, 0) :-',
'\n product(EntFrom, VerFrom, CompFrom, Activity),',
'\n substrate(EntTo, VerTo, CompTo, Activity),',
'\n in_model(Activity, Model),',
'\n activity(Activity).',
'\n',
'\npath_back_from_to(EntFrom, VerFrom, CompFrom, EntTo, VerTo, CompTo, Model, 0) :-',
'\n product(EntFrom, VerFrom, CompFrom, Activity),',
'\n substrate(InterEnt, InterVer, InterComp, Activity),',
'\n in_model(Activity, Model),',
'\n path_back_from_to(InterEnt, InterVer, InterComp, EntTo, VerTo, CompTo, Model, 0).',
'\n',
'\n% reachability for subsequent iterations.',
'\npath_back_from_to(EntFrom, VerFrom, CompFrom, EntTo, VerTo, CompTo, Model, Int+1) :-',
'\n iteration(Int),',
'\n product(EntFrom, VerFrom, CompFrom, Activity),',
'\n substrate(EntTo, VerTo, CompTo, Activity),',
'\n in_model(Activity, Model),',
'\n not eliminated(Activity, Model, Int), % not eliminated on previous iteration',
'\n activity(Activity).',
'\n',
'\npath_back_from_to(EntFrom, VerFrom, CompFrom, EntTo, VerTo, CompTo, Model, Int+1) :-',
'\n iteration(Int),',
'\n product(EntFrom, VerFrom, CompFrom, Activity),',
'\n substrate(InterEnt, InterVer, InterComp, Activity),',
'\n in_model(Activity, Model),',
'\n not eliminated(Activity, Model, Int), % not eliminated on previous iteration',
'\n path_back_from_to(InterEnt, InterVer, InterComp, EntTo, VerTo, CompTo, Model, Int+1).',
'\n',
'\n% is initially_present entity reachable from particular entity',
'\npath_back_to_initially_present(EntFrom, VerFrom, CompFrom, Model, Int) :-',
'\n iteration(Int),',
'\n path_back_from_to(EntFrom, VerFrom, CompFrom, EntTo, VerTo, CompTo, Model, Int),',
'\n initially_present(EntTo, VerTo, CompTo, Model).',
'\n',
'\npath_back_to_initially_present(Ent, Ver, Comp, Model, Int) :-',
'\n iteration(Int),',
'\n initially_present(Ent, Ver, Comp, Model).',
'\n',
'\n% has enzymes/transporter',
'\nhas_enzyme(Activity, Model, Int) :-',
'\n iteration(Int),',
'\n in_model(Activity, Model),',
'\n enz_required(Activity),',
'\n compartment(Comp),',
'\n enz_compartment(Comp, Activity),',
'\n catalyses(Entity, Version, Activity),',
'\n connected_compartments(SomeComp, Comp),',
'\n path_back_to_initially_present(Entity, Version, SomeComp, Model, Int).',
'\n',
'\nhas_transporter(Activity, Model, Int) :-',
'\n iteration(Int),',
'\n in_model(Activity, Model),',
'\n transp_required(Activity),',
'\n compartment(Comp),',
'\n transp_compartment(Comp, Activity),',
'\n transports(Entity, Version, Activity),',
'\n path_back_to_initially_present(Entity, Version, Comp, Model, Int).',
'\n',
'\n% elimination of activities',
'\neliminated(Activity, Model, Int) :-',
'\n iteration(Int),',
'\n activity(Activity),',
'\n in_model(Activity, Model),',
'\n substrate(EntFrom, VerFrom, CompFrom, Activity),',
'\n not path_back_to_initially_present(EntFrom, VerFrom, CompFrom, Model, Int).',
'\n',
'\neliminated(Activity, Model, Int) :-',
'\n iteration(Int),',
'\n activity(Activity),',
'\n in_model(Activity, Model),',
'\n enz_required(Activity),',
'\n not has_enzyme(Activity, Model, Int).',
'\n',
'\neliminated(Activity, Model, Int) :-',
'\n iteration(Int),',
'\n activity(Activity),',
'\n in_model(Activity, Model),',
'\n transp_required(Activity),',
'\n not has_transporter(Activity, Model, Int).']
def predictions_rules():
return ['\n%%%%%%%%%%%%%%%%%%%%%%%%%%%%',
'\n%%%%% prediction rules %%%%%',
'\n%%%%%%%%%%%%%%%%%%%%%%%%%%%%',
'\n',
'\ngrowth(dummy).',
'\n',
'\nindifferent(Model, Experiment) :-',
'\n not predicts(Model, Experiment, true),',
'\n not predicts(Model, Experiment, false),',
'\n model(Model),',
'\n designed(Experiment).',
'\n',
'\n% Adams reconstruction',
'\n predicts(Model, experiment(adam_two_factor_exp, Gene, Metabolite), false) :-',
'\n not predicts(Model, experiment(detection_activity_exp, GrowthActivity), false),',
'\n growth(GrowthActivity),',
'\n not predicts(Model, experiment(adam_two_factor_exp, Gene, Metabolite), true),',
'\n involved(Gene, GeneVer, Model),',
'\n gene(Gene, GeneVer),',
'\n involved(Metabolite, MetVersion, Model),',
'\n metabolite(Metabolite, MetVersion).',
'\n',
'\n predicts(Model, experiment(adam_two_factor_exp, Gene, Metabolite), true) :-% for genes that produce enzymes',
'\n not predicts(Model, experiment(detection_activity_exp, GrowthActivity), false),',
'\n growth(GrowthActivity),',
'\n substrate(Gene, GeneVer, GeneComp, ExpressionAct),',
'\n compartment(GeneComp),',
'\n expression(ExpressionAct),',
'\n in_model(ExpressionAct, Model),',
'\n metabolite(Metabolite, MetVersion),',
'\n % expression produces a catalyst',
'\n product(Ent, Ver, Comp, ExpressionAct),',
'\n catalyses(Ent, Ver, Reaction),',
'\n in_model(Reaction, Model),',
'\n enz_required(Reaction),% redundancy, but better make sure (might also help the ASP solver)',
'\n enz_compartment(SomeComp, Reaction),',
'\n connected_compartments(Comp, SomeComp),',
'\n % metabolite is within accepted distance from the catalysed reaction',
'\n distance(Reaction, Metabolite, MetVersion, MetComp, Integer, Model),',
'\n compartment(MetComp).',
'\n',
'\n predicts(Model, experiment(adam_two_factor_exp, Gene, Metabolite), true) :-% for genes that produce subunits of enzymes',
'\n not predicts(Model, experiment(detection_activity_exp, GrowthActivity), false),',
'\n growth(GrowthActivity),',
'\n substrate(Gene, GeneVer, GeneComp, ExpressionAct),',
'\n compartment(GeneComp),',
'\n expression(ExpressionAct),',
'\n in_model(ExpressionAct, Model),',
'\n metabolite(Metabolite, MetVersion),',
'\n % expression produces sth that is involved in production of enzyme down the line',
'\n product(EntTo, VerTo, CompTo, ExpressionAct),',
'\n entity(EntFrom, VerFrom),',
'\n compartment(CompFrom),',
'\n path_back_from_to(EntFrom, VerFrom, CompFrom, EntTo, VerTo, CompTo, Model, Int),',
'\n int(Integer),',
'\n catalyses(EntFrom, VerFrom, Reaction),',
'\n % enzyme is in appropriate compartment',
'\n enz_compartment(SomeComp, Reaction),',
'\n connected_compartments(SomeComp, CompFrom),',
'\n compartment(SomeComp),',
'\n % metabolite is within accepted distance from the catalysed reaction',
'\n distance(Reaction, Metabolite, MetVersion, MetComp, Integer, Model),',
'\n compartment(MetComp).',
'\n',
'\nint(0;1;2).% distance goes up to 2',
'\n',
'\ndistance(ActivityFrom, EntTo, VerTo, CompTo, 0, Model) :-',
'\n product(EntTo, VerTo, CompTo, ActivityFrom),',
'\n in_model(ActivityFrom, Model),',
'\n reaction(ActivityFrom),% only metabolic reactions',
'\n enz_required(ActivityFrom).% optimisation - only these activities make sense in the context of experiment',
'\n',
'\ndistance(ActivityFrom, EntTo, VerTo, CompTo, Int+1, Model) :-',
'\n distance(ActivityFrom, InterEnt, InterVer, InterComp, Int, Model),',
'\n substrate(InterEnt, InterVer, InterComp, InterActivity),',
'\n product(EntTo, VerTo, CompTo, InterActivity),',
'\n in_model(InterActivity, Model),',
'\n reaction(InterActivity),% only metabolic reactions',
'\n Int < 2,',
'\n int(Int).',
'\n',
'\n',
'\n% in vitro reconstruction: transporter required',
'\npredicts(Model, experiment(transp_reconstruction_exp, Activity, Entity), true) :-',
'\n in_model(Activity, Model),',
'\n transp_required(Activity),',
'\n involved(Entity, Version, Model),',
'\n transports(Entity, Version, Activity).',
'\n',
'\npredicts(Model, experiment(transp_reconstruction_exp, Activity, Entity), false) :-',
'\n in_model(Activity, Model),',
'\n transp_required(Activity),',
'\n involved(Entity, Version, Model),% there is a version of the entity in the model',
'\n not transports(Entity, Version, Activity),% that doesnt transport that stuff',
'\n transports(Entity, OtherVersion, Activity).% but some propose that this entity could do that',
'\n',
'\n',
'\n% in vitro reconstruction: enzymatic',
'\npredicts(Model, experiment(enz_reconstruction_exp, Activity, Entity), true) :-',
'\n in_model(Activity, Model),',
'\n enz_required(Activity),',
'\n involved(Entity, Version, Model),',
'\n catalyses(Entity, Version, Activity).',
'\n',
'\npredicts(Model, experiment(enz_reconstruction_exp, Activity, Entity), false) :-',
'\n in_model(Activity, Model),',
'\n enz_required(Activity),',
'\n involved(Entity, Version, Model),% there is a version of the entity in the model',
'\n not catalyses(Entity, Version, Activity),% that doesnt catalyse the reaction',
'\n catalyses(Entity, OtherVersion, Activity).% but some propose that this entity could do that',
'\n',
'\n',
'\n% in vitro reconstruction: basic',
'\npredicts(Model, experiment(basic_reconstruction_exp, Activity), true) :-',
'\n activity(Activity),',
'\n in_model(Activity, Model),',
'\n not enz_required(Activity),',
'\n not transp_required(Activity).',
'\n',
'\n',
'\n% localisation experiments: detecting entities',
'\npredicts(Model, experiment(localisation_entity_exp, Entity, Compartment), true) :-',
'\n not predicts(Model, experiment(detection_activity_exp, GrowthActivity), false),',
'\n growth(GrowthActivity),',
'\n compartment(Compartment),',
'\n involved(Entity, Version, Model),',
'\n synthesizable(Entity, Version, Compartment, Model).',
'\n',
'\npredicts(Model, experiment(localisation_entity_exp, Entity, Compartment), true) :-',
'\n compartment(Compartment),',
'\n in_model(setup_present(Entity, Version, Compartment), Model).',
'\n',
'\npredicts(Model, experiment(localisation_entity_exp, Entity, Compartment), false) :-',
'\n not predicts(Model, experiment(detection_activity_exp, GrowthActivity), false),',
'\n growth(GrowthActivity),',
'\n compartment(Compartment),',
'\n involved(Entity, Version, Model),',
'\n not predicts(Model, experiment(localisation_entity_exp, Entity, Compartment), true).',
'\n',
'\n',
'\n% detection experiments: detecting entities',
'\npredicts(Model, experiment(detection_entity_exp, Entity), true) :-',
'\n not predicts(Model, experiment(detection_activity_exp, GrowthActivity), false),',
'\n growth(GrowthActivity),',
'\n compartment(Compartment),',
'\n involved(Entity, Version, Model),',
'\n synthesizable(Entity, Version, Compartment, Model).',
'\n',
'\npredicts(Model, experiment(detection_entity_exp, Entity), true) :-% for stuff in the setup',
'\n compartment(Compartment),',
'\n in_model(setup_present(Entity, Version, Compartment), Model).',
'\n',
'\npredicts(Model, experiment(detection_entity_exp, Entity), false) :-',
'\n not predicts(Model, experiment(detection_activity_exp, GrowthActivity), false),',
'\n growth(GrowthActivity),',
'\n involved(Entity, Version, Model),',
'\n not predicts(Model, experiment(detection_entity_exp, Entity), true).',
'\n',
'\n',
'\n% growth experiments (or any other detecting activities)',
'\npredicts(Model, experiment(detection_activity_exp, Activity), true) :-',
'\n not predicts(Model, experiment(detection_activity_exp, GrowthActivity), false),',
'\n growth(GrowthActivity),',
'\n in_model(Activity, Model),',
'\n activity(Activity),',
'\n active(Activity, Model).',
'\n',
'\npredicts(Model, experiment(detection_activity_exp, Activity), false) :-',
'\n not predicts(Model, experiment(detection_activity_exp, GrowthActivity), false),',
'\n growth(GrowthActivity),',
'\n in_model(Activity, Model),',
'\n activity(Activity),',
'\n not active(Activity, Model).',
'\n',
'\npredicts(Model, experiment(detection_activity_exp, GrowthActivity), false) :-',
'\n growth(GrowthActivity),',
'\n in_model(GrowthActivity, Model),',
'\n activity(GrowthActivity),',
'\n not active(GrowthActivity, Model).',
'\n',
'\n% involved species (to handle open world)',
'\ninvolved(Entity, Version, Model) :-',
'\n compartment(Compartment),',
'\n initially_present(Entity, Version, Compartment, Model).',
'\n',
'\ninvolved(Entity, Version, Model) :-',
'\n compartment(Compartment),',
'\n product(Entity, Version, Compartment, Activity),',
'\n in_model(Activity, Model).',
'\n',
'\ninvolved(Entity, Version, Model) :-',
'\n compartment(Compartment),',
'\n substrate(Entity, Version, Compartment, Activity),',
'\n in_model(Activity, Model).']
def interventions_rules():
return ['\n%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%',
'\n%%%%%% application of interventions to all models %%%%%%',
'\n%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%',
'\n',
'\nremove(ReverseActivity):-',
'\n remove(BaseActivity),',
'\n reverse(BaseActivity, ReverseActivity).',
'\n',
'\nadd(ReverseActivity):-',
'\n add(BaseActivity),',
'\n reverse(BaseActivity, ReverseActivity).',
'\n',
'\nremoved(ActivityOrCondition, Model) :-',
'\n remove(ActivityOrCondition),',
'\n model(Model).',
'\n',
'\nadded_to_model(ActivityOrCondition, Model) :-',
'\n add(ActivityOrCondition),',
'\n model(Model).']
def inconsistency_rules():
return ['\n%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%',
'\n%%%%%% inconsistency between models and results - for revision %%%%%%',
'\n%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%',
'\n',
'\nnot_clean_model(Model) :-',
'\n activity(Activity),',
'\n in_model(Activity, Model),',
'\n not active(Activity, Model).',
'\n',
'\nnot_clean_model(Model) :-',
'\n involved(Entity, Version1, Model),',
'\n involved(Entity, Version2, Model),',
'\n Version1 != Version2.',
'\n',
'\ninconsistent(Model, Result) :-',
'\n predicts(Model, Experiment, Prediction),',
'\n result(Result, Experiment, Outcome),',
'\n Prediction != Outcome,',
'\n relevant(Result, Model),',
'\n not ignored(Result).']
def model_difference_rules():
return ['\n different(Model, ExternalModel) :-',
'\n model(Model),',
'\n activity(Activity),',
'\n external_model(ExternalModel),',
'\n in_model(Activity, Model),',
'\n not in_model(Activity, ExternalModel).',
'\n',
'\ndifferent(Model, ExternalModel) :-',
'\n model(Model),',
'\n activity(Activity),',
'\n external_model(ExternalModel),',
'\n not in_model(Activity, Model),',
'\n in_model(Activity, ExternalModel).']
#
# additional stuff for experiment design:
#
def export_models_exp_design(models):
strings = []
joined_models = ';'.join([x.ID for x in models])
strings.append(joined_models.join(['\nmodel(', ').']))
# specification:
for model in models:
strings.extend(export_model_specification(model))
return strings
def modeh_replacement(cost_model):
exp_spec_elements = [e for e in export_experiment_specification_elements(cost_model).keys()]
joined_elements = ','.join(exp_spec_elements)
return joined_elements.join(['\n0{', '}%s.' % len(exp_spec_elements)])
def models_nr_and_probabilities(models):
out = []
counter = 0
for model in models:
out.append('\nnr(%s,%s).' % (counter, model.ID))
out.append('\nprobability(%s, %s).' % (model.quality, model.ID))
counter += 1
return out
def cost_rules(cost_model):
output = []
cost_dict = export_experiment_specification_elements(cost_model)
counter = 0
for element in cost_dict.keys():
output.append(''.join(['\ncost(%s, %s) :- ' % (cost_dict[element], counter), element, '.']))
counter += 1
return output
def ban_experiment(expDescription):
exp_info = []
# dealing with experiment types
if isinstance(expDescription.experiment_type, exp_repr.DetectionEntity):
exp_info.append('\n:- designed(experiment(detection_entity_exp, %s))' % expDescription.experiment_type.entity_id)
elif isinstance(expDescription.experiment_type, exp_repr.LocalisationEntity):
tpl = (expDescription.experiment_type.entity_id, expDescription.experiment_type.compartment_id)
exp_info.append('\n:- designed(experiment(localisation_entity_exp, %s, %s))' % tpl)
elif isinstance(expDescription.experiment_type, exp_repr.DetectionActivity):
exp_info.append('\n:- designed(experiment(detection_activity_exp, %s))' % expDescription.experiment_type.activity_id)
elif isinstance(expDescription.experiment_type, exp_repr.AdamTwoFactorExperiment):
tpl = (expDescription.experiment_type.gene_id, expDescription.experiment_type.metabolite_id)
exp_info.append('\n:- designed(experiment(adam_two_factor_exp, %s, %s))' % tpl)
elif isinstance(expDescription.experiment_type, exp_repr.ReconstructionActivity):
exp_info.append('\n:- designed(experiment(basic_reconstruction_exp, %s))' % expDescription.experiment_type.activity_id)
elif isinstance(expDescription.experiment_type, exp_repr.ReconstructionEnzReaction):
tpl = (expDescription.experiment_type.reaction_id, expDescription.experiment_type.enzyme_id)
exp_info.append('\n:- designed(experiment(enz_reconstruction_exp, %s, %s))' % tpl)
elif isinstance(expDescription.experiment_type, exp_repr.ReconstructionTransporterRequired):
tpl = (expDescription.experiment_type.transport_activity_id, expDescription.experiment_type.transporter_id)
exp_info.append('\n:- designed(experiment(transp_reconstruction_exp, %s, %s))' % tpl)
else:
raise TypeError("ban_experiment: exp description type not recognised: %s" % expDescription)
# dealing with interventions:
for inter in expDescription.interventions:
if (isinstance(inter, mnm_repr.Add) and isinstance(inter.condition_or_activity, mnm_repr.Condition)):
tpl = (inter.condition_or_activity.entity.ID, inter.condition_or_activity.entity.version, inter.condition_or_activity.compartment.ID)
exp_info.append('add(setup_present(%s, %s, %s))' % tpl)
elif (isinstance(inter, mnm_repr.Remove) and isinstance(inter.condition_or_activity, mnm_repr.Condition)):
tpl = (inter.condition_or_activity.entity.ID, inter.condition_or_activity.entity.version, inter.condition_or_activity.compartment.ID)
exp_info.append('remove(setup_present(%s, %s, %s))' % tpl)
elif (isinstance(inter, mnm_repr.Add) and isinstance(inter.condition_or_activity, mnm_repr.Activity)): # additional import activities
exp_info.append('add(%s)' % inter.condition_or_activity.ID)
else:
raise TypeError("ban_experiment: intervention type not recognised: %s" % inter)
# formatting:
st = ','.join(exp_info)
return ''.join([st, '.'])
def export_experiment_specification_elements(cost_model):
output = {}
for element in cost_model.types.keys():
if element == exp_repr.DetectionEntity:
output['design_type(detection_entity_exp)'] = cost_model.types[element]
elif element == exp_repr.AdamTwoFactorExperiment:
output['design_type(adam_two_factor_exp)'] = cost_model.types[element]
elif element == exp_repr.ReconstructionActivity:
output['design_type(basic_reconstruction_exp)'] = cost_model.types[element]
elif element == exp_repr.ReconstructionEnzReaction:
output['design_type(enz_reconstruction_exp)'] = cost_model.types[element]
elif element == exp_repr.ReconstructionTransporterRequired:
output['design_type(transp_reconstruction_exp)'] = cost_model.types[element]
elif element == exp_repr.LocalisationEntity:
output['design_type(localisation_entity_exp)'] = cost_model.types[element]
elif element == exp_repr.DetectionActivity:
output['design_type(detection_activity_exp)'] = cost_model.types[element]
else:
raise TypeError("export_experiment_specification_elements: type not recognised: %s" % element)
for element in cost_model.design_compartment.keys():
output['design_compartment(%s)' % element] = cost_model.design_compartment[element]
for element in cost_model.design_deletable.keys():
output['design_deletable(%s)' % element.ID] = cost_model.design_deletable[element]
for element in cost_model.design_available.keys():
output['design_available(%s)' % element.ID] = cost_model.design_available[element]
for element in cost_model.design_activity_rec.keys():
output['design_activity_rec(%s)' % element.ID] = cost_model.design_activity_rec[element]
for element in cost_model.design_activity_det.keys():
output['design_activity_det(%s)' % element.ID] = cost_model.design_activity_det[element]
for element in cost_model.design_entity_loc.keys():
output['design_entity_loc(%s)' % element.ID] = cost_model.design_entity_loc[element]
for element in cost_model.design_entity_det.keys():
output['design_entity_det(%s)' % element.ID] = cost_model.design_entity_det[element]
for element in cost_model.intervention_add.keys():
if isinstance(element.condition_or_activity, Condition):
tup = (element.condition_or_activity.entity.ID, element.condition_or_activity.entity.version, element.condition_or_activity.compartment.ID)
output['add(setup_present(%s, %s, %s))' % tup] = cost_model.intervention_add[element]
# import activities for metabolites added to medium
elif isinstance(element.condition_or_activity, Activity):
output['add(%s)' % element.condition_or_activity.ID] = cost_model.intervention_add[element]
else:
raise TypeError("export_experiment_specification_elements: intervention_add: type not recognised: %s" % element)
for element in cost_model.intervention_remove.keys():
tup = (element.condition_or_activity.entity.ID, element.condition_or_activity.entity.version, element.condition_or_activity.compartment.ID)
output['remove(setup_present(%s, %s, %s))' % tup] = cost_model.intervention_remove[element]
return output
def constant_for_calculating_score(Int):
return '\n\n#const n = %s.' % Int
def design_constraints_basic():
return ['\n\ndesigned :- designed(experiment(adam_two_factor_exp, Gene, Metabolite)), gene(Gene, Ver), metabolite(Metabolite, Ver).',
'\ndesigned :- designed(experiment(transp_reconstruction_exp, Activity, Entity)), activity(Activity), entity(Entity, Ver).',
'\ndesigned :- designed(experiment(enz_reconstruction_exp, Activity, Entity)), activity(Activity), entity(Entity, Ver).',
'\ndesigned :- designed(experiment(basic_reconstruction_exp, Activity)), activity(Activity).',
'\ndesigned :- designed(experiment(detection_activity_exp, Activity)), activity(Activity).',
'\ndesigned :- designed(experiment(localisation_entity_exp, Entity, Compartment)), entity(Entity, Ver), compartment(Compartment).',
'\ndesigned :- designed(experiment(detection_entity_exp, Entity)), entity(Entity, Ver).',
'\n',
# design at lest one exp
'\n:- not designed.',
'\n',
# design no more than one exp
'\n:- designed(Experiment1), designed(Experiment2), Experiment1 != Experiment2.',
'\n',
# designed exp must make at least
'\nhas_true_model :- predicts(Model, Experiment, true), model(Model), designed(Experiment).',
# one model false and one true
'\nhas_false_model :- predicts(Model, Experiment, false), model(Model), designed(Experiment).',
'\n:- not has_true_model.',
'\n:- not has_false_model.',
'\n',
'\n:- designed(experiment(adam_two_factor_exp, Gene, Metabolite)), add(Whatever).',
# no interventions for adam-style exps
'\n:- designed(experiment(adam_two_factor_exp, Gene, Metabolite)), remove(Whatever).',
# only one version of entity in model
'\n:- involved(Entity, Version1, Model), involved(Entity, Version2, Model), Version1 != Version2.',
'\n',
'\nentity_in_model(Entity, Version, Compartment, Model) :- in_model(setup_present(Entity, Version, Compartment), Model).',
'\nentity_in_model(Entity, Version, Compartment, Model) :- synthesizable(Entity, Version, Compartment, Model).',
# import restriction: can't add without adding the substance
'\n:- add(ImportAct), substrate(Ent,Ver,Comp,ImportAct), not add(setup_present(Ent, Ver, Comp)).']
def cost_minimisation_rules():
return ['\n\ntotal_cost(TCost) :- TCost = #sum[cost(Cost, Nr)=Cost].',
'\n#minimize[total_cost(TCost) = TCost@1].']
def experiment_design_rules():
return ['\n\n%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%',
'\n%%%%%% rules for exp design %%%%%%',
'\n%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%',
'\n',
'\ndesigned(experiment(adam_two_factor_exp, Gene, Metabolite)) :-',
'\n design_type(adam_two_factor_exp),',
'\n design_deletable(Gene),',
'\n design_available(Metabolite).',
'\n',
'\ndesigned(experiment(transp_reconstruction_exp, Activity, Entity)) :-',
'\n design_type(transp_reconstruction_exp),',
'\n design_activity_rec(Activity),',
'\n design_available(Entity).',
'\n',
'\ndesigned(experiment(enz_reconstruction_exp, Activity, Entity)) :-',
'\n design_type(enz_reconstruction_exp),',
'\n design_activity_rec(Activity),',
'\n design_available(Entity).',
'\n',
'\ndesigned(experiment(basic_reconstruction_exp, Activity)) :-',
'\n design_type(basic_reconstruction_exp),',
'\n design_activity_rec(Activity).',
'\n',
'\ndesigned(experiment(detection_activity_exp, Activity)) :-',
'\n design_type(detection_activity_exp),',
'\n design_activity_det(Activity).',
'\n',
'\ndesigned(experiment(localisation_entity_exp, Entity, Compartment)) :-',
'\n design_type(localisation_entity_exp),',
'\n design_entity_loc(Entity),',
'\n design_compartment(Compartment).',
'\n',
'\ndesigned(experiment(detection_entity_exp, Entity)) :-',
'\n design_type(detection_entity_exp),',
'\n design_entity_det(Entity).']
def hide_show_statements():
return ['\n#hide.',
'\n#show add/1.',
'\n#show remove/1.',
'\n#show design_type/1.',
'\n#show design_deletable/1.',
'\n#show design_available/1.',
'\n#show design_entity_det/1.',
'\n#show design_entity_loc/1.',
'\n#show design_compartment/1.',
'\n#show design_activity_rec/1.',
'\n#show design_activity_det/1.']
def advanced_exp_design_rules():
return ['\n\nexp_score(0,Prb,0,0) :- indifferent(Model, Experiment), designed(Experiment), model(Model), nr(0,Model), probability(Prb, Model).',
'\nexp_score(0,0,Prb,0) :- predicts(Model, Experiment, true), designed(Experiment), model(Model), nr(0,Model), probability(Prb, Model).',
'\nexp_score(0,0,0,Prb) :- predicts(Model, Experiment, false), designed(Experiment), model(Model), nr(0,Model), probability(Prb, Model).',
'\n',
'\nexp_score(I,Ind+Prb,Tr,Fa) :- exp_score(I-1, Ind, Tr, Fa), indifferent(Model, Experiment), designed(Experiment), model(Model), nr(I, Model), probability(Prb, Model).',
'\nexp_score(I,Ind,Tr+Prb,Fa) :- exp_score(I-1, Ind, Tr, Fa), predicts(Model, Experiment, true), designed(Experiment), model(Model), nr(I, Model), probability(Prb, Model).',
'\nexp_score(I,Ind,Tr,Fa+Prb) :- exp_score(I-1, Ind, Tr, Fa), predicts(Model, Experiment, false), designed(Experiment), model(Model), nr(I, Model), probability(Prb, Model).',
'\n',
'\nexp_score(I) :- exp_score(I,Ind,Tr,Fa).',
'\n',
'\nfinal_score(I,Ind,Tr,Fa) :- exp_score(I,Ind,Tr,Fa), not exp_score(I+1).',
'\n',
'\nfinal_score(|Tr*10-n| + |Fa*10-n| + Ind*10) :- final_score(I,Ind,Tr,Fa).'
'\n#minimize[final_score(Score) = Score@2].']
#
# predictions
#
def export_display_for_oracle(expDescription):
if isinstance(expDescription.experiment_type, DetectionEntity) or isinstance(expDescription.experiment_type, LocalisationEntity):
return ['\n#hide.', '\n#show synthesizable/4.', '\n#show initially_present/4.']
elif isinstance(expDescription.experiment_type, DetectionActivity):
return ['\n#hide.', '\n#show active/2.']
elif isinstance(expDescription.experiment_type, AdamTwoFactorExperiment):
return ['\n#hide.', '\n#show predicts/3.']
else:
raise TypeError("export_display_for_oracle: exp type not recognised: %" % expDescription.experiment_type)