-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.py
89 lines (73 loc) · 3.07 KB
/
api.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
86
87
88
89
from io import BytesIO
from flask import Flask, send_file, request
import json
from hashlib import md5
import logging
from utils import hash_redis, get_db_from_task, task_submit
import re
app = Flask(__name__)
sequence_checker = re.compile("^[ACDEFGHIKLMNPQRSTUVWY\s]+$")
def check(seq):
return sequence_checker.match(seq)
@app.route("/api", methods=["POST"])
def api():
try:
req = request.get_json()
if check(req["sequence"]):
md5_hash = md5(req["sequence"].encode()).hexdigest()
hash_redis.set(md5_hash, req["sequence"])
for task in req["tasks"]:
db = get_db_from_task(task)
if not db.get(md5_hash) or eval(db.get(md5_hash))["status"] == "Failed":
if type(req["tasks"][task]) == dict:
task_submit(task, md5_hash, **req["tasks"][task])
elif req["tasks"][task] == True:
task_submit(task, md5_hash)
db.set(md5_hash, json.dumps({"status": "Running"}))
return json.dumps({"hash": md5_hash})
except Exception as e:
raise e
# return json.dumps({"status": str(e)})
@app.route("/api/<md5_hash>/<task>/", methods=["get"])
def get_task_info(md5_hash, task):
if hash_redis.get(md5_hash):
result = get_db_from_task(task).get(md5_hash)
result = eval(result)
if task == "DeepTMHMM" and result["status"] == "Success":
del result["/plot.png"]
result["/TMRs.gff3"] = result["/TMRs.gff3"].decode()
result["/predicted_topologies.3line"] = result["/predicted_topologies.3line"].decode()
return json.dumps(result)
if result != None:
return json.dumps(result)
else:
return json.dumps({"status": "NotRequested"})
else:
return json.dumps({"status": "NoSuchSequence"})
@app.route("/api/<md5_hash>/DeepTMHMM/plot.png", methods=["get"])
def get_plot(md5_hash):
if hash_redis.get(md5_hash):
result = eval(get_db_from_task("DeepTMHMM").get(md5_hash))
if (result["status"] == "Success"):
return send_file(BytesIO(result["/plot.png"]), mimetype="image/png", as_attachment=False, attachment_filename="plot.png")
else:
return json.dumps({"status": result["status"]})
else:
return json.dumps({"status": "NoSuchSequence"})
# @app.route("/random")
# def random():
# root = "/root/JSApiSample/statics"
# with open(path.join(root, choice(os.listdir(root))), 'rb') as f:
# content = f.read()
# return send_file(BytesIO(content), mimetype="image/png", as_attachment=False, attachment_filename="random.png")
@app.after_request
def after_request(res):
res.headers['Access-Control-Allow-Origin'] = '*'
res.headers["Access-Control-Allow-Headers"] = '*'
return res
if __name__ == "__main__":
app.run("0.0.0.0", 6002)
if __name__ != '__main__':
gunicorn_logger = logging.getLogger('gunicorn.error')
app.logger.handlers = gunicorn_logger.handlers
app.logger.setLevel(gunicorn_logger.level)