forked from EnzymeML/EnzymeML_JupyterLab
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from PyEED/blast_graph_db
Blast graph db
- Loading branch information
Showing
3 changed files
with
115 additions
and
60 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |