-
Notifications
You must be signed in to change notification settings - Fork 2
/
server.js
executable file
·342 lines (291 loc) · 12 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
const express = require("express");
const path = require("path");
const logger = require("morgan");
const cookieParser = require("cookie-parser");
const bodyParser = require("body-parser");
const fileUpload = require("express-fileupload");
const { exec } = require("child_process");
const fs = require("fs");
const timeout = require("connect-timeout");
// const ncp = require("ncp").ncp;
// const cors = require("cors");
const {
dominoPostProcess,
separateActiveGenes,
draftSessionDirectoryDetails,
hasNonAlphaNumericChars,
hasExpectedFileExtension,
formatDate,
convert2Ensg
} = require("./utils.js");
const {
addExecution,
aggregateExecutions,
aggregateGit,
aggregateBioconda,
aggregatePypi,
createDummyValues
} = require("./db_helper.js");
const errorMsgs=require("./errors.js")
const fileStructure = require("./src/components/public/files_node");
const conf = require("./config.js").conf;
const mongoose = require('mongoose');
/** Database setup */
//const uri = "mongodb://nimsi:[email protected]/executions?retryWrites=true&w=majority";
//mongoose.set('bufferCommands', false); // temporary
//const uri = 'mongodb://127.0.0.1/executions';
//mongoose.connect(uri, {useNewUrlParser: true, useUnifiedTopology: true})
// .then(() => console.log("Successful MongoDB connection."));
//const db = mongoose.connection;
//db.on('error', console.error.bind(console, 'MongoDB connection error:'));
const app = express();
app.use(express.static(path.join(__dirname, 'build')));
app.use(express.static(path.join(__dirname, 'public')));
// Set 'views' directory for any views
// being rendered res.render()
app.set('views', path.join(__dirname, 'views'));
app.set("view engine", "jade");
// Set view engine as EJS
app.engine('html', require('ejs').renderFile);
app.set('view engine', 'html');
app.use(logger("dev"));
app.use(function(req, res, next) {
res.set({
"Access-Control-Allow-Origin": "http://" + "localhost"+ ":8000",
"Access-Control-Allow-Headers": "X-Requested-With",
"Access-Control-Allow-Headers": "Content-Type",
"Access-Control-Allow-Methods": "PUT, GET, POST, DELETE, OPTIONS",
"Access-Control-Allow-Credentials": "true"
});
next();
});
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
/* Use cors and fileUpload*/
// app.use(cors());
app.use(fileUpload());
/* Promise wrappers. */
const execAsync = (cmd) => {
/**
* Executes a shell command and return it as a Promise.
* @param cmd {string}
* @return {Promise<string>}
*/
return new Promise((resolve, reject) => {
exec(cmd, (error, stdout, stderr) => {
if (error) {
console.warn(error);
reject(error);
}
console.log(stdout);
console.log(stderr);
resolve(stdout? stdout : stderr);
});
});
}
app.get("/aggregated-usage", async (req, res, next) => {
// aggregate
console.log("Aggregating");
let [totalExecutions, networkExecutions, monthlyExecutionsByNetworks] = await aggregateExecutions();
let [yearlyGit, monthlyGit] = await aggregateGit();
let [yearlyBioconda, monthlyBioconda] = await aggregateBioconda();
let [yearlyPypi, monthlyPypi] = await aggregatePypi();
console.log(yearlyPypi)
console.log(monthlyPypi)
console.log("Done aggregating");
// post processing
networkExecutions = networkExecutions.map((usage) => {
return {
network: usage._id,
freq: usage.freq
};
});
res.json({
totalExecutions: totalExecutions[0],
networkExecutions: networkExecutions,
monthlyExecutionsByNetworks: monthlyExecutionsByNetworks,
yearlyGit: yearlyGit[0],
monthlyGit: monthlyGit,
yearlyBioconda: yearlyBioconda[0],
monthlyBioconda: monthlyBioconda,
yearlyPypi: yearlyPypi[0],
monthlyPypi: monthlyPypi
});
res.end();
});
app.post("/upload", timeout("20m"), (req, res, next) => {
console.log("Starting upload POST request ...");
// create session directory (within the public folder)
let fileNames = fileStructure.files.map(file => file.name);
const userFileNames = fileNames.reduce(
(obj, file) => ({
...obj,
[file]: req.body[`${file} name`]
}),{}); // input files to DOMINO selected by the user
if (! hasExpectedFileExtension(userFileNames["Active gene file"], "txt") || ! hasExpectedFileExtension(userFileNames["Network file"], "sif")){
res.status(400).send(errorMsgs.invalidFileExtension);
return;
}
const isInvalidFileNames=Object.values(userFileNames).map((cur) => cur.split('.').slice(0,-1).join('.')).reduce((agg,cur)=>{return agg || hasNonAlphaNumericChars(cur)}, false);
if (isInvalidFileNames){
res.status(400).send(errorMsgs.invalidAlphaNumericFileName);
return;
}
const [sessionDirectory, customFile] = draftSessionDirectoryDetails(userFileNames);
fs.mkdirSync(sessionDirectory);
// move network file to session directory
// initialize values for the following variables
let networkFilePath = req.body[`Network file path`];
const cachedNetworkFile = networkFilePath;
let mvNetworkFile, networkFileContents;
if (cachedNetworkFile) {
networkFileContents = fs.promises.readFile(networkFilePath);
} else {
let networkFile = req.files[`Network file contents`];
networkFilePath = `${sessionDirectory}/${userFileNames["Network file"]}`;
mvNetworkFile = networkFile.mv(networkFilePath);
networkFileContents = new String(networkFile);
}
const sliceNetworkFile = execAsync(
`bash runners/slicer.sh ` + [`"${networkFilePath}"`, `${networkFilePath}.slicer`, conf.DOMINO_PYTHON_ENV].join(' ')
);
let activeGenesSet = separateActiveGenes(new String(req.files["Active gene file contents"].data));
const setNames = Object.keys(activeGenesSet);
const isInvalidActiveGeneSetNames=Object.values(setNames).reduce((agg,cur)=>{return agg || hasNonAlphaNumericChars(cur)}, false);
if (cachedNetworkFile){
activeGenesSet = convert2Ensg(activeGenesSet);
console.log(activeGenesSet)
}
if (isInvalidActiveGeneSetNames){
res.status(400).send(errorMsgs.invalidAlphaNumericSetName);
return;
}
console.log("set names identified --> ", setNames.join(", "));
const singleDOMINORun = async (serverNum, sessionDirectory, setName) => {
/** Manages one run of DOMINO until completion of DOMINO postprocessing.
* Returns a Promise.
* Takes advantage of:
* activeGenesSet
* req
* userFileNames
* networkFilePath, cachedNetworkFile,
* networkFileContents (guaranteed to be resolved when this function runs)
* */
console.log('Starting a single DOMINO run')
const subRunDirectory = `${sessionDirectory}/${setName}`;
const outputFile = `${subRunDirectory}/modules`;
await fs.promises.mkdir(subRunDirectory);
await fs.promises.mkdir(outputFile);
// load the active gene file into the sub run directory
const activeGenesFilePath = `${subRunDirectory}/active_gene_file.txt`;
await fs.promises.writeFile(
activeGenesFilePath,
activeGenesSet[setName].join("\n")
);
console.log(`Starting domino py execution on set ${setName}...`);
// question -> not sure why gdocker up after cd works but not the other way around?!
let cmdArgs=[
`"${subRunDirectory}"`,
"active_gene_file.txt",
`"${subRunDirectory}/active_gene_file.txt"`,
`"${networkFilePath}"`,
`"${outputFile}"`,
conf.DOMINO_PYTHON_ENV,
conf.AMI_PLUGINS_PYTHON_ENV
].join(" ");
localExecution=`bash runners/domino.sh ${cmdArgs}`
try {
if (conf.REMOTE_EXECUTION){
console.log("About to start remote execution")
execution=`ssh ${conf.USERNAME}@rack-shamir${serverNum}.cs.tau.ac.il "udocker run --volume /specific:/mnt/specific domino_updated bash -c 'cd /specific/netapp5/gaga/hagailevi/domino_web && ${localExecution}'"`
}
else{
console.log("About to start local execution")
execution=localExecution
}
await execAsync(execution);
} catch (error) {
console.log(`Error with DOMINO execution on set ${setName}.`);
return Promise.reject(error);
}
console.log(`Reading the output of domino py on set ${setName} ...`);
const dominoOutput = await fs.promises.readFile(`${outputFile}/modules.out`);
const algOutput = dominoPostProcess(dominoOutput, networkFileContents);
console.log(`DOMINO post process on set ${setName} ...`);
console.log("modules --> ", algOutput.modules);
console.log("numModules --> ", Object.keys(algOutput.modules).length);
return {[setName]: algOutput};
};
Promise.all([mvNetworkFile, networkFileContents, sliceNetworkFile])
.then(_ => {
let serverBase = 3, serverOffset = 0, serverOffsetLimit = 3;
return Promise.all(
setNames.map(setName => {
let serverNum = serverBase + serverOffset;
serverOffset = (serverOffset + 1) % serverOffsetLimit;
return singleDOMINORun(serverNum, sessionDirectory, setName);
})
);
})
.then(listOfOutputs => {
// update response with domino output
const algOutputs = listOfOutputs.reduce((obj, output) =>
Object.assign(obj, output)
, {});
const geneSets = Object.keys(algOutputs).reduce((obj, setName) => ({
...obj,
[setName]: Object.keys(algOutputs[setName].modules).length
}), {});
console.log(JSON.stringify({
algOutput: algOutputs,
...((req.body["fromWebExecutor"] === "true") ?
{webDetails: {
geneSets: geneSets,
customFile: customFile,
zipURL: `${customFile}.zip`,
}}
: {}
)
}));
res.json({
algOutput: algOutputs,
...((req.body["fromWebExecutor"] === "true") ?
{webDetails: {
geneSets: geneSets,
customFile: customFile,
zipURL: `${customFile}.zip`,
}}
: {}
)
});
})
.then(_ => {
// finalize folder structure, create zip file, and log execution details
const rmCachedFiles = exec(`rm ${sessionDirectory}/*.plk ${sessionDirectory}/*slicer`);
console.log("Zipping solution ...");
const zipFiles = execAsync(
`cd "${sessionDirectory}/.."
zip -r "${customFile}.zip" "${customFile}"`
);
const logExec = addExecution(userFileNames["Network file"], !cachedNetworkFile);
return Promise.all([rmCachedFiles, zipFiles, logExec]);
})
.catch(error => {
console.log(error);
res.status(400).send(errorMsgs.nonSpecific);
})
.then(_ => res.end());
});
app.get('/*', function (req, res) { // why "/*"?
res.sendFile(path.join(__dirname, 'build', 'index.html'));
});
app.use((err, req, res, next) => {
// delegate to the default Express error handler, when the headers have already been sent to the client
console.error(err.stack);
if (res.headersSent) {
return next(err);
}
res.status(500).send('Something broke!');
});
app.listen(conf.PORT || 8000);