Skip to content

Commit

Permalink
fixed errors- functions for easy clustering
Browse files Browse the repository at this point in the history
  • Loading branch information
alacheim committed Jan 14, 2025
1 parent 8dd6b62 commit 2241ccb
Showing 1 changed file with 19 additions and 75 deletions.
94 changes: 19 additions & 75 deletions mmseqs2/app.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
from fastapi import FastAPI, HTTPException, Request
from starlette.responses import FileResponse
import logging

import subprocess
import os
import shutil

app = FastAPI()
logging.basicConfig(level=logging.INFO)
Expand Down Expand Up @@ -60,21 +62,6 @@ def validate_sequence(sequence: str) -> bool:

print(f"FASTA file created: {filename}")

def create_queryDB_from_seq(filename):

command = [
"mmseqs", "createdb",
filename,
filename.replace('fasta', '') + ".db"
]

try:
subprocess.run(command, check=True)

except subprocess.CalledProcessError as e:
raise HTTPException(status_code=600, detail=str(e))


@app.get("/")
async def read_root():
return {"message": "Welcome to the MMSeqs2 API!"}
Expand All @@ -91,60 +78,14 @@ def help():
except subprocess.CalledProcessError as e:
raise HTTPException(status_code=400, detail=f"Command failed {e.stderr}")


# @app.post("/easycluster")
# async def easycluster(request: Request):

# logging.basicConfig(level=logging.INFO)
# logger = logging.getLogger(__name__)

# data = await request.json()
# logger.info(f"Received request data: {data}")

# print(f" Received request to run mmseqs with data: {data}")

# query_filename =f"in.fasta"
# result_filename = f"out"

# # Clear or create result file
# open(result_filename, 'w').close()

# # Create the fasta file from the query string
# create_fastas_file_from_seq(data['query'], query_filename)

# # Run the mmseqs2 search command
# # command = [
# # "mmseqs", "easy-cluster",
# # query_filename,
# # result_filename,
# # "--min-seq-id", request['min_seq_id'],
# # "-c", request['coverage'],
# # "--cov-mode", request['cov_mode'],
# # "tmp"
# # ]
# command = ["mmseqs", "easy-cluster", query_filename, result_filename, "tmp"]

# logger.info(f"Running command: {' '.join(command)}")

# try:
# subprocess.run(command, check=True)
# except subprocess.CalledProcessError as e:
# logger.error(f"MMSeqs command failed: {e}")
# raise HTTPException(status_code=500, detail=str(e))

# with open(result_filename, 'r') as file:
# result = file.read()

# return result

@app.post("/easycluster")
async def easycluster(request: Request):
data = await request.json()
logger.info(f"Received request data: {data}")

BASE_DIR = "/app"
query_filename = os.path.join(BASE_DIR, "in.fasta")
result_filename = os.path.join(BASE_DIR, "output.out")
result_filename = os.path.join(BASE_DIR, "output")
tmp_dir = os.path.join(BASE_DIR, "tmp")

os.makedirs(tmp_dir, exist_ok=True)
Expand All @@ -154,29 +95,32 @@ async def easycluster(request: Request):
create_fastas_file_from_seq(data['query'], query_filename)

# Run the mmseqs2 command
command = ["mmseqs", "easy-cluster", query_filename, result_filename, tmp_dir]
command = [
"mmseqs",
"easy-cluster",
query_filename,
result_filename,
'--min-seq-id', str(data['min_seq_id']),
'-c', str(data['coverage']),
'--cov-mode', str(data['cov_mode']),
tmp_dir]
logger.info(f"Running command: {' '.join(command)}")

try:
results = subprocess.run(command, capture_output=True, text=True, check=True)
logger.info(f"Command output: {results.stdout}")
result = subprocess.run(command, capture_output=True, text=True, check=True)
logger.info(f"Command output: {result.stdout}")

except subprocess.CalledProcessError as e:
logger.error(f"Command failed with return code {e.returncode}")
logger.error(f"STDOUT: {e.stdout}")
logger.error(f"STDERR: {e.stderr}")
raise HTTPException(status_code=500, detail=f"Command failed: {e.stderr}")

# with open(f"/app/{result_filename}.out_all_seqs.fasta", 'r') as file:
# result = file.read()

with open(result_filename, 'r') as file:
print(f"Reading result file: {result_filename}")
result = file.read()

return {"result": result}

# add easy search
with open("/app/output_all_seqs.fasta", 'r') as file:
logger.info(f"Reading result file: /app/output_all_seqs.fasta")
result = file.read()

return result

if __name__ == '__main__':
import uvicorn
Expand Down

0 comments on commit 2241ccb

Please sign in to comment.