-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.js
406 lines (367 loc) · 11.8 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
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
var directInput = document.getElementById("inputSequence");
var candidateCanvas = document.getElementById("candidateCanvas");
var candidateCanvasCtx = candidateCanvas.getContext("2d");
var designCanvas = document.getElementById("designCanvas");
var designCanvasCtx = designCanvas.getContext("2d");
var overviewCanvas = document.getElementById("overviewCanvas");
var overviewCanvasCtx = overviewCanvas.getContext("2d");
var utilityCanvas = document.getElementById("utilityCanvas");
var utilityCanvasCtx = utilityCanvas.getContext("2d");
var distrCanvas = document.getElementById("distrCanvas");
var distrCanvasCtx = distrCanvas.getContext("2d");
var clipboardButton = document.getElementById("clipboardButton");
var scoreShowDesign = document.getElementById("scoreDisplay");
var loadingbar = document.getElementById("loadingbar");
function updateLoading(x)
{
// x between 0 and 100
window.requestAnimationFrame( ()=>{loadingbar.style.width = Math.floor(x) + "%";} );
}
updateLoading(0);
var global_levenshtein_parameter = 3;
var init = true;
var processing = true;
var slidecards = [];
for (let i = 1; i < 7; i++)
{
slidecards.push(document.getElementById("card"+i));
}
function slideInCards()
{
if (init)
{
for (let i = 0; i < 6; i++)
{
const element = slidecards[i];
element.classList.remove("offsetLeft");
element.classList.remove("offsetRight");
element.classList.remove("offsetTop");
}
document.getElementById("design1").classList.remove("bkg2");
document.getElementById("design2").classList.remove("truebkg3");
//document.getElementById("bigcontainer").classList.remove("container2");
}
}
var processstack = []
function startProcessing(){
processing = true;
processstack.push(1);
}
function endProcessing(){
processstack.pop();
if (processstack.length == 0)
{
processing = false;
}
}
var candidateScroll = {
elements: [],
y: 0,
entryheight: 32,
active: false,
activeOffset: 0,
scrollbarWidth: 16,
selected: null
}
/*for (let i = 0; i < 100; i++)
{
candidateScroll.elements.push(
{
title: "ACGTAATAATAATAA",
score: i/100,
onClick: () => alert("Push")
}
)
}*/
var designScroll = {
elements: [],
y: 0,
entryheight: 32,
active: false,
activeOffset: 0,
scrollbarWidth: 16,
selected: -1
}
// Not top level
function addToDesign()
{
if (candidateScroll.selected != null)
{
designScroll.elements.push(candidateScroll.selected);
if (designScroll.elements.length > 1)
{
clipboardButton.textContent = "Copy " + designScroll.elements.length + " peptides to clipboard";
}
candidateScroll.selected.onAdd();
drawScroll(candidateCanvasCtx, candidateCanvas, candidateScroll);
drawScroll(designCanvasCtx, designCanvas, designScroll);
}
}
// Not top level
function forgetAllCandidateScores()
{
for (let i = 0; i < candidateScroll.elements.length; i++)
{
candidateScroll.elements[i].score = null;
}
if (candidateScroll.selected != null) candidateScroll.selected.getscore();
drawScroll(candidateCanvasCtx, candidateCanvas, candidateScroll);
}
// Not top level
var forgetOverviews = () => null;
function clipboardDesign()
{
if (processing || init) return;
if (!(designScroll.elements.length === 0))
{
let = text = "";
for (let i = 0; i < designScroll.elements.length; i++)
{
if (text != "") text += ",";
text += designScroll.elements[i].title;
}
navigator.clipboard.writeText(text);
}
}
function initPage()
{
processing = false;
//alert("Page is loaded");
}
function compareKmerScore(a,b)
{
let x1 = a.getscore();
let x2 = b.getscore();
if (a.sequon || !(a.redundant === null) || a.selfpeptide) x1 -= 1000;
if (b.sequon || !(b.redundant === null) || b.selfpeptide) x2 -= 1000;
return x1-x2;
}
function compareKmerAlphabet(a,b)
{
let x = 0;
if (a.title < b.title) x = 1;
if (a.title > b.title) x = -1;
let x1 = 0;
let x2 = 0;
if (a.sequon || !(a.redundant === null) || a.selfpeptide) x1 -= 1000;
if (b.sequon || !(b.redundant === null) || b.selfpeptide) x2 -= 1000;
return x+x1-x2;
}
function sortCandidates(f)
{
if (processing || init) return;
startProcessing();
candidateScroll.elements.sort(f);
drawScroll(candidateCanvasCtx, candidateCanvas, candidateScroll);
endProcessing()
}
function updateDesign(state, pvec, scrollObj, kmer)
{
state.updateDistr(pvec);
state.remove(kmer)
for (let i = 0; i < state.kmers.length; i++){
if (levenshtein(kmer, state.kmers[i].kmer) <= global_levenshtein_parameter) state.kmers[i].redundant = kmer;
}
const utilityScores = calculateUtility(state);
forgetOverviews();
scrollObj.elements = [];
for (let i = 0; i < state.kmers.length; i++){
const pvec = state.kmers[i].scores;
const kmer = state.kmers[i].kmer;
const scrollElement = {
pvec : pvec,
title: kmer,
score: utilityScores[i],
sequon: state.kmers[i].sequon,
redundant: state.kmers[i].redundant,
selfpeptide: state.kmers[i].selfpeptide,
onAdd: () => {
startProcessing();
updateDesign(state, pvec, candidateScroll, kmer);
scrollObj.draw2();
endProcessing();
}
}
scrollElement.getscore = () =>
{
if (scrollElement.score === null) {
scrollElement.score = calculateOneUtility(state, pvec);
}
return scrollElement.score;
}
scrollObj.elements.push(scrollElement);
}
scrollObj.elements.sort( compareKmerScore );
if (scrollObj.elements.length > 0) scrollObj.selected = scrollObj.elements[scrollObj.elements.length-1];
else scrollObj.selected = null;
}
function textHasProblems(text)
{
for (let i = 0; i < text.length; i++)
{
if (codeChar(text[i]) < 0) return true;
}
return false;
}
function uploadDirect(n, procfunc, getPopulation, lvparam, mhcload)
{
if (processing) return;
if (directInput.value.length < 9 && mhcload == 1)
{
alert("Sequence must be at least length 9 for Class I MHC design.");
return;
}if (directInput.value.length < 15 && mhcload == 2)
{
alert("Sequence must be at least length 15 for Class I MHC design.");
return;
}
if (textHasProblems(directInput.value))
{
alert("Input sequence must consist of only the 20 letters of the amino acid alphabet (only ACDEFGHIKLMNPQRSTVWY)");
return;
}
//uploadDummy();
//return;
startProcessing();
loadHumanProteome(
() =>
{
startLoadMhc("mhc"+mhcload+".png", mhcload,
() =>
{
clipboardButton.textContent = "Copy to clipboard";
global_levenshtein_parameter = lvparam;
const text = [["Input", directInput.value]];
const kmers = processInputs(text, n, procfunc);
const population = getPopulation(); // numbers, names
uploadInput(kmers, population)
endProcessing();
});
}
);
}
var zoominfunc2 = () => {};
var zoomoutfunc2 = () => {};
function zoominfunc(){zoominfunc2()};
function zoomoutfunc(){zoomoutfunc2()};
function uploadInput(kmers, population)
{
startProcessing();
candidateScroll = {
elements: [],
y: 0,
entryheight: 32,
active: false,
activeOffset: 0,
scrollbarWidth: 16,
selected: null,
draw2 : () => {}
}
designScroll = {
elements: [],
y: 0,
entryheight: 32,
active: false,
activeOffset: 0,
scrollbarWidth: 16,
selected: -1,
draw2 : () => {}
}
//let text = directInput.value;
//let kmers = processInputs([["Input", text]], 9, dummyFunc);
//let population = mockPopulation(100); // numbers, names
let state = makeGlobalState(kmers, population[0], population[1]);
const utilityScores = calculateUtility(state);
const scrollObj = candidateScroll;
for (let i = 0; i < state.kmers.length; i++){
const pvec = state.kmers[i].scores;
const kmer = state.kmers[i].kmer;
const scrollElement = {
pvec : pvec,
title: kmer,
score: utilityScores[i],
sequon: state.kmers[i].sequon,
redundant: state.kmers[i].redundant,
selfpeptide: state.kmers[i].selfpeptide,
onAdd: () => {
startProcessing();
updateDesign(state, pvec, candidateScroll, kmer);
scrollObj.draw2();
endProcessing();
}
}
scrollElement.getscore = () =>
{
if (scrollElement.score === null) {
scrollElement.score = calculateOneUtility(state, pvec);
}
return scrollElement.score;
}
scrollObj.elements.push(scrollElement);
}
scrollObj.elements.sort( compareKmerScore );
if (scrollObj.elements.length > 0) scrollObj.selected = scrollObj.elements[scrollObj.elements.length-1];
else scrollObj.selected = null;
initScrollCanvas(candidateCanvasCtx, candidateCanvas, candidateScroll);
initScrollCanvas(designCanvasCtx, designCanvas, designScroll);
initUtilityCanvas(state, utilityCanvas, utilityCanvasCtx);
const hlaCanvasObjects = initHLACanvas(state, overviewCanvas, overviewCanvasCtx);
initDistrCanvas(state, hlaCanvasObjects[1], candidateScroll, distrCanvas, distrCanvasCtx);
// Allow HLA panel to be updated globally
forgetOverviews = () => {
hlaCanvasObjects[0]();
}
// zoom buttons
//document.getElementById("zoominbutton").addEventListener("click", hlaCanvasObjects[1].zoomIn);
//document.getElementById("zoomoutbutton").addEventListener("click", hlaCanvasObjects[1].zoomOut);
zoominfunc2 = hlaCanvasObjects[1].zoomIn;
zoomoutfunc2 = hlaCanvasObjects[1].zoomOut;
slideInCards();
init = false;
endProcessing();
}
function uploadDummy()
{
startProcessing();
candidateScroll = {
elements: [],
y: 0,
entryheight: 32,
active: false,
activeOffset: 0,
scrollbarWidth: 16,
selected: null,
draw2 : () => {}
}
designScroll = {
elements: [],
y: 0,
entryheight: 32,
active: false,
activeOffset: 0,
scrollbarWidth: 16,
selected: -1,
draw2 : () => {}
}
//let text = directInput.value;
//let kmers = processInputs([["Input", text]], 9, dummyFunc);
//let population = mockPopulation(100); // numbers, names
let state = makeGlobalState([],[0.5, 0.2, 0.2, 0.1],["A", "B", "C", "D"]);
initScrollCanvas(candidateCanvasCtx, candidateCanvas, candidateScroll);
initScrollCanvas(designCanvasCtx, designCanvas, designScroll);
initUtilityCanvas(state, utilityCanvas, utilityCanvasCtx);
const hlaCanvasObjects = initHLACanvas(state, overviewCanvas, overviewCanvasCtx);
initDistrCanvas(state, hlaCanvasObjects[1], candidateScroll, distrCanvas, distrCanvasCtx);
// Allow HLA panel to be updated globally
forgetOverviews = () => {
hlaCanvasObjects[0]();
}
// zoom buttons
//document.getElementById("zoominbutton").addEventListener("click", hlaCanvasObjects[1].zoomIn);
//document.getElementById("zoomoutbutton").addEventListener("click", hlaCanvasObjects[1].zoomOut);
zoominfunc2 = hlaCanvasObjects[1].zoomIn;
zoomoutfunc2 = hlaCanvasObjects[1].zoomOut;
slideInCards();
init = false;
endProcessing();
}