From 06cce3b7b33eb45b2500e0511cb7c3dd2606be7d Mon Sep 17 00:00:00 2001 From: greg Date: Wed, 25 Sep 2024 13:30:14 -0400 Subject: [PATCH] Galaxy 24.1 upgrade --- .../webapps/coralsnp_reports/api/__init__.py | 0 .../galaxy/webapps/coralsnp_reports/app.py | 34 +- .../webapps/coralsnp_reports/buildapp.py | 88 +-- .../galaxy/webapps/coralsnp_reports/config.py | 6 +- .../coralsnp_reports/controllers/__init__.py | 31 - .../coralsnp_reports/controllers/alleles.py | 27 +- .../controllers/collectors.py | 49 +- .../coralsnp_reports/controllers/colonies.py | 73 ++- .../controllers/experiments.py | 47 +- .../coralsnp_reports/controllers/genotypes.py | 112 ++-- .../controllers/phenotypes.py | 57 +- .../coralsnp_reports/controllers/query.py | 17 + .../coralsnp_reports/controllers/reefs.py | 81 +-- .../coralsnp_reports/controllers/root.py | 9 +- .../coralsnp_reports/controllers/samples.py | 563 +++++++++--------- .../controllers/taxonomies.py | 39 +- .../webapps/coralsnp_reports/fast_app.py | 25 + .../webapps/coralsnp_reports/fast_factory.py | 56 ++ 18 files changed, 680 insertions(+), 634 deletions(-) create mode 100644 galaxy/coralsnp_reports/lib/galaxy/webapps/coralsnp_reports/api/__init__.py create mode 100644 galaxy/coralsnp_reports/lib/galaxy/webapps/coralsnp_reports/controllers/query.py create mode 100644 galaxy/coralsnp_reports/lib/galaxy/webapps/coralsnp_reports/fast_app.py create mode 100644 galaxy/coralsnp_reports/lib/galaxy/webapps/coralsnp_reports/fast_factory.py diff --git a/galaxy/coralsnp_reports/lib/galaxy/webapps/coralsnp_reports/api/__init__.py b/galaxy/coralsnp_reports/lib/galaxy/webapps/coralsnp_reports/api/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/galaxy/coralsnp_reports/lib/galaxy/webapps/coralsnp_reports/app.py b/galaxy/coralsnp_reports/lib/galaxy/webapps/coralsnp_reports/app.py index cc2a0ed2..9f2765e4 100644 --- a/galaxy/coralsnp_reports/lib/galaxy/webapps/coralsnp_reports/app.py +++ b/galaxy/coralsnp_reports/lib/galaxy/webapps/coralsnp_reports/app.py @@ -2,40 +2,46 @@ import sys import time -import galaxy.model.corals import galaxy.model.corals.mapping +from galaxy.config import configure_logging +from galaxy.model.base import SharedModelMapping from galaxy.security import idencoding -from galaxy.web.stack import application_stack_instance +from galaxy.structured_app import BasicSharedApp +from galaxy.web_stack import application_stack_instance from . import config log = logging.getLogger(__name__) -class UniverseApplication(object): +class UniverseApplication(BasicSharedApp): """Encapsulates the state of a Universe application""" def __init__(self, **kwargs): - log.debug("python path is: %s", ", ".join(sys.path)) + super().__init__() + self[BasicSharedApp] = self self.name = "coralsnp_reports" # Read config file and check for errors self.config = config.Configuration(**kwargs) self.config.check() - config.configure_logging(self.config) + configure_logging(self.config) + log.debug("XXX kwargs: %s\n" % str(kwargs)) + log.debug("XXX self.config: %s\n" % str(self.config)) + log.debug("XXX self.config.corals_database_connection: %s\n" % self.config.corals_database_connection) + log.debug("python path is: %s", ", ".join(sys.path)) self.application_stack = application_stack_instance() # Determine the database url - if self.config.database_connection: - db_url = self.config.database_connection + if self.config.corals_database_connection: + db_url = self.config.corals_database_connection else: - db_url = "postgresql://galaxy:galaxy@localhost/stag" + db_url = f"sqlite:///{self.config.database}?isolation_level=IMMEDIATE" # Setup the database engine and ORM - self.model = galaxy.model.corals.mapping.init(db_url, - self.config.database_engine_options) - if not self.config.database_connection: - self.targets_mysql = False - else: - self.targets_mysql = 'mysql' in self.config.database_connection + self.model = galaxy.model.corals.mapping.init(self.config.file_path, db_url, self.config.database_engine_options) # Security helper self.security = idencoding.IdEncodingHelper(id_secret=self.config.id_secret) + + self._register_singleton(idencoding.IdEncodingHelper, self.security) + self._register_singleton(SharedModelMapping, self.model) + # used for cachebusting -- refactor this into a *SINGLE* UniverseApplication base. self.server_starttime = int(time.time()) diff --git a/galaxy/coralsnp_reports/lib/galaxy/webapps/coralsnp_reports/buildapp.py b/galaxy/coralsnp_reports/lib/galaxy/webapps/coralsnp_reports/buildapp.py index e110858d..3566d7b9 100644 --- a/galaxy/coralsnp_reports/lib/galaxy/webapps/coralsnp_reports/buildapp.py +++ b/galaxy/coralsnp_reports/lib/galaxy/webapps/coralsnp_reports/buildapp.py @@ -8,21 +8,16 @@ from paste import httpexceptions -import galaxy.model.corals -import galaxy.web.framework.webapp +import galaxy.webapps.base.webapp from galaxy.util import asbool from galaxy.util.properties import load_app_properties -from galaxy.webapps.util import ( - build_template_error_formatters, - MiddlewareWrapUnsupported, - wrap_if_allowed, - wrap_if_allowed_or_fail -) +from galaxy.web.framework.middleware.request_id import RequestIDMiddleware +from galaxy.webapps.util import wrap_if_allowed log = logging.getLogger(__name__) -class CoralSNPReportsWebApplication(galaxy.web.framework.webapp.WebApplication): +class CoralSNPReportsWebApplication(galaxy.webapps.base.webapp.WebApplication): pass @@ -31,9 +26,9 @@ def add_ui_controllers(webapp, app): Search for controllers in the 'galaxy.webapps.coralsnp_reports.controllers' module and add them to the webapp. """ - #from galaxy.web.base.controller import BaseUIController import galaxy.webapps.coralsnp_reports.controllers - from galaxy.webapps.coralsnp_reports.controllers import BaseUIController + from galaxy.webapps.base.controller import BaseUIController + controller_dir = galaxy.webapps.coralsnp_reports.controllers.__path__[0] for fname in os.listdir(controller_dir): if not fname.startswith("_") and fname.endswith(".py"): @@ -52,10 +47,8 @@ def add_ui_controllers(webapp, app): def app_factory(global_conf, load_app_kwds={}, **kwargs): """Return a wsgi application serving the root object""" # Create the Galaxy application unless passed in - kwargs = load_app_properties( - kwds=kwargs, - **load_app_kwds - ) + load_app_kwds = load_app_kwds or {} + kwargs = load_app_properties(kwds=kwargs, **load_app_kwds) if 'app' in kwargs: app = kwargs.pop('app') else: @@ -73,15 +66,7 @@ def app_factory(global_conf, load_app_kwds={}, **kwargs): if kwargs.get('middleware', True): webapp = wrap_in_middleware(webapp, global_conf, app.application_stack, **kwargs) if asbool(kwargs.get('static_enabled', True)): - webapp = wrap_if_allowed(webapp, app.application_stack, wrap_in_static, - args=(global_conf,), - kwargs=kwargs) - # Close any pooled database connections before forking - try: - galaxy.model.corals.mapping.metadata.bind.dispose() - except Exception: - log.exception("Unable to dispose of pooled coralsnp_reports model database connections.") - # Return + webapp = wrap_if_allowed(webapp, app.application_stack, galaxy.webapps.base.webapp.build_url_map, args=(global_conf,), kwargs=kwargs) return webapp @@ -91,7 +76,6 @@ def wrap_in_middleware(app, global_conf, application_stack, **local_conf): # Merge the global and local configurations conf = global_conf.copy() conf.update(local_conf) - debug = asbool(conf.get('debug', False)) # First put into place httpexceptions, which must be most closely # wrapped around the application (it can interact poorly with # other middleware): @@ -101,54 +85,20 @@ def wrap_in_middleware(app, global_conf, application_stack, **local_conf): if asbool(conf.get('use_recursive', True)): from paste import recursive app = wrap_if_allowed(app, stack, recursive.RecursiveMiddleware, args=(conf,)) - # Various debug middleware that can only be turned on if the debug - # flag is set, either because they are insecure or greatly hurt - # performance - if debug: - # Middleware to check for WSGI compliance - if asbool(conf.get('use_lint', True)): - from paste import lint - app = wrap_if_allowed(app, stack, lint.make_middleware, name='paste.lint', args=(conf,)) - # Middleware to run the python profiler on each request - if asbool(conf.get('use_profile', False)): - import profile - app = wrap_if_allowed(app, stack, profile.ProfileMiddleware, args=(conf,)) - # Middleware that intercepts print statements and shows them on the - # returned page - if asbool(conf.get('use_printdebug', True)): - from paste.debug import prints - app = wrap_if_allowed(app, stack, prints.PrintDebugMiddleware, args=(conf,)) - if debug and asbool(conf.get('use_interactive', False)): - # Interactive exception debugging, scary dangerous if publicly - # accessible, if not enabled we'll use the regular error printing - # middleware. - try: - from weberror import evalexception - app = wrap_if_allowed_or_fail(app, stack, evalexception.EvalException, - args=(conf,), - kwargs=dict(templating_formatters=build_template_error_formatters())) - except MiddlewareWrapUnsupported as exc: - log.warning(str(exc)) - import galaxy.web.framework.middleware.error - app = wrap_if_allowed(app, stack, galaxy.web.framework.middleware.error.ErrorMiddleware, args=(conf,)) - else: - # Not in interactive debug mode, just use the regular error middleware - import galaxy.web.framework.middleware.error - app = wrap_if_allowed(app, stack, galaxy.web.framework.middleware.error.ErrorMiddleware, args=(conf,)) + # Error middleware + import galaxy.web.framework.middleware.error + app = wrap_if_allowed(app, stack, galaxy.web.framework.middleware.error.ErrorMiddleware, args=(conf,)) # Transaction logging (apache access.log style) - if asbool(conf.get('use_translogger', True)): + if asbool(conf.get("use_translogger", True)): from paste.translogger import TransLogger app = wrap_if_allowed(app, stack, TransLogger) # X-Forwarded-Host handling from galaxy.web.framework.middleware.xforwardedhost import XForwardedHostMiddleware app = wrap_if_allowed(app, stack, XForwardedHostMiddleware) - return app - + # Request ID middleware + app = wrap_if_allowed(app, stack, RequestIDMiddleware) + if asbool(conf.get("enable_per_request_sql_debugging", False)): + from galaxy.web.framework.middleware.sqldebug import SQLDebugMiddleware -def wrap_in_static(app, global_conf, **local_conf): - urlmap, _ = galaxy.web.framework.webapp.build_url_map(app, global_conf, local_conf) - return urlmap - - -def uwsgi_app(): - return galaxy.web.framework.webapp.build_native_uwsgi_app(app_factory, "coralsnp_reports") + app = wrap_if_allowed(app, stack, SQLDebugMiddleware, args=(webapp, {})) + return app diff --git a/galaxy/coralsnp_reports/lib/galaxy/webapps/coralsnp_reports/config.py b/galaxy/coralsnp_reports/lib/galaxy/webapps/coralsnp_reports/config.py index cfd5dd60..f4f3f764 100644 --- a/galaxy/coralsnp_reports/lib/galaxy/webapps/coralsnp_reports/config.py +++ b/galaxy/coralsnp_reports/lib/galaxy/webapps/coralsnp_reports/config.py @@ -11,7 +11,7 @@ def resolve_path(path, root): """If 'path' is relative make absolute by prepending 'root'""" - if not(os.path.isabs(path)): + if not (os.path.isabs(path)): path = os.path.join(root, path) return path @@ -26,7 +26,7 @@ def __init__(self, **kwargs): self.root = kwargs.get('root_dir', '.') # Database related configuration self.database = resolve_path(kwargs.get("database_file", "database/universe.sqlite"), self.root) - self.database_connection = kwargs.get("database_connection", False) + self.corals_database_connection = kwargs.get("corals_database_connection", False) self.database_engine_options = get_database_engine_options(kwargs) # Where dataset files are stored self.file_path = resolve_path(kwargs.get("file_path", "database/files"), self.root) @@ -36,7 +36,7 @@ def __init__(self, **kwargs): self.require_login = string_as_bool(kwargs.get("require_login", "False")) self.session_duration = kwargs.get("session_duration", "0") self.template_path = resolve_path(kwargs.get("template_path", "templates"), self.root) - self.template_cache = resolve_path(kwargs.get("template_cache_path", "database/compiled_templates/coralsnp_reports"), self.root) + self.template_cache_path = resolve_path(kwargs.get("template_cache_path", "database/compiled_templates/coralsnp_reports"), self.root) self.allow_user_creation = string_as_bool(kwargs.get("allow_user_creation", "True")) self.allow_user_deletion = string_as_bool(kwargs.get("allow_user_deletion", "False")) self.log_actions = string_as_bool(kwargs.get('log_actions', 'False')) diff --git a/galaxy/coralsnp_reports/lib/galaxy/webapps/coralsnp_reports/controllers/__init__.py b/galaxy/coralsnp_reports/lib/galaxy/webapps/coralsnp_reports/controllers/__init__.py index 139ccf37..02a1465f 100644 --- a/galaxy/coralsnp_reports/lib/galaxy/webapps/coralsnp_reports/controllers/__init__.py +++ b/galaxy/coralsnp_reports/lib/galaxy/webapps/coralsnp_reports/controllers/__init__.py @@ -1,32 +1 @@ """Galaxy coralsnp reports controllers.""" -import logging - -from galaxy import util - -log = logging.getLogger(__name__) - - -class BaseController(object): - """ - Base class for CoralSNP Reports web application controllers. - """ - - def __init__(self, app): - """Initialize an interface for application 'app'""" - self.app = app - self.sa_session = app.model.context - - -class BaseUIController(BaseController): - - def get_object(self, trans, id, class_name, check_ownership=False, check_accessible=False, deleted=None): - try: - return BaseController.get_object(self, trans, id, class_name, check_ownership=check_ownership, - check_accessible=check_accessible, deleted=deleted) - except Exception: - log.exception("Exception in get_object check for %s %s:", class_name, str(id)) - raise Exception('Server error retrieving %s id ( %s ).' % (class_name, str(id))) - - def message_exception(self, trans, message, sanitize=True): - trans.response.status = 400 - return {'err_msg': util.sanitize_text(message) if sanitize else message} diff --git a/galaxy/coralsnp_reports/lib/galaxy/webapps/coralsnp_reports/controllers/alleles.py b/galaxy/coralsnp_reports/lib/galaxy/webapps/coralsnp_reports/controllers/alleles.py index ed7857ea..22c8ff3c 100644 --- a/galaxy/coralsnp_reports/lib/galaxy/webapps/coralsnp_reports/controllers/alleles.py +++ b/galaxy/coralsnp_reports/lib/galaxy/webapps/coralsnp_reports/controllers/alleles.py @@ -3,11 +3,13 @@ import sqlalchemy as sa from markupsafe import escape -import galaxy.model +from galaxy.model import corals from galaxy import util -from . import BaseUIController -from galaxy.web.base.controller import web -from galaxy.webapps.reports.controllers.query import ReportQueryBuilder +from galaxy.webapps.base.controller import ( + BaseUIController, + web, +) +from galaxy.webapps.coralsnp_reports.controllers.query import ReportQueryBuilder log = logging.getLogger(__name__) @@ -19,14 +21,17 @@ def of_sample(self, trans, **kwd): message = escape(util.restore_text(kwd.get('message', ''))) affy_id = kwd.get('affy_id') allele_id = kwd.get('allele_id') - q = sa.select((galaxy.model.corals.Allele.table.c.id, - galaxy.model.corals.Allele.table.c.allele), - whereclause=galaxy.model.corals.Allele.table.c.id == allele_id, - from_obj=[galaxy.model.corals.Allele.table]) + q = ( + sa.select( + corals.Allele.id, + corals.Allele.allele + ) + .select_from(corals.Allele.table) + .where(corals.Allele.table.c.id == allele_id) + ) alleles = [] - for row in q.execute(): - cols_tup = (row.id, row.allele) - alleles.append(cols_tup) + for row in trans.sa_session.execute(q): + alleles.append((row.id, row.allele)) return trans.fill_template('/webapps/coralsnp_reports/allele_of_sample.mako', affy_id=affy_id, alleles=alleles, diff --git a/galaxy/coralsnp_reports/lib/galaxy/webapps/coralsnp_reports/controllers/collectors.py b/galaxy/coralsnp_reports/lib/galaxy/webapps/coralsnp_reports/controllers/collectors.py index 6ee168fa..de900a82 100644 --- a/galaxy/coralsnp_reports/lib/galaxy/webapps/coralsnp_reports/controllers/collectors.py +++ b/galaxy/coralsnp_reports/lib/galaxy/webapps/coralsnp_reports/controllers/collectors.py @@ -3,11 +3,13 @@ import sqlalchemy as sa from markupsafe import escape -import galaxy.model +from galaxy.model import corals from galaxy import util -from . import BaseUIController -from galaxy.web.base.controller import web -from galaxy.webapps.reports.controllers.query import ReportQueryBuilder +from galaxy.webapps.base.controller import ( + BaseUIController, + web, +) +from galaxy.webapps.coralsnp_reports.controllers.query import ReportQueryBuilder log = logging.getLogger(__name__) @@ -17,35 +19,32 @@ class Collectors(BaseUIController, ReportQueryBuilder): @web.expose def all(self, trans, **kwd): message = escape(util.restore_text(kwd.get('message', ''))) - q = sa.select((galaxy.model.corals.Person.table.c.id, - galaxy.model.corals.Person.table.c.last_name, - galaxy.model.corals.Person.table.c.first_name, - galaxy.model.corals.Person.table.c.organization, - galaxy.model.corals.Person.table.c.email), - from_obj=[galaxy.model.corals.Person.table], - order_by=[galaxy.model.corals.Person.table.c.last_name]) collectors = [] - for row in q.execute(): - cols_tup = (row.id, row.last_name, row.first_name, row.organization, row.email) - collectors.append(cols_tup) - return trans.fill_template('/webapps/coralsnp_reports/collectors.mako', collectors=collectors, message=message) + for row in trans.sa_session.query(corals.Person): + collectors.append((row.id, row.last_name, row.first_name, row.organization, row.email)) + return trans.fill_template('/webapps/coralsnp_reports/collectors.mako', + collectors=collectors, + message=message) @web.expose def of_sample(self, trans, **kwd): message = escape(util.restore_text(kwd.get('message', ''))) affy_id = kwd.get('affy_id') collector_id = kwd.get('collector_id') - q = sa.select((galaxy.model.corals.Person.table.c.id, - galaxy.model.corals.Person.table.c.last_name, - galaxy.model.corals.Person.table.c.first_name, - galaxy.model.corals.Person.table.c.organization, - galaxy.model.corals.Person.table.c.email), - whereclause=galaxy.model.corals.Person.table.c.id == collector_id, - from_obj=[galaxy.model.corals.Person.table]) + q = ( + sa.select( + corals.Person.id, + corals.Person.last_name, + corals.Person.first_name, + corals.Person.organization, + corals.Person.email + ) + .select_from(corals.Person.table) + .where(corals.Person.table.c.id == collector_id) + ) collectors = [] - for row in q.execute(): - cols_tup = (row.last_name, row.first_name, row.organization, row.email) - collectors.append(cols_tup) + for row in trans.sa_session.execute(q): + collectors.append((row.last_name, row.first_name, row.organization, row.email)) return trans.fill_template('/webapps/coralsnp_reports/collector_of_sample.mako', affy_id=affy_id, collectors=collectors, diff --git a/galaxy/coralsnp_reports/lib/galaxy/webapps/coralsnp_reports/controllers/colonies.py b/galaxy/coralsnp_reports/lib/galaxy/webapps/coralsnp_reports/controllers/colonies.py index 49525ffd..5232b0af 100644 --- a/galaxy/coralsnp_reports/lib/galaxy/webapps/coralsnp_reports/controllers/colonies.py +++ b/galaxy/coralsnp_reports/lib/galaxy/webapps/coralsnp_reports/controllers/colonies.py @@ -3,11 +3,13 @@ import sqlalchemy as sa from markupsafe import escape -import galaxy.model +from galaxy.model import corals from galaxy import util -from . import BaseUIController -from galaxy.web.base.controller import web -from galaxy.webapps.reports.controllers.query import ReportQueryBuilder +from galaxy.webapps.base.controller import ( + BaseUIController, + web, +) +from galaxy.webapps.coralsnp_reports.controllers.query import ReportQueryBuilder log = logging.getLogger(__name__) @@ -17,19 +19,22 @@ class Colonies(BaseUIController, ReportQueryBuilder): @web.expose def all(self, trans, **kwd): message = escape(util.restore_text(kwd.get('message', ''))) - q = sa.select((galaxy.model.corals.Colony.table.c.id, - galaxy.model.corals.Colony.table.c.latitude, - galaxy.model.corals.Colony.table.c.longitude, - galaxy.model.corals.Colony.table.c.depth, - galaxy.model.corals.Colony.table.c.reef_id, - galaxy.model.corals.Sample.table.c.public, - galaxy.model.corals.Sample.table.c.public_after_date), - from_obj=[galaxy.model.corals.Colony.table, - galaxy.model.corals.Sample.table], - whereclause=galaxy.model.corals.Colony.table.c.id == galaxy.model.corals.Sample.table.c.colony_id, - order_by=[galaxy.model.corals.Colony.table.c.id]) + q = ( + sa.select( + corals.Colony.id, + corals.Colony.latitude, + corals.Colony.longitude, + corals.Colony.depth, + corals.Colony.reef_id, + corals.Sample.public, + corals.Sample.public_after_date + ) + .select_from(corals.Colony) + .join(corals.Sample, corals.Colony.id == corals.Sample.colony_id) + .order_by(corals.Colony.id) + ) colonies = [] - for row in q.execute(): + for row in trans.sa_session.execute(q): public_after_date = str(row.public_after_date)[:10] if str(row.public) == "True": latitude = row.latitude @@ -37,28 +42,31 @@ def all(self, trans, **kwd): else: latitude = "Private until %s" % public_after_date longitude = "Private until %s" % public_after_date - cols_tup = (row.id, latitude, longitude, row.depth, row.reef_id) - colonies.append(cols_tup) - return trans.fill_template('/webapps/coralsnp_reports/colonies.mako', colonies=colonies, message=message) + colonies.append((row.id, latitude, longitude, row.depth, row.reef_id)) + return trans.fill_template('/webapps/coralsnp_reports/colonies.mako', + colonies=colonies, + message=message) @web.expose def of_sample(self, trans, **kwd): message = escape(util.restore_text(kwd.get('message', ''))) affy_id = kwd.get('affy_id') colony_id = kwd.get('colony_id') - q = sa.select((galaxy.model.corals.Colony.table.c.latitude, - galaxy.model.corals.Colony.table.c.longitude, - galaxy.model.corals.Colony.table.c.depth, - galaxy.model.corals.Colony.table.c.reef_id, - galaxy.model.corals.Sample.table.c.public, - galaxy.model.corals.Sample.table.c.public_after_date), - from_obj=[galaxy.model.corals.Colony.table, - galaxy.model.corals.Sample.table], - whereclause=sa.and_(galaxy.model.corals.Colony.table.c.id == colony_id, - galaxy.model.corals.Colony.table.c.id == galaxy.model.corals.Sample.table.c.colony_id), - order_by=[galaxy.model.corals.Colony.table.c.id]) + q = ( + sa.select( + corals.Colony.latitude, + corals.Colony.longitude, + corals.Colony.depth, + corals.Colony.reef_id, + corals.Sample.public, + corals.Sample.public_after_date + ) + .select_from(corals.Colony) + .join(corals.Sample, sa.and_(corals.Colony.id == colony_id, corals.Colony.id == corals.Sample.colony_id)) + .order_by(corals.Colony.id) + ) colonies = [] - for row in q.execute(): + for row in trans.sa_session.execute(q): public_after_date = str(row.public_after_date)[:10] if str(row.public) == "True": latitude = row.latitude @@ -66,8 +74,7 @@ def of_sample(self, trans, **kwd): else: latitude = "Private until %s" % public_after_date longitude = "Private until %s" % public_after_date - cols_tup = (latitude, longitude, row.depth, row.reef_id) - colonies.append(cols_tup) + colonies.append((latitude, longitude, row.depth, row.reef_id)) return trans.fill_template('/webapps/coralsnp_reports/colony_of_sample.mako', affy_id=affy_id, colonies=colonies, diff --git a/galaxy/coralsnp_reports/lib/galaxy/webapps/coralsnp_reports/controllers/experiments.py b/galaxy/coralsnp_reports/lib/galaxy/webapps/coralsnp_reports/controllers/experiments.py index 7d879923..b891800f 100644 --- a/galaxy/coralsnp_reports/lib/galaxy/webapps/coralsnp_reports/controllers/experiments.py +++ b/galaxy/coralsnp_reports/lib/galaxy/webapps/coralsnp_reports/controllers/experiments.py @@ -3,11 +3,13 @@ import sqlalchemy as sa from markupsafe import escape -import galaxy.model +from galaxy.model import corals from galaxy import util -from . import BaseUIController -from galaxy.web.base.controller import web -from galaxy.webapps.reports.controllers.query import ReportQueryBuilder +from galaxy.webapps.base.controller import ( + BaseUIController, + web, +) +from galaxy.webapps.coralsnp_reports.controllers.query import ReportQueryBuilder log = logging.getLogger(__name__) @@ -17,18 +19,9 @@ class Experiments(BaseUIController, ReportQueryBuilder): @web.expose def all(self, trans, **kwd): message = escape(util.restore_text(kwd.get('message', ''))) - q = sa.select((galaxy.model.corals.Experiment.table.c.id, - galaxy.model.corals.Experiment.table.c.seq_facility, - galaxy.model.corals.Experiment.table.c.array_version, - galaxy.model.corals.Experiment.table.c.result_folder_name, - galaxy.model.corals.Experiment.table.c.plate_barcode), - from_obj=[galaxy.model.corals.Experiment.table], - order_by=[galaxy.model.corals.Experiment.table.c.id]) experiments = [] - for row in q.execute(): - cols_tup = (row.id, row.seq_facility, row.array_version, - row.result_folder_name, row.plate_barcode) - experiments.append(cols_tup) + for row in trans.sa_session.query(corals.Experiment): + experiments.append((row.id, row.seq_facility, row.array_version, row.result_folder_name, row.plate_barcode)) return trans.fill_template('/webapps/coralsnp_reports/experiments.mako', experiments=experiments, message=message) @web.expose @@ -36,18 +29,20 @@ def of_sample(self, trans, **kwd): message = escape(util.restore_text(kwd.get('message', ''))) affy_id = kwd.get('affy_id') experiment_id = kwd.get('experiment_id') - q = sa.select((galaxy.model.corals.Experiment.table.c.seq_facility, - galaxy.model.corals.Experiment.table.c.array_version, - galaxy.model.corals.Experiment.table.c.result_folder_name, - galaxy.model.corals.Experiment.table.c.plate_barcode), - whereclause=sa.and_(galaxy.model.corals.Experiment.table.c.id == experiment_id), - from_obj=[galaxy.model.corals.Experiment.table], - order_by=[galaxy.model.corals.Experiment.table.c.id]) + q = ( + sa.select( + corals.Experiment.seq_facility, + corals.Experiment.array_version, + corals.Experiment.result_folder_name, + corals.Experiment.plate_barcode + ) + .select_from(corals.Experiment.table) + .where(corals.Experiment.table.c.id == experiment_id) + .order_by(corals.Experiment.table.c.id) + ) experiments = [] - for row in q.execute(): - cols_tup = (row.seq_facility, row.array_version, - row.result_folder_name, row.plate_barcode) - experiments.append(cols_tup) + for row in trans.sa_session.execute(q): + experiments.append((row.seq_facility, row.array_version, row.result_folder_name, row.plate_barcode)) return trans.fill_template('/webapps/coralsnp_reports/experiment_of_sample.mako', affy_id=affy_id, experiments=experiments, diff --git a/galaxy/coralsnp_reports/lib/galaxy/webapps/coralsnp_reports/controllers/genotypes.py b/galaxy/coralsnp_reports/lib/galaxy/webapps/coralsnp_reports/controllers/genotypes.py index 6872fed6..a26fdaed 100644 --- a/galaxy/coralsnp_reports/lib/galaxy/webapps/coralsnp_reports/controllers/genotypes.py +++ b/galaxy/coralsnp_reports/lib/galaxy/webapps/coralsnp_reports/controllers/genotypes.py @@ -9,12 +9,14 @@ import sqlalchemy as sa from markupsafe import escape -import galaxy.model +from galaxy.model import corals +from galaxy.webapps.base.controller import ( + BaseUIController, + web, +) from galaxy import util -from . import BaseUIController -from galaxy.web.base.controller import web from galaxy.webapps.reports.controllers.jobs import sorter -from galaxy.webapps.reports.controllers.query import ReportQueryBuilder +from galaxy.webapps.coralsnp_reports.controllers.query import ReportQueryBuilder log = logging.getLogger(__name__) @@ -24,22 +26,15 @@ class Genotypes(BaseUIController, ReportQueryBuilder): @web.expose def all(self, trans, **kwd): message = escape(util.restore_text(kwd.get('message', ''))) - q = sa.select((galaxy.model.corals.Genotype.table.c.id, - galaxy.model.corals.Genotype.table.c.coral_mlg_clonal_id, - galaxy.model.corals.Genotype.table.c.coral_mlg_rep_sample_id, - galaxy.model.corals.Genotype.table.c.genetic_coral_species_call), - from_obj=[galaxy.model.corals.Genotype.table], - order_by=[galaxy.model.corals.Genotype.table.c.id]) genotypes = [] - for row in q.execute(): - cols_tup = (row.id, row.coral_mlg_clonal_id, row.coral_mlg_rep_sample_id, row.genetic_coral_species_call) - genotypes.append(cols_tup) + for row in trans.sa_session.query(corals.Genotype): + genotypes.append((row.id, row.coral_mlg_clonal_id, row.coral_mlg_rep_sample_id, row.genetic_coral_species_call)) return trans.fill_template('/webapps/coralsnp_reports/genotypes.mako', genotypes=genotypes, message=message) @web.expose def all_by_sample_upload_date(self, trans, **kwd): message = escape(util.restore_text(kwd.get('message', ''))) - num_genotypes = trans.sa_session.query(galaxy.model.corals.Genotype).count() + num_genotypes = trans.sa_session.query(corals.Genotype).count() return trans.fill_template('/webapps/coralsnp_reports/genotypes_by_sample_upload_date.mako', num_genotypes=num_genotypes, message=message) @web.expose @@ -48,16 +43,20 @@ def of_sample(self, trans, **kwd): # If specified_date is not received, we'll default to the current month affy_id = kwd.get('affy_id') genotype_id = kwd.get('genotype_id') - q = sa.select((galaxy.model.corals.Genotype.table.c.coral_mlg_clonal_id, - galaxy.model.corals.Genotype.table.c.coral_mlg_rep_sample_id, - galaxy.model.corals.Genotype.table.c.genetic_coral_species_call), - whereclause=sa.and_(galaxy.model.corals.Genotype.table.c.id == genotype_id), - from_obj=[galaxy.model.corals.Genotype.table], - order_by=[galaxy.model.corals.Genotype.table.c.id]) + q = ( + sa.select( + corals.Genotype.id, + corals.Genotype.coral_mlg_clonal_id, + corals.Genotype.coral_mlg_rep_sample_id, + corals.Genotype.genetic_coral_species_call + ) + .select_from(corals.Genotype.table) + .where(corals.Genotype.table.c.id == genotype_id) + .order_by(corals.Genotype.table.c.id) + ) genotypes = [] - for row in q.execute(): - cols_tup = (row.coral_mlg_clonal_id, row.coral_mlg_rep_sample_id, row.genetic_coral_species_call) - genotypes.append(cols_tup) + for row in trans.sa_session.execute(q): + genotypes.append((row.coral_mlg_clonal_id, row.coral_mlg_rep_sample_id, row.genetic_coral_species_call)) return trans.fill_template('/webapps/coralsnp_reports/genotype_of_sample.mako', affy_id=affy_id, genotypes=genotypes, @@ -70,15 +69,18 @@ def per_month(self, trans, **kwd): sort_id = specs.sort_id order = specs.order arrow = specs.arrow - _order = specs.exc_order - q = sa.select((self.select_month(galaxy.model.corals.Genotype.table.c.create_time).label('date'), - sa.func.count(galaxy.model.corals.Genotype.table.c.id).label('num_genotypes')), - from_obj=[galaxy.model.corals.Genotype.table], - group_by=self.group_by_month(galaxy.model.corals.Genotype.table.c.create_time), - order_by=[_order]) + q = ( + sa.select( + self.select_month(corals.Genotype.table.c.create_time).label('date'), + sa.func.count(corals.Genotype.table.c.id).label('num_genotypes') + ) + .select_from(corals.Genotype.table) + .group_by(self.group_by_month(corals.Genotype.table.c.create_time)) + .order_by(specs.exc_order) + ) genotypes = [] - for row in q.execute(): + for row in trans.sa_session.execute(q): genotypes.append((row.date.strftime("%Y-%m"), row.num_genotypes, row.date.strftime("%B"), @@ -93,7 +95,8 @@ def per_month(self, trans, **kwd): @web.expose def specified_month(self, trans, **kwd): message = escape(util.restore_text(kwd.get('message', ''))) - # If specified_date is not received, we'll default to the current month + # If specified_date is not received, + # we'll default to the current month. specified_date = kwd.get('specified_date', datetime.utcnow().strftime("%Y-%m-%d")) specified_month = specified_date[:7] year, month = map(int, specified_month.split("-")) @@ -101,15 +104,19 @@ def specified_month(self, trans, **kwd): end_date = start_date + timedelta(days=calendar.monthrange(year, month)[1]) month_label = start_date.strftime("%B") year_label = start_date.strftime("%Y") - q = sa.select((self.select_day(galaxy.model.corals.Genotype.table.c.create_time).label('date'), - sa.func.count(galaxy.model.corals.Genotype.table.c.id).label('num_genotypes')), - whereclause=sa.and_(galaxy.model.corals.Genotype.table.c.create_time >= start_date, - galaxy.model.corals.Genotype.table.c.create_time < end_date), - from_obj=[galaxy.model.corals.Genotype.table], - group_by=self.group_by_day(galaxy.model.corals.Genotype.table.c.create_time), - order_by=[sa.desc('date')]) + q = ( + sa.select( + self.select_day(corals.Genotype.table.c.create_time).label('date'), + sa.func.count(corals.Genotype.table.c.id).label('num_genotypes') + ) + .select_from(corals.Genotype.table) + .where(sa.and_(corals.Genotype.table.c.create_time >= start_date, + corals.Genotype.table.c.create_time < end_date)) + .group_by(self.group_by_day(corals.Genotype.table.c.create_time)) + .order_by(sa.desc('date')) + ) genotypes = [] - for row in q.execute(): + for row in trans.sa_session.execute(q): genotypes.append((row.date.strftime("%Y-%m-%d"), row.date.strftime("%d"), row.num_genotypes, @@ -133,19 +140,22 @@ def specified_date(self, trans, **kwd): day_label = start_date.strftime("%A") month_label = start_date.strftime("%B") year_label = start_date.strftime("%Y") - q = sa.select((self.select_day(galaxy.model.corals.Genotype.table.c.create_time).label('date'), - galaxy.model.corals.Genotype.table.c.id, - galaxy.model.corals.Genotype.table.c.coral_mlg_clonal_id, - galaxy.model.corals.Genotype.table.c.coral_mlg_rep_sample_id, - galaxy.model.corals.Genotype.table.c.genetic_coral_species_call), - whereclause=sa.and_(galaxy.model.corals.Genotype.table.c.create_time >= start_date, - galaxy.model.corals.Genotype.table.c.create_time < end_date), - from_obj=[galaxy.model.corals.Genotype.table], - order_by=[galaxy.model.corals.Genotype.table.c.id]) + q = ( + sa.select( + self.select_day(corals.Genotype.table.c.create_time).label('date'), + corals.Genotype.id, + corals.Genotype.coral_mlg_clonal_id, + corals.Genotype.coral_mlg_rep_sample_id, + corals.Genotype.genetic_coral_species_call + ) + .where(sa.and_(corals.Genotype.table.c.create_time >= start_date, + corals.Genotype.table.c.create_time < end_date)) + .select_from(corals.Genotype.table) + .order_by(corals.Genotype.table.c.id) + ) genotypes = [] - for row in q.execute(): - cols_tup = (row.id, row.coral_mlg_clonal_id, row.coral_mlg_rep_sample_id, row.genetic_coral_species_call) - genotypes.append(cols_tup) + for row in trans.sa_session.execute(q): + genotypes.append((row.id, row.coral_mlg_clonal_id, row.coral_mlg_rep_sample_id, row.genetic_coral_species_call)) return trans.fill_template('/webapps/coralsnp_reports/genotypes_specified_date.mako', specified_date=start_date, day_label=day_label, diff --git a/galaxy/coralsnp_reports/lib/galaxy/webapps/coralsnp_reports/controllers/phenotypes.py b/galaxy/coralsnp_reports/lib/galaxy/webapps/coralsnp_reports/controllers/phenotypes.py index 43ee0860..8229b0ba 100644 --- a/galaxy/coralsnp_reports/lib/galaxy/webapps/coralsnp_reports/controllers/phenotypes.py +++ b/galaxy/coralsnp_reports/lib/galaxy/webapps/coralsnp_reports/controllers/phenotypes.py @@ -3,11 +3,13 @@ import sqlalchemy as sa from markupsafe import escape -import galaxy.model +from galaxy.model import corals from galaxy import util -from . import BaseUIController -from galaxy.web.base.controller import web -from galaxy.webapps.reports.controllers.query import ReportQueryBuilder +from galaxy.webapps.base.controller import ( + BaseUIController, + web, +) +from galaxy.webapps.coralsnp_reports.controllers.query import ReportQueryBuilder log = logging.getLogger(__name__) @@ -17,22 +19,9 @@ class Phenotypes(BaseUIController, ReportQueryBuilder): @web.expose def all(self, trans, **kwd): message = escape(util.restore_text(kwd.get('message', ''))) - q = sa.select((galaxy.model.corals.Phenotype.table.c.id, - galaxy.model.corals.Phenotype.table.c.disease_resist, - galaxy.model.corals.Phenotype.table.c.bleach_resist, - galaxy.model.corals.Phenotype.table.c.mortality, - galaxy.model.corals.Phenotype.table.c.tle, - galaxy.model.corals.Phenotype.table.c.spawning, - galaxy.model.corals.Phenotype.table.c.sperm_motility, - galaxy.model.corals.Phenotype.table.c.healing_time), - from_obj=[galaxy.model.corals.Phenotype.table], - order_by=[galaxy.model.corals.Phenotype.table.c.id]) phenotypes = [] - for row in q.execute(): - cols_tup = (row.id, row.disease_resist, row.bleach_resist, - row.mortality, row.tle, row.spawning, row.sperm_motility, - row.healing_time) - phenotypes.append(cols_tup) + for row in trans.sa_session.query(corals.Phenotype): + phenotypes.append((row.id, row.disease_resist, row.bleach_resist, row.mortality, row.tle, row.spawning, row.sperm_motility, row.healing_time)) return trans.fill_template('/webapps/coralsnp_reports/phenotypes.mako', phenotypes=phenotypes, message=message) @web.expose @@ -40,21 +29,23 @@ def of_sample(self, trans, **kwd): message = escape(util.restore_text(kwd.get('message', ''))) affy_id = kwd.get('affy_id') phenotype_id = kwd.get('phenotype_id') - q = sa.select((galaxy.model.corals.Phenotype.table.c.disease_resist, - galaxy.model.corals.Phenotype.table.c.bleach_resist, - galaxy.model.corals.Phenotype.table.c.mortality, - galaxy.model.corals.Phenotype.table.c.tle, - galaxy.model.corals.Phenotype.table.c.spawning, - galaxy.model.corals.Phenotype.table.c.sperm_motility, - galaxy.model.corals.Phenotype.table.c.healing_time), - whereclause=sa.and_(galaxy.model.corals.Phenotype.table.c.id == phenotype_id), - from_obj=[galaxy.model.corals.Phenotype.table], - order_by=[galaxy.model.corals.Phenotype.table.c.id]) + q = ( + sa.select( + corals.Phenotype.disease_resist, + corals.Phenotype.bleach_resist, + corals.Phenotype.mortality, + corals.Phenotype.tle, + corals.Phenotype.spawning, + corals.Phenotype.sperm_motility, + corals.Phenotype.healing_time + ) + .select_from(corals.Phenotype.table) + .where(corals.Phenotype.table.c.id == phenotype_id) + .order_by(corals.Phenotype.table.c.id) + ) phenotypes = [] - for row in q.execute(): - cols_tup = (row.disease_resist, row.bleach_resist, row.mortality, row.tle, - row.spawning, row.sperm_motility, row.healing_time) - phenotypes.append(cols_tup) + for row in trans.sa_session.execute(q): + phenotypes.append((row.disease_resist, row.bleach_resist, row.mortality, row.tle, row.spawning, row.sperm_motility, row.healing_time)) return trans.fill_template('/webapps/coralsnp_reports/phenotype_of_sample.mako', affy_id=affy_id, phenotypes=phenotypes, diff --git a/galaxy/coralsnp_reports/lib/galaxy/webapps/coralsnp_reports/controllers/query.py b/galaxy/coralsnp_reports/lib/galaxy/webapps/coralsnp_reports/controllers/query.py new file mode 100644 index 00000000..f264d625 --- /dev/null +++ b/galaxy/coralsnp_reports/lib/galaxy/webapps/coralsnp_reports/controllers/query.py @@ -0,0 +1,17 @@ +""" Mixin to help build advanced queries coralsnp for reports interface. +""" +import sqlalchemy as sa + + +class ReportQueryBuilder: + def group_by_month(self, column): + return sa.func.date_trunc("month", sa.func.date(column)) + + def select_month(self, column): + return sa.func.date_trunc("month", sa.func.date(column)) + + def group_by_day(self, column): + return sa.func.date_trunc("day", sa.func.date(column)) + + def select_day(self, column): + return sa.func.date_trunc("day", sa.func.date(column)) diff --git a/galaxy/coralsnp_reports/lib/galaxy/webapps/coralsnp_reports/controllers/reefs.py b/galaxy/coralsnp_reports/lib/galaxy/webapps/coralsnp_reports/controllers/reefs.py index d6be4080..e2dc8ce8 100644 --- a/galaxy/coralsnp_reports/lib/galaxy/webapps/coralsnp_reports/controllers/reefs.py +++ b/galaxy/coralsnp_reports/lib/galaxy/webapps/coralsnp_reports/controllers/reefs.py @@ -3,11 +3,13 @@ import sqlalchemy as sa from markupsafe import escape -import galaxy.model +from galaxy.model import corals from galaxy import util -from . import BaseUIController -from galaxy.web.base.controller import web -from galaxy.webapps.reports.controllers.query import ReportQueryBuilder +from galaxy.webapps.base.controller import ( + BaseUIController, + web, +) +from galaxy.webapps.coralsnp_reports.controllers.query import ReportQueryBuilder log = logging.getLogger(__name__) @@ -17,23 +19,25 @@ class Reefs(BaseUIController, ReportQueryBuilder): @web.expose def all(self, trans, **kwd): message = escape(util.restore_text(kwd.get('message', ''))) - q = sa.select((galaxy.model.corals.Reef.table.c.id, - galaxy.model.corals.Reef.table.c.name, - galaxy.model.corals.Reef.table.c.region, - galaxy.model.corals.Reef.table.c.latitude, - galaxy.model.corals.Reef.table.c.longitude, - galaxy.model.corals.Reef.table.c.geographic_origin, - galaxy.model.corals.Sample.table.c.colony_id, - galaxy.model.corals.Sample.table.c.public, - galaxy.model.corals.Sample.table.c.public_after_date), - from_obj=[galaxy.model.corals.Reef.table, - galaxy.model.corals.Colony.table, - galaxy.model.corals.Sample.table], - whereclause=sa.and_(galaxy.model.corals.Sample.table.c.colony_id == galaxy.model.corals.Colony.table.c.id, - galaxy.model.corals.Colony.table.c.reef_id == galaxy.model.corals.Reef.table.c.id), - order_by=[galaxy.model.corals.Reef.table.c.id]) + q = ( + sa.select( + corals.Reef.id, + corals.Reef.name, + corals.Reef.region, + corals.Reef.latitude, + corals.Reef.longitude, + corals.Reef.geographic_origin, + corals.Sample.colony_id, + corals.Sample.public, + corals.Sample.public_after_date + ) + .select_from(corals.Reef) + .join(corals.Colony, corals.Reef.id == corals.Colony.reef_id) + .join(corals.Sample, corals.Colony.id == corals.Sample.colony_id) + .order_by(corals.Reef.id) + ) reefs = [] - for row in q.execute(): + for row in trans.sa_session.execute(q): public_after_date = str(row.public_after_date)[:10] if str(row.public) == "True": latitude = row.latitude @@ -41,8 +45,7 @@ def all(self, trans, **kwd): else: latitude = "Private until %s" % public_after_date longitude = "Private until %s" % public_after_date - cols_tup = (row.id, row.name, row.region, latitude, longitude, row.geographic_origin) - reefs.append(cols_tup) + reefs.append((row.id, row.name, row.region, latitude, longitude, row.geographic_origin)) return trans.fill_template('/webapps/coralsnp_reports/reefs.mako', reefs=reefs, message=message) @web.expose @@ -52,22 +55,23 @@ def of_colony(self, trans, **kwd): latitude = kwd.get('latitude') longitude = kwd.get('longitude') depth = kwd.get('depth') - q = sa.select((galaxy.model.corals.Reef.table.c.name, - galaxy.model.corals.Reef.table.c.region, - galaxy.model.corals.Reef.table.c.latitude, - galaxy.model.corals.Reef.table.c.longitude, - galaxy.model.corals.Reef.table.c.geographic_origin, - galaxy.model.corals.Sample.table.c.public, - galaxy.model.corals.Sample.table.c.public_after_date), - from_obj=[galaxy.model.corals.Reef.table, - galaxy.model.corals.Colony.table, - galaxy.model.corals.Sample.table], - whereclause=sa.and_(galaxy.model.corals.Reef.table.c.id == reef_id, - galaxy.model.corals.Sample.table.c.colony_id == galaxy.model.corals.Colony.table.c.id, - galaxy.model.corals.Colony.table.c.reef_id == galaxy.model.corals.Reef.table.c.id), - order_by=[galaxy.model.corals.Reef.table.c.id]) + q = ( + sa.select( + corals.Reef.name, + corals.Reef.region, + corals.Reef.latitude, + corals.Reef.longitude, + corals.Reef.geographic_origin, + corals.Sample.public, + corals.Sample.public_after_date + ) + .select_from(corals.Reef).where(corals.Reef.id == reef_id) + .join(corals.Colony, corals.Reef.id == corals.Colony.reef_id) + .join(corals.Sample, corals.Sample.colony_id == corals.Colony.id) + .order_by(corals.Reef.id) + ) reefs = [] - for row in q.execute(): + for row in trans.sa_session.execute(q): public_after_date = str(row.public_after_date)[:10] if str(row.public) == "True": latitude = row.latitude @@ -75,8 +79,7 @@ def of_colony(self, trans, **kwd): else: latitude = "Private until %s" % public_after_date longitude = "Private until %s" % public_after_date - cols_tup = (row.name, row.region, latitude, longitude, row.geographic_origin) - reefs.append(cols_tup) + reefs.append((row.name, row.region, latitude, longitude, row.geographic_origin)) return trans.fill_template('/webapps/coralsnp_reports/reef_of_colony.mako', latitude=latitude, longitude=longitude, diff --git a/galaxy/coralsnp_reports/lib/galaxy/webapps/coralsnp_reports/controllers/root.py b/galaxy/coralsnp_reports/lib/galaxy/webapps/coralsnp_reports/controllers/root.py index f8d42b28..2b03fa70 100644 --- a/galaxy/coralsnp_reports/lib/galaxy/webapps/coralsnp_reports/controllers/root.py +++ b/galaxy/coralsnp_reports/lib/galaxy/webapps/coralsnp_reports/controllers/root.py @@ -1,12 +1,15 @@ import logging -from . import BaseUIController -from galaxy.web.base.controller import web +from galaxy.webapps.base.controller import ( + BaseUIController, + web, +) +from galaxy.webapps.coralsnp_reports.controllers.query import ReportQueryBuilder log = logging.getLogger(__name__) -class CoralSNPReport(BaseUIController): +class CoralSNPReport(BaseUIController, ReportQueryBuilder): @web.expose def index(self, trans, **kwd): return trans.fill_template('/webapps/coralsnp_reports/index.mako') diff --git a/galaxy/coralsnp_reports/lib/galaxy/webapps/coralsnp_reports/controllers/samples.py b/galaxy/coralsnp_reports/lib/galaxy/webapps/coralsnp_reports/controllers/samples.py index b0ec0aba..8a937cec 100644 --- a/galaxy/coralsnp_reports/lib/galaxy/webapps/coralsnp_reports/controllers/samples.py +++ b/galaxy/coralsnp_reports/lib/galaxy/webapps/coralsnp_reports/controllers/samples.py @@ -9,12 +9,14 @@ import sqlalchemy as sa from markupsafe import escape -import galaxy.model +from galaxy.model import corals from galaxy import util -from . import BaseUIController -from galaxy.web.base.controller import web +from galaxy.webapps.base.controller import ( + BaseUIController, + web, +) from galaxy.webapps.reports.controllers.jobs import sorter -from galaxy.webapps.reports.controllers.query import ReportQueryBuilder +from galaxy.webapps.coralsnp_reports.controllers.query import ReportQueryBuilder log = logging.getLogger(__name__) @@ -24,34 +26,8 @@ class Samples(BaseUIController, ReportQueryBuilder): @web.expose def all(self, trans, **kwd): message = escape(util.restore_text(kwd.get('message', ''))) - q = sa.select((galaxy.model.corals.Sample.table.c.id, - galaxy.model.corals.Sample.table.c.affy_id, - galaxy.model.corals.Sample.table.c.sample_id, - galaxy.model.corals.Sample.table.c.allele_id, - galaxy.model.corals.Sample.table.c.genotype_id, - galaxy.model.corals.Sample.table.c.phenotype_id, - galaxy.model.corals.Sample.table.c.experiment_id, - galaxy.model.corals.Sample.table.c.colony_id, - galaxy.model.corals.Sample.table.c.colony_location, - galaxy.model.corals.Sample.table.c.taxonomy_id, - galaxy.model.corals.Sample.table.c.collector_id, - galaxy.model.corals.Sample.table.c.field_call, - galaxy.model.corals.Sample.table.c.collection_date, - galaxy.model.corals.Sample.table.c.user_specimen_id, - galaxy.model.corals.Sample.table.c.registry_id, - galaxy.model.corals.Sample.table.c.depth, - galaxy.model.corals.Sample.table.c.dna_extraction_method, - galaxy.model.corals.Sample.table.c.dna_concentration, - galaxy.model.corals.Sample.table.c.public_after_date, - galaxy.model.corals.Sample.table.c.percent_missing_data_coral, - galaxy.model.corals.Sample.table.c.percent_acerv_coral, - galaxy.model.corals.Sample.table.c.percent_apalm_coral, - galaxy.model.corals.Sample.table.c.percent_heterozygous_coral, - galaxy.model.corals.Sample.table.c.bcoral_genet_id), - from_obj=[galaxy.model.corals.Sample.table], - order_by=[galaxy.model.corals.Sample.table.c.id]) samples = [] - for row in q.execute(): + for row in trans.sa_session.query(corals.Sample): try: collection_date = row.collection_date.strftime("%Y-%m-%d") except Exception: @@ -74,7 +50,7 @@ def all(self, trans, **kwd): @web.expose def all_by_upload_date(self, trans, **kwd): message = escape(util.restore_text(kwd.get('message', ''))) - num_samples = trans.sa_session.query(galaxy.model.corals.Sample).count() + num_samples = trans.sa_session.query(corals.Sample).count() return trans.fill_template('/webapps/coralsnp_reports/samples_by_upload_date.mako', num_samples=num_samples, message=message) @web.expose @@ -83,34 +59,38 @@ def collected_by(self, trans, **kwd): last_name = kwd.get('last_name') first_name = kwd.get('first_name') collector_id = kwd.get('collector_id') - q = sa.select((galaxy.model.corals.Sample.table.c.id, - galaxy.model.corals.Sample.table.c.affy_id, - galaxy.model.corals.Sample.table.c.sample_id, - galaxy.model.corals.Sample.table.c.allele_id, - galaxy.model.corals.Sample.table.c.genotype_id, - galaxy.model.corals.Sample.table.c.phenotype_id, - galaxy.model.corals.Sample.table.c.experiment_id, - galaxy.model.corals.Sample.table.c.colony_id, - galaxy.model.corals.Sample.table.c.colony_location, - galaxy.model.corals.Sample.table.c.taxonomy_id, - galaxy.model.corals.Sample.table.c.field_call, - galaxy.model.corals.Sample.table.c.collection_date, - galaxy.model.corals.Sample.table.c.user_specimen_id, - galaxy.model.corals.Sample.table.c.registry_id, - galaxy.model.corals.Sample.table.c.depth, - galaxy.model.corals.Sample.table.c.dna_extraction_method, - galaxy.model.corals.Sample.table.c.dna_concentration, - galaxy.model.corals.Sample.table.c.public_after_date, - galaxy.model.corals.Sample.table.c.percent_missing_data_coral, - galaxy.model.corals.Sample.table.c.percent_acerv_coral, - galaxy.model.corals.Sample.table.c.percent_apalm_coral, - galaxy.model.corals.Sample.table.c.percent_heterozygous_coral, - galaxy.model.corals.Sample.table.c.bcoral_genet_id), - whereclause=galaxy.model.corals.Sample.table.c.collector_id == collector_id, - from_obj=[galaxy.model.corals.Sample.table], - order_by=[galaxy.model.corals.Sample.table.c.id]) + q = ( + sa.select( + corals.Sample.id, + corals.Sample.affy_id, + corals.Sample.sample_id, + corals.Sample.allele_id, + corals.Sample.genotype_id, + corals.Sample.phenotype_id, + corals.Sample.experiment_id, + corals.Sample.colony_id, + corals.Sample.colony_location, + corals.Sample.taxonomy_id, + corals.Sample.field_call, + corals.Sample.collection_date, + corals.Sample.user_specimen_id, + corals.Sample.registry_id, + corals.Sample.depth, + corals.Sample.dna_extraction_method, + corals.Sample.dna_concentration, + corals.Sample.public_after_date, + corals.Sample.percent_missing_data_coral, + corals.Sample.percent_acerv_coral, + corals.Sample.percent_apalm_coral, + corals.Sample.percent_heterozygous_coral, + corals.Sample.bcoral_genet_id + ) + .select_from(corals.Sample.table) + .where(corals.Sample.table.c.collector_id == collector_id) + .order_by(corals.Sample.table.c.id) + ) samples = [] - for row in q.execute(): + for row in trans.sa_session.execute(q): try: collection_date = row.collection_date.strftime("%Y-%m-%d") except Exception: @@ -142,35 +122,39 @@ def of_colony(self, trans, **kwd): longitude = kwd.get('longitude') depth = kwd.get('depth') reef_id = kwd.get('reef_id') - q = sa.select((galaxy.model.corals.Sample.table.c.id, - galaxy.model.corals.Sample.table.c.affy_id, - galaxy.model.corals.Sample.table.c.sample_id, - galaxy.model.corals.Sample.table.c.allele_id, - galaxy.model.corals.Sample.table.c.genotype_id, - galaxy.model.corals.Sample.table.c.phenotype_id, - galaxy.model.corals.Sample.table.c.experiment_id, - galaxy.model.corals.Sample.table.c.colony_id, - galaxy.model.corals.Sample.table.c.colony_location, - galaxy.model.corals.Sample.table.c.taxonomy_id, - galaxy.model.corals.Sample.table.c.collector_id, - galaxy.model.corals.Sample.table.c.collection_date, - galaxy.model.corals.Sample.table.c.user_specimen_id, - galaxy.model.corals.Sample.table.c.registry_id, - galaxy.model.corals.Sample.table.c.depth, - galaxy.model.corals.Sample.table.c.dna_extraction_method, - galaxy.model.corals.Sample.table.c.dna_concentration, - galaxy.model.corals.Sample.table.c.public_after_date, - galaxy.model.corals.Sample.table.c.percent_missing_data_coral, - galaxy.model.corals.Sample.table.c.percent_acerv_coral, - galaxy.model.corals.Sample.table.c.percent_apalm_coral, - galaxy.model.corals.Sample.table.c.percent_heterozygous_coral, - galaxy.model.corals.Sample.table.c.field_call, - galaxy.model.corals.Sample.table.c.bcoral_genet_id), - whereclause=galaxy.model.corals.Sample.table.c.colony_id == colony_id, - from_obj=[galaxy.model.corals.Sample.table], - order_by=[galaxy.model.corals.Sample.table.c.id]) + q = ( + sa.select( + corals.Sample.id, + corals.Sample.affy_id, + corals.Sample.sample_id, + corals.Sample.allele_id, + corals.Sample.genotype_id, + corals.Sample.phenotype_id, + corals.Sample.experiment_id, + corals.Sample.colony_id, + corals.Sample.colony_location, + corals.Sample.taxonomy_id, + corals.Sample.collector_id, + corals.Sample.collection_date, + corals.Sample.user_specimen_id, + corals.Sample.registry_id, + corals.Sample.depth, + corals.Sample.dna_extraction_method, + corals.Sample.dna_concentration, + corals.Sample.public_after_date, + corals.Sample.percent_missing_data_coral, + corals.Sample.percent_acerv_coral, + corals.Sample.percent_apalm_coral, + corals.Sample.percent_heterozygous_coral, + corals.Sample.field_call, + corals.Sample.bcoral_genet_id + ) + .select_from(corals.Sample.table) + .where(corals.Sample.table.c.colony_id == colony_id) + .order_by(corals.Sample.table.c.id) + ) samples = [] - for row in q.execute(): + for row in trans.sa_session.execute(q): try: collection_date = row.collection_date.strftime("%Y-%m-%d") except Exception: @@ -203,35 +187,39 @@ def of_experiment(self, trans, **kwd): array_version = kwd.get('array_version') result_folder_name = kwd.get('result_folder_name') plate_barcode = kwd.get('plate_barcode') - q = sa.select((galaxy.model.corals.Sample.table.c.id, - galaxy.model.corals.Sample.table.c.affy_id, - galaxy.model.corals.Sample.table.c.sample_id, - galaxy.model.corals.Sample.table.c.allele_id, - galaxy.model.corals.Sample.table.c.genotype_id, - galaxy.model.corals.Sample.table.c.phenotype_id, - galaxy.model.corals.Sample.table.c.experiment_id, - galaxy.model.corals.Sample.table.c.colony_id, - galaxy.model.corals.Sample.table.c.colony_location, - galaxy.model.corals.Sample.table.c.taxonomy_id, - galaxy.model.corals.Sample.table.c.collector_id, - galaxy.model.corals.Sample.table.c.collection_date, - galaxy.model.corals.Sample.table.c.user_specimen_id, - galaxy.model.corals.Sample.table.c.registry_id, - galaxy.model.corals.Sample.table.c.depth, - galaxy.model.corals.Sample.table.c.dna_extraction_method, - galaxy.model.corals.Sample.table.c.dna_concentration, - galaxy.model.corals.Sample.table.c.public_after_date, - galaxy.model.corals.Sample.table.c.percent_missing_data_coral, - galaxy.model.corals.Sample.table.c.percent_acerv_coral, - galaxy.model.corals.Sample.table.c.percent_apalm_coral, - galaxy.model.corals.Sample.table.c.percent_heterozygous_coral, - galaxy.model.corals.Sample.table.c.field_call, - galaxy.model.corals.Sample.table.c.bcoral_genet_id), - whereclause=galaxy.model.corals.Sample.table.c.experiment_id == experiment_id, - from_obj=[galaxy.model.corals.Sample.table], - order_by=[galaxy.model.corals.Sample.table.c.id]) + q = ( + sa.select( + corals.Sample.id, + corals.Sample.affy_id, + corals.Sample.sample_id, + corals.Sample.allele_id, + corals.Sample.genotype_id, + corals.Sample.phenotype_id, + corals.Sample.experiment_id, + corals.Sample.colony_id, + corals.Sample.colony_location, + corals.Sample.taxonomy_id, + corals.Sample.collector_id, + corals.Sample.collection_date, + corals.Sample.user_specimen_id, + corals.Sample.registry_id, + corals.Sample.depth, + corals.Sample.dna_extraction_method, + corals.Sample.dna_concentration, + corals.Sample.public_after_date, + corals.Sample.percent_missing_data_coral, + corals.Sample.percent_acerv_coral, + corals.Sample.percent_apalm_coral, + corals.Sample.percent_heterozygous_coral, + corals.Sample.field_call, + corals.Sample.bcoral_genet_id + ) + .select_from(corals.Sample.table) + .where(corals.Sample.table.c.experiment_id == experiment_id) + .order_by(corals.Sample.table.c.id) + ) samples = [] - for row in q.execute(): + for row in trans.sa_session.execute(q): try: collection_date = row.collection_date.strftime("%Y-%m-%d") except Exception: @@ -265,41 +253,42 @@ def of_reef(self, trans, **kwd): latitude = kwd.get('latitude') longitude = kwd.get('longitude') geographic_origin = kwd.get('geographic_origin') - q = sa.select((galaxy.model.corals.Sample.table.c.id, - galaxy.model.corals.Sample.table.c.affy_id, - galaxy.model.corals.Sample.table.c.sample_id, - galaxy.model.corals.Sample.table.c.allele_id, - galaxy.model.corals.Sample.table.c.genotype_id, - galaxy.model.corals.Sample.table.c.phenotype_id, - galaxy.model.corals.Sample.table.c.experiment_id, - galaxy.model.corals.Sample.table.c.colony_id, - galaxy.model.corals.Sample.table.c.colony_location, - galaxy.model.corals.Sample.table.c.taxonomy_id, - galaxy.model.corals.Sample.table.c.collector_id, - galaxy.model.corals.Sample.table.c.collection_date, - galaxy.model.corals.Sample.table.c.user_specimen_id, - galaxy.model.corals.Sample.table.c.registry_id, - galaxy.model.corals.Sample.table.c.depth, - galaxy.model.corals.Sample.table.c.dna_extraction_method, - galaxy.model.corals.Sample.table.c.dna_concentration, - galaxy.model.corals.Sample.table.c.public_after_date, - galaxy.model.corals.Sample.table.c.percent_missing_data_coral, - galaxy.model.corals.Sample.table.c.percent_acerv_coral, - galaxy.model.corals.Sample.table.c.percent_apalm_coral, - galaxy.model.corals.Sample.table.c.percent_heterozygous_coral, - galaxy.model.corals.Sample.table.c.field_call, - galaxy.model.corals.Sample.table.c.bcoral_genet_id, - galaxy.model.corals.Colony.table.c.reef_id, - galaxy.model.corals.Reef.table.c.id), - from_obj=[galaxy.model.corals.Sample.table, - galaxy.model.corals.Colony.table, - galaxy.model.corals.Reef.table], - whereclause=sa.and_(galaxy.model.corals.Sample.table.c.colony_id == galaxy.model.corals.Colony.table.c.id, - galaxy.model.corals.Colony.table.c.reef_id == galaxy.model.corals.Reef.table.c.id, - galaxy.model.corals.Reef.table.c.id == reef_id), - order_by=[galaxy.model.corals.Sample.table.c.id]) + q = ( + sa.select( + corals.Sample.id, + corals.Sample.affy_id, + corals.Sample.sample_id, + corals.Sample.allele_id, + corals.Sample.genotype_id, + corals.Sample.phenotype_id, + corals.Sample.experiment_id, + corals.Sample.colony_id, + corals.Sample.colony_location, + corals.Sample.taxonomy_id, + corals.Sample.collector_id, + corals.Sample.collection_date, + corals.Sample.user_specimen_id, + corals.Sample.registry_id, + corals.Sample.depth, + corals.Sample.dna_extraction_method, + corals.Sample.dna_concentration, + corals.Sample.public_after_date, + corals.Sample.percent_missing_data_coral, + corals.Sample.percent_acerv_coral, + corals.Sample.percent_apalm_coral, + corals.Sample.percent_heterozygous_coral, + corals.Sample.field_call, + corals.Sample.bcoral_genet_id, + corals.Colony.reef_id, + corals.Reef.id + ) + .select_from(corals.Sample) + .join(corals.Colony, corals.Colony.table.c.id == corals.Sample.table.c.colony_id) + .join(corals.Reef, sa.and_(corals.Reef.table.c.id == reef_id, corals.Reef.table.c.id == corals.Colony.table.c.reef_id)) + .order_by(corals.Sample.table.c.id) + ) samples = [] - for row in q.execute(): + for row in trans.sa_session.execute(q): try: collection_date = row.collection_date.strftime("%Y-%m-%d") except Exception: @@ -331,35 +320,39 @@ def of_taxonomy(self, trans, **kwd): taxonomy_id = kwd.get('taxonomy_id') species_name = kwd.get('species_name') genus_name = kwd.get('genus_name') - q = sa.select((galaxy.model.corals.Sample.table.c.id, - galaxy.model.corals.Sample.table.c.affy_id, - galaxy.model.corals.Sample.table.c.sample_id, - galaxy.model.corals.Sample.table.c.allele_id, - galaxy.model.corals.Sample.table.c.genotype_id, - galaxy.model.corals.Sample.table.c.phenotype_id, - galaxy.model.corals.Sample.table.c.experiment_id, - galaxy.model.corals.Sample.table.c.colony_id, - galaxy.model.corals.Sample.table.c.colony_location, - galaxy.model.corals.Sample.table.c.taxonomy_id, - galaxy.model.corals.Sample.table.c.collector_id, - galaxy.model.corals.Sample.table.c.collection_date, - galaxy.model.corals.Sample.table.c.user_specimen_id, - galaxy.model.corals.Sample.table.c.registry_id, - galaxy.model.corals.Sample.table.c.depth, - galaxy.model.corals.Sample.table.c.dna_extraction_method, - galaxy.model.corals.Sample.table.c.dna_concentration, - galaxy.model.corals.Sample.table.c.public_after_date, - galaxy.model.corals.Sample.table.c.percent_missing_data_coral, - galaxy.model.corals.Sample.table.c.percent_acerv_coral, - galaxy.model.corals.Sample.table.c.percent_apalm_coral, - galaxy.model.corals.Sample.table.c.percent_heterozygous_coral, - galaxy.model.corals.Sample.table.c.field_call, - galaxy.model.corals.Sample.table.c.bcoral_genet_id), - whereclause=galaxy.model.corals.Sample.table.c.taxonomy_id == taxonomy_id, - from_obj=[galaxy.model.corals.Sample.table], - order_by=[galaxy.model.corals.Sample.table.c.id]) + q = ( + sa.select( + corals.Sample.id, + corals.Sample.affy_id, + corals.Sample.sample_id, + corals.Sample.allele_id, + corals.Sample.genotype_id, + corals.Sample.phenotype_id, + corals.Sample.experiment_id, + corals.Sample.colony_id, + corals.Sample.colony_location, + corals.Sample.taxonomy_id, + corals.Sample.collector_id, + corals.Sample.collection_date, + corals.Sample.user_specimen_id, + corals.Sample.registry_id, + corals.Sample.depth, + corals.Sample.dna_extraction_method, + corals.Sample.dna_concentration, + corals.Sample.public_after_date, + corals.Sample.percent_missing_data_coral, + corals.Sample.percent_acerv_coral, + corals.Sample.percent_apalm_coral, + corals.Sample.percent_heterozygous_coral, + corals.Sample.field_call, + corals.Sample.bcoral_genet_id + ) + .select_from(corals.Sample.table) + .where(corals.Sample.table.c.taxonomy_id == taxonomy_id) + .order_by(corals.Sample.table.c.id) + ) samples = [] - for row in q.execute(): + for row in trans.sa_session.execute(q): try: collection_date = row.collection_date.strftime("%Y-%m-%d") except Exception: @@ -389,15 +382,18 @@ def per_month(self, trans, **kwd): sort_id = specs.sort_id order = specs.order arrow = specs.arrow - _order = specs.exc_order - q = sa.select((self.select_month(galaxy.model.corals.Sample.table.c.create_time).label('date'), - sa.func.count(galaxy.model.corals.Sample.table.c.id).label('num_samples')), - from_obj=[galaxy.model.corals.Sample.table], - group_by=self.group_by_month(galaxy.model.corals.Sample.table.c.create_time), - order_by=[_order]) + q = ( + sa.select( + self.select_month(corals.Sample.table.c.create_time).label('date'), + sa.func.count(corals.Sample.table.c.id).label('num_samples'), + ) + .select_from(corals.Sample.table) + .group_by(self.group_by_month(corals.Sample.table.c.create_time)) + .order_by(specs.exc_order) + ) samples = [] - for row in q.execute(): + for row in trans.sa_session.execute(q): samples.append((row.date.strftime("%Y-%m"), row.num_samples, row.date.strftime("%B"), @@ -421,37 +417,41 @@ def specified_date(self, trans, **kwd): day_label = start_date.strftime("%A") month_label = start_date.strftime("%B") year_label = start_date.strftime("%Y") - q = sa.select((self.select_day(galaxy.model.corals.Sample.table.c.create_time).label('date'), - galaxy.model.corals.Sample.table.c.id, - galaxy.model.corals.Sample.table.c.affy_id, - galaxy.model.corals.Sample.table.c.sample_id, - galaxy.model.corals.Sample.table.c.allele_id, - galaxy.model.corals.Sample.table.c.genotype_id, - galaxy.model.corals.Sample.table.c.phenotype_id, - galaxy.model.corals.Sample.table.c.experiment_id, - galaxy.model.corals.Sample.table.c.colony_id, - galaxy.model.corals.Sample.table.c.colony_location, - galaxy.model.corals.Sample.table.c.taxonomy_id, - galaxy.model.corals.Sample.table.c.collector_id, - galaxy.model.corals.Sample.table.c.field_call, - galaxy.model.corals.Sample.table.c.collection_date, - galaxy.model.corals.Sample.table.c.user_specimen_id, - galaxy.model.corals.Sample.table.c.registry_id, - galaxy.model.corals.Sample.table.c.depth, - galaxy.model.corals.Sample.table.c.dna_extraction_method, - galaxy.model.corals.Sample.table.c.dna_concentration, - galaxy.model.corals.Sample.table.c.public_after_date, - galaxy.model.corals.Sample.table.c.percent_missing_data_coral, - galaxy.model.corals.Sample.table.c.percent_acerv_coral, - galaxy.model.corals.Sample.table.c.percent_apalm_coral, - galaxy.model.corals.Sample.table.c.percent_heterozygous_coral, - galaxy.model.corals.Sample.table.c.bcoral_genet_id), - whereclause=sa.and_(galaxy.model.corals.Sample.table.c.create_time >= start_date, - galaxy.model.corals.Sample.table.c.create_time < end_date), - from_obj=[galaxy.model.corals.Sample.table], - order_by=[galaxy.model.corals.Sample.table.c.id]) + q = ( + sa.select( + self.select_day(corals.Sample.table.c.create_time).label('date'), + corals.Sample.id, + corals.Sample.affy_id, + corals.Sample.sample_id, + corals.Sample.allele_id, + corals.Sample.genotype_id, + corals.Sample.phenotype_id, + corals.Sample.experiment_id, + corals.Sample.colony_id, + corals.Sample.colony_location, + corals.Sample.taxonomy_id, + corals.Sample.collector_id, + corals.Sample.field_call, + corals.Sample.collection_date, + corals.Sample.user_specimen_id, + corals.Sample.registry_id, + corals.Sample.depth, + corals.Sample.dna_extraction_method, + corals.Sample.dna_concentration, + corals.Sample.public_after_date, + corals.Sample.percent_missing_data_coral, + corals.Sample.percent_acerv_coral, + corals.Sample.percent_apalm_coral, + corals.Sample.percent_heterozygous_coral, + corals.Sample.bcoral_genet_id + ) + .select_from(corals.Sample.table) + .where(sa.and_(corals.Sample.table.c.create_time >= start_date, + corals.Sample.table.c.create_time < end_date)) + .order_by(corals.Sample.table.c.id) + ) samples = [] - for row in q.execute(): + for row in trans.sa_session.execute(q): try: collection_date = row.collection_date.strftime("%Y-%m-%d") except Exception: @@ -489,15 +489,18 @@ def specified_month(self, trans, **kwd): end_date = start_date + timedelta(days=calendar.monthrange(year, month)[1]) month_label = start_date.strftime("%B") year_label = start_date.strftime("%Y") - q = sa.select((self.select_day(galaxy.model.corals.Sample.table.c.create_time).label('date'), - sa.func.count(galaxy.model.corals.Sample.table.c.id).label('num_samples')), - whereclause=sa.and_(galaxy.model.corals.Sample.table.c.create_time >= start_date, - galaxy.model.corals.Sample.table.c.create_time < end_date), - from_obj=[galaxy.model.corals.Sample.table], - group_by=self.group_by_day(galaxy.model.corals.Sample.table.c.create_time), - order_by=[sa.desc('date')]) + q = ( + sa.select( + self.select_day(corals.Sample.table.c.create_time).label('date'), + sa.func.count(corals.Sample.table.c.id).label('num_samples')) + .select_from(corals.Sample.table) + .where(sa.and_(corals.Sample.table.c.create_time >= start_date, + corals.Sample.table.c.create_time < end_date)) + .group_by(self.group_by_day(corals.Sample.table.c.create_time)) + .order_by(sa.desc('date')) + ) samples = [] - for row in q.execute(): + for row in trans.sa_session.execute(q): samples.append((row.date.strftime("%Y-%m-%d"), row.date.strftime("%d"), row.num_samples, @@ -516,34 +519,38 @@ def with_genotype(self, trans, **kwd): coral_mlg_clonal_id = kwd.get('coral_mlg_clonal_id') coral_mlg_rep_sample_id = kwd.get('coral_mlg_rep_sample_id') genetic_coral_species_call = kwd.get('genetic_coral_species_call') - q = sa.select((galaxy.model.corals.Sample.table.c.id, - galaxy.model.corals.Sample.table.c.affy_id, - galaxy.model.corals.Sample.table.c.sample_id, - galaxy.model.corals.Sample.table.c.allele_id, - galaxy.model.corals.Sample.table.c.phenotype_id, - galaxy.model.corals.Sample.table.c.experiment_id, - galaxy.model.corals.Sample.table.c.colony_id, - galaxy.model.corals.Sample.table.c.colony_location, - galaxy.model.corals.Sample.table.c.taxonomy_id, - galaxy.model.corals.Sample.table.c.collector_id, - galaxy.model.corals.Sample.table.c.collection_date, - galaxy.model.corals.Sample.table.c.user_specimen_id, - galaxy.model.corals.Sample.table.c.registry_id, - galaxy.model.corals.Sample.table.c.depth, - galaxy.model.corals.Sample.table.c.dna_extraction_method, - galaxy.model.corals.Sample.table.c.dna_concentration, - galaxy.model.corals.Sample.table.c.public_after_date, - galaxy.model.corals.Sample.table.c.percent_missing_data_coral, - galaxy.model.corals.Sample.table.c.percent_acerv_coral, - galaxy.model.corals.Sample.table.c.percent_apalm_coral, - galaxy.model.corals.Sample.table.c.percent_heterozygous_coral, - galaxy.model.corals.Sample.table.c.field_call, - galaxy.model.corals.Sample.table.c.bcoral_genet_id), - whereclause=galaxy.model.corals.Sample.table.c.genotype_id == genotype_id, - from_obj=[galaxy.model.corals.Sample.table], - order_by=[galaxy.model.corals.Sample.table.c.id]) + q = ( + sa.select( + corals.Sample.id, + corals.Sample.affy_id, + corals.Sample.sample_id, + corals.Sample.allele_id, + corals.Sample.phenotype_id, + corals.Sample.experiment_id, + corals.Sample.colony_id, + corals.Sample.colony_location, + corals.Sample.taxonomy_id, + corals.Sample.collector_id, + corals.Sample.collection_date, + corals.Sample.user_specimen_id, + corals.Sample.registry_id, + corals.Sample.depth, + corals.Sample.dna_extraction_method, + corals.Sample.dna_concentration, + corals.Sample.public_after_date, + corals.Sample.percent_missing_data_coral, + corals.Sample.percent_acerv_coral, + corals.Sample.percent_apalm_coral, + corals.Sample.percent_heterozygous_coral, + corals.Sample.field_call, + corals.Sample.bcoral_genet_id + ) + .select_from(corals.Sample.table) + .where(corals.Sample.table.c.genotype_id == genotype_id) + .order_by(corals.Sample.table.c.id) + ) samples = [] - for row in q.execute(): + for row in trans.sa_session.execute(q): try: collection_date = row.collection_date.strftime("%Y-%m-%d") except Exception: @@ -578,34 +585,38 @@ def with_phenotype(self, trans, **kwd): spawning = kwd.get('spawning') sperm_motility = kwd.get('sperm_motility') healing_time = kwd.get('healing_time') - q = sa.select((galaxy.model.corals.Sample.table.c.id, - galaxy.model.corals.Sample.table.c.affy_id, - galaxy.model.corals.Sample.table.c.sample_id, - galaxy.model.corals.Sample.table.c.allele_id, - galaxy.model.corals.Sample.table.c.genotype_id, - galaxy.model.corals.Sample.table.c.experiment_id, - galaxy.model.corals.Sample.table.c.colony_id, - galaxy.model.corals.Sample.table.c.colony_location, - galaxy.model.corals.Sample.table.c.taxonomy_id, - galaxy.model.corals.Sample.table.c.collector_id, - galaxy.model.corals.Sample.table.c.collection_date, - galaxy.model.corals.Sample.table.c.user_specimen_id, - galaxy.model.corals.Sample.table.c.registry_id, - galaxy.model.corals.Sample.table.c.depth, - galaxy.model.corals.Sample.table.c.dna_extraction_method, - galaxy.model.corals.Sample.table.c.dna_concentration, - galaxy.model.corals.Sample.table.c.public_after_date, - galaxy.model.corals.Sample.table.c.percent_missing_data_coral, - galaxy.model.corals.Sample.table.c.percent_acerv_coral, - galaxy.model.corals.Sample.table.c.percent_apalm_coral, - galaxy.model.corals.Sample.table.c.percent_heterozygous_coral, - galaxy.model.corals.Sample.table.c.field_call, - galaxy.model.corals.Sample.table.c.bcoral_genet_id), - whereclause=galaxy.model.corals.Sample.table.c.phenotype_id == phenotype_id, - from_obj=[galaxy.model.corals.Sample.table], - order_by=[galaxy.model.corals.Sample.table.c.id]) + q = ( + sa.select( + corals.Sample.id, + corals.Sample.affy_id, + corals.Sample.sample_id, + corals.Sample.allele_id, + corals.Sample.genotype_id, + corals.Sample.experiment_id, + corals.Sample.colony_id, + corals.Sample.colony_location, + corals.Sample.taxonomy_id, + corals.Sample.collector_id, + corals.Sample.collection_date, + corals.Sample.user_specimen_id, + corals.Sample.registry_id, + corals.Sample.depth, + corals.Sample.dna_extraction_method, + corals.Sample.dna_concentration, + corals.Sample.public_after_date, + corals.Sample.percent_missing_data_coral, + corals.Sample.percent_acerv_coral, + corals.Sample.percent_apalm_coral, + corals.Sample.percent_heterozygous_coral, + corals.Sample.field_call, + corals.Sample.bcoral_genet_id + ) + .select_from(corals.Sample.table) + .where(corals.Sample.table.c.phenotype_id == phenotype_id) + .order_by(corals.Sample.table.c.id) + ) samples = [] - for row in q.execute(): + for row in trans.sa_session.execute(q): try: collection_date = row.collection_date.strftime("%Y-%m-%d") except Exception: diff --git a/galaxy/coralsnp_reports/lib/galaxy/webapps/coralsnp_reports/controllers/taxonomies.py b/galaxy/coralsnp_reports/lib/galaxy/webapps/coralsnp_reports/controllers/taxonomies.py index 3d17484a..f95f9a75 100644 --- a/galaxy/coralsnp_reports/lib/galaxy/webapps/coralsnp_reports/controllers/taxonomies.py +++ b/galaxy/coralsnp_reports/lib/galaxy/webapps/coralsnp_reports/controllers/taxonomies.py @@ -3,11 +3,13 @@ import sqlalchemy as sa from markupsafe import escape -import galaxy.model +from galaxy.model import corals from galaxy import util -from . import BaseUIController -from galaxy.web.base.controller import web -from galaxy.webapps.reports.controllers.query import ReportQueryBuilder +from galaxy.webapps.base.controller import ( + BaseUIController, + web, +) +from galaxy.webapps.coralsnp_reports.controllers.query import ReportQueryBuilder log = logging.getLogger(__name__) @@ -17,15 +19,9 @@ class Taxonomies(BaseUIController, ReportQueryBuilder): @web.expose def all(self, trans, **kwd): message = escape(util.restore_text(kwd.get('message', ''))) - q = sa.select((galaxy.model.corals.Taxonomy.table.c.id, - galaxy.model.corals.Taxonomy.table.c.species_name, - galaxy.model.corals.Taxonomy.table.c.genus_name), - from_obj=[galaxy.model.corals.Taxonomy.table], - order_by=[galaxy.model.corals.Taxonomy.table.c.id]) taxonomies = [] - for row in q.execute(): - cols_tup = (row.id, row.species_name, row.genus_name) - taxonomies.append(cols_tup) + for row in trans.sa_session.query(corals.Taxonomy): + taxonomies.append((row.id, row.species_name, row.genus_name)) return trans.fill_template('/webapps/coralsnp_reports/taxonomies.mako', taxonomies=taxonomies, message=message) @web.expose @@ -33,15 +29,18 @@ def of_sample(self, trans, **kwd): message = escape(util.restore_text(kwd.get('message', ''))) affy_id = kwd.get('affy_id') taxonomy_id = kwd.get('taxonomy_id') - q = sa.select((galaxy.model.corals.Taxonomy.table.c.species_name, - galaxy.model.corals.Taxonomy.table.c.genus_name), - whereclause=sa.and_(galaxy.model.corals.Taxonomy.table.c.id == taxonomy_id), - from_obj=[galaxy.model.corals.Taxonomy.table], - order_by=[galaxy.model.corals.Taxonomy.table.c.id]) + q = ( + sa.select( + corals.Taxonomy.species_name, + corals.Taxonomy.genus_name + ) + .select_from(corals.Taxonomy.table) + .where(corals.Taxonomy.table.c.id == taxonomy_id) + .order_by(corals.Taxonomy.table.c.id) + ) taxonomies = [] - for row in q.execute(): - cols_tup = (row.species_name, row.genus_name) - taxonomies.append(cols_tup) + for row in trans.sa_session.execute(q): + taxonomies.append((row.species_name, row.genus_name)) return trans.fill_template('/webapps/coralsnp_reports/taxonomy_of_sample.mako', affy_id=affy_id, taxonomies=taxonomies, diff --git a/galaxy/coralsnp_reports/lib/galaxy/webapps/coralsnp_reports/fast_app.py b/galaxy/coralsnp_reports/lib/galaxy/webapps/coralsnp_reports/fast_app.py new file mode 100644 index 00000000..f743a263 --- /dev/null +++ b/galaxy/coralsnp_reports/lib/galaxy/webapps/coralsnp_reports/fast_app.py @@ -0,0 +1,25 @@ +from a2wsgi import WSGIMiddleware +from fastapi import FastAPI + +from galaxy.webapps.base.api import ( + add_exception_handler, + add_request_id_middleware, + include_all_package_routers, +) + + +def initialize_fast_app(gx_webapp): + app = FastAPI( + title="Galaxy CoralSNP Reports API", + description=( + "This API will give you insights into the Galaxy instance's usage and load. " + "It aims to provide data about alleles, collectors, colonies, experiments, and much more." + ), + docs_url="/api/docs", + ) + add_exception_handler(app) + add_request_id_middleware(app) + include_all_package_routers(app, "galaxy.webapps.coralsnp_reports.api") + wsgi_handler = WSGIMiddleware(gx_webapp) + app.mount("/", wsgi_handler) + return app diff --git a/galaxy/coralsnp_reports/lib/galaxy/webapps/coralsnp_reports/fast_factory.py b/galaxy/coralsnp_reports/lib/galaxy/webapps/coralsnp_reports/fast_factory.py new file mode 100644 index 00000000..d42b9987 --- /dev/null +++ b/galaxy/coralsnp_reports/lib/galaxy/webapps/coralsnp_reports/fast_factory.py @@ -0,0 +1,56 @@ +"""Module containing factory class for building uvicorn app for the Galaxy CoralSNP Reports. + +Information on uvicorn, its various settings, and how to invoke it can +be found at https://www.uvicorn.org/. + +The Galaxy CoralSNP Reports can be launched with uvicorn using the following invocation: + +:: + + uvicorn --app-dir lib --factory galaxy.webapps.coralsnp_reports.fast_factory:factory + +Use the environment variable ``GALAXY_CORALSNP_REPORTS_CONFIG`` to specify a Galaxy CoralSNP Reports +configuration file. + +:: + + GALAXY_CORALSNP_REPORTS_CONFIG=config/coralsnp_reports.yml uvicorn --app-dir lib --factory galaxy.webapps.coralsnp_reports.fast_factory:factory + +.. note:: + + Information on additional ways to configure uvicorn can be found at + https://www.uvicorn.org/. + + +`Gunicorn `__ is a server with +more complex management options. + +This factory function can be executed as a uvicorn worker managed with gunicorn +with the following command-line. + +:: + + gunicorn 'galaxy.webapps.coralsnp_reports.fast_factory:factory()' --env GALAXY_CORALSNP_REPORTS_CONFIG=config/coralsnp_reports.yml --pythonpath lib -w 4 -k uvicorn.workers.UvicornWorker + +""" + +from galaxy.main_config import ( + WebappConfigResolver, + WebappSetupProps, +) +from galaxy.webapps.coralsnp_reports.buildapp import app_factory +from .fast_app import initialize_fast_app + + +def factory(): + props = WebappSetupProps( + app_name="coralsnp_reports", + default_section_name="coralsnp_reports", + env_config_file="GALAXY_CORALSNP_REPORTS_CONFIG", + ) + config_provider = WebappConfigResolver(props) + config = config_provider.resolve_config() + gx_webapp = app_factory( + global_conf=config.global_conf, load_app_kwds=config.load_app_kwds, wsgi_preflight=config.wsgi_preflight + ) + return initialize_fast_app(gx_webapp)