-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
102 lines (89 loc) · 3.02 KB
/
index.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
//
// Computing With Large Data Sets
// Visualization project
//
// December 2011
// @author Josh Leitzel <josh.leitzel@nyu.edu>
//
var http = require('http'),
_ = require('underscore')._,
childProcess = require('child_process'),
fs = require('fs'),
express = require('express');
// Constants
var GRAPHS_DIR = 'graphs/',
DATA_ROOT = 'data/',
DATA_USER = 'uploads', // relative to DATA_ROOT
SCRIPT_MAP = {
cluster : 'r/cluster.r',
regression : 'r/regression.r'
};
// Clear tmp directory
console.log('Clearing public/tmp/ directory...');
childProcess.exec('rm public/tmp/*.tar');
var server = express.createServer();
server.use(express.bodyParser())
.use(express.static(__dirname + '/public'))
server.get('/cleargraphs', function (req, res) {
console.log('Clearing graphs...');
childProcess.exec('rm public/graphs/*.png', function () {
childProcess.exec('rm public/graphs/*.pdf', function () {
res.end('done');
});
})
});
server.get('/genes', function (req, res) {
console.log('Getting genes...');
fs.readdir('clusters/dist', function (err, files) {
res.end(_.map(files, function (file) {
return file.substring(0, file.length - 7); // chop off the `.sqlite`
}).join(','));
});
});
server.get('/graphs', function (req, res) {
console.log('Getting graphs...');
fs.readdir('public/graphs', function (err, files) {
res.end(_.map(_.filter(files, function (file) { return file.substring(file.length - 4, file.length) === '.png'; }), function (file) { return 'graphs/' + file; }).join(','));
});
});
server.get('/tar', function (req, res) {
console.log('Building tarball of pngs...');
var publicTarURL = 'tmp/graphs_png_' + new Date().getTime() + '.tar';
childProcess.exec('tar czf public/' + publicTarURL + ' public/graphs/*.png', function (error, stdout, stderr) {
if (error) {
throw error;
}
res.end(publicTarURL);
});
});
server.get('/pdf', function (req, res) {
console.log('Building tarball of pdfs...');
var publicTarURL = 'tmp/graphs_pdf_' + new Date().getTime() + '.tar';
childProcess.exec('tar czf public/' + publicTarURL + ' public/graphs/*.pdf', function (error, stdout, stderr) {
if (error) {
throw error;
}
res.end(publicTarURL);
});
});
server.post('/process', function (req, res) {
console.log('Processing graph...');
var clusters = req.body.clusters ? 'clusters=' + req.body.clusters : '',
genes = req.body.genes ? 'genes=' + req.body.genes : '',
graph = 'graph=' + req.body.graph,
process = req.body.process,
visual = req.body.visual,
options = req.body.options;
if (process != 'cluster' && process != 'regression') {
res.end('Error: data sent is not valid');
}
var args = _.union(['r/process.r'], graph, clusters, genes, visual, options);
console.log('Invoking Rscript ' + args.join(' '));
childProcess.exec('Rscript ' + args.join(' '), function (error, stdout, stderr) {
if (error) {
throw error;
}
res.end(stdout || stderr);
});
});
server.listen(1337);