-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathcsq.js
40 lines (36 loc) · 1.35 KB
/
csq.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
var CSQ = function (info_csq, csq_names, numeric_fields) {
var csq = this;
csq._values = info_csq.split('|');
csq._numeric_fields = numeric_fields;
// create an object of key => index from vcf_csq
csq._names = {};
csq_names.forEach(function (name, index) { csq._names[name] = index; });
return new Proxy(csq, {
get: function (target, name) {
if (!(name in target._names)) {
throw "unknown CSQ field: " + name;
}
var result = target._values[target._names[name]];
if (target._numeric_fields[name]) {
if(result.length == 0 || isNaN(result)) {
result = NaN
} else {
try {
result = parseFloat(result);
} catch(e) { result = NaN }
}
}
return result
}
});
};
function CSQs(csq_string, vcf_csq, numeric_fields) {
// if numeric fields is an array, turn it into an object
if (Array.isArray(numeric_fields)) {
var numeric_fields_obj = {};
numeric_fields.forEach(function (field) { numeric_fields_obj[field] = true; });
numeric_fields = numeric_fields_obj;
}
csqs = csq_string.split(',');
return csqs.map(function (csq) { return new CSQ(csq, vcf_csq, numeric_fields); });
}