-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathGroups.js
1216 lines (1150 loc) · 35.2 KB
/
Groups.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
var groups = new Array();
var groupNames = new Array();
var numGroups = 0;
var numKeys = 0;
var searchGroup = 0;
var showVal = new Array();
var showImVal = new Array();
var maxarrayget = 200;
function urlencode(str) {
var symbols = {
'@': '%40',
'&': '%26',
'*': '%2A',
'+': '%2B',
'/': '%2F',
'<': '%3C',
'>': '%3E'
};
return escape(str).replace(/([@*+/]|%26(amp|lt|gt)%3B)/g, function (m) { return symbols[m]; });
}
function createGroup(str) {
var g = new Array();
var i = 0;
var index = 0;
while ( (i+1) < str.length && str[i] && str[i] != "") {
var count = +str[i];
if (count > 0) {
g[index] = ["", [] ];
var list = str[i + 1].split("\t");
g[index][0] = list[0];
}
for (var j = 0; j < count; j++) {
var k = i + 2 + j;
if (k >= str.length) {
return g;
}
var list = str[k].split("\t");
g[index][1][j] = list[0];
}
if (count > 0) {
index++;
}
i = i + 2 + count;
}
return g;
}
function removeGroups(id) {
if (typeof(groups[id]) != 'undefined') {
delete groups[id];
delete groupNames[id];
numGroups--;
}
}
function addGroups(g, updateName) {
if (g && g.length > 0) {
for (var i = 0; i < g.length; i++) {
if (g[i] && g[i][1] && g[i][1].length > 0) {
groups[numKeys] = g[i][1];
groupNames[numKeys] = 'Group ' + numKeys;
if (updateName) {
groupNames[numKeys] = g[i][0];
}
numKeys++;
numGroups++;
}
}
}
}
function callDiffBA() {
var list = document.getElementsByName('selectGroup');
var g = new Array();
var num = 0;
for (var i = list.length - 1; i >= 0; i--) {
if (list[i].checked) {
if (num == 0) {
num++;
var id = list[i].value;
for (x in groups[id]) {
g[groups[id][x]] = 1;
}
}
else {
num++;
var id = list[i].value;
for (x in groups[id]) {
if (g[groups[id][x]]) {
g[groups[id][x]] = 0;
}
}
}
}
}
if (num > 0) {
var res = new Array();
for (x in g) {
if (g[x] == 1) {
res.push(x);
}
}
addGroups([["DiffBA", res]]);
updateGroupDisplay();
}
return false;
}
function callDiffAB() {
var list = document.getElementsByName('selectGroup');
var g = new Array();
var num = 0;
for (var i = 0; i < list.length; i++) {
if (list[i].checked) {
if (num == 0) {
num++;
var id = list[i].value;
for (x in groups[id]) {
g[groups[id][x]] = 1;
}
}
else {
num++;
var id = list[i].value;
for (x in groups[id]) {
if (g[groups[id][x]]) {
g[groups[id][x]] = 0;
}
}
}
}
}
if (num > 0) {
var res = new Array();
for (x in g) {
if (g[x] == 1) {
res.push(x);
}
}
addGroups([["DiffAB", res]]);
updateGroupDisplay();
}
return false;
}
function callIntersection() {
var list = document.getElementsByName('selectGroup');
var g = new Array();
var num = 0;
for (var i = 0; i < list.length; i++) {
if (list[i].checked) {
num++;
var id = list[i].value;
for (x in groups[id]) {
if (g[groups[id][x]]) {
g[groups[id][x]] += 1;
}
else {
g[groups[id][x]] = 1;
}
}
}
}
var res = new Array();
for (x in g) {
if (g[x] == num) {
res.push(x);
}
}
addGroups([["Intersect", res]]);
updateGroupDisplay();
return false;
}
function callUnion() {
var list = document.getElementsByName('selectGroup');
var g = new Array();
for (var i = 0; i < list.length; i++) {
if (list[i].checked) {
var id = list[i].value;
for (x in groups[id]) {
g[groups[id][x]] = 1;
}
}
}
var res = new Array();
for (x in g) {
res.push(x);
}
addGroups([["Union", res]]);
updateGroupDisplay();
return false;
}
function callRemove() {
var list = document.getElementsByName('selectGroup');
for (var i = 0; i < list.length; i++) {
if (list[i].checked) {
var id = list[i].value;
removeGroups(id);
}
}
updateGroupDisplay();
return false;
}
function callReset() {
groups = new Array();
groupNames = new Array();
numGroups = 0;
numKeys = 0;
searchGroup = 0;
showVal = new Array();
showImVal = new Array();
updateGroupDisplay();
return false;
}
function displayGroups() {
var str = '';
str += numGroups + "<br/>";
for (var i in groups) {
str += '<input type="checkbox" name="selectGroup" value="' + i + '" ';
str += ' onclick="changeImage();" />' ;
str += '<input type="text" size="10" value="' + groupNames[i] + '" ';
str += ' onchange="changeGroupNames(this, ' + i + ');" />' ;
str += ' ' + groups[i].length + ' <br/> ';
}
str += "";
if (showVal && showVal.length > 0) {
str += '<textarea name="showVal" cols="20" rows="5">';
for (var i in showVal) {
str += showVal[i] + "\n";
}
str += '</textarea>';
str += "<br/>\n";
str += "<div style=\"line-height:110%;\">\n";
var pattern = /(GSM\d+).*/;
for (var i in showVal) {
if (pattern.test(showVal[i])) {
var id = showVal[i].replace(pattern, "$1");
str += "<a href=\"http://www.ncbi.nlm.nih.gov/geo/query/acc.cgi?acc=" + id + "\" target=\"_blank\">" + id + "</a><br/>\n";
}
}
str += "</div>\n";
}
if (showImVal && showImVal.length > 0) {
var imgurl= document.getElementById('img0link').href;
var keys = getKeys(imgurl);
str += "<div style=\"line-height:110%;\">\n";
for (var i in showImVal) {
var id = showImVal[i];
str += "<a href=\"zoom.php?dataset=" + keys['id'] +
"&aid=" + id + "\" target=\"_blank\">" + id + "</a><br/>\n";
}
str += "</div>\n";
}
if (searchGroup) {
str += '<textarea id="searchGroupArea" name="searchGroupArea" cols="20" rows="5">';
str += '</textarea><br/>';
str += '<input type="button" name="searchGroup" value="searchGroup"';
str += ' onclick="callSearchGroup();" />' ;
}
return str;
}
function updateGroupDisplay() {
var dis = document.getElementById('group_display');
dis.innerHTML = displayGroups();
dis.style.visibility="visible";
}
function submitMouseEvent() {
if (xmlHttp != null && xmlHttp.readyState==4) {
var str = xmlHttp.responseText.split("\n");
if (str[0] > 0) {
addGroups(createGroup(str));
}
updateGroupDisplay();
}
}
function loadDisplay(mouse, e) {
var imgobj = document.getElementById('img0');
var ori = getOffset(imgobj);
var url = '';
url = url + "&top=" + mouse.getTop();
url = url + "&left=" + mouse.getLeft();
url = url + "&width=" + mouse.getWidth();
url = url + "&height=" + mouse.getHeight();
url = url + "&orix=" + ori.left;
url = url + "&oriy=" + ori.top;
var imgurl= document.getElementById('img0link').href;
try {
url = imgurl.replace("go=plot", "go=group") + url;
xmlHttp.open("GET",url,true);
xmlHttp.onreadystatechange=submitMouseEvent;
xmlHttp.send(null);
} catch(e) {
alert("URL Problem");
}
}
function getGroupStr() {
var list = document.getElementsByName('selectGroup');
var str = '';
for (var i = 0; i < list.length; i++) {
if (list[i].checked) {
var id = list[i].value;
str += i + '=' + groupNames[id] + '=' + groups[id].join(':') + ';' ;
}
}
return urlencode(str);
}
function getNumArrays() {
var list = document.getElementsByName('selectGroup');
var sum = 0;
for (var i = 0; i < list.length; i++) {
if (list[i].checked) {
var id = list[i].value;
sum += groups[id].length;
}
}
return sum;
}
function updateImg1linkPost(url) {
var list = url.split("?");
var d = list[1].split('&').reduce(function(s,c){
var t=c.split('=');s[t[0]]=t[1];return s;},{})
$.ajax({type: 'POST',
data: d,
url: list[0],
success: function (data) {
var ur = data;
document.getElementById('img0').src = ur;
document.getElementById('img1link').href = ur;
document.getElementById('img1link').style.visibility = 'visible';
}});
}
function changeImage() {
var num = getNumArrays();
var url = document.getElementById('img0link').href;
if (num < maxarrayget) {
url += '&groups=' + getGroupStr();
document.getElementById('img0').src = url;
document.getElementById('img1link').href = url;
document.getElementById('img1link').style.visibility = 'visible';
}
else {
var nurl = url + '&groups=' + getGroupStr();
updateImg1linkPost(nurl);
}
}
function changeGroupNames(box, id) {
groupNames[id] = box.value;
}
function callSurvival() {
var num = getNumArrays();
var str1 = escape(document.getElementById('CT').value);
var url = document.getElementById('img0link').href;
url += '&CT=' + str1 + '&groups=' + getGroupStr();
url = url.replace("go=plot", "go=survival");
if (num < maxarrayget) {
document.getElementById('img0').src = url;
document.getElementById('img2link').href = url;
document.getElementById('img2link').style.visibility = 'visible';
}
else {
var list = url.split("?");
var params = list[1];
xmlHttp.open("POST", list[0], true);
//Send the proper header information along with the request
xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xmlHttp.setRequestHeader("Content-length", params.length);
xmlHttp.setRequestHeader("Connection", "close");
xmlHttp.onreadystatechange = function() {
if(xmlHttp.readyState == 4 && xmlHttp.status == 200) {
var ur = xmlHttp.responseText;
document.getElementById('img0').src = ur;
document.getElementById('img2link').href = ur;
document.getElementById('img2link').style.visibility = 'visible';
}
}
xmlHttp.send(params);
}
}
function callScr() {
if (!(xmlHttp.readyState == 4 || xmlHttp.readyState == 0)) {
return;
}
var num = getNumArrays();
var str1 = escape(document.getElementById('CT').value);
var url = document.getElementById('img0link').href;
url += '&CT=' + str1 + '&groups=' + getGroupStr();
url = url.replace("go=plot", "go=getsurvival");
var ss = document.getElementById('results');
ss.innerHTML ='Please Wait ... <br/><img width=70 src="loading.gif"/>' ;
ss.style.visibility = 'visible';
if (num < maxarrayget) {
xmlHttp.open("GET", url, true);
xmlHttp.onreadystatechange = handleSurvival;
xmlHttp.send(null);
}
else {
var list = url.split("?");
var params = list[1];
xmlHttp.open("POST", list[0], true);
//Send the proper header information along with the request
xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xmlHttp.setRequestHeader("Content-length", params.length);
xmlHttp.setRequestHeader("Connection", "close");
xmlHttp.onreadystatechange = handleSurvival;
xmlHttp.send(params);
}
}
function handleSurvival() {
if (xmlHttp.readyState == 4) {
var ss = document.getElementById('results');
ss.innerHTML = '';
ss.innerHTML = xmlHttp.responseText;
ss.style.visibility = 'visible';
}
}
function callSelectLineTool() {
if (xmlHttp.readyState == 4 || xmlHttp.readyState == 0) {
var url = document.getElementById('img0link').href;
var str1 = escape(document.getElementById('clinical0').value);
url = url.replace("n=-1", "n=" + str1);
document.getElementById('imgh0').src = url;
}
}
function callSelectTool() {
if (xmlHttp.readyState == 4 || xmlHttp.readyState == 0) {
var s1 = escape(getDatasetID('dataset'));
var url = document.getElementById('img0link').href;
var list = url.split("?");
url = list[0] + '?go=selectPatientInfo';
url += '&id=' + s1;
var str1 = escape(document.getElementById('clinical0').value);
url += '&clinical=' + str1;
xmlHttp.open("GET", url, true);
xmlHttp.onreadystatechange = handleSelectTool;
xmlHttp.send(null);
}
}
function handleSelectTool() {
if (xmlHttp.readyState == 4) {
var ss = document.getElementById('selectPatientInfo');
ss.innerHTML = xmlHttp.responseText;
ss.style.visibility = 'visible';
}
}
function callPatientGroup(id, num) {
if (xmlHttp.readyState == 4 || xmlHttp.readyState == 0) {
var s1 = escape(getDatasetID('dataset'));
var url = document.getElementById('img0link').href;
var list = url.split("?");
url = list[0] + '?go=getPatients&id=' + s1;
url += '&clinical=' + num;
var str1 = escape(document.getElementById(id).value);
str1 = str1.replace(/\+/g, "%2B");
url += '&value=' + str1;
xmlHttp.open("GET", url, true);
xmlHttp.onreadystatechange = submitMouseEvent;
xmlHttp.send(null);
}
}
function callRect() {
if (xmlHttp.readyState == 4 || xmlHttp.readyState == 0) {
var url = document.getElementById('img0link').href;
url = url.replace("go=plot", "go=getrect");
xmlHttp.open("GET", url, true);
xmlHttp.onreadystatechange = handleRect;
xmlHttp.send(null);
}
}
function handleRect() {
if (xmlHttp.readyState == 4) {
var ss = document.getElementById('selectPatientInfo');
ss.innerHTML = xmlHttp.responseText;
ss.style.visibility = 'visible';
}
}
function callRectGroup(id) {
if (xmlHttp.readyState == 4 || xmlHttp.readyState == 0) {
var str1 = escape(document.getElementById(id).value);
var url = document.getElementById('img0link').href;
url += '&value=' + str1;
url = url.replace("go=plot", "go=getrectgroup");
xmlHttp.open("GET", url, true);
xmlHttp.onreadystatechange = submitMouseEvent;
xmlHttp.send(null);
}
}
function callThr() {
if (xmlHttp.readyState == 4 || xmlHttp.readyState == 0) {
var url = document.getElementById('img0link').href;
url = url.replace("go=plot", "go=getthr");
xmlHttp.open("GET", url, true);
xmlHttp.onreadystatechange = handleThr;
xmlHttp.send(null);
}
}
function handleThr() {
if (xmlHttp.readyState == 4) {
var ss = document.getElementById('selectPatientInfo');
ss.innerHTML = xmlHttp.responseText;
ss.style.visibility = 'visible';
}
}
function callThrGroup(id) {
var num = getNumArrays();
var url = document.getElementById('img0link').href;
url += '&groups=' + getGroupStr();
var str2 = escape(document.getElementById(id).value);
url += '&value=' + str2;
url = url.replace("go=plot", "go=getthrgroup");
document.getElementById('img0').src = url;
document.getElementById('img1link').href = url;
document.getElementById('img1link').style.visibility = 'visible';
}
function callGetLinePlots() {
if (xmlHttp.readyState == 4 || xmlHttp.readyState == 0) {
var ss = document.getElementById('lineresults');
ss.innerHTML ='Please Wait ... <br/><img width=70 src="loading.gif"/>' ;
ss.style.visibility = 'visible';
var s1 = escape(getDatasetID('dataset'));
var str1 = escape(document.getElementById('gList').value);
if ( !s1 ) {
ss.innerHTML = 'Select a dataset';
return;
}
if ( s1 && (str1 == null)) {
ss.innerHTML = 'Enter a list of genes';
return;
}
var params = "go=getlineplots&id=" + s1 + "&gList=" + str1;
var url = document.getElementById('img0link').href;
var list = url.split("?");
xmlHttp.open("POST", list[0], true);
//Send the proper header information along with the request
xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xmlHttp.setRequestHeader("Content-length", params.length);
xmlHttp.setRequestHeader("Connection", "close");
xmlHttp.onreadystatechange = function() {
if(xmlHttp.readyState == 4 && xmlHttp.status == 200) {
var ss = document.getElementById('lineresults');
ss.innerHTML = xmlHttp.responseText;
ss.style.visibility = 'visible';
}
}
xmlHttp.send(params);
}
}
function callBoxP() {
var num = getNumArrays();
var str1 = escape(document.getElementById('CT').value);
var url = document.getElementById('img0link').href;
url += '&CT=' + str1 + '&groups=' + getGroupStr();
url = url.replace("go=plot", "go=boxplot");
if (num < maxarrayget) {
document.getElementById('img0').src = url;
document.getElementById('img2link').href = url;
document.getElementById('img2link').style.visibility = 'visible';
}
else {
var list = url.split("?");
var params = list[1];
xmlHttp.open("POST", list[0], true);
//Send the proper header information along with the request
xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xmlHttp.setRequestHeader("Content-length", params.length);
xmlHttp.setRequestHeader("Connection", "close");
xmlHttp.onreadystatechange = function() {
if(xmlHttp.readyState == 4 && xmlHttp.status == 200) {
var ur = xmlHttp.responseText;
document.getElementById('img0').src = ur;
document.getElementById('img2link').href = ur;
document.getElementById('img2link').style.visibility = 'visible';
}
}
xmlHttp.send(params);
}
}
function callShow() {
var list = document.getElementsByName('selectGroup');
var g = new Array();
for (var i = 0; i < list.length; i++) {
if (list[i].checked) {
var id = list[i].value;
for (x in groups[id]) {
g[groups[id][x]] = 1;
}
}
}
var res = new Array();
for (x in g) {
res.push(x);
}
showVal = res;
updateGroupDisplay();
}
function callShowIm() {
var list = document.getElementsByName('selectGroup');
var g = new Array();
for (var i = 0; i < list.length; i++) {
if (list[i].checked) {
var id = list[i].value;
for (x in groups[id]) {
g[groups[id][x]] = 1;
}
}
}
var res = new Array();
for (x in g) {
res.push(x);
}
showImVal = res;
updateGroupDisplay();
}
function callStats() {
var url = document.getElementById('img0link').href;
url = url.replace("go=plot", "go=getlstats");
xmlHttp.open("GET", url, true);
xmlHttp.onreadystatechange = function() {
if(xmlHttp.readyState == 4 && xmlHttp.status == 200) {
var ur = xmlHttp.responseText;
var ss1 = document.getElementById('lineresults');
ss1.innerHTML = ur;
ss1.style.visibility = 'visible';
}
}
xmlHttp.send(null);
}
function getKeys(str) {
var url= str;
var pr = url.split("?");
var vars = pr[1].split("&");
var keys = new Array();
keys['?'] = pr[0];
for (var i=0;i<vars.length;i++) {
var pair = vars[i].split("=");
keys[pair[0]] = pair[1];
}
return keys;
}
function callClear() {
var ss = document.getElementById('results');
ss.innerHTML = '';
ss.style.visibility = 'visible';
var ss1 = document.getElementById('lineresults');
ss1.innerHTML = '';
ss1.style.visibility = 'visible';
}
var selectionRect = {
element : null,
previousElement : null,
currentY : 0,
currentX : 0,
originX : 0,
originY : 0,
setElement: function(ele) {
this.previousElement = this.element;
this.element = ele;
},
getNewAttributes: function() {
var x = this.currentX<this.originX?this.currentX:this.originX;
var y = this.currentY<this.originY?this.currentY:this.originY;
var width = Math.abs(this.currentX - this.originX);
var height = Math.abs(this.currentY - this.originY);
return {
x : x,
y : y,
width : width,
height : height
};
},
getCurrentAttributes: function() {
// use plus sign to convert string into number
var x = +this.element.attr("x");
var y = +this.element.attr("y");
var width = +this.element.attr("width");
var height = +this.element.attr("height");
return {
x1 : x,
y1 : y,
x2 : x + width,
y2 : y + height
};
},
getCurrentAttributesAsText: function() {
var attrs = this.getCurrentAttributes();
return "x1: " + attrs.x1 + " x2: " + attrs.x2 + " y1: " + attrs.y1 + " y2: " + attrs.y2;
},
init: function(newX, newY, svg) {
var rectElement = svg.append("rect")
.attr("rx", 4)
.attr("ry", 4)
.attr("x", 0)
.attr("y", 0)
.attr("width", 0)
.attr("height", 0)
.classed("selection", true);
this.setElement(rectElement);
this.originX = newX;
this.originY = newY;
this.update(newX, newY);
},
update: function(newX, newY) {
this.currentX = newX;
this.currentY = newY;
var a = this.getNewAttributes();
for (var k in a) {
this.element.attr(k, a[k]);
}
},
focus: function() {
this.element
.style("stroke", "#DE695B")
.style("stroke-width", "1");
},
remove: function() {
this.element.remove();
this.element = null;
},
removePrevious: function() {
if(this.previousElement) {
this.previousElement.remove();
}
}
};
function displayCorrRes(corr, cData) {
var columns = corrData.getColumns();
d3.select("#tablenum").text(function (a) { return corr.length; });
var table = d3.select("#lineresults").select("table")
.html(function (a) { return ""; });
var thead = table.append("thead"),
tbody = table.append("tbody");
thead.append("tr").selectAll("th").data(columns).enter().append("th")
.text(function (c) { return c; });
var rows = tbody.selectAll("tr").data(corr).enter().append("tr");
var cells = rows.selectAll("td")
.data(function(row) {
var r = [row, "E", "E", "E"];
if (typeof row != "undefined") {
r = [row].concat(cData.get(row));
r[1] = cData.getC(row, 0);
r[2] = cData.getC(row, 1);
}
return r;})
.enter()
.append("td")
.attr("style", "font-family: Courier") // sets the font style
.html(function(d) { return d; });
cells.each(function(p, j) {
if (j === 1) {
var id = d3.select(this.previousElementSibling).text();
d3.select(this).text(null)
.append("a").attr("href", "javascript:void(0)").html(function (d) {
return d;})
.on("click", function(d,i){
cData.changeExplore(id, 1);
return false;});
}
if (j === 2) {
var sib = this.previousElementSibling;
var id = d3.select(sib.previousElementSibling).text();
d3.select(this).text(null)
.append("a").attr("href", "javascript:void(0)").html(function (d) {
return d;})
.on("click", function(d,i){
cData.changeExplore(id, 0);
return false;});
}
});
}
function dragStart() {
var p = d3.mouse(this);
selectionRect.init(p[0], p[1], d3.select(this));
selectionRect.removePrevious();
}
function dragMove() {
var p = d3.mouse(this);
selectionRect.update(p[0], p[1]);
}
function dragEnd(cData, xMap, yMap) {
var finalAttributes = selectionRect.getCurrentAttributes();
if(finalAttributes.x2 - finalAttributes.x1 > 1 && finalAttributes.y2 - finalAttributes.y1 > 1){
// range selected
d3.event.sourceEvent.preventDefault();
selectionRect.focus();
var x1 = xMap.invert(finalAttributes.x1);
var x2 = xMap.invert(finalAttributes.x2);
var y1 = yMap.invert(finalAttributes.y1);
var y2 = yMap.invert(finalAttributes.y2);
var res = cData.getKeys(x1, y1, x2, y2);
displayCorrRes(res, cData);
}
}
var corrData = { obj : null,
keys : null,
url : null,
columns : null,
keysSorted : [ null, null],
init : function(obj, url) {
this.obj = obj;
this.url = url;
var keys = d3.keys(obj);
this.keys = keys.slice(0);
this.keysSorted[0] = keys.slice(0).sort(function(a,b){return obj[a][0]-obj[b][0]});
this.keysSorted[1] = keys.slice(0).sort(function(a,b){return obj[a][1]-obj[b][1]});
},
get: function(id) {
return this.obj[id];
},
getC: function(id, i) {
var str = (+this.obj[id][i]).toPrecision(3);
return str;
},
setColumns: function (c) { this.columns = c; },
getColumns: function () { return this.columns; },
binaryIndexOf: function(valueIndex, searchElement) {
var array = this.keysSorted[valueIndex];
var minIndex = 0;
var maxIndex = array.length - 1;
var currentIndex;
var currentElement;
while (minIndex <= maxIndex) {
currentIndex = (minIndex + maxIndex) / 2 | 0;
currentElement = this.obj[array[currentIndex]][valueIndex];
if (currentElement < searchElement) {
minIndex = currentIndex + 1;
}
else if (currentElement > searchElement) {
maxIndex = currentIndex - 1;
}
else {
return currentIndex;
}
}
return minIndex;
},
getKeys : function(x1, y1, x2, y2) {
var xi1 = this.binaryIndexOf(0, x1);
var xi2 = this.binaryIndexOf(0, x2);
var yi1 = this.binaryIndexOf(1, y1);
var yi2 = this.binaryIndexOf(1, y2);
if (xi2 < xi1) {
var tmp = xi1;
xi1 = xi2;
xi2 = tmp;
}
if (yi2 < yi1) {
var tmp = yi1;
yi1 = yi2;
yi2 = tmp;
}
var hh = d3.map();
var array = this.keysSorted[1];
for (var i = yi1; i < array.length && i <= yi2; i++) {
hh.set(array[i], 1);
}
array = this.keysSorted[0];
var res = [];
for (var i = xi1; i < array.length && i <= xi2; i++) {
if (hh.has(array[i])) {
res.push(array[i]);
}
}
return res.reverse();
},
changeExplore : function(id, index) {
var list = this.url.split("?");
var d = list[1].split('&').reduce(function(s,c){
var t=c.split('=');s[t[0]]=t[1];return s;},{})
d["go"] = "plotids";
var groups = d["groups"];
delete d["groups"];
var url = list[0] + "?" + jQuery.param(d);
d3.json(url, function(data) {
data[index][0] = id;
var id1 = data[0][0];
var id2 = data[1][0];
var url = list[0] + "?go=getplots&A=" + id1 +
"&B=" + id2 + "&id=" + d["id"];
d3.tsv(url, function(data) {
if (data.columns && data.columns.length === 5) {
var img0link = data.columns[4];
var list = img0link.split("?");
var h = list[1].split('&').reduce(function(s,c){
var t=c.split('=');s[t[0]]=t[1];return s;},{})
h["xn"] = data.columns[1];
h["yn"] = data.columns[3];
img0link = list[0] + "?" + jQuery.param(h);
var img1link = img0link +"&groups="+groups;
d3.select("#img0link").attr("href", function (p, i) {
return img0link;});
d3.select("#gInfoX").text(id1 + " : " + h["xn"]);
d3.select("#gInfoY").text(id2 + " : " + h["yn"]);
updateImg1linkPost(img1link);
}
});
});
},
search : function (val, max) {
if (val in this.obj) {
return [val];
}
var obj = this.obj;
var index = this.columns.length - 2;
var res = [];
this.keys.forEach(function (e, i, a) {
if (obj[e][index].search(val) >= 0 && res.length < max) {
res.push(e);
}
});
return res;
}
};
function displayGCorr(data, url) {
try {
var obj = JSON.parse(data);
var keys = d3.keys(obj);
corrData.init(obj, url);
var columns = ["ID", "corrX", "corrY", "n1", "n2", "Name"];
corrData.setColumns(columns);
var mn = {t: 20, r: 20, b: 20, l: 30},
width = 300, height = 150;
// add the graph canvas to the body of the webpage
d3.select("#lineresults").html("")
var svg = d3.select("#lineresults").append("svg")
.attr("width", width).attr("height", height);
var tablesearch = d3.select("#lineresults").append("input")
.attr("type", "text").attr("size", 10).attr("id", "tablesearch")
.attr("value", "");
var tablego = d3.select("#lineresults").append("input")
.attr("type", "button").attr("name", "GO")
.attr("value", "GO").on("click", function () {
var val = document.getElementById("tablesearch").value;
var res = corrData.search(val, 100);
displayCorrRes(res, corrData);
});
var tablenum = d3.select("#lineresults").append("div")
.attr("id", "tablenum");
var table = d3.select("#lineresults").append("table")
.attr("id", "tableresults").attr("border", 0);
var x = d3.scaleLinear().domain([-1, 1]).range([mn.l, width-mn.r]),
y = d3.scaleLinear().domain([-1, 1]).range([height-mn.b, mn.t]);
x.clamp(true);
y.clamp(true);
var xAxis = d3.axisBottom(x).ticks(8),
yAxis = d3.axisLeft(y).ticks(5);
var gx = svg.append("g")
.attr("class", "axis axis--x")
.attr("transform", "translate("+ 0 +"," + (height - mn.b) + ")")
.call(xAxis);
var gy = svg.append("g")
.attr("class", "axis axis--y")