Skip to content

Commit

Permalink
Merge pull request #4 from PyEED/blast_graph_db
Browse files Browse the repository at this point in the history
Blast graph db
  • Loading branch information
NiklasAbraham authored Jan 16, 2025
2 parents 3aabaf1 + 7e7b99b commit f533666
Show file tree
Hide file tree
Showing 3 changed files with 115 additions and 60 deletions.
3 changes: 0 additions & 3 deletions blast/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
164 changes: 111 additions & 53 deletions blast/app.py
Original file line number Diff line number Diff line change
@@ -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)

8 changes: 4 additions & 4 deletions blast/reload_development.sh
Original file line number Diff line number Diff line change
@@ -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
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

0 comments on commit f533666

Please sign in to comment.