-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathConversion.js
121 lines (112 loc) · 5.57 KB
/
Conversion.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
// Keep the require for local testing using node path/to/Conversion.js or can use npm start
// libantimony = require('./libantimony.js');
var sbmlResult = "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; //
/**
* @description Load LibAntimonyJs and use the library's functions to convert Antimony to SBML
*/
function processAntimony() {
// Grab antimonyString global variable from AntimonyEditor to get ant model string
let antimonyString = window.antimonyString;
const event = new CustomEvent('grabbedSBMLResult', { detail: window.sbmlResult });
try {
libantimony().then((libantimony) => {
// Load LibAntimonyJs
// Format: libantimony.cwrap( function name, return type, input param array of types).
loadString = libantimony.cwrap('loadString', 'number', ['string']);
loadAntimonyString = libantimony.cwrap('loadAntimonyString', 'number', ['string']);
loadSBMLString = libantimony.cwrap('loadSBMLString', 'number', ['string']);
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']);
jsAllocateUTF8 = (newStr) => libantimony.allocateUTF8(newStr);
jsUTF8ToString = (strPtr) => libantimony.UTF8ToString(strPtr);
jsFree = (strPtr) => libantimony._free(strPtr);
// Load Antimony string to the library
var ptrAntCode = jsAllocateUTF8(antimonyString);
var load_int = loadAntimonyString(antimonyString);
// If Antimony string has no errors, grab sbml string and save to sbmlString global variable.
if (load_int > 0) {
sbmlResult = getSBMLString();
window.sbmlResult = sbmlResult;
window.dispatchEvent(event);
} else {
var errStr = getLastError();
window.alert(errStr);
}
jsFree(ptrAntCode);
});
} catch (err) {
console.log("Load libantimony error: ", err);
}
}
function processSBML() {
// Grab antimonyString global variable from AntimonyEditor to get ant model string
let sbmlString = window.sbmlString;
const event = new CustomEvent('grabbedAntimonyResult', { detail: window.antimonyString });
try {
libantimony().then((libantimony) => {
// Load LibAntimonyJs
// Format: libantimony.cwrap( function name, return type, input param array of types).
loadString = libantimony.cwrap('loadString', 'number', ['string']);
loadAntimonyString = libantimony.cwrap('loadAntimonyString', 'number', ['string']);
loadSBMLString = libantimony.cwrap('loadSBMLString', 'number', ['string']);
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']);
jsAllocateUTF8 = (newStr) => libantimony.allocateUTF8(newStr);
jsUTF8ToString = (strPtr) => libantimony.UTF8ToString(strPtr);
jsFree = (strPtr) => libantimony._free(strPtr);
sbmlCode = sbmlString;
clearPreviousLoads();
var ptrSBMLCode = jsAllocateUTF8(sbmlCode);
var load_int = loadSBMLString(sbmlCode);
if (load_int > 0) {
antResult = getAntimonyString();
if (window.conversion == "biomodels") {
citation = (window.citation == null) ? "// No citation provided by PubMed" : "// Citation: " + window.citation;
antResult = "// Link to the paper: " + window.url + "\n" + "// Link to BioModels: " + window.biomodelsUrl + "\n" +
"// Title: " + window.title + "\n" + "// Authors: " + window.authors + "\n" +
"// Journal: " + window.journal + "\n" + citation + "\n" + "// Date: " + window.date + "\n" + antResult
window.conversion = "standard";
}
window.antimonyResult = antResult;
window.dispatchEvent(event);
} else {
var errStr = getLastError();
window.alert(errStr);
}
jsFree(ptrSBMLCode);
});
} catch (err) {
console.log("Load libantimony error: ", err);
}
}
// Save processAntimony and processSBML function as global function to be used in AntimonyEditor. THIS IS IMPORTANT DO NOT REMOVE.
window.processAntimony = processAntimony;
window.processSBML = processSBML;