File size: 1,707 Bytes
0e5520d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
62
63
64

from fastapi import FastAPI, UploadFile, File
from pydantic import BaseModel
import uvicorn
import os, uuid, pandas as pd, threading
from runners.genie3 import run_genie3
from runners.grnboost2 import run_grnboost2
from runners.scns import run_scns

app=FastAPI()
DATA="data"
INP=f"{DATA}/inputs"
OUT=f"{DATA}/outputs"
os.makedirs(INP,exist_ok=True)
os.makedirs(OUT,exist_ok=True)

class RunRequest(BaseModel):
    dataset_id:str
    algorithm:str
    params:dict

jobs={}

def run_job(job_id,req):
    infile=f"{INP}/{req.dataset_id}.csv"
    outfile=f"{OUT}/{job_id}_rankedEdges.csv"
    if req.algorithm=="GENIE3":
        run_genie3(infile,outfile)
    elif req.algorithm=="GRNBOOST2":
        run_grnboost2(infile,outfile)
    elif req.algorithm=="SCNS":
        run_scns(infile,outfile)
    jobs[job_id]["status"]="done"

@app.post("/api/upload-dataset")
async def upload_dataset(file:UploadFile=File(...)):
    df=pd.read_csv(file.file)
    path=f"{INP}/{file.filename}"
    df.to_csv(path,index=False)
    return {"status":"ok","dataset":file.filename}

@app.get("/api/datasets")
def list_datasets():
    return {"datasets":[f for f in os.listdir(INP) if f.endswith(".csv")]}

@app.post("/api/run")
def run(req:RunRequest):
    job_id=str(uuid.uuid4())
    jobs[job_id]={"status":"running"}
    t=threading.Thread(target=run_job,args=(job_id,req))
    t.start()
    return {"job_id":job_id}

@app.get("/api/jobs/{job_id}")
def job_status(job_id:str):
    return jobs.get(job_id,{"status":"unknown"})

@app.get("/api/results/{job_id}")
def results(job_id:str):
    path=f"{OUT}/{job_id}_rankedEdges.csv"
    if not os.path.exists(path):
        return {"error":"not ready"}
    return {"file":path}