joduor commited on
Commit
00e9dea
·
verified ·
1 Parent(s): 724adb7

Fix pLDDT: fetch per-residue scores from AlphaFold confidence JSON endpoint (alphafold_client.py)

Browse files
Files changed (1) hide show
  1. 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 extract_plddt_scores(alphafold_data: dict) -> list[float]:
133
  """
134
- Extract per-residue pLDDT scores from AlphaFold summary.
135
- Falls back to a single average value if per-residue data unavailable.
 
136
  """
137
- scores = alphafold_data.get("confidenceScores") or []
138
- if not scores:
139
- avg = alphafold_data.get("confidenceAvgLocalScore") or \
140
- alphafold_data.get("plddt") or 0.0
141
- return [float(avg)]
142
- return [float(s) for s in scores]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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)]