Skip to content

Commit

Permalink
Use dsl query builder signature
Browse files Browse the repository at this point in the history
  • Loading branch information
Jerry committed Apr 6, 2020
1 parent 7fa1fbf commit c5b8940
Show file tree
Hide file tree
Showing 4 changed files with 396 additions and 535 deletions.
161 changes: 76 additions & 85 deletions src/web/api/handlers.py
Original file line number Diff line number Diff line change
@@ -1,105 +1,96 @@
# -*- coding: utf-8 -*-
from biothings.web.api.es.handlers import BiothingHandler
from biothings.web.api.es.handlers import MetadataHandler
from biothings.web.api.es.handlers import QueryHandler
from biothings.web.api.es.handlers import StatusHandler
from biothings.utils.version import get_repository_information
from tornado.web import RequestHandler
from collections import OrderedDict
import requests
import os

#import logging


def get_es_index(inst, options):
return inst.web_settings.ES_INDEX
# if 'all' in options.esqb_kwargs.species or len(set(options.esqb_kwargs.species)-
# set([x['tax_id'] for x in inst.web_settings.TAXONOMY.values()])) > 0:
# return inst.web_settings.ES_INDEX
# else:
# return inst.web_settings.ES_INDEX_TIER1


class FrontPageHandler(BiothingHandler):
def get(self):
self.write("MYGENE FRONTPAGE!")


class GeneHandler(BiothingHandler):
''' This class is for the /gene endpoint. '''
import requests

def _get_es_index(self, options):
return get_es_index(self, options)
from biothings.utils.version import (get_repository_information,
get_software_info)
from biothings.web.api.es.handlers import (QueryHandler,
MetadataFieldHandler,
MetadataSourceHandler)


class QueryHandler(QueryHandler):
class MygeneQueryHandler(QueryHandler):
''' This class is for the /query endpoint. '''

def _pre_query_builder_GET_hook(self, options):
if options.esqb_kwargs.include_tax_tree and 'all' not in options.esqb_kwargs.species:
headers = {
'user-agent': 'Python-requests_mygene.info/{} (gzip)'.format(requests.__version__)}

body = {
"ids": ','.join([str(sid) for sid in options.esqb_kwargs.species]),
"expand_species": 'True'
}
def pre_query_builder_hook(self, options):

res = requests.post(
self.web_settings.INCLUDE_TAX_TREE_REDIRECT_ENDPOINT, data=body, headers=headers)
if self.request.method == 'GET':
if options.esqb.include_tax_tree and \
'all' not in options.esqb.species:
headers = {
'user-agent': 'Python-requests_mygene.info/{} (gzip)'.format(requests.__version__)}

if res.status_code == requests.codes.ok:
options['esqb_kwargs']['species'] = [str(x) for x in res.json()]
# logging.debug('tax_tree species: {}'.format(options.esqb_kwargs.species))

return options
body = {
"ids": ','.join([str(sid) for sid in options.esqb.species]),
"expand_species": 'True'
}

def _get_es_index(self, options):
return get_es_index(self, options)
res = requests.post(
self.web_settings.INCLUDE_TAX_TREE_REDIRECT_ENDPOINT, data=body, headers=headers)

if res.status_code == requests.codes.ok:
options['esqb']['species'] = [str(x) for x in res.json()]
# logging.debug('tax_tree species: {}'.format(options.esqb_kwargs.species))

class StatusHandler(StatusHandler):
''' This class is for the /status endpoint. '''
pass
return options


class MetadataHandler(MetadataHandler):
''' This class is for the /metadata endpoint. '''
class MygeneSourceHandler(MetadataSourceHandler):
"""
GET /metadata
GET /v3/metadata
{
"app_revision": "", # TODO
"available_fields": "http://mygene.info/metadata/fields",
"biothing_type": "gene",
"build_date": "2020-03-29T04:00:00.012426",
"build_version": "20200329",
"genome_assembly": {
"human": "hg38",
"mouse": "mm10",
...
},
"source": null,
"src": { ... }, // 28 items
"stats": {
"total": 36232158,
"total_genes": 36232158,
"total_entrez_genes": 27119488,
"total_ensembl_genes": 38915576,
"total_ensembl_genes_mapped_to_entrez": 5954466,
"total_ensembl_only_genes": 9112670,
"total_species": 28605
},
"taxonomy": {
"human": 9606,
"mouse": 10090,
...
}
}
"""

def _pre_finish_GET_hook(self, options, res):
if not self.request.path.endswith('fields'):
# Add mygene specific metadata stuff
res['available_fields'] = 'http://mygene.info/metadata/fields'
res['app_revision'] = get_repository_information(
app_dir=self.web_settings._app_git_repo).get(
'commit-hash', '')
res['genome_assembly'] = {}
res['taxonomy'] = {}
for (s, d) in self.web_settings.TAXONOMY.items():
if 'tax_id' in d:
res['taxonomy'][s] = int(d['tax_id'])
if 'assembly' in d:
res['genome_assembly'][s] = d['assembly']
if "source" not in res:
# occurs when loaded from scratch, not from a change/diff file
res["source"] = None
res = OrderedDict(sorted(list(res.items()), key=lambda x: x[0]))
return res
def get(self):

self.web_settings.read_index_mappings()
res = self.web_settings.source_metadata[self.biothing_type]
software = get_software_info(app_dir=self.web_settings.get_git_repo_path())

class TaxonHandler(RequestHandler):
def initialize(self, web_settings):
pass
if self.kwargs.source.dev:
res['software'] = software

def get(self, taxid):
self.redirect("http://t.biothings.io/v1/taxon/%s?include_children=1" % taxid)
res['available_fields'] = 'http://mygene.info/metadata/fields'
res['app_revision'] = software.get('commit-hash', '')
res['genome_assembly'] = {}
res['taxonomy'] = {}

for s, d in self.web_settings.TAXONOMY.items():
if 'tax_id' in d:
res['taxonomy'][s] = int(d['tax_id'])
if 'assembly' in d:
res['genome_assembly'][s] = d['assembly']

class DemoHandler(RequestHandler):
def initialize(self, web_settings):
pass
if "source" not in res:
# occurs when loaded from scratch, not from a change/diff file
res["source"] = None

def get(self):
with open('../docs/demo/index.html', 'r') as demo_file:
self.write(demo_file.read())
self.finish(dict(sorted(res.items())))

0 comments on commit c5b8940

Please sign in to comment.