Mappers¶
Application Layer - Mappers Package.
This package contains mappers for converting between domain entities and application DTOs. Mappers are stateless and can be used as functions following Single Responsibility Principle.
Modules¶
- Sample Mapper: Maps between Sample entities and DataFrames
- Merged Data Mapper: Maps between MergedData entities and DTOs
Notes¶
- Mappers are stateless and can be used as functions
- Follow Single Responsibility Principle
- Enable layer independence (Domain ↔ Application)
Package Overview¶
Application Layer - Mappers Package.
This package contains mappers for converting between domain entities and application DTOs.
Mappers
SampleMapper Maps between Sample entities and DataFrames MergedDataMapper Maps between MergedData entities and DTOs
Notes
- Mappers are stateless and can be used as functions
- Follow Single Responsibility Principle
- Enable layer independence (Domain ↔ Application)
SampleMapper¶
SampleMapper ¶
Map between Sample entities and DataFrames.
Provides bidirectional conversion between domain entities (Sample, Dataset) and pandas DataFrames used by the application layer.
Methods:
| Name | Description |
|---|---|
to_dataframe | Convert Dataset to DataFrame |
from_dataframe | Convert DataFrame to Dataset |
samples_to_dict | Convert Dataset to dictionary format |
Notes
- All methods are static (stateless)
- Preserves KO identifiers exactly
- Handles empty datasets gracefully
Functions¶
to_dataframe staticmethod ¶
Convert Dataset to DataFrame.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dataset | Dataset | Domain entity containing samples | required |
Returns:
| Type | Description |
|---|---|
DataFrame | DataFrame with columns: Sample, KO (each row is one Sample-KO pair) |
Notes
- Each KO creates a new row
- Sample ID is repeated for multiple KOs
- Empty datasets return empty DataFrame with correct columns
Source code in src/application/mappers/sample_mapper.py
from_dataframe staticmethod ¶
Convert DataFrame to Dataset.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
df | DataFrame | DataFrame with columns: Sample, KO | required |
Returns:
| Type | Description |
|---|---|
Dataset | Domain entity with reconstructed samples |
Raises:
| Type | Description |
|---|---|
ValueError | If DataFrame missing required columns |
ValueError | If DataFrame contains invalid KO identifiers |
Notes
- Groups KOs by Sample ID
- Validates KO format during conversion
- Preserves original order of samples
Source code in src/application/mappers/sample_mapper.py
samples_to_dict staticmethod ¶
Convert Dataset to dictionary format.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dataset | Dataset | Domain entity with samples | required |
Returns:
| Type | Description |
|---|---|
dict | Dictionary with sample_id as keys, list of KO IDs as values |
Notes
- Useful for JSON serialization
- Preserves all KOs per sample
- Returns empty dict for empty dataset
Source code in src/application/mappers/sample_mapper.py
dict_to_dataset staticmethod ¶
Convert dictionary to Dataset.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data | dict | Dictionary with sample_id as keys, list of KO IDs as values | required |
Returns:
| Type | Description |
|---|---|
Dataset | Reconstructed domain entity |
Notes
- Validates KO format during conversion
- Preserves dictionary key order (Python 3.7+)
Source code in src/application/mappers/sample_mapper.py
MergedDataMapper¶
MergedDataMapper ¶
Map between MergedData entity and MergedDataDTO.
Converts between the domain MergedData entity and the application layer DTO used for data transfer.
Methods:
| Name | Description |
|---|---|
to_dto | Convert MergedData entity to DTO |
from_dto | Convert DTO to MergedData entity |
Notes
- All methods are static (stateless)
- Does not copy DataFrames (shares references)
- Preserves all metadata
Functions¶
to_dto staticmethod ¶
Convert MergedData entity to DTO.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
entity | MergedData | Domain entity with merged data | required |
cache_key | str | Cache key for this merge operation | required |
processing_time_seconds | float | Time taken for processing | 0.0 |
Returns:
| Type | Description |
|---|---|
MergedDataDTO | Immutable DTO for application layer |
Notes
- Shares DataFrame references (no copying)
- Adds cache_key and processing_time metadata
- Preserves all optional database results
Source code in src/application/mappers/merged_data_mapper.py
from_dto staticmethod ¶
Convert DTO to MergedData entity.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dto | MergedDataDTO | Application layer DTO | required |
Returns:
| Type | Description |
|---|---|
MergedData | Reconstructed domain entity |
Notes
- Shares DataFrame references (no copying)
- Loses cache_key and processing_time (domain doesn't need them)
- Preserves all database results
Source code in src/application/mappers/merged_data_mapper.py
create_empty_dto staticmethod ¶
Create empty DTO for no matches scenario.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cache_key | str | Cache key for this operation | required |
Returns:
| Type | Description |
|---|---|
MergedDataDTO | DTO with empty DataFrames and zero counts |
Notes
- Useful when no matches found in database
- All DataFrames are empty but not None
- Maintains DTO structure consistency