-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser.js
481 lines (328 loc) · 12.5 KB
/
parser.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
var isNotFullScreen = true;
$(function() {
var element = "#graphics";
fileName = "";
$("#uploadLink").on("click", function(event) {
toggleFullScreen(); //disable for testing
//#uploadLink is a dummy element used to activate the hidden #uploadInput element
event.preventDefault();
$("#uploadInput").trigger("click");
// toggleFullScreen(); //disable for testing
});
$("#fullscreenLink").on("click", function() {
// toggleFullScreen();
});
$("#uploadInput").change(function() { //code called by $("#uploadInput").trigger("click");
var file = $("#uploadInput")[0].files[0]; //the file uploaded by the user
window.variantFilename = file.name;
// toggleFullScreen();
if (validateXLSX(file)) {
showSpinner();
parseXLS(file);
} else {
console.log("invalid input");
// showError();
}
});
});
function validateXLSX(file) {
var extension = file.name.split(".").slice(-1)[0];
return extension == "xlsx" || extension == "xls";
}
function parseXLS(xls) {
var reader = new FileReader();
reader.onload = function(e) {
var data = e.target.result;
var arr = fixdata(data);
var workbook = XLSX.read(btoa(arr), {type: "base64"});
var sheetNames = workbook.SheetNames;
//just parses first sheet
var sheet = XLSX.utils.sheet_to_json(workbook.Sheets[sheetNames[0]]);
parseSheet(sheet);
};
reader.readAsArrayBuffer(xls);
}
//parses sheetJS's export of an xls row to a JS object
function parseSheet(sheet) {
var visualizationData = [];
var columns = [
//basic information
"Chromosome",
"Position",
"Reference Allele",
"Sample Allele",
"Variation Type",
"QUAL",
"FILTER",
"GT", // "proband|dad|mom"
//gene info
"Gene Region",
"Gene Symbol",
"Transcript ID",
"Transcript Variant",
"Protein Variant",
"Translation Impact", // < missense, frameshift, stop loss, stop gain, ...
//model scores
"SIFT Function Prediction",
"PolyPhen-2 Function Prediction",
"MutationTaster",
"CADD Score",
"phyloP",
"fathmm",
// "Sift",
//frequencies:: LOTS OF MISSING CELLS
"1000 Genomes Frequency",
"ExAC Frequency",
"ExAC East Asian Frequency",
"ExAC South Asian Frequency",
"ExAC African Frequency",
"ExAC European Frequency",
"ExAC Latino Frequency",
"AF_EAS",
"AF_NFE",
"AF_SAS",
"AF_AMR",
"AF_AFR",
"AN_EAS",
"AN_NFE",
"AN_SAS",
"AN_AMR",
"AN_AFR",
"GNOMADMaxAlleleFreq",
"GNOMAD_Max_Allele_Freq_POP"
];
for (i in sheet) {
var row = sheet[i];
var variant = {
"core" : {},
"metadata" : {
"notes" : "",
"isDeleted" : false,
"flags" : []
}
};
function fillCore(originalValue, column) {
var pVData = parseValue(originalValue, column);
var value = pVData[0];
var displayName = pVData[1];
var isMissing = pVData[2];
return {
"value" : value,
"originalValue" : originalValue,
"displayName" : displayName,
"isMissing" : isMissing
};
}
$.each(columns, (_, column) => {
//make variant.core a dictionary where the keys are the column names and the values are the template returned by filledTemplate
variant.core[column] = fillCore(row[column], column);
});
visualizationData.push(variant);
}
window.variantData = visualizationData;
window.variantIndex = 0;
console.log(visualizationData);
renderVisualization();
}
//given the original value and the column returns a drawing value
function parseValue(originalValue, column) {
//returns [(final) value, displayName (for annotation), isMissing];
//model scores
//all normalized to [0, 1], where 0 is least pathogenic and 1 is most pathogenic
var modelScores = ["SIFT Function Prediction","PolyPhen-2 Function Prediction","CADD Score","phyloP","MutationTaster","fathmm","Sift"];
if (column == "SIFT Function Prediction") {
var displayName = "SIFT Function Prediction";
var lookup = {"Tolerated" : 0, "Activating": .5, "Damaging" : 1};
if (!(originalValue in lookup)) {
return [0, displayName, true];
}
var value = stringToNumber(originalValue, lookup, column);
return [scaleValue(value, column), displayName, false];
} else if (column == "PolyPhen-2 Function Prediction") {
var displayName = "PolyPhen-2 Function Prediction";
var lookup = {"Benign" : 0, "Probably Damaging" : 1};
if (!(originalValue in lookup)) {
return [0, displayName, true];
}
var value = stringToNumber(originalValue, lookup, column);
return [scaleValue(value, column), displayName, false];
} else if (column == "CADD Score") { //TODO
var displayName = "CADD Score";
//PHRED-like (-10*log10(rank/total)) scaled C-score ranking a variant relative to all possible substitutions of the human genome (8.6x10^9)
//A scaled C-score of greater or equal 10 indicates that these are predicted to be the 10% most deleterious substitutions that you can do to the human genome,
//a score of greater or equal 20 indicates the 1% most deleterious and so on.
//http://cadd.gs.washington.edu/info
var originalDomain = [1, 99];
if (isNaN(originalValue)) {
return [0, displayName, true];
}
var parsedValue = parseFloat(originalValue);
if (parsedValue < originalDomain[0] || parsedValue > originalDomain[1]) {
return [0, displayName, true];
}
//but really we only see scores in [1,28]
//clip to [1, 28]
if (parsedValue > 28) {
parsedValue = 28;
}
var normalizedValue = zeroOneNormalizeValue(parsedValue, [1, 28], column, false);
return [scaleValue(normalizedValue, column), displayName, false];
} else if (column == "phyloP") {
//we use the mammalian phylop rankscore
//a rank score is always between 0 and 1 and a score of 0.9 means it is more likely to be damaging than 90% of all potential nsSNVs predicted by that method https://www.ncbi.nlm.nih.gov/pmc/articles/PMC4752381/
var displayName = "phyloP";
var originalDomain = [0, 1];
if (isNaN(originalValue)) {
return [0, displayName, true];
}
var parsedValue = parseFloat(originalValue);
if (parsedValue < originalDomain[0] || parsedValue > originalDomain[1]) {
return [0, displayName, true];
}
var normalizedValue = zeroOneNormalizeValue(parsedValue, originalDomain, column, false);
return [scaleValue(normalizedValue, column), displayName, false];
} else if (column == "MutationTaster") {
//we use mutation taster converted rankscore: 1 is more damaging, 0 is less damaging—see phyloP above for more details
var displayName = "MutationTaster";
var lookup = {"N" : 0, "P" : 0, "D" : 1, "A" : 1};
if (!(originalValue in lookup)) {
return [0, displayName, true];
}
var value = stringToNumber(originalValue, lookup, column);
return [scaleValue(value, column), displayName, false];
} else if (column == "fathmm") {
var displayName = "fathmm";
var originalDomain = [-16.13, 10.64];
if (isNaN(originalValue)) {
return [0, displayName, true];
}
var parsedValue = parseFloat(originalValue);
if (parsedValue < originalDomain[0] || parsedValue > originalDomain[1]) {
return [0, displayName, true];
}
//Positive FATHMM scores predict a tolerance to the variation while negative FATHMM scores predict intolerance to the variation, and is subsequently considered to be pathogenic. Following proof of concept analysis it was determined that the best possible cut-off value for the FATHMM score is 1.0 https://www.ncbi.nlm.nih.gov/pmc/articles/PMC4929716/
var normalizedValue = zeroOneNormalizeValue(parsedValue, originalDomain, column, true);
return [scaleValue(normalizedValue, column), displayName, false];
}
//frequencies
//should all be in [0, 1]
//primary: ExAC Frequency
//GNOMADMaxAlleleFreq
//population: ExAC East Asian Frequency, ExAC South Asian Frequency, ExAC African Frequency, ExAC European Frequency, ExAC Latino Frequency
//AF_EAS, AF_NFE, AF_SAS, AF_AMR, AF_AFR
var frequencies = ["1000 Genomes Frequency","ExAC Frequency","GNOMADMaxAlleleFreq","ExAC East Asian Frequency","ExAC South Asian Frequency","ExAC African Frequency","ExAC European Frequency","ExAC Latino Frequency","AF_EAS","AF_NFE","AF_SAS","AF_AMR","AF_AFR"];
var frequenciesDisplayNames = {
"1000 Genomes Frequency" : "1000 Genomes Frequency",
"ExAC Frequency" : "ExAC Frequency",
"GNOMADMaxAlleleFreq" : "gnomAD Max Frequency",
"ExAC East Asian Frequency" : "ExAC East Asian Frequency",
"ExAC South Asian Frequency" : "ExAC South Asian Frequency",
"ExAC African Frequency" : "ExAC African Frequency",
"ExAC European Frequency" : "ExAC European Frequency",
"ExAC Latino Frequency" : "ExAC Latino Frequency",
"AF_EAS" : "gnomAD East Asian Frequency",
"AF_NFE" : "gnomAD European (non-Finnish) Frequency",
"AF_SAS" : "gnomAD South Asian Frequency",
"AF_AMR" : "gnomAD Latino Frequency",
"AF_AFR" : "gnomAD African Frequency"
};
if ($.inArray(column, frequencies) !== -1) {
var displayName = frequenciesDisplayNames[column];
var originalDomain = [0, 1];
if (isNaN(originalValue)) {
return [0, displayName, true];
}
var parsedValue = parseFloat(originalValue);
if (parsedValue < originalDomain[0] || parsedValue > originalDomain[1]) {
return [0, displayName, true];
}
parsedValue = 1 - parsedValue; //for frequencies, 0 is more interesting (for model scores, 1 is more interesting)
return [scaleValue(parsedValue, column), displayName, false];
}
//miscellaneous strings
var strings = ["Chromosome","Reference Allele","Sample Allele","Variation Type","FILTER","GT","GNOMAD_Max_Allele_Freq_POP"];
var stringsDisplayNames = {
"Chromosome" : "Chromosome",
"Reference Allele" : "Reference Allele",
"Sample Allele" : "Sample Allele",
"Variation Type" : "Variant Type",
"FILTER" : "Filter",
"GT" : "Genotype",
"GNOMAD_Max_Allele_Freq_POP" : "gnomAD Max Frequency Population"
};
if ($.inArray(column, strings) !== -1) {
if (originalValue) {
return [originalValue, stringsDisplayNames[column], false];
} else {
return ["", stringsDisplayNames[column], true];
}
}
var ints = ["Position","QUAL","Gene Region","Gene Symbol","Transcript ID","Transcript Variant","Protein Variant","Translation Impact","AN_EAS","AN_NFE","AN_SAS","AN_AMR","AN_AFR"];
var intsDisplayNames = {
"Position" : "Position",
"QUAL" : "Quality",
"Gene Region" : "Gene Region",
"Gene Symbol" : "Gene Symbol",
"Transcript ID" : "Transcript ID",
"Transcript Variant" : "Transcript Variant",
"Protein Variant" : "Protein Variant",
"Translation Impact" : "Translation Impact",
"AN_EAS" : "gnomAD East Asian n",
"AN_NFE" : "gnomAD European (non-Finnish) n",
"AN_SAS" : "gnomAD South Asian n",
"AN_AMR" : "gnomAD Latino n",
"AN_AFR" : "gnomAD African n"
};
if ($.inArray(column, ints) !== -1) {
if (originalValue) {
return [originalValue, intsDisplayNames[column], false];
} else {
return [0, intsDisplayNames[column], true];
}
}
console.log("Not sure how to parse " + column);
}
//normalizes a value in the range 0-1
//takes as parameters: value, min possible value, max possible value, name or the value (for error reporting), boolean invert scale for when we want to flip the high and low values
//we want 1 to represent values that are of interest to us (pathogenic, rare etc). If a value of 1 means not pathogenic and zero means pathogenic, we invert scale. Similarly we invert the scale for frequencies so rare variants are most visualized
function zeroOneNormalizeValue(value, domain, column, shouldInvert) {
if (shouldInvert) {
var distanceFromBottom = Math.abs(value - domain[0]);
value = domain[1] - distanceFromBottom;
}
var normalizer = d3.scaleLinear()
.domain(domain)
.range([0, 1]);
return normalizer(value);
}
function stringToNumber(value, lookup, column) {
return lookup[value];
}
function scaleValue(value, column) {
if (value < 0 || value > 1) {
console.log("error normalizing: " + value + " from " + column);
}
var exponent = 1;
if (column == "GNOMADMaxAlleleFreq") {
exponent = 5000;
} else if (column == "ExAC Frequency") {
exponent = 100;
}
var scale = d3.scalePow()
.exponent(exponent) //************
.domain([0, 1])
.range([0, 1]);
return scale(value);
}
function getValue(variantIndex, property) {
return variantData[variantIndex].core[property].value;
}
function getOriginalValue(variantIndex, property) {
return variantData[variantIndex].core[property].originalValue;
}
function getDisplayName(variantIndex, property) {
return variantData[variantIndex].core[property].displayName;
}
function getIsMissing(variantIndex, property) {
return variantData[variantIndex].core[property].isMissing;
}