Skip to content

Commit

Permalink
Merge pull request #59 from Clinical-Genomics/remove_alchy
Browse files Browse the repository at this point in the history
Remove alchy and use SQLAlchemy instead
  • Loading branch information
northwestwitch authored May 22, 2024
2 parents 9ca7a8a + 9c3145e commit 2fcf0a0
Show file tree
Hide file tree
Showing 7 changed files with 18 additions and 47 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
4 changes: 2 additions & 2 deletions chanjo_report/server/blueprints/index/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
1 change: 0 additions & 1 deletion chanjo_report/server/blueprints/report/controllers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
14 changes: 7 additions & 7 deletions chanjo_report/server/blueprints/report/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'),
)
Expand All @@ -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,
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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'),
Expand Down Expand Up @@ -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))

Expand All @@ -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))
Expand Down
6 changes: 3 additions & 3 deletions chanjo_report/server/blueprints/report/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand Down
32 changes: 1 addition & 31 deletions chanjo_report/server/extensions.py
Original file line number Diff line number Diff line change
@@ -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)
4 changes: 1 addition & 3 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
alchy
cairocffi
chanjo>=4.1.0
chanjo>=4.7
cffi
Flask
Flask-Assets
Expand All @@ -12,7 +11,6 @@ path.py
pymysql
pyscss
six
sqlservice
Flask-SQLAlchemy
tabulate
toml
Expand Down

0 comments on commit 2fcf0a0

Please sign in to comment.