From 2f9be8aec83686f16c8bbfa7ebd1c2f4d29a08cd Mon Sep 17 00:00:00 2001 From: mgcam Date: Wed, 12 Jun 2024 16:59:42 +0100 Subject: [PATCH 1/2] Added a check for unlinked data. --- lang_qc/db/helper/wells.py | 27 ++++++++++++++++++--------- tests/test_pac_bio_qc_data_well.py | 13 +++++++++++++ 2 files changed, 31 insertions(+), 9 deletions(-) diff --git a/lang_qc/db/helper/wells.py b/lang_qc/db/helper/wells.py index 69fac7e..57d7b01 100644 --- a/lang_qc/db/helper/wells.py +++ b/lang_qc/db/helper/wells.py @@ -85,7 +85,15 @@ def get_metrics_by_well_product_id( ) -> QCPoolMetrics | None: well = self.get_mlwh_well_by_product_id(id_product) if well and well.demultiplex_mode and "Instrument" in well.demultiplex_mode: + product_metrics = well.pac_bio_product_metrics + lib_lims_data = [ + row + for row in map(lambda product: product.pac_bio_run, product_metrics) + if row is not None + ] + if len(lib_lims_data) != len(product_metrics): + raise Exception("Partially linked LIMS data or no linked LIMS data") cov: float | None if any(p.hifi_num_reads is None for p in product_metrics): @@ -94,13 +102,13 @@ def get_metrics_by_well_product_id( hifi_reads = [prod.hifi_num_reads for prod in product_metrics] cov = stdev(hifi_reads) / mean(hifi_reads) * 100 - return QCPoolMetrics( - pool_coeff_of_variance=cov, - products=[ + sample_stats = [] + for (i, prod) in enumerate(product_metrics): + sample_stats.append( SampleDeplexingStats( id_product=prod.id_pac_bio_product, - tag1_name=prod.pac_bio_run.tag_identifier, - tag2_name=prod.pac_bio_run.tag2_identifier, + tag1_name=lib_lims_data[i].tag_identifier, + tag2_name=lib_lims_data[i].tag2_identifier, deplexing_barcode=prod.barcode4deplexing, hifi_read_bases=prod.hifi_read_bases, hifi_num_reads=prod.hifi_num_reads, @@ -108,13 +116,14 @@ def get_metrics_by_well_product_id( hifi_bases_percent=prod.hifi_bases_percent, percentage_total_reads=( prod.hifi_num_reads / well.hifi_num_reads * 100 - if well.hifi_num_reads + if (well.hifi_num_reads and prod.hifi_num_reads) else None ), ) - for prod in product_metrics - ], - ) + ) + + return QCPoolMetrics(pool_coeff_of_variance=cov, products=sample_stats) + return None def recent_completed_wells(self) -> List[PacBioRunWellMetrics]: diff --git a/tests/test_pac_bio_qc_data_well.py b/tests/test_pac_bio_qc_data_well.py index bd4318a..3be9de9 100644 --- a/tests/test_pac_bio_qc_data_well.py +++ b/tests/test_pac_bio_qc_data_well.py @@ -1,3 +1,4 @@ +import pytest from npg_id_generation.pac_bio import PacBioEntity from lang_qc.db.helper.wells import WellWh @@ -134,3 +135,15 @@ def test_pool_metrics_from_well(mlwhdb_test_session, multiplexed_run): assert ( int(metrics.products[1].percentage_total_reads) == 66 ), "20 of 30 reads is 66.6%" + + +def test_pool_metrics_from_well(mlwhdb_test_session): + + id = PacBioEntity( + run_name="TRACTION-RUN-1140", well_label="C1", plate_number=2 + ).hash_product_id() + helper = WellWh(session=mlwhdb_test_session) + with pytest.raises( + Exception, match=r"Partially linked LIMS data or no linked LIMS data" + ): + helper.get_metrics_by_well_product_id(id) From 372fc567c64a4f70806079291b8e48d0390afa60 Mon Sep 17 00:00:00 2001 From: mgcam Date: Thu, 13 Jun 2024 12:09:32 +0100 Subject: [PATCH 2/2] Simplified getting linked lims data. --- lang_qc/db/helper/wells.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lang_qc/db/helper/wells.py b/lang_qc/db/helper/wells.py index 57d7b01..4c8f089 100644 --- a/lang_qc/db/helper/wells.py +++ b/lang_qc/db/helper/wells.py @@ -88,9 +88,9 @@ def get_metrics_by_well_product_id( product_metrics = well.pac_bio_product_metrics lib_lims_data = [ - row - for row in map(lambda product: product.pac_bio_run, product_metrics) - if row is not None + product.pac_bio_run + for product in product_metrics + if product.pac_bio_run is not None ] if len(lib_lims_data) != len(product_metrics): raise Exception("Partially linked LIMS data or no linked LIMS data")