From 342130ff6c34f6f54bf3330fea19ac3306573e11 Mon Sep 17 00:00:00 2001 From: alacheim Date: Fri, 6 Dec 2024 15:41:39 +0000 Subject: [PATCH 1/3] updated blast docker --- blast/Dockerfile | 3 - blast/app.py | 123 ++++++++++++++++++++---------------- blast/reload_development.sh | 8 +-- 3 files changed, 73 insertions(+), 61 deletions(-) diff --git a/blast/Dockerfile b/blast/Dockerfile index 4dc9f89..eb601c8 100644 --- a/blast/Dockerfile +++ b/blast/Dockerfile @@ -10,8 +10,5 @@ COPY app.py /usr/local/bin/app.py # Set the working directory WORKDIR /usr/local/bin -# Expose the port the server will run on -EXPOSE 6001 - # Run the Python server script CMD ["python3", "app.py"] diff --git a/blast/app.py b/blast/app.py index 08bc9dc..bd2d573 100644 --- a/blast/app.py +++ b/blast/app.py @@ -1,71 +1,86 @@ -from fastapi import FastAPI, HTTPException, Request -from pydantic import BaseModel +from fastapi import FastAPI, HTTPException, Request, UploadFile import subprocess import os -import uuid -from typing import Optional -import logging app = FastAPI() -logger = logging.getLogger(__name__) -class BlastRequest(BaseModel): - tool: str - query: str - db: str - evalue: str - outfmt: str - - -## ENDPOINTS -------------------------- - -def create_fastas_file_from_seq(seq, filename): - with open(filename, 'w') as file: +def create_fasta_files_from_seq(seq, filename): + with open(filename, 'w') as file: file.write(f">seq\n{seq}\n") @app.get("/") -async def read_root(): - return {"message": "Welcome to the BLAST API!"} +async def read_root(): + return {"message": "Welcome to the BLAST API"} -# this get json params -@app.post("/run_blast") -async def run_blast(request: Request): - request = await request.json() +@app.get("/blastp_help") +def blastp_help(): + try: + result = subprocess.run( + ["blastp", "--help"], + capture_output=True, + text=True, + ) + return {"help": result.stdout} + except subprocess.CalledProcessError as e: + raise HTTPException(status_code=400, detail=f"Command failed: {e.stderr}") + +@app.get("/blastn_help") +def blastn_help(): + try: + result = subprocess.run( + ["blastn", "--help"], + capture_output=True, + text=True, + ) + return {"help": result.stdout} + except subprocess.CalledProcessError as e: + raise HTTPException(status_code=400, detail=f"Command failed: {e.stderr}") +@app.post("/blastp") +async def run_blastp(request: Request): + data = await request.json() + + print(f" Received request to run blastp with data: {data}") + query_filename = f"in.fasta" result_filename = f"out.out" - # create empty file + + # Clear or create result file open(result_filename, 'w').close() - - # Create the FASTA file - create_fastas_file_from_seq(request['query'], query_filename) - - # Run the BLAST command - command = [ - request['tool'], - '-query', query_filename, - '-db', request['db'], - '-evalue', request['evalue'], - '-outfmt', request['outfmt'], - '-num_threads', request['num_threads'], - '-out', result_filename, - '-max_target_seqs', '10000' - ] - + + # Create the fasta file from the query string + create_fasta_files_from_seq(data['query'], query_filename) + try: - subprocess.run(command, check=True) + command = [ + "blastp", + '-query', query_filename, + '-db', data['db'], + '-evalue', str(data['evalue']), + '-outfmt', str(data['outfmt']), + '-num_threads', str(data['num_threads']), + '-out', result_filename, + '-max_target_seqs', str(data['max_target_seqs']) + ] + + result = subprocess.run( + command, + capture_output=True, + check=True, + text=True + ) + + with open(result_filename, 'r') as file: + result_data = file.read() + + return {"result": result_data} + except subprocess.CalledProcessError as e: - raise HTTPException(status_code=500, detail=str(e)) - - - with open(result_filename, 'r') as file: - result = file.read() - - return result - - - -if __name__ == '__main__': - import uvicorn + raise HTTPException(status_code=400, detail=f"Command failed: {e.stderr}") + +if __name__ == "__main__": + import uvicorn + uvicorn.run("app:app", host="0.0.0.0", port=6001, reload=True) + diff --git a/blast/reload_development.sh b/blast/reload_development.sh index 5409855..4a06b24 100644 --- a/blast/reload_development.sh +++ b/blast/reload_development.sh @@ -1,4 +1,4 @@ -sudo docker stop blast_docker -sudo docker remove blast_docker -sudo docker build --no-cache -t blast_docker . -sudo docker run --name blast_docker --volume /mnt/databases:/blast/blastdb -p 6001:6001 blast_docker \ No newline at end of file +sudo docker stop blast +sudo docker remove blast +sudo docker build --no-cache -t blast_image . +sudo docker run --name blast --volume /mnt/databases:/blast/blastdb -p 6001:6001 blast_image \ No newline at end of file From 614ac11245552148d6c0eb008fa8efa1877b4092 Mon Sep 17 00:00:00 2001 From: alacheim Date: Thu, 12 Dec 2024 10:01:13 +0000 Subject: [PATCH 2/3] set volume as testing db --- blast/reload_development.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/blast/reload_development.sh b/blast/reload_development.sh index 4a06b24..be8cbb7 100644 --- a/blast/reload_development.sh +++ b/blast/reload_development.sh @@ -1,4 +1,4 @@ sudo docker stop blast sudo docker remove blast sudo docker build --no-cache -t blast_image . -sudo docker run --name blast --volume /mnt/databases:/blast/blastdb -p 6001:6001 blast_image \ No newline at end of file +sudo docker run --name blast --volume /home/ala/BA/mydb:/blast/db -p 6001:6001 blast_image \ No newline at end of file From 7e7b99bcd995daa85e6273dfdf4b752743efdd4a Mon Sep 17 00:00:00 2001 From: Niklas Abraham - INFlux Date: Thu, 12 Dec 2024 14:03:43 +0000 Subject: [PATCH 3/3] added blastn added second path in reload_devlopment --- blast/app.py | 43 +++++++++++++++++++++++++++++++++++++ blast/reload_development.sh | 2 +- 2 files changed, 44 insertions(+), 1 deletion(-) diff --git a/blast/app.py b/blast/app.py index bd2d573..206b08a 100644 --- a/blast/app.py +++ b/blast/app.py @@ -78,6 +78,49 @@ async def run_blastp(request: Request): except subprocess.CalledProcessError as e: raise HTTPException(status_code=400, detail=f"Command failed: {e.stderr}") + +@app.post("/blastn") +async def run_blastn(request: Request): + data = await request.json() + + print(f" Received request to run blastn with data: {data}") + + query_filename = f"in.fasta" + result_filename = f"out.out" + + # Clear or create result file + open(result_filename, 'w').close() + + # Create the fasta file from the query string + create_fasta_files_from_seq(data['query'], query_filename) + + try: + command = [ + "blastn", + '-query', query_filename, + '-db', data['db'], + '-evalue', str(data['evalue']), + '-outfmt', str(data['outfmt']), + '-num_threads', str(data['num_threads']), + '-out', result_filename, + '-max_target_seqs', str(data['max_target_seqs']) + ] + + result = subprocess.run( + command, + capture_output=True, + check=True, + text=True + ) + + with open(result_filename, 'r') as file: + result_data = file.read() + + return {"result": result_data} + + except subprocess.CalledProcessError as e: + raise HTTPException(status_code=400, detail=f"Command failed: {e.stderr}") + if __name__ == "__main__": import uvicorn diff --git a/blast/reload_development.sh b/blast/reload_development.sh index be8cbb7..f9a5379 100644 --- a/blast/reload_development.sh +++ b/blast/reload_development.sh @@ -1,4 +1,4 @@ sudo docker stop blast sudo docker remove blast sudo docker build --no-cache -t blast_image . -sudo docker run --name blast --volume /home/ala/BA/mydb:/blast/db -p 6001:6001 blast_image \ No newline at end of file +sudo docker run --name blast --volume /home/ala/BA/mydb:/blast/db --volume /mnt/databases:/blast/db/custom -p 6001:6001 blast_image \ No newline at end of file