-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
364 lines (301 loc) · 10.4 KB
/
main.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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
const LIZ = [20, 40, 60, 80, 100, 114, 120, 140, 160, 180, 200, 214, 220, 240, 260, 280, 300, 314, 320, 340, 360, 380, 400, 414, 420, 440, 460, 480, 500, 514, 520, 540, 560, 580, 600];
const tagDict = {
"DATA1": { "tagName": "DATA", "tagNum": 1, "typeToReturn": "getShort" },
"DATA2": { "tagName": "DATA", "tagNum": 2, "typeToReturn": "getShort" },
"DATA3": { "tagName": "DATA", "tagNum": 3, "typeToReturn": "getShort" },
"DATA4": { "tagName": "DATA", "tagNum": 4, "typeToReturn": "getShort" },
"DATA205": { "tagName": "DATA", "tagNum": 205, "typeToReturn": "getShort" },
};
// inspired by https://github.com/eamitchell/ab1ToJSON
let DIR_SIZE = 28;
class abiConverter {
constructor(buffer) {
this.buffer = buffer;
this.dirLocation = buffer.getInt32(26);
this.numElements = buffer.getInt32(18);
this.lastEntry = this.dirLocation + (this.numElements * DIR_SIZE);
}
getTagName(inOffset) {
var name = "";
for (var loopOffset = inOffset; loopOffset < inOffset + 4; loopOffset++) {
name += String.fromCharCode(this.buffer.getInt8(loopOffset));
}
return name;
}
getNumber(inOffset, numEntries) {
var retArray = [];
for (var counter = 0; counter < numEntries; counter += 1) {
retArray.push(this.buffer.getInt8(inOffset + counter));
}
return retArray;
}
getShort(inOffset, numEntries) {
var retArray = [];
for (var counter = 0; counter < numEntries; counter += 2) {
retArray.push(this.buffer.getInt16(inOffset + counter));
}
return retArray;
}
getData(inTag) {
let output;
let curElem = this.dirLocation;
while (curElem < this.lastEntry) {
let currTagName = this.getTagName(curElem);
let tagNum = this.buffer.getInt32(curElem + 4);
if (currTagName == inTag.tagName && tagNum === inTag.tagNum) {
let numEntries = this.buffer.getInt32(curElem + 16);
let entryOffset = this.buffer.getInt32(curElem + 20);
output = this[inTag.typeToReturn](entryOffset, numEntries);
break;
}
curElem += DIR_SIZE;
}
return output;
}
}
function parseAbifFile(file) {
return new Promise((resolve, reject) => {
const reader = new window.FileReader();
reader.onload = (event) => {
const arrayBuffer = event.target.result;
resolve(arrayBuffer);
};
reader.onerror = (event) => {
reject(event.target.error);
};
reader.readAsArrayBuffer(file);
});
}
findPeaks = (data, threshold, windowSize) => {
// handle cases where current === after and before
// handle where i < halfWindow
let peaks = [];
const halfWindow = Math.floor(windowSize / 2);
// start from index 1 (no peaks at 0 anyways)
for (let i = 1; i < data.length; i++) {
let current = data[i];
if (current >= threshold) {
let before = data[i - 1];
let after = data[i + 1];
if (current > before && current > after) {
let windowArray = data.slice(i - halfWindow, i + halfWindow);
if (current === Math.max(...windowArray)) {
let pair = [i, current];
peaks.push(pair);
}
}
}
}
return peaks;
}
// Ladder matching functions
function* range(start, end) {
for (; start <= end; ++start) { yield start; }
}
function last(arr) { return arr[arr.length - 1]; }
function* numericCombinations(n, r, loc = []) {
const idx = loc.length;
if (idx === r) {
yield loc;
return;
}
for (let next of range(idx ? last(loc) + 1 : 0, n - r + idx)) { yield* numericCombinations(n, r, loc.concat(next)); }
}
function* combinations(arr, r) {
for (let idxs of numericCombinations(arr.length, r)) { yield idxs.map(i => arr[i]); }
}
function diffs(array, max_diff) {
for (let i = 0; i < array.length - 1; i++) {
if (array[i + 1] - array[i] > max_diff) {
return true;
}
}
return false;
}
// implement pearsonr corr function
function pearson (x, y) {
const promedio = l => l.reduce((s, a) => s + a, 0) / l.length
const calc = (v, prom) => Math.sqrt(v.reduce((s, a) => (s + a * a), 0) - n * prom * prom)
let n = x.length
let nn = 0
for (let i = 0; i < n; i++, nn++) {
if ((!x[i] && x[i] !== 0) || (!y[i] && y[i] !== 0)) {
nn--
continue
}
x[nn] = x[i]
y[nn] = y[i]
}
if (n !== nn) {
x = x.splice(0, nn)
y = y.splice(0, nn)
n = nn
}
const prom_x = promedio(x), prom_y = promedio(y)
return (x
.map((e, i) => ({ x: e, y: y[i] }))
.reduce((v, a) => v + a.x * a.y, 0) - n * prom_x * prom_y
) / (calc(x, prom_x) * calc(y, prom_y))
}
function findBestMatch(peaks, ladder) {
let highScoreMatch;
let pearsonScore = 0;
for (comb of combinations(peaks, LIZ.length)) {
if (diffs(comb, 200)) {
continue;
}
let score = pearson(comb, LIZ);
if (score > pearsonScore) {
pearsonScore = score;
highScoreMatch = comb;
}
}
return highScoreMatch;
}
// Fit basepairs to best model
function polynomialFit(x, y, degree) {
if (x.length !== y.length) {
throw new Error("Input arrays x and y must have the same length.");
}
if (degree < 1) {
throw new Error("Degree of the polynomial must be at least 1.");
}
const n = x.length;
const matrix = [];
// Build the matrix of equation coefficients
for (let i = 0; i < degree + 1; i++) {
matrix[i] = [];
for (let j = 0; j < degree + 1; j++) {
let sum = 0;
for (let k = 0; k < n; k++) {
sum += Math.pow(x[k], i + j);
}
matrix[i][j] = sum;
}
}
const rhs = [];
// Build the right-hand side of the equation
for (let i = 0; i < degree + 1; i++) {
let sum = 0;
for (let j = 0; j < n; j++) {
sum += y[j] * Math.pow(x[j], i);
}
rhs[i] = sum;
}
// Solve the system of equations using Gaussian elimination
for (let i = 0; i < degree + 1; i++) {
let maxRow = i;
for (let j = i + 1; j < degree + 1; j++) {
if (Math.abs(matrix[j][i]) > Math.abs(matrix[maxRow][i])) {
maxRow = j;
}
}
const temp = matrix[i];
matrix[i] = matrix[maxRow];
matrix[maxRow] = temp;
const rhsTemp = rhs[i];
rhs[i] = rhs[maxRow];
rhs[maxRow] = rhsTemp;
for (let j = i + 1; j < degree + 1; j++) {
const factor = matrix[j][i] / matrix[i][i];
for (let k = i; k < degree + 1; k++) {
matrix[j][k] -= factor * matrix[i][k];
}
rhs[j] -= factor * rhs[i];
}
}
const coefficients = [];
// Back substitution
for (let i = degree; i >= 0; i--) {
let sum = 0;
for (let j = i + 1; j < degree + 1; j++) {
sum += matrix[i][j] * coefficients[j];
}
coefficients[i] = (rhs[i] - sum) / matrix[i][i];
}
return coefficients;
}
function predict(coefficients, x) {
const degree = coefficients.length - 1;
let result = 0;
for (let i = 0; i <= degree; i++) {
result += coefficients[i] * Math.pow(x, i);
}
return result;
}
function plotData(converter, tag, divName) {
let data = converter.getData(tagDict[tag]);
const s = new dfd.Series(data);
s.plot(divName).line()
}
function plotAllData(converter, coefs) {
let df = new dfd.DataFrame(
{
"data1": converter.getData(tagDict["DATA1"]),
// "data2": converter.getData(tagDict["DATA2"]),
// "data3": converter.getData(tagDict["DATA3"]),
// "data4": converter.getData(tagDict["DATA4"]),
}
);
df = df.addColumn("time",
[...Array(df.shape[0]).keys()].map((num) => num + 1)
);
df = df.addColumn("bp",
df.time.$data.map((x) => predict(coefs, x))
)
df.setIndex({column: "bp", inplace: true})
df.drop({ columns: ["time", "bp"], inplace: true });
df.plot("allData").line()
}
function calculateCombinations(n, k) {
if (n < 0 || k < 0 || k > n) {
return 0; // Invalid input
}
let numerator = 1;
let denominator = 1;
for (let i = 1; i <= k; i++) {
numerator *= (n - i + 1);
denominator *= i;
}
return Math.floor(numerator / denominator);
}
function findAndMatchLadder(converter, threshold, windowSize=10) {
let ladder = converter.getData(tagDict["DATA205"]);
let peaks = findPeaks(ladder, threshold=threshold, windowSize=windowSize);
let peaksIndices = peaks.map(x => x[0]);
if (peaksIndices.length < LIZ.length) {
alert(`Too few peaks found!, Number of peaks: ${peaksIndices.length}`);
return;
}
let numCombinations = calculateCombinations(peaksIndices.length, LIZ.length);
if (numCombinations >= 200000) {
alert(`Too many combinations!, Number of combs: ${numCombinations}`);
return;
}
let bestMatch = findBestMatch(peaksIndices, LIZ);
return bestMatch
}
let coefs;
document.addEventListener("DOMContentLoaded", function () {
let fileInput = document.querySelector('#fsa-file');
fileInput.addEventListener('change', (event) => {
const file = fileInput.files[0];
parseAbifFile(file)
.then((buffer) => {
const dataView = new DataView(buffer);
let converter = new abiConverter(dataView);
let threshold = document.querySelector("#peakThreshold").value
const bestMatch = findAndMatchLadder(converter, threshold=threshold);
coefs = polynomialFit(bestMatch, LIZ, 5);
// ladder 205
plotData(converter, "DATA205", "data1");
plotData(converter, "DATA2", "data2");
plotData(converter, "DATA3", "data3");
plotData(converter, "DATA1", "data4");
plotAllData(converter, coefs);
})
.catch((error) => {
console.error('An error occurred while parsing the file:', error);
});
});
});