Skip to content

Commit 5ba8f9b

Browse files
committed
fix bug
1 parent 67405a1 commit 5ba8f9b

File tree

9 files changed

+259
-31
lines changed

9 files changed

+259
-31
lines changed

cg/services/deliver_files/file_fetcher/analysis_service.py

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,10 @@ def get_files_to_deliver(self, case_id: str, sample_id: str | None = None) -> De
4242
"""Return a list of analysis files to be delivered for a case."""
4343
LOG.debug(f"[FETCH SERVICE] Fetching analysis files for case: {case_id}")
4444
case: Case = self.status_db.get_case_by_internal_id(internal_id=case_id)
45-
analysis_case_files: list[CaseFile] = self._get_analysis_case_delivery_files(case)
45+
analysis_case_files: list[CaseFile] = self._get_analysis_case_delivery_files(
46+
case=case, sample_id=sample_id
47+
)
48+
4649
analysis_sample_files: list[SampleFile] = self._get_analysis_sample_delivery_files(
4750
case=case, sample_id=sample_id
4851
)
@@ -73,9 +76,11 @@ def _validate_delivery_has_content(delivery_files: DeliveryFiles) -> DeliveryFil
7376
@handle_missing_bundle_errors
7477
def _get_sample_files_from_case_bundle(
7578
self, workflow: Workflow, sample_id: str, case_id: str
76-
) -> list[SampleFile]:
79+
) -> list[SampleFile] | None:
7780
"""Return a list of files from a case bundle with a sample id as tag."""
7881
sample_tags: list[set[str]] = self.tags_fetcher.fetch_tags(workflow).sample_tags
82+
if not sample_tags:
83+
return []
7984
sample_tags_with_sample_id: list[set[str]] = [tag | {sample_id} for tag in sample_tags]
8085
sample_files: list[File] = self.hk_api.get_files_from_latest_version_containing_tags(
8186
bundle_name=case_id, tags=sample_tags_with_sample_id
@@ -105,13 +110,17 @@ def _get_analysis_sample_delivery_files(
105110
return delivery_files
106111

107112
@handle_missing_bundle_errors
108-
def _get_analysis_case_delivery_files(self, case: Case) -> list[CaseFile]:
113+
def _get_analysis_case_delivery_files(
114+
self, case: Case, sample_id: str | None
115+
) -> list[CaseFile] | None:
109116
"""
110117
Return a complete list of analysis case files to be delivered and ignore analysis sample
111118
files.
112119
"""
113120
case_tags: list[set[str]] = self.tags_fetcher.fetch_tags(case.data_analysis).case_tags
114-
sample_id_tags: list[str] = case.sample_ids
121+
if not case_tags:
122+
return []
123+
sample_id_tags: list[str] = [sample_id] if sample_id else case.sample_ids
115124
case_files: list[File] = self.hk_api.get_files_from_latest_version_containing_tags(
116125
bundle_name=case.internal_id, tags=case_tags, excluded_tags=sample_id_tags
117126
)

cg/services/deliver_files/tag_fetcher/fohm_upload_service.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,15 @@ def fetch_tags(self, workflow: Workflow) -> DeliveryFileTags:
1616
if workflow is RAW_DATA, return tags for fastq to fetch fastq files from the sample bundle.
1717
Required since some of the sample specific files are stored on the case bundle, but also fastq files.
1818
Not separating these would cause fetching of case bundle fastq files if present.
19+
20+
args:
21+
workflow: Workflow: The workflow to fetch tags
1922
"""
2023
self._validate_workflow(workflow=workflow)
2124
return (
2225
DeliveryFileTags(
2326
case_tags=None,
24-
sample_tags=[{"consensus-sample", "vcf-report"}],
27+
sample_tags=[{"consensus-sample"}, {"vcf-report"}],
2528
)
2629
if workflow == Workflow.MUTANT
2730
else DeliveryFileTags(
@@ -35,6 +38,8 @@ def _validate_workflow(workflow: Workflow):
3538
"""
3639
Validate the workflow.
3740
NOTE: workflow raw data here is required to fit the implementation of the raw data delivery file fetcher.
41+
args:
42+
workflow: Workflow: The workflow to validate.
3843
"""
3944
if workflow not in [Workflow.MUTANT, Workflow.RAW_DATA]:
4045
raise ValueError(f"Workflow {workflow} is not supported for FOHM upload file delivery.")

tests/fixture_plugins/delivery_fixtures/bundle_fixtures.py

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -109,8 +109,12 @@ def hk_delivery_case_bundle_fohm_upload(
109109
sample_id: str,
110110
another_sample_id: str,
111111
delivery_report_file: Path,
112-
delivery_cram_file: Path,
113-
delivery_another_cram_file: Path,
112+
delivery_case_fastq_file: Path,
113+
delivery_another_case_fastq_file: Path,
114+
delivery_consensus_sample_file: Path,
115+
delivery_another_consensus_sample_file: Path,
116+
delivery_vcf_report_file: Path,
117+
delivery_another_vcf_report_file: Path,
114118
) -> dict:
115119
case_hk_bundle: dict[str, Any] = deepcopy(case_hk_bundle_no_files)
116120
case_hk_bundle["name"] = case_id
@@ -122,12 +126,32 @@ def hk_delivery_case_bundle_fohm_upload(
122126
},
123127
{
124128
"archive": False,
125-
"path": delivery_cram_file.as_posix(),
129+
"path": delivery_case_fastq_file.as_posix(),
130+
"tags": ["fastq", sample_id],
131+
},
132+
{
133+
"archive": False,
134+
"path": delivery_another_case_fastq_file.as_posix(),
135+
"tags": ["fastq", another_sample_id],
136+
},
137+
{
138+
"archive": False,
139+
"path": delivery_consensus_sample_file.as_posix(),
126140
"tags": ["consensus-sample", sample_id],
127141
},
128142
{
129143
"archive": False,
130-
"path": delivery_another_cram_file.as_posix(),
144+
"path": delivery_another_consensus_sample_file.as_posix(),
145+
"tags": ["consensus-sample", another_sample_id],
146+
},
147+
{
148+
"archive": False,
149+
"path": delivery_vcf_report_file.as_posix(),
150+
"tags": ["vcf-report", sample_id],
151+
},
152+
{
153+
"archive": False,
154+
"path": delivery_another_vcf_report_file.as_posix(),
131155
"tags": ["vcf-report", another_sample_id],
132156
},
133157
]

tests/fixture_plugins/delivery_fixtures/context_fixtures.py

Lines changed: 83 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,33 @@ def delivery_housekeeper_api(
2121
hk_delivery_case_bundle: dict[str, Any],
2222
) -> HousekeeperAPI:
2323
"""Delivery API Housekeeper context."""
24+
hk_api: HousekeeperAPI = real_housekeeper_api
25+
helpers.ensure_hk_bundle(store=hk_api, bundle_data=hk_delivery_sample_bundle, include=True)
2426
helpers.ensure_hk_bundle(
25-
store=real_housekeeper_api, bundle_data=hk_delivery_sample_bundle, include=True
27+
store=hk_api, bundle_data=hk_delivery_another_sample_bundle, include=True
2628
)
29+
helpers.ensure_hk_bundle(store=hk_api, bundle_data=hk_delivery_case_bundle, include=True)
30+
return hk_api
31+
32+
33+
@pytest.fixture
34+
def delivery_fohm_upload_housekeeper_api(
35+
real_housekeeper_api: HousekeeperAPI,
36+
helpers: StoreHelpers,
37+
hk_delivery_case_bundle_fohm_upload: dict[str, Any],
38+
hk_delivery_sample_bundle: dict[str, Any],
39+
hk_delivery_another_sample_bundle: dict[str, Any],
40+
) -> HousekeeperAPI:
41+
"""Delivery API Housekeeper context."""
42+
hk_api: HousekeeperAPI = real_housekeeper_api
43+
helpers.ensure_hk_bundle(store=hk_api, bundle_data=hk_delivery_sample_bundle, include=True)
2744
helpers.ensure_hk_bundle(
28-
store=real_housekeeper_api, bundle_data=hk_delivery_another_sample_bundle, include=True
45+
store=hk_api, bundle_data=hk_delivery_another_sample_bundle, include=True
2946
)
3047
helpers.ensure_hk_bundle(
31-
store=real_housekeeper_api, bundle_data=hk_delivery_case_bundle, include=True
48+
store=hk_api, bundle_data=hk_delivery_case_bundle_fohm_upload, include=True
3249
)
33-
return real_housekeeper_api
50+
return hk_api
3451

3552

3653
@pytest.fixture
@@ -144,6 +161,68 @@ def delivery_store_microsalt(
144161
return status_db
145162

146163

164+
@pytest.fixture
165+
def delivery_store_mutant(
166+
cg_context: CGConfig,
167+
helpers: StoreHelpers,
168+
case_id: str,
169+
no_sample_case_id: str,
170+
case_name: str,
171+
sample_id: str,
172+
another_sample_id: str,
173+
sample_id_not_enough_reads: str,
174+
total_sequenced_reads_pass: int,
175+
total_sequenced_reads_not_pass: int,
176+
sample_name: str,
177+
another_sample_name: str,
178+
microbial_application_tag: str,
179+
) -> Store:
180+
"""Delivery API StatusDB context for Mutant."""
181+
status_db: Store = cg_context.status_db
182+
183+
# Error case without samples
184+
helpers.add_case(store=status_db, internal_id=no_sample_case_id, name=no_sample_case_id)
185+
186+
# Mutant case with fastq-analysis as data delivery
187+
case: Case = helpers.add_case(
188+
store=status_db,
189+
internal_id=case_id,
190+
name=case_name,
191+
data_analysis=Workflow.MUTANT,
192+
data_delivery=DataDelivery.FASTQ_ANALYSIS,
193+
)
194+
order: Order = helpers.add_order(store=status_db, customer_id=case.customer.id, ticket_id=1)
195+
case.orders.append(order)
196+
# Mutant samples
197+
sample: Sample = helpers.add_sample(
198+
store=status_db,
199+
application_tag=microbial_application_tag,
200+
internal_id=sample_id,
201+
name=sample_name,
202+
reads=total_sequenced_reads_pass,
203+
)
204+
205+
another_sample: Sample = helpers.add_sample(
206+
store=status_db,
207+
application_tag=microbial_application_tag,
208+
internal_id=another_sample_id,
209+
name=another_sample_name,
210+
reads=total_sequenced_reads_pass,
211+
)
212+
213+
sample_not_enough_reads: Sample = helpers.add_sample(
214+
store=status_db,
215+
application_tag=microbial_application_tag,
216+
internal_id=sample_id_not_enough_reads,
217+
reads=total_sequenced_reads_not_pass,
218+
)
219+
220+
for sample_mutant in [sample, another_sample, sample_not_enough_reads]:
221+
helpers.add_relationship(store=status_db, case=case, sample=sample_mutant)
222+
223+
return status_db
224+
225+
147226
@pytest.fixture
148227
def delivery_context_balsamic(
149228
cg_context: CGConfig,

tests/fixture_plugins/delivery_fixtures/delivery_files_models_fixtures.py

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,66 @@ def expected_bam_delivery_files_single_sample(
102102
return expected_bam_delivery_files
103103

104104

105+
@pytest.fixture
106+
def expected_fohm_delivery_files(
107+
delivery_fohm_upload_housekeeper_api: HousekeeperAPI,
108+
case_id: str,
109+
case_name: str,
110+
sample_id: str,
111+
sample_name: str,
112+
another_sample_id: str,
113+
another_sample_name: str,
114+
delivery_store_mutant: Store,
115+
) -> DeliveryFiles:
116+
"""Return the expected fastq delivery files."""
117+
sample_info: list[tuple[str, str]] = [
118+
(sample_id, sample_name),
119+
(another_sample_id, another_sample_name),
120+
]
121+
sample_files: list[SampleFile] = [
122+
SampleFile(
123+
case_id=case_id,
124+
sample_id=sample[0],
125+
sample_name=sample[1],
126+
file_path=delivery_fohm_upload_housekeeper_api.get_files_from_latest_version(
127+
bundle_name=sample[0], tags=[SequencingFileTag.FASTQ]
128+
)[0].full_path,
129+
)
130+
for sample in sample_info
131+
]
132+
case_sample_info: list[tuple[str, str, str]] = [
133+
(sample_id, sample_name, "consensus-sample"),
134+
(sample_id, sample_name, "vcf-report"),
135+
(another_sample_id, another_sample_name, "consensus-sample"),
136+
(another_sample_id, another_sample_name, "vcf-report"),
137+
]
138+
case_sample_files: list[SampleFile] = [
139+
SampleFile(
140+
case_id=case_id,
141+
sample_id=sample[0],
142+
sample_name=sample[1],
143+
file_path=delivery_fohm_upload_housekeeper_api.get_files_from_latest_version_containing_tags(
144+
bundle_name=case_id, tags=[{sample[2], sample[0]}]
145+
)[
146+
0
147+
].full_path,
148+
)
149+
for sample in case_sample_info
150+
]
151+
152+
case: Case = delivery_store_mutant.get_case_by_internal_id(case_id)
153+
delivery_meta_data = DeliveryMetaData(
154+
case_id=case.internal_id,
155+
customer_internal_id=case.customer.internal_id,
156+
ticket_id=case.latest_ticket,
157+
)
158+
return DeliveryFiles(
159+
delivery_data=delivery_meta_data,
160+
case_files=[],
161+
sample_files=case_sample_files + sample_files,
162+
)
163+
164+
105165
@pytest.fixture
106166
def expected_analysis_delivery_files(
107167
delivery_housekeeper_api: HousekeeperAPI,

tests/fixture_plugins/delivery_fixtures/delivery_services_fixtures.py

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -73,20 +73,6 @@ def bam_data_delivery_service(
7373
)
7474

7575

76-
@pytest.fixture
77-
def fohm_data_delivery_service(
78-
delivery_housekeeper_api: HousekeeperAPI,
79-
delivery_store_microsalt: Store,
80-
) -> RawDataAndAnalysisDeliveryFileFetcher:
81-
"""Fixture to get an instance of FetchFastqDeliveryFilesService."""
82-
tag_service = FOHMUploadTagsFetcher()
83-
return RawDataAndAnalysisDeliveryFileFetcher(
84-
hk_api=delivery_housekeeper_api,
85-
status_db=delivery_store_microsalt,
86-
tags_fetcher=tag_service,
87-
)
88-
89-
9076
@pytest.fixture
9177
def bam_data_delivery_service_no_housekeeper_bundle(
9278
real_housekeeper_api: HousekeeperAPI,
@@ -101,6 +87,20 @@ def bam_data_delivery_service_no_housekeeper_bundle(
10187
)
10288

10389

90+
@pytest.fixture
91+
def fohm_data_delivery_service(
92+
delivery_fohm_upload_housekeeper_api: HousekeeperAPI,
93+
delivery_store_mutant: Store,
94+
) -> RawDataAndAnalysisDeliveryFileFetcher:
95+
"""Fixture to get an instance of FetchFastqDeliveryFilesService."""
96+
tag_service = FOHMUploadTagsFetcher()
97+
return RawDataAndAnalysisDeliveryFileFetcher(
98+
hk_api=delivery_fohm_upload_housekeeper_api,
99+
status_db=delivery_store_mutant,
100+
tags_fetcher=tag_service,
101+
)
102+
103+
104104
@pytest.fixture
105105
def analysis_delivery_service(
106106
delivery_housekeeper_api: HousekeeperAPI,

0 commit comments

Comments
 (0)