From 3a3102d58e97bfbabbd40a51e1611e3e0abf706e Mon Sep 17 00:00:00 2001 From: Chiara Rasi Date: Mon, 20 May 2024 11:16:14 +0200 Subject: [PATCH 01/11] Some fixes --- CHANGELOG.md | 4 +++ .../server/blueprints/index/views.py | 4 +-- .../server/blueprints/report/utils.py | 14 ++++---- chanjo_report/server/extensions.py | 32 +------------------ requirements.txt | 3 +- 5 files changed, 15 insertions(+), 42 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4341945..fd94981 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ # Change log +## 4.11 +### 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/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/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..ca98fed 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,6 +1,5 @@ -alchy cairocffi -chanjo>=4.1.0 +chanjo @ git+https://github.com/Clinical-Genomics/chanjo@deprecate_alchy cffi Flask Flask-Assets From 3fdd3dc3890d9e6c9f0e9df04653d17ad8d2008d Mon Sep 17 00:00:00 2001 From: Chiara Rasi Date: Mon, 20 May 2024 13:47:07 +0200 Subject: [PATCH 02/11] It works, finally --- chanjo_report/server/blueprints/report/controllers.py | 3 +++ chanjo_report/server/blueprints/report/utils.py | 1 + 2 files changed, 4 insertions(+) diff --git a/chanjo_report/server/blueprints/report/controllers.py b/chanjo_report/server/blueprints/report/controllers.py index e870cbd..b1e70d5 100644 --- a/chanjo_report/server/blueprints/report/controllers.py +++ b/chanjo_report/server/blueprints/report/controllers.py @@ -1,3 +1,5 @@ +import logging + from chanjo.store.models import Sample from flask import session @@ -5,6 +7,7 @@ from .utils import keymetrics_rows, samplesex_rows, transcripts_rows +LOG = logging.getLogger(__name__) def report_contents(request): """Check args or form provided by user request and prepare contents for report or pdf endpoints diff --git a/chanjo_report/server/blueprints/report/utils.py b/chanjo_report/server/blueprints/report/utils.py index 5b07c8d..33dbe51 100644 --- a/chanjo_report/server/blueprints/report/utils.py +++ b/chanjo_report/server/blueprints/report/utils.py @@ -98,6 +98,7 @@ def keymetrics_rows(samples_ids, genes=None): query = ( api.session.query( TranscriptStat, + TranscriptStat.sample, func.avg(TranscriptStat.mean_coverage).label('mean_coverage'), func.avg(TranscriptStat.completeness_10).label('completeness_10'), func.avg(TranscriptStat.completeness_15).label('completeness_15'), From 4b3e78b33cb655ed5c8c749a5ebd335fb7b60577 Mon Sep 17 00:00:00 2001 From: Chiara Rasi Date: Mon, 20 May 2024 13:48:45 +0200 Subject: [PATCH 03/11] Remove unused lines --- chanjo_report/server/blueprints/report/controllers.py | 4 ---- 1 file changed, 4 deletions(-) diff --git a/chanjo_report/server/blueprints/report/controllers.py b/chanjo_report/server/blueprints/report/controllers.py index b1e70d5..a28feb4 100644 --- a/chanjo_report/server/blueprints/report/controllers.py +++ b/chanjo_report/server/blueprints/report/controllers.py @@ -1,5 +1,3 @@ -import logging - from chanjo.store.models import Sample from flask import session @@ -7,8 +5,6 @@ from .utils import keymetrics_rows, samplesex_rows, transcripts_rows -LOG = logging.getLogger(__name__) - def report_contents(request): """Check args or form provided by user request and prepare contents for report or pdf endpoints Args: From 8fafe6e6f121cb04023083dbf87c7f73076810e6 Mon Sep 17 00:00:00 2001 From: Chiara Rasi Date: Mon, 20 May 2024 13:49:48 +0200 Subject: [PATCH 04/11] Don't assign version --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index fd94981..9900ad1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,6 @@ # Change log -## 4.11 +## [unreleased] ### Changed - Removed alchy dependency From bb801f95bd1f4e1b61dcf6878e204a276437253f Mon Sep 17 00:00:00 2001 From: Chiara Rasi Date: Mon, 20 May 2024 14:08:44 +0200 Subject: [PATCH 05/11] Removed an unused lib and one that gets installed by chanjo anyway --- requirements.txt | 2 -- 1 file changed, 2 deletions(-) diff --git a/requirements.txt b/requirements.txt index ca98fed..9dea7ee 100644 --- a/requirements.txt +++ b/requirements.txt @@ -11,8 +11,6 @@ path.py pymysql pyscss six -sqlservice -Flask-SQLAlchemy tabulate toml toolz From b7b08b754d59c01d7d83eedf9a50b79739704de8 Mon Sep 17 00:00:00 2001 From: Chiara Rasi Date: Mon, 20 May 2024 14:33:50 +0200 Subject: [PATCH 06/11] Revert code --- chanjo_report/server/blueprints/report/utils.py | 1 - 1 file changed, 1 deletion(-) diff --git a/chanjo_report/server/blueprints/report/utils.py b/chanjo_report/server/blueprints/report/utils.py index 33dbe51..5b07c8d 100644 --- a/chanjo_report/server/blueprints/report/utils.py +++ b/chanjo_report/server/blueprints/report/utils.py @@ -98,7 +98,6 @@ def keymetrics_rows(samples_ids, genes=None): query = ( api.session.query( TranscriptStat, - TranscriptStat.sample, func.avg(TranscriptStat.mean_coverage).label('mean_coverage'), func.avg(TranscriptStat.completeness_10).label('completeness_10'), func.avg(TranscriptStat.completeness_15).label('completeness_15'), From b38b91616ca2a1a43cf485bdcbb7531c18938292 Mon Sep 17 00:00:00 2001 From: Chiara Rasi Date: Mon, 20 May 2024 15:58:07 +0200 Subject: [PATCH 07/11] Flask-SQLAlchemy is required after all --- requirements.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/requirements.txt b/requirements.txt index 9dea7ee..aca2e3c 100644 --- a/requirements.txt +++ b/requirements.txt @@ -11,6 +11,7 @@ path.py pymysql pyscss six +Flask-SQLAlchemy tabulate toml toolz From 3428c11b106ed7fe8900a3b0b8a842d753097c5f Mon Sep 17 00:00:00 2001 From: Chiara Rasi Date: Tue, 21 May 2024 10:03:16 +0200 Subject: [PATCH 08/11] Attempt at fixing overview report --- chanjo_report/server/blueprints/report/views.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/chanjo_report/server/blueprints/report/views.py b/chanjo_report/server/blueprints/report/views.py index a42ae41..b249fb0 100644 --- a/chanjo_report/server/blueprints/report/views.py +++ b/chanjo_report/server/blueprints/report/views.py @@ -49,7 +49,7 @@ 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)) From 3d20236e5f576b6e56aa9c16a36e9c68a491dfd8 Mon Sep 17 00:00:00 2001 From: Chiara Rasi Date: Tue, 21 May 2024 10:51:22 +0200 Subject: [PATCH 09/11] Fix error --- chanjo_report/server/blueprints/report/views.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/chanjo_report/server/blueprints/report/views.py b/chanjo_report/server/blueprints/report/views.py index b249fb0..ef6320c 100644 --- a/chanjo_report/server/blueprints/report/views.py +++ b/chanjo_report/server/blueprints/report/views.py @@ -54,7 +54,7 @@ def genes(): 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) From 8a5ae0f7805e20063fdc27887c22310951a4ea6c Mon Sep 17 00:00:00 2001 From: Chiara Rasi Date: Tue, 21 May 2024 11:07:16 +0200 Subject: [PATCH 10/11] Fix gene report --- chanjo_report/server/blueprints/report/views.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/chanjo_report/server/blueprints/report/views.py b/chanjo_report/server/blueprints/report/views.py index ef6320c..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 From 9c3145eda3315013dedff80cf899de9400d44da7 Mon Sep 17 00:00:00 2001 From: Chiara Rasi Date: Wed, 22 May 2024 13:26:15 +0200 Subject: [PATCH 11/11] Fix chanjo version in requirements --- requirements.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/requirements.txt b/requirements.txt index aca2e3c..660e302 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,5 +1,5 @@ cairocffi -chanjo @ git+https://github.com/Clinical-Genomics/chanjo@deprecate_alchy +chanjo>=4.7 cffi Flask Flask-Assets