forked from Discngine/fpocket
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
45 lines (38 loc) · 1.39 KB
/
main.py
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
from fastapi import FastAPI, File
from fastapi.responses import Response
from typing import Annotated
from tempfile import NamedTemporaryFile, TemporaryDirectory
from subprocess import check_output
from io import BytesIO
from pathlib import Path
from logging import getLogger
app = FastAPI()
log = getLogger("uvicorn.error")
@app.post("/")
async def create_item(pdb: Annotated[bytes, File()], ligand: str):
with TemporaryDirectory() as tmpdir:
with (
NamedTemporaryFile(dir=tmpdir, suffix=".pdb") as fp,
NamedTemporaryFile(dir=tmpdir, suffix=".txt") as tmp,
):
fp.write(pdb)
tmp.write(f"{fp.name}\t{ligand}".encode())
tmp.flush()
log.info(
"Input file content: '%s'",
Path(tmp.name).read_text(),
)
result = check_output(
["dpocket", "-f", tmp.name, "-v", "10000", "-d", "5.0"],
cwd=tmpdir,
)
log.info(
"Output:\n\t\t%s",
result.decode().replace("\n", "\n\t\t"),
)
log.info(
"Files in tempdir: \n\t\t%s",
"\n\t\t".join(map(str, Path(tmpdir).rglob("*"))),
)
data = BytesIO((Path(tmpdir) / "dpout_explicitp.txt").read_bytes())
return Response(content=data.getvalue(), media_type="text/plain")