Spaces:
Sleeping
Sleeping
Fix pLDDT: fetch per-residue scores from AlphaFold confidence JSON endpoint (alphafold_client.py)
Browse files- alphafold_client.py +36 -9
alphafold_client.py
CHANGED
|
@@ -129,14 +129,41 @@ def extract_protein_metadata(uniprot_data: dict) -> dict[str, Any]:
|
|
| 129 |
}
|
| 130 |
|
| 131 |
|
| 132 |
-
def
|
| 133 |
"""
|
| 134 |
-
|
| 135 |
-
|
|
|
|
| 136 |
"""
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 129 |
}
|
| 130 |
|
| 131 |
|
| 132 |
+
async def fetch_plddt_scores(alphafold_data: dict) -> list[float]:
|
| 133 |
"""
|
| 134 |
+
Fetch per-residue pLDDT scores from AlphaFold confidence JSON file.
|
| 135 |
+
The summary endpoint doesn't include scores inline — they're in a separate file.
|
| 136 |
+
Falls back gracefully if unavailable.
|
| 137 |
"""
|
| 138 |
+
# Try the confidence JSON URL directly
|
| 139 |
+
conf_url = alphafold_data.get("confidenceUrl") or alphafold_data.get("confidence_url")
|
| 140 |
+
if not conf_url:
|
| 141 |
+
# Construct from entry ID and version
|
| 142 |
+
entry_id = alphafold_data.get("entryId", "")
|
| 143 |
+
version = alphafold_data.get("latestVersion", 4)
|
| 144 |
+
if entry_id:
|
| 145 |
+
conf_url = f"https://alphafold.ebi.ac.uk/files/{entry_id}-confidence_v{version}.json"
|
| 146 |
+
|
| 147 |
+
if conf_url:
|
| 148 |
+
try:
|
| 149 |
+
async with httpx.AsyncClient(timeout=TIMEOUT) as client:
|
| 150 |
+
r = await client.get(conf_url)
|
| 151 |
+
r.raise_for_status()
|
| 152 |
+
data = r.json()
|
| 153 |
+
scores = data.get("confidenceScore") or data if isinstance(data, list) else []
|
| 154 |
+
if scores:
|
| 155 |
+
return [float(s) for s in scores]
|
| 156 |
+
except Exception:
|
| 157 |
+
pass
|
| 158 |
+
|
| 159 |
+
# Final fallback: single average value from summary
|
| 160 |
+
avg = (alphafold_data.get("confidenceAvgLocalScore") or
|
| 161 |
+
alphafold_data.get("plddt") or 75.0)
|
| 162 |
+
return [float(avg)]
|
| 163 |
+
|
| 164 |
+
|
| 165 |
+
def extract_plddt_scores(alphafold_data: dict) -> list[float]:
|
| 166 |
+
"""Synchronous fallback — use fetch_plddt_scores() for accurate per-residue data."""
|
| 167 |
+
avg = (alphafold_data.get("confidenceAvgLocalScore") or
|
| 168 |
+
alphafold_data.get("plddt") or 75.0)
|
| 169 |
+
return [float(avg)]
|