-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
152 lines (143 loc) · 5.09 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
const matrixElm = document.querySelector(".matrix");
const seqAElm = document.querySelector("#seq-a");
const seqBElm = document.querySelector("#seq-b");
const needleSmithInputsElm = document.querySelector(".needle-smith__inputs");
const matchElm = document.querySelector("#match");
const misMatchElm = document.querySelector("#mismatch");
const gapElm = document.querySelector("#gap");
const dotMatrixInputsElm = document.querySelector(".dot-matrix__inputs");
const wSizeElm = document.querySelector("#w-size");
const stepElm = document.querySelector("#step");
const threshElm = document.querySelector("#thresh");
const alignmentsElm = document.querySelector(".alignments-section");
const solveBtnElm = document.querySelector("#solve-btn");
const algorithmsListElm = document.querySelector(".algorithms");
const algorithms = {
needlemanWunsch: "Needleman-Wunsch",
smithWaterman: "Smith-Waterman",
dotMatrixWindow: "Dot Plot Matrix With Window",
};
let currAlgorithm = algorithms.needlemanWunsch;
const fillMatrixElm = (matrix) => {
for (let i = 0; i < matrix.length; i++) {
const rowElm = document.createElement("tr");
if (i === 0) rowElm.className = "highlight";
for (let j = 0; j < matrix[0].length; j++) {
const cellElm = document.createElement("td");
if (j === 0) cellElm.className = "highlight";
if (matrix[i][j] === true) {
cellElm.innerText = "•";
cellElm.classList.add("dot-cell");
} else if (matrix[i][j] === undefined) {
cellElm.innerText = "";
} else {
cellElm.innerText = matrix[i][j];
}
rowElm.appendChild(cellElm);
}
matrixElm.appendChild(rowElm);
}
};
const switchAlgorithm = (e) => {
const elm = e.target;
algorithmsListElm
.querySelectorAll("li")
.forEach((elm) => elm.classList.remove("active"));
if (elm.innerText === algorithms.needlemanWunsch) {
elm.classList.add("active");
needleSmithInputsElm.style.display = "block";
dotMatrixInputsElm.style.display = "none";
currAlgorithm = algorithms.needlemanWunsch;
} else if (elm.innerText === algorithms.smithWaterman) {
elm.classList.add("active");
needleSmithInputsElm.style.display = "block";
dotMatrixInputsElm.style.display = "none";
currAlgorithm = algorithms.smithWaterman;
} else if (elm.innerText === algorithms.dotMatrixWindow) {
elm.classList.add("active");
needleSmithInputsElm.style.display = "none";
dotMatrixInputsElm.style.display = "block";
alignmentsElm.style.display = "none";
currAlgorithm = algorithms.dotMatrixWindow;
}
};
const solve = () => {
matrixElm.innerHTML = "";
alignmentsElm.innerHTML = "<h2>Alignments</h2>";
let matrix;
let inputs;
let alignments;
switch (currAlgorithm) {
case algorithms.needlemanWunsch:
inputs = getNeedleOrSmithInputsValues();
const needlemanWunsch = new NeedlemanWunsch(...inputs);
needlemanWunsch.solve();
matrix = needlemanWunsch.getDisplayMatrix();
alignments = needlemanWunsch.alignments;
displayAlignments(alignments);
break;
case algorithms.smithWaterman:
inputs = getNeedleOrSmithInputsValues();
const smithWaterman = new SmithWaterman(...inputs);
smithWaterman.solve();
matrix = smithWaterman.getDisplayMatrix();
alignments = smithWaterman.alignments;
displayAlignments(alignments);
break;
case algorithms.dotMatrixWindow:
inputs = getDotMatrixInputsValues();
const dotMatrixWindow = new DotMatrixWindow(...inputs);
dotMatrixWindow.solve();
matrix = dotMatrixWindow.getDisplayMatrix();
break;
default:
break;
}
fillMatrixElm(matrix);
};
const getNeedleOrSmithInputsValues = () => {
return [
seqAElm.value.trim(),
seqBElm.value.trim(),
parseInt(matchElm.value),
parseInt(misMatchElm.value),
parseInt(gapElm.value),
];
};
const getDotMatrixInputsValues = () => {
return [
seqAElm.value.trim(),
seqBElm.value.trim(),
parseInt(wSizeElm.value),
parseInt(stepElm.value),
parseInt(threshElm.value),
];
};
const displayAlignments = (alignments) => {
alignmentsElm.style.display = "block";
const alignmentscontainerElm = document.createElement("div");
alignmentscontainerElm.className = "alignments";
alignments.forEach((alignment) => {
const alignmentElm = document.createElement("div");
const scrollContainerElm = document.createElement("div");
scrollContainerElm.className = "scroll-container";
alignmentElm.className = "alignment";
let seqA = "";
let seqB = "";
alignment.A.split("").forEach((letter) => {
seqA += `<div>${letter}</div>`;
});
alignment.B.split("").forEach((letter) => {
seqB += `<div>${letter}</div>`;
});
alignmentElm.innerHTML = `
<div class="seq"><span>A: </span>${seqA}</div>
<div class="seq"><span>B: </span>${seqB}</div>
`;
scrollContainerElm.appendChild(alignmentElm);
alignmentscontainerElm.appendChild(scrollContainerElm);
});
alignmentsElm.appendChild(alignmentscontainerElm);
};
solveBtnElm.addEventListener("click", solve);
algorithmsListElm.addEventListener("click", switchAlgorithm);