-
Notifications
You must be signed in to change notification settings - Fork 1
/
searchdb.html
319 lines (290 loc) · 12.3 KB
/
searchdb.html
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
<!doctype html>
<html lang="en-us">
<head>
<meta charset="utf-8">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Emscripten-Generated Code</title>
<!-- See installation instructions for kekule.js for ways to reduce file requirements: http://partridgejiang.github.io/Kekule.js/documents/tutorial/content/installation.html -->
<script src="kekule/kekule.min.js"></script>
<link rel="stylesheet" type="text/css" href="kekule/themes/default/kekule.css" />
<style>
/* See modal example and advice at https://css-tricks.com/considerations-styling-modal/ */
.modal {
display: block;
z-index: 100;
position: fixed;
/*left: 50%;*/
top: 50%;
/* transform: translate(-50%, -50%); */ /* Centers without knowing the width/height */
left: 5%;
transform: translate(0, -50%);
padding: 20px;
border: 3px solid #a7bb3c;
background: white;
}
.modal.closed {
display: none;
}
* + div {
margin-top: 1em;
}
</style>
<link href="mofid.css" rel="stylesheet" type="text/css">
<link rel="shortcut icon" href="favicon.ico">
<link rel="icon" href="favicon.ico">
</head>
<body>
<script type="text/javascript">
var runSearch = function(query, invert_selection) {
C_obcode = Module.cwrap(
'runSearchc', 'null', ['string', 'string', 'number', 'string']
);
var input_file = "/web_data/core.smi";
var results_file = "search_results.smi";
if (query != "") {
C_obcode(query, input_file, invert_selection, results_file);
} else {
results_file = input_file; // return everything
}
return FS.readFile(results_file, {'encoding': 'utf8'});
};
var parseMOFid = function(mofid) {
// Converted from run_mofid.py:parse_mofid
var mofid_parts = mofid.split(';');
var mofid_data = mofid_parts[0];
var mof_name = null;
if (mofid_parts.length > 1) {
mof_name = mofid_parts.slice(1).join(';');
}
var components = mofid_data.split(/\t| /);
if (components.length == 1) {
if (mofid_data.trimLeft() != mofid_data) { // empty SMILES: no MOF found
components.push(components[0]); // move metadata to the right
components[0] = '';
} else {
throw 'MOF metadata required';
}
} else if (components.length > 2) {
throw 'Bad MOFid containing extra spaces before the semicolon: ' + mofid;
}
var smiles = components[0];
var metadata = components[1].split('.');
var cat = null;
var topology = null;
for (var i=0; i<metadata.length; i++) {
var tag = metadata[i];
if (i == 0 && !tag.startsWith('MOFid')) {
throw 'MOFid-v1 must start with the correct tag';
}
if (i == 0 && tag.substr(5) != '-v1') {
throw 'Unsupported version of MOFid';
} else if (i == 1) {
topology = tag;
} else if (tag.toLowerCase().startsWith('cat')) {
cat = tag.slice(3); // end arg is optional. Extracts through the end
} // else: ignoring other MOFid tags, at least for now
}
return {
smiles: smiles,
topology: topology,
cat: cat,
mof_name: mof_name
};
}
var filterTopology = function(mofid, topology, invert) {
var mof_topology = parseMOFid(mofid).topology;
if (invert) {
return (mof_topology != topology);
} else {
return (mof_topology == topology);
}
}
var getInput = function(html_id) {
return document.getElementById(html_id).value.trim(); // strip whitespace at begin/end
}
var handleInput = function() {
var invertSearch = document.getElementById("inverseclick").checked==true;
var smartsQuery = getInput("smartsinput");
var rcsrQuery = getInput("rcsrinput");
var catQuery = getInput("catenation");
var matches_raw = runSearch(smartsQuery, invertSearch);
var matches = matches_raw.split(/\r\n|\r|\n/g).slice(0, -1); // matches end in a newline
if (rcsrQuery != "") {
matches = matches.filter(function(mofid) {
return filterTopology(mofid, rcsrQuery, false);
});
}
if (catQuery != "unused") {
if (catQuery == "null") {
catQuery = null;
}
matches = matches.filter(function(mofid) {
return parseMOFid(mofid).cat == catQuery;
});
}
document.getElementById("resultswrapper").classList.remove("inactive");
var numlines = matches.length;
matches = matches.join('\n'); // defaults to UNIX line endings, which is fine tbh
if (matches != "") {
var download_matches = "data:text/plain;base64," + btoa(matches);
var html_matches = "<a href=\"" + download_matches + "\" download>Download results</a>" + "<p>Found " + numlines + " matches.</p>" + "<pre>" + matches + "</pre>";
document.getElementById("obresults").innerHTML = html_matches;
} else {
document.getElementById("obresults").innerHTML = "<p>No matches found.</p>";
}
};
var transferQuery = function() {
//var kekule_mols = getComposer().exportObjs(Kekule.Molecule);
// Thankfully, getComposer is defined in partridgejiang.github.io/Kekule.js/documents/tutorial/examples/composer.html
var getComposer = function() {
return Kekule.Widget.getWidgetById('composer');
}
var kekule_mols = getComposer().exportObjs(Kekule.Molecule);
var drawn_structure = '';
if (kekule_mols.length > 1) {
alert('Too many molecules drawn.');
return false;
} else if (kekule_mols.length == 1) {
drawn_structure = Kekule.IO.saveFormatData(kekule_mols[0], 'smi');
} // else if zero, default of empty ''
document.getElementById("smartsinput").value = drawn_structure;
return true;
}
var openKekule = function() {
// show the modal box for kekule.js
var modal = document.getElementById('structure-modal');
modal.classList.toggle('closed');
}
var closeKekule = function() {
// close the modal box. Turns out we can toggle, just like open.
openKekule();
}
var transferQueryAndClose = function() {
if (transferQuery()) {
closeKekule();
}
}
</script>
<header>
<img src="mofid_logo.png" id="headerlogo" />
<h1>CoRE MOF search</h1>
<div id="loading">
<div class="spinner" id='spinner'></div>
<div class="emscripten" id="status">Downloading...</div>
<div class="emscripten">
<progress value="0" max="100" id="progress" hidden=1></progress>
</div>
</div>
</header>
<!--<div class="content">--><div>
<h3>Query parameters</h3>
<p>(blank fields are ignored)</p>
<p><b>Substructure:</b> Enter SMILES/SMARTS or <input type="submit" value="draw query" onclick="openKekule()"></input>
<input type="text" name="smartsinput" id="smartsinput"></input>
<input type="checkbox" name="opposite" id="inverseclick">Invert selection</input></p>
<p><b>RCSR topology:</b> <input type="text" name="rcsrinput" id="rcsrinput"></input></p>
<p>
<b>Catenation:</b>
<select id="catenation">
<option value="unused">(unused)</option>
<option value="0">Uncatenated</option>
<option value="1">2 nets total</option>
<option value="2">3 nets total</option>
<option value="3">4 nets total</option>
<option value="null">No MOFs identified</option>
</select>
</p>
<input type="submit" value="Submit" id="emrun" onclick="handleInput()" disabled></input>
</div>
<div class="modal closed" id="structure-modal">
<!-- See kekule.js documentation at http://partridgejiang.github.io/Kekule.js/documents/tutorial/content/composer.html -->
<h3>Substructure query</h3>
<div class="query-buttons" style="clear:both;margin-bottom:1em;">
<input type="submit" value="Accept query" id="transfer" onclick="transferQueryAndClose()"></input>
<input type="submit" value="Cancel" id="cancel-query" onclick="closeKekule()"></input>
</div>
<div id="composer" style="width:600px;height:400px;" data-widget="Kekule.Editor.Composer"></div>
</div>
<!--<div class="content inactive" id="resultswrapper">-->
<div class="inactive" id="resultswrapper">
<h3>Results</h3>
<span id="obresults">(Results show up here)</span>
</div>
<div id="results_table"></div>
<script type='text/javascript'>
var statusElement = document.getElementById('status');
var progressElement = document.getElementById('progress');
var spinnerElement = document.getElementById('spinner');
var Module = {
preRun: [],
postRun: [function() {document.getElementById("emrun").disabled = false;}],
print: (function() {
var element = document.getElementById('output');
if (element) element.value = ''; // clear browser cache
return function(text) {
if (arguments.length > 1) text = Array.prototype.slice.call(arguments).join(' ');
// These replacements are necessary if you render to raw HTML
//text = text.replace(/&/g, "&");
//text = text.replace(/</g, "<");
//text = text.replace(/>/g, ">");
//text = text.replace('\n', '<br>', 'g');
console.log(text);
if (element) {
element.value += text + "\n";
element.scrollTop = element.scrollHeight; // focus on bottom
}
};
})(),
printErr: function(text) {
if (arguments.length > 1) text = Array.prototype.slice.call(arguments).join(' ');
if (0) { // XXX disabled for safety typeof dump == 'function') {
dump(text + '\n'); // fast, straight to the real console
} else {
console.error(text);
}
},
canvas: (function() {
// As a default initial behavior, pop up an alert when webgl context is lost. To make your
// application robust, you may want to override this behavior before shipping!
// See http://www.khronos.org/registry/webgl/specs/latest/1.0/#5.15.2
return null;
})(),
setStatus: function(text) {
if (!Module.setStatus.last) Module.setStatus.last = { time: Date.now(), text: '' };
if (text === Module.setStatus.text) return;
var m = text.match(/([^(]+)\((\d+(\.\d+)?)\/(\d+)\)/);
var now = Date.now();
if (m && now - Date.now() < 30) return; // if this is a progress update, skip it if too soon
if (m) {
text = m[1];
progressElement.value = parseInt(m[2])*100;
progressElement.max = parseInt(m[4])*100;
progressElement.hidden = false;
spinnerElement.hidden = false;
} else {
progressElement.value = null;
progressElement.max = null;
progressElement.hidden = true;
if (!text) spinnerElement.style.display = 'none';
}
statusElement.innerHTML = text;
},
totalDependencies: 0,
monitorRunDependencies: function(left) {
this.totalDependencies = Math.max(this.totalDependencies, left);
Module.setStatus(left ? 'Preparing... (' + (this.totalDependencies-left) + '/' + this.totalDependencies + ')' : 'All downloads complete.');
}
};
Module.setStatus('Downloading...');
window.onerror = function(event) {
// TODO: do not warn on ok events like simulating an infinite loop or exitStatus
Module.setStatus('Exception thrown, see JavaScript console');
spinnerElement.style.display = 'none';
Module.setStatus = function(text) {
if (text) Module.printErr('[post-exception status] ' + text);
};
};
</script>
<script async type="text/javascript" src="searchdb.js"></script>
</body>
</html>