-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapp.py
More file actions
146 lines (116 loc) · 4.34 KB
/
app.py
File metadata and controls
146 lines (116 loc) · 4.34 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
import json
import re
from flask import Flask, request, abort, send_from_directory
from werkzeug.serving import run_simple
from flask_cors import CORS
from utils.DataLoader import DataLoader
from utils.Cache import Cache
from server import availableAlgorithms, externalApiCalls, algorithmExecution
import optparse
import os
datasets = {
"dataset4": "TCGA",
"dataset5": "TCGA + GTEX"
}
dataLoaders = {dataset: DataLoader(dataset) for dataset in datasets}
statistics = {dataset: dataLoader.getStatistics()
for (dataset, dataLoader) in dataLoaders.items()}
cache = Cache()
app = Flask(__name__)
CORS(app)
@app.route('/', defaults={'path': 'index.html'})
@app.route('/<path:path>')
def serve(path):
if(os.path.exists('client/build/' + path)):
return send_from_directory('client/build/', path)
else:
return send_from_directory('client/build/', 'index.html')
@app.route('/static/js/<path:path>')
def servejs(path):
return send_from_directory('client/build/static/js/', path)
@app.route('/static/media/<path:path>')
def servemedia(path):
return send_from_directory('client/build/static/media/', path)
@app.route('/testGenes', methods=["POST"])
def test_genes():
genes = request.get_json()["genes"]
return json.dumps(externalApiCalls.testGenes(genes, cache))
@app.route('/fullTestGenes', methods=["POST"])
def full_test_genes():
genes = request.get_json()["genes"]
return json.dumps(externalApiCalls.fullTestGenes(genes, cache))
@app.route("/context", methods=["GET"])
def context():
response = {
'datasets': datasets,
'statistics': statistics,
'algorithms': availableAlgorithms.algorithms
}
return json.dumps(response)
@app.route("/runAlgorithm", methods=["POST"])
def runSpecificAlgorithm():
requestObject = request.get_json()
algorithm = requestObject["algorithm"]
oneAgainstRest = requestObject["oneAgainstRest"]
oversampling = requestObject["oversampling"]
if "dataset" not in algorithm:
return abort(400, "need dataset parameter")
dataset = algorithm["dataset"]
if dataset not in dataLoaders:
return abort(400, "unknown datatset id")
dataLoader = dataLoaders[dataset]
cache_key = "_".join((
"V2",
dataset,
algorithm["key"],
str(oneAgainstRest),
str(oversampling),
"-".join([key+str(value) for key,value in algorithm["parameters"].items()]),
"-".join(algorithm["cancerTypes"]),
"-".join(algorithm["healthyTissueTypes"]),
"-".join(algorithm["sickTissueTypes"])
))
if cache.isCached(cache_key):
return cache.getCache(cache_key)
response = algorithmExecution.execute(algorithm, dataLoader, oneAgainstRest, oversampling)
# workaround to replace NaN by null
json_response = json.dumps(response)
json_respone = re.sub(r'\bNaN\b', 'null', json_response)
cache.cache(cache_key, json_respone)
return json_respone
def flaskrun(app, default_host="0.0.0.0",
default_port="5000"):
"""
Takes a flask.Flask instance and runs it. Parses
command-line flags to configure the app.
"""
# Set up the command-line options
parser = optparse.OptionParser()
parser.add_option("-H", "--host",
help="Hostname of the Flask app " +
"[default %s]" % default_host,
default=default_host)
parser.add_option("-t", "--threaded",
action="store_true",
dest="threaded",
default=False,
help=optparse.SUPPRESS_HELP)
parser.add_option("-p", "--processes",
dest="processes",
default=1,
help=optparse.SUPPRESS_HELP)
parser.add_option("-P", "--port",
help="Port for the Flask app " +
"[default %s]" % default_port,
default=default_port)
parser.add_option("-d", "--debug",
action="store_true", dest="debug",
help=optparse.SUPPRESS_HELP)
options, _ = parser.parse_args()
run_simple(options.host, int(options.port), app,
use_reloader=options.debug,
threaded=options.threaded,
processes=int(options.processes)
)
if __name__ == '__main__':
flaskrun(app)