Skip to content

Commit

Permalink
fix: TypeError for params that accept union of enums (#566)
Browse files Browse the repository at this point in the history
* Methods now raise ValueError if incorrect enum value passed
  • Loading branch information
korikuzma authored Jul 16, 2024
1 parent ee9083c commit aecbd08
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 16 deletions.
49 changes: 34 additions & 15 deletions src/variation/hgvs_dup_del_mode.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,25 @@
# Define deletion alt types
DELS = {AltType.DELETION_AMBIGUOUS, AltType.DELETION}

# Define supported alt types for HGVS Dup Del Mode
DELS_DUPS = {
AltType.DELETION,
AltType.DELETION_AMBIGUOUS,
AltType.DUPLICATION,
AltType.DUPLICATION_AMBIGUOUS,
}


def _check_supported_alt_type(alt_type: AltType) -> None:
"""Check that ``alt_type`` is one of ``DUP_DELS``
:param alt_type: Alteration type
:raises ValueError: If ``alt_type`` not one of ``DELS_DUPS``.
"""
if alt_type not in DELS_DUPS:
err_msg = f"`alt_type` must be one of: {DELS_DUPS}"
raise ValueError(err_msg)


class HGVSDupDelMode:
"""Class for handling how to interpret HGVS duplications and deletions."""
Expand All @@ -24,10 +43,7 @@ def __init__(self, seqrepo_access: SeqRepoAccess) -> None:

def default_mode(
self,
alt_type: AltType.DELETION
| AltType.DELETION_AMBIGUOUS
| AltType.DUPLICATION
| AltType.DUPLICATION_AMBIGUOUS,
alt_type: AltType,
location: dict,
vrs_seq_loc_ac: str,
baseline_copies: int | None = None,
Expand All @@ -43,14 +59,17 @@ def default_mode(
else
allele
:param alt_type: The type of alteration
:param alt_type: The type of alteration. Must be one of ``DELS_DUPS``.
:param location: Sequence Location object
:param vrs_seq_loc_ac: Accession used in VRS Sequence Location
:param baseline_copies: Baseline copies for Copy Number Count variation
:param copy_change: copy change for Copy Number Change Variation
:param alt: Alteration
:raises ValueError: If ``alt_type`` not one of ``DELS_DUPS``.
:return: VRS Variation object represented as a dict
"""
_check_supported_alt_type(alt_type)

variation = None
if not baseline_copies and alt_type in AMBIGUOUS_REGIONS:
variation = self.copy_number_change_mode(alt_type, location, copy_change)
Expand All @@ -62,20 +81,20 @@ def default_mode(

def copy_number_count_mode(
self,
alt_type: AltType.DELETION
| AltType.DELETION_AMBIGUOUS
| AltType.DUPLICATION
| AltType.DUPLICATION_AMBIGUOUS,
alt_type: AltType,
location: dict,
baseline_copies: int,
) -> dict:
"""Return a VRS Copy Number Variation.
:param alt_type: The type of alteration
:param alt_type: The type of alteration. Must be one of ``DELS_DUPS``.
:param location: VRS SequenceLocation
:param baseline_copies: Baseline copies number
:raises ValueError: If ``alt_type`` not one of ``DELS_DUPS``.
:return: VRS Copy Number object represented as a dict
"""
_check_supported_alt_type(alt_type)

copies = baseline_copies - 1 if alt_type in DELS else baseline_copies + 1
seq_loc = models.SequenceLocation(**location)
seq_loc.id = ga4gh_identify(seq_loc)
Expand All @@ -85,20 +104,20 @@ def copy_number_count_mode(

def copy_number_change_mode(
self,
alt_type: AltType.DELETION
| AltType.DELETION_AMBIGUOUS
| AltType.DUPLICATION
| AltType.DUPLICATION_AMBIGUOUS,
alt_type: AltType,
location: dict,
copy_change: models.CopyChange | None = None,
) -> dict:
"""Return copy number change variation
:param alt_type: The type of alteration
:param alt_type: The type of alteration. Must be one of ``DELS_DUPS``.
:param location: VRS SequenceLocation
:param copy_change: The copy change
:raises ValueError: If ``alt_type`` not one of ``DELS_DUPS``.
:return: Copy Number Change variation as a dict
"""
_check_supported_alt_type(alt_type)

if not copy_change:
copy_change = (
models.CopyChange.EFO_0030067
Expand Down
9 changes: 8 additions & 1 deletion src/variation/translators/translator.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ async def get_p_or_cdna_translation_result(
start_pos: int,
end_pos: int,
alt_type: AltType,
coordinate_type: AnnotationLayer.PROTEIN | AnnotationLayer.CDNA,
coordinate_type: AnnotationLayer,
errors: list[str],
cds_start: int | None = None,
ref: str | None = None,
Expand All @@ -184,8 +184,15 @@ async def get_p_or_cdna_translation_result(
`coordinate_type == AnnotationLayer.CDNA`.
:param ref: Expected reference sequence
:param alt: Expected change
:raises ValueError: If ``coordinate`` type not one of
``AnnotationLayer.PROTEIN`` or ``AnnotationLayer.CDNA``
:return: Translation result if successful. Else, `None`
"""
supported_coordinate_types = {AnnotationLayer.PROTEIN, AnnotationLayer.CDNA}
if coordinate_type not in supported_coordinate_types:
err_msg = f"`coordinate_type` must be one of {supported_coordinate_types}"
raise ValueError(err_msg)

vrs_allele = None
vrs_seq_loc_ac = None
vrs_seq_loc_ac_status = VrsSeqLocAcStatus.NA
Expand Down

0 comments on commit aecbd08

Please sign in to comment.