Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
Initially gwaportal-gwas-server supports retrieving LD data and calculating LD. I
  • Loading branch information
timeu committed May 21, 2016
0 parents commit 02cca5c
Show file tree
Hide file tree
Showing 8 changed files with 269 additions and 0 deletions.
51 changes: 51 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
*.py[cod]

# C extensions
*.c
*.so

# Jython
*$py.class

# Packages
*.egg
*.egg-info
dist
build
eggs
parts
var
sdist
develop-eggs
.installed.cfg
lib
lib64

# Installer logs
pip-log.txt

# Unit test / coverage reports
.coverage
.coverage_*
.tox
nosetests.xml
htmlcov
*.dat

# Docs
doc/_build

# Translations
*.mo

# Idea
.idea

# System
.DS_Store

# VIM swap files
.*.swp

# VIM temp files
*~
22 changes: 22 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Dockerfile for docker-gwaportal-gwas-server
# Version 0.1

FROM timeu/docker-gwas-base
MAINTAINER Uemit Seren <[email protected]>

WORKDIR /tmp

RUN mkdir /GWAS_STUDY_FOLDER && mkdir /GENOTYPE_FOLDER

COPY . /tmp

RUN /env/bin/pip install . && rm -fr /tmp/*

VOLUME ["/GWAS_STUDY_FOLDER","/GENOTYPE_FOLDER"]


ENV GWAS_STUDY_FOLDER /GWAS_STUDY_FOLDER

ENV GENOTYPE_FOLDER /GENOTYPE_FOLDER

CMD ["/env/bin/gunicorn","gwasrv:api"]
20 changes: 20 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
The MIT License (MIT)

Copyright (c) 2014 Ümit Seren

Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
1 change: 1 addition & 0 deletions MANIFEST.in
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
include README.rst
1 change: 1 addition & 0 deletions README.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
gwaportal-hdf5-server
123 changes: 123 additions & 0 deletions gwasrv.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
import falcon
import json
import logging
import os
from pygwas.core import ld
from pygwas.core import genotype
from wsgiref import simple_server

GWAS_STUDY_FOLDER=os.environ['GWAS_STUDY_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):
# 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 = 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 = ld.calculate_ld_for_region(genotypeData,accessions,chr,position,num_snps=num_snps)
req.context['result'] = ld_data
resp.status = falcon.HTTP_200



api = falcon.API(middleware=[
RequireJSON(),
JSONTranslator(),
])

ldForSnp = LdForSnpResource(GWAS_STUDY_FOLDER)
ldForRegion = LdForRegionResource(GWAS_STUDY_FOLDER)
ldForExactRegion = LdExactForRegionResource(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)

def main():
httpd = simple_server.make_server('127.0.0.1', 8000, api)
httpd.serve_forever()

if __name__ == '__main__':
main()
5 changes: 5 additions & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[bdist_wheel]
# This flag says that the code is written to work on both Python 2 and Python
# 3. If at all possible, it is good practice to do this. If you cannot, you
# will need to generate wheels for each Python version that you support.
universal=1
46 changes: 46 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
from setuptools import setup, find_packages # Always prefer setuptools over distutils
from codecs import open # To use a consistent encoding
from os import path

here = path.abspath(path.dirname(__file__))

# Get the long description from the relevant file
with open(path.join(here, 'README.rst'), encoding='utf-8') as f:
long_description = f.read()

setup(
name='gwasrv',
version="0.0.1",
description='A RESTful backend for accessing GWAS HDF5 files GWA-Portal',
long_description=long_description,
url='https://github.com/timeu/gwaportal-gwas-server',
author='Uemit Seren',
author_email='[email protected]',
license='MIT',
classifiers=[
'Development Status :: 5 - Production/Stable',
'Intended Audience :: Developers',
'Topic :: Scientific/Engineering :: Bio-Informatics',
'License :: OSI Approved :: MIT License',
'Programming Language :: Python :: 2',
'Programming Language :: Python :: 2.6',
'Programming Language :: Python :: 2.7',
'Programming Language :: Python :: 3',
'Programming Language :: Python :: 3.2',
'Programming Language :: Python :: 3.3',
'Programming Language :: Python :: 3.4',
],
keywords='GWAS hdf5 GWA-Portal',
py_modules=['gwasrv'],
install_requires=[
"PyGWAS >= 1.1.0",
"gunicorn >=19.0.0",
"falcon >= 0.3.0",
],
entry_points={
'console_scripts': [
'gwasrv=gwasrv:main'
],
},
)

0 comments on commit 02cca5c

Please sign in to comment.