Skip to content

Commit 3a097b5

Browse files
committed
User python serialization mode instead of json
FastAPI uses json serialization by default, which flattens the Allele sequence location field. To avoid this, use pydantic's serializer with python mode instead, and return the response directly as a JSON response.
1 parent 46f1b2d commit 3a097b5

File tree

1 file changed

+43
-20
lines changed

1 file changed

+43
-20
lines changed

src/api/routers/map.py

Lines changed: 43 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
""""Provide mapping router"""
22
from cool_seq_tool.schemas import AnnotationLayer
33
from fastapi import APIRouter, HTTPException
4+
from fastapi.responses import JSONResponse
45
from requests import HTTPError
56

67
from dcd_mapping.align import AlignmentError, BlatNotFoundError, align
@@ -57,12 +58,20 @@ async def map_scoreset(urn: str) -> ScoresetMapping:
5758
msg = f"BLAT resource could not be acquired: {e}"
5859
raise HTTPException(status_code=500, detail=msg) from e
5960
except AlignmentError as e:
60-
return ScoresetMapping(metadata=metadata, error_message=str(e).strip("'"))
61+
return JSONResponse(
62+
content=ScoresetMapping(
63+
metadata=metadata, error_message=str(e).strip("'")
64+
).model_dump(exclude_none=True)
65+
)
6166

6267
try:
6368
transcript = await select_transcript(metadata, records, alignment_result)
6469
except (TxSelectError, KeyError, ValueError) as e:
65-
return ScoresetMapping(metadata=metadata, error_message=str(e).strip("'"))
70+
return JSONResponse(
71+
content=ScoresetMapping(
72+
metadata=metadata, error_message=str(e).strip("'")
73+
).model_dump(exclude_none=True)
74+
)
6675
except HTTPError as e:
6776
msg = f"HTTP error occurred during transcript selection: {e}"
6877
raise HTTPException(status_code=500, detail=msg) from e
@@ -73,7 +82,11 @@ async def map_scoreset(urn: str) -> ScoresetMapping:
7382
try:
7483
vrs_results = vrs_map(metadata, alignment_result, records, transcript, True)
7584
except VrsMapError as e:
76-
return ScoresetMapping(metadata=metadata, error_message=str(e).strip("'"))
85+
return JSONResponse(
86+
content=ScoresetMapping(
87+
metadata=metadata, error_message=str(e).strip("'")
88+
).model_dump(exclude_none=True)
89+
)
7790
if vrs_results is None:
7891
return ScoresetMapping(
7992
metadata=metadata,
@@ -83,7 +96,11 @@ async def map_scoreset(urn: str) -> ScoresetMapping:
8396
try:
8497
vrs_results = annotate(vrs_results, transcript, metadata, VrsVersion.V_2)
8598
except Exception as e:
86-
return ScoresetMapping(metadata=metadata, error_message=str(e).strip("'"))
99+
return JSONResponse(
100+
content=ScoresetMapping(
101+
metadata=metadata, error_message=str(e).strip("'")
102+
).model_dump(exclude_none=True)
103+
)
87104
if vrs_results is None:
88105
return ScoresetMapping(
89106
metadata=metadata,
@@ -118,21 +135,27 @@ async def map_scoreset(urn: str) -> ScoresetMapping:
118135
# drop annotation layer from mapping object
119136
mapped_scores.append(ScoreAnnotation(**m.model_dump()))
120137
except Exception as e:
121-
return ScoresetMapping(metadata=metadata, error_message=str(e).strip("'"))
138+
return JSONResponse(
139+
content=ScoresetMapping(
140+
metadata=metadata, error_message=str(e).strip("'")
141+
).model_dump(exclude_none=True)
142+
)
122143

123-
return ScoresetMapping(
124-
metadata=raw_metadata,
125-
computed_protein_reference_sequence=reference_sequences[
126-
AnnotationLayer.PROTEIN
127-
]["computed_reference_sequence"],
128-
mapped_protein_reference_sequence=reference_sequences[AnnotationLayer.PROTEIN][
129-
"mapped_reference_sequence"
130-
],
131-
computed_genomic_reference_sequence=reference_sequences[
132-
AnnotationLayer.GENOMIC
133-
]["computed_reference_sequence"],
134-
mapped_genomic_reference_sequence=reference_sequences[AnnotationLayer.GENOMIC][
135-
"mapped_reference_sequence"
136-
],
137-
mapped_scores=mapped_scores,
144+
return JSONResponse(
145+
content=ScoresetMapping(
146+
metadata=raw_metadata,
147+
computed_protein_reference_sequence=reference_sequences[
148+
AnnotationLayer.PROTEIN
149+
]["computed_reference_sequence"],
150+
mapped_protein_reference_sequence=reference_sequences[
151+
AnnotationLayer.PROTEIN
152+
]["mapped_reference_sequence"],
153+
computed_genomic_reference_sequence=reference_sequences[
154+
AnnotationLayer.GENOMIC
155+
]["computed_reference_sequence"],
156+
mapped_genomic_reference_sequence=reference_sequences[
157+
AnnotationLayer.GENOMIC
158+
]["mapped_reference_sequence"],
159+
mapped_scores=mapped_scores,
160+
).model_dump(exclude_none=True)
138161
)

0 commit comments

Comments
 (0)