Skip to content

Commit 430f1b7

Browse files
committed
feat: refactor custom exceptions to exceptions module
1 parent 1e732a2 commit 430f1b7

File tree

9 files changed

+87
-47
lines changed

9 files changed

+87
-47
lines changed

src/api/routers/map.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,29 +6,31 @@
66
from fastapi.responses import JSONResponse
77
from requests import HTTPError
88

9-
from dcd_mapping.align import AlignmentError, BlatNotFoundError, build_alignment_result
9+
from dcd_mapping.align import build_alignment_result
1010
from dcd_mapping.annotate import (
1111
_get_computed_reference_sequence,
1212
_get_mapped_reference_sequence,
1313
_set_scoreset_layer,
1414
annotate,
1515
)
1616
from dcd_mapping.exceptions import (
17+
AlignmentError,
18+
BlatNotFoundError,
19+
DataLookupError,
1720
MissingSequenceIdError,
21+
ResourceAcquisitionError,
22+
ScoresetNotSupportedError,
1823
UnsupportedReferenceSequenceNameSpaceError,
1924
UnsupportedReferenceSequencePrefixError,
2025
VrsMapError,
2126
)
22-
from dcd_mapping.lookup import DataLookupError
2327
from dcd_mapping.mavedb_data import (
24-
ScoresetNotSupportedError,
2528
get_raw_scoreset_metadata,
2629
get_scoreset_metadata,
2730
get_scoreset_records,
2831
patch_target_sequence_type,
2932
with_mavedb_score_set,
3033
)
31-
from dcd_mapping.resource_utils import ResourceAcquisitionError
3234
from dcd_mapping.schemas import (
3335
ScoreAnnotation,
3436
ScoresetMapping,

src/dcd_mapping/align.py

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,15 @@
1212
from Bio.SearchIO._model import Hit, QueryResult
1313
from cool_seq_tool.schemas import Strand
1414

15-
from dcd_mapping.lookup import get_chromosome_identifier, get_gene_location
16-
from dcd_mapping.mavedb_data import LOCAL_STORE_PATH, ScoresetNotSupportedError
17-
from dcd_mapping.resource_utils import (
15+
from dcd_mapping.exceptions import (
16+
AlignmentError,
17+
BlatNotFoundError,
1818
ResourceAcquisitionError,
19-
http_download,
19+
ScoresetNotSupportedError,
2020
)
21+
from dcd_mapping.lookup import get_chromosome_identifier, get_gene_location
22+
from dcd_mapping.mavedb_data import LOCAL_STORE_PATH
23+
from dcd_mapping.resource_utils import http_download
2124
from dcd_mapping.schemas import (
2225
AlignmentResult,
2326
GeneLocation,
@@ -32,14 +35,6 @@
3235
_logger = logging.getLogger(__name__)
3336

3437

35-
class AlignmentError(Exception):
36-
"""Raise when errors encountered during alignment."""
37-
38-
39-
class BlatNotFoundError(AlignmentError):
40-
"""Raise when BLAT binary appears to be missing."""
41-
42-
4338
def _write_query_file(file: Path, lines: list[str]) -> None:
4439
"""Write lines to query file. This method is broken out to enable easy mocking while
4540
testing.

src/dcd_mapping/cli.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,14 @@
66

77
import click
88

9-
from dcd_mapping.align import AlignmentError
10-
from dcd_mapping.exceptions import VrsMapError
9+
from dcd_mapping.exceptions import (
10+
AlignmentError,
11+
ResourceAcquisitionError,
12+
TxSelectError,
13+
VrsMapError,
14+
)
1115
from dcd_mapping.main import map_scoreset_urn
12-
from dcd_mapping.resource_utils import ResourceAcquisitionError
1316
from dcd_mapping.schemas import VrsVersion
14-
from dcd_mapping.transcripts import TxSelectError
1517

1618
_logger = logging.getLogger(__name__)
1719

src/dcd_mapping/exceptions.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
"""Exceptions for DCD Mapping Module"""
22

33

4+
### Custom Exceptions for VRS Mapping Errors ###
5+
6+
47
class VrsMapError(Exception):
58
"""Raise in case of generic VRS mapping errors."""
69

@@ -15,3 +18,42 @@ class MissingSequenceIdError(ValueError):
1518

1619
class UnsupportedReferenceSequencePrefixError(ValueError):
1720
"""Raised when a reference sequence prefix is not supported."""
21+
22+
23+
### Custom Exceptions for Alignment Errors ###
24+
25+
26+
class AlignmentError(ValueError):
27+
"""Raise when errors encountered during alignment."""
28+
29+
30+
class BlatNotFoundError(AlignmentError):
31+
"""Raise when BLAT binary appears to be missing."""
32+
33+
34+
### Custom Exceptions for Data Lookup Errors ###
35+
36+
37+
class DataLookupError(Exception):
38+
"""Raise for misc. issues related to resource acquisition/lookup."""
39+
40+
41+
### Custom Exceptions for MaveDB Data Errors ###
42+
43+
44+
class ScoresetNotSupportedError(ValueError):
45+
"""Raise when a score set cannot be mapped because it has characteristics that are not currently supported."""
46+
47+
48+
### Custom Exceptions for Resource Acquisition Errors ###
49+
50+
51+
class ResourceAcquisitionError(ValueError):
52+
"""Raise when resource acquisition fails."""
53+
54+
55+
### Custom Exceptions for Transcript Selection Errors ###
56+
57+
58+
class TxSelectError(ValueError):
59+
"""Raise for transcript selection failure."""

src/dcd_mapping/lookup.py

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
from gene.query import QueryHandler
5050
from gene.schemas import MatchType, SourceName
5151

52+
from dcd_mapping.exceptions import DataLookupError
5253
from dcd_mapping.schemas import (
5354
GeneLocation,
5455
ManeDescription,
@@ -93,10 +94,6 @@ def cdot_rest() -> RESTDataProvider:
9394
# ---------------------------------- Global ---------------------------------- #
9495

9596

96-
class DataLookupError(Exception):
97-
"""Raise for misc. issues related to resource acquisition/lookup."""
98-
99-
10097
class CoolSeqToolBuilder:
10198
"""Singleton constructor for ``cool-seq-tool`` instance."""
10299

@@ -241,7 +238,9 @@ async def check_uta() -> None:
241238
query = f"select * from {uta.schema}.meta" # noqa: S608
242239
result = await uta.execute_query(query)
243240
if not result:
244-
raise DataLookupError
241+
msg = "UTA schema check failed. No results returned."
242+
_logger.error(msg)
243+
raise DataLookupError(msg)
245244

246245

247246
async def get_protein_accession(transcript: str) -> str | None:
@@ -302,9 +301,13 @@ async def get_transcripts(
302301
def check_gene_normalizer() -> None:
303302
q = GeneNormalizerBuilder()
304303
if (not q.db.check_schema_initialized()) or not (q.db.check_tables_populated()):
305-
raise DataLookupError
304+
msg = "Gene Normalizer database schema check failed. No results returned."
305+
_logger.error(msg)
306+
raise DataLookupError(msg)
306307
if q.normalize("BRAF").match_type == MatchType.NO_MATCH:
307-
raise DataLookupError
308+
msg = "Gene Normalizer returned no normalization results for BRAF. This indicates an underlying issue with the database that should be investigated."
309+
_logger.error(msg)
310+
raise DataLookupError(msg)
308311

309312

310313
def _get_hgnc_symbol(term: str) -> str | None:
@@ -436,7 +439,9 @@ def get_gene_location(target_gene: TargetGene) -> GeneLocation | None:
436439
def check_seqrepo() -> None:
437440
sr = get_seqrepo()
438441
if not sr.sr["NC_000001.11"][780000:780020]:
439-
raise DataLookupError
442+
msg = "SeqRepo returned no sequence for NC_000001.11 at 780000:780020. This indicates an underlying issue with SeqRepo that should be investigated."
443+
_logger.error(msg)
444+
raise DataLookupError(msg)
440445
conn = sr.sr.aliases._db
441446
try:
442447
# conn = sr.sr.aliases._db

src/dcd_mapping/main.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,32 +8,34 @@
88
import click
99
from requests import HTTPError
1010

11-
from dcd_mapping.align import AlignmentError, BlatNotFoundError, build_alignment_result
11+
from dcd_mapping.align import build_alignment_result
1212
from dcd_mapping.annotate import (
1313
annotate,
1414
save_mapped_output_json,
1515
write_scoreset_mapping_to_json,
1616
)
1717
from dcd_mapping.exceptions import (
18+
AlignmentError,
19+
BlatNotFoundError,
20+
DataLookupError,
1821
MissingSequenceIdError,
22+
ResourceAcquisitionError,
23+
ScoresetNotSupportedError,
1924
UnsupportedReferenceSequenceNameSpaceError,
2025
UnsupportedReferenceSequencePrefixError,
2126
VrsMapError,
2227
)
2328
from dcd_mapping.lookup import (
24-
DataLookupError,
2529
check_gene_normalizer,
2630
check_seqrepo,
2731
check_uta,
2832
)
2933
from dcd_mapping.mavedb_data import (
30-
ScoresetNotSupportedError,
3134
get_scoreset_metadata,
3235
get_scoreset_records,
3336
patch_target_sequence_type,
3437
with_mavedb_score_set,
3538
)
36-
from dcd_mapping.resource_utils import ResourceAcquisitionError
3739
from dcd_mapping.schemas import (
3840
ScoreRow,
3941
ScoresetMapping,

src/dcd_mapping/mavedb_data.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,14 @@
1717
from fastapi import HTTPException
1818
from pydantic import ValidationError
1919

20-
from dcd_mapping.lookup import DataLookupError
20+
from dcd_mapping.exceptions import (
21+
DataLookupError,
22+
ResourceAcquisitionError,
23+
ScoresetNotSupportedError,
24+
)
2125
from dcd_mapping.resource_utils import (
2226
LOCAL_STORE_PATH,
2327
MAVEDB_BASE_URL,
24-
ResourceAcquisitionError,
2528
authentication_header,
2629
http_download,
2730
)
@@ -47,10 +50,6 @@
4750
_logger = logging.getLogger(__name__)
4851

4952

50-
class ScoresetNotSupportedError(Exception):
51-
"""Raise when a score set cannot be mapped because it has characteristics that are not currently supported."""
52-
53-
5453
def get_scoreset_urns() -> set[str]:
5554
"""Fetch all scoreset URNs. Since species is annotated at the scoreset target level,
5655
we can't yet filter on anything like `homo sapien` -- meaning this is fairly slow.

src/dcd_mapping/resource_utils.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,6 @@
1818
LOCAL_STORE_PATH.mkdir(exist_ok=True, parents=True)
1919

2020

21-
class ResourceAcquisitionError(Exception):
22-
"""Raise when resource acquisition fails."""
23-
24-
2521
def authentication_header() -> dict | None:
2622
"""Fetch with api key envvar, if available."""
2723
return {"X-API-key": MAVEDB_API_KEY} if MAVEDB_API_KEY is not None else None

src/dcd_mapping/transcripts.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
from Bio.SeqUtils import seq1
88
from cool_seq_tool.schemas import TranscriptPriority
99

10+
from dcd_mapping.exceptions import TxSelectError
1011
from dcd_mapping.lookup import (
1112
get_chromosome_identifier,
1213
get_gene_symbol,
@@ -29,15 +30,11 @@
2930
TxSelectResult,
3031
)
3132

32-
__all__ = ["select_transcript", "TxSelectError"]
33+
__all__ = ["select_transcript"]
3334

3435
_logger = logging.getLogger(__name__)
3536

3637

37-
class TxSelectError(Exception):
38-
"""Raise for transcript selection failure."""
39-
40-
4138
async def _get_compatible_transcripts(
4239
target_gene: TargetGene, align_result: AlignmentResult
4340
) -> list[list[str]]:

0 commit comments

Comments
 (0)