Download app/api/variant.py from OpenVar/backend: direct link, hf CLI and curl.
- Browser
- Download file 2.92 kB
-
https://huggingface.co/spaces/OpenVar/backend/resolve/main/app/api/variant.py
- Command line
-
hf download hf://spaces/OpenVar/backend/app/api/variant.py
-
curl -L -o variant.py https://huggingface.co/spaces/OpenVar/backend/resolve/main/app/api/variant.py
2.92 kB
| 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 | |
| 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)) | |
| 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)) | |
| 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)) | |
| 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)) | |