|
| 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