from fastapi import APIRouter, HTTPException, Query from typing import Optional from pydantic import BaseModel router = APIRouter() class GeneInfo(BaseModel): symbol: str name: Optional[str] = None chromosome: Optional[str] = None variant_count: int = 0 @router.get("", response_model=list[GeneInfo]) async def list_genes( query: Optional[str] = None, limit: int = Query(default=50, ge=1, le=200), ): """List genes, optionally filtered by query""" genes = [ {"symbol": "BRCA1", "name": "BRCA1 DNA repair associated", "chromosome": "17", "variant_count": 1250}, {"symbol": "BRCA2", "name": "BRCA2 DNA repair associated", "chromosome": "13", "variant_count": 980}, {"symbol": "TP53", "name": "Tumor protein p53", "chromosome": "17", "variant_count": 2100}, {"symbol": "EGFR", "name": "Epidermal growth factor receptor", "chromosome": "7", "variant_count": 1850}, {"symbol": "KRAS", "name": "KRAS proto-oncogene GTPase", "chromosome": "12", "variant_count": 720}, ] if query: genes = [g for g in genes if query.upper() in g["symbol"].upper()] return genes[:limit] @router.get("/{gene_symbol}", response_model=GeneInfo) async def get_gene(gene_symbol: str): """Get information about a specific gene""" genes_db = { "BRCA1": {"symbol": "BRCA1", "name": "BRCA1 DNA repair associated", "chromosome": "17", "variant_count": 1250}, "BRCA2": {"symbol": "BRCA2", "name": "BRCA2 DNA repair associated", "chromosome": "13", "variant_count": 980}, "TP53": {"symbol": "TP53", "name": "Tumor protein p53", "chromosome": "17", "variant_count": 2100}, } gene = genes_db.get(gene_symbol.upper()) if not gene: raise HTTPException(status_code=404, detail="Gene not found") return gene