File size: 2,924 Bytes
af567cf | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 | from typing import Any, Optional
from fastapi import APIRouter, HTTPException, Query
from pydantic import BaseModel
from app.models.variant import VariantSearchRequest, VariantResponse
from app.services.variant_service import annotate_variant, search_variants
from app.services.franklin_service import (
get_franklin_variant_assessment,
proxy_franklin_api,
)
router = APIRouter()
class FranklinProxyRequest(BaseModel):
endpoint: str
method: str = "GET"
payload: Optional[Any] = None
params: Optional[dict[str, Any]] = None
@router.api_route("/search", methods=["POST", "GET"], response_model=list[VariantResponse])
async def search_variants_endpoint(
request: Optional[VariantSearchRequest] = None,
query: Optional[str] = Query(default=None),
genome_build: Optional[str] = Query(default=None),
):
"""Search for variants by query string (POST body or GET query params)."""
try:
if request is not None:
q = request.query
build = request.genome_build or "hg38"
else:
q = query
build = genome_build or "hg38"
if not q:
raise HTTPException(status_code=400, detail="Missing query parameter")
results = await search_variants(q, build)
return results
except HTTPException:
raise
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))
@router.get("/franklin/{variant_query:path}")
async def get_franklin_assessment(variant_query: str, reference_version: str = "hg19"):
"""Fetch Franklin-like tab data for a given variant query."""
try:
return await get_franklin_variant_assessment(variant_query, reference_version=reference_version)
except ValueError as e:
raise HTTPException(status_code=400, detail=str(e))
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))
@router.post("/franklin/proxy")
async def franklin_proxy_endpoint(request: FranklinProxyRequest):
"""Generic Franklin API proxy for /api/* endpoints."""
try:
return await proxy_franklin_api(
endpoint=request.endpoint,
method=request.method,
payload=request.payload,
params=request.params,
)
except ValueError as e:
raise HTTPException(status_code=400, detail=str(e))
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))
@router.get("/{variant_id}", response_model=VariantResponse)
async def get_variant(variant_id: str):
"""Get detailed information about a specific variant"""
try:
result = await annotate_variant(variant_id)
if not result:
raise HTTPException(status_code=404, detail="Variant not found")
return result
except HTTPException:
raise
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))
|