-
Notifications
You must be signed in to change notification settings - Fork 0
/
viz.js
73 lines (70 loc) · 2.35 KB
/
viz.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
const anyToJson = require('bio-parsers/parsers/anyToJson');
parseFasta = (fileObj) =>
{
var fileSections = [];
var lines = fileObj.split(/\r\n|\n/);
lines.forEach((line) => {
if (line[0] == '>'){
/*add to description*/
fileSections.push({ desc: line.slice(1), data: '' });
}
else if (line == /\r\n|\n/){/*blank line, do nothing*/}
else {
fileSections[fileSections.length-1].data += line;
}
})
return fileSections;
};
parseGenbank = (fileObj) =>
{
var fileSections = [];
var lines = fileObj.split(/\r\n|\n/);
var featIdx;
var originIdx;
lines.forEach((line) => {
if (line.toUpperCase().includes('FEATURES')) {featIdx = lines.indexOf(line)};
if (line.toUpperCase().includes('ORIGIN')) {originIdx = lines.indexOf(line)};
});
fileSections.push({ desc: 'metadata', data: lines.slice(0, featIdx)});
fileSections.push({ desc: 'features', data: lines.slice(featIdx + 1, originIdx)});
fileSections.push({ desc: 'origin', data: lines.slice(originIdx + 1)});
return fileSections;
};
getFileText = (fileName, fileType, fileData) =>
{
switch (fileType) {
case 'fasta':
case 'fa':
return parseFasta(fileData);
case 'genbank':
case 'gb':
case 'gbk':
return parseGenbank(fileData);
default:
return [{'desc':fileName, 'data':fileData}];
};
};
var loadFile = function(event) {
var input = event.target;
var fname = input.files[0].name;
var ftype = fname.substring(fname.lastIndexOf('.') + 1, fname.length) || fname;
var reader = new FileReader();
reader.readAsText(input.files[0]);
reader.onload = function(e) {
var textZone = document.getElementById('textZone');
while (textZone.firstChild) textZone.removeChild(textZone.firstChild);
var textBlocks = getFileText(fname, ftype, reader.result);
textBlocks.forEach((textBlock) =>
{
var zoneItem = document.createElement('li');
var innerDesc = document.createElement('h4');
innerDesc.innerHTML = textBlock.desc;
innerDesc.style += 'bold';
var innerText = document.createElement('textarea');
innerText.innerHTML = textBlock.data;
zoneItem.appendChild(innerDesc)
zoneItem.appendChild(innerText);
textZone.appendChild(zoneItem);
});
};
};