Skip to content

Commit c529469

Browse files
committed
added basic implementation
1 parent e41984b commit c529469

File tree

3 files changed

+262
-0
lines changed

3 files changed

+262
-0
lines changed

.gitignore

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,3 +60,10 @@ target/
6060

6161
#Ipython Notebook
6262
.ipynb_checkpoints
63+
64+
# Flask
65+
flask
66+
67+
# IntelliJ
68+
*.iml
69+
.idea

README.md

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
#Python Beacon
2+
3+
##Contents
4+
5+
* [What it is](#what-it-is)
6+
* [System requirements](#system-requirements)
7+
* [How to run it](#how-to-run-it)
8+
* [How it works](#how-it-works)
9+
* [Technologies](#technologies)
10+
11+
##What it is
12+
This project contains BDK (beacon development kit) for Python developers. It provides a skeleton of a simple beacon allowing the developers to plug in their own data/functionality. The API makes sure the response produced is compatible with what the Beacon of Beacons can consume.
13+
14+
##System requirements
15+
All you need to build this project is Python and Flask web framework. If you're running Linux, OS X or Windows with cygwin support, the following commands should be enough to set up Flask (the process is a bit different with the native version of Python on Windows):
16+
17+
$ cd todo-api
18+
$ virtualenv flask
19+
$ flask/bin/pip install flask
20+
21+
##How to run it
22+
Launch beacon.py:
23+
24+
$ chmod a+x beacon.py
25+
$ ./beacon.py
26+
27+
This starts an embedded server. By default, the application will be available at <http://127.0.0.1:5000>
28+
29+
##How it works
30+
In order to implement a beacon, simply override beacon details and query function in beacon.py (marked with TODO in the source code).
31+
32+
The API takes care of the rest and provides the following endpoints when you start your beacon:
33+
34+
http://127.0.0.1:5000/beacon-python/info - information about your beacon
35+
http://127.0.0.1:5000/beacon-python/query - access to query service
36+
37+
Query example:
38+
39+
GET http://127.0.0.1:5000/beacon-python/query?chrom=15&pos=41087870&allele=A&ref=hg19
40+
41+
##Technologies
42+
Python, Flask.

beacon.py

Lines changed: 213 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,213 @@
1+
#!flask/bin/python
2+
3+
'''
4+
The MIT License
5+
6+
Copyright 2014 DNAstack.
7+
8+
Permission is hereby granted, free of charge, to any person obtaining a copy
9+
of this software and associated documentation files (the "Software"), to deal
10+
in the Software without restriction, including without limitation the rights
11+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
copies of the Software, and to permit persons to whom the Software is
13+
furnished to do so, subject to the following conditions:
14+
15+
The above copyright notice and this permission notice shall be included in
16+
all copies or substantial portions of the Software.
17+
18+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24+
THE SOFTWARE.
25+
'''
26+
27+
from flask import Flask, jsonify, request
28+
29+
30+
class IncompleteQuery(Exception):
31+
status_code = 400
32+
33+
def __init__(self, message, status_code=None, payload=None, ErrorResource=None, query=None, beacon_id=None):
34+
Exception.__init__(self)
35+
self.message = message
36+
if status_code is not None:
37+
self.status_code = status_code
38+
self.payload = payload
39+
self.ErrorResource = ErrorResource
40+
self.query = query
41+
self.beacon_id = beacon_id
42+
43+
def to_dict(self):
44+
rv = dict(self.payload or ())
45+
rv["beacon"] = self.beacon_id
46+
rv["query"] = self.query
47+
rv['error'] = self.ErrorResource
48+
return rv
49+
50+
51+
app = Flask(__name__)
52+
53+
# --------------- Information endpont (start) --------------------#
54+
55+
# TODO: override with the details of your beacon
56+
57+
########### DataSetResource for beacon details ############
58+
59+
# required field(s): name
60+
DataUseRequirementResource = {
61+
'name': u'example name',
62+
'description': u'example description'
63+
}
64+
65+
# required field(s): variants
66+
DataSizeResource = {
67+
'variants': 1, # integer
68+
'samples': 1 # integer
69+
}
70+
71+
# required field(s): category
72+
DataUseResource = {
73+
'category': u'example use category',
74+
'description': u'example description',
75+
'requirements': [
76+
DataUseRequirementResource
77+
]
78+
}
79+
80+
# required field(s): id
81+
DataSetResource = {
82+
'id': u'example Id',
83+
'description': u'dataset description',
84+
'reference': u'reference genome',
85+
'size': DataSizeResource, # Dimensions of the data set (required if the beacon reports allele frequencies)
86+
'data_uses': [
87+
DataUseResource # Data use limitations
88+
]
89+
}
90+
91+
########### QueryResource for beacon details ###############
92+
93+
# required field(s): allele, chromosome, position, reference
94+
QueryResource = {
95+
'allele': u'allele string',
96+
'chromosome': u'chromosome Id',
97+
'position': 1, # integer
98+
'reference': u'genome Id',
99+
'dataset_id': u'dataset Id'
100+
}
101+
102+
################### Beacon details #########################
103+
104+
# required field(s): id, name, organization, api
105+
beacon = {
106+
'id': u'foo',
107+
'name': u'bar',
108+
'organization': u'org',
109+
'api': u'0.1/0.2',
110+
'description': u'beacon description',
111+
'datasets': [
112+
DataSetResource # Datasets served by the beacon
113+
],
114+
'homepage': u'http://dnastack.com/ga4gh/bob/',
115+
'email': u'beacon@dnastack.com',
116+
'auth': u'oauth2', # OAUTH2, defaults to none
117+
'queries': [
118+
QueryResource # Examples of interesting queries
119+
]
120+
}
121+
122+
123+
# --------------- Information endpoint (end) ----------------------#
124+
125+
# info function
126+
@app.route('/beacon-python/info', methods=['GET'])
127+
def info():
128+
return jsonify(beacon)
129+
130+
131+
# query function
132+
# TODO: plug in the functionality of your beacon
133+
@app.route('/beacon-python/query', methods=['GET'])
134+
def query():
135+
# parse query
136+
chromosome = request.args.get('chrom')
137+
position = long(request.args.get('pos'))
138+
allele = request.args.get('allele')
139+
reference = request.args.get('ref')
140+
dataset = request.args.get('dataset') if 'dataset' in request.args else beacon['datasets'][0]['id']
141+
142+
# ---- TODO: override with the necessary response details ----#
143+
144+
############## AlleleResource for response ###############
145+
146+
# required field(s): allele
147+
AlleleResource = {
148+
'allele': allele,
149+
'frequency': 0.5 # double between 0 & 1
150+
}
151+
152+
############# ErrorResource for response #################
153+
154+
# required field(s): name
155+
ErrorResource = {
156+
'name': u'error name/code',
157+
'description': u'error message'
158+
}
159+
160+
################### Response object #########################
161+
162+
# generate response
163+
# required field(s): exists
164+
response = {
165+
'exists': True,
166+
'observed': 0, # integer, min 0
167+
'alleles': [
168+
AlleleResource
169+
],
170+
'info': u'response information',
171+
}
172+
173+
query = {
174+
'chromosome': chromosome,
175+
'position': position,
176+
'allele': allele,
177+
'reference': reference,
178+
'dataset_id': dataset
179+
}
180+
181+
if query['chromosome'] is None or query['position'] is None or query['allele'] is None or query[
182+
'reference'] is None:
183+
ErrorResource['description'] = 'Required parameters are missing'
184+
ErrorResource['name'] = 'Incomplete Query'
185+
raise IncompleteQuery('IncompleteQuery', status_code=410, ErrorResource=ErrorResource, query=query,
186+
beacon_id=beacon["id"])
187+
188+
# --------------------------------------------------------------#
189+
190+
return jsonify({"beacon": beacon['id'], "query": query, "response": response})
191+
192+
193+
# info function
194+
@app.route('/beacon-python/', methods=['GET'])
195+
def welcome():
196+
return 'WELCOME!!! Beacon of Beacons Project (BoB) provides a unified REST API to publicly available GA4GH Beacons. BoB standardizes the way beacons are accessed and aggregates their results, thus addressing one of the missing parts of the Beacon project itself. BoB was designed with ease of programmatic access in mind. It provides XML, JSON and plaintext responses to accommodate needs of all the clients across all the programming languages. The API to use is determined using the header supplied by the client in its GET request, e.g.: "Accept: application/json".'
197+
198+
199+
# required parameters missing
200+
@app.errorhandler(IncompleteQuery)
201+
def handle_invalid_usage(error):
202+
response = jsonify(error.to_dict())
203+
return response
204+
205+
206+
# page not found
207+
@app.errorhandler(404)
208+
def not_found(error):
209+
return 'Page not found (Bad URL)', 404
210+
211+
212+
if __name__ == '__main__':
213+
app.run()

0 commit comments

Comments
 (0)