-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlwidget.js
More file actions
2239 lines (2043 loc) · 70.5 KB
/
lwidget.js
File metadata and controls
2239 lines (2043 loc) · 70.5 KB
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
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/////////////////////////////////////////////////////////////////
// lwidget29.js: Code to define chemical system in js for the purposes
// of layout and display in a browser
/////////////////////////////////////////////////////////////////
const pixelWidth = 700;
const pixelHeight = 700;
const layoutGroupWidth = 5;
var svgContainer = "";
var cx = 500.0;
var cy = 420.0;
var wx = 150;
var wy = 150;
/////////////////////////////////////////////////////////////////////
// Some system toggles.
var isZoomedOut = true;
var showProxies = false; // flag: decides if system should show proxies.
var autoPositionFlag = true; // Get the system to auto-pos enzymes.
var positionEnzWithParent = false; // If auto-positioning is on:
// false: Place enz in same group as substrate
// true: Place enz in same group as parent
/////////////////////////////////////////////////////////////////////
var textScale = 1.0;
const objSpacing = 0.6; // Worst case horizontal spacing of reacs and enz
var poolWidth = 6.0;
var poolHeight = 1.5;
var poolInterval= 1.5; // Scale poolWidth for mid-point spacing of pools
var arrowWidth = 0.25;
var textXoffset = 0.3;
var enzYoffsetFromPool = 2.0; // Scaling factor on PoolHeight
var enzYoffsetFromReagents = 3.0; // Scaling factor on PoolHeight
var reacYoffsetFromReagents = 5.0; // Scaling factor on PoolHeight
var poolYoffset = 8.0; // Scaling factor on PoolHeight
const reacMsgColor = "green";
const groupFillColor = "cornsilk";
const groupBorderColor = "blue";
const groupTextColor = "darkblue";
const comptFillColor = "palegreen";
var displayError = function( msg ) {
}
var objLookup = {};
var nameLookup = {};
var centredGroup = "";
var poolData = [];
var reacData = [];
var enzData = [];
var mmEnzData = [];
var chanData = [];
var groupData = [];
var comptData = [];
var groupPlusComptData = [];
var funcData = [];
var msgData = [];
var poolProxies = [];
var reacProxies = [];
var foundEnzCoords = false;
var isMooseSBML = false;
var concUnits = "uM";
const concUnitScale = { "M": 1e-3, "mM": 1.0, "uM": 1000.0, "nM":1e6 };
/////////////////////////////////////////////////////////////////////////
// Some stuff for screen size scaling
/////////////////////////////////////////////////////////////////////////
function scaleFontsEtc( factor ) {
textScale *= factor;
poolWidth *= factor;
poolHeight *= factor;
arrowWidth *= factor;
textXoffset *= factor;
}
var xScale = d3.scale.linear()
.domain( [cx - wx/2, cx + wx/2] )
.range( [0, pixelWidth] )
var yScale = d3.scale.linear()
.domain( [ cy + wy/2, cy - wy/2] )
.range( [ 0, pixelHeight] )
var xObjScale = d3.scale.linear()
.domain( [ 0, wx ] )
.range( [ 0, pixelWidth] )
var yObjScale = d3.scale.linear()
.domain( [ 0, wy ] )
.range( [ 0, pixelHeight] )
/////////////////////////////////////////////////////////////////
function ChemObj( name, className, id, color, textfg, x, y, notes) {
this.name = name;
this.className = className;
this.parentObj = "";
this.id = id;
this.fg = color;
this.textfg = textfg;
// this.opacity = 1;
this.x = x;
this.y = y;
this.dispx = x;
this.dispy = y;
this.ndisp = 1;
this.notes = "";
this.wasDragged = false;
this.selected = false; // When true, highlights by thick stroke
this.deselected = false; // When true, dims the object
this.visible = true; // When true, object is visible on screen
this.getUniquePath = function() {
if ( this.className == "Compt" || this.parentObj == "" || (typeof this.parentObj.base === "undefined" ) ) {
return this.name;
} else {
return this.parentObj.base.getUniquePath() + "/"+this.name;
}
}
Object.defineProperty( this, "opacity",
{get: function() { return this.visible * (1.0 - this.deselected*0.7); } } );
}
function getSelectedAsString() {
var ret = ""
for ( const key in objLookup ) {
var b = objLookup[key].base;
if (b.selected) {
ret += b.getUniquePath() + ",";
}
}
return ret;
}
/// Returns a color. Arg is a numeric value 0-63 or a color name
function convColor( val, shading = 0.0 ){
if ( isNaN( val ) ) {
return val;
} else {
ret = d3.interpolateSpectral( Math.round(val/64.0) );
return shadeRGBColor( ret, shading );
}
}
/// From Stack Overflow Pimp Trizkit.
function shadeRGBColor(color, percent) {
var f=color.split(","),t=percent<0?0:255,p=percent<0?percent*-1:percent,R=parseInt(f[0].slice(4)),G=parseInt(f[1]),B=parseInt(f[2]);
return "rgb("+(Math.round((t-R)*p)+R)+","+(Math.round((t-G)*p)+G)+","+(Math.round((t-B)*p)+B)+")";
}
/////////////////////////////////////////////////////////////////
// Utility access functions that look for a field but return a sensible
// default if not found.
function annoGetFloat( anno, name, defaultValue ) {
if (anno == "" )
return defaultValue;
var tag = anno.getElementsByTagName( name )
if (tag && tag.length > 0) {
return parseFloat( tag[0].textContent );
}
return defaultValue;
}
function annoGetStr( anno, name, defaultValue ) {
if ( anno == "" )
return defaultValue;
var tag = anno.getElementsByTagName( name )
if (tag && tag.length > 0) {
return tag[0].textContent;
}
return defaultValue;
}
function annoGetBool( anno, name, defaultValue ) {
if ( anno == "" )
return defaultValue;
var tag = anno.getElementsByTagName( name )
if (tag && tag.length > 0) {
var val = tag[0].textContent;
if (val) {
if (val.toLowerCase() == "true" || val != "0") {
return true;
}
return false;
}
}
return defaultValue;
}
function annoGetManno( anno, name ) {
var val = anno.getElementsByTagName( name );
return (val && val.length > 0) ? val[0] : "";
}
function attrGetFloat( attr, name, defaultValue ) {
var item = attr.getNamedItem( name );
if ( item == null )
return defaultValue;
return parseFloat( item.nodeValue );
}
function attrGetInt( attr, name, defaultValue ) {
var item = attr.getNamedItem( name );
if ( item == null )
return defaultValue;
return parseInt( item.nodeValue );
}
function attrGetBool( attr, name, defaultValue ) {
var item = attr.getNamedItem( name );
if ( item == null )
return defaultValue;
if ( item.toLowerCase == "true" ) return true;
if ( item.toLowerCase == "false" ) return false;
return defaultValue;
}
function attrGetStr( attr, name, defaultValue ) {
var item = attr.getNamedItem( name );
if ( item == null )
return defaultValue;
return item.nodeValue;
}
/////////////////////////////////////////////////////////////////
function makeBaseObj( className, xobj, anno, attr, annoName ) {
var id = attrGetStr( attr, "id", "dummy_id" );
var name = attrGetStr( attr, "name", "" );
if ( name == "" ) { // Old SBML
name = id;
name = name.replace( /_slash_/g, "__" );
name = name.replace( /_minus_/g, "_" );
name = name.replace( /_dot_/g, "_" );
name = name.replace( /_star_/g, "_p" );
}
var x = 0.0;
var y = 0.0;
var textfg = "black";
var bg = "cyan";
var notes = "";
// if ( typeof anno !== "undefined") {
if ( anno.length > 0 ) {
manno = annoGetManno( anno[0], annoName );
if ( manno != "" ) {
x = annoGetFloat( manno, "moose:xCord", 0.0 );
y = annoGetFloat( manno, "moose:yCord", 0.0 );
textfg = convColor( annoGetStr( manno, "moose:textColor", "black" ) );
bg = convColor( annoGetStr(manno,"moose:bgColor", "cyan") );
notes = annoGetStr( xobj, "notes", "" )
}
}
return new ChemObj( name, className, id, bg, textfg, x,y, notes );
}
/////////////////////////////////////////////////////////////////
// This works on groups and compartments that are not inside any other.
function autoPositionTopLevelGroupsAndCompartments() {
var k;
var gc = [];
var dx = poolInterval * poolWidth * (layoutGroupWidth + 1)
for (k = 0; k < groupPlusComptData.length; k++ ) {
x = groupPlusComptData[k];
if ( x.base.parentObj == "" )
gc.push(x);
}
currX = 0;
currY = 0;
for (k = 0; k < gc.length; k++ ) {
// At this point we don't know how big the outer groups are actually
// going to be: we can't assign absolute coords just yet.
// gc.base.dispx = gc.base.x = k % side;
// gc.base.dispy = gc.base.y = k / side;
// Go through and set up child groups and other things in a
// square lattice. Each entry of it will assign space based on
// a square of size (layoutGroupWidth +1) * poolWidth.
// We assume that the reacs will be in the (0,0) grid place, and
// the groups fill the rest.
var kids = gc[k].groupAndComptChildren;
var side = 1 + Math.floor( Math.sqrt( kids.length ) );
var j;
// Assume no deeper nesting.
// Here we start with index 1 so that any reacns can occupy (0,0)
for (j = 0; j < kids.length; j++ ) {
kids[j].base.dispx = currX + dx * ((1+j) % side);
kids[j].base.dispy = currY + dx * Math.floor((1+j) / side);
kids[j].repositionGroup();
}
gc[k].updateCoords()
currX += gc[k].width + poolWidth * 2;
}
}
function GroupBase( id, name, compartment, parentObjId, bg, className = "Group" ) {
this.compartment = compartment;
this.parentObjId = parentObjId;
this.base = new ChemObj( name, className, id, convColor(bg, 0.6), "yellow", 0,0, "" );
this.width = poolWidth * 2;
this.height = poolHeight * 2;
this.children = []; // Array of Ids as strings.
this.groupAndComptChildren = []; // any group or compt children.
this.poolChildren = []; // PoolObj in this group.
this.reacChildren = []; // ReacObj in this group.
this.enzChildren = []; // parent is a pool; positions NOT set by group.
this.funcChildren = []; // parent is a pool; positioned right over pool
this.enzAdoptees = []; // Parent may be on another group; Subs are local; positions ARE set by group.
this.assignChildParents = function() {
this.children = this.children.filter( function(value, idx, arr) {
return typeof objLookup[ value ] !== "undefined";
} );
var k;
for (k = 0; k < this.children.length ; k++ ) {
objLookup[ this.children[k] ].base.parentObj = this;
}
}
this.addFuncChild = function( funcObj ) {
this.funcChildren.push( funcObj.base.id );
}
this.addEnzChild = function( enzObj ) {
this.enzChildren.push( enzObj.base.id );
}
this.addEnzAdoptee = function( enzObj ) {
this.enzAdoptees.push( enzObj.base.id );
}
// Populates the subcategory arrays for pools, reacs, groups, compts
this.subCategorizeChildren = function() {
this.poolChildren = [];
this.reacChildren = [];
this.groupAndComptChildren = [];
var k;
for ( k = 0; k < this.children.length; k++ ) {
obj = objLookup[this.children[k]];
if ( obj.base.className == "Pool" )
this.poolChildren.push( obj );
else if (obj.base.className == "Reac" )
this.reacChildren.push( obj );
else if (obj.base.className == "Group" )
this.groupAndComptChildren.push( obj );
else if (obj.base.className == "Compt" )
this.groupAndComptChildren.push( obj );
}
}
this.updatePoolCoords = function() {
if ( autoPositionFlag ) {
var k;
var num = this.poolChildren.length;
var groupWidth = Math.max( 1, Math.min(num, layoutGroupWidth));
var gx = this.base.x + 0.6 * poolWidth;
var gy = this.base.y + poolHeight;
for ( k = 0; k < this.poolChildren.length; k++ ) {
var pool = this.poolChildren[k];
pool.base.dispx = pool.base.x = gx + poolInterval * poolWidth*(k % groupWidth);
pool.base.dispy = pool.base.y = gy + poolYoffset * poolHeight * Math.floor(k/groupWidth);
pool.base.ypos = Math.floor(k/groupWidth);
}
for ( k = 0; k < this.funcChildren.length; k++ ) {
var func = objLookup[this.funcChildren[k]];
var pool = objLookup[func.tgtPool]
func.base.dispx = func.base.x = pool.base.dispx;
func.base.dispy = func.base.y = pool.base.dispy + poolHeight;
}
}
}
this.updateEnzCoords = function() {
var k;
if ( autoPositionFlag ) {
if ( positionEnzWithParent ) {
for ( k = 0; k < this.poolChildren.length; k++ ) {
this.poolChildren[k].numEnzSites = 0;
}
this.enzChildren.forEach( autoPositionEnz );
} else {
this.enzAdoptees.forEach( autoPositionEnz );
var ez = [];
this.enzAdoptees.forEach( function( e ) { ez.push( objLookup[e] ) } );
spaceOutObjectsOnGrid( ez );
ez.forEach( function(e) { e.base.dispx = e.base.x; } );
}
}
}
this.updateCoords = function() {
var k;
var x = [];
var y = [];
var allkids = this.children.concat( this.funcChildren );
if ( positionEnzWithParent ) {
allkids = allkids.concat( this.enzChildren );
} else {
allkids = allkids.concat( this.enzAdoptees );
}
if ( autoPositionFlag ) {
// this.updateEnzCoords();
// This places reacs as per location of reagents.
this.reacChildren.forEach( function(r) { autoPositionReac( r ) } );
// This goes through them all and spaces out to avoid overlap
spaceOutObjectsOnGrid( this.reacChildren );
// Now put the final value in the dispx.
this.reacChildren.forEach( function(r) { r.base.dispx = r.base.x; } );
}
for (k = 0; k < allkids.length ; k++ ) {
var child = objLookup[ allkids[k] ];
if ( child.base.className == "Group" || child.base.className == "Compt" ) {
// Here we include the top right corner of the group.
x.push( child.base.x + child.width );
y.push( child.base.y + child.height );
}
x.push( child.base.x );
y.push( child.base.y );
}
if ( allkids.length > 0 ) {
this.base.x = Math.min( ...x ) - 0.6*poolWidth; // Another dumb syntax.
this.base.y = Math.min( ...y ) - poolHeight;
this.base.dispx = this.base.x;
this.base.dispy = this.base.y;
this.width = 0.8 * poolWidth + Math.max( ...x ) - this.base.x;
this.height = 2 * poolHeight + Math.max( ...y ) - this.base.y;
}
}
this.repositionGroup = function() {
//var dx = this.base.dispx - this.base.x - this.width/2;
//var dy = this.base.dispy - this.base.y - this.height/2;
var dx = this.base.dispx - this.base.x;
var dy = this.base.dispy - this.base.y;
var k;
var allkids = this.children.concat( this.funcChildren );
if ( positionEnzWithParent ) {
allkids = allkids.concat( this.enzChildren );
} else {
allkids = allkids.concat( this.enzAdoptees );
}
for (k = 0; k < allkids.length ; k++ ) {
var child = objLookup[ allkids[k] ];
child.base.dispx += dx;
child.base.dispy += dy;
// This does very weird things. To avoid.
// if ( child instanceof "GroupBase" )
if ( isGroupOrCompt( child ) ) {
child.repositionGroup();
} else {
child.base.x += dx;
child.base.y += dy;
}
}
this.base.x += dx;
this.base.y += dy;
// this.updateCoords();
}
this.setVisibility = function( visible ) {
if (isZoomedOut) {
var origVisibility = this.base.visible;
this.base.visible = false;
this.setChildVisibility( this.children );
this.setChildVisibility( this.funcChildren );
if (positionEnzWithParent ) {
this.setChildVisibility( this.enzChildren );
} else {
this.setChildVisibility( this.enzAdoptees );
}
this.base.visible = visible;
} else {
// Even if opacity hasn't changed, child objects may have gained
// or lost message links which affect their opacity. So we update
// all regardless.
/*
if ( centredGroup == this ) { // Don't blank it
visiblity = true;
}
*/
// In all remaining cases, go through and set opacity recursively.
this.base.visible = visible;
this.setChildVisibility( this.children );
this.setChildVisibility( this.funcChildren );
if ( positionEnzWithParent ) {
this.setChildVisibility( this.enzChildren );
} else {
this.setChildVisibility( this.enzAdoptees );
}
}
}
this.setChildVisibility = function( children ) {
var j;
for ( j = 0; j < children.length; j++ ) {
child = objLookup[ children[j] ];
//if ( !isGroupOrCompt( child ) ) {
// Let the outer loop through groups handle child groups.
child.base.visible = this.base.visible;
child.base.dispx = 0;
child.base.dispy = 0;
child.base.ndisp = 0;
// }
}
}
this.setDeselect = function( flag ) {
var j;
var kids = this.children.concat( this.funcChildren, this.enzChildren );
this.base.deselected = flag;
for ( j = 0; j < kids.length; j++ ) {
child = objLookup[ kids[j] ];
if ( isGroupOrCompt( child ) ) {
child.setDeselect( flag );
} else {
child.base.deselected = flag;
}
}
}
this.computeProxyLayout = function() {
// for now a dummy function, just averages out the coords.
computeGroupChildLayout( this.children );
computeGroupChildLayout( this.funcChildren );
if (positionEnzWithParent ) {
computeGroupChildLayout( this.enzChildren );
} else {
computeGroupChildLayout( this.enzAdoptees );
}
}
this.zoomInOut = function() {
// Toggles between zoom for current group, and for entire frame.
if ( isZoomedOut ) {
wx = 1.1 * this.width + 4 * poolWidth;
wy = 1.2 * this.height + 4 * poolHeight;
cx = this.base.x + wx/2 - 3 * poolWidth;
cy = this.base.y + wy/2 - 4 * poolHeight;
isZoomedOut = false;
setDisplayScales();
} else {
zoomToEntireModel();
}
document.getElementById( "isZoomedOut" ).checked = isZoomedOut;
zoomVisibility();
// Need to do the centredGroup here because zoomVisibility sets it.
centredGroup = this;
transition(svgContainer);
// displayError( "zoomInOut on " + centredGroup.base.name );
}
}
function GroupObj( xgroup, anno, attr ) {
var id = attr.getNamedItem( "groups:id" ).nodeValue;
var name = attr.getNamedItem( "groups:name" ).nodeValue;
var manno = anno.getElementsByTagName("moose:GroupAnnotation");
var compartment = annoGetStr( manno[0], "moose:Compartment", "" );
var parentObjId = annoGetStr( manno[0], "moose:Parent", "" );
var bg = annoGetStr( manno[0], "moose:bgColor", groupFillColor );
var members = xgroup.getElementsByTagName("groups:member");
GroupBase.call( this, id, name, compartment, parentObjId, bg, "Group");
for (k = 0; k < members.length; k++ ) {
var childId = members[k].attributes.getNamedItem( "groups:idRef" ).nodeValue;
if (childId != null)
this.children.push( childId );
}
}
function computeGroupChildLayout( children ) {
var j;
for ( j = 0; j < children.length; j++ ) {
child = objLookup[ children[j] ];
if ( child.base.ndisp > 0 ) {
child.base.dispx /= child.base.ndisp;
child.base.dispy /= child.base.ndisp;
if ( child.base.dispy > cy ) {
poolProxies.push( child );
} else {
reacProxies.push( child );
}
} else {
child.base.dispx = child.base.x;
child.base.dispy = child.base.y;
}
}
}
/////////////////////////////////////////////////////////
// Compartments contain other objects, including Groups and Compartments.
// These are all managed by the ComptObj, which derives from the GroupBase
//
/////////////////////////////////////////////////////////
function ComptObj( xcompt, anno, attr ) {
var suffix = "_" + comptData.length.toString();
var id = attrGetStr( attr, "id", "compt" + suffix );
var name = attrGetStr( attr, "name", "compt" + suffix );
// var id = attr.getNamedItem( "id" ).nodeValue;
// var name = attr.getNamedItem( "name" ).nodeValue;
var bg = comptFillColor;
var compartment = "";
var parentObjId = "";
this.size = attrGetFloat( attr, "size", 1.0e-15 );
this.spatialDimensions = attrGetFloat( attr, "spatialDimensions", 3 );
if ( anno.length > 0 ) {
var manno = annoGetManno( anno[0], "moose:CompartmentAnnotation" );
this.shape = annoGetStr( manno, "moose:Mesh", "CubeMesh" );
this.isMembraneBound = annoGetBool( manno, "moose:isMembraneBound", true );
bg = annoGetStr( manno, "moose:bgColor", comptFillColor );
compartment = annoGetStr( manno, "moose:compartment", "" );
parentObjId = annoGetStr( manno, "moose:parent", "" );
} else { // Put in the defaults
this.shape = "CubeMesh";
this.isMembraneBound = true;
}
GroupBase.call( this, id, name, compartment, parentObjId, bg, "Compt");
// this.size = parseFloat( attr.getNamedItem("size") );
// this.spatialDimensions = parseInt( attr.getNamedItem("spatialDimensions") );
// var compartment = manno[0].getElementsByTagName("moose:ContainedBy")[0].textContent;
// var parentObj = manno[0].getElementsByTagName("moose:Parent")[0].textContent;
this.addchild = function( child ) {
this.children.push( child.base.id );
}
}
function assignCompartments() {
// Go through all Groups, put them in their Parents which will
// either be another group or a Compartment.
// Then go through all objects, check if their Parent is defined.
}
/////////////////////////////////////////////////////////
function PoolObj( xpool, anno, attr ) {
this.base = makeBaseObj( "Pool", xpool, anno, attr, "moose:ModelAnnotation" );
this.CoInit = concUnitScale[concUnits] * attrGetFloat(attr, "initialConcentration", 0.0 );
this.isBuffered = attrGetBool(attr, "constant", false );
this.diffConst = 0.0;
this.motorConst = 0.0;
this.numEnzSites = 0;
this.compt = attrGetStr( attr, "compartment", "" );
this.setComptObj = function() {
if ( this.compt != "" )
this.compt = objLookup[ this.compt ];
}
this.fallbackSetParent = function() {
if ( this.base.parentObj == "" ) {
if ( this.compt == "" ) // Bad!
return;
this.compt.children.push( this.base.id );
this.base.parentObj = this.compt;
}
}
if ( isMooseSBML && anno.length > 0 ) {
var manno = annoGetManno( anno[0], "moose:ModelAnnotation" );
this.diffConst = annoGetFloat( manno, "moose:diffConstant", 0.0 );
this.motorConst = annoGetFloat( manno, "moose:motorConstant", 0.0);
}
}
/////////////////////////////////////////////////////////
function addEnzSubToMsg( id, xenz, enzPool, fg ) {
var xlist = xenz.getElementsByTagName( "listOfReactants" );
var enzPa = "";
var myMsgList = [];
if (xlist.length > 0 ) {
var xpool = xlist[0].getElementsByTagName( "speciesReference" );
var k;
for (k = 0; k < xpool.length; k++ ) {
var sattr = xpool[k].attributes;
var pool = sattr.species.nodeValue;
if (pool != enzPool ) { // Avoid parent pool of enzyme
var numpool=sattr.getNamedItem( "stoichiometry" ).nodeValue;
var msg = new MsgObj( "EnzSub", pool, id, numpool, fg );
msgData.push( msg );
myMsgList.push( msg );
}
}
}
return myMsgList;
}
function addEnzPrdToMsg( id, xenz, enzPool, fg ) {
var xlist = xenz.getElementsByTagName( "listOfProducts" );
var enzPa = "";
var myMsgList = [];
if (xlist.length > 0 ) {
var xpool = xlist[0].getElementsByTagName( "speciesReference" );
var k;
for (k = 0; k < xpool.length; k++ ) {
var sattr = xpool[k].attributes;
var pool = sattr.species.nodeValue;
if (pool != enzPool ) { // Avoid parent molecule of enzyme
var numpool=sattr.getNamedItem( "stoichiometry" ).nodeValue;
var msg = new MsgObj( "EnzPrd", id, pool, numpool, fg );
msgData.push( msg );
myMsgList.push( msg );
}
}
}
return myMsgList;
}
function addReactantsToMsg( id, xreac, listName, msgName, fg ) {
var xlist = xreac.getElementsByTagName( listName );
var myMsgList = [];
if (xlist.length > 0 ) {
var startIdx = 0;
var xpool = xlist[0].getElementsByTagName( "speciesReference" );
var k;
for (k = 0; k < xpool.length; k++ ){
var sattr = xpool[k].attributes;
// pool = sattr.getNamedItem( "species" ).nodeValue;
var pool = sattr.species.nodeValue;
var numpool = 1;
var numPoolItem = sattr.getNamedItem( "stoichiometry" );
if ( numPoolItem != null )
numpool = numPoolItem.nodeValue;
var msg;
if ( msgName.indexOf( "Prd" ) != -1 ) {
msg = new MsgObj( msgName, id, pool, numpool, fg );
} else {
msg = new MsgObj( msgName, pool, id, numpool, fg );
}
msgData.push( msg );
myMsgList.push( msg );
}
}
return myMsgList;
}
/////////////////////////////////////////////////////////
//
// Works for regular binding reaction.
function extractReacReagents( re ) {
var paId = re.base.parentObj.base.id;
var obj = [];
var k;
for ( k = 0; k < re.subs.length; k++ ) {
sub = objLookup[re.subs[k].src];
if ( sub.base.parentObj.base.id == paId )
obj.push( sub );
}
for ( k = 0; k < re.prds.length; k++ ) {
prd = objLookup[re.prds[k].dest];
if ( prd.base.parentObj.base.id == paId )
obj.push( prd );
}
return obj;
}
function spaceOutObjectsOnGrid( obj ) {
// Separates objects into sub-arrays, for each level of objects,
// then spaces each level individually.
const delta = poolHeight * 0.1;
obj.sort( function( a, b ) { return a.base.y - b.base.y } );
var lastobj = obj[0];
var subArray = [];
subArray.push( lastobj );
var i;
for ( i = 1; i < obj.length; i++ ) {
if ( ( obj[i].base.y - lastobj.base.y ) < delta ) {
subArray.push( obj[i] );
} else {
spaceOutObjectsInX( subArray );
lastobj = obj[i];
subArray = [];
subArray.push( lastobj );
}
}
if ( subArray.length > 1 )
spaceOutObjectsInX( subArray );
}
function spaceOutObjectsInX( obj ) { // takes array of objects, spreads out
obj.sort( function( a, b ) { return a.base.x - b.base.x } );
var mindx = poolWidth * objSpacing;
var i;
var j;
var k = 0; // Loop cutoff if it doesn't converge.
do {
j = 0;
for ( i = 1; i < obj.length; i++ ) { // start on 2nd object.
var dx = obj[i].base.x - obj[i-1].base.x;
if ( dx < mindx ) {
obj[i-1].base.x -= (mindx -dx/2) * 0.3;
obj[i].base.x += (mindx -dx/2) * 0.4;
j++;
}
}
k++;
} while (j > 0 && k < 20 );
}
function autoPositionReac( reacObj, xpos ) {
// var reacObj = objLookup[reac];
const xAvg = arr => arr.reduce((a, b) => a + b.base.x, 0) / arr.length;
const yMin = arr => arr.reduce((a, b) => Math.min(a, b.base.y), 1.0e6);
reacReagents = extractReacReagents( reacObj );
reacObj.base.x = xAvg( reacReagents );
reacObj.base.y = yMin( reacReagents ) + reacYoffsetFromReagents * poolHeight;
// reacObj.base.dispx = reacObj.base.x; //Reassigned by spaceOutObjects
reacObj.base.dispy = reacObj.base.y;
}
function ReacObj( xreac, anno, attr ) {
this.base = makeBaseObj( "Reac", xreac, anno, attr, "moose:ModelAnnotation" );
xparams = xreac.getElementsByTagName("localParameter");
if (xparams.length > 0) { // Here we compute Kf and Kb for SI units
this.innerKf = xparams[0].attributes.getNamedItem("value").nodeValue;
if (xparams.length > 1) {
this.innerKb = xparams[1].attributes.getNamedItem("value").nodeValue;
}
}
this.subs = addReactantsToMsg( this.base.id, xreac, "listOfReactants", "ReacSub",reacMsgColor);
this.prds = addReactantsToMsg( this.base.id, xreac, "listOfProducts", "ReacPrd", reacMsgColor);
var cs = concUnitScale[ concUnits ];
this.Kf = this.innerKf * Math.pow( cs, 1-this.subs.length );
this.Kb = this.innerKb * Math.pow( cs, 1-this.prds.length );
this.fallbackSetParent = function() {
if ( this.base.parentObj == "" ) {
var reactant = ""
if ( this.subs.length > 0 ) {
reactant = objLookup[this.subs[0].src]
} else if ( this.prds.length > 0 ) {
reactant = objLookup[this.prds[0].dest]
} else {
// Bad!
throw( "Reaction " + this.base.id + " has neither substrates nor products." );
}
if ( reactant == "" || typeof reactant === "undefined" ) {
throw( "Reactant not found for " + this.base.id );
}
var compt = reactant.base.parentObj;
compt.children.push( this.base.id );
this.base.parentObj = compt;
}
}
}
function getEnzParent( xenz, anno ) {
manno = anno.getElementsByTagName("moose:EnzymaticReaction");
if ( (typeof manno !== "undefined") && (manno.length > 0 ) ) {
var enzMol = manno[0].getElementsByTagName("moose:enzyme")[0].textContent;
return enzMol;
}
return "";
}
/////////////////////////////////////////////////////////
//
function extractEnzReagents( re ) {
var obj = [];
var k;
for ( k = 0; k < re.subs.length; k++ ) {
obj.push( objLookup[re.subs[k].src] );
}
for ( k = 0; k < re.prds.length; k++ ) {
obj.push( objLookup[re.prds[k].dest] );
}
return obj;
}
// This works both for EnzObj and MMEnzObj
function autoPositionEnz( enz ) {
var enzObj = objLookup[enz];
if (positionEnzWithParent) {
var ep = objLookup[ enzObj.enzPool ];
ep.numEnzSites++;
enzObj.base.x = ep.base.x;
enzObj.base.y = ep.base.y + ep.numEnzSites * enzYoffsetFromPool* poolHeight;
} else {
const xAvg = arr => arr.reduce( (a, b) => a + b.base.x, 0 ) / arr.length;
const yMax = arr => arr.reduce( (a, b) => Math.max(a, b.base.y), -1.0e6 );
enzReagents = extractEnzReagents( enzObj );
enzObj.base.x = xAvg( enzReagents );
enzObj.base.y = yMax( enzReagents ) + enzYoffsetFromReagents * poolHeight;
}
enzObj.base.dispx = enzObj.base.x;
enzObj.base.dispy = enzObj.base.y;
}
function EnzObj( xenz, anno, attr ) {
this.base = makeBaseObj( "Enz", xenz, anno, attr, "moose:EnzymaticReaction" );
var xparams = xenz.getElementsByTagName("localParameter");
this.Km = 0.0;
this.kcat = 0.0;
this.enzPool = getEnzParent(xenz, anno[0] );
if (xparams.length > 0) {
this.K1 = parseFloat( xparams[0].attributes.getNamedItem("value").nodeValue );
if (xparams.length > 1) {
this.K2 = parseFloat( xparams[1].attributes.getNamedItem("value").nodeValue );
}
}
this.prds = [];
this.addProduct = function( xenz, anno, attr ) {
var xparams = xenz.getElementsByTagName("localParameter");
if (xparams.length > 0) {
this.kcat = parseFloat( xparams[0].attributes.getNamedItem("value").nodeValue );
}
this.prds = addEnzPrdToMsg( this.base.id, xenz, this.enzPool, "red");
this.Km = this.calcKm( concUnits );
}
this.subs = addEnzSubToMsg( this.base.id, xenz, this.enzPool, "red" );
this.calcKm = function( concUnits ) {
var scale = concUnitScale[ concUnits ];
var innerKm = (this.kcat+ this.K2)/this.K1;
return innerKm * Math.pow( scale, this.subs.length );
}
}
/////////////////////////////////////////////////////////
function MMEnzObj( xenz, anno, attr, id, enzPool ) {
this.base = makeBaseObj( "MMEnz", xenz, anno, attr, "moose:EnzymaticReaction" );
this.enzPool = enzPool;
this.kcat = 0.0;
this.innerKm = 1.0;
xparams = xenz.getElementsByTagName("localParameter");
if (xparams.length >= 2) { // There must be a way to check id
this.innerKm = parseFloat( xparams[0].attributes.getNamedItem("value").nodeValue );
this.kcat = parseFloat( xparams[1].attributes.getNamedItem("value").nodeValue );
}
this.subs = addReactantsToMsg( this.base.id, xenz, "listOfReactants", "MMEnzSub", "blue" );
this.prds = addReactantsToMsg( this.base.id, xenz, "listOfProducts", "MMEnzPrd", "blue" );
this.Km = this.innerKm * Math.pow( concUnitScale[ concUnits ], this.subs.length );
}
////////////////////////////////////////////////////////////////////
function getPoolNameFromId( id ) {
var s = id.split('_');
var i;
var ret = '';
for ( i = 0; i < s.length - 3; i++) {
if ( i > 0 ) {
ret += '_';
}
ret += s[i];
}
return ret;
}
function FuncObj( tgtPool, argList ) {
var funcName = getPoolNameFromId( tgtPool ) + "_func";
// Later we may have more useful annotations on the Func, and if so we
// can fill in more terms from the src SBML file.
this.base = new ChemObj( funcName, "Func", tgtPool + "_func", "red", "maroon", 0, 0, "" );
this.tgtPool = tgtPool;
this.argList = argList;
this.func = "Plus"; // Later put in arbitrary expression.
this.addMsgs = function() {
// Add the msgs for the object, also do first pass assignment of
// its coords if these were pending at file load.
var msg = new MsgObj( "FuncOutput", this.base.id, this.tgtPool, 1, "blue");
msgData.push(msg );
var i;
for ( i = 0; i < this.argList.length; i++ ) {
msg = new MsgObj( "FuncInput", this.argList[i], this.base.id, 1, "blue");
msgData.push(msg );
}
/*
if ( this.base.x == 0 && this.base.y == 0 ) {
var tgtObjBase = objLookup[ this.tgtPool ].base;
this.base.dispx = this.base.x = tgtObjBase.x;
this.base.dispy = this.base.y = tgtObjBase.y + 5;
}
*/
}
}
/// Utility function for
function addMsgs( msg ) {
msg.addMsgs();
}
/////////////////////////////////////////////////////////
function isObjVisible( obj ) {
if ( obj.base.className == "Enz" || obj.base.className == "MMEnz" ) {
if ( obj.subs.length > 0 ) {
return objLookup[obj.subs[0].src].base.parentObj.base.visible;
}
} else {
return obj.base.parentObj.base.visible;
}
return false;
}