-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathapp.py
509 lines (399 loc) · 15.6 KB
/
app.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
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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
from flask import Flask, jsonify, request,g, render_template
from flask import Flask, flash, request, redirect, url_for
from flask import Flask, session
from werkzeug.utils import secure_filename
import click
from flask import current_app, g
from flask.cli import with_appcontext
from flask_cors import CORS, cross_origin
try:
from tables import *
except:
from .tables import *
from werkzeug.contrib.cache import SimpleCache
app = Flask(__name__)
app.secret_key = b'_5#y2L"F4Q8z\n\xec]/'
cors = CORS(app, send_wildcard=True)
app.config['CORS_HEADERS'] = 'Content-Type'
cache = SimpleCache()
ALLOWED_EXTENSIONS = {'txt', 'pdf', 'png', 'jpg', 'jpeg', 'gif'}
def allowed_file(filename):
return '.' in filename and \
filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
@app.route('/upload', methods=['POST'])
def upload_file():
#print("namespace", request.args.get("namespace"))
form = request.form.to_dict()
#print(request.files)
if form["namespace"] == "New":
namespace = form["new_name"]
Upload.create_new_namespace(form["new_name"])
else:
namespace = form["existing_namespace"]
if not namespace:
return "namespace fail"
Upload.create_new_temp_namespace(namespace)
# GET LAYOUT
layout_files = request.files.getlist("layouts")
if len(layout_files) > 0 and len(layout_files[0].filename) > 0:
#print("loading layouts", len(layout_files))
Upload.upload_layouts(namespace, layout_files)
# GET NODES
node_files = request.files.getlist("nodes")
if len(node_files) > 0 and len(node_files[0].filename) > 0:
print("loading nodes", len(node_files))
Upload.upload_nodes(namespace, node_files)
# GET EDGES
edge_files = request.files.getlist("links")
if len(edge_files) > 0 and len(edge_files[0].filename) > 0:
Upload.upload_edges(namespace, edge_files)
# GET ATTRIBUTES
attribute_files = request.files.getlist("attributes")
if len(attribute_files) > 0:
Upload.upload_attributes(namespace, attribute_files)
label_files = request.files.getlist("labels")
if len(label_files) > 0:
Upload.upload_labels(namespace, request.files.getlist("labels"))
return "success"
if 'file' not in request.files:
flash('No file part')
return redirect(request.url)
file = request.files['file']
# if user does not select file, browser also
# submit an empty part without filename
if file.filename == '':
flash('No selected file')
return redirect(request.url)
if file and allowed_file(file.filename):
filename = secure_filename(file.filename)
#file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
return redirect(url_for('uploaded_file',
filename=filename))
########
# Data #
########
@app.route("/api/namespace/summary", methods=["GET"])
@cross_origin()
def get_summary():
return jsonify(Data.summary())
# @app.route("/api/<string:db_namespace>/subgraph", methods=['GET'])
# @cross_origin()
# def get_subgraph(db_namespace):
# nodes = request.args.getlist('node_ids')
# attribute = request.args.getlist('attribute_id')
# if attribute:
# nodes = [node["id"] for node in Node.nodes_for_attribute(db_namespace, attribute)]
# if not nodes:
# return "Fail"
# edges = Edge.for_nodelist(db_namespace, nodes)
# nodes = Node.get(db_namespace, nodes)
# return jsonify({"nodes": nodes, "edges": edges})
########
# Node #
########
@app.route("/api/<string:db_namespace>/node", methods=['GET'])
@cross_origin()
def nodes(db_namespace):
prefix = request.args.get('prefix') or ""
symbols = request.args.getlist("symbols")
external_ids = request.args.getlist("external_ids")
node_ids = request.args.getlist("id")
neighbors = request.args.getlist("neighbor")
random = request.args.get('random') or None
attribute_ids = request.args.getlist("attribute_id")
#print(node_ids)
if random:
return jsonify(Node.show_random(random, db_namespace))
if node_ids:
return jsonify(Node.get(db_namespace, node_ids))
if external_ids:
return jsonify(Node.get_by_external_ids(db_namespace, external_ids))
if symbols:
return jsonify(Node.get_by_symbols(db_namespace, symbols))
if neighbors:
return jsonify(Node.get_neighbors(db_namespace, neighbors))
if attribute_ids:
return jsonify(Node.nodes_for_attribute(db_namespace, attribute_ids))
return jsonify(Node.nodes_for_autocomplete(db_namespace, prefix))
## IP_CELINE: implement GSEA
#@app.route('/api/<string:db_namespace>/node/gsea', methods=['GET', 'POST'])
@app.route('/api/<string:db_namespace>/node/gsea', methods=['POST'])
@cross_origin()
def gsea(db_namespace):
if request.method == 'POST':
#data = request.form
data = request.get_json()
print('in POST')
else:
data = request.args
print([i for i in data.keys()])
# check if arguments are there
print(data)
if ('node_ids' in data.keys()) & ('annotation' in data.keys()):
#selectionNodes = data.getlist('node_ids')
#selectionNodes = [int(x) for x in data['node_ids']]
selectionNodes = data['node_ids']
currAnno = data['annotation']
elif ('node_ids' not in data.keys()) & ('annotation' not in data.keys()):
return 'must provide node_ids and (optionally) annotation desired'
elif 'node_ids' not in data.keys():
return 'must provide node_ids'
elif 'annotation' not in data.keys():
# return 'must provide annotation'
#selectionNodes = data.getlist('node_ids')
# selectionNodes = [int(x) for x in data['node_ids']]
selectionNodes = data['node_ids']
#selectionNodes = tuple([int(x) for x in data['node_ids']])
currAnno = None
# real return
print(data['node_ids'])
return jsonify(Node.gsea(db_namespace, selectionNodes, currAnno))
@app.route('/api/<string:db_namespace>/node/random_walk', methods=['GET', 'POST'])
@cross_origin()
def random_walk(db_namespace):
if request.method == 'POST':
#data = request.form
data = request.get_json()
else:
data = request.args
# node_ids = data.node_ids
node_ids = [int(x) for x in data['node_ids']]
if 'variants' in data.keys():
variants = [int(x) for x in data['variants']]
else:
variants = []
# print(node_ids)
#node_ids = [int(x) for x in data.getlist("node_ids")]
restart_probability = data["restart_probability"]
restart_probability = float(restart_probability or 0.9)
if "max_elements" in data.keys():
max_elements = data["max_elements"]
else:
max_elements = 200
return jsonify(Node.random_walk(db_namespace,node_ids,variants,restart_probability,max_elements, cache))
@app.route('/api/<string:db_namespace>/node/random_walk_dock2', methods=['GET', 'POST'])
@cross_origin()
def random_walk_dock2(db_namespace):
if request.method == 'POST':
#data = request.form
data =request.get_json()
else:
data = request.args
# node_ids = data.node_ids
node_ids = [int(x) for x in data['node_ids']]
if 'variants' in data.keys():
variants = [int(x) for x in data['variants']]
else:
variants = []
# print(node_ids)
#node_ids = [int(x) for x in data.getlist("node_ids")]
restart_probability = data["restart_probability"]
restart_probability = float(restart_probability or 0.9)
if "max_elements" in data.keys():
max_elements = data["max_elements"]
else:
max_elements = 200
return jsonify(Node.random_walk_dock2(db_namespace,node_ids,variants,restart_probability,max_elements, cache))
@app.route('/api/<string:db_namespace>/node/gene_card', methods=['GET'])
@cross_origin()
def gene_card(db_namespace):
node_id = request.args.get("node_id")
return jsonify(Node.gene_card(db_namespace, node_id, cache))
@app.route('/api/<string:db_namespace>/node/shortest_path', methods=['GET'])
@cross_origin()
def shortest_path(db_namespace):
from_id = request.args.get("from")
to_id = request.args.get("to")
return Node.shortest_path(db_namespace, from_id, to_id)
# @app.route('/api/<string:db_namespace>/node/expression', methods=['GET'])
# @cross_origin()
# def shortest_path(db_namespace):
#
# from_id = request.args.get("from")
# to_id = request.args.get("to")
# return Node.expression(db_namespace, from_id, to_id)
#
@app.route('/api/<string:db_namespace>/node/connect_set_dfs', methods=['POST'])
@cross_origin()
def connect_set_dfs(db_namespace):
data =request.get_json()
seeds = data["seeds"]
variants = data["variants"]
return Node.connect_set_dfs(db_namespace, seeds, variants,cache)
@app.route('/api/<string:db_namespace>/node/sub_layout', methods=['POST'])
@cross_origin()
def sub_layout(db_namespace):
data =request.get_json()
# node_ids = data.node_ids
node_ids = [int(x) for x in data['node_ids']]
return jsonify(Node.layout(db_namespace,node_ids, cache))
@app.route('/api/<string:db_namespace>/node/scale_selection', methods=['POST'])
@cross_origin()
def scale_selection(db_namespace):
data =request.get_json()
# node_ids = data.node_ids
node_ids = [str(x) for x in data['node_ids']]
layout = data['layout']
return jsonify(Node.scale_selection(db_namespace,node_ids,layout, cache))
@app.route("/api/<string:namespace>/node/search", methods=['GET', 'POST'])
@cross_origin()
def search(namespace):
if request.method == 'POST':
data = request.form
results = Node.search(namespace, data)
return jsonify(results)
else:
data = request.args
results = Node.search(namespace, data)
return jsonify(results)
#############
# Attribute #
#############
@app.route("/api/<string:db_namespace>/attribute/", methods=['GET'])
@cross_origin()
def attributes_for_node(db_namespace):
attr_namespace = request.args.get("namespace") or None
node_id = request.args.get("node_id")
external_ids = request.args.getlist("external_ids")
if node_id:
return jsonify(Attribute.attributes_for_node(db_namespace, node_id, attr_namespace))
prefix = request.args.get("prefix") or ""
if prefix:
return jsonify(Attribute.attributes_for_autocomplete(db_namespace, prefix, attr_namespace))
if external_ids:
return jsonify(Attribute.attributes_for_external_ids(db_namespace, external_ids))
if attr_namespace:
return jsonify(Attribute.all_attribute_names(db_namespace, attr_namespace))
else:
return "no arguments supplied!"
@app.route("/api/<string:db_namespace>/attribute/delete/<int:attr_id>", methods=['GET'])
@cross_origin()
def delete_attribute(db_namespace, attr_id):
return Attribute.delete(db_namespace, attr_id)
# Cache database call for attribute taxonomy to prevent repeated queries.
def get_attribute_taxonomy(db_namespace, root_node_id):
zkey = 'attribute_taxonomy_parents-%s-%d' % (db_namespace, root_node_id)
dtc = cache.get(zkey)
if dtc is None:
print("!!FEFDKJBFSKD")
dtc = AttributeTaxonomy.construct_taxonomy(db_namespace, root_node_id)
cache.set(zkey, dtc, timeout=5 * 60 * 60 * 24 * 1000)
return dtc
@app.route("/api/<string:db_namespace>/attribute_taxonomy/<int:root_node_id>", methods=['GET'])
@cross_origin()
def taxonomy(db_namespace, root_node_id):
return jsonify(get_attribute_taxonomy(db_namespace, root_node_id))
@app.route("/api/<string:db_namespace>/attribute_taxonomy/", methods=['GET'])
@cross_origin()
def taxonomy_from_name(db_namespace):
taxonomy_name = request.args.get("namespace")
root_node_id = AttributeTaxonomy.get_root(db_namespace, taxonomy_name)
if root_node_id:
return jsonify(get_attribute_taxonomy(db_namespace, root_node_id))
else:
return jsonify([])
@app.route("/api/<string:db_namespace>/attribute/namespaces", methods=["GET"])
@cross_origin()
def attribute_namespaces(db_namespace):
return jsonify(Attribute.get_attribute_namespaces(db_namespace))
@app.route("/api/<string:db_namespace>/selection/create", methods=['GET', 'POST'])
@cross_origin()
def create_selection(db_namespace):
if request.method == 'POST':
data = request.form
selection_name = data.get("selection_name")
node_ids = data.get("node_ids")
if not data:
data = request.get_json(force=True)
selection_name = data["selection_name"]
node_ids = data["node_ids"]
else:
data = request.args
selection_name = data.get("selection_name")
node_ids = data.get("node_ids")
if not selection_name or not node_ids:
return jsonify({"status": "FAIL", "reason": "invalid request"})
return jsonify(Attribute.create_selection(db_namespace, selection_name, node_ids))
@app.route("/api/<string:db_namespace>/attribute/attribute2attribute", methods=["GET"])
@cross_origin()
def attribute2attribute(db_namespace):
att_id = request.args.get("att_id")
return jsonify(Attribute.attribute2attribute(db_namespace,att_id))
###########
# Article #
###########
@app.route("/api/<string:namespace>/article", methods=['GET'])
@cross_origin()
def article(namespace):
pubid = request.args.get('pubid')
if pubid:
return jsonify(Article.article_for_pubid(namespace, pubid))
node_id = request.args.get("node_id") or 0
return jsonify(Article.articles_for_node(namespace, node_id))
########
# Edge #
########
@app.route("/api/<string:namespace>/edge", methods=['GET'])
@cross_origin()
def get_edge(namespace):
data = request.args
node_ids = data.get("node_id")
print(node_ids)
if node_ids:
return jsonify(Edge.for_nodelist(node_ids))
return jsonify(Edge.all(namespace))
##########
# Layout #
##########
@app.route("/api/<string:db_namespace>/layout/<string:layout_namespace>")
@cross_origin()
def get_layout(db_namespace, layout_namespace):
return jsonify(Layout.fetch(db_namespace, layout_namespace))
#########
# Label #
#########
@app.route("/api/<string:db_namespace>/label/<string:label_namespace>")
@cross_origin()
def get_label(db_namespace, label_namespace):
return jsonify(Label.fetch(db_namespace, label_namespace))
##################
# EXPORT RESULTS #
##################
@app.route('/api/<string:db_namespace>/export/results', methods=['POST'])
@cross_origin()
def export_dashboard_data(db_namespace):
data =request.get_json()
Exports.saveSidepanel(db_namespace,data)
return '0'
@app.route('/api/<string:db_namespace>/import/results')
@cross_origin()
def load_filenamesSidepanel(db_namespace):
return jsonify(Exports.load_filenamesSidepanel(db_namespace))
@app.route('/api/<string:db_namespace>/import/resultsfilename', methods=['GET'])
@cross_origin()
def load_Sidepanelbyfilename(db_namespace):
f_name = request.args.get("fname")
return jsonify(Exports.load_Sidepanelbyfilename(db_namespace,f_name))
#EXPERIMENTAL
#MAP SERVING
@app.route('/maps/<filename>/')
def get_image(filename):
# https://stackoverflow.com/questions/53202636/render-dynamically-changing-images-with-same-filenames-in-flask
print('display_image filename: ' + filename)
return redirect(url_for('static', filename='maps/' + filename), code=301)
#############
# SavedView #
#############
# @app.route("/api/saved_views/<string:username>/<string:view_name>", methods=['GET'])
# @cross_origin()
# def get_saved_view(username, view_name):
# return jsonify(SavedView.get(username, view_name))
#
# @app.route("/api/saved_views/create", methods=['POST'])
# @cross_origin()
# def create_saved_view():
# data = request.form
# return jsonify(SavedView.create(data))
if __name__ == "__main__":
app.debug = True
app.run(port=1337)