Spaces:
Runtime error
Runtime error
Download app/api/explorer.py from Ukandu/webgenie_api: direct link, hf CLI and curl.
- Browser
- Download file 5.34 kB
-
https://huggingface.co/spaces/Ukandu/webgenie_api/resolve/main/app/api/explorer.py
- Command line
-
hf download hf://spaces/Ukandu/webgenie_api/app/api/explorer.py
-
curl -L -o explorer.py https://huggingface.co/spaces/Ukandu/webgenie_api/resolve/main/app/api/explorer.py
5.34 kB
| from fastapi import APIRouter, HTTPException | |
| from typing import List, Dict, Optional | |
| from pydantic import BaseModel | |
| router = APIRouter(prefix="/explorer", tags=["Explorer"]) | |
| # ----------------------------- | |
| # DATA MODELS | |
| # ----------------------------- | |
| class DatasetMeta(BaseModel): | |
| nGenes: int | |
| nCells: int | |
| isSingleCell: bool | |
| isBulk: bool | |
| sparsity: Optional[float] = None | |
| hasTFList: Optional[bool] = None | |
| isTimeSeries: Optional[bool] = None | |
| class Dataset(BaseModel): | |
| id: str | |
| name: str | |
| organism: str | |
| description: str | |
| meta: DatasetMeta | |
| class AlgoProfile(BaseModel): | |
| name: str | |
| minGenes: Optional[int] = None | |
| maxGenes: Optional[int] = None | |
| requiresSingleCell: Optional[bool] = None | |
| requiresBulk: Optional[bool] = None | |
| requiresTFList: Optional[bool] = None | |
| requiresTimeSeries: Optional[bool] = None | |
| undirected: Optional[bool] = None | |
| maxCells: Optional[int] = None | |
| minCells: Optional[int] = None | |
| dynamic: Optional[bool] = None | |
| edgeSparsity: float | |
| # ----------------------------- | |
| # MOCK DATASETS (replace later with HF) | |
| # ----------------------------- | |
| DATASETS = [ | |
| { | |
| "id": "dyn-bf", | |
| "name": "dyn-BF", | |
| "organism": "Synthetic", | |
| "description": "Bifurcating synthetic GRN", | |
| "meta": { | |
| "nGenes": 100, | |
| "nCells": 500, | |
| "isSingleCell": True, | |
| "isBulk": False, | |
| "hasTFList": True, | |
| "isTimeSeries": True, | |
| "sparsity": 0.7, | |
| }, | |
| }, | |
| { | |
| "id": "gsd", | |
| "name": "GSD", | |
| "organism": "Human", | |
| "description": "Gonadal sex determination GRN", | |
| "meta": { | |
| "nGenes": 2000, | |
| "nCells": 300, | |
| "isSingleCell": True, | |
| "isBulk": False, | |
| "hasTFList": True, | |
| "isTimeSeries": False, | |
| }, | |
| }, | |
| ] | |
| ALGORITHMS: List[AlgoProfile] = [ | |
| AlgoProfile(name="GENIE3", requiresTFList=True, maxGenes=20000, edgeSparsity=0.88), | |
| AlgoProfile(name="GRNBoost2", requiresSingleCell=True, requiresTFList=True, maxGenes=25000, edgeSparsity=0.92), | |
| AlgoProfile(name="Arboreto", requiresSingleCell=True, requiresTFList=True, maxGenes=30000, edgeSparsity=0.93), | |
| AlgoProfile(name="PIDC", requiresSingleCell=True, minGenes=50, maxGenes=8000, undirected=True, edgeSparsity=0.65), | |
| AlgoProfile(name="ARACNE", requiresBulk=True, minGenes=50, maxGenes=10000, undirected=True, edgeSparsity=0.60), | |
| AlgoProfile(name="Pearson", minGenes=10, undirected=True, edgeSparsity=0.95), | |
| AlgoProfile(name="Spearman", minGenes=10, undirected=True, edgeSparsity=0.95), | |
| AlgoProfile(name="SCODE", requiresSingleCell=True, requiresTimeSeries=True, maxGenes=5000, dynamic=True, edgeSparsity=0.55), | |
| AlgoProfile(name="SINGE", requiresSingleCell=True, requiresTimeSeries=True, dynamic=True, edgeSparsity=0.50), | |
| AlgoProfile(name="LEAP", requiresSingleCell=True, requiresTimeSeries=True, dynamic=True, edgeSparsity=0.70), | |
| AlgoProfile(name="GRNVBEM", requiresSingleCell=True, maxGenes=8000, edgeSparsity=0.75), | |
| AlgoProfile(name="GRISLI", requiresSingleCell=True, requiresTimeSeries=True, dynamic=True, edgeSparsity=0.60), | |
| AlgoProfile(name="SNS", requiresSingleCell=True, maxGenes=10000, edgeSparsity=0.72), | |
| ] | |
| # ----------------------------- | |
| # CORE LOGIC (converted from React) | |
| # ----------------------------- | |
| def is_algorithm_compatible(algo: AlgoProfile, dataset: DatasetMeta) -> bool: | |
| if algo.requiresSingleCell and not dataset.isSingleCell: | |
| return False | |
| if algo.requiresBulk and not dataset.isBulk: | |
| return False | |
| if algo.requiresTFList and not dataset.hasTFList: | |
| return False | |
| if algo.requiresTimeSeries and not dataset.isTimeSeries: | |
| return False | |
| if algo.minGenes and dataset.nGenes < algo.minGenes: | |
| return False | |
| if algo.maxGenes and dataset.nGenes > algo.maxGenes: | |
| return False | |
| if algo.minCells and dataset.nCells < algo.minCells: | |
| return False | |
| if algo.maxCells and dataset.nCells > algo.maxCells: | |
| return False | |
| return True | |
| # ----------------------------- | |
| # ROUTES | |
| # ----------------------------- | |
| def list_datasets(): | |
| return DATASETS | |
| def get_dataset(dataset_id: str): | |
| dataset = next((d for d in DATASETS if d["id"] == dataset_id), None) | |
| if not dataset: | |
| raise HTTPException(status_code=404, detail="Dataset not found") | |
| return dataset | |
| def list_algorithms(): | |
| return ALGORITHMS | |
| def compatible_algorithms(dataset_id: str): | |
| dataset = next((d for d in DATASETS if d["id"] == dataset_id), None) | |
| if not dataset: | |
| raise HTTPException(status_code=404, detail="Dataset not found") | |
| meta = DatasetMeta(**dataset["meta"]) | |
| return [ | |
| algo for algo in ALGORITHMS | |
| if is_algorithm_compatible(algo, meta) | |
| ] | |
| def check_compatibility(dataset: DatasetMeta): | |
| return { | |
| algo.name: is_algorithm_compatible(algo, dataset) | |
| for algo in ALGORITHMS | |
| } |