-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrpvizServe.py
86 lines (73 loc) · 2.51 KB
/
rpvizServe.py
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
'''
doeServe (c) University of Manchester 2018
doeServe is licensed under the MIT License.
To view a copy of this license, visit <http://opensource.org/licenses/MIT/>.
@author: Pablo Carbonell, SYNBIOCHEM
@description: A REST service for OptDes
'''
import os
import uuid
import shutil
import json
from datetime import datetime
from flask import Flask, request, jsonify,send_file
from flask_restful import Resource, Api
from rpviz.main import run
app = Flask(__name__)
api = Api(app)
dataFolder = os.path.join( os.path.dirname(__file__), 'data' )
def stamp( data, status=1 ):
appinfo = {'app': 'rpviz', 'version': '1.0',
'author': 'Anaelle Badier, Pablo Carbonell',
'organization': 'Synbiochem',
'time': datetime.now().isoformat(),
'status': status}
out = appinfo.copy()
out['data'] = data
return out
class RestApp( Resource ):
""" REST App."""
def post(self):
return jsonify( stamp(None) )
def get(self):
return jsonify( stamp(None) )
class RestQuery( Resource ):
""" REST interface that generates the Design.
Avoid returning numpy or pandas object in
order to keep the client lighter.
"""
def post(self):
file_upload = request.files['file']
data_upload = request.files['data']
data = json.load( data_upload )
try:
selenzyme_table = data['selenzyme_table']
except:
selenzyme_table = 'N'
try:
input_format = data['input_format']
except:
input_format = 'sbml'
fid = str(uuid.uuid4())
infile= os.path.join(dataFolder,fid+'.tar')
content = file_upload.read()
open(infile, 'wb').write(content)
oid = str(uuid.uuid4())
outfolder = os.path.join( dataFolder, oid )
os.mkdir( outfolder )
print('done')
outfile = run( infile, outfolder, selenzyme_table=selenzyme_table, typeformat=input_format, choice='5')
return send_file(outfile, as_attachment=True)
with open(outfile,'rb') as h:
tar = h.read()
data = {'tar': tar}
os.remove(infile)
shutil.rmtree( outfolder )
return jsonify( stamp(data, 1) )
api.add_resource(RestApp, '/REST')
api.add_resource(RestQuery, '/REST/Query')
if __name__== "__main__":
if not os.path.exists( dataFolder ):
os.mkdir(dataFolder)
debug = os.getenv('USER') == 'pablo'
app.run(host="0.0.0.0", port=8998, debug=debug, threaded=True)