Samad14 commited on
Commit
be32f12
·
1 Parent(s): 5b7cd01

fix(md): BioPython fallback crash on Atom.element.name ('str' object has no attribute 'name')

Browse files

atom.element is already the element string in BioPython; use it directly
for the SASA heavy-atom scan instead of elem.name. Regression test runs the
fallback on a mini PDB.

bioai-platform/backend/app/tools/md_sim.py CHANGED
@@ -664,12 +664,13 @@ def _run_biopython_analysis(pdb_path: str, pdb_id: str, mode: str, reason: str =
664
  heavy_coords: list[np.ndarray] = []
665
  heavy_radii_list: list[float] = []
666
  for atom in atoms:
667
- elem = atom.element
668
- name = elem.name if elem is not None else ""
 
669
  if name == "H":
670
  continue
671
  heavy_coords.append(atom.get_vector().get_array())
672
- heavy_radii_list.append(_VDW_RADII.get(name.upper(), 1.5))
673
  if heavy_coords:
674
  sasa_est = round(_sasa_shrake_ruger(
675
  np.array(heavy_coords), np.array(heavy_radii_list, dtype=np.float64)), 1)
 
664
  heavy_coords: list[np.ndarray] = []
665
  heavy_radii_list: list[float] = []
666
  for atom in atoms:
667
+ # BioPython Atom.element is already the element string (e.g. "C"),
668
+ # not an Element object — no .name attribute.
669
+ name = (atom.element or "").strip().upper()
670
  if name == "H":
671
  continue
672
  heavy_coords.append(atom.get_vector().get_array())
673
+ heavy_radii_list.append(_VDW_RADII.get(name, 1.5))
674
  if heavy_coords:
675
  sasa_est = round(_sasa_shrake_ruger(
676
  np.array(heavy_coords), np.array(heavy_radii_list, dtype=np.float64)), 1)
bioai-platform/backend/tests/test_md_sim_unit.py CHANGED
@@ -264,3 +264,32 @@ class TestLangevinTemperature:
264
  assert 250 < mean_temp < 350, f"mean temperature {mean_temp:.1f} K far from 300 K target"
265
  finally:
266
  del context
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
264
  assert 250 < mean_temp < 350, f"mean temperature {mean_temp:.1f} K far from 300 K target"
265
  finally:
266
  del context
267
+
268
+
269
+ class TestBioPythonFallback:
270
+ """BioPython structural-analysis fallback (regression for 'str' object has
271
+ no attribute 'name' — BioPython Atom.element is a string, not an object)."""
272
+
273
+ _MINI_PDB = """\
274
+ ATOM 1 N ALA A 1 11.104 6.134 -6.504 1.00 11.79 N
275
+ ATOM 2 CA ALA A 1 11.639 6.071 -5.145 1.00 11.80 C
276
+ ATOM 3 C ALA A 1 12.839 6.979 -4.873 1.00 11.52 C
277
+ ATOM 4 O ALA A 1 13.419 7.636 -5.737 1.00 11.76 O
278
+ ATOM 5 CB ALA A 1 10.514 6.523 -4.225 1.00 12.30 C
279
+ ATOM 6 N GLY A 2 13.220 7.004 -3.610 1.00 11.67 N
280
+ ATOM 7 CA GLY A 2 14.370 7.807 -3.226 1.00 11.95 C
281
+ ATOM 8 C GLY A 2 14.359 8.072 -1.730 1.00 12.19 C
282
+ ATOM 9 O GLY A 2 13.417 8.612 -1.169 1.00 12.34 O
283
+ END
284
+ """
285
+
286
+ def test_fallback_completes_with_sasa(self, tmp_path):
287
+ from app.tools.md_sim import _run_biopython_analysis
288
+ pdb_path = tmp_path / "mini.pdb"
289
+ pdb_path.write_text(self._MINI_PDB)
290
+ result = _run_biopython_analysis(str(pdb_path), "MINI", "minimize")
291
+ assert result["status"] == "complete"
292
+ assert result["engine"] == "biopython_structural"
293
+ assert len(result["radius_of_gyration"]) >= 1
294
+ assert result["sasa"][0]["sasa_angstrom2"] > 0
295
+ assert result["atom_count"] > 0