-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.js
352 lines (311 loc) · 11.7 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
import {searchModels, getModel} from "./buildBiomodelsSearch/getBiomodels.js";
let { log } = console;
let models = [];
//const maxRec = 15; currently Not used
const makeSBMLversion = "MakeSBML version 1.6.1 ";
const makeSBMLinfo = makeSBMLversion + "\nCopyright 2023-24, Bartholomew Jardine and Herbert M. Sauro,\nUniversity of Washington, USA.\nSpecial thanks to University of Washington student Tracy Chan for her assistance with this software.\n\nThis project was funded by NIH/NIGMS (R01GM123032 and P41EB023912).";
var antCode;
var sbmlCode;
var sbmlResult = "None";
var antResult = "None";
var loadAntimonyString; // libantimony function
var loadString; // "
var loadSBMLString; // "
var getSBMLString; // "
var getAntimonyString; // "
var getCompSBMLString; // "
var clearPreviousLoads; // "
var getLastError; // "
var getWarnings; // "
var getSBMLInfoMessages; // "
var getSBMLWarnings; // "
var freeAll; // "
var jsFree; // emscripten function
var jsAllocateUTF8; // "
const inputFile = document.getElementById("inputfile")
const saveAntimonyBtn = document.getElementById("saveAntimonyBtn");
const copyAntimonyBtn = document.getElementById("copyAntimonyBtn");
const procAntimonyBtn = document.getElementById("procAntimonyBtn");
const procSBMLBtn = document.getElementById("procSBMLBtn");
const aboutBtn = document.getElementById("aboutBtn");
//const xmlDownloadButton = document.querySelector("#xml-download-wrapper button")
const xmlImportButton = document.querySelector("#xml-import-wrapper button")
const xmlRecList1Loader = document.getElementById("loader-list1");
const xmlDownloadInput = document.getElementById("xml-id-search-input1");
const xmlRecList1 = document.getElementById("xml-1-rec");
const saveSBMLBtn = document.getElementById("saveSBMLBtn");
const copySBMLBtn = document.getElementById("copySBMLBtn");
const rec1Wrapper = document.getElementById("rec1-wrapper");
const antTextArea = document.getElementById("antimonycode");
const sbmlTextArea = document.getElementById("sbmlcode");
window.onload = function() {
initLoad();
saveAntimonyBtn.addEventListener("click", (_) => saveCode("antimony"));
copyAntimonyBtn.addEventListener("click", (_) => copyToClipboard("antimony"));
procAntimonyBtn.addEventListener("click", processAntimony);
procSBMLBtn.addEventListener("click", processSBML);
const createRecItem = (id_nameMap, onclickEvent) => {
const itr = id_nameMap.values();
const id = itr.next().value;
const name = itr.next().value.name;
const maxNameLength = 50;
let li = document.createElement("li");
let a = document.createElement("a");
a.addEventListener("click", onclickEvent);
a.innerText = name.slice(0, maxNameLength);
if (name.length > maxNameLength) a.innerText += "...";
a.innerText += `: ${id}`;
li.append(a);
return li;
};
xmlDownloadInput.addEventListener("click", (e) => {
e.preventDefault();
xmlRecList1.style.display = "block"; // allow list of models to be displayed below input box.
e.stopPropagation();
});
xmlDownloadInput.addEventListener("mousedown", (event) => {
xmlRecList1.style.display = "block";
});
/*xmlDownloadInput.addEventListener("mouseup", (event) => {
xmlRecList1Loader.classList.remove("showLoader");
}); */
function delay(fn, ms) { // use to delay event from triggering.
let timer = 0
return function(...args) {
clearTimeout(timer)
timer = setTimeout(fn.bind(this, ...args), ms || 0)
}
}
async function processkeySearch(e) {
const searchText = e.target.value.trim();
if (searchText.length < 3) { // 2 chars before start search
xmlRecList1.innerHTML = "";
return;
}
xmlRecList1Loader.classList.add("showLoader");
let recommends = await getBiomodelsInfo(searchText);
const handleSelection = (e) => {
e.preventDefault();
const text = e.target.innerText;
xmlDownloadInput.value = text.split(": ").slice(-1);
document.getElementById("sbmlcode").value = '[SBML code here.]'; // Clear out old model
handleDownloadModel(); // view biomodel that user selected.
};
let recommendMap = recommends.models;
if (recommendMap?.entries()) {
var numb = 0;
xmlRecList1.innerHTML = "";
// recommendMap = recommendMap?.slice(0, maxRec); // grab the first maxRec entries
for (const rec of recommendMap) {
// Chk if id starts with 'BIOMD' : implies model has been curated
const itr = rec.values();
const id = itr.next().value;
if(id.includes('BIOMD')) {
xmlRecList1.append(createRecItem(rec, handleSelection)); // rec -> one Map entry (id, name)
numb+=1;
}
}
}
};
// Do not start processing user search until at least 500 ms has elapsed.
// We want to reduce the number of searches and speed up populating result list.
xmlDownloadInput.addEventListener("keyup", async (e) => {
delay(processkeySearch(e), 500);});
saveSBMLBtn.addEventListener("click", (_) => saveCode("sbml"));
copySBMLBtn.addEventListener("click", (_) => copyToClipboard("sbml"));
aboutBtn.addEventListener("click", (_) => showAbout());
inputFile.addEventListener("change", function() {
var fr = new FileReader();
xmlRecList1Loader.classList.add("showLoader");
fr.onload = function() {
var modelString = fr.result;
processFile(modelString);
};
fr.readAsText(this.files[0]);
});
};
// Load library functions (asynchronous call):
function initLoad() {
try {
libantimony().then((libantimony) => {
// Format: libantimony.cwrap( function name, return type, input param array of types).
loadString = libantimony.cwrap("loadString", "number", ["number"]);
loadAntimonyString = libantimony.cwrap("loadAntimonyString", "number", [
"number",
]);
loadSBMLString = libantimony.cwrap("loadSBMLString", "number", [
"number",
]);
getSBMLString = libantimony.cwrap("getSBMLString", "string", ["null"]);
getAntimonyString = libantimony.cwrap("getAntimonyString", "string", [
"null",
]);
getCompSBMLString = libantimony.cwrap("getCompSBMLString", "string", [
"string",
]);
clearPreviousLoads = libantimony.cwrap("clearPreviousLoads", "null", [
"null",
]);
getLastError = libantimony.cwrap("getLastError", "string", ["null"]);
getWarnings = libantimony.cwrap("getWarnings", "string", ["null"]);
getSBMLInfoMessages = libantimony.cwrap("getSBMLInfoMessages", "string", [
"string",
]);
getSBMLWarnings = libantimony.cwrap("getSBMLWarnings", "string", [
"string",
]);
freeAll = libantimony.cwrap("freeAll", "null", ["null"]);
jsFree = (strPtr) => libantimony._free(strPtr);
jsAllocateUTF8 = (newStr) => libantimony.allocateUTF8(newStr);
});
} catch (err) {
console.log("Load libantimony error: ", err);
}
}
async function processAntimony() { // Generate SBML version.
antCode = document.getElementById("antimonycode").value;
clearPreviousLoads();
//console.log("*** Antimony code: ",antCode);
var ptrAntCode = jsAllocateUTF8(antCode);
var load_int = loadAntimonyString(ptrAntCode);
if (load_int > 0) {
sbmlResult = getSBMLString();
document.getElementById("sbmlcode").value = sbmlResult;
document.getElementById("procSBMLBtn").disabled = false;
document.getElementById("copySBMLBtn").disabled = false;
document.getElementById("saveSBMLBtn").disabled = false;
} else {
var errStr = getLastError();
window.alert(errStr);
}
jsFree(ptrAntCode);
}
async function processSBML() { // Generate Antimony version
sbmlCode = sbmlTextArea.value;
clearPreviousLoads();
var ptrSBMLCode = jsAllocateUTF8(sbmlCode);
var load_int = loadSBMLString(ptrSBMLCode);
//console.log("processSBML: int returned: ", load_int);
if (load_int > 0) {
antResult = getAntimonyString();
antTextArea.value = antResult;
procAntimonyBtn.disabled = false;
copyAntimonyBtn.disabled = false;
saveAntimonyBtn.disabled = false;
} else {
var errStr = getLastError();
window.alert(errStr);
}
jsFree(ptrSBMLCode);
}
async function processFile(fileStr) {
if(fileStr.length > 1000000){
alert('Model file is very large and may take a minute or more to process!');
}
try {
clearPreviousLoads;
var ptrFileStr = jsAllocateUTF8(fileStr);
if (loadAntimonyString(ptrFileStr) > 0) {
antTextArea.value = fileStr;
procSBMLBtn.disabled = true;
sbmlTextArea.value = "[SBML code here.]";
await processAntimony();
xmlRecList1Loader.classList.remove("showLoader");// Remove drop-down list of biomodels
// added in inputFile.addEventListener("change" )
} else if (loadSBMLString(ptrFileStr) > 0) {
sbmlTextArea.value = fileStr;
procAntimonyBtn.disabled = true;
antTextArea.value = "[Antimony code here.]";
await processSBML();
xmlRecList1Loader.classList.remove("showLoader");// added in inputFile.addEventListener("change" )
} else {
var errStr = getLastError();
window.alert(errStr);
clearPreviousLoads();
}
} catch (err) {
console.log("processing file error: :", err);
window.alert(err);
}
jsFree(ptrFileStr);
}
function copyToClipboard(copyType) {
var copyText;
if (copyType == "antimony") {
copyText = antTextArea;
} else {
copyText = sbmlTextArea;
}
// Select the text field
copyText.select();
copyText.setSelectionRange(0, 99999); // For mobile devices
// Copy the text inside the text field
navigator.clipboard.writeText(copyText.value);
}
function saveCode(codeType) {
var fileExt;
var promptFilename;
if (codeType == "antimony") {
fileExt = ".txt";
} else {
fileExt = ".xml";
}
if ((promptFilename = prompt("Save file as (" + fileExt + ") ", ""))) {
var textBlob;
if (codeType == "antimony") {
textBlob = new Blob([antTextArea.value], {
type: "text/plain",
});
} else
textBlob = new Blob([sbmlTextArea.value], {
type: "text/plain",
});
var downloadLink = document.createElement("a");
if( promptFilename.includes(fileExt) || promptFilename.includes(".sbml") ) {
downloadLink.download = promptFilename; }
else { downloadLink.download = promptFilename + fileExt; }
downloadLink.innerHTML = "Download File";
downloadLink.href = window.URL.createObjectURL(textBlob);
downloadLink.click();
}
}
// loads biomodels cache and returns models that match query
async function getBiomodelsInfo(query) {
let models;
xmlRecList1Loader.classList.remove("showLoader"); // Remove drop-down list of biomodels ??
return searchModels(query) //fetch(biomodelsInfoURL)
}
// Get URL of biomodel and then get model, calls getBiomodel.js ->getModel()
async function getModelFile(modelId) {
clearPreviousLoads;
await getModel(modelId)
.then((response) => {
const filename = response[0];
const sbmlStr = response[1];
sbmlTextArea.value = response[1];
processSBML(); // generate antimony version
xmlRecList1Loader.classList.remove("showLoader") // Remove drop-down list of biomodels
xmlRecList1.style.display = "none"; // Clear drop-down list of biomodels
})
.catch((err) => console.error(err));
}
async function getModelIdRecommendNew(query) {
xmlRecList1Loader.classList.add("showLoader") // Show drop-down list of biomodels
return getBiomodelsInfo(query);
}
async function handleDownloadModel() {
if (xmlDownloadInput.value.trim().length > 1) {
xmlRecList1Loader.classList.add("showLoader") // Show drop-down list of biomodels
try{
await getModelFile(xmlDownloadInput.value.trim()); // Download SBML and display.
}
catch(err) {
console.log('handleDownloadModel():', err);
const errorStr = modelId + ': NOT found!';
window.alert(errorStr);
}
}
}
async function showAbout() {
window.alert(makeSBMLinfo);
}