forked from CCGE-BOADICEA/pedigreejs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathio.js
987 lines (906 loc) · 32.1 KB
/
io.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
// pedigree utils
(function (utils, $, undefined) {
utils.isIE = function () {
var ua = navigator.userAgent;
/* MSIE used to detect old browsers and Trident used to newer ones*/
return ua.indexOf("MSIE ") > -1 || ua.indexOf("Trident/") > -1;
}
utils.isEdge = function () {
return navigator.userAgent.match(/Edge/g);
}
/**
* Get formatted time or data & time
*/
utils.getFormattedDate = function (time) {
var d = new Date();
if (time)
return ('0' + d.getHours()).slice(-2) + ":" + ('0' + d.getMinutes()).slice(-2) + ":" + ('0' + d.getSeconds()).slice(-2);
else
return d.getFullYear() + "-" + ('0' + (d.getMonth() + 1)).slice(-2) + "-" + ('0' + d.getDate()).slice(-2) + " " + ('0' + d.getHours()).slice(-2) + ":" + ('0' + d.getMinutes()).slice(-2) + ":" + ('0' + d.getSeconds()).slice(-2);
}
/**
* Show message or confirmation dialog.
* @param title - dialog window title
* @param msg - message to diasplay
* @param onConfirm - function to call in a confirmation dialog
* @param opts - pedigreejs options
* @param dataset - pedigree dataset
*/
utils.messages = function (title, msg, onConfirm, opts, dataset) {
if (onConfirm) {
$('<div id="msgDialog">' + msg + '</div>').dialog({
modal: true,
title: title,
width: 350,
buttons: {
"Yes": function () {
$(this).dialog('close');
onConfirm(opts, dataset);
},
"No": function () {
$(this).dialog('close');
}
}
});
} else {
$('<div id="msgDialog">' + msg + '</div>').dialog({
title: title,
width: 350,
buttons: [{
text: "OK",
click: function () { $(this).dialog("close"); }
}]
});
}
}
/**
* Validate age and yob is consistent with current year. The sum of age and
* yob should not be greater than or equal to current year. If alive the
* absolute difference between the sum of age and year of birth and the
* current year should be <= 1.
* @param age - age in years.
* @param yob - year of birth.
* @param status - 0 = alive, 1 = dead.
* @return true if age and yob are consistent with current year otherwise false.
*/
utils.validate_age_yob = function (age, yob, status) {
var year = new Date().getFullYear();
var sum = parseInt(age) + parseInt(yob);
if (status == 1) { // deceased
return year >= sum;
}
return Math.abs(year - sum) <= 1 && year >= sum;
}
utils.capitaliseFirstLetter = function (string) {
return string.charAt(0).toUpperCase() + string.slice(1);
}
}(window.utils = window.utils || {}, jQuery));
// pedigree I/O
(function (io, $, undefined) {
// cancers, genetic & pathology tests
io.cancers = {
'breast_cancer': 'breast_cancer_diagnosis_age',
'breast_cancer2': 'breast_cancer2_diagnosis_age',
'ovarian_cancer': 'ovarian_cancer_diagnosis_age',
'prostate_cancer': 'prostate_cancer_diagnosis_age',
'pancreatic_cancer': 'pancreatic_cancer_diagnosis_age'
};
io.genetic_test = ['brca1', 'brca2', 'palb2', 'atm', 'chek2', 'rad51d', 'rad51c', 'brip1'];
io.pathology_tests = ['er', 'pr', 'her2', 'ck14', 'ck56'];
// get breast and ovarian PRS values
io.get_prs_values = function () {
var prs = {};
if (io.hasInput("breast_prs_a") && io.hasInput("breast_prs_z")) {
prs['breast_cancer_prs'] = {
'alpha': parseFloat($('#breast_prs_a').val()),
'zscore': parseFloat($('#breast_prs_z').val()),
'percent': parseFloat($('#breast_prs_percent').val())
};
}
if (io.hasInput("ovarian_prs_a") && io.hasInput("ovarian_prs_z")) {
prs['ovarian_cancer_prs'] = {
'alpha': parseFloat($('#ovarian_prs_a').val()),
'zscore': parseFloat($('#ovarian_prs_z').val()),
'percent': parseFloat($('#ovarian_prs_percent').val())
};
}
console.log(prs);
return (isEmpty(prs) ? 0 : prs);
}
// check if input has a value
io.hasInput = function (id) {
return $.trim($('#' + id).val()).length !== 0;
}
// return true if the object is empty
var isEmpty = function (myObj) {
for (var key in myObj) {
if (myObj.hasOwnProperty(key)) {
return false;
}
}
return true;
}
io.get_surgical_ops = function () {
var meta = "";
if (!$('#A6_4_3_check').parent().hasClass("off")) {
meta += ";OVARY2=y";
}
if (!$('#A6_4_7_check').parent().hasClass("off")) {
meta += ";MAST2=y";
}
return meta;
};
io.add = function (opts) {
$('#load').change(function (e) {
io.load(e, opts);
});
$('#save').click(function (e) {
io.save(opts, "pedigree.json");
});
$('#save_canrisk').click(function (e) {
var meta = io.get_surgical_ops();
try {
var prs = io.get_prs_values();
if (prs.breast_cancer_prs && prs.breast_cancer_prs.alpha !== 0 && prs.breast_cancer_prs.zscore !== 0) {
meta += "\n##PRS_BC=alpha=" + prs.breast_cancer_prs.alpha + ",zscore=" + prs.breast_cancer_prs.zscore;
}
if (prs.ovarian_cancer_prs && prs.ovarian_cancer_prs.alpha !== 0 && prs.ovarian_cancer_prs.zscore !== 0) {
meta += "\n##PRS_OC=alpha=" + prs.ovarian_cancer_prs.alpha + ",zscore=" + prs.ovarian_cancer_prs.zscore;
}
} catch (err) { console.warn("PRS", prs); }
io.save_canrisk(opts, meta);
});
$('#saveBOADICEA').click(function (e) {
io.save_boadicea(opts);
});
$('#print').click(function (e) {
io.print(io.get_printable_svg(opts));
});
$('#svg_download').click(function (e) {
io.svg_download(io.get_printable_svg(opts));
});
$('#png_download').click(function (e) {
var deferred = io.svg2png($('svg'), "pedigree", utils.isEdge() || utils.isIE());
$.when.apply($, [deferred]).done(function () {
var obj = getByName(arguments, "pedigree");
if (utils.isEdge() || utils.isIE()) {
var html = "<img src='" + obj.img + "' alt='canvas image'/>";
var newTab = window.open();
newTab.document.write(html);
} else {
var a = document.createElement('a');
a.href = obj.img;
a.download = 'plot.png';
a.target = '_blank';
document.body.appendChild(a); a.click(); document.body.removeChild(a);
}
});
});
};
/**
* Get object from array by the name attribute.
*/
function getByName(arr, name) {
return $.grep(arr, function (o) { return o && o.name == name; })[0];
}
/**
* Given a SVG document element convert to PNG.
*/
io.svg2png = function (svg, deferred_name, iscanvg) {
var deferred = $.Deferred();
var svgStr;
if (typeof window.XMLSerializer != "undefined") {
svgStr = (new XMLSerializer()).serializeToString(svg.get(0));
} else if (typeof svg.xml != "undefined") {
svgStr = svg.get(0).xml;
}
var imgsrc = 'data:image/svg+xml;base64,' + btoa(unescape(encodeURIComponent(svgStr))); // convert SVG string to data URL
var canvas = document.createElement("canvas");
var context = canvas.getContext("2d");
canvas.width = svg.width();
canvas.height = svg.height();
var img = document.createElement("img");
img.onload = function () {
if (utils.isIE() || iscanvg) {
// change font so it isn't tiny
svgStr = svgStr.replace(/ font-size="\d?.\d*em"/g, '');
svgStr = svgStr.replace(/<text /g, '<text font-size="13px" ');
canvg(canvas, svgStr, {
scaleWidth: svg.width(),
scaleHeight: svg.height(),
ignoreDimensions: true
});
console.log(deferred_name, "use canvg to create PNG");
} else {
context.clearRect(0, 0, svg.width(), svg.height());
context.drawImage(img, 0, 0, svg.width(), svg.height());
}
deferred.resolve({ 'name': deferred_name, 'img': canvas.toDataURL("image/png"), 'w': svg.width(), 'h': svg.height() });
};
img.src = imgsrc;
return deferred.promise();
}
// return a copy pedigree svg
io.copy_svg_html = function (opts) {
var svg_html = io.get_printable_svg(opts).html();
return io.copy_svg(svg_html);
}
// return a copy of svg html with unique url references (e.g. for clippath)
io.copy_svg = function (svg_html) {
// find all url's to make unique
var myRegexp = /url\(("|"|'){0,1}\#(.*?)("|"|'){0,1}\)/g;
var matches = [];
var match;
var c = 0;
myRegexp.lastIndex = 0;
while (match = myRegexp.exec(svg_html)) {
c++;
if (c > 800) {
console.error("io.copy_svg_html: counter exceeded 800");
return "ERROR DISPLAYING PEDIGREE";
}
matches.push(match);
if (myRegexp.lastIndex === match.index) {
myRegexp.lastIndex++;
}
}
for (var i = 0; i < matches.length; i++) {
var quote = (matches[i][1] ? matches[i][1] : "");
var val = matches[i][2];
var m1 = "id=\"" + val + "\"";
var m2 = "url\\(" + quote + "\#" + val + quote + "\\)";
var newval = val + ptree.makeid(2);
svg_html = svg_html.replace(new RegExp(m1, 'g'), "id=\"" + newval + "\"");
svg_html = svg_html.replace(new RegExp(m2, 'g'), "url(#" + newval + ")");
}
return svg_html;
};
// get printable svg div, adjust size to tree dimensions and scale to fit
io.get_printable_svg = function (opts) {
var local_dataset = pedcache.current(opts); // get current dataset
if (local_dataset !== undefined && local_dataset !== null) {
opts.dataset = local_dataset;
}
var tree_dimensions = ptree.get_tree_dimensions(opts);
var svg_div = $('#' + opts.targetDiv).find('svg').parent();
if (opts.width < tree_dimensions.width || opts.height < tree_dimensions.height ||
tree_dimensions.width > 595 || tree_dimensions.height > 842) {
var wid = tree_dimensions.width;
var hgt = tree_dimensions.height + 100;
var scale = 1.0;
if (tree_dimensions.width > 595 || tree_dimensions.height > 842) { // scale to fit A4
if (tree_dimensions.width > 595) wid = 595;
if (tree_dimensions.height > 842) hgt = 842;
var xscale = wid / tree_dimensions.width;
var yscale = hgt / tree_dimensions.height;
scale = (xscale < yscale ? xscale : yscale);
}
svg_div = $('<div></div>'); // create a new div
svg_div.append($('#' + opts.targetDiv).find('svg').parent().html()); // copy svg html to new div
var svg = svg_div.find("svg");
svg.attr('width', wid); // adjust dimensions
svg.attr('height', hgt);
var ytransform = (-opts.symbol_size * 1.5 * scale);
svg.find(".diagram").attr("transform", "translate(0, " + ytransform + ") scale(" + scale + ")");
}
return svg_div;
};
// download the SVG to a file
io.svg_download = function (svg) {
var a = document.createElement('a');
a.href = 'data:image/svg+xml;base64,' + btoa(unescape(encodeURIComponent(svg.html())));
a.download = 'plot.svg';
a.target = '_blank';
document.body.appendChild(a); a.click(); document.body.removeChild(a);
};
// open print window for a given element
io.print = function (el, id) {
if (el.constructor !== Array)
el = [el];
var width = $(window).width() * 0.9;
var height = $(window).height() - 10;
var cssFiles = [
'/static/css/canrisk.css',
'https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css'
];
var printWindow = window.open('', 'PrintMap', 'width=' + width + ',height=' + height);
var headContent = '';
for (var i = 0; i < cssFiles.length; i++)
headContent += '<link href="' + cssFiles[i] + '" rel="stylesheet" type="text/css" media="all">';
headContent += "<style>body {font-size: " + $("body").css('font-size') + ";}</style>";
/*var headContent2 = '';
var links = document.getElementsByTagName('link');
for(var i=0;i<links.length; i++) {
var html = links[i].outerHTML;
if(html.indexOf('href="http') !== -1)
headContent2 += html;
}
headContent2 += html;
var scripts = document.getElementsByTagName('script');
for(var i=0;i<scripts.length; i++) {
var html = scripts[i].outerHTML;
if(html.indexOf('src="http') !== -1)
headContent += html;
}*/
html = "";
for (i = 0; i < el.length; i++) {
if (i === 0 && id)
html += id;
html += $(el[i]).html();
if (i < el.length - 1)
html += '<div style="page-break-before:always"> </div>';
}
printWindow.document.write(headContent);
printWindow.document.write(html);
printWindow.document.close();
printWindow.focus();
setTimeout(function () {
printWindow.print();
printWindow.close();
}, 300);
};
// save content to a file
io.save_file = function (opts, content, filename, type) {
if (opts.DEBUG)
console.log(content);
if (!filename) filename = "ped.txt";
if (!type) type = "text/plain";
var file = new Blob([content], { type: type });
if (window.navigator.msSaveOrOpenBlob) // IE10+
window.navigator.msSaveOrOpenBlob(file, filename);
else { // other browsers
var a = document.createElement("a");
var url = URL.createObjectURL(file);
a.href = url;
a.download = filename;
document.body.appendChild(a);
a.click();
setTimeout(function () {
document.body.removeChild(a);
window.URL.revokeObjectURL(url);
}, 0);
}
}
io.save = function (opts, filename) {
if (!filename) filename = "ped.txt";
var content = JSON.stringify(pedcache.current(opts));
io.save_file(opts, content, filename);
};
io.save_canrisk = function (opts, meta) {
io.save_file(opts, run_prediction.get_non_anon_pedigree(pedcache.current(opts), meta), "canrisk.txt");
};
io.save_boadicea = function (opts) {
var diseases_valid = true;
var dis_names = $.map(opts.diseases, function (val, i) { return val.type; });
for (var i = 0; i < DEFAULT_DISEASES.length; i++) {
if ($.inArray(DEFAULT_DISEASES[i].type, dis_names) == -1) {
diseases_valid = false;
}
}
currentOpts = pedcache.current(opts);
var targetDefined = false;
for (var i = 0; i < currentOpts.length; i++) {
var individual = currentOpts[i];
targetDefined = targetDefined || individual['proband']
}
if (!diseases_valid) {
$('#boadicea-error').dialog({
autoOpen: false,
title: 'Boadicea Format Error',
height: 200,
width: 400,
dialogClass: 'boadicea-error-dialog',
buttons: {
Ok: function () {
io.boadicea_to_file(opts);
$(this).dialog("close");
},
Cancel: function () {
$(this).dialog("close");
}
}
});
$('#boadicea-error').html("<p>It appears that not all diseases required for BOADICEA are present in" +
" the disease configuration.</p><br><br><p>Proceed anyway?</p>");
$('#boadicea-error').dialog('open');
} if (!targetDefined) {
$('#boadicea-error').dialog({
autoOpen: false,
title: 'Boadicea Format Error',
height: 200,
width: 400,
dialogClass: 'boadicea-error-dialog',
buttons: {
Ok: function () {
$(this).dialog("close");
}
}
});
$('#boadicea-error').html("<p>There is currently no proband defined. Please make sure that the pedigree features a proband for the risk calcualtion.</p>");
$('#boadicea-error').dialog('open');
} else {
io.boadicea_to_file(opts);
}
}
io.boadicea_to_file = function (opts) {
var filetext = "BOADICEA import pedigree file format 4.0\n";
filetext += "FamID\tName\tTarget\tIndivID\tFathID\tMothID\tSex\tMZtwin\tDead\tAge\tYob" +
"\t1stBrCa\t2ndBrCa\tOvCa\tProCa\tPanCa" +
"\tAshkn" +
"\tBRCA1t\tBRCA1r\tBRCA2t\tBRCA2r\tPALB2t\tPALB2r\tATMt\tATMr\tCHEK2t\tCHEK2r" +
"\tER\tPR\tHER2\tCK14\tCK56\n";
currentOpts = pedcache.current(opts);
var formatError = false;
for (var i = 0; i < currentOpts.length; i++) {
var nextline = "";
var individual = currentOpts[i];
// the ids of the possible information in the dataset (not everything gets read when loading a boadicea file)
// 17 - 21 are dictionaries with [type] and [result]
var infoID = ['famid', 'display_name', 'proband', 'name', 'father', 'mother',
'sex', 'mztwin', 'status', 'age', 'yob', 'breast_cancer_diagnosis_age',
'breast_cancer2_diagnosis_age', 'ovarian_cancer_diagnosis_age',
'prostate_cancer_diagnosis_age', 'pancreatic_cancer_diagnosis_age',
'ashkenazi', 'brca1_gene_test', 'brca2_gene_test', 'palb2_gene_test', 'atm_gene_test', 'chek2_gene_test',
'er_bc_pathology', 'pr_bc_pathology', 'her2_bc_pathology', 'ck14_bc_pathology', 'ck56_bc_pathology'];
for (var j = 0; j < infoID.length; j++) {
if (individual[infoID[j]] && individual[infoID[j]] !== "") {
if (j == 2) { // checks if individual is proband (saved as true or false, thus no extra checkup needed)
nextline += "1\t";
} else if (j==4 && individual['noparents'] && individual['noparents']==true) {
nextline += "0\t";
} else if (j==5 && individual['noparents'] && individual['noparents']==true) {
nextline += "0\t";
} else if (j == 6 && individual[infoID[j]] == 'U') { // prevents Default Gender 'Unknown'
formatError = true;
break;
} else if (j > 16 && j < 22) { // saves the genetests
if (individual[infoID[j]]['type'] == "" || individual[infoID[j]]['result'] == "") {
nextline += "0\t0\t";
} else {
nextline += individual[infoID[j]]['type'] + "\t" + individual[infoID[j]]['result'] + "\t";
}
} else { // copies remaining values
nextline += individual[infoID[j]] + "\t";
}
} else { // everything that wasn't selected/configured gets default value '0'
if (j > 16 && j < 22) {
nextline += "0\t0\t";
} else {
nextline += "0\t";
}
}
}
filetext += nextline + "\n";
}
if (formatError) {
$('#boadicea-error').dialog({
autoOpen: false,
title: 'Boadicea Format Error',
height: 175,
width: 400,
dialogClass: 'boadicea-error-dialog',
buttons: {
Ok: function () {
$(this).dialog("close");
}
}
});
$('#boadicea-error').html("<p>The BOADICEA format requires a Gender that's either male or female. Please check for any unknown genders and try again.</p>");
$('#boadicea-error').dialog('open');
} else {
io.save_file(opts, filetext, 'bodicea_ped.tsv');
}
}
io.canrisk_validation = function (opts) {
$.each(opts.dataset, function (idx, p) {
if (!p.hidden && p.sex === 'M' && !pedigree_util.isProband(p)) {
if (p[io.cancers['breast_cancer2']]) {
var msg = 'Male family member (' + p.display_name + ') with contralateral breast cancer found. ' +
'Please note that as the risk models do not take this into account the second ' +
'breast cancer is ignored.'
console.error(msg);
delete p[io.cancers['breast_cancer2']];
utils.messages("Warning", msg);
}
}
});
}
io.setDiseases = function (opts) {
var diseases = [];
// get the diseases
$.each(opts.dataset, function (k, v) {
diseases = $.merge(diseases, $.map(Object.keys(this), function (val, i) {
if (val.endsWith("_diagnosis_age"))
return val.replace("_diagnosis_age", "");
}))
});
// make correct format
var disease_list = $.map([...new Set(diseases)], function (val, i) {
return { 'type': val, 'colour': 'darkgrey' };
});
// check against defaults to split in default and additional
var def_dis = $.map(DEFAULT_DISEASES, function (val, i) { return val.type; });
opts.diseases = [];
opts.additional_diseases = [];
$.each(disease_list, function (k, v) {
if (def_dis.includes(v.type)) {
opts.diseases.push(v);
} else {
opts.additional_diseases.push(v);
}
});
localStorage.setItem('diseases', JSON.stringify(opts.diseases));
localStorage.setItem('additional_diseases', JSON.stringify(opts.additional_diseases));
}
io.load = function (e, opts) {
var f = e.target.files[0];
if (f) {
var reader = new FileReader();
reader.onload = function (e) {
if (opts.DEBUG)
console.log(e.target.result);
try {
if (e.target.result.startsWith("BOADICEA import pedigree file format 4.0")) {
opts.dataset = io.readBoadiceaV4(opts, e.target.result, 4);
io.canrisk_validation(opts);
} else if (e.target.result.startsWith("BOADICEA import pedigree file format 2.0")) {
opts.dataset = io.readBoadiceaV4(opts, e.target.result, 2);
io.canrisk_validation(opts);
} else if (e.target.result.startsWith("##") && e.target.result.indexOf("CanRisk") !== -1) {
var canrisk_data = io.readCanRiskV1(e.target.result);
var risk_factors = canrisk_data[0];
opts.dataset = canrisk_data[1];
io.canrisk_validation(opts);
} else {
try {
opts.dataset = JSON.parse(e.target.result);
io.setDiseases(opts);
} catch (err) {
opts.dataset = io.readLinkage(e.target.result);
}
}
ptree.validate_pedigree(opts);
} catch (err1) {
console.error(err1, e.target.result);
utils.messages("File Error", (err1.message ? err1.message : err1));
return;
}
if (opts.DEBUG)
console.log(opts.dataset);
try {
ptree.rebuild(opts);
if (risk_factors !== undefined) {
console.log(risk_factors);
// load risk factors - fire riskfactorChange event
$(document).trigger('riskfactorChange', [opts, risk_factors]);
}
$(document).trigger('fhChange', [opts]); // trigger fhChange event
try {
// update FH section
acc_FamHist_ticked();
acc_FamHist_Leave();
RESULT.FLAG_FAMILY_MODAL = true;
} catch (err3) { }
} catch (err2) {
utils.messages("File Error", (err2.message ? err2.message : err2));
}
};
reader.onerror = function (event) {
utils.messages("File Error", "File could not be read! Code " + event.target.error.code);
};
reader.readAsText(f);
} else {
console.error("File could not be read!");
}
$("#load")[0].value = ''; // reset value
};
//
// https://www.cog-genomics.org/plink/1.9/formats#ped
// https://www.cog-genomics.org/plink/1.9/formats#fam
// 1. Family ID ('FID')
// 2. Within-family ID ('IID'; cannot be '0')
// 3. Within-family ID of father ('0' if father isn't in dataset)
// 4. Within-family ID of mother ('0' if mother isn't in dataset)
// 5. Sex code ('1' = male, '2' = female, '0' = unknown)
// 6. Phenotype value ('1' = control, '2' = case, '-9'/'0'/non-numeric = missing data if case/control)
// 7. Genotypes (column 7 onwards);
// columns 7 & 8 are allele calls for first variant ('0' = no call); colummns 9 & 10 are calls for second variant etc.
io.readLinkage = function (boadicea_lines) {
var lines = boadicea_lines.trim().split('\n');
var ped = [];
var famid;
for (var i = 0; i < lines.length; i++) {
var attr = $.map(lines[i].trim().split(/\s+/), function (val, i) { return val.trim(); });
if (attr.length < 5)
throw ('unknown format');
var sex = (attr[4] == '1' ? 'M' : (attr[4] == '2' ? 'F' : 'U'));
var indi = {
'famid': attr[0],
'display_name': attr[1],
'name': attr[1],
'sex': sex
};
if (attr[2] !== "0") indi.father = attr[2];
if (attr[3] !== "0") indi.mother = attr[3];
if (typeof famid != 'undefined' && famid !== indi.famid) {
console.error('multiple family IDs found only using famid = ' + famid);
break;
}
if (attr[5] == "2") indi.affected = 2;
// add genotype columns
if (attr.length > 6) {
indi.alleles = "";
for (var j = 6; j < attr.length; j += 2) {
indi.alleles += attr[j] + "/" + attr[j + 1] + ";";
}
}
ped.unshift(indi);
famid = attr[0];
}
return process_ped(ped);
};
io.readCanRiskV1 = function (boadicea_lines) {
var lines = boadicea_lines.trim().split('\n');
var ped = [];
var hdr = []; // collect risk factor header lines
// assumes two line header
for (var i = 0; i < lines.length; i++) {
if (lines[i].startsWith("##")) {
if (lines[i].startsWith("##CanRisk") && lines[i].indexOf(";") > -1) { // contains surgical op data
var ops = lines[i].split(";");
for (var j = 1; j < ops.length; j++) {
var opdata = ops[j].split("=");
if (opdata.length === 2) {
hdr.push(ops[j]);
}
}
}
if (lines[i].indexOf("CanRisk") === -1 && !lines[i].startsWith("##FamID")) {
hdr.push(lines[i].replace("##", ""));
}
continue;
}
var delim = /\t/;
if (lines[i].indexOf('\t') < 0) {
delim = /\s+/;
console.log("NOT TAB DELIM");
}
var attr = $.map(lines[i].trim().split(delim), function (val, i) { return val.trim(); });
if (attr.length > 1) {
var indi = {
'famid': attr[0],
'display_name': attr[1],
'name': attr[3],
'sex': attr[6],
'status': attr[8]
};
if (attr[2] == 1) indi.proband = true;
if (attr[4] !== "0") indi.father = attr[4];
if (attr[5] !== "0") indi.mother = attr[5];
if (attr[7] !== "0") indi.mztwin = attr[7];
if (attr[9] !== "0") indi.age = attr[9];
if (attr[10] !== "0") indi.yob = attr[10];
var idx = 11;
$.each(io.cancers, function (cancer, diagnosis_age) {
// Age at 1st cancer or 0 = unaffected, AU = unknown age at diagnosis (affected unknown)
if (attr[idx] !== "0") {
indi[diagnosis_age] = attr[idx];
}
idx++;
});
if (attr[idx++] !== "0") indi.ashkenazi = 1;
// BRCA1, BRCA2, PALB2, ATM, CHEK2, .... genetic tests
// genetic test type, 0 = untested, S = mutation search, T = direct gene test
// genetic test result, 0 = untested, P = positive, N = negative
for (var j = 0; j < io.genetic_test.length; j++) {
var gene_test = attr[idx].split(":");
if (gene_test[0] !== '0') {
if ((gene_test[0] === 'S' || gene_test[0] === 'T') && (gene_test[1] === 'P' || gene_test[1] === 'N'))
indi[io.genetic_test[j] + '_gene_test'] = { 'type': gene_test[0], 'result': gene_test[1] };
else
console.warn('UNRECOGNISED GENE TEST ON LINE ' + (i + 1) + ": " + gene_test[0] + " " + gene_test[1]);
}
idx++;
}
// status, 0 = unspecified, N = negative, P = positive
var path_test = attr[idx].split(":");
for (j = 0; j < path_test.length; j++) {
if (path_test[j] !== '0') {
if (path_test[j] === 'N' || path_test[j] === 'P')
indi[io.pathology_tests[j] + '_bc_pathology'] = path_test[j];
else
console.warn('UNRECOGNISED PATHOLOGY ON LINE ' + (i + 1) + ": " + io.pathology_tests[j] + " " + path_test[j]);
}
}
ped.unshift(indi);
}
}
try {
return [hdr, process_ped(ped)];
} catch (e) {
console.error(e);
return [hdr, ped];
}
};
// read boadicea format v4 & v2
io.readBoadiceaV4 = function (opts, boadicea_lines, version) {
// set diseases accordingly
opts.diseases = $.extend(true, [], DEFAULT_DISEASES);
opts.additional_diseases = [];
localStorage.setItem('diseases', JSON.stringify(opts.diseases));
localStorage.setItem('additional_diseases', JSON.stringify(opts.additional_diseases));
var lines = boadicea_lines.trim().split('\n');
var ped = [];
// assumes two line header
for (var i = 2; i < lines.length; i++) {
var attr = $.map(lines[i].trim().split(/\s+/), function (val, i) { return val.trim(); });
if (attr.length > 1) {
var indi = {
'famid': attr[0],
'display_name': attr[1],
'name': attr[3],
'sex': attr[6],
'status': attr[8]
};
if (attr[2] == 1) indi.proband = true;
if (attr[4] !== "0") indi.father = attr[4];
if (attr[5] !== "0") indi.mother = attr[5];
if (attr[7] !== "0") indi.mztwin = attr[7];
if (attr[9] !== "0") indi.age = attr[9];
if (attr[10] !== "0") indi.yob = attr[10];
var idx = 11;
$.each(io.cancers, function (cancer, diagnosis_age) {
// Age at 1st cancer or 0 = unaffected, AU = unknown age at diagnosis (affected unknown)
if (attr[idx] !== "0") {
indi[diagnosis_age] = attr[idx];
}
idx++;
});
if (version === 4) {
if (attr[idx++] !== "0") indi.ashkenazi = 1;
// BRCA1, BRCA2, PALB2, ATM, CHEK2 genetic tests
// genetic test type, 0 = untested, S = mutation search, T = direct gene test
// genetic test result, 0 = untested, P = positive, N = negative
for (var j = 0; j < 5; j++) {
idx += 2;
if (attr[idx - 2] !== '0') {
if ((attr[idx - 2] === 'S' || attr[idx - 2] === 'T') && (attr[idx - 1] === 'P' || attr[idx - 1] === 'N'))
indi[io.genetic_test[j] + '_gene_test'] = { 'type': attr[idx - 2], 'result': attr[idx - 1] };
else
console.warn('UNRECOGNISED GENE TEST ON LINE ' + (i + 1) + ": " + attr[idx - 2] + " " + attr[idx - 1]);
}
}
} else if (version === 2) {
// genetic test BRCA1, BRCA2
// type, 0 = untested, S = mutation search, T = direct gene test
// result, 0 = untested, N = no mutation, 1 = BRCA1 positive, 2 = BRCA2 positive, 3 = BRCA1/2 positive
idx += 2; // gtest
if (attr[idx - 2] !== '0') {
if ((attr[idx - 2] === 'S' || attr[idx - 2] === 'T')) {
if (attr[idx - 1] === 'N') {
indi['brca1_gene_test'] = { 'type': attr[idx - 2], 'result': 'N' };
indi['brca2_gene_test'] = { 'type': attr[idx - 2], 'result': 'N' };
} else if (attr[idx - 1] === '1') {
indi['brca1_gene_test'] = { 'type': attr[idx - 2], 'result': 'P' };
indi['brca2_gene_test'] = { 'type': attr[idx - 2], 'result': 'N' };
} else if (attr[idx - 1] === '2') {
indi['brca1_gene_test'] = { 'type': attr[idx - 2], 'result': 'N' };
indi['brca2_gene_test'] = { 'type': attr[idx - 2], 'result': 'P' };
} else if (attr[idx - 1] === '3') {
indi['brca1_gene_test'] = { 'type': attr[idx - 2], 'result': 'P' };
indi['brca2_gene_test'] = { 'type': attr[idx - 2], 'result': 'P' };
}
} else {
console.warn('UNRECOGNISED GENE TEST ON LINE ' + (i + 1) + ": " + attr[idx - 2] + " " + attr[idx - 1]);
}
}
if (attr[idx++] !== "0") indi.ashkenazi = 1;
}
// status, 0 = unspecified, N = negative, P = positive
for (j = 0; j < io.pathology_tests.length; j++) {
if (attr[idx] !== '0') {
if (attr[idx] === 'N' || attr[idx] === 'P')
indi[io.pathology_tests[j] + '_bc_pathology'] = attr[idx];
else
console.warn('UNRECOGNISED PATHOLOGY ON LINE ' + (i + 1) + ": " + io.pathology_tests[j] + " " + attr[idx]);
}
idx++;
}
ped.unshift(indi);
}
}
try {
return process_ped(ped);
} catch (e) {
console.error(e);
return ped;
}
};
function process_ped(ped) {
// find the level of individuals in the pedigree
for (var j = 0; j < 2; j++) {
for (var i = 0; i < ped.length; i++) {
getLevel(ped, ped[i].name);
}
}
// find the max level (i.e. top_level)
var max_level = 0;
for (i = 0; i < ped.length; i++) {
if (ped[i].level && ped[i].level > max_level)
max_level = ped[i].level;
}
// identify top_level and other nodes without parents
for (i = 0; i < ped.length; i++) {
if (pedigree_util.getDepth(ped, ped[i].name) == 1) {
if (ped[i].level && ped[i].level == max_level) {
ped[i].top_level = true;
} else {
ped[i].noparents = true;
// 1. look for partners parents
var pidx = getPartnerIdx(ped, ped[i]);
if (pidx > -1) {
if (ped[pidx].mother) {
ped[i].mother = ped[pidx].mother;
ped[i].father = ped[pidx].father;
}
}
// 2. or adopt parents from level above
if (!ped[i].mother) {
for (var j = 0; j < ped.length; j++) {
if (ped[i].level == (ped[j].level - 1)) {
pidx = getPartnerIdx(ped, ped[j]);
if (pidx > -1) {
ped[i].mother = (ped[j].sex === 'F' ? ped[j].name : ped[pidx].name);
ped[i].father = (ped[j].sex === 'M' ? ped[j].name : ped[pidx].name);
}
}
}
}
}
} else {
delete ped[i].top_level;
}
}
return ped;
}
// get the partners for a given node
function getPartnerIdx(dataset, anode) {
var ptrs = [];
for (var i = 0; i < dataset.length; i++) {
var bnode = dataset[i];
if (anode.name === bnode.mother)
return pedigree_util.getIdxByName(dataset, bnode.father);
else if (anode.name === bnode.father)
return pedigree_util.getIdxByName(dataset, bnode.mother);
}
return -1;
}
// for a given individual assign levels to a parents ancestors
function getLevel(dataset, name) {
var idx = pedigree_util.getIdxByName(dataset, name);
var level = (dataset[idx].level ? dataset[idx].level : 0);
update_parents_level(idx, level, dataset);
}
// recursively update parents levels
function update_parents_level(idx, level, dataset) {
var parents = ['mother', 'father'];
level++;
for (var i = 0; i < parents.length; i++) {
var pidx = pedigree_util.getIdxByName(dataset, dataset[idx][parents[i]]);
if (pidx >= 0) {
var ma = dataset[pedigree_util.getIdxByName(dataset, dataset[idx].mother)];
var pa = dataset[pedigree_util.getIdxByName(dataset, dataset[idx].father)];
if (!dataset[pidx].level || dataset[pidx].level < level) {
ma.level = level;
pa.level = level;
}
if (ma.level < pa.level) {
ma.level = pa.level;
} else if (pa.level < ma.level) {
pa.level = ma.level;
}
update_parents_level(pidx, level, dataset);
}
}
}
}(window.io = window.io || {}, jQuery));