-
Notifications
You must be signed in to change notification settings - Fork 0
/
hydrogeneFast.js
349 lines (282 loc) · 8.72 KB
/
hydrogeneFast.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
var fs = require('fs');
var hydroGene = function(seq1,seq2) {
function makeArray(length, val) {
if (val === undefined) {
val = 0 | 0;
}
if (val < 0) {
val = 0;
}
length |= 0;
var max = 0;
for (var i = length; i !== 0; i >>>= 1) {
max++;
}
var n = Array(max);
n[0] = [val];
for (i = 1; i < max; i++) {
n[i] = n[i - 1].concat(n[i - 1]);
}
var a = [];
for (var i = 0, l = length; l !== 0; l >>>= 1, i++) {
if (l & 1) {
a = a.concat(n[i]);
}
}
return a;
};
// Map --> Protein to 5 bit
var __proteinTo5Bit = {
'A': 1,
'R': 2,
'N': 3,
'D': 4,
'C': 5,
'E': 6,
'Q': 7,
'G': 8,
'H': 9,
'I': 10,
'L': 11,
'K': 12,
'M': 13,
'F': 14,
'P': 15,
'S': 16,
'T': 17,
'W': 18,
'Y': 19,
'V': 20,
'-': 21,
'_': 22
};
// Reverse Map for mapping each Integer to a Character again
var reverseMap = {};
for (var key in __proteinTo5Bit) {
if (__proteinTo5Bit.hasOwnProperty(key)) {
reverseMap[__proteinTo5Bit[key]] = key;
}
}
/* Double up to form bytes */
var __proteinTo2Bytes;
var __2BytesToProtein;
var __byteNucleotideContent;
void function() {
var a = Object.create(null);
var c = new makeArray(256);
var d = new makeArray(256);
var keys = Object.keys(__proteinTo5Bit);
var len = keys.length;
var ki;
var kj;
var kk;
var byte;
for (var i = 0; i < len; i++) {
ki = keys[i];
for (var j = 0; j < len; j++) {
kj = keys[j];
for (var k = 0; k < len; k++) {
kk = keys[k];
byte = __proteinTo5Bit[kk] << 10 | (__proteinTo5Bit[kj] << 5) | __proteinTo5Bit[ki];
a[kk + kj + ki] = byte;
c[byte] = kk + kj + ki;
d[byte] = [kk, kj, ki];
}
}
}
__proteinTo2Bytes = a;
__2BytesToProtein = c;
__byteNucleotideContent = d;
}();
function proteinTo2Bytes(ss) {
return __proteinTo2Bytes[ss] | 0;
}
function Seq() {
this.__endPadding = 0;
//Initialization for the buffer
this.__buffer = new ArrayBuffer(4);
};
Seq.prototype.read1 = function(sequence) {
var proteinString = sequence.toUpperCase()
.replace(/\s/g, '');
var length = proteinString.length;
var max = Math.ceil(length / 3);
var endPadding = (4 - (max) % 4) % 4;
this.__endPadding = endPadding;
var buffer = new ArrayBuffer((max*2));
// console.log('max : ' + max);
var dataArray = new Int16Array(buffer);
var n;
var byte;
var content;
for (var i = 0; i < dataArray.length; i++) {
// Increment this variable by 3 every iteration in the loop
n = (i << 1) + i;
if (sequence[n+1] == undefined) {
dataArray[i] = proteinTo2Bytes(sequence[n] + '-' + '-');
// console.log('dataArray: ' + dataArray[i]);
// console.log('Sequence: ' + __2BytesToProtein[dataArray[i]]);
continue;
}
if (sequence[n+2] == undefined) {
dataArray[i] = proteinTo2Bytes(sequence[n] + sequence[n+1] + '-');
// console.log('dataArray: ' + dataArray[i]);
// console.log('Sequence: ' + __2BytesToProtein[dataArray[i]]);
continue;
}
dataArray[i] = proteinTo2Bytes(sequence[n] + sequence[n + 1] + sequence[n + 2]);
// console.log('dataArray: ' + dataArray[i]);
// console.log('Sequence: ' + __2BytesToProtein[dataArray[i]]);
}
this.__buffer = buffer;
this.__length = length;
// console.log("Successfully loaded sequence : " + this.sequence() + " -- Size : " + this.size());
return this;
};
Seq.prototype.read2 = function(sequence) {
var proteinString = sequence.toUpperCase()
.replace(/\s/g, '');
var length = proteinString.length;
var max = Math.ceil(length / 3);
var endPadding = (4 - (max) % 4) % 4;
this.__endPadding = endPadding;
var buffer = new ArrayBuffer((max*2));
// console.log('max : ' + max);
var dataArray = new Int16Array(buffer);
var n;
var byte;
var content;
for (var i = 0; i < dataArray.length; i++) {
// Increment this variable by 3 every iteration in the loop
n = (i << 1) + i;
if (sequence[n+1] == undefined) {
dataArray[i] = proteinTo2Bytes(sequence[n] + '_' + '_');
// console.log('dataArray: ' + dataArray[i]);
// console.log('Sequence: ' + __2BytesToProtein[dataArray[i]]);
continue;
}
if (sequence[n+2] == undefined) {
dataArray[i] = proteinTo2Bytes(sequence[n] + sequence[n+1] + '_');
// console.log('dataArray: ' + dataArray[i]);
// console.log('Sequence: ' + __2BytesToProtein[dataArray[i]]);
continue;
}
dataArray[i] = proteinTo2Bytes(sequence[n] + sequence[n + 1] + sequence[n + 2]);
// console.log('dataArray: ' + dataArray[i]);
// console.log('Sequence: ' + __2BytesToProtein[dataArray[i]]);
}
this.__buffer = buffer;
this.__length = length;
// console.log("Successfully loaded sequence : " + this.sequence() + " -- Size : " + this.size());
return this;
};
Seq.prototype.readFASTA = function(strFASTA) {
var data = strFASTA.split(/\n\r?/gi);
while (data.length && data[0][0] === '>') {
data.shift();
}
return this.read(data.join(''));
};
Seq.prototype.loadFASTA = function(path) {
return this.readFASTA(fs.readFileSync(path).toString());
};
Seq.prototype.size = function() {
return this.__length;
};
Seq.prototype.sequence = function() {
var __bytesToProtein = __2BytesToProtein;
var buffer = this.__buffer;
if (buffer.byteLength < 4) {
return '';
}
var dataArray = new Uint16Array(buffer);
var len = (buffer.byteLength);
var nts = makeArray(len);
for (var i = 0; i < len; i++) {
nts[i] = __bytesToProtein[dataArray[i]];
}
var returnString;
returnString = nts.join('');
if (returnString.includes("undefined")) {
returnString = returnString.replace(/undefined/gi, '');
}
// if (returnString.includes("-")) {
// returnString = returnString.replace(/-/gi, '');
// }
return returnString.replace(/-/gi,'').replace(/_/gi,'');
};
// This Function retreives the buffer of a sequence type object
Seq.prototype.getBuffer = function() {
var buffer = this.__buffer;
return buffer;
};
Seq.prototype.align = function(tBuffer) {
var searchBuffer = this.__buffer;
var queryBuffer = tBuffer;
var result = 0;
var tempResult = 0;
var finalResult = 0;
var index = 0;
var searchView = new Int16Array(searchBuffer);
var queryView = new Int16Array(queryBuffer);
for (var i = 0; i < queryView.length; i++) {
result = 0 ;
for (var j = i; j < searchView.length; j++) {
tempResult = compare(queryView[i],searchView[j]);
if (tempResult == 3) {
result = tempResult;
// console.log(result);
break;
}
else if (tempResult > result) {
result = tempResult;
}
// console.log('No. of matches is : ' + result);
}
// console.log('Final Result before increment: ' + finalResult);
finalResult += result;
// console.log('Final Result after increment: ' + finalResult);
};
return finalResult;
};
var compare = function(num1, num2) {
var matches = num1 ^ num2;
var result = 0;
matches |= matches >>> 1;
matches |= matches >>> 1;
matches |= matches >>> 2;
matches = ~matches & 1057;
matches |= matches >>> 4;
matches |= matches >>> 4;
matches = matches & 7;
if (matches == 1 | matches == 2 | matches == 4) result = 1;
else if (matches == 3 | matches == 6 | matches == 5) result = 2;
else if (matches == 7) result = 3;
else result = 0;
// console.log('Result for comparing ' + __2BytesToProtein[num1] + " -->" + num1 + " and " + __2BytesToProtein[num2] + " -->" + num2 + " is: " + result );
return result;
};
var sequence1 = seq1 || "";
var sequence2 = seq2 || "";
var a = new Seq().read1(sequence1);
// console.log("Done Reading Sequence.");
// console.log(a.sequence().length);
var b = new Seq().read2(sequence2);
// console.log("Done Reading Sequence.");
// console.log(b.sequence().length);
console.log("Starting the Comparison ...");
console.time('align');
var numberOfMatches = a.align(b.getBuffer());
console.timeEnd('align');
var res = {
matches: numberOfMatches,
seqAlignment: ""
};
return res;
// console.log("Finished the Comparison");
//
// console.log("Maximum number of matches is " + res);
//Testing function Compare
// console.log(compare(proteinTo2Bytes("AGC"),proteinTo2Bytes("TTA")));
};
module.exports = hydroGene;