Skip to content

Commit

Permalink
improve factory docstring
Browse files Browse the repository at this point in the history
  • Loading branch information
ChrOertlin committed Dec 18, 2024
1 parent 6f6e3b0 commit dc6c7ad
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 21 deletions.
7 changes: 3 additions & 4 deletions cg/services/deliver_files/factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,9 @@ class DeliveryServiceFactory:
"""
Class to build the delivery services based on case, workflow, delivery type, delivery destination and delivery structure.
The delivery destination is used to specify delivery to the customer or for external upload.
It determines how the delivery_base_path is managed and its underlying folder structure.
Workflow is used to specify the workflow of the case and is required for the tag fetcher.
Delivery type is used to specify the type of delivery to perform.
Delivery structure is used to specify the structure of the delivery.
"""

def __init__(
Expand Down Expand Up @@ -177,8 +176,8 @@ def _get_file_fetcher(

def _convert_workflow(self, case: Case) -> Workflow:
"""Change the workflow of a Microbial Fastq case to Microsalt to allow the concatenation of fastq files.
With the introduction of the microbial-fastq delivery type, an unsupported combination of delivery type and
workflow setup is required. This function makes sure that a raw data workflow with microbial fastq delivery
With the introduction of the microbial-fastq delivery type, an unsupported combination of delivery type and
workflow setup is required. This function makes sure that a raw data workflow with microbial fastq delivery
type is treated as a microsalt workflow so that the microbial-fastq sample files can be concatenated.
args:
case: The case to convert the workflow for
Expand Down
4 changes: 3 additions & 1 deletion cg/services/deliver_files/file_fetcher/analysis_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,9 @@ def get_files_to_deliver(self, case_id: str, sample_id: str | None = None) -> De
case_id: The case id to deliver files for
sample_id: The sample id to deliver files for
"""
LOG.debug(f"[FETCH SERVICE] Fetching analysis files for case: {case_id}, sample: {sample_id}")
LOG.debug(
f"[FETCH SERVICE] Fetching analysis files for case: {case_id}, sample: {sample_id}"
)
case: Case = self.status_db.get_case_by_internal_id(internal_id=case_id)
analysis_case_files: list[CaseFile] = self._get_analysis_case_delivery_files(
case=case, sample_id=sample_id
Expand Down
2 changes: 1 addition & 1 deletion cg/services/deliver_files/file_formatter/files/abstract.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@


class FileFormatter(ABC):

@abstractmethod
def format_files(
self, moved_files: list[CaseFile | SampleFile], delivery_path: Path
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,17 @@ def _get_sample_names(sample_files: list[SampleFile]) -> set[str]:
"""Extract sample names from the sample files."""
return {sample_file.sample_name for sample_file in sample_files}

def _create_sample_directories(self, sample_names: set[str], delivery_path: Path) -> None:
"""Create directories for each sample name only if the file name formatter is the NestedSampleFileFormatter.
args:
sample_names: set[str]: Set of sample names.
delivery_path: Path: Path to the delivery directory.
"""
if not isinstance(self.path_name_formatter, NestedStructurePathFormatter):
return
for sample_name in sample_names:
self.file_manager.create_directories(base_path=delivery_path, directories={sample_name})

def _format_sample_file_paths(self, sample_files: list[SampleFile]) -> list[FormattedFile]:
"""
Return a list of formatted sample files.
Expand Down Expand Up @@ -108,17 +119,6 @@ def _rename_original_files(self, formatted_files: list[FormattedFile]) -> None:
src=formatted_file.original_path, dst=formatted_file.formatted_path
)

def _create_sample_directories(self, sample_names: set[str], delivery_path: Path) -> None:
"""Create directories for each sample name only if the file name formatter is the NestedSampleFileFormatter.
args:
sample_names: set[str]: Set of sample names.
delivery_path: Path: Path to the delivery directory.
"""
if not isinstance(self.path_name_formatter, NestedStructurePathFormatter):
return
for sample_name in sample_names:
self.file_manager.create_directories(base_path=delivery_path, directories={sample_name})

def _concatenate_fastq_files(
self, delivery_path: Path, sample_names: set[str]
) -> dict[Path, Path]:
Expand Down Expand Up @@ -246,7 +246,7 @@ def _group_fastq_files_per_sample(
}
for fastq_file in fastq_files:
sample_fastq_files[fastq_file.sample_name].append(fastq_file)
self._all_sample_fastq_file_share_same_directory(sample_fastq_files=sample_fastq_files)
self._validate_sample_fastq_file_share_same_directory(sample_fastq_files=sample_fastq_files)
return sample_fastq_files

def _replace_fastq_paths(
Expand All @@ -266,12 +266,13 @@ def _replace_fastq_paths(
formatted_file.formatted_path = concatenation_maps[formatted_file.formatted_path]

@staticmethod
def _all_sample_fastq_file_share_same_directory(
def _validate_sample_fastq_file_share_same_directory(
sample_fastq_files: dict[str, list[FastqFile]]
) -> None:
"""
Assert that all fastq files for a sample share the same directory.
This is to ensure that the files are concatenated within the expected directory path.
raises: ValueError if the fastq files are not in the same directory.
args:
sample_fastq_files: dict[str, list[FastqFile]]: Dictionary of sample names and their fastq files.
"""
Expand Down
4 changes: 3 additions & 1 deletion cg/services/deliver_files/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,9 @@ def update_file_paths(
file_model.file_path = Path(target_dir, file_model.file_path.name)
return file_models

def move_and_update_files(self, file_models: list[CaseFile | SampleFile], target_dir: Path) -> list[CaseFile | SampleFile]:
def move_and_update_files(
self, file_models: list[CaseFile | SampleFile], target_dir: Path
) -> list[CaseFile | SampleFile]:
"""Move files to the target directory and update the file paths.
args:
file_models: The file models that contain the files to move.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@
),
],
)
def test_component_formatters(
def test_file_formatters(
moved_files: list[CaseFile | SampleFile],
expected_formatted_files: list[FormattedFile],
file_formatter: FileFormatter,
Expand Down

0 comments on commit dc6c7ad

Please sign in to comment.