Conversation
Add Entity.radius_of_gyration(geometric=False), returning the mass-weighted root-mean-square distance of the atoms from their center of mass (or the geometric radius of gyration when geometric=True). The atom-collection walk is factored into a shared private helper _get_atoms(), also used by center_of_mass() (behaviour unchanged). Adds tests for structure- and chain-level radii of gyration and the empty structure error case. See biopython#5257
There was a problem hiding this comment.
Pull request overview
This PR adds a new structural metric API, Entity.radius_of_gyration(geometric=False), to Bio.PDB entities (Structure/Model/Chain/Residue) and refactors shared atom-collection logic to support it consistently alongside the existing center_of_mass() method.
Changes:
- Add
Entity.radius_of_gyration()with mass-weighted (default) and geometric (equal-weight) modes. - Factor recursive/disordered atom collection into a shared private helper
Entity._get_atoms(), used by bothcenter_of_mass()andradius_of_gyration(). - Extend
Tests/test_PDB_SMCRA.pywith new radius-of-gyration assertions and an empty-entity error test.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| Bio/PDB/Entity.py | Adds radius_of_gyration() and refactors atom traversal into _get_atoms() shared with center_of_mass(). |
| Tests/test_PDB_SMCRA.py | Adds test coverage for structure-level and per-chain radius of gyration plus empty-entity error behavior. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| entities = deque([self]) # start with [self] to avoid auto-unpacking | ||
| while True: | ||
| e = entities.popleft() | ||
| if e.level in maybe_disordered: | ||
| entities += e.get_unpacked_list() | ||
| else: | ||
| entities += e.child_list |
| if geometric: | ||
| masses = np.ones(len(coords), dtype=np.float32) | ||
| else: | ||
| masses = np.asarray([a.mass for a in entities], dtype=np.float32) |
|
For the record: I used an AI assistant for this patch, and I used AI to fix my @peterjc asked me about this on #5278 and I answered there, including my |
CONTRIBUTING.rstfile and run the style checks locally (black,ruff,flake8all clean); I understand CI will confirm the unit tests and style checks pass.NEWS.rst/CONTRIB.rst(optional, not wished).Closes #5257
Adds
Entity.radius_of_gyration(geometric=False)(proposed by @Grumb0t): the mass-weighted root-mean-square distance of the atoms from their center of mass, available on Structure/Model/Chain/Residue likecenter_of_mass().Uses each
Atom.mass(IUPAC weights); the atom-collection walk is factored into a shared_get_atoms()helper also used bycenter_of_mass()(behaviour unchanged); raisesValueErroron empty entity.Adds tests (structure mass/geometric, per-chain, empty-structure) cross-checked against an independent NumPy computation;
test_PDB_SMCRA.py(29) + related PDB tests pass; black/ruff/flake8 clean.