Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: TypeError for params that accept union of enums #566

Merged
merged 5 commits into from
Jul 16, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/variation/gnomad_vcf_to_protein_variation.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from gene.query import QueryHandler as GeneQueryHandler
from gene.schemas import MatchType as GeneMatchType

from variation import __version__
from variation.classify import Classify
from variation.schemas.classification_response_schema import Nomenclature
from variation.schemas.gnomad_vcf_to_protein_schema import GnomadVcfToProteinService
Expand All @@ -19,7 +20,6 @@
from variation.tokenize import Tokenize
from variation.translate import Translate
from variation.validate import Validate
from variation import __version__


class GnomadVcfToProteinError(Exception):
Expand Down
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``.
korikuzma marked this conversation as resolved.
Show resolved Hide resolved
: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
2 changes: 1 addition & 1 deletion src/variation/normalize.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from cool_seq_tool.sources import UtaDatabase
from ga4gh.vrs import models

from variation import __version__
from variation.classify import Classify
from variation.schemas.app_schemas import Endpoint
from variation.schemas.normalize_response_schema import (
Expand All @@ -25,7 +26,6 @@
from variation.translate import Translate
from variation.utils import update_warnings_for_no_resp
from variation.validate import Validate
from variation import __version__


class Normalize(ToVRS):
Expand Down
2 changes: 1 addition & 1 deletion src/variation/schemas/copy_number_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
model_validator,
)

from variation.schemas.normalize_response_schema import ServiceResponse
from variation import __version__
from variation.schemas.normalize_response_schema import ServiceResponse


class ParsedPosType(str, Enum):
Expand Down
2 changes: 1 addition & 1 deletion src/variation/schemas/hgvs_to_copy_number_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
from ga4gh.vrs import models
from pydantic import ConfigDict, StrictStr

from variation.schemas.normalize_response_schema import ServiceResponse
from variation import __version__
from variation.schemas.normalize_response_schema import ServiceResponse


class HgvsToCopyNumberCountService(ServiceResponse):
Expand Down
2 changes: 1 addition & 1 deletion src/variation/schemas/service_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
from cool_seq_tool.schemas import ToGenomicService as ToGenomic
from pydantic import ConfigDict

from variation.schemas.normalize_response_schema import ServiceMeta
from variation import __version__
from variation.schemas.normalize_response_schema import ServiceMeta


class ClinVarAssembly(str, Enum):
Expand Down
2 changes: 1 addition & 1 deletion src/variation/to_copy_number_variation.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
from gene.schemas import MatchType as GeneMatchType
from pydantic import ValidationError

from variation import __version__
from variation.classify import Classify
from variation.schemas.app_schemas import Endpoint
from variation.schemas.classification_response_schema import ClassificationType
Expand Down Expand Up @@ -43,7 +44,6 @@
from variation.translate import Translate
from variation.utils import get_priority_sequence_location
from variation.validate import Validate
from variation import __version__

VALID_CLASSIFICATION_TYPES = [
ClassificationType.GENOMIC_DUPLICATION,
Expand Down
2 changes: 1 addition & 1 deletion src/variation/to_vrs.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from cool_seq_tool.handlers import SeqRepoAccess
from ga4gh.vrs import models

from variation import __version__
from variation.classify import Classify
from variation.schemas.app_schemas import Endpoint
from variation.schemas.normalize_response_schema import (
Expand All @@ -18,7 +19,6 @@
from variation.tokenize import Tokenize
from variation.translate import Translate
from variation.validate import Validate
from variation import __version__
from variation.vrs_representation import VRSRepresentation


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