-
Notifications
You must be signed in to change notification settings - Fork 0
/
run_e_service.py
127 lines (105 loc) · 4.03 KB
/
run_e_service.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
__author__ = 'dexter'
# This script is called from the command line to run the enrichment server with the persisted e_sets
#
# The script reads all of the e_sets and then starts the bottle server
#
# The optional argument 'verbose' specifies verbose logging for testing
#
#
# python run_e_service.py
#
# python run_e_service.py --verbose
#
# body
import argparse
from bottle import route, run, template, default_app, request, response
import data_model as dm
import json
import term2gene_mapper
parser = argparse.ArgumentParser(description='run the enrichment server')
parser.add_argument('--verbose', dest='verbose', action='store_const',
const=True, default=False,
help='verbose mode')
arg = parser.parse_args()
if arg.verbose:
print "Starting enrichment server in verbose mode"
else:
print "Starting enrichment server"
e_data = dm.EnrichmentData("e_sets")
e_data.load()
app = default_app()
app.config['enrichment_data'] = e_data
app.config['enrichment_verbose'] = arg.verbose
class EnableCors(object):
name = 'enable_cors'
api = 2
def apply(self, fn, context):
def _enable_cors(*args, **kwargs):
# set CORS headers
response.headers['Access-Control-Allow-Origin'] = '*'
response.headers['Access-Control-Allow-Methods'] = 'GET, POST, OPTIONS'
response.headers['Access-Control-Allow-Headers'] = 'Origin, Accept, Content-Type, X-Requested-With, X-CSRF-Token'
if request.method != 'OPTIONS':
# actual request; reply with the actual response
return fn(*args, **kwargs)
return _enable_cors
@route('/')
def get_status():
version = '1.0.0'
status = {'status': 'available', 'version': version, 'service': 'enrich' }
return status
@route('/hello/<name>',method=['OPTIONS','GET'])
def index(name):
verbose_mode = app.config.get("enrichment_verbose")
if verbose_mode:
return template('<b>This is the test method saying Hello verbosely to {{name}}</b>!', name=name)
else:
return template('<b>Hello {{name}}</b>!', name=name)
# GET the list of all the loaded e_sets
@route('/esets', method=['OPTIONS','GET'])
def get_e_sets():
e_data = app.config.get("enrichment_data")
result = []
url=request.url
for eset_name in e_data.get_e_set_names() :
result.append(url + "/" + eset_name)
return { "e_sets": result} # json.dumps(result)
# GET the information for one e_set, all the id_set names and meta information
@route('/esets/<esetname>', method=['OPTIONS','GET'])
def get_id_sets(esetname):
e_data = app.config.get("enrichment_data")
return e_data.get_id_set_names(esetname, url=request.url)
# GET the information for one id_set
@route('/esets/<esetname>/idsets/<idsetid>' , method=['OPTIONS','GET'] )
def get_id_set(esetname, idsetid):
e_data = app.config.get("enrichment_data")
id_set = e_data.get_id_set(esetname, idsetid)
if not id_set:
return {}
return id_set.to_dict()
@route('/esets/query', method=['OPTIONS','POST'])
def run_query():
verbose_mode = app.config.get("enrichment_verbose")
e_data = app.config.get("enrichment_data")
data = request.body
# t = type(data)
# s = str(data)
dict = json.load(data)
esetname = dict.get("eset")
query_ids = dict.get('ids')
skip_normalization = dict.get('skip_query_normalization')
# query_id_set = dm.IdentifierSet({"ids": query_ids})
# if skip_normalization == None:
# skip_normalization = True
if skip_normalization == None or skip_normalization:
matched_genes = {}
for term in query_ids:
matched_genes[term] = [term]
standardized_search_terms = {'matched': matched_genes, 'unmatched':[]}
else:
term_mapper = term2gene_mapper.Term2gene_mapper()
standardized_search_terms = term_mapper.standarize_terms(query_ids)
result = e_data.get_scores_on_standarized_query_terms(esetname, standardized_search_terms, verbose_mode)
return result
app.install(EnableCors())
app.run(host='0.0.0.0', port=8090)