Skip to content

Commit

Permalink
Improve component query
Browse files Browse the repository at this point in the history
  • Loading branch information
kjsanger committed Aug 15, 2023
1 parent 52041d3 commit 93c829b
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 7 deletions.
17 changes: 11 additions & 6 deletions src/npg_irods/ont.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,8 @@
from sqlalchemy.orm import Session
from structlog import get_logger

from npg_irods.common import update_metadata, update_permissions, infer_zone
from npg_irods.common import infer_zone, update_metadata, update_permissions
from npg_irods.db.mlwh import OseqFlowcell, Sample, Study
from npg_irods.metadata import ont
from npg_irods.metadata.common import SeqConcept
from npg_irods.metadata.lims import (
ensure_consent_withdrawn,
Expand Down Expand Up @@ -344,7 +343,7 @@ def find_components_changed(
if include_tags:
columns.append(OseqFlowcell.tag_identifier)

for cols in (
query = (
sess.query(*columns)
.distinct()
.join(OseqFlowcell.sample)
Expand All @@ -354,9 +353,15 @@ def find_components_changed(
| (Study.recorded_at >= since)
| (OseqFlowcell.recorded_at >= since)
)
.group_by(OseqFlowcell.experiment_name, OseqFlowcell.instrument_slot)
.order_by(asc(OseqFlowcell.experiment_name), asc(OseqFlowcell.instrument_slot))
):
.group_by(*columns)
)

order = [asc(OseqFlowcell.experiment_name), asc(OseqFlowcell.instrument_slot)]
if include_tags:
order.append(asc(OseqFlowcell.tag_identifier))
query = query.order_by(*order)

for cols in sess.execute(query):
yield Component(*cols)


Expand Down
33 changes: 32 additions & 1 deletion tests/test_ml_warehouse_queries.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

from pytest import mark as m

from conftest import BEGIN, EARLY, LATE, LATEST
from conftest import BEGIN, EARLY, LATE, LATEST, ont_tag_identifier
from npg_irods.metadata import illumina
from npg_irods.metadata.lims import TrackedSample, TrackedStudy
from npg_irods.ont import Component, find_components_changed, find_recent_expt
Expand Down Expand Up @@ -128,6 +128,37 @@ def test_find_recent_component(self, ont_synthetic_mlwh):
)
] == []

@m.describe(
"Finding updated experiments, positions and tag identifiers by datetime"
)
@m.context("When a query date is provided")
@m.it("Finds the correct experiment, slot, tag identifier tuples")
def test_find_recent_component_tag(self, ont_synthetic_mlwh):
before_latest = LATEST - timedelta(days=1)
odd_positions = []

# Odd slot multiplexed experiments were done at LATEST time
for expt_name in ["multiplexed_experiment_001", "multiplexed_experiment_003"]:
for slot in [1, 3, 5]:
# Tag identifiers NB01 - NB12
for tag_id in [ont_tag_identifier(i + 1) for i in range(12)]:
odd_positions.append(Component(expt_name, slot, tag_id))

assert [
c
for c in find_components_changed(
ont_synthetic_mlwh, before_latest, include_tags=True
)
] == odd_positions

after_latest = LATEST + timedelta(days=1)
assert [
c
for c in find_components_changed(
ont_synthetic_mlwh, after_latest, include_tags=True
)
] == []


@m.describe("Finding Illumina recently changed information in Illumina tables")
class TestIlluminaMLWarehouseQueries(object):
Expand Down

0 comments on commit 93c829b

Please sign in to comment.