Skip to content

Commit

Permalink
Update with compath_utils
Browse files Browse the repository at this point in the history
  • Loading branch information
cthoyt committed Apr 18, 2018
1 parent ceb87f8 commit 7cfa348
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 233 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -100,3 +100,4 @@ ENV/
# mypy
.mypy_cache/
.idea/
.pytest_cache
3 changes: 2 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
click
bio2bel>=0.0.7
compath_utils>=0.0.2
pybel[deployment]>=0.11.1
sqlalchemy==1.1.15
bio2bel_hgnc>=0.0.4
bio2bel_chebi>=0.0.4
pandas
tqdm
openpyxl
bio2bel
flask[web]
flask_admin[web]
3 changes: 2 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
META_PATH = os.path.join('src', 'bio2bel_{}'.format(BIO2BEL_MODULE), '__init__.py')
INSTALL_REQUIRES = [
'click',
'bio2bel',
'bio2bel>=0.0.7',
'compath_utils>=0.0.2',
'bio2bel_hgnc>=0.0.4',
'bio2bel_chebi>=0.0.4',
'pybel[deployment]>=0.11.1',
Expand Down
93 changes: 3 additions & 90 deletions src/bio2bel_reactome/cli.py
Original file line number Diff line number Diff line change
@@ -1,91 +1,18 @@
# -*- coding: utf-8 -*-

from __future__ import print_function

import logging
import os
import sys

import click
from pybel_tools.ols_utils import OlsNamespaceOntology

from .constants import DEFAULT_CACHE_CONNECTION, MODULE_DOMAIN, MODULE_FUNCTION, MODULE_NAME
from bio2bel import build_cli
from .constants import DEFAULT_CACHE_CONNECTION
from .manager import Manager
from .to_belns import deploy_to_arty
from .utils import dict_to_pandas_df

log = logging.getLogger(__name__)


def set_debug(level):
logging.basicConfig(level=level, format="%(asctime)s - %(levelname)s - %(message)s")
log.setLevel(level=level)


def set_debug_param(debug):
if debug == 0:
set_debug(30)
elif debug == 1:
set_debug(20)
elif debug == 2:
set_debug(10)


@click.group(help='Convert Reactome to BEL. Default connection at {}'.format(DEFAULT_CACHE_CONNECTION))
def main():
pass


@main.command()
@click.option('-v', '--debug', count=True, help="Turn on debugging.")
@click.option('-c', '--connection', help="Defaults to {}".format(DEFAULT_CACHE_CONNECTION))
@click.option('-d', '--reset-db', default=True)
@click.option('-n', '--not-only-human', is_flag=True, help="Do not only build with human")
def populate(debug, connection, reset_db, not_only_human):
"""Build the local version of Reactome."""

set_debug_param(debug)

m = Manager(connection=connection)

if reset_db is True:
log.info('Deleting the previous instance of the database')
m.drop_all()
log.info('Creating new models')
m.create_all()

m.populate(only_human=(not not_only_human))


@main.command()
@click.option('-v', '--debug', count=True, help="Turn on debugging.")
@click.option('-y', '--yes', is_flag=True)
@click.option('-c', '--connection', help='Defaults to {}'.format(DEFAULT_CACHE_CONNECTION))
def drop(debug, yes, connection):
"""Drop the Reactome database."""

set_debug_param(debug)

if yes or click.confirm('Do you really want to delete the database?'):
m = Manager(connection=connection)
click.echo("drop db")
m.drop_all()


@main.command()
@click.option('--force', is_flag=True, help="Force knowledge to be uploaded even if not new namespace")
def deploy(force):
"""Deploy to Artifactory"""
deploy_to_arty(not force)


@main.command()
@click.option('-b', '--ols-base', help="Custom OLS base url")
@click.option('-o', '--output', type=click.File('w'), default=sys.stdout)
def write(ols_base, output):
"""Writes BEL namespace"""
ontology = OlsNamespaceOntology(MODULE_NAME, MODULE_DOMAIN, bel_function=MODULE_FUNCTION, ols_base=ols_base)
ontology.write_namespace(output)
main = build_cli(Manager)


@main.command()
Expand All @@ -105,19 +32,5 @@ def export(connection, species, top_hierarchy):
genesets.to_excel('reactome_gene_sets.xlsx', index=False)


@main.command()
@click.option('-v', '--debug', count=True, help="Turn on debugging.")
@click.option('-c', '--connection', help="Defaults to {}".format(DEFAULT_CACHE_CONNECTION))
@click.option('-p', '--port', type=int)
@click.option('-h', '--host')
def web(debug, connection, port, host):
"""Run web"""
set_debug_param(debug)

from bio2bel_reactome.web import create_app
app = create_app(connection=connection)
app.run(host=host, port=port)


if __name__ == '__main__':
main()
121 changes: 51 additions & 70 deletions src/bio2bel_reactome/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,12 @@
import logging
from collections import Counter

from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker, scoped_session
from tqdm import tqdm

from bio2bel.utils import get_connection
from bio2bel import bio2bel_populater
from bio2bel_chebi.manager import Manager as ChebiManager
from bio2bel_hgnc.manager import Manager as HgncManager
from compath_utils import CompathManager
from .constants import MODULE_NAME
from .models import Base, Chemical, Pathway, Protein, Species
from .parsers import *
Expand All @@ -26,41 +25,24 @@
]


class Manager(object):
class Manager(CompathManager):
"""Database manager"""
module_name = MODULE_NAME
pathway_model = Pathway
protein_model = Protein
pathway_model_identifier_column = Pathway.reactome_id

has_hierarchy = True # Indicates that this manager can handle hierarchies with the Pathway Model
has_hierarchy = True # Indicates that this manager can handle hierarchies with the Pathway Model

def __init__(self, connection=None):
self.connection = get_connection(MODULE_NAME, connection)
self.engine = create_engine(self.connection)
self.session_maker = sessionmaker(bind=self.engine, autoflush=False, expire_on_commit=False)
self.session = scoped_session(self.session_maker)
self.create_all()
super().__init__(connection=connection)

# Global dictionary
self.pid_protein = {}

def create_all(self, check_first=True):
"""Create tables for Bio2BEL Reactome"""
log.info('create table in {}'.format(self.engine.url))
Base.metadata.create_all(self.engine, checkfirst=check_first)

def drop_all(self):
"""drops all tables for Bio2BEL Reactome"""
log.info('drop tables in {}'.format(self.engine.url))
Base.metadata.drop_all(self.engine)

@staticmethod
def ensure(connection=None):
"""Checks and allows for a Manager to be passed to the function. """
if connection is None or isinstance(connection, str):
return Manager(connection=connection)

if isinstance(connection, Manager):
return connection

raise TypeError
@property
def base(self):
return Base

def count_pathways(self):
"""Counts the pathways in the database
Expand Down Expand Up @@ -126,15 +108,6 @@ def query_gene_set(self, gene_set):

return enrichment_results

def _query_proteins_in_hgnc_list(self, gene_set):
"""Returns the proteins in the database within the gene set query
:param list[str] gene_set: hgnc symbol lists
:rtype: list[bio2bel_reactome.models.Protein]
:return: list of proteins
"""
return self.session.query(Protein).filter(Protein.hgnc_symbol.in_(gene_set)).all()

def export_genesets(self, species=None, top_hierarchy=None):
"""Returns pathway - genesets mapping
Expand Down Expand Up @@ -228,21 +201,6 @@ def get_or_create_species(self, species_name):

return species

def query_pathway_by_name(self, query, limit=None):
"""Returns all pathways having the query in their names
:param query: query string
:param Optional[int] limit: limit result query
:rtype: list[Pathway]
"""

q = self.session.query(Pathway).filter(Pathway.name.contains(query))

if limit:
q = q.limit(limit)

return q.all()

def get_or_create_protein(self, uniprot_id, hgnc_symbol=None, hgnc_id=None):
"""Gets an protein from the database or creates it
Expand Down Expand Up @@ -271,14 +229,6 @@ def get_or_create_protein(self, uniprot_id, hgnc_symbol=None, hgnc_id=None):

return protein

def get_pathway_by_id(self, reactome_id):
"""Gets a pathway by its reactome id
:param str reactome_id: reactome identifier
:rtype: Optional[Pathway]
"""
return self.session.query(Pathway).filter(Pathway.reactome_id == reactome_id).one_or_none()

def get_species_by_name(self, species_name):
"""Gets a Species by its species_name
Expand All @@ -287,13 +237,6 @@ def get_species_by_name(self, species_name):
"""
return self.session.query(Species).filter(Species.name == species_name).one_or_none()

def get_all_pathways(self):
"""Gets all pathways stored in the database
:rtype: list[Pathway]
"""
return self.session.query(Pathway).all()

def get_pathway_names_to_ids(self):
"""Returns a dictionary of pathway names to ids
Expand Down Expand Up @@ -321,7 +264,6 @@ def get_all_hgnc_symbols(self):
def get_pathway_size_distribution(self):
"""Returns pathway sizes
:param bool url: only_human: only human pathways. Defaults to True.
:rtype: dict
:return: pathway sizes
"""
Expand Down Expand Up @@ -600,6 +542,7 @@ def _pathway_chemical(self, chebi_manager, url=None, only_human=True):

self.session.commit()

@bio2bel_populater(MODULE_NAME)
def populate(
self,
hgnc_manager=None,
Expand Down Expand Up @@ -629,3 +572,41 @@ def populate(

self._pathway_protein(hgnc_manager=hgnc_m, url=pathways_proteins_path, only_human=only_human)
self._pathway_chemical(chebi_manager=chebi_m, url=pathways_chemicals_path, only_human=only_human)

def _add_admin(self, app, **kwargs):
from flask_admin import Admin
from flask_admin.contrib.sqla import ModelView

class PathwayView(ModelView):
"""Pathway view in Flask-admin"""
column_searchable_list = (
Pathway.reactome_id,
Pathway.name,
)

class ProteinView(ModelView):
"""Protein view in Flask-admin"""
column_searchable_list = (
Protein.hgnc_symbol,
Protein.uniprot_id,
Protein.hgnc_id
)

class SpeciesView(ModelView):
"""Species view in Flask-admin"""
column_searchable_list = (
Species.name,
)

class ChemicalView(ModelView):
"""Chemical view in Flask-admin"""
column_searchable_list = (
Chemical.chebi_id,
)

admin = Admin(app, **kwargs)
admin.add_view(PathwayView(Pathway, self.session))
admin.add_view(ProteinView(Protein, self.session))
admin.add_view(ChemicalView(Chemical, self.session))
admin.add_view(SpeciesView(Species, self.session))
return admin
Loading

0 comments on commit 7cfa348

Please sign in to comment.