Skip to content

Commit 8551762

Browse files
committed
route to optimize model
1 parent bce0e5c commit 8551762

File tree

5 files changed

+99
-1
lines changed

5 files changed

+99
-1
lines changed

python-scripts/optimize.py

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import sys
2+
import json
3+
# COnstraint Based Reconstruction and Analysis
4+
import cobra
5+
6+
# Get command line arguments
7+
# Full file path including extension. e.g. /path/to/iJO1366.json
8+
model_file = sys.argv[1]
9+
# Just name: 'iJO1366' will save to 'iJO1366_solution.json'
10+
out_file = sys.argv[2]
11+
12+
# Load JSON model
13+
model = cobra.io.load_json_model(model_file)
14+
15+
# Optimize for biomass
16+
model.optimize()
17+
18+
# Create a dictionary out of Solution object
19+
solution = {}
20+
solution['f'] = model.solution.f
21+
solution['status'] = model.solution.status
22+
solution['x_dict'] = model.solution.x_dict
23+
# solution['x'] = model.solution.x
24+
solution['y_dict'] = model.solution.y_dict
25+
# solution['y'] = model.solution.x
26+
27+
# Write solution dictionary to json file
28+
# solution_filename = out_file.split('.json')[0] + '_solution.json'
29+
solution_filename = out_file + '_solution.json'
30+
with open(solution_filename, 'w') as outfile:
31+
json.dump(solution, outfile)
32+
print 'Dumped ' + solution_filename

routes/model/optimize/index.js

+64
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
var router = require('express').Router();
2+
var fs = require('fs')
3+
var cp = require('child_process');
4+
5+
MetabolicModel = App.Model('metabolicmodel');
6+
7+
router.get('/:id', function(req, res, next) {
8+
MetabolicModel.findOne({id: req.params.id}, function(err, model) {
9+
if (err) {
10+
res.status(500).send('500 Internal Server Error');
11+
return;
12+
}
13+
14+
if (!model) {
15+
res.status(204).send('204 no content. That model does not exist.\n');
16+
} else {
17+
model.dictifyReactionMetabolites(function(model) {
18+
fileName = 'temp/' + req.params.id + '.json';
19+
20+
fs.writeFile(fileName, JSON.stringify(model), function(err) {
21+
if (err) {
22+
res.status(500).send('500 Internal Server Error');
23+
} else {
24+
25+
var results = {
26+
output : new String(),
27+
errorlog : new String(),
28+
exitcode : null
29+
};
30+
31+
args = [
32+
'python-scripts/optimize.py',
33+
fileName,
34+
'temp/' + req.params.id
35+
]
36+
37+
var optimizeScript = cp.spawn('python', args)
38+
39+
// get stdout
40+
optimizeScript.stdout.on('data', function(stdout) {
41+
results.output += stdout.toString();
42+
});
43+
44+
// get stderr
45+
optimizeScript.stderr.on('data', function(stderr) {
46+
results.errorlog += stderr.toString();
47+
});
48+
49+
// script finished
50+
optimizeScript.on('close', function(code) {
51+
results.exitcode = code;
52+
53+
// Respond on process close
54+
// otherwise, async problems!
55+
res.send(results);
56+
});
57+
}
58+
});
59+
});
60+
}
61+
})
62+
})
63+
64+
module.exports = router;

routes/model/retrieve/index.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ router.get('/:id', function(req,res, next) {
2323
}
2424

2525
if (!model) {
26-
res.status(403).send('204 no content. That model does not exist.\n');
26+
res.status(204).send('204 no content. That model does not exist.\n');
2727
} else {
2828
model.dictifyReactionMetabolites(function(model) {
2929
res.send(model);

temp/iJO1366.json

+1
Large diffs are not rendered by default.

temp/iJO1366_solution.json

+1
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)