-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
313 lines (272 loc) · 12.8 KB
/
index.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
const pako = require('pako')
const fetch = require('node-fetch')
const DnaAnalyzer = require('./lib/analyzer').DnaAnalyzer
const GzipInflate = pako.Inflate
class TextInflate { //{{{
push(data, end) {
this.onData(new Uint8Array(data));
if (end) this.onEnd();
}
}//}}}
class LineReader {//{{{
CHUNK_SIZE = 1024000;
position = 0;
length = 0;
byteBuffer = new Uint8Array(0);
lines = [];
lineCount = 0;
lineIndexTracker = 0;
fileReader = new FileReader();
textDecoder = new TextDecoder('utf8');
get position() { return this.position }
get length() { return this.length }
get progress() { return this.position/this.length }
get allCachedLinesAreDispatched() {
return !(this.lineIndexTracker < this.lineCount);
}
get blobIsReadInFull() {
return !(this.position < this.length);
}
get bufferIsEmpty() {
return this.byteBuffer.length === 0;
}
get endOfStream() {
return this.blobIsReadInFull && this.allCachedLinesAreDispatched && this.bufferIsEmpty;
}
constructor(blob) {
const self = this;
this.blob = blob;
this.length = blob.size;
const name = this.blob.name.split(/(\\|\/)/g).pop();
const ext = name.split('.').pop().toLowerCase();
switch (ext) {
case 'gz':
this.inflator = new GzipInflate({chunkSize:this.CHUNK_SIZE});
break
default:
this.inflator = new TextInflate({chunkSize:this.CHUNK_SIZE});
break
}
this.inflator.onData = function (chunk) {
let tempByteBuffer = new Uint8Array(self.byteBuffer.length + chunk.byteLength);
tempByteBuffer.set(self.byteBuffer);
tempByteBuffer.set(chunk, self.byteBuffer.length);
self.byteBuffer = tempByteBuffer;
};
this.inflator.onEnd = function (status) {
this.err = status;
this.msg = this.strm.msg;
console.error('Error:', this.err, this.msg)
};
}
blob2arrayBuffer(blob) {
return new Promise((resolve, reject) => {
this.fileReader.onerror = reject;
this.fileReader.onload = () => {
resolve(this.fileReader.result);
};
this.fileReader.readAsArrayBuffer(blob);
});
}
read(offset, count) {
return new Promise(async (resolve, reject) => {
if (!Number.isInteger(offset) || !Number.isInteger(count) || count < 1 || offset < 0 || offset > this.length - 1) {
resolve(new ArrayBuffer(0));
return
}
let endIndex = offset + count;
if (endIndex > this.length) endIndex = this.length;
let blobSlice = this.blob.slice(offset, endIndex);
resolve(await this.blob2arrayBuffer(blobSlice));
});
}
readLine() {
return new Promise(async (resolve, reject) => {
if (!this.allCachedLinesAreDispatched) {
resolve(this.lines[this.lineIndexTracker++] + '\n');
return;
}
while (!this.blobIsReadInFull) {
let chunk = await this.read(this.position, this.CHUNK_SIZE);
this.position += chunk.byteLength;
this.inflator.push(chunk);
let lastIndexOfLineFeedCharacter = this.byteBuffer.lastIndexOf(10); // LINE FEED CHARACTER (\n) IS ONE BYTE LONG IN UTF-8 AND IS 10 IN ITS DECIMAL FORM
if (lastIndexOfLineFeedCharacter > -1) {
let lines = this.textDecoder.decode(this.byteBuffer).split('\n');
this.byteBuffer = this.byteBuffer.slice(lastIndexOfLineFeedCharacter + 1);
let firstLine = lines[0];
this.lines = lines.slice(1, lines.length - 1);
this.lineCount = this.lines.length;
this.lineIndexTracker = 0;
resolve(firstLine + '\n');
return;
}
}
this.inflator.push(new Uint8Array(), true);
if (!this.bufferIsEmpty) {
let line = this.textDecoder.decode(this.byteBuffer);
this.byteBuffer = new Uint8Array(0);
resolve(line);
return;
}
resolve(null);
});
}
}//}}}
window.addEventListener('load', async event => { //{{{
const logArea = document.getElementById('log')
const log = console.log
console.log = function() {
logArea.value += '\n' + Array.prototype.join.call(arguments, ' ')
logArea.scrollTop = logArea.scrollHeight
log.apply(console, arguments)
}
logArea.value = 'Loading...'
const dataFiles = ['Rsnum', 'Genotype', 'Genoset', 'Medicine']
const dataRequests = await Promise.all(dataFiles.map(e => fetch('data/' + e + '.json')))
const [Rsnum, Genotype, Genoset, Medicine] = await Promise.all(dataRequests.map(e => e.json()))
const MedicineIndex = {}
const NumMedicineIndex = {}
Medicine.forEach(e => {
MedicineIndex[e.id] = e
e.criteria.forEach(f => {
if (!NumMedicineIndex[f]) {
NumMedicineIndex[f] = [e.id]
} else {
NumMedicineIndex[f].push(e.id)
}
})
})
const analyzer = new DnaAnalyzer(Rsnum, Genotype, Genoset, MedicineIndex)
const fileInput = document.getElementById('file')
//const outputArea = document.getElementById('output')
console.log('Rsnum:', Rsnum.length)
console.log('Genotype:', Genotype.length)
console.log('Genoset:', Genoset.length)
console.log('Medicine:', Medicine.length)
console.log('Loaded.')
fileInput.disabled = false
//outputArea.value = 'Output...'
console.outputRsnum = function(rsid, genotype, magnitude, repute, summary, mergedRsid) {
const outputBody = document.getElementById('RsnumTableBody')
const Rs = 'rs' + rsid
const MergedRs = 'rs' + mergedRsid
const Gs = '(' + genotype.join(';') + ')'
genotype = genotype.join(';')
const html = '<tr><td><a id="' + Rs +'">'
+ rsid +'</td><td></a>'
+ genotype.substr(0,11) + (genotype.length > 11 ? '...' : '') +'</td><td>'
+ (magnitude === undefined ? 'Undefined' : magnitude) + '</td><td>'
+ (repute === undefined ? 'Undefined' : repute) + '</td><td>'
+ (summary === undefined ? '' : summary) + (mergedRsid ? ' Merged with above: <a href="#' + MergedRs +'">#' + MergedRs + '</a>.' : '') + '</td><td>'
+ '<a target="blank" href="https://www.snpedia.com/index.php/' + Rs +'">' + Rs + '</a>'
+ '<a target="blank" href="https://www.snpedia.com/index.php/' + Rs + Gs + '">' + Gs.substr(0,6) + (Gs.length > 6 ? '...' : '') + '</a>'
//+ (mergedRsid ? ', <a href="#' + MergedRs +'">#' + MergedRs + '</a>' : '')
+ '<td></tr>'
outputBody.innerHTML += html
/*if (rsid == 15793179) {
outputBody.innerHTML += "Dedly mutation for chickens detected. Do not worry unless you are a chicken!"
}*/
if (NumMedicineIndex[Rs]) {
const outputBodyPharm = document.getElementById('PharmacogeneticsTableBody')
NumMedicineIndex[Rs].forEach(e => {
const html = '<tr><td>'
+ '<a target="blank" href="https://www.snpedia.com/index.php/' + e + '">' + e + '</a></td><td>'
+ (magnitude === undefined ? 'Undefined' : magnitude) + '</td><td>'
+ (repute === undefined ? 'Undefined' : repute) + '</td><td>'
+ (summary === undefined ? '' : summary) + (mergedRsid ? ' Merged with above: <a href="#' + MergedRs +'">#' + MergedRs + '</a>.' : '') + '</td><td>'
+ '<a target="blank" href="https://www.snpedia.com/index.php/' + Rs +'">' + Rs + '</a>'
+ '<a target="blank" href="https://www.snpedia.com/index.php/' + Rs + Gs + '">' + Gs.substr(0,6) + (Gs.length > 6 ? '...' : '') + '</a>'
+ '<td></tr>'
outputBodyPharm.innerHTML += html
})
}
}
console.outputGenoset = function(gsid, magnitude, repute, summary, output) {
const outputBody = document.getElementById('GenosetTableBody')
const Gs = 'gs' + gsid
const html = '<tr><td><a id="' + Gs + '">'
+ gsid +'</td><td></a>'
+ (magnitude === undefined ? 'Undefined' : magnitude) + '</td><td>'
+ (repute === undefined ? 'Undefined' : repute) + '</td><td>'
+ (summary === undefined ? '' : summary) + '<h3>Criteria:</h3><p>' + output + '</p></td><td>'
+ '<a target="blank" href="https://www.snpedia.com/index.php/' + Gs + '">' + Gs + '</a>'
+ '<td></tr>'
outputBody.innerHTML += html
if (NumMedicineIndex[Gs]) {
const outputBodyPharm = document.getElementById('PharmacogeneticsTableBody')
NumMedicineIndex[Gs].forEach(e => {
const html = '<tr><td>'
+ '<a target="blank" href="https://www.snpedia.com/index.php/' + e + '">' + e + '</a></td><td>'
+ (magnitude === undefined ? 'Undefined' : magnitude) + '</td><td>'
+ (repute === undefined ? 'Undefined' : repute) + '</td><td>'
+ (summary === undefined ? '' : summary) + '<h3>Criteria:</h3><p>' + output + '</p></td><td>'
+ '<a target="blank" href="https://www.snpedia.com/index.php/' + Gs + '">' + Gs + '</a>'
+ '<td></tr>'
outputBodyPharm.innerHTML += html
})
}
}
const searchFields = ['searchRsid', 'searchGenotype', 'searchMagnitude', 'searchRepute', 'searchSummary', 'searchSource']
searchFields.forEach((e,j) => {
const outputBody = document.getElementById('RsnumTablebody')
const input = document.getElementById(e)
input.addEventListener('change', event => {
const rows = outputBody.getElementsByTagName('tr')
for (let i = 0; i < rows.length; i++) {
const row = rows[i]
let match = true
searchFields.forEach((e,j) => {
if (!match) return
const input = document.getElementById(e)
const filter = input.value.toLowerCase()
if (!filter || filter.trim() == '') return
const td = row.getElementsByTagName("td")[j]
if (td) {
const txtValue = td.textContent || td.innerText
const c = filter[0]
if (c == '>') {
match &= txtValue.toLowerCase() > parseFloat(filter.substr(1))
} else if (c == '<') {
match &= txtValue.toLowerCase() < parseFloat(filter.substr(1))
} else if (c == '=') {
match &= txtValue.toLowerCase() == filter.substr(1)
} else if (c == '!') {
match &= txtValue.toLowerCase() != filter.substr(1)
} else if (c == '/') {
match &= txtValue.toLowerCase().match(new RegExp(filter.substr(1)))
} else {
match &= txtValue.toLowerCase().indexOf(filter) > -1
}
}
});
row.style.display = match ? "" : "none"
}
})
})
fileInput.addEventListener('change', async event => {
fileInput.disabled = true
const file = fileInput.files[0]
const name = file.name.split(/(\\|\/)/g).pop()
const ext = name.split('.').pop().toLowerCase()
console.log('Input:', name, ext)
const textReader = new LineReader(file)
await analyzer.run(textReader, console.log)
console.log('Done.')
})
const tablinks = document.getElementsByClassName("tablinks");
for (let j = 0; j < tablinks.length; j++) {
tablinks[j].addEventListener('click', event => {
const tabcontent = document.getElementsByClassName("tabcontent");
for (let i = 0; i < tabcontent.length; i++) {
tabcontent[i].style.display = "none";
}
for (let i = 0; i < tablinks.length; i++) {
tablinks[i].className = tablinks[i].className.replace(" active", "");
}
document.getElementById(event.currentTarget.id.split('Button')[0]).style.display = "table";
event.currentTarget.className += " active";
})
}
document.getElementById("RsnumTabButton").click();
});//}}}