-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathserver.js
457 lines (404 loc) · 12.3 KB
/
server.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
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
var express = require("express");
var cors = require("cors");
var compression = require("compression");
var app = express();
var fs = require("fs");
const path = require("node:path");
const os = require("node:os");
var https = require("https");
var axios = require("axios");
var pako = require("pako");
var importing;
var filtering;
var exporting;
const { program } = require("commander");
program
.option("--ssl", "use ssl")
.option("--port <port>", "port", 8000)
.option("--config_json <config_json>", "config json")
.option("--data_url <data url>", "data url")
.option(
"--data_file <data file>",
"local data file, as alternative to data url"
);
program.parse();
const command_options = program.opts();
const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), "taxonium"));
const in_cache = new Set();
const cache_helper = {
retrieve_from_cache: (key) => {
console.log("retrieving ", key);
if (!in_cache.has(key)) {
console.log("not found");
return undefined;
} else {
// get from tmpDir, parsing the JSON
console.log("found");
const retrieved = JSON.parse(fs.readFileSync(path.join(tmpDir, key)));
return retrieved;
}
},
store_in_cache: (key, value) => {
console.log("caching ", key);
// store in tmpDir, serializing the JSON
fs.writeFileSync(path.join(tmpDir, key), JSON.stringify(value));
in_cache.add(key);
},
};
// Either data_url or data_file must be defined, if not display error
if (
command_options.data_url === undefined &&
command_options.data_file === undefined
) {
console.log("--data_url or --data_file must be supplied");
process.exit(1);
}
import("taxonium_data_handling/importing.js").then((imported) => {
importing = imported.default;
console.log("imported importing");
console.log("importing is ", importing);
});
import("taxonium_data_handling/filtering.js").then((imported) => {
filtering = imported.default;
console.log("imported filtering");
});
import("taxonium_data_handling/exporting.js").then((imported) => {
exporting = imported.default;
console.log("imported exporting");
});
waitForTheImports = async () => {
if (importing === undefined || filtering === undefined) {
await new Promise((resolve) => {
const interval = setInterval(() => {
if (importing !== undefined && filtering !== undefined) {
clearInterval(interval);
resolve();
}
}, 100);
});
}
};
var processedData = null;
var cached_starting_values = null;
let options;
app.use(cors());
app.use(compression());
const logStatusMessage = (status_obj) => {
console.log("status", status_obj);
};
app.get("/", function (req, res) {
res.send("Hello World, Taxonium is here!");
});
app.get("/search", function (req, res) {
const start_time = Date.now();
console.log("/search");
const json = req.query.json;
const spec = JSON.parse(JSON.parse(json));
console.log(spec);
const minYbound =
req.query.min_y !== undefined ? req.query.min_y : processedData.overallMinY;
const maxYbound =
req.query.max_y !== undefined ? req.query.max_y : processedData.overallMaxY;
const minXbound =
req.query.min_x !== undefined ? req.query.min_x : processedData.overallMinX;
const maxXbound =
req.query.max_x !== undefined ? req.query.max_x : processedData.overallMaxX;
const forSingleSearch = {
data: processedData.nodes,
spec,
min_y: minYbound,
max_y: maxYbound,
min_x: minXbound,
max_x: maxXbound,
y_positions: processedData.y_positions,
mutations: processedData.mutations,
node_to_mut: processedData.node_to_mut,
xType: req.query.xType,
cache_helper: cache_helper,
};
const result = filtering.singleSearch(forSingleSearch);
validateSIDandSend(result, req.query.sid, res);
console.log(
"Found " +
result.data.length +
" results in " +
(Date.now() - start_time) +
"ms"
);
console.log("Result type was " + result.type);
});
const path_for_config = command_options.config_json;
// check if path exists
let config;
if (path_for_config && fs.existsSync(path_for_config)) {
config = JSON.parse(fs.readFileSync(path_for_config));
} else {
config = { title: "", source: "" };
}
app.get("/config", function (req, res) {
config.num_nodes = processedData.nodes.length;
config.initial_x =
(processedData.overallMinX + processedData.overallMaxX) / 2;
config.initial_y =
(processedData.overallMinY + processedData.overallMaxY) / 2;
config.initial_zoom = -2;
config.genes = processedData.genes;
config.mutations = processedData.mutations;
config = { ...config, ...processedData.overwrite_config };
config.rootMutations = processedData.rootMutations;
config.rootId = processedData.rootId;
validateSIDandSend(config, req.query.sid, res);
});
app.get("/nodes/", function (req, res) {
const start_time = Date.now();
let min_y =
req.query.min_y !== undefined ? req.query.min_y : processedData.overallMinY;
let max_y =
req.query.max_y !== undefined ? req.query.max_y : processedData.overallMaxY;
let min_x =
req.query.min_x !== undefined ? req.query.min_x : processedData.overallMinX;
let max_x =
req.query.max_x !== undefined ? req.query.max_x : processedData.overallMaxX;
if (min_y < processedData.overallMinY) {
min_y = processedData.overallMinY;
}
if (max_y > processedData.overallMaxY) {
max_y = processedData.overallMaxY;
}
if (min_x < processedData.overallMinX) {
min_x = processedData.overallMinX;
}
if (max_x > processedData.overallMaxX) {
max_x = processedData.overallMaxX;
}
let result;
if (
min_y === processedData.overallMinY &&
max_y === processedData.overallMaxY &&
min_x === processedData.overallMinX &&
max_x === processedData.overallMaxX &&
req.query.xType === "x_dist"
) {
result = cached_starting_values;
console.log("Using cached values");
} else {
result = filtering.getNodes(
processedData.nodes,
processedData.y_positions,
min_y,
max_y,
min_x,
max_x,
req.query.xType
);
}
console.log("Ready to send after " + (Date.now() - start_time) + "ms.");
// This will be sent as json
validateSIDandSend({ nodes: result }, req.query.sid, res);
console.log(
"Request took " +
(Date.now() - start_time) +
"ms, and output " +
result.length +
" nodes."
);
});
function startListening() {
if (command_options.ssl) {
options = {
key: fs.readFileSync(
"/etc/letsencrypt/live/api.taxonium.org/privkey.pem"
),
ca: fs.readFileSync("/etc/letsencrypt/live/api.taxonium.org/chain.pem"),
cert: fs.readFileSync(
"/etc/letsencrypt/live/api.taxonium.org/fullchain.pem"
),
};
https.createServer(options, app).listen(command_options.port, "0.0.0.0");
console.log("SSL on port " + command_options.port);
} else {
app.listen(command_options.port, "0.0.0.0");
console.log("Non SSL on port " + command_options.port);
}
}
let sid_cache = {};
async function validateSID(sid) {
/*
Create a call to https://gpsapi.epicov.org/epi3/gps_api
with URL encoded version of the following parameters:
{"cmd":"state/session/validate",
"client_id": "TEST-1234",
"api": {"version":1},
"sid":"RFWFYY...PQZNQQASXUR"}
packaged as req
*/
const caching_time = 1000 * 60 * 5; // 5 minutes
if (
sid_cache[sid] !== undefined &&
sid_cache[sid].time > Date.now() - caching_time &&
sid_cache[sid].validity === "ok"
) {
console.log("Using cached validity");
return "ok";
}
const key = process.env.GPS_API_KEY;
const req_obj = {
cmd: "state/session/validate",
client_id: key,
api: { version: 1 },
sid: sid,
};
const req_raw = JSON.stringify(req_obj);
const req = encodeURIComponent(req_raw);
const url = "https://gpsapi.epicov.org/epi3/gps_api?req=" + req;
console.log(url);
response = await axios.get(url);
console.log("got response", response.data);
validity =
response.data && response.data.rc && response.data.rc === "ok"
? "ok"
: "invalid";
console.log("validity", validity);
sid_cache[sid] = { validity: validity, time: Date.now() };
return validity;
}
async function validateSIDandSend(to_send, sid, res) {
if (!config.validate_SID) {
res.send(to_send);
return;
}
const validity = await validateSID(sid);
if (validity === "ok") {
res.send(to_send);
} else {
res.send({ error: "Invalid session ID" });
}
}
app.get("/validate/", async function (req, res) {
const start_time = new Date();
const query_sid = req.query.sid;
const validity = await validateSID(query_sid);
console.log("Got validity", validity);
res.send(validity);
console.log(new Date() - start_time);
});
// "Takes EPI_ISL_12345" input
function get_epi_isl_url(epi_isl) {
if (epi_isl.length > 4) {
return (
"https://www.epicov.org/acknowledgement/" +
epi_isl.slice(-4, -2) +
"/" +
epi_isl.slice(-2) +
"/" +
epi_isl +
".json"
);
}
}
app.get("/node_details/", async (req, res) => {
const start_time = Date.now();
const query_id = req.query.id;
const node = processedData.nodes[query_id];
const node_mutations = processedData.node_to_mut[query_id].map((mutation) => {
return processedData.mutations[mutation];
});
const detailed_node = { ...node, mutations: node_mutations };
// If node name starts with EPI_ISL_, then get the URL
if (detailed_node.name.startsWith("EPI_ISL_")) {
const acknowledgements_url = get_epi_isl_url(detailed_node.name);
// get the data from the URL
const response = await axios.get(acknowledgements_url).catch((e) => {
console.log(e);
});
try {
const data = response.data;
detailed_node.acknowledgements = data;
} catch (e) {
detailed_node.acknowledgements = {
covv_orig_lab:
"The GISAID acknowledgements server did not return a valid response",
covv_orig_lab:
"The GISAID acknowledgements server did not return a valid response",
covv_authors:
"The GISAID acknowledgements server did not return a valid response",
covv_subm_lab:
"The GISAID acknowledgements server did not return a valid response",
};
console.log(e);
}
}
validateSIDandSend(detailed_node, req.query.sid, res);
console.log(
"Request took " + (Date.now() - start_time) + "ms, and output " + node
);
});
app.get("/tip_atts", async (req, res) => {
const start_time = Date.now();
const node_id = req.query.id;
const att = req.query.att;
const atts = filtering.getTipAtts(processedData.nodes, node_id, att);
validateSIDandSend(atts, req.query.sid, res);
console.log(
"Request took " + (Date.now() - start_time) + "ms, and output " + atts
);
});
// match /nextstrain_json/12345
app.get("/nextstrain_json/:root_id", async (req, res) => {
const root_id = parseInt(req.params.root_id);
const json = await exporting.getNextstrainSubtreeJson(
root_id,
processedData.nodes,
config
);
res.setHeader(
"Content-Disposition",
"attachment; " + "filename=" + root_id + ".nextstrain.json"
);
res.send(json);
});
const loadData = async () => {
await waitForTheImports();
let supplied_object;
if (command_options.data_file) {
local_file = command_options.data_file;
// local_file = "tfci.jsonl";
// Read as bytes
const file_data = fs.readFileSync(local_file);
supplied_object = {
data: file_data,
status: "loaded",
filename: local_file,
};
} else {
url = command_options.data_url;
supplied_object = { status: "url_supplied", filename: url };
}
processedData = await importing.processJsonl(
supplied_object,
logStatusMessage
);
processedData.genes = new Set(
processedData.mutations.map((mutation) => mutation.gene)
);
// as array
processedData.genes = Array.from(processedData.genes);
console.log("Loaded data");
result = filtering.getNodes(
processedData.nodes,
processedData.y_positions,
processedData.overallMinY,
processedData.overallMaxY,
processedData.overallMinX,
processedData.overallMaxX,
"x_dist"
);
cached_starting_values = result;
console.log("Saved cached starting vals");
// set a timeout to start listening
setTimeout(() => {
console.log("Starting to listen");
startListening();
}, 10);
};
loadData();