Skip to content
Merged
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
50 changes: 50 additions & 0 deletions src/mavedb/routers/alphafold.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
from fastapi import APIRouter, HTTPException
import httpx
import xml.etree.ElementTree as ET
import re

from mavedb.lib.logging.logged_route import LoggedRoute

ALPHAFOLD_BASE = "https://alphafold.ebi.ac.uk/files/"

router = APIRouter(
prefix="/api/v1",
tags=["alphafold files"],
responses={404: {"description": "Not found"}},
route_class=LoggedRoute,
)

@router.get("/alphafold-files/version")
async def proxy_alphafold_index():
"""
Proxy the AlphaFold files index (XML document).
"""
async with httpx.AsyncClient(follow_redirects=True, timeout=30) as client:
resp = await client.get(ALPHAFOLD_BASE, headers={"Accept": "application/xml"})
if resp.status_code != 200:
raise HTTPException(status_code=resp.status_code, detail="Upstream error fetching AlphaFold files index")

# parse XML response
try:
root = ET.fromstring(resp.content)

# Detect default namespace
if root.tag.startswith("{"):
ns_uri = root.tag.split("}", 1)[0][1:]
ns = {"x": ns_uri}
next_marker_tag = "x:NextMarker"
else:
ns = {}
next_marker_tag = "NextMarker"

next_marker_el = root.find(next_marker_tag, ns)
next_marker = next_marker_el.text if next_marker_el is not None else None

match = re.search(r"model_(v\d+)\.pdb$", next_marker, re.IGNORECASE)
if not match:
raise HTTPException(status_code=500, detail="Malformed AlphaFold PDB ID in XML")
version = match.group(1)
return {"version": version.lower()}

except ET.ParseError as e:
raise HTTPException(status_code=502, detail=f"Failed to parse upstream XML: {e}")
2 changes: 2 additions & 0 deletions src/mavedb/server_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
taxonomies,
users,
variants,
alphafold,
)

logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -106,6 +107,7 @@
app.include_router(taxonomies.router)
app.include_router(users.router)
app.include_router(variants.router)
app.include_router(alphafold.router)


@app.exception_handler(PermissionException)
Expand Down