Skip to content

Commit

Permalink
Add searchable and sortable flow cell column
Browse files Browse the repository at this point in the history
  • Loading branch information
diitaz93 committed May 23, 2024
1 parent 8b84381 commit 783f402
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 3 deletions.
29 changes: 27 additions & 2 deletions cg/server/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@
from flask_admin.contrib.sqla import ModelView
from flask_dance.contrib.google import google
from markupsafe import Markup
from sqlalchemy.orm import aliased

from cg.constants.constants import NG_UL_SUFFIX, CaseActions, DataDelivery, Workflow
from cg.server.ext import db
from cg.store.models import Sample
from cg.store.models import RunDevice, Sample
from cg.utils.flask.enum import SelectEnumField


Expand Down Expand Up @@ -459,11 +460,35 @@ class IlluminaFlowCellView(BaseView):
"has_backup",
)
column_formatters = {
"flow_cell": view_flow_cell_internal_id,
"flow_cell": lambda v, c, m, p: m.flow_cell,
}
column_default_sort = ("sequenced_at", True)
column_filters = ["sequencer_type", "sequencer_name", "data_availability"]
column_editable_list = ["data_availability"]
column_searchable_list = ["flow_cell", "sequencer_type", "sequencer_name"]
column_sortable_list = ["flow_cell", "sequenced_at", "sequencer_type", "sequencer_name"]

def scaffold_sortable_columns(self):
columns = super(IlluminaFlowCellView, self).scaffold_sortable_columns()
columns["flow_cell"] = "flow_cell"
return columns

def scaffold_search(self, search_term):
if not search_term:
return None

# Generate the base query
query = super(IlluminaFlowCellView, self).scaffold_search(search_term)

# Add a search for the hybrid property 'flow_cell'
device_alias = aliased(RunDevice)
flow_cell_search = (
self.session.query(self.model)
.join(device_alias, self.model.device)
.filter(device_alias.internal_id.ilike(f"%{search_term}%"))
)

return query.union(flow_cell_search)


class OrganismView(BaseView):
Expand Down
17 changes: 16 additions & 1 deletion cg/store/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,12 @@
Numeric,
String,
Table,
select,
)
from sqlalchemy import Text as SLQText
from sqlalchemy import UniqueConstraint, orm, types
from sqlalchemy.orm import DeclarativeBase, Mapped, mapped_column
from sqlalchemy.ext.hybrid import hybrid_property
from sqlalchemy.orm import DeclarativeBase, Mapped, aliased, mapped_column
from sqlalchemy.orm.attributes import InstrumentedAttribute

from cg.constants import DataDelivery, FlowCellStatus, Priority, Workflow
Expand Down Expand Up @@ -1049,6 +1051,19 @@ class IlluminaSequencingRun(InstrumentRun):

__mapper_args__ = {"polymorphic_identity": DeviceType.ILLUMINA}

@hybrid_property
def flow_cell(self):
return self.device.internal_id if self.device else None

@flow_cell.expression
def flow_cell(cls):
device_alias = aliased(RunDevice)
return (
select(device_alias.internal_id)
.where(device_alias.id == cls.device_id)
.scalar_subquery()
)


class SampleRunMetrics(Base):
"""Parent model for the different types of sample run metrics."""
Expand Down

0 comments on commit 783f402

Please sign in to comment.