forked from felicityallen/SelfTarget
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py
More file actions
158 lines (131 loc) · 5.64 KB
/
server.py
File metadata and controls
158 lines (131 loc) · 5.64 KB
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
import http
import logging
import os
import tempfile
import mpld3
import requests
from flask import Flask, request, jsonify, send_file
from indel_prediction.predictor.predict import plot_predictions
from mongoengine import connect, Document, StringField, MultipleObjectsReturned
from predictor.predict import build_plot_by_profile
from selftarget.profile import readSummaryToProfile
from werkzeug.exceptions import BadRequest
app = Flask(__name__)
model_path = "indel_prediction/predictor/model_output_10000_0.01000000_0.01000000_-0.607_theta.txt_cf0.txt"
HUMAN = "human"
MOUSE = "mouse"
MONGODB_HOST = os.getenv("MONGODB_HOST")
S3_BASE = os.getenv("S3_BASE", "https://fa9.cog.sanger.ac.uk/")
DB_FILEPATH_BASE = os.getenv("DB_FILEPATH_BASE", "/lustre/scratch117/cellgen/team227/FORECasT_profiles_for_AK/")
connect(host=MONGODB_HOST, connect=False)
class WGE(Document):
wge_id = StringField(required=True, max_length=50, unique=True)
oligo_id = StringField(max_length=50)
filename = StringField(required=True, max_length=500)
species = StringField(required=True, choices=[HUMAN, MOUSE], max_length=6)
class NoWGEException(Exception):
def __init__(self, species, seq):
self.species = species
self.seq = seq
def msg(self):
return f"Error - no WGE found for seq {self.seq} {self.species}"
def get_obj_by_id(id, species):
wge = False
# if id is integer, this is wge, if not, it's oligo_id
try:
wge = int(id) or True
except ValueError:
pass
if wge:
objects = WGE.objects(wge_id=id, species=species)
else:
objects = WGE.objects(oligo_id=id, species=species)
if len(objects) > 1:
raise MultipleObjectsReturned()
elif len(objects) == 0:
raise NoWGEException(species, id)
obj = objects[0]
obj['filename'] = obj['filename'].replace(DB_FILEPATH_BASE, S3_BASE)
return obj
def read_profile(obj):
reads_file = tempfile.mkstemp()[1]
profile_file = tempfile.mkstemp()[1]
r = requests.get(obj['filename'], allow_redirects=True)
with open(reads_file, 'w') as f:
f.write(r.text)
r = requests.get(obj['filename'].replace("_predicted_rep_reads.txt", "_predicted_mapped_indel_summary.txt"),
allow_redirects=True)
with open(profile_file, 'w') as f:
f.write(r.text)
profile = {}
readSummaryToProfile(profile_file, profile, oligoid=obj['oligo_id'], remove_wt=False)
return reads_file, profile_file, profile
@app.route('/ping', methods=['GET'])
def ping():
return "The server is alive!", http.HTTPStatus.OK
@app.route('/api/profile', methods=['GET'])
def get_profile():
"""
This endpoint returns a profile as a text file. It is created and saved automatically if the plot
has been built before (via the `plot` endpoint) for a sequence and PAM index pair.
If the plot has not been built, there is no profile file saved, so an error is raised.
request body: {"seq": "SEQUENCE", "pam_idx": "NUMBER"}
:return: {"plot": "plot data"}
:return: {"error": "error message"}
"""
data = request.args or request.get_json()
seq = data.get("seq", "")
pam_idx = data.get("pam_idx", "")
if not (seq and pam_idx):
return jsonify({'error': 'Target sequence or pam index not provided'}), http.HTTPStatus.BAD_REQUEST
filename = '{0}_{1}.txt'.format(seq, pam_idx)
if os.path.exists(filename):
return send_file(filename, as_attachment=True)
else:
return jsonify({'error': 'Profile with those target sequence and pam index not found'}), http.HTTPStatus.BAD_REQUEST
@app.route('/plot', methods=['GET', 'POST'])
def plot():
"""
This endpoint returns a javascript code that is rendered in a browser as a profile plot
for a particular pair of sequence and PAM index.
request body: {"seq": "SEQUENCE", "pam_idx": "NUMBER"}
:return: {"plot": "plot data"}
:return: {"error": "error message"}
"""
if request.method == 'GET':
data = request.args
wge = data.get("wge", "")
oligoid = data.get("id", "")
species = data.get("species", "")
if not ((wge or oligoid) and species):
raise BadRequest()
try:
obj = get_obj_by_id(wge or oligoid, species)
except NoWGEException as ex:
return jsonify({"error": ex.msg()}), http.HTTPStatus.NOT_FOUND
reads_file, profile_file, profile = read_profile(obj)
try:
graph_html = mpld3.fig_to_html(build_plot_by_profile(reads_file, profile, obj['oligo_id']),
template_type="simple",
figid="plot",
no_extras=True)
except Exception as e:
logging.exception("Model error")
return jsonify({"error": str(e)}), http.HTTPStatus.INTERNAL_SERVER_ERROR
elif request.method == 'POST':
data = request.form or request.get_json()
seq = data.get("seq", "")
pam_idx = int(data.get("pam_idx", ""))
if not (seq and pam_idx):
return jsonify({'message': 'Empty request'}), http.HTTPStatus.BAD_REQUEST
try:
graph_html = mpld3.fig_to_html(plot_predictions(model_path, seq, pam_idx),
template_type="simple",
figid="plot",
no_extras=True)
except Exception as e:
logging.exception("Model error")
return jsonify({"error": str(e)}), http.HTTPStatus.INTERNAL_SERVER_ERROR
return jsonify({"plot": graph_html})
if __name__ == "__main__":
app.run(port=5001)