Sample¶
sample ¶
Sample Entity
Represents a biological sample with its associated KOs.
Classes¶
Sample dataclass ¶
Sample(id: SampleId, ko_list: List[KO] = list(), created_at: datetime = datetime.now(), metadata: Dict[str, Any] = dict())
Aggregate Root - Represents a biological sample.
Encapsulates business rules related to samples and their associated KOs. A sample is uniquely identified by its SampleId and contains a list of KOs (KEGG Orthology) that were detected in it.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
id | SampleId | Unique sample identifier | required |
ko_list | List[KO] | List of KOs associated with the sample | [] |
created_at | datetime | Sample creation timestamp | datetime.now() |
metadata | Dict[str, Any] | Additional sample metadata | {} |
Raises:
| Type | Description |
|---|---|
ValueError | If sample is validated without at least one KO |
Notes
This is an Aggregate Root entity in DDD context, responsible for maintaining consistency of its invariants (e.g., every valid sample must have at least one KO).
Attributes¶
ko_count property ¶
Returns quantity of KOs associated with the sample.
Returns:
| Type | Description |
|---|---|
int | Number of KOs in the list. |
Functions¶
add_ko ¶
Adds a KO to the sample with duplicate validation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ko | KO | KO to be added. | required |
Notes
Duplicate KOs are automatically ignored.
Source code in src/domain/entities/sample.py
remove_ko ¶
Removes a KO from the sample if it exists.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ko | KO | KO to be removed. | required |
Notes
If KO does not exist in the list, operation is silently ignored.
Source code in src/domain/entities/sample.py
has_ko ¶
Checks if sample has a specific KO.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ko | KO | KO to be checked. | required |
Returns:
| Type | Description |
|---|---|
bool | True if KO is present in the sample. |
Source code in src/domain/entities/sample.py
get_unique_kos ¶
Returns list of unique KOs (without duplicates).
Returns:
| Type | Description |
|---|---|
List[KO] | List of unique KOs. |
Notes
In practice, ko_list should not contain duplicates due to add_ko() method, but this method ensures uniqueness.
Source code in src/domain/entities/sample.py
validate ¶
Validates entity business rules.
Raises:
| Type | Description |
|---|---|
ValueError | If sample does not have at least one KO. |
Notes
This validation ensures the business invariant: every processed sample must contain at least one valid KO.
Source code in src/domain/entities/sample.py
__str__ ¶
Returns string representation of sample.
Returns:
| Type | Description |
|---|---|
str | String in format "Sample(id) with X KOs". |
__repr__ ¶
Returns debug representation of sample.
Returns:
| Type | Description |
|---|---|
str | Detailed representation. |
Source code in src/domain/entities/sample.py
__eq__ ¶
Compares samples by identity (SampleId).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
other | object | Object to be compared. | required |
Returns:
| Type | Description |
|---|---|
bool | True if both samples have the same ID. |