Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,12 @@ def _update_sample(
sequencing_date: datetime,
) -> None:
"""Update the reads and last sequenced date for the SMRT cell samples."""
for sample_dto in sample_run_metrics_dtos:
self.store.update_sample_reads(
internal_id=sample_dto.sample_internal_id, reads=sample_dto.hifi_reads
)
self.store.update_sample_sequenced_at(
internal_id=sample_dto.sample_internal_id, date=sequencing_date
)
sample_ids_to_update: set[str] = {
sample_dto.sample_internal_id for sample_dto in sample_run_metrics_dtos
}
for sample_id in sample_ids_to_update:
self.store.recalculate_sample_reads_pacbio(sample_id)
self.store.update_sample_sequenced_at(internal_id=sample_id, date=sequencing_date)

@handle_post_processing_errors(
to_except=(PostProcessingDataTransferError, ValueError),
Expand Down
2 changes: 1 addition & 1 deletion cg/store/crud/create.py
Original file line number Diff line number Diff line change
Expand Up @@ -552,7 +552,7 @@ def create_pac_bio_sample_sequencing_run(
) -> PacbioSampleSequencingMetrics:
sample_id: str = sample_run_metrics_dto.sample_internal_id
LOG.debug(f"Creating Pacbio sample sequencing metric for sample {sample_id}")
sample: Sample = self.get_sample_by_internal_id(sample_id)
sample: Sample = self.get_sample_by_internal_id_strict(sample_id)
if not sample:
self.rollback()
raise EntryNotFoundError(f"Sample not found: {sample_id}")
Expand Down
6 changes: 3 additions & 3 deletions cg/store/crud/update.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,10 @@ def update_sample_reads_illumina(self, internal_id: str, sequencer_type: Sequenc
sample.reads = total_reads_for_sample
self.commit_to_store()

def update_sample_reads(self, internal_id: str, reads: int):
def update_sample_reads_pacbio(self, internal_id: str, reads: int):
"""Add reads to the current reads for a sample."""
sample: Sample = self.get_sample_by_internal_id(internal_id)
sample.reads += reads
sample: Sample = self.get_sample_by_internal_id_strict(internal_id)
sample.reads = reads
self.commit_to_store()

def update_sample_sequenced_at(self, internal_id: str, date: datetime):
Expand Down
9 changes: 8 additions & 1 deletion cg/store/store.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,11 @@ class Store(
DeleteMixin,
UpdateMixin,
):
pass
def recalculate_sample_reads_pacbio(self, sample_id: str) -> None:
reads = sum(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess it is not obvious that it is an int

Suggested change
reads = sum(
reads: int = sum(

metric.hifi_reads
for metric in self.get_pacbio_sample_sequencing_metrics(
sample_id=sample_id, smrt_cell_ids=None
)
)
self.update_sample_reads_pacbio(internal_id=sample_id, reads=reads)
55 changes: 54 additions & 1 deletion tests/store/api/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,15 @@

from sqlalchemy.orm import Query

from cg.constants.devices import DeviceType
from cg.constants.subject import PhenotypeStatus
from cg.store.models import CaseSample
from cg.services.run_devices.pacbio.data_transfer_service.dto import (
PacBioSampleSequencingMetricsDTO,
PacBioSMRTCellDTO,
)
from cg.store.models import CaseSample, PacbioSequencingRun
from cg.store.store import Store
from tests.store_helpers import StoreHelpers


def test_get_latest_analyses_for_cases_query(
Expand Down Expand Up @@ -45,3 +51,50 @@ def test_get_latest_analyses_for_cases_query(
# THEN only the newest analysis should be returned
assert analysis_newest in analyses
assert analysis_oldest not in analyses


def test_update_pacbio_sample_reads(base_store: Store, helpers: StoreHelpers):

# GIVEN a sample in the database with some initial reads and corresponding sample sequencing metrics
initial_reads = 1000
sample_id = "sample_id"
helpers.add_sample(store=base_store, internal_id=sample_id, reads=initial_reads)
pacbio_smrt_cell = PacBioSMRTCellDTO(type=DeviceType.PACBIO, internal_id="EA123")
pacbio_smrt_cell = base_store.create_pac_bio_smrt_cell(pacbio_smrt_cell)
base_store.commit_to_store()
sequencing_run: PacbioSequencingRun = helpers.add_pacbio_sequencing_run(
store=base_store, id=1, device_id=pacbio_smrt_cell.id, run_name="run_name"
)
sample_run_metrics_dto = PacBioSampleSequencingMetricsDTO(
sample_internal_id=sample_id,
hifi_reads=initial_reads,
hifi_yield=1,
hifi_mean_read_length=1,
hifi_median_read_quality="good",
polymerase_mean_read_length=1,
)
base_store.create_pac_bio_sample_sequencing_run(
sample_run_metrics_dto=sample_run_metrics_dto, sequencing_run=sequencing_run
)
base_store.commit_to_store()

# GIVEN that there are additional sample sequencing metrics for the sample
new_reads = 2000
new_sample_run_metrics_dto = PacBioSampleSequencingMetricsDTO(
sample_internal_id=sample_id,
hifi_reads=new_reads,
hifi_yield=1,
hifi_mean_read_length=1,
hifi_median_read_quality="good",
polymerase_mean_read_length=1,
)
base_store.create_pac_bio_sample_sequencing_run(
sample_run_metrics_dto=new_sample_run_metrics_dto, sequencing_run=sequencing_run
)
base_store.commit_to_store()

# WHEN updating a samples reads
base_store.recalculate_sample_reads_pacbio("sample_id")

# THEN the sample should have updated the reads to the sum of the sample sequencing metrics
assert base_store.get_sample_by_internal_id_strict(sample_id).reads == initial_reads + new_reads
23 changes: 22 additions & 1 deletion tests/store/crud/update/test_update.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,13 +123,34 @@ def test_update_sample_reads_pacbio(
reads: int = 10000

# WHEN updating the reads for the sample
store.update_sample_reads(internal_id=pacbio_barcoded_sample_internal_id, reads=reads)
store.update_sample_reads_pacbio(internal_id=pacbio_barcoded_sample_internal_id, reads=reads)

# THEN the reads for the sample is updated
sample: Sample = store.get_sample_by_internal_id(pacbio_barcoded_sample_internal_id)
assert sample.reads == reads
Comment on lines 123 to 130
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suggest this test(test_update_sample_reads_pacbio) be removed. Seems to me that the new test (test_update_sample_reads_pacbio_not_incremented) covers all the functionality



def test_update_sample_reads_pacbio_not_incremented(
pacbio_barcoded_sample_internal_id: str,
store: Store,
helpers: StoreHelpers,
):
"""Tests that updating the reads for a PacBio sample does not increment the reads."""
# GIVEN a store with a PacBio sample with reads
sample: Sample = helpers.add_sample(store=store, internal_id=pacbio_barcoded_sample_internal_id)
sample.reads = 1
new_reads = 10000

# WHEN updating the reads for the sample
store.update_sample_reads_pacbio(
internal_id=pacbio_barcoded_sample_internal_id, reads=new_reads
)

# THEN the reads for the sample are updated
sample: Sample = store.get_sample_by_internal_id_strict(pacbio_barcoded_sample_internal_id)
assert sample.reads == new_reads


@pytest.mark.parametrize(
"sample_id_fixture",
["sample_id_sequenced_on_multiple_flow_cells", "pacbio_barcoded_sample_internal_id"],
Expand Down
Loading