Skip to content

Add radius of gyration to Bio.PDB Entity (#5257) - #5279

Open
yiyabo wants to merge 1 commit into
biopython:masterfrom
yiyabo:feat-radius-of-gyration
Open

yiyabo wants to merge 1 commit into
biopython:masterfrom
yiyabo:feat-radius-of-gyration

Conversation

@yiyabo

@yiyabo yiyabo commented Aug 5, 2026

Copy link
Copy Markdown
  • I hereby agree to dual licence this and any previous contributions under both the Biopython License Agreement AND the BSD 3-Clause License.
  • I have read the CONTRIBUTING.rst file and run the style checks locally (black, ruff, flake8 all clean); I understand CI will confirm the unit tests and style checks pass.
  • I have added my name to 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 like center_of_mass().

Uses each Atom.mass (IUPAC weights); the atom-collection walk is factored into a shared _get_atoms() helper also used by center_of_mass() (behaviour unchanged); raises ValueError on 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.

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
@yiyabo
yiyabo requested a review from JoaoRodrigues as a code owner August 5, 2026 15:19
Copilot AI lite review requested due to automatic review settings August 5, 2026 15:19

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 both center_of_mass() and radius_of_gyration().
  • Extend Tests/test_PDB_SMCRA.py with 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.

Comment thread Bio/PDB/Entity.py
Comment on lines 363 to 369
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
Comment thread Bio/PDB/Entity.py
Comment on lines +413 to +416
if geometric:
masses = np.ones(len(coords), dtype=np.float32)
else:
masses = np.asarray([a.mass for a in entities], dtype=np.float32)
@yiyabo

yiyabo commented Aug 6, 2026

Copy link
Copy Markdown
Author

For the record: I used an AI assistant for this patch, and I used AI to fix my
English in the description. I am not a native English speaker.

@peterjc asked me about this on #5278 and I answered there, including my
thoughts on #5241. Short version: I accept whatever the project decides, and I
can close this PR if AI-assisted patches are not wanted even when disclosed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Proposition to add Radius of Gyration calculating function to Biopython

2 participants