File size: 1,585 Bytes
6dd9839
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/usr/bin/env python3
"""Minimal FastAPI app for the two website endpoints."""
from __future__ import annotations

import sys
from pathlib import Path

from fastapi import FastAPI, HTTPException
from pydantic import BaseModel, Field

API_DIR = Path(__file__).resolve().parent
sys.path.insert(0, str(API_DIR))

from common import load_env  # noqa: E402
from generate_rna_and_motif import generate_rna_and_motif  # noqa: E402
from score_binding import score_binding  # noqa: E402

load_env()
app = FastAPI(title="ProRiboGen API", version="0.1.0")


class GenerateRequest(BaseModel):
    protein: str = Field(..., description="氨基酸序列")
    p_id: str = "QUERY"
    num_sequences: int = 256
    length_bp: int = 80
    run_motif: bool = True


class ScoreRequest(BaseModel):
    protein: str
    rna: str
    p_id: str = "QUERY"


@app.get("/health")
def health() -> dict:
    return {"status": "ok"}


@app.post("/v1/generate")
def api_generate(req: GenerateRequest) -> dict:
    try:
        return generate_rna_and_motif(
            req.protein,
            p_id=req.p_id,
            num_sequences=req.num_sequences,
            length_bp=req.length_bp,
            run_motif=req.run_motif,
        )
    except Exception as exc:  # noqa: BLE001
        raise HTTPException(status_code=500, detail=str(exc)) from exc


@app.post("/v1/score")
def api_score(req: ScoreRequest) -> dict:
    try:
        return score_binding(req.protein, req.rna, p_id=req.p_id)
    except Exception as exc:  # noqa: BLE001
        raise HTTPException(status_code=500, detail=str(exc)) from exc