-
Notifications
You must be signed in to change notification settings - Fork 191
/
glmodel.js
3046 lines (2681 loc) · 126 KB
/
glmodel.js
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
// A model is a collection of related atoms. Bonds are only allowed between
//atoms in the same model. An atom is uniquely specified by its model id and
//its serial number.
//A glmodel knows how to apply the styles on each atom to create a gl object
var $3Dmol = $3Dmol || {};
/**
* GLModel represents a group of related atoms
* @constructor
* @param {number=} mid
* @param {Object=} defaultcolors Object defining default atom colors as atom => color property value pairs
* @see $3Dmol.download
*/
$3Dmol.GLModel = (function() {
// class variables go here
var defaultAtomStyle = {
line : {}
};
var defaultlineWidth = 1.0;
// Reference: A. Bondi, J. Phys. Chem., 1964, 68, 441.
// https://en.wikipedia.org/wiki/Van_der_Waals_radius
var vdwRadii = {
"H" : 1.2,
"He" : 1.4,
"Li" : 1.82,
"Be" : 1.53,
"B" : 1.92,
"C" : 1.7,
"N" : 1.55,
"O" : 1.52,
"F" : 1.47,
"Ne" :1.54,
"Na" : 2.27,
"Mg" : 1.73,
"Al" : 1.84,
"Si" : 2.1,
"P" : 1.8,
"S" : 1.8,
"Cl": 1.75,
"Ar": 1.88,
"K" : 2.75,
"Ca" : 2.31,
"Ni" : 1.63,
"Cu" : 1.4,
"Zn" : 1.39,
"Ga" : 1.87,
"Ge" : 2.11,
"As" : 1.85,
"Se" : 1.9,
"Br" : 1.85,
"Kr" : 2.02,
"Rb" : 3.03,
"Sr" : 2.49,
"Pd" : 1.63,
"Ag" : 1.72,
"Cd" : 1.58,
"In" : 1.93,
"Sn" : 2.17,
"Sb" : 2.06,
"Te" : 2.06,
"I" : 1.98,
"Xe" : 2.16,
"Cs" : 3.43,
"Ba" : 2.68,
"Pt" : 1.75,
"Au" : 1.66,
"Hg" : 1.55,
"Tl" : 1.96,
"Pb" : 2.02,
"Bi" : 2.07,
"Po" : 1.97,
"At" : 2.02,
"Rn" : 2.20,
"Fr" : 3.48,
"Ra" : 2.83,
"U" : 1.86
};
/*
The dictionaries are for dropdown menus and validation of the viewer
*/
GLModel.validElements = [
"H",
"Li",
"LI",
"Na",
"NA",
"K",
"C",
"N",
"O",
"F",
"P",
"S",
"CL",
"Cl",
"BR",
"Br",
"SE",
"Se",
"ZN",
"Zn",
"CU",
"Cu",
"NI",
"Ni"
];
// prop : It is used to add the option for property in context menu in the 3dmol ui
// the code for prop can be found under /ui/ui.js -> UI -> ContextMenu -> setProperties -> submit.ui.on
// gui : It is used to generate forms for different features in the 3dmol ui
// the code for gui can be found under /ui/form.js -> Form (Form defination)
// floatType : separates integer from float since these are used in
// input validation of the 3dmol ui
GLModel.validAtomSpecs = {
"resn":{type:"string",valid :true, prop: true, gui: true}, // Parent residue name
"x":{type:"number", floatType : true,valid:false,step:0.1, prop: true}, // Atom's x coordinate
"y":{type:"number", floatType : true,valid:false,step:0.1, prop: true}, // Atom's y coordinate
"z":{type:"number", floatType : true,valid:false,step:0.1, prop: true}, // Atom's z coordinate
"color":{type:"color",gui:false}, // Atom's color, as hex code
"surfaceColor":{type:"color",gui:false}, // Hex code for color to be used for surface patch over this atom
"elem":{type:"element",gui:true, prop: true}, // Element abbreviation (e.g. 'H', 'Ca', etc)
"hetflag":{type:"boolean",valid:false, gui:true}, // Set to true if atom is a heteroatom
"chain":{type:"string",gui:true, prop: true}, // Chain this atom belongs to, if specified in input file (e.g 'A' for chain A)
"resi":{type:"array_range",gui:true}, // Residue number
"icode":{type:"number",valid:false,step:0.1},
"rescode":{type:"number",valid:false,step:0.1, prop: true},
"serial":{type:"number",valid:false,step:0.1}, // Atom's serial id numbermodels
"atom":{type:"string",valid:false, gui:true, prop: true}, // Atom name; may be more specific than 'elem' (e.g 'CA' for alpha carbon)
"bonds":{type:"array",valid:false}, // Array of atom ids this atom is bonded to
"ss":{type:"string",valid:false}, // Secondary structure identifier (for cartoon render; e.g. 'h' for helix)
"singleBonds":{type:"boolean",valid:false}, // true if this atom forms only single bonds or no bonds at all
"bondOrder":{type:"array",valid:false}, // Array of this atom's bond orders, corresponding to bonds identfied by 'bonds'
"properties":{type:"properties",valid:false}, // Optional mapping of additional properties
"b":{type:"number", floatType : true,valid:false,step:0.1, prop: true}, // Atom b factor data
"pdbline":{type:"string",valid:false}, // If applicable, this atom's record entry from the input PDB file (used to output new PDB from models)
"clickable":{type:"boolean",valid:false, gui:false}, // Set this flag to true to enable click selection handling for this atom
"contextMenuEnabled":{type:"boolean",valid:false, gui:false}, // Set this flag to true to enable click selection handling for this atom
"callback":{type:"function",valid:false}, // Callback click handler function to be executed on this atom and its parent viewer
"invert":{type:"boolean",valid:false}, // for selection, inverts the meaning of the selection
//unsure about this
"reflectivity":{type:"number", floatType : true,gui:false,step:0.1}, //for describing the reflectivity of a model
"altLoc":{type:"invalid",valid:false}, //alternative location, e.g. in PDB
"sym":{type:'number',gui:false}, //which symmetry
};
//type is irrelivent here becuase htey are are invalid
var validExtras ={ // valid atom specs are ok too
"model":{type:"string",valid :false}, // a single model or list of models from which atoms should be selected
"bonds":{type:"number", valid :false, gui:true}, // overloaded to select number of bonds, e.g. {bonds: 0} will select all nonbonded atoms
"predicate":{type:"string",valid :false}, // user supplied function that gets passed an {AtomSpec} and should return true if the atom should be selected
"invert":{type:"boolean",valid :false, gui:true}, // if set, inverts the meaning of the selection
"byres":{type:"boolean",valid :false, gui:true}, // if set, expands the selection to include all atoms of any residue that has any atom selected
"expand":{type:"number",valid :false, gui:false}, // expands the selection to include all atoms within a given distance from the selection
"within":{type:"string",valid :false}, // intersects the selection with the set of atoms within a given distance from another selection
"and":{type:"string",valid :false}, // and boolean logic
"or":{type:"string",valid :false}, // or boolean logic
"not":{type:"string",valid :false}, // not boolean logic
};
GLModel.validAtomSelectionSpecs = $3Dmol.extend(GLModel.validAtomSpecs,validExtras);
var validLineSpec = {
"hidden":{type:"boolean",gui:true},
"linewidth":{type:"number", floatType : true,gui:true,step:0.1,default:defaultlineWidth},
"colorscheme":{type:"colorscheme",gui:true},
"color":{type:"color",gui:true},
"opacity":{type:"number", floatType : true,gui:true,step:0.1,default:1,min:0,max:1},
};
var validCrossSpec = {
"hidden":{type:"boolean",gui:true},
"linewidth":{type:"number", floatType : true,gui:false,step:0.1,default:defaultlineWidth,min:0},//deprecated
"colorscheme":{type:"colorscheme",gui:true},
"color":{type:"color",gui:true},
"radius":{type:"number", floatType : true,gui:true,step:0.1,default:1,min:0.1},
"scale":{type:"number", floatType : true,gui:true,step:0.1,default:1,min:0},
"opacity":{type:"number", floatType : true,gui:true,step:0.1,default:1,min:0,max:1},
};
var validStickSpec = {
"hidden":{type:"boolean",gui:true},
"colorscheme":{type:"colorscheme",gui:true},
"color":{type:"color",gui:true},
"radius":{type:"number", floatType : true,gui:true,step:0.1,default:0.25,min:0.1},
"singleBonds":{type:"boolean",gui:true},
"opacity":{type:"number", floatType : true,gui:true,step:0.1,default:1,min:0,max:1},
};
var validSphereSpec = {
"hidden":{type:"boolean",gui:false}, // needed in the new gui it has separate function to hide the spheres
"singleBonds":{type:"boolean",gui:true},
"colorscheme":{type:"colorscheme",gui:true},
"color":{type:"color",gui:true},
"radius":{type:"number", floatType : true,gui:true,step:0.1,default:1.5,min:0},
"scale":{type:"number", floatType : true,gui:true,step:0.1,default:1.0,min:0.1},
"opacity":{type:"number", floatType : true,gui:true,step:0.1,default:1,min:0,max:1},
};
var validCartoonSpec = {
"style":{validItems:["trace","oval","rectangle","parabola","edged"],gui:true},
"color":{type:"color",gui:true, spectrum : true},
"arrows":{type:"boolean",gui:true},
"ribbon":{type:"boolean",gui:true},
"hidden":{type:"boolean",gui:true},
"tubes":{type:"boolean",gui:true},
"thickness":{type:"number", floatType : true,gui:true,step:0.1,default:1,min:0},
"width":{type:"number", floatType : true,gui:true,step:0.1,default:1,min:0},
"opacity":{type:"number", floatType : true,gui:true,step:0.1,default:1,min:0,max:1},
};
GLModel.validAtomStyleSpecs = {
"line":{validItems:validLineSpec,type:"form",gui:true}, // draw bonds as lines
"cross":{validItems:validCrossSpec,type:"form",gui:true}, // draw atoms as crossed lines (aka stars)
"stick":{validItems:validStickSpec,type:"form",gui:true}, // draw bonds as capped cylinders
"sphere":{validItems:validSphereSpec,type:"form",gui:true}, // draw atoms as spheres
"cartoon":{validItems:validCartoonSpec,type:"form",gui:true}, // draw cartoon representation of secondary structure
"colorfunc":{validItems:null,type:"js",valid:false},
"clicksphere":{validItems:validSphereSpec,type:"form"} //invisible style for click handling
};
GLModel.validSurfaceSpecs = {
"opacity":{type:"number", floatType : true,gui:true,step:0.01,default:1,min:0,max:1},
"colorscheme":{type:"colorscheme",gui:true},
"color":{type:"color",gui:true},
"voldata":{type:"number", floatType : true,gui:false},
"volscheme":{type:"number", floatType : true,gui:false},
"map":{type:"number",gui:false}
};
GLModel.validLabelResSpecs = {
"font":{type:"string",gui:true},
"fontSize":{type:"number", floatType : true,gui:true,step:1,default:12,min:1},
"fontColor":{type:"color",gui:true},
"fontOpacity":{type:"number", floatType : true,gui:true,step:0.1,default:1,min:0,max:1},
"borderThickness":{type:"number", floatType : true,gui:true,step:0.1,default:1,min:0},
"borderColor":{type:"color",gui:true},
"borderOpacity":{type:"number", floatType : true,gui:true,step:0.1,default:1,min:0,max:1},
"backgroundColor":{type:"color",gui:true},
"backgroundOpacity":{type:"number", floatType : true,gui:true,step:0.1,default:1,min:0,max:1},
"position":{type:"array",valid:false},
"inFront":{type:"boolean",gui:true},
"showBackground":{type:"boolean",gui:true},
"fixed":{type:"boolean",gui:true},
"alignment":{validItems:["topLeft","topCenter","topRight","centerLeft","center","centerRight","bottomLeft","bottomCenter","bottomRight"],gui:true},
"scale":{type:"boolean",gui:false},
};
// class functions
// return true if a and b represent the same style
var sameObj = function(a,b) {
if(a && b)
return JSON.stringify(a) == JSON.stringify(b);
else
return a == b;
};
function GLModel(mid, options) {
// private variables
var atoms = [];
var frames = [];
var box = null;
var atomdfs = null; //depth first search over connected components
var id = mid;
var hidden = false;
var molObj = null;
var renderedMolObj = null;
var lastColors = null;
var modelData = {};
var modelDatas = null; //if there is different modelData per frame
var idMatrix = new $3Dmol.Matrix4();
var dontDuplicateAtoms = true;
var defaultColor = $3Dmol.elementColors.defaultColor;
options = options ? options : {};
var ElementColors = (options.defaultcolors) ? options.defaultcolors : $3Dmol.elementColors.defaultColors;
// drawing functions must be associated with model object since
// geometries can't span multiple canvases
// sphere drawing
var defaultSphereRadius = (options.defaultSphereRadius) ? options.defaultSphereRadius : 1.5;
var defaultCartoonQuality = (options.cartoonQuality)? options.cartoonQuality : 5;
// return proper radius for atom given style
/**
*
* @param {AtomSpec} atom
* @param {atomstyle} style
* @return {number}
*
*/
var getRadiusFromStyle = function(atom, style) {
var r = defaultSphereRadius;
if (typeof (style.radius) != "undefined")
r = style.radius;
else if (vdwRadii[atom.elem])
r = vdwRadii[atom.elem];
else if(atom.elem.length > 1) { //see if adjusting case helps
let e = atom.elem;
e[1] = e[1].toLowerCase();
if(vdwRadii[e]) r = vdwRadii[e];
}
if (typeof (style.scale) != "undefined")
r *= style.scale;
return r;
};
// cross drawing
/** @typedef CrossStyleSpec
* @prop {boolean} hidden - do not show
* @prop {number} linewidth *deprecated due to vanishing browser support*
* @prop {number} radius
* @prop {number} scale - scale radius by specified amount
* @prop {ColorschemeSpec} colorscheme - element based coloring
* @prop {ColorSpec} color - fixed coloring, overrides colorscheme
* @prop {number} opacity - opacity, must be the same for all atoms in the model
*/
/**
*
* @param {AtomSpec} atom
* @param {$3Dmol.Geometry[]} geos
*/
var drawAtomCross = function(atom, geos) {
if (!atom.style.cross)
return;
var style = atom.style.cross;
if (style.hidden)
return;
var linewidth = (style.linewidth || defaultlineWidth);
if (!geos[linewidth])
geos[linewidth] = new $3Dmol.Geometry();
var geoGroup = geos[linewidth].updateGeoGroup(6);
var delta = getRadiusFromStyle(atom, style);
var points = [ [ delta, 0, 0 ], [ -delta, 0, 0 ], [ 0, delta, 0 ],
[ 0, -delta, 0 ], [ 0, 0, delta ], [ 0, 0, -delta ] ];
var clickable = atom.clickable || atom.hoverable;
if (clickable && atom.intersectionShape === undefined)
atom.intersectionShape = {sphere : [], cylinder : [], line : []};
var c = $3Dmol.getColorFromStyle(atom, style);
var vertexArray = geoGroup.vertexArray;
var colorArray = geoGroup.colorArray;
for ( var j = 0; j < 6; j++) {
var offset = geoGroup.vertices*3;
geoGroup.vertices++;
vertexArray[offset] = atom.x + points[j][0];
vertexArray[offset+1] = atom.y + points[j][1];
vertexArray[offset+2] = atom.z + points[j][2];
colorArray[offset] = c.r;
colorArray[offset+1] = c.g;
colorArray[offset+2] = c.b;
if (clickable){
var point = new $3Dmol.Vector3(points[j][0], points[j][1], points[j][2]);
//decrease cross size for selection to prevent misselection from atom overlap
point.multiplyScalar(0.1);
point.set(point.x+atom.x, point.y+atom.y, point.z+atom.z);
atom.intersectionShape.line.push(point);
}
}
};
//from atom, return a normalized vector v that is orthogonal and along which
//it is appropraite to draw multiple bonds
var getSideBondV = function(atom, atom2, i) {
var i2, j2, atom3, p3, dir2;
var p1 = new $3Dmol.Vector3(atom.x, atom.y, atom.z);
var p2 = new $3Dmol.Vector3(atom2.x, atom2.y, atom2.z);
var dir = p2.clone();
var v = null;
dir.sub(p1);
var getGoodCross = function(atom, atom2) {
// get vector 2 different neighboring atom
//find most divergent neighbor
var bestv = null;
var bestlen = -1;
for(var j = 0, n = atom.bonds.length; j < n; j++) {
if(atom.bonds[j] != atom2.index) {
j2 = atom.bonds[j];
atom3 = atoms[j2];
p3 = new $3Dmol.Vector3(atom3.x, atom3.y, atom3.z);
dir2 = p3.clone();
dir2.sub(p1);
v = dir2.clone();
v.cross(dir);
var l = v.lengthSq();
if(l > bestlen) {
bestlen = l;
bestv = v;
if(bestlen > 0.1) {
return bestv;
}
}
}
}
return bestv;
};
if (atom.bonds.length === 1) {
if (atom2.bonds.length === 1) {
v = dir.clone();
if (Math.abs(v.x) > 0.0001)
v.y += 1;
else
v.x += 1;
} else {
i2 = (i + 1) % atom2.bonds.length;
j2 = atom2.bonds[i2];
atom3 = atoms[j2];
p3 = new $3Dmol.Vector3(atom3.x, atom3.y, atom3.z);
dir2 = p3.clone();
dir2.sub(p1);
v = dir2.clone();
v.cross(dir);
}
} else {
v = getGoodCross(atom,atom2);
if(v.lengthSq() < 0.01) {
var v2 = getGoodCross(atom2,atom);
if(v2 != null) v = v2; //can be null if no other neighbors
}
}
// especially for C#C (triple bond) dir and dir2
// may be opposites resulting in a zero v
if (v.lengthSq() < 0.01) {
v = dir.clone();
if (Math.abs(v.x) > 0.0001)
v.y += 1;
else
v.x += 1;
}
v.cross(dir);
v.normalize();
return v;
//v.multiplyScalar(r * 1.5);
};
var addLine = function(vertexArray, colorArray, offset, p1, p2, c1) {
//make line from p1 to p2, does not incremeant counts
vertexArray[offset] = p1.x; vertexArray[offset+1] = p1.y; vertexArray[offset+2] = p1.z;
colorArray[offset] = c1.r; colorArray[offset+1] = c1.g; colorArray[offset+2] = c1.b;
vertexArray[offset+3] = p2.x; vertexArray[offset+4] = p2.y; vertexArray[offset+5] = p2.z;
colorArray[offset+3] = c1.r; colorArray[offset+4] = c1.g; colorArray[offset+5] = c1.b;
};
/**@typedef LineStyleSpec
* @prop {boolean} hidden - do not show line
* @prop {number} linewidth *deprecated due to vanishing browser support*
* @prop {ColorschemeSpec} colorscheme - element based coloring
* @prop {ColorSpec} color - fixed coloring, overrides colorscheme
* @prop {number} opacity - opacity, must be the same for all atoms in the model
*/
// bonds - both atoms must match bond style
// standardize on only drawing for lowest to highest
/**
*
* @param {AtomSpec}
* atom
* @param {AtomSpec[]} atoms
* @param {$3Dmol.Geometry[]} geos
*/
var drawBondLines = function(atom, atoms, geos) {
if (!atom.style.line)
return;
var style = atom.style.line;
if (style.hidden)
return;
var p1a, p1b, p2a, p2b;
// have a separate geometry for each linewidth
var linewidth = (style.linewidth || defaultlineWidth);
if (!geos[linewidth])
geos[linewidth] = new $3Dmol.Geometry();
/** @type {geometryGroup} */
var geoGroup = geos[linewidth].updateGeoGroup(6*atom.bonds.length); //reserve enough space even for triple bonds
var vertexArray = geoGroup.vertexArray;
var colorArray = geoGroup.colorArray;
for ( var i = 0; i < atom.bonds.length; i++) {
var j = atom.bonds[i]; // our neighbor
var atom2 = atoms[j];
if (!atom2.style.line)
continue; // don't sweat the details
if (atom.index >= atom2.index) // only draw if less, this way we can do multi bonds correctly
continue;
var p1 = new $3Dmol.Vector3(atom.x, atom.y, atom.z);
var p2 = new $3Dmol.Vector3(atom2.x, atom2.y, atom2.z);
var mp = p1.clone().add(p2).multiplyScalar(0.5);
var singleBond = false;
var atomneedsi = atom.clickable || atom.hoverable;
var atom2needsi = atom2.clickable || atom2.hoverable;
if(atomneedsi || atom2needsi) {
if (atomneedsi){
if (atom.intersectionShape === undefined)
atom.intersectionShape = {sphere : [], cylinder : [], line : [], triangle : []};
atom.intersectionShape.line.push(p1);
atom.intersectionShape.line.push(mp);
}
if (atom2needsi) {
if (atom2.intersectionShape === undefined)
atom2.intersectionShape = {sphere : [], cylinder : [], line : [], triangle : []};
atom2.intersectionShape.line.push(mp);
atom2.intersectionShape.line.push(p2);
}
}
var c1 = $3Dmol.getColorFromStyle(atom, atom.style.line);
var c2 = $3Dmol.getColorFromStyle(atom2, atom2.style.line);
if(atom.bondStyles && atom.bondStyles[i]) {
var bstyle = atom.bondStyles[i];
if(!bstyle.iswire) {
continue;
}
if(bstyle.singleBond) singleBond = true;
if(typeof(bstyle.color1) != "undefined") {
c1 = $3Dmol.CC.color(bstyle.color1);
}
if(typeof(bstyle.color2) != "undefined") {
c2 = $3Dmol.CC.color(bstyle.color2);
}
}
var offset = geoGroup.vertices*3;
var mpa, mpb;
if(atom.bondOrder[i] > 1 && atom.bondOrder[i] < 4 && !singleBond) {
var v = getSideBondV(atom, atom2, i);
var dir = p2.clone();
dir.sub(p1);
if(atom.bondOrder[i] == 2) { //double
v.multiplyScalar(0.1);
p1a = p1.clone();
p1a.add(v);
p1b = p1.clone();
p1b.sub(v);
p2a = p1a.clone();
p2a.add(dir);
p2b = p1b.clone();
p2b.add(dir);
if(c1 == c2) {
geoGroup.vertices += 4;
addLine(vertexArray, colorArray, offset, p1a, p2a, c1);
addLine(vertexArray, colorArray, offset+6, p1b, p2b, c1);
}
else {
geoGroup.vertices += 8;
dir.multiplyScalar(0.5);
mpa = p1a.clone();
mpa.add(dir);
mpb = p1b.clone();
mpb.add(dir);
addLine(vertexArray, colorArray, offset, p1a, mpa, c1);
addLine(vertexArray, colorArray, offset+6, mpa, p2a, c2);
addLine(vertexArray, colorArray, offset+12, p1b, mpb, c1);
addLine(vertexArray, colorArray, offset+18, mpb, p2b, c2);
}
}
else if(atom.bondOrder[i] == 3) { //triple
v.multiplyScalar(0.1);
p1a = p1.clone();
p1a.add(v);
p1b = p1.clone();
p1b.sub(v);
p2a = p1a.clone();
p2a.add(dir);
p2b = p1b.clone();
p2b.add(dir);
if(c1 == c2) {
geoGroup.vertices += 6;
addLine(vertexArray, colorArray, offset, p1, p2, c1);
addLine(vertexArray, colorArray, offset+6, p1a, p2a, c1);
addLine(vertexArray, colorArray, offset+12, p1b, p2b, c1);
}
else {
geoGroup.vertices += 12;
dir.multiplyScalar(0.5);
mpa = p1a.clone();
mpa.add(dir);
mpb = p1b.clone();
mpb.add(dir);
addLine(vertexArray, colorArray, offset, p1, mp, c1);
addLine(vertexArray, colorArray, offset+6, mp, p2, c2);
addLine(vertexArray, colorArray, offset+12, p1a, mpa, c1);
addLine(vertexArray, colorArray, offset+18, mpa, p2a, c2);
addLine(vertexArray, colorArray, offset+24, p1b, mpb, c1);
addLine(vertexArray, colorArray, offset+30, mpb, p2b, c2);
}
}
}
else { //single bond
if(c1 == c2) {
geoGroup.vertices += 2;
addLine(vertexArray, colorArray, offset, p1, p2, c1);
} else {
geoGroup.vertices += 4;
addLine(vertexArray, colorArray, offset, p1, mp, c1);
addLine(vertexArray, colorArray, offset+6, mp, p2, c2);
}
}
}
};
// bonds as cylinders
var defaultStickRadius = 0.25;
/**@typedef SphereStyleSpec
* @prop {boolean} hidden - do not show atom
* @prop {number} radius - override van der waals radius
* @prop {number} scale - scale radius by specified amount
* @prop {ColorschemeSpec} colorscheme - element based coloring
* @prop {ColorSpec} color - fixed coloring, overrides colorscheme
* @prop {number} opacity - opacity, must be the same for all atoms in the model
*/
//sphere drawing
//See also: drawCylinder
/**
*
* @param {AtomSpec} atom
* @param {$3Dmol.Geometry} geo
*/
var drawAtomSphere = function(atom, geo) {
if (!atom.style.sphere)
return;
var style = atom.style.sphere;
if (style.hidden)
return;
var C = $3Dmol.getColorFromStyle(atom, style);
var radius = getRadiusFromStyle(atom, style);
if ((atom.clickable === true || atom.hoverable) && (atom.intersectionShape !== undefined)) {
var center = new $3Dmol.Vector3(atom.x, atom.y, atom.z);
atom.intersectionShape.sphere.push(new $3Dmol.Sphere(center, radius));
}
$3Dmol.GLDraw.drawSphere(geo, atom, radius, C);
};
/** Register atom shaped click handlers */
var drawAtomClickSphere = function(atom) {
if (!atom.style.clicksphere)
return;
var style = atom.style.clicksphere;
if (style.hidden)
return;
var radius = getRadiusFromStyle(atom, style);
if ((atom.clickable === true || atom.hoverable) && (atom.intersectionShape !== undefined)) {
var center = new $3Dmol.Vector3(atom.x, atom.y, atom.z);
atom.intersectionShape.sphere.push(new $3Dmol.Sphere(center, radius));
}
};
var drawAtomInstanced = function(atom, geo) {
if (!atom.style.sphere)
return;
var style = atom.style.sphere;
if (style.hidden)
return;
var radius = getRadiusFromStyle(atom, style);
var C = $3Dmol.getColorFromStyle(atom, style);
var geoGroup = geo.updateGeoGroup(1);
var startv = geoGroup.vertices;
var start = startv*3;
var vertexArray = geoGroup.vertexArray;
var colorArray = geoGroup.colorArray;
var radiusArray = geoGroup.radiusArray;
vertexArray[start] = atom.x;
vertexArray[start+1] = atom.y ;
vertexArray[start+2] = atom.z;
colorArray[start] = C.r;
colorArray[start+1] = C.g;
colorArray[start+2] = C.b;
radiusArray[startv] = radius;
if ((atom.clickable === true || atom.hoverable) && (atom.intersectionShape !== undefined)) {
var center = new $3Dmol.Vector3(atom.x, atom.y, atom.z);
atom.intersectionShape.sphere.push(new $3Dmol.Sphere(center, radius));
}
geoGroup.vertices += 1;
};
var drawSphereImposter = function(geo, center, radius, C) {
//create flat square
var geoGroup = geo.updateGeoGroup(4);
var i;
var startv = geoGroup.vertices;
var start = startv*3;
var vertexArray = geoGroup.vertexArray;
var colorArray = geoGroup.colorArray;
//use center point for each vertex
for( i = 0; i < 4; i++) {
vertexArray[start+3*i] = center.x;
vertexArray[start+3*i+1] = center.y ;
vertexArray[start+3*i+2] = center.z;
}
//same colors for all 4 vertices
var normalArray = geoGroup.normalArray;
for( i = 0; i < 4; i++) {
colorArray[start+3*i] = C.r;
colorArray[start+3*i+1] = C.g;
colorArray[start+3*i+2] = C.b;
}
normalArray[start+0] = -radius;
normalArray[start+1] = radius;
normalArray[start+2] = 0;
normalArray[start+3] = -radius;
normalArray[start+4] = -radius;
normalArray[start+5] = 0;
normalArray[start+6] = radius;
normalArray[start+7] = -radius;
normalArray[start+8] = 0;
normalArray[start+9] = radius;
normalArray[start+10] = radius;
normalArray[start+11] = 0;
geoGroup.vertices += 4;
//two faces
var faceArray = geoGroup.faceArray;
var faceoffset = geoGroup.faceidx; //not number faces, but index
faceArray[faceoffset+0] = startv;
faceArray[faceoffset+1] = startv+1;
faceArray[faceoffset+2] = startv+2;
faceArray[faceoffset+3] = startv+2;
faceArray[faceoffset+4] = startv+3;
faceArray[faceoffset+5] = startv;
geoGroup.faceidx += 6;
};
//dkoes - code for sphere imposters
var drawAtomImposter = function(atom, geo) {
if (!atom.style.sphere)
return;
var style = atom.style.sphere;
if (style.hidden)
return;
var radius = getRadiusFromStyle(atom, style);
var C = $3Dmol.getColorFromStyle(atom, style);
if ((atom.clickable === true || atom.hoverable) && (atom.intersectionShape !== undefined)) {
var center = new $3Dmol.Vector3(atom.x, atom.y, atom.z);
atom.intersectionShape.sphere.push(new $3Dmol.Sphere(center, radius));
}
drawSphereImposter(geo, atom, radius, C);
};
var drawStickImposter = function(geo, from, to, radius, color) {
//we need the four corners - two have from coord, two have to coord, the normal
//is the opposing point, from which we can get the normal and length
//also need the radius
var geoGroup = geo.updateGeoGroup(4);
var startv = geoGroup.vertices;
var start = startv*3;
var vertexArray = geoGroup.vertexArray;
var colorArray = geoGroup.colorArray;
var radiusArray = geoGroup.radiusArray;
var normalArray = geoGroup.normalArray;
//encode extra bits of information in the color
var r = color.r;
var g = color.g;
var b = color.b;
var negateColor = function(c) {
//set sign bit
var n = -c;
if(n == 0) n = -0.0001;
return n;
};
/* for sticks, always draw caps, but we could in theory set caps in color */
//4 vertices, distinguish between p1 and p2 with neg blue
var pos = start;
for(var i = 0; i < 4; i++) {
vertexArray[pos] = from.x;
normalArray[pos] = to.x;
colorArray[pos] = r;
pos++;
vertexArray[pos] = from.y;
normalArray[pos] = to.y;
colorArray[pos] = g;
pos++;
vertexArray[pos] = from.z;
normalArray[pos] = to.z;
if(i < 2)
colorArray[pos] = b;
else
colorArray[pos] = negateColor(b);
pos++;
}
geoGroup.vertices += 4;
radiusArray[startv] = -radius;
radiusArray[startv+1] = radius;
radiusArray[startv+2] = -radius;
radiusArray[startv+3] = radius;
//two faces
var faceArray = geoGroup.faceArray;
var faceoffset = geoGroup.faceidx; //not number faces, but index
faceArray[faceoffset+0] = startv;
faceArray[faceoffset+1] = startv+1;
faceArray[faceoffset+2] = startv+2;
faceArray[faceoffset+3] = startv+2;
faceArray[faceoffset+4] = startv+3;
faceArray[faceoffset+5] = startv;
geoGroup.faceidx += 6;
};
/**@typedef StickStyleSpec
* @prop {boolean} hidden - do not show
* @prop {number} radius
* @prop {boolean} singleBonds - draw all bonds as single bonds if set
* @prop {ColorschemeSpec} colorscheme - element based coloring
* @prop {ColorSpec} color - fixed coloring, overrides colorscheme
* @prop {number} opacity - opacity, must be the same for all atoms in the model
*/
// draws cylinders and small spheres (at bond radius)
var drawBondSticks = function(atom, atoms, geo) {
if (!atom.style.stick)
return;
var style = atom.style.stick;
if (style.hidden)
return;
var atomBondR = style.radius || defaultStickRadius;
var bondR = atomBondR;
var atomSingleBond = style.singleBonds || false;
var fromCap = 0, toCap = 0;
var atomneedsi, atom2needsi, i, singleBond, bstyle;
var cylinder1a, cylinder1b, cylinder1c, cylinder2a, cylinder2b, cylinder2c;
var C1 = $3Dmol.getColorFromStyle(atom, style);
var mp, mp2, mp3;
if (!atom.capDrawn && atom.bonds.length < 4)
fromCap = 2;
var drawCyl = $3Dmol.GLDraw.drawCylinder; //mesh cylinder
if(geo.imposter)
drawCyl = drawStickImposter;
for ( i = 0; i < atom.bonds.length; i++) {
var j = atom.bonds[i]; // our neighbor
var atom2 = atoms[j]; //parsePDB, etc should only add defined bonds
mp = mp2 = mp3 = null;
if (atom.index < atom2.index) {// only draw if less, this
// lets us combine
// cylinders of the same
// color
var style2 = atom2.style;
if (!style2.stick || style2.stick.hidden)
continue; // don't sweat the details
var C2 = $3Dmol.getColorFromStyle(atom2, style2.stick);
//support bond specific styles
bondR = atomBondR;
singleBond = atomSingleBond;
if(atom.bondStyles && atom.bondStyles[i]) {
bstyle = atom.bondStyles[i];
if(bstyle.iswire) {
continue;
}
if(bstyle.radius) bondR = bstyle.radius;
if(bstyle.singleBond) singleBond = true;
if(typeof(bstyle.color1) != "undefined") {
C1 = $3Dmol.CC.color(bstyle.color1);
}
if(typeof(bstyle.color2) != "undefined") {
C2 = $3Dmol.CC.color(bstyle.color2);
}
}
var p1 = new $3Dmol.Vector3(atom.x, atom.y, atom.z);
var p2 = new $3Dmol.Vector3(atom2.x, atom2.y, atom2.z);
// draw cylinders
if (atom.bondOrder[i] === 1 || singleBond || atom.bondOrder[i] > 3) { //TODO: aromatics at 4
if (!atom2.capDrawn && atom2.bonds.length < 4)
toCap = 2;
if (C1 != C2) {
mp = new $3Dmol.Vector3().addVectors(p1, p2)
.multiplyScalar(0.5);
drawCyl(geo, p1, mp, bondR, C1, fromCap, 0);
drawCyl(geo, mp, p2, bondR, C2, 0, toCap);
} else {
drawCyl(geo, p1, p2, bondR, C1, fromCap, toCap);
}
atomneedsi = atom.clickable || atom.hoverable;
atom2needsi = atom2.clickable || atom2.hoverable;
if(atomneedsi || atom2needsi) {
if(!mp) mp = new $3Dmol.Vector3().addVectors(p1, p2).multiplyScalar(0.5);
if (atomneedsi){
var cylinder1 = new $3Dmol.Cylinder(p1 , mp , bondR);
var sphere1 = new $3Dmol.Sphere(p1 , bondR);
atom.intersectionShape.cylinder.push(cylinder1);
atom.intersectionShape.sphere.push(sphere1);
}
if (atom2needsi){
var cylinder2 = new $3Dmol.Cylinder(p2 , mp , bondR);
var sphere2 = new $3Dmol.Sphere(p2 , bondR);
atom2.intersectionShape.cylinder.push(cylinder2);
atom2.intersectionShape.sphere.push(sphere2);
}
}
}
else if (atom.bondOrder[i] > 1) {
//multi bond caps
var mfromCap = 0;
var mtoCap = 0;
if(bondR != atomBondR) {
//assume jmol style multiple bonds - the radius doesn't fit within atom sphere
mfromCap = 2;
mtoCap = 2;
}
var dir = p2.clone();
var v = null;
dir.sub(p1);