-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgwasrv.py
239 lines (193 loc) · 8.51 KB
/
gwasrv.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
import falcon
import json
import logging
import os
import tempfile
from pygwas.core import ld
from pygwas.core import genotype
from pygwas.core import phenotype
from wsgiref import simple_server
from pygwas import pygwas
import mimetypes
import numpy as np
GWAS_STUDY_FOLDER=os.environ['GWAS_STUDY_FOLDER']
GWAS_VIEWER_FOLDER=os.environ['GWAS_VIEWER_FOLDER']
GENOTYPE_FOLDER=os.environ['GENOTYPE_FOLDER']
class RequireJSON(object):
def process_request(self, req, resp):
if not req.client_accepts_json:
raise falcon.HTTPNotAcceptable(
'This API only supports responses encoded as JSON.',
href='http://docs.examples.com/api/json')
'''if req.method in ('POST', 'PUT'):
if 'application/json' not in req.content_type:
raise falcon.HTTPUnsupportedMediaType(
'This API only supports requests encoded as JSON.',
href='http://docs.examples.com/api/json')'''
class JSONTranslator(object):
def process_request(self, req, resp):
# req.stream corresponds to the WSGI wsgi.input environ variable,
# and allows you to read bytes from the request body.
#
# See also: PEP 3333
if req.content_length in (None, 0) or 'application/json' not in req.content_type:
# Nothing to do
return
body = req.stream.read()
if not body:
raise falcon.HTTPBadRequest('Empty request body',
'A valid JSON document is required.')
try:
req.context['doc'] = json.loads(body.decode('utf-8'))
except (ValueError, UnicodeDecodeError):
raise falcon.HTTPError(falcon.HTTP_753,
'Malformed JSON',
'Could not decode the request body. The '
'JSON was incorrect or not encoded as '
'UTF-8.')
def process_response(self, req, resp, resource):
if 'result' not in req.context:
return
resp.body = json.dumps(req.context['result'])
class LdForSnpResource(object):
def __init__(self, storage_path):
self.storage_path = storage_path
self.logger = logging.getLogger(__name__)
def on_get(self, req, resp,analysis_id,chr,position):
position = int(position)
ld_data = ld.get_ld_for_snp('%s/%s.hdf5' % (self.storage_path,analysis_id),chr,position)
req.context['result'] = ld_data
resp.status = falcon.HTTP_200
class LdForRegionResource(object):
def __init__(self, storage_path):
self.storage_path = storage_path
self.logger = logging.getLogger(__name__)
def on_get(self, req, resp,analysis_id,chr,start_pos,end_pos):
start_pos = int(start_pos)
end_pos = int(end_pos)
ld_data = _replace_NaN(ld.get_ld_for_region('%s/%s.hdf5' % (self.storage_path,analysis_id),chr,start_pos,end_pos))
req.context['result'] = ld_data
resp.status = falcon.HTTP_200
class LdExactForRegionResource(object):
def __init__(self, storage_path):
self.storage_path = storage_path
self.logger = logging.getLogger(__name__)
def on_post(self, req, resp,genotype_id,chr,position):
#filter nan
position = int(position)
genotypeData = genotype.load_hdf5_genotype_data('%s/%s/all_chromosomes_binary.hdf5' % (self.storage_path,genotype_id))
num_snps = int(req.params.get('num_snps',250))
accessions = req.context.get('doc',[])
ld_data = _replace_NaN(ld.calculate_ld_for_region(genotypeData,accessions,chr,position,num_snps=num_snps))
req.context['result'] = ld_data
resp.status = falcon.HTTP_200
class StatisticsResource(object):
def __init__(self,storage_path):
self.storage_path = storage_path
self.logger = logging.getLogger(__name__)
def on_post(self,req,resp,genotype_id,type):
phenotypes = zip(*req.context.get('doc',[]))
accessions = phenotypes[0]
values = phenotypes[1]
phen_data = phenotype.Phenotype(accessions,values,'phenotype')
genotype_folder = '%s/%s' % (self.storage_path,genotype_id)
arguments = {'type':type,'genotype_folder':genotype_folder,'phen_data':phen_data,'file':''}
statistics = pygwas.calculate_stats(arguments)
req.context['result'] = statistics
resp.status = falcon.HTTP_200
class PlottingGwasResource(object):
def __init__(self,study_path,viewer_path):
self.study_path = study_path
self.viewer_path = viewer_path
self.logger = logging.getLogger(__name__)
def on_get(self, req, resp,type,id):
if type not in ('study','viewer'):
raise falcon.HTTPNotFound()
file = '%s/%s.hdf5' % (self.viewer_path if type == 'viewer' else self.study_path,id)
_gwas_plot(file,resp,req)
class PlottingGenericResource(object):
def __init__(self,plot_func):
self.logger = logging.getLogger(__name__)
self.plot_func = plot_func
def on_post(self,req,resp):
'''Sending a gwas result file to plot'''
ext = mimetypes.guess_extension(req.content_type)
if not ext and req.content_type == 'application/hdf5':
ext = 'hdf'
if ext not in ('hdf','csv'):
raise falcon.HTTPUnsupportedMediaType('Only hdf5 and csv files are supported')
_,file = tempfile.mkstemp(suffix='.%s' % ext)
with open(file, 'wb') as f:
while True:
chunk = req.stream.read(4096)
if not chunk:
break
f.write(chunk)
self.plot_func(file,resp,req)
os.unlink(file)
class PlottingQQResource(object):
def __init__(self,study_path,viewer_path):
self.study_path = study_path
self.viewer_path = viewer_path
self.logger = logging.getLogger(__name__)
def on_get(self, req, resp,type,id):
if type not in ('study','viewer'):
raise falcon.HTTPNotFound()
file = '%s/%s.hdf5' % (self.viewer_path if type == 'viewer' else self.study_path,id)
format = req.params.get('format','png')
if format not in ('png','pdf'):
raise falcon.HTTPUnsupportedMediaType('Only png and pdf formats are supported')
_qq_plot(file,resp,req)
def _replace_NaN(ld_data):
ld_data['r2'] = map(lambda x: np.nan_to_num(x).tolist(),ld_data['r2'])
return ld_data
def _qq_plot(file,resp,req):
_plot(file,resp,req,pygwas.qq_plot,{})
def _gwas_plot(file,resp,req):
args = {}
args['chr'] = req.params.get('chr',None)
args['macs'] = int(req.params.get('macs',15))
args['fdr'] = 'all'
args['colored'] = False
args['marker_size'] = int(req.params.get('marker_size',10))
_plot(file,resp,req,pygwas.plot,args)
def _plot(file,resp,req,plot_func,args):
format = req.params.get('format','png')
if format not in ('png','pdf'):
raise falcon.HTTPUnsupportedMediaType('Only png and pdf formats are supported')
args['file'] = file
_,args['output'] = tempfile.mkstemp(suffix='.%s' % format)
try:
plot_func(args)
resp.content_type = 'application/pdf' if format == 'pdf' else 'image/png'
with open(args['output'],'rb') as f:
resp.body = f.read()
except Exception as err:
raise err
finally:
os.unlink(args['output'])
api = falcon.API(middleware=[
RequireJSON(),
JSONTranslator(),
])
ldForSnp = LdForSnpResource(GWAS_STUDY_FOLDER)
ldForRegion = LdForRegionResource(GWAS_STUDY_FOLDER)
ldForExactRegion = LdExactForRegionResource(GENOTYPE_FOLDER)
plotting_gwas = PlottingGwasResource(GWAS_STUDY_FOLDER,GWAS_VIEWER_FOLDER)
plotting_gwas_generic = PlottingGenericResource(_gwas_plot)
plotting_qq_generic = PlottingGenericResource(_qq_plot)
plotting_qq = PlottingQQResource(GWAS_STUDY_FOLDER,GWAS_VIEWER_FOLDER)
statistics = StatisticsResource(GENOTYPE_FOLDER)
api.add_route('/analysis/{analysis_id}/ld/{chr}/{position}', ldForSnp)
api.add_route('/analysis/{analysis_id}/ld/region/{chr}/{start_pos}/{end_pos}', ldForRegion)
api.add_route('/ld/{genotype_id}/{chr}/{position}',ldForExactRegion)
api.add_route('/plotting/{type}/{id}/gwas',plotting_gwas)
api.add_route('/plotting/{type}/{id}/qq',plotting_qq)
api.add_route('/plotting/gwas',plotting_gwas_generic)
api.add_route('/plotting/qq',plotting_qq_generic)
api.add_route('/statistics/{genotype_id}/{type}',statistics)
def main():
httpd = simple_server.make_server('0.0.0.0', 8009, api)
httpd.serve_forever()
if __name__ == '__main__':
main()