Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

resolve rapid species home #10

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions app/api/resources/rapid_view.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
from fastapi import APIRouter, Request
from fastapi.responses import RedirectResponse
import logging
from core.logging import InterceptHandler
from core.config import ENSEMBL_URL
from api.utils.metadata import get_genome_id_from_assembly_accession_id

logging.getLogger().handlers = [InterceptHandler()]

router = APIRouter()


# Resolve species home
# https://rapid.ensembl.org/Homo_sapiens_GCA_009914755.4/
# https://rapid.ensembl.org/Homo_sapiens_GCA_009914755.4/Info/Index
@router.get("/{species_url_name}/{subpath:path}", name="Rapid Species Home")
async def resolve_species(request: Request, species_url_name: str, subpath: str):
_, accession_id = species_url_name.split("_GCA_")
assembly_accession_id = "GCA_" + accession_id
genome_object = get_genome_id_from_assembly_accession_id(assembly_accession_id)
if genome_object:
genome_id = genome_object.get("genomeUuid")
url = f"{ENSEMBL_URL}/species/{genome_id}"
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we get genome tag from the metadata api, by the way?

I suppose none of the species on rapid will have a genome tag; but still, what if?

return RedirectResponse(url)


@router.get("/", name="Rapid Home")
async def resolve_home(request: Request):
return RedirectResponse(ENSEMBL_URL)
3 changes: 2 additions & 1 deletion app/api/resources/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@

from fastapi import APIRouter

from api.resources import resolver_view
from api.resources import resolver_view, rapid_view

router = APIRouter()

router.include_router(resolver_view.router, tags=["resolver"], prefix="/id")
router.include_router(rapid_view.router, tags=["rapid"], prefix="/rapid")
19 changes: 19 additions & 0 deletions app/api/utils/metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,22 @@ def get_metadata(matches: List[SearchMatch] = []):
return None

return metadata_results


def get_genome_id_from_assembly_accession_id(accession_id: str):

try:
session = requests.Session()
metadata_api_url = (
f"{ENSEMBL_URL}/api/metadata/genome?assembly_accession_id={accession_id}"
)
with session.get(url=metadata_api_url) as response:
response.raise_for_status()
response_json = response.json()
return response_json
except requests.exceptions.HTTPError as HTTPError:
logger.error(f"HTTPError: {HTTPError}")
return None
except Exception as e:
logger.exception(e)
return None