Skip to content

Commit

Permalink
Merge pull request #157 from databio/bedset_creation
Browse files Browse the repository at this point in the history
Added endpoint and ui modal that creates bedsets
  • Loading branch information
khoroshevskyi authored Nov 26, 2024
2 parents ea59540 + d669d63 commit 94be5c4
Show file tree
Hide file tree
Showing 12 changed files with 713 additions and 59 deletions.
4 changes: 4 additions & 0 deletions bedhost/data_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,3 +77,7 @@ class BaseListResponse(BaseModel):
limit: int
offset: int
results: list


class CreateBEDsetRequest(BaseModel):
registry_path: str
21 changes: 21 additions & 0 deletions bedhost/routers/bed_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
BedStatsModel,
TokenizedBedResponse,
TokenizedPathResponse,
QdrantSearchResult,
)
from fastapi import APIRouter, File, HTTPException, Query, UploadFile
from fastapi.responses import PlainTextResponse
Expand Down Expand Up @@ -376,6 +377,26 @@ async def text_to_bed_search(query, limit: int = 10, offset: int = 0):
# offset=offset,
# results=(results_sql.results + results_qdr.results)[0:limit],
# )
spaceless_query = query.replace(" ", "")
if len(spaceless_query) == 32 and spaceless_query == query:
try:
similar_results = bbagent.bed.get_neighbours(
query, limit=limit, offset=offset
)

if similar_results.results and offset == 0:

result = QdrantSearchResult(
id=query,
payload={},
score=1.0,
metadata=bbagent.bed.get(query),
)

similar_results.results.insert(0, result)
return similar_results
except Exception as _:
pass

results = bbagent.bed.text_to_bed_search(
query,
Expand Down
62 changes: 62 additions & 0 deletions bedhost/routers/bedset_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@
BedSetPlots,
BedSetStats,
)
from pephubclient.helpers import is_registry_path, unwrap_registry_path
from fastapi import APIRouter, HTTPException, Request, Response

from ..const import EXAMPLE_BEDSET, PKG_NAME
from ..main import bbagent
from ..data_models import CreateBEDsetRequest
from ..utils import zip_pep

router = APIRouter(prefix="/v1/bedset", tags=["bedset"])
Expand Down Expand Up @@ -183,3 +185,63 @@ async def get_trackDb_file_bedset(bedset_id: str):
)

return Response(trackDb_txt, media_type="text/plain")


@router.post(
"/create/",
description="Create a new bedset by providing registry path to the PEPhub project",
)
async def create_bedset(bedset: CreateBEDsetRequest):
"""
Create a new bedset
"""
# Validate the PEPhub project string
if not is_registry_path(bedset.registry_path):
raise HTTPException(status_code=406, detail="Invalid registry path")

project_reg_path = unwrap_registry_path(bedset.registry_path)

if project_reg_path.namespace not in ["databio", "bedbase", "pepkit"]:
raise HTTPException(status_code=403, detail="User is not in admin list")

try:
project = bbagent.config.phc.load_project(bedset.registry_path)
except Exception as _:
raise HTTPException(
status_code=404, detail=f"Project: '{bedset.registry_path}' not found"
)

bedfiles_list = [
bedfile_id.get("record_identifier") or bedfile_id.sample_name
for bedfile_id in project.samples
]

try:
bbagent.bedset.get(identifier=project.name)
raise HTTPException(
status_code=409,
detail=f"BEDset with identifier {project.name} already exists",
)
except BedSetNotFoundError as _:
pass

try:
bbagent.bedset.create(
identifier=project.name,
name=project.name,
bedid_list=bedfiles_list,
statistics=True,
description=project.description,
annotation={
"source": project.config.get("source", ""),
"author": project.config.get("author", project_reg_path.namespace),
},
no_fail=False,
overwrite=False,
)
except Exception as err:
raise HTTPException(
status_code=400, detail=f"Unable to create bedset. Error: {err}"
)

return {"status": "success"}
Loading

0 comments on commit 94be5c4

Please sign in to comment.