-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
290 lines (249 loc) · 7.67 KB
/
script.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
// var in_fasta='>seq1\nATGCTG\n>seq2\nATCCCCC\nfafafaf\n>seq3\nfgjksdfl';
// var document.getElementById('file');
var rowFastaStr;
var fastaEntries = [];
var oriFasta;
var $grid = $('#grid');
var hot;
var inFileName;
var $fileInput = $('#file');
var $numEntries = $('#num_files');
var $totalBases = $('#total_bases');
var $loadingStatus = $('#loading_status');
var $downloadBotton = $('#download');
var $createGridButton = $('#CreateGrid');
var $downloadFileName = $('#dlFilename');
var FILE_LOADING_MSG = '... Now LOADING ...';
var FILE_LOADED_MSG = '';
(function loop() {
setTimeout(loop, 1000);
$numEntries.text(fastaEntries.length);
let num_bases = 0;
for(let i=0; i < fastaEntries.length;i++){
num_bases += fastaEntries[i].seq.length
};
$totalBases.text(num_bases);
if (num_bases == 0){
$downloadBotton.prop('disabled', true);
$createGridButton.prop('disabled', true);
};
if ($downloadFileName.val() == '' && inFileName){
$downloadFileName.val('RENAMED_' + inFileName.replace(/\.[^/.]+$/, ""))
}
}
)();
// $('#file')[0].addEventListener('change', getFastaFile, false);
// $fileInput.bind('change', getFastaFile);
function validateHeader(in_str){
var isAscii = /^[\x00-\x7F]+$/;
var forbidden = /[>]/;
if (in_str == ''){
return false
}
if (!isAscii.test(in_str)){
return false
}
if (forbidden.test(in_str)){
return false
}
return true
}
function dropFastaFile(evt){
evt.preventDefault();
var isfasta = /.fasta$|.fa$|.fas$|.msa$/;
$downloadBotton.prop('disabled', true);
$createGridButton.prop('disabled', true);
if (evt.dataTransfer.items) {
// Use DataTransferItemList interface to access the file(s)
let n_finished = 0;
let n_files = evt.dataTransfer.items.length;
$loadingStatus.text(FILE_LOADING_MSG);
for (var i = 0; i < n_files; i++) {
// If dropped items aren't files, reject them
if (evt.dataTransfer.items[i].kind === 'file') {
var f = evt.dataTransfer.items[i].getAsFile();
if (!isfasta.test(f.name)){
console.log(f.name + ' =>error');
n_finished++;
if (n_finished >= n_files){
// generateGrid()
$loadingStatus.text(FILE_LOADED_MSG)
}
continue
}
let reader = new FileReader();
reader.fileName = f.name;
inFileName = f.name;
reader.onload = function() {
let tmp = parseFASTA(reader.result, reader.fileName);
if (tmp) {
fastaEntries = fastaEntries.concat(tmp)
};
n_finished++;
if (n_finished >= n_files){
// generateGrid()
$loadingStatus.text(FILE_LOADED_MSG)
}
};
reader.readAsText(f)
}
}
} else {
// Use DataTransfer interface to access the file(s)
for (var i = 0; i < evt.dataTransfer.files.length; i++) {
console.log('... file[' + i + '].name = ' + ev.dataTransfer.files[i].name);
}
};
$downloadBotton.prop('disabled', false);
$createGridButton.prop('disabled', false);
// generateGrid()
};
function dragOverHandler(ev) {
// Prevent default behavior (Prevent file from being opened)
ev.preventDefault();
}
function generateGrid(){
var data = [];
// if (!hot){
// return
// }
fastaEntries.forEach((item, i) => {
// data.push([item.sourceFile, item.id, ' => ', item.id])
data.push({
SourceFile: item.sourceFile,
OriginalName: item.original_id,
Arrow: ' => ',
NewName: item.id,
TotalBases: item.len,
UnknownBases: item.num_unknown,
Include: item.include
})
});
$grid.empty();
// let tbl = document.createElement("div");
let $tbl = $('<div></div>');
// $grid[0].appendChild(tbl);
$grid.append($tbl);
hot = new Handsontable($tbl[0], {
data: data,
columns: [
{ data: 'SourceFile', readOnly: true, type: 'text' },
{ data: 'OriginalName', readOnly: true, type: 'text' },
{ data: 'Arrow', readOnly: true, type: 'text' },
{ data: 'NewName', type: 'text'},
{ data: 'UnknownBases', readOnly: true,type: 'text' },
{ data: 'TotalBases', readOnly: true,type: 'text' },
{ data: 'Include', type: 'checkbox' },
{ data: 'Info', readOnly: true, type: 'text' }
],
width: '100%',
colWidths: [150, 400, 50, 400, 50, 80, 80, 200],
height: 500,
manualColumnResize: true,
rowHeaders: true,
colHeaders: ['SourceFile','OriginalName', '', 'NewName',
'Unknown<br> Bases', 'Total<br> Bases',
'Include in<br>output<br>file', 'Info'],
fixedColumnsLeft: 2
});
hot.validating = false;
hot.addHook('afterChange', function(changes, source){
if (source === 'loadData' || source === 'internal'){return};
// if (!changes) {return};
if (this.validating) {return};
// console.log(source);
// return false;
for (let i=0; i < changes.length; i++){
let row, prop, oldValue, newValue;
[row, prop, oldValue, newValue] = changes[i];
if (prop == 'NewName'){
if (newValue != this.getDataAtRowProp(row,'OriginalName')){
this.setDataAtRowProp(row,'Info', 'Edited')
}else{
this.setDataAtRowProp(row,'Info', '')
}
this.setDataAtRowProp(row,'Include', true)
}
};
this.validate()
});
hot.validate = function (){
this.validating = true;
let newnames = this.getDataAtProp('NewName');
let originalnames = this.getDataAtProp('OriginalName');
let isincludes = this.getDataAtProp('Include');
let already_used = {};
// console.log(isincludes);
for (let row=0; row < newnames.length; row++){
fastaEntries[row].id = newnames[row];
fastaEntries[row].include = isincludes[row];
if (!validateHeader(newnames[row])){
this.setDataAtRowProp(row,'Info', 'Contains invalid character(s) or is empty!');
this.setDataAtRowProp(row,'Include', false);
// console.log('aa');
fastaEntries[row].include = false;
}else if (already_used[newnames[row]]){
this.setDataAtRowProp(row,'Info', 'Duplicated!');
this.setDataAtRowProp(row,'Include', false);
fastaEntries[row].include = false;
}else if (originalnames[row] != newnames[row]){
this.setDataAtRowProp(row,'Info', 'Edited')
}else if(isincludes[row]){
already_used[newnames[row]] = true;
}
};
this.validating = false
// console.log(this.getDataAtProp('Include'));
};
hot.validate()
};
function parseFASTA(fastaStr, fileName=''){
let inLines = fastaStr.split(/\r?\r?\n/);
let outFastaEntries = [];
let e = null;
if (!inLines[0].startsWith('>')) {
window.alert(fileName + ' does not seem FASTA.');
return
}
for (let i=0; i < inLines.length; i++){
let line = inLines[i];
if (line.startsWith('>')){
if (e){
outFastaEntries.push(e)
}
let h = line.replace(/^>/, '');
e = new fastaEntry(h,'',fileName)
}else{
if (line){
e.extendSeq(line)
}
}
}
if (e){
outFastaEntries.push(e)
}
return outFastaEntries
}
function downloadFasta(){
var valid = true;
if(valid){
let outFastaStrArray = [];
for (let i=0,e; e = fastaEntries[i]; i++){
if (e.include){
outFastaStrArray.push(e.outFastaStr());
console.log(e.id, 'len: ' + e.seq.length)
}
}
let newFasta = outFastaStrArray.join('\n');
// console.log(newFasta);
let blob = new Blob([newFasta], {type:"text/plain"});
const url = URL.createObjectURL(blob);
const a = document.createElement("a");
document.body.appendChild(a);
a.download = $downloadFileName.val() + '.fasta';
a.href = url;
a.click();
a.remove();
URL.revokeObjectURL(url);
}
}