diff --git a/src/npg_irods/ont.py b/src/npg_irods/ont.py index a3367305..0eb47bee 100644 --- a/src/npg_irods/ont.py +++ b/src/npg_irods/ont.py @@ -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, @@ -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) @@ -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) diff --git a/tests/test_ml_warehouse_queries.py b/tests/test_ml_warehouse_queries.py index e345e481..335af64a 100644 --- a/tests/test_ml_warehouse_queries.py +++ b/tests/test_ml_warehouse_queries.py @@ -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 @@ -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):