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..206b08a 100644 --- a/blast/app.py +++ b/blast/app.py @@ -1,71 +1,129 @@ -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"} + +@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}") -# this get json params -@app.post("/run_blast") -async def run_blast(request: Request): - request = await request.json() +@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 - - + raise HTTPException(status_code=400, detail=f"Command failed: {e.stderr}") + -if __name__ == '__main__': - import uvicorn +@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 + 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..f9a5379 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 /home/ala/BA/mydb:/blast/db --volume /mnt/databases:/blast/db/custom -p 6001:6001 blast_image \ No newline at end of file