diff --git a/lang_qc/db/helper/wells.py b/lang_qc/db/helper/wells.py index 69fac7e..4c8f089 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 = [ + 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") 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)