-
Notifications
You must be signed in to change notification settings - Fork 1
/
plot.js
1053 lines (905 loc) · 29.5 KB
/
plot.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
// interactive diversity plot
var cx,cy;
var rsize = 6;
var imgwidth = 150; var imgheight = 110;
var BGCOLOR = "#eaeaea";
var MENU_RESET = 1, MENU_DOWNLOAD = 2, MENU_OPTIONS = 3, MENU_INFO = 4;
var TOUCH = 1, MARK = 2, LEGEND = 3;
var rtag = Math.sqrt(imgwidth*imgwidth + imgheight+imgheight) * 0.8;
var g,g2; // graphics
var touched = -1; // touched point
var tagborder = 5; var tagshadow = 10;
var menudark = false; // indicates menu touch
var menu = null;
var xdown = -1;
var ydown = -1;
var zooming = false;
var showLegend = true;
var showAxes = true;
var pids = []; // only points with ids in pids will be drawn
var zerox,zero,unitx,unity,tickx,ticky; // for axes
var minaxx,maxaxx,minaxy,maxaxy;
var craigset = ["00001","00002","00003","00005","00006","00007","00008","00009","00010","00013","00014","00017","00018","00020","00024","00027","00031","00059","00065","00067","00087","00091","00124","01135","02114","06278"];
//var imgserver = "http://molecule.nibr.novartis.net/";
var imgserver = "https://peter-ertl.com/molecular/substituents/";
var doCraig = false;
$(document).ready(function(){
// this makes page non-scrollable (otherwise x,y messed-up)
$("html,body").css({overflow: 'hidden', height: '100%', margin:0, padding:0});
cx = document.body.clientWidth;
cy = document.body.clientHeight;
// add menu image
menu = new Image();
//menu.src = "menu4.png";
menu.src = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAGAAAAAYCAQAAACv4DAfAAAAAmJLR0QA/4ePzL8AAAAJcEhZcwAACxMAAAsTAQCanBgAAAAHdElNRQfjCAYIDC9AaPR8AAADeUlEQVRYw92Xz0tUURTHPzPVjELkBLUoqCAwxiS0X+gmIlr2Awm0omhRi5EU6g8wnH4YkQSVE7mwNlGLQl1IbYIWUYtqssFFhEplKm3EotJx3ticFvN8vfvm/SJHos5dvXPPvfec8773e86F/1biC7RvkBJKKaWEYDG2W+w407pAQWzjBCFA4zYvPK3DHKSOrawCPpOklwdk/B0kiK8AxDT8yHk0BEGjzdP2EOPK/oIwRoP7os10MkhGN48XLYCADqC75BCEHHd1EAUcoHYTQegnRjkhQmygkRSCkHCCXyldlniLE8Ai9tNOLRH2MmxYD7OPCDVcpo5FBWtuIkxzoiANMdIICXv3nyKkucIWwkWF0Fr6yTLBc77o+c//gy88Z4Isb1hXAB5hmh22u+0kjVBfONGF8IlKw7Hf7i+bVwBLaNNxP+d4hjQ/TRqNNoVOwowjSvbV3RsRRglZsS+kDffNDLSej2yaRwDVJtgI33lEC8308NWkHaLKtOIYQn/BKWYgpRCOqMd0IlyxdeASwg2bOiEOwwq8SpLM6nNTtLAcgDJO8U3XzpI0pQ56EGKuwD2J8EBVDSJscWBu4Z1tqfPjPgSJ0ouGkOMxKw19hD5yCBq9RBVeGUEodw0givBBVWUQwrbGJYhD8Yj7cD8vu5lE0DhvIs0ArWgIk+y2WGuIBeFWgIbnfAqa8oQDigOOM3HOKt9nPXlrVvkKFbPEDyJstp3ZjvDWpWfyyn6QCgNCfUQM/Qoek0PI2kJog+sfqEB4X1g4Ltg6cB2hw7Xtc68Zlbw2LvE3TlMGBFjOGaaMS/yKjaYV3QiNrgE0I9xXj6limLU2JFpNhpwLjeat3aCj0uhXummihYf8MGkHFRo9ipBSWgwrjQ4gHLYetFTJ6dzh406F27cs4aJSyH6SJmOqyEKGc5ZCNmYhUjWAJoQR5xuUh0Qp27lOBuGJAzv5l3UMkGWCZ5ZWYlJvJZKssaxoQEiz03a3XcwgHPC+lPlDEg7ui8sobObquEyNpZkbYg8Ramlnn00zl0BIE7P0qgGamEG4hierzPCWDp8thFcA5nb6ntFO3/FopxMIQoqTRAkTpoJmBhCEq+6vubjvx8mfyEXjQXPO07ae0YLkjKjgCTiE0OowM3+p4bj+pLzFS0/rEPXUsY3VwDhJeulG+5tP+qI/6v95+QVbQvzAXfsxeAAAAABJRU5ErkJggg==";
$("#canvas").mousemove(function(e) {
var rect = canvas.getBoundingClientRect();
var x = e.pageX - rect.left;
var y = e.pageY - rect.top;
if (e.which == 1) { // left button pressed
if (!zooming) {
if (xdown==-1 && ydown==-1) return; // too fast movement
// very small drags ignored
var dx = Math.abs(x-xdown);
var dy = Math.abs(y-ydown);
if (dx < 10 && dy < 10) return;
zooming = true;
// clearing touched and glass canvas
$("#tags").empty();
g2.clearRect(0,0,cx,cy);
$("#pop").css({visibility:'hidden'});
}
// redrawing glass canvas with selection rectangle
//g2.clearRect(0,0,cx,cy);
redrawGlassCanvas();
g2.strokeStyle = "black";
g2.beginPath();
g2.rect(xdown,ydown,x-xdown,y-ydown);
g2.stroke();
}
else {
$("html,body").css("cursor","pointer");
// x and y are relative to canvas
updateTouch(x,y);
}
});
$("#canvas").mouseleave(function(e) {
updateTouch(-1,-1);
});
$("#canvas").mousedown(function(e) {
var rect = canvas.getBoundingClientRect();
xdown = e.pageX - rect.left;
ydown = e.pageY - rect.top;
});
$("#canvas").mouseup(function(e) {
if (zooming) { // end of zooming
zooming = false;
// get only zoomed subset in the rectangle
var rect = canvas.getBoundingClientRect();
var x = e.pageX - rect.left;
var y = e.pageY - rect.top;
minxrect = Math.min(x,xdown);
maxxrect = Math.max(x,xdown);
minyrect = Math.min(y,ydown);
maxyrect = Math.max(y,ydown);
pd = [];
for (var i=0;i<p.length;i++) {
p[i].mark = false;
if (p[i].x < minxrect || p[i].x > maxxrect) continue;
if (p[i].y < minyrect || p[i].y > maxyrect) continue;
pd.push(i);
}
if (pd.length < 10) {
alert("Too few molecules selected (only " + pd.length + "), select at least 10!");
g2.clearRect(0,0,cx,cy);
return;
}
pids = pd;
// redrawing zoomed subset
scalePoints();
drawAll();
}
});
$("#canvas").click(function(e) { // or mousedown?
var rect = canvas.getBoundingClientRect();
var x = e.pageX - rect.left;
var y = e.pageY - rect.top;
updateClick(x,y,e);
});
$("body").on("mousemove",".tag",function(e) {
$(this).draggable();
var rect = canvas.getBoundingClientRect();
var x = e.pageX - rect.left;
var y = e.pageY - rect.top;
// need to hide all molpops
$("html,body").css("cursor","move");
// redrawing glass canvas - connections to all tags
redrawGlassCanvas();
});
// tag all molecules
// keypress not working for Esc, but keyup yes
$("body").keyup(function(e) {
//console.log(e.keyCode,e.which,e.key);
if(e.key=='t'|| e.key=='T') { // t(ag)
if (pids.length > 500) {
alert("Molecule images can be displayed for max. 500 structures!");
return;
}
showLegend = false;
for (var n=0;n<pids.length;n++) {
i = pids[n];
p[i].mark = true;
}
drawAll();
showTags();
}
if(e.key=='c' || e.key=='C') {
showCraigPlot();
}
if(e.key=='a' || e.key=='A') {
// marking all visible points
//drawAll(); // not, this removes marks
for (var n=0;n<pids.length;n++) {
i = pids[n];
p[i].mark = true;
}
drawAll();
}
if(e.key == 'Escape') { // Esc
resetAll();
}
});
// options changed - update plot
$("#opt-submit").click(function(e) {
// remove tags and close pop-ups
$("#tags").empty();
// n most common
doCraig = false;
var upto = $("#opt-upto option:selected").text();
if (upto == "all") upto = p.length;
if (upto == "Craig set") {upto = p.length; doCraig = true;}
var types = $("#opt-type").val();
// attached to
var ats = $("#opt-attachedto").val();
if (ats == "all" || ats.length == 0) ats = [0];
// natoms
var natoms = $("#opt-natoms option:selected").text();
if (natoms == "all" ) natoms = 15;
// marking points according to the options
pd = []
for (var i=0;i<upto;i++) {
p[i].mark = false;
if (p[i].na > natoms) continue;
if (doCraig) {
if (!craigset.includes(p[i].id)) continue;
}
else {
if (i > upto) break;
}
if (types != null && parseInt(types[0]) != 0) {
var ok = false;
for (var j=0;j<types.length;j++) {
if (p[i].t == parseInt(types[j])) ok = true;
}
if (!ok) continue;
}
if (parseInt(ats[0]) != 0) {
var ok = false;
for (var j=0;j<ats.length;j++) {
for (var k=0;k<p[i].at.length;k++) {
if (p[i].at[k] == parseInt(ats[j])) {ok = true; break;}
}
}
if (!ok) {
continue;
}
}
pd.push(i);
}
if (pd.length < 10) {
alert("Too few substituents selected (only " + pd.length + ") - change the options");
g2.clearRect(0,0,cx,cy);
return;
}
pids = pd;
if ($("#opt-legend").is(':checked')) showLegend = true;
else showLegend = false;
if ($("#opt-axes").is(':checked')) showAxes = true;
else showAxes = false;
if ($("#opt-smallimg").is(':checked')) {imgwidth=90; imgheight=66;}
else {imgwidth=150; imgheight=110;}
$("#options").dialog('close');
//console.log("OPTIONS",upto,types,ats,natoms);
//console.log("OPTIONS",showLegend,showAxes,imgwidth);
// redrawing subset
scalePoints();
drawAll();
}); // end options changed
$("#opt-cancel").click(function(e) {
$("#options").dialog('close');
});
$("#opt-reset").click(function(e) {
$("#options").dialog('close');
resetAll();
});
// browser resize
$(window).on('resize', function() {
// this sometimes wotking sometims not
cx = document.body.clientWidth;
cy = document.body.clientHeight;
g.canvas.width = cx;
g.canvas.height = cy;
g2.canvas.width = cx;
g2.canvas.height = cy;
scalePoints();
drawAll();
//location.reload(); // reset all, how to keep current selection?
});
// colors
for (var i=0;i<p.length;i++) {
if (!p[i].t) {p[i].t = "#0000ff";} // !!! check
}
var canvas = $("#canvas")[0];
var canvas2 = $("#canvas2")[0];
g = canvas.getContext('2d')
g2 = canvas2.getContext('2d')
g.canvas.width = cx;
g.canvas.height = cy;
g2.canvas.width = cx;
g2.canvas.height = cy;
resetAll();
menu.onload = function() {
drawMenu();
}
});
function drawTagLines() {
// called from redrawGlassCanvas - always cleans background
// draw only shadow and line
g2.strokeStyle = "black";
g2.lineWidth = 2;
g2.setLineDash([3,3]);
$("#tags").children().each(function(i) {
var tag = $(this);
var tpos = tag.position();
var torg = this.id.substring(3); // id of tag origin
var xc = tpos.left + imgwidth/2;
var yc = tpos.top + imgheight/2;
// line
g2.beginPath();
g2.moveTo(p[torg].x,p[torg].y);
g2.lineTo(xc,yc);
g2.stroke();
// shadow
g2.fillStyle = "gray";
g2.globalAlpha = 0.7;
var b = 10 * imgwidth / 150. ;
g2.fillRect(tpos.left+b,tpos.top+b,imgwidth,imgheight);
});
g2.globalAlpha = 1;
g2.lineWidth = 1;
g2.setLineDash([]);
}
function scalePoints() {
// scaling points stored in pids
var minx = Number.MAX_VALUE; var maxx = -Number.MAX_VALUE; var miny = Number.MAX_VALUE; var maxy = -Number.MAX_VALUE;
for (var n=0;n<pids.length;n++) {
var i = pids[n];
// adding also mark info
//p[i].mark = false; // needs to keep for resize
p[i].x = p[i].p; p[i].y = p[i].s; // pi,hammett => x,y
p[i].y = -p[i].y // nitro should by at the top as in Craig's plot
if (p[i].x < minx) minx = p[i].x;
if (p[i].x > maxx) maxx = p[i].x;
if (p[i].y < miny) miny = p[i].y;
if (p[i].y > maxy) maxy = p[i].y;
}
var lenx = maxx - minx;
var leny = maxy - miny;
minaxx = minx;
maxaxx = maxx;
minaxy = miny;
maxaxy = maxy;
var border = 15;
var bordert = border; var borderb = border; var borderl = border; var borderr = border;
if (doCraig) { // special borders for Criag plot with tabs
bordert = 50; borderb = 100; borderl = 150; borderr = 60;
}
for (var i=0;i<p.length;i++) {
var x = borderl + (p[i].x -minx) * (cx - borderl - borderr) / lenx;
p[i].x = x;
var y = bordert + (p[i].y -miny) * (cy - bordert - borderb) / leny;
p[i].y = y;
}
zerox = borderl - minx * (cx - borderl - borderr) / lenx;
zeroy = bordert - miny * (cy - bordert - borderb) / leny;
unitx = borderl + (1. - minx) * (cx - borderl - borderr) / lenx - zerox;
unity = bordert + (1. - miny) * (cy - bordert - borderb) / leny - zeroy;
var nx = cx / unitx;
var ny = cy / unity;
if (nx > 5) tickx = 1.;
else if (nx > 1.5) tickx = 0.5;
else tickx = 0.2;
if (ny > 5) ticky = 1.;
else if (ny > 2.0) ticky = 0.5;
else if (ny > 0.5) ticky = 0.2;
else ticky = 0.1;
}
function drawCircle(x,y,color,type) {
// shaded drawing (up to 3 colors and touch)
var c,cdark;
var gg = g;
var rr = rsize;
if (type == TOUCH) {
c = "red";
cdark = "#660000";
gg = g2;
rr = rsize + 2;
}
else if (type == MARK) { // marked are drawn to g2
c = "magenta";
cdark = "#306";
gg = g2;
}
else if (type == LEGEND) { // legend circles painted on g2
gg = g2;
c = color;
cdark = darkenColor(c);
}
else { // 10 shaded colors
c = color;
cdark = darkenColor(c);
}
// gradient
var grd = g.createRadialGradient(x-rr/3.,y-rr/3.,rr*.7,x,y,rr * 1.5);
grd.addColorStop(0,c);
grd.addColorStop(1,cdark);
gg.beginPath();
gg.fillStyle = grd;
gg.arc(x,y,rr,0,2*Math.PI);
gg.fill();
// white reflex
gg.fillStyle = "white"
gg.beginPath();
gg.arc(x-rr/3.,y-rr/3.,rr/4.,0,2*Math.PI);
gg.fill();
// circle around
gg.strokeStyle = cdark;
gg.beginPath();
gg.arc(x,y,rr,0,2*Math.PI);
gg.stroke();
}
function getTouchedPoint(x,y) {
// points
rsizeplus = rsize + 20;
rsize2plus = (rsize + 20) * (rsize + 20); // ???
min = 999;
t = -1;
for (var i=0;i<p.length;i++) {
dx = p[i].x - x;
if (Math.abs(dx) > rsizeplus) continue;
dy = p[i].y - y;
if (Math.abs(dy) > rsizeplus) continue;
d = dx * dx + dy * dy;
if (d < rsize2plus) {
if (d < min) {
min = d;
t = i;
}
}
}
return t;
}
function updateTouch(x,y) {
var m = checkMenu(x,y);
if (menudark && !m) {
// cleaning menu - !!! redraw points that may be there
g2.fillStyle = BGCOLOR;
g2.fillRect(5,5,24*4,24);
g2.drawImage(menu,5,5);
menudark = false;
}
var t = getTouchedPoint(x,y);
if (t == touched) return; // no action necessary
if (touched > -1) {
if (p[touched].mark) {
drawCircle(p[touched].x,p[touched].y,color[p[touched].t],MARK);
}
else {
// returning previously touched to normal
redrawGlassCanvas();
}
$("#pop").css({visibility: 'hidden'});
}
if (t > -1) {
drawCircle(p[t].x,p[t].y,color[p[t].t],TOUCH);
// molecule pane
// where to place it
dx = p[t].x - cx/2;
dy = p[t].y - cy/2;
rx = Math.sqrt(dx * dx + dy * dy);
ex = rtag * dx / rx
ey = rtag * dy / rx
px = p[t].x + ex - imgwidth / 2;
py = p[t].y + ey - imgheight / 2;
// checking for placing outside borders
if (py < 0) {
py = 0;
px = p[t].x - 20 -imgwidth;
if (p[t].x > cx/2) px = p[t].x + 20;
}
if (py+imgheight > cy) {
py = cy-imgheight;
px = p[t].x - 20 -imgwidth;
if (p[t].x > cx/2) px = p[t].x + 20;
}
// check x-axis for border crossing
if (px < 0) {
px = p[t].x + 50;
}
if (px + imgwidth > cx) {
px = p[t].x - imgwidth - 50;
}
// pop molimage
// need to check whether this tag exists
var tagid = "tag" + t;
if ($("#"+tagid).length) {
// visited existing tag
touched = t;
return;
}
var imgid = p[t].id.substring(0,2) + "/" + p[t].id + ".png"
url = imgserver + "/rimages/" + imgid
$("#molimage").attr("src",url).width(imgwidth).height(imgheight);
var molpop = $("#pop");
molpop.css({position:'fixed',left:px,top:py, visibility: 'visible', 'z-index':9}); // molecule pop always on top
}
touched = t;
}
function checkMenu(x,y) {
// border 5, icon size 24
if (x < 5) return 0;
if (x > 5 + 24*4) return 0;
if (y > 5 + 24) return 0;
if (y < 5) return 0;
menudark = true;
g2.fillStyle = BGCOLOR;
g2.fillRect(5,5,24*4,24);
g2.fillStyle = "#bbb";
if (x < 30) {
g2.fillRect(5,5,24,24);
g2.drawImage(menu,5,5);
return MENU_RESET;
}
else if (x < 54) {
g2.fillRect(5+24,5,24,24);
g2.drawImage(menu,5,5);
return MENU_DOWNLOAD;
}
else if (x < 78) {
g2.fillRect(5+24*2,5,24,24);
g2.drawImage(menu,5,5);
return MENU_OPTIONS;
}
else {
g2.fillRect(5+24*3,5,24,24);
g2.drawImage(menu,5,5);
return MENU_INFO;
}
}
function updateClick(x,y,e) {
var m = checkMenu(x,y);
if (m == MENU_RESET) {
resetAll();
return;
}
else if (m == MENU_DOWNLOAD) {
downloadSelectedMols();
return;
}
else if (m == MENU_OPTIONS) {
$("#options").css({visibility:'visible'});
$("#options").dialog({width:550, height:580});
return;
}
else if (m == MENU_INFO) {
$("#info").css({visibility:'visible'});
$("#info").dialog({width:620, height:520});
return;
}
// checking touch of point or tag
var t = getTouchedPoint(x,y);
if (t == -1) return;
// marking or deleting existing mark
if (p[t].mark) {
p[t].mark = false;
drawCircle(p[t].x,p[t].y,color[p[t].t],0);
// removesing this tag if existing
var tagid = "tag" + t;
if ($("#"+tagid).length) { // element exists
//console.log("tag point click");
$("#"+tagid).remove();
}
return;
}
// select/mark point
p[t].mark = true;
drawCircle(p[t].x,p[t].y,color[p[t].t],MARK);
if (!e.shiftKey) return;
var pos = $("#pop").position();
drawTag(t,pos.left,pos.top); // set tag paramters
redrawGlassCanvas(); // must be called, otherwise shade top left
$("#pop").css({visibility: 'hidden'});
// adding tag to the list of tags
//tags.push({"n":t,"id":tagid,"top":pos.top,"left":pos.left});
}
function redrawGlassCanvas() {
g2.clearRect(0,0,cx,cy);
drawMenu();
if (showLegend) drawLegend();
if (showAxes) drawAxes();
// drawing all marked
for (var i=0;i<p.length;i++) {
if (!p[i].mark) continue;
drawCircle(p[i].x,p[i].y,color[p[i].t],MARK);
}
drawTagLines();
// frame around
g2.strokeStyle = "black";
g2.lineWidth = 1;
g2.rect(1,1,cx-2,cy-2);
g2.stroke();
}
// draws molecule tag (after shift click or programatically)
function drawTag(t,left,top) {
var lt = moveTagFromBorder(left,top);
//left = lt[0]; top = lt[1];
// adding new fixed panel
var tagid = "tag" + t;
var imgid = "img" + t;
var tag = '<div id=' + tagid + ' class="tag" style="width:' + imgwidth + 'px; height:' + imgheight + 'px; border: 1px solid black;"><img id=' + imgid + '></img></div>';
$("#tags").append(tag); // necessary to become visible
// need to store also origin (molecule position) of this tag
var imgname = p[t].id.substring(0,2) + "/" + p[t].id + ".png"
url = imgserver + "/rimages/" + imgname;
$("#"+imgid).attr("src",url).width(imgwidth).height(imgheight);
$("#"+tagid).css({position:'fixed',left:left,top:top,visibility:'visible','z-index':3});
}
function moveTagFromBorder(left,top) {
if (left < tagborder) left = tagborder;
if (top < tagborder) top = tagborder;
if (left + imgwidth + tagborder + tagshadow > cx) left = cx - imgwidth -tagborder;
if (top + imgheight + tagborder + tagshadow > cy) top = cy - imgheight - tagshadow -tagborder;
return [left,top];
}
// showing tags over the points
function showTags() {
$("#tags").empty(); // removing old (if any)
var n = -1;
var tp = new Object();
var tag2p = new Object();
for (var i=0;i<pids.length;i++) {
var ii = pids[i];
if (!p[ii].mark) continue;
n++;
tp[n] = new Object();
tp[n].x = p[ii].x;
tp[n].y = p[ii].y;
tag2p[n] = ii;
}
var ntags = n+1;
if (ntags == 0) {
//alert("No molecules selected");
return;
}
avoidTagOverlap(ntags,tp,tag2p);
for (var n=0;n<ntags;n++) {
// tp[] coordinates are in top left corner
// drawing with middle of the tag over the point
drawTag(tag2p[n],tp[n].x-imgwidth/2, tp[n].y-imgheight/2);
}
redrawGlassCanvas();
}
function showCraigPlot() {
resetAll();
// ??? strange, old JQuery ??? // need to be opened first
$("#options").css({visibility:'visible'});
$("#options").dialog({width:550, height:580});
$("#options").dialog('close');
//var upto = $("#opt-upto").setText("Craig set");
$("#opt-upto").prop('selectedIndex',7);
$("#opt-legend").prop('checked',false);
$("#opt-axes").prop('checked',true);
$("#opt-submit").click();
// mark items
for (var n=0;n<pids.length;n++) {
i = pids[n];
p[i].mark = true;
}
console.log("CP",pids.length);
showTags();
}
function avoidTagOverlap(ntags,tp,tag2p) {
// tp is center point
var maxsteps = 100;
if (ntags > 200) maxsteps = 20;
if (ntags > 500) maxsteps = 1;
if (ntags > 1000) maxsteps = 0;
var imgw2 = imgwidth / 2.;
var imgh2 = imgheight / 2.;
// initial removel from borders
for (var i=0;i<ntags;i++) {
if (tp[i].x -imgw2 < tagborder) tp[i].x = tagborder + imgw2;
if (tp[i].x + imgw2 + tagborder + tagshadow > cx) tp[i].x = cx - tagborder -tagshadow - imgw2;
if (tp[i].y - imgh2 < tagborder) tp[i].y = tagborder + imgh2;
if (tp[i].y + imgh2 + tagborder + tagshadow > cy) tp[i].y = cy - tagborder -tagshadow - imgh2;
}
// keep the original position
var op = new Object();
for (var i=0;i<ntags;i++) {
op[i] = new Object();
op[i].x = tp[i].x;
op[i].y = tp[i].y;
}
var wp = imgwidth *.8; // allowed overlap
var hp = imgheight *.9;
var m = 10; // initial move - is then decreased
for (var step=0;step<maxsteps;step++) {
if (step > 10) m = 2;
for (var i=0;i<ntags-1;i++) {
for (var j=i+1;j<ntags;j++) {
// need to move
// but must not move too much from the original pos (op)
// moving max imwidth from the op
var dx = tp[i].x - tp[j].x;
var dy = tp[i].y - tp[j].y;
if (Math.abs(dx) > wp) continue;
if (Math.abs(dy) > hp) continue;
// saving positio in case it goes over border
var oix = tp[i].x;
var oiy = tp[i].y;
var ojx = tp[j].x;
var ojy = tp[j].y;
var ratio = (Math.abs(dx) + Math.abs(dy)) / 10.;
if (Math.abs(dx) < 0.001 && Math.abs(dy) < 0.001) { // 2 points overlap exactly
dx = m; dy = m; ratio = 1.;
}
tp[i].x += dx / ratio;
tp[j].x -= dx / ratio;
tp[i].y += dy / ratio;
tp[j].y -= dy / ratio;
// check going beyound borders
// align with moveTagFromBorder
if (tp[i].x -imgw2 < tagborder) tp[i].x = tagborder + imgw2;
if (tp[i].x +imgw2 +tagborder+tagshadow > cx) tp[i].x = cx - tagborder -tagshadow - imgw2;
if (tp[j].x -imgw2 < tagborder) tp[j].x = tagborder + imgw2;
if (tp[j].x +imgw2 + tagborder + tagshadow > cx) tp[j].x = cx - tagborder - tagshadow - imgw2;
if (tp[i].y - imgh2 < tagborder) tp[i].y = tagborder + imgh2;
if (tp[i].y + imgh2 + tagborder + tagshadow > cy) tp[i].y = cy - tagborder -tagshadow - imgh2;
if (tp[j].y - imgh2 < tagborder) tp[j].y = tagborder + imgh2;
if (tp[j].y + imgh2 + tagborder + tagshadow > cy) tp[j].y = cy - tagborder -tagshadow - imgh2;
// check distance from origin
var maxr = imgw2;
var dx = tp[i].x - op[i].x;
var dy = tp[i].y - op[i].y;
var r = Math.sqrt(dx*dx + dy * dy);
if (r > maxr) {tp[i].x = oix, tp[i].y = oiy;}
var dx = tp[j].x - op[j].x;
var dy = tp[j].y - op[j].y;
var r = Math.sqrt(dx*dx + dy * dy);
if (r > maxr) {tp[j].x = ojx, tp[j].y = ojy;}
var dx = tp[i].x - tp[j].x;
var dy = tp[i].y - tp[j].y;
}
}
}
}
function downloadSelectedMols() {
var n = 0;
var s = "SMILES\tHansch pi\tHammett sigma p\n"
for (var i=0;i<p.length;i++) {
if (p[i].mark) {
n += 1;
s += p[i].smi + "\t" + p[i].id + "\t" + p[i].p + "\t" + p[i].s + "\n";
}
}
if (n == 0) {
alert("No molecules selected");
return;
}
var molwin = window.open("","Selected molecules","height=400,width=650,scrollbars=yes,toolbar=no,menubar=no,status=no,titlebar=no,resizable=yes,left=100,top=100")
var doc = molwin.document;
doc.open("text/html");
doc.write("<pre>"+s+"</pre>");
doc.close();
}
function drawAll() {
g.fillStyle = BGCOLOR;
g.fillRect(0,0,cx,cy);
// keeping subset
/*
for (var i=0;i<p.length;i++) {
p[i].mark = false;
}
*/
touched = -1;
$("#pop").css({visibility:'hidden'});
// redraw all
for (var n=0;n<pids.length;n++) {
i = pids[n];
drawCircle(p[i].x,p[i].y,color[p[i].t],0);
}
// g2
redrawGlassCanvas();
if ($("#tags").children().length > 0) showTags();
}
function resetAll() {
resetOptions(); // but they are not read!
// remove selection and tags
$("#tags").empty();
g2.clearRect(0,0,cx,cy);
pids = []
for (var i=0;i<p.length;i++) pids[i] = i;
scalePoints();
g.fillStyle = BGCOLOR;
g.fillRect(0,0,cx,cy);
// draw points
if (typeof rrsize != 'undefined') rsize = rrsize;
for (var i=0;i<p.length;i++) {
drawCircle(p[i].x,p[i].y,color[p[i].t],0);
}
redrawGlassCanvas();
}
function drawMenu() {
// info
var info = pids.length + " substituents";
g2.font = "22px Arial";
var w = g2.measureText(info).width; // need to know w of the bg rectangle
// rectangle below
g2.fillStyle = BGCOLOR;
g2.globalAlpha = 0.8;
g2.fillRect(0,0,24*4+w+25,52);
g2.globalAlpha = 1;
g2.fillStyle = BGCOLOR;
g2.fillRect(5,5,24*4,24);
g2.drawImage(menu,5,5);
// info
var info = pids.length + " substituents";
g2.fillStyle = "black";
g2.fillText(info,114,25);
g2.font = "14px Arial";
g2.fillStyle = "black";
g2.fillText("Substituent plot v2019.09, by Peter Ertl",8,45);
}
// reset the option pane
function resetOptions() {
$("#opt-type").prop('selectedIndex',0);
$("#opt-upto").prop('selectedIndex',0);
$("#opt-attachedTo").prop('selectedIndex',0);
$("#opt-natoms").prop('selectedIndex',0);
$("#opt-legend").prop('checked',true);
$("#opt-axes").prop('checked',true);
$("#opt-smallimg").prop('checked',false);
$("#options").dialog();
$("#opt-submit").click();
$("#options").dialog('close');
doCraig = false;
showLegend = true;
showAxes = true;
imgwidth = 150; imgheight = 110;
}
function drawLegend() {
// on g or g2 ???
if (typeof legend == 'undefined') return;
if (cx < 650) {
console.log("screen too small to drawlegend");
return;
}
g2.font = "20px Arial";
g2.fillStyle = "black";
var legh = legend.length * 24 + 20;
var x = 35;
var y = cy-12-legy;
maxw = 0;
for (var i=0;i<legend.length;i++) {
var w = g2.measureText(legend[i][1]).width;
if (legend[i][0] == "#") w -= 40; // no circle
if (w > maxw) maxw = w;
}
var legw = maxw + 60;
// position of the legend
var legb = 20; // border
var legx = legb;
var legy = cy - legb - legh;
legx = cx - legw - legb;
legy = legb;
// legend position bottom left
if (typeof legpos !== 'undefined' && legpos == "bl") {
legx = legb;
legy = cy - legh - legb;
}
// legend position top left (below info line)
if (typeof legpos !== 'undefined' && legpos == "tl") {
legx = legb;
legy = legb + 20;
}
// rectangle below legend
g2.fillStyle = BGCOLOR;
g2.globalAlpha = 0.8;
var b = 3;
g2.fillRect(legx-b,legy-b,legw+2*b,legh+2*b);
g2.globalAlpha = 1;
// frame around the canvas
g2.strokeStyle = "black";
g2.beginPath(); // must be here
g2.rect(legx,legy,legw,legh);
g2.stroke();
var y = legy-2;
for (var i=0;i<legend.length;i++) {
var group = legend[i][0]
y += 24;
if (legend[i][0] == "#") { // header info, ni circle
g2.fillStyle = "black";
g2.fillText(legend[i][1],legx+10,y+7);
}
else { // normal circle + legend
drawCircle(legx+22,y,color[group],LEGEND)
g2.fillStyle = "black";
g2.fillText(legend[i][1],legx+40,y+7);
}
}
}
function drawAxes() {
g2.font = "20px Arial";
g2.fillStyle = "black";
// x axis (pi)
for (var x=-5;x<=5;x+=tickx) {
var xx = zerox + x*unitx;
if (xx > cx - 20) continue;
if (xx < 20) continue;
g2.beginPath();
g2.moveTo(xx,cy);
g2.lineTo(xx,cy-10);
g2.stroke();