forked from C3BI-pasteur-fr/Cassandre
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrouter.js
388 lines (315 loc) · 11.5 KB
/
router.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
var express = require('express');
var bodyParser = require('body-parser');
var multer = require('multer');
var async = require('async');
var parseFile = require('./lib/parseFile');
var rowsToCells = require('./lib/rowsToCells');
module.exports = function (app, db) {
// CONGIGURATION
// =========================================================================
// Multer middleware to handle file uploads
var storage = multer.diskStorage({
destination: './uploads/',
filename: function (req, file, callback) {
return callback(null, file.originalname + '-' + Date.now());
}
});
var upload = multer({
storage: storage
// fileFilter
// limits
});
//var datasetsHandler = upload.single('dataset');
var datasetsHandler = upload.fields([
{ name: 'dataset', maxCount: 1},
{ name: 'metadata', maxCount: 1},
]);
var annotHandler = upload.single('annotations');
// Database collections
var data = db.collection('data');
var datasets = db.collection('datasets');
var annotations = db.collection('annotations');
// ROUTES
// =========================================================================
app.route('/api/stats')
// Get the numbers of datasets, experiments and genes in the database
.get(function (req, res) {
// Aggregation pipeline
var pipeline = [{
$group: {
_id: null,
datasets: { $addToSet: '$set'},
exps: { $addToSet: '$exp'},
genes: { $addToSet: '$gene'}
}
}, {
$project: {
_id: false,
datasets: { $size: '$datasets'},
exps: { $size: '$exps'},
genes : { $size: '$genes'}
}
}];
// Add another stage before the others to filter unrequested datasets
if (req.query.datasets) {
pipeline.unshift({
$match: {
set: { $in: [].concat(req.query.datasets) }
}
});
}
data.aggregate(pipeline, function (err, results) {
if (err) {
return res.status(500).send('Error with the database : ' + err.message);
}
return res.status(200).send(results[0]);
});
})
// =========================================================================
app.route('/api/datasets')
// Get the list of datasets
.get(function (req, res) {
datasets.find().toArray(function (err, list) {
if (err) {
return res.status(500).send('Error with the database : ' + err.message);
}
return res.status(200).send(list);
});
})
// Insert the data file into the database
.post(datasetsHandler, function (req, res) {
var datafile = req.files.dataset[0];
var metafile = req.files.metadata ? req.files.metadata[0] : null;
async.waterfall([
// Read The metadata file if exists
function (mainCallback) {
if (!metafile) {
return mainCallback();
}
parseFile(metafile, function (err, rows) {
if (err) {
err.httpCode = 400;
return mainCallback(err);
}
var metadata = {};
// Turn the rows into a single object
async.each(rows, function (row, callback) {
metadata[row.ID] = row;
delete metadata[row.ID]['ID'];
callback();
}, function () {
return mainCallback(null, metadata);
});
});
},
// Read the dataset
function (metadata, mainCallback) {
mainCallback = arguments.length === 2 ? mainCallback : metadata;
parseFile(datafile, function (err, dataset) {
if (err) {
err.httpCode = 400;
return mainCallback(err);
}
return mainCallback(null, metadata, dataset);
});
},
// Check the compatibility between metadata and dataset
function (metadata, dataset, mainCallback) {
if (!metadata) {
return mainCallback(null, null, dataset);
}
return mainCallback(null, metadata, dataset);
},
// Insert the datasets information and its metadata
function (metadata, dataset, mainCallback) {
datasets.insertOne({
name: req.body.name,
description: req.body.description,
hidden: false,
postedDate: new Date(),
metadata: metadata
}, function (err) {
if (err) {
if (err.name === 'MongoError') {
err.httpCode = 400;
err.message = "A dataset with this name already exists.";
return mainCallback(err);
}
return mainCallback(err);
}
return mainCallback(null, dataset);
});
},
// Insert the dataset, turn every row into cells before insertion
function (dataset, mainCallback) {
data.insertMany(rowsToCells(dataset, req.body.name), function (err) {
if (err) {
datasets.deleteOne({ name: req.body.name });
err.httpCode = 500;
return mainCallback(err);
}
////// REMOVE FILE HERE /////////////////////////
return mainCallback(null);
});
}
], function (err, result) {
if (err) {
return res.status(err.httpCode).send(err.message);
}
return res.status(201).send({ name: req.body.name });
});
})
// Update datasets informations
.put(function (req, res, next) {
datasets.update({
name: decodeURIComponent(req.query.name)
}, {
$set: req.body
}, function (err) {
if (err) {
return next(err);
}
return next();
});
},
// Also update the data collection if a dataset name changes
function (req, res, next) {
if (req.query.name === req.body.name) {
return res.sendStatus(204);
}
data.updateMany({
set: decodeURIComponent(req.query.name)
}, {
$set: { set: req.body.name }
}, function (err) {
if (err) {
return next(err);
}
return res.sendStatus(204);
});
},
// Error handler
function (err, req, res, next) {
if (err.name === 'MongoError') {
return res.status(400).send("A dataset with this name already exists.");
}
return res.status(500).send(err.message);
})
// Remove the given datasets from the database
.delete(function (req, res) {
var dataset = decodeURIComponent(req.query.name);
datasets.remove({
name: dataset
}, function (err) {
if (err) {
return res.status(500).send(err.message);
}
data.remove({
set: dataset
}, function (err) {
if (err) {
return res.status(500).send(err.message);
}
return res.sendStatus(204);
});
});
});
// =========================================================================
app.route('/api/annotations/')
// Get all the annotations
.get(function(req, res) {
var list = {};
annotations.find().project({ _id: false }).each(function (err, annotation) {
if (err) {
return res.status(500).send('Error with the database : ' + err.message);
}
if (annotation === null) {
return res.status(200).send(list);
}
// Turn all the annotations into a single object
list[annotation.ID] = annotation;
delete list[annotation.ID]['ID'];
});
})
// Insert the general annotations file into the database
.post(annotHandler, function (req, res) {
parseFile(req.file, function (err, rows) {
if (err) {
return res.status(400).send(err.message);
}
annotations.insertMany(rows, function (err) {
if (err) {
return res.status(500).send(err.message);
}
return res.sendStatus(201);
});
});
})
// Remove annotations from the database
.delete(function (req, res) {
annotations.deleteMany({}, function (err) {
if (err) {
return res.status(500).send(err.message);
}
return res.sendStatus(204);
})
});
// =========================================================================
app.route('/api/data/exp/')
// List all the experiments (columns) for given datasets
.get(function (req, res, next) {
var query = {};
if (req.query.sets) {
query.set = {
'$in' : decodeURIComponent(req.query.sets).split(',')
};
}
data.distinct('exp', query, function (err, list) {
if (err) {
return res.status(500).send('Error with the database : ' + err.message);
}
return res.status(200).send(list);
});
});
// =========================================================================
app.route('/api/data/genes/')
// List all the genes (lines) for given datasets
.get(function (req, res, next) {
var query = {};
if (req.query.sets) {
query.set = {
'$in' : decodeURIComponent(req.query.sets).split(',')
};
}
data.distinct('gene', query, function (err, list) {
if (err) {
return res.status(500).send('Error with the database : ' + err.message);
}
return res.status(200).send(list);
});
});
// =========================================================================
app.route('/api/data/:sets')
// Get the values for given datasets, possibly filtered by lines and/or columns
.get(function (req, res) {
var query = {
'set': {
'$in' : decodeURIComponent(req.params.sets).split(',')
}
};
if (req.query.genes){
var genes = typeof req.query.genes == 'string' ? [req.query.genes] : req.query.genes;
query['gene'] = { '$in': genes };
}
if (req.query.exps){
var exps = typeof req.query.exps == 'string' ? [req.query.exps] : req.query.exps;
query['exp'] = { '$in': exps };
}
data.find(query).toArray(function (err, list) {
if (err) {
return res.status(500).send('Error with the database : ' + err.message);
}
return res.status(200).send(list);
});
});
};