Spaces:
Sleeping
Sleeping
| """ | |
| Biomarkers List Endpoint | |
| """ | |
| import json | |
| from datetime import datetime | |
| from pathlib import Path | |
| from fastapi import APIRouter, HTTPException | |
| from app.models.schemas import BiomarkerInfo, BiomarkerReferenceRange, BiomarkersListResponse | |
| router = APIRouter(prefix="/api/v1", tags=["biomarkers"]) | |
| async def list_biomarkers(): | |
| """ | |
| Get list of all supported biomarkers with reference ranges. | |
| Returns comprehensive information about all 24 biomarkers: | |
| - Name and unit | |
| - Normal reference ranges (gender-specific if applicable) | |
| - Critical thresholds | |
| - Clinical significance | |
| Useful for: | |
| - Frontend validation | |
| - Understanding what biomarkers can be analyzed | |
| - Getting reference ranges for display | |
| """ | |
| try: | |
| # Load biomarker references | |
| config_path = Path(__file__).parent.parent.parent.parent / "config" / "biomarker_references.json" | |
| with open(config_path, encoding="utf-8") as f: | |
| config_data = json.load(f) | |
| biomarkers_data = config_data.get("biomarkers", {}) | |
| biomarkers_list = [] | |
| for name, info in biomarkers_data.items(): | |
| # Parse reference range | |
| normal_range_data = info.get("normal_range", {}) | |
| if "male" in normal_range_data or "female" in normal_range_data: | |
| # Gender-specific ranges | |
| reference_range = BiomarkerReferenceRange( | |
| min=None, max=None, male=normal_range_data.get("male"), female=normal_range_data.get("female") | |
| ) | |
| else: | |
| # Universal range | |
| reference_range = BiomarkerReferenceRange( | |
| min=normal_range_data.get("min"), max=normal_range_data.get("max"), male=None, female=None | |
| ) | |
| biomarker_info = BiomarkerInfo( | |
| name=name, | |
| unit=info.get("unit", ""), | |
| normal_range=reference_range, | |
| critical_low=info.get("critical_low"), | |
| critical_high=info.get("critical_high"), | |
| gender_specific=info.get("gender_specific", False), | |
| description=info.get("description", ""), | |
| clinical_significance=info.get("clinical_significance", {}), | |
| ) | |
| biomarkers_list.append(biomarker_info) | |
| return BiomarkersListResponse( | |
| biomarkers=biomarkers_list, total_count=len(biomarkers_list), timestamp=datetime.now().isoformat() | |
| ) | |
| except FileNotFoundError as exc: | |
| raise HTTPException(status_code=500, detail="Biomarker configuration file not found") from exc | |
| except Exception as e: | |
| raise HTTPException(status_code=500, detail=f"Failed to load biomarkers: {e!s}") from e | |