diff --git a/CHANGELOG.md b/CHANGELOG.md index 4341945..9900ad1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ # Change log +## [unreleased] +### Changed +- Removed alchy dependency + ## 4.10.2 (2023-08-16) ### Fixed - Timeout when creating report due to slow transcript_stat query diff --git a/chanjo_report/server/blueprints/index/views.py b/chanjo_report/server/blueprints/index/views.py index 866a635..c8e679d 100644 --- a/chanjo_report/server/blueprints/index/views.py +++ b/chanjo_report/server/blueprints/index/views.py @@ -13,7 +13,7 @@ @index_bp.route('/') def index(): - sample_objs = api.query(Sample).limit(20) - tx_models = api.query(Transcript).distinct(Transcript.gene_id).limit(20) + sample_objs = api.session.query(Sample).limit(20) + tx_models = api.session.query(Transcript).distinct(Transcript.gene_id).limit(20) return render_template('index/index.html', samples=sample_objs, transcripts=tx_models) diff --git a/chanjo_report/server/blueprints/report/controllers.py b/chanjo_report/server/blueprints/report/controllers.py index e870cbd..a28feb4 100644 --- a/chanjo_report/server/blueprints/report/controllers.py +++ b/chanjo_report/server/blueprints/report/controllers.py @@ -5,7 +5,6 @@ from .utils import keymetrics_rows, samplesex_rows, transcripts_rows - def report_contents(request): """Check args or form provided by user request and prepare contents for report or pdf endpoints Args: diff --git a/chanjo_report/server/blueprints/report/utils.py b/chanjo_report/server/blueprints/report/utils.py index 4a56d34..5b07c8d 100644 --- a/chanjo_report/server/blueprints/report/utils.py +++ b/chanjo_report/server/blueprints/report/utils.py @@ -17,7 +17,7 @@ def chromosome_coverage(samples_ids, chrom=None): """Return mean coverage over all transcripts of a chromosome for one or more samples""" query = ( - api.query( + api.session.query( TranscriptStat, func.avg(TranscriptStat.mean_coverage).label('mean_coverage'), ) @@ -32,7 +32,7 @@ def chromosome_coverage(samples_ids, chrom=None): def transcript_coverage(api, gene_id, *sample_ids): """Return coverage metrics per transcript for a given gene.""" - query = (api.query(TranscriptStat) + query = (api.session.query(TranscriptStat) .join(TranscriptStat.transcript) .filter(Transcript.gene_id == gene_id) .order_by(TranscriptStat.transcript_id, @@ -62,7 +62,7 @@ def map_samples(group_id=None, sample_ids=None): def samplesex_rows(sample_ids): """Generate sex prediction info rows.""" - sex_query = (api.query( + sex_query = (api.session.query( TranscriptStat.sample_id, Transcript.chromosome, func.avg(TranscriptStat.mean_coverage) @@ -96,7 +96,7 @@ def samplesex_rows(sample_ids): def keymetrics_rows(samples_ids, genes=None): """Generate key metrics rows.""" query = ( - api.query( + api.session.query( TranscriptStat, func.avg(TranscriptStat.mean_coverage).label('mean_coverage'), func.avg(TranscriptStat.completeness_10).label('completeness_10'), @@ -156,8 +156,8 @@ def diagnostic_yield(api, genes=None, samples=None, group=None, level=10): str_level = "completeness_{}".format(level) completeness_col = getattr(TranscriptStat, str_level) - all_tx = api.query(Transcript) - missed_tx = (api.query(TranscriptStat) + all_tx = api.session.query(Transcript) + missed_tx = (api.session.query(TranscriptStat) .filter(completeness_col < threshold) .order_by(TranscriptStat.sample_id)) @@ -166,7 +166,7 @@ def diagnostic_yield(api, genes=None, samples=None, group=None, level=10): .filter(Transcript.gene_id.in_(genes))) all_tx = all_tx.filter(Transcript.gene_id.in_(genes)) - samples_query = api.query(Sample.id) + samples_query = api.session.query(Sample.id) if samples: samples_query = samples_query.filter(Sample.id.in_(samples)) missed_tx = missed_tx.filter(TranscriptStat.sample_id.in_(samples)) diff --git a/chanjo_report/server/blueprints/report/views.py b/chanjo_report/server/blueprints/report/views.py index a42ae41..7f691db 100644 --- a/chanjo_report/server/blueprints/report/views.py +++ b/chanjo_report/server/blueprints/report/views.py @@ -26,7 +26,7 @@ def gene(gene_id): """Display coverage information on a gene.""" sample_ids = request.args.getlist("sample_id") sample_dict = map_samples(sample_ids=sample_ids) - matching_tx = Transcript.filter_by(gene_id=gene_id).first() + matching_tx = api.session.query(Transcript).filter_by(gene_id=gene_id).first() if matching_tx is None: return abort(404, "gene not found: {}".format(gene_id)) gene_name = matching_tx.gene_name @@ -49,12 +49,12 @@ def genes(): limit = int(request.args.get("limit", 30)) exonlink = request.args.get("exonlink") sample_ids = request.args.getlist("sample_id") - samples_q = Sample.filter(Sample.id.in_(sample_ids)) + samples_q = api.session.query(Sample).filter(Sample.id.in_(sample_ids)) level = request.args.get("level", 10) raw_gene_ids = request.args.get("gene_id") or request.form.get("gene_ids") completeness_col = getattr(TranscriptStat, "completeness_{}".format(level)) query = ( - api.query(TranscriptStat) + api.session.query(TranscriptStat) .join(TranscriptStat.transcript) .filter(completeness_col < 100) .order_by(completeness_col) diff --git a/chanjo_report/server/extensions.py b/chanjo_report/server/extensions.py index f0b5128..ea9d4f0 100644 --- a/chanjo_report/server/extensions.py +++ b/chanjo_report/server/extensions.py @@ -1,34 +1,4 @@ -from alchy import make_declarative_base, QueryModel, ManagerMixin from chanjo.store.models import BASE from flask_sqlalchemy import SQLAlchemy -from threading import get_ident - -class Alchy(SQLAlchemy, ManagerMixin): - """Flask extension that integrates alchy with Flask-SQLAlchemy. - Originally developed in Flask-Alchy (https://github.com/dgilland/flask-alchy). - It has some changes here due to deprecated code in required libs (flask and flask_sqlalchemy) - """ - - def __init__( - self, app=None, use_native_unicode=True, session_options=None, Model=None, metadata=None - ): - if session_options is None: - session_options = {} - - session_options.setdefault("query_cls", QueryModel) - session_options.setdefault("scopefunc", get_ident) - - self.Model = Model - self.make_declarative_base(Model) - - super(Alchy, self).__init__(app, use_native_unicode, session_options, metadata=metadata) - - self.Query = session_options["query_cls"] - - def __getattr__(self, attr): - """Delegate all other attributes to self.session""" - return getattr(self.session, attr) - - -api = Alchy(Model=BASE) +api = SQLAlchemy(model_class=BASE) diff --git a/requirements.txt b/requirements.txt index 4734f6a..660e302 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,6 +1,5 @@ -alchy cairocffi -chanjo>=4.1.0 +chanjo>=4.7 cffi Flask Flask-Assets @@ -12,7 +11,6 @@ path.py pymysql pyscss six -sqlservice Flask-SQLAlchemy tabulate toml