Skip to content

Add some topology slicing methods.#961

Open
chrisjonesBSU wants to merge 7 commits intomosdef-hub:mainfrom
chrisjonesBSU:calc-charges
Open

Add some topology slicing methods.#961
chrisjonesBSU wants to merge 7 commits intomosdef-hub:mainfrom
chrisjonesBSU:calc-charges

Conversation

@chrisjonesBSU
Copy link
Copy Markdown
Contributor

PR Summary:

This is ultimately an effort to help out @JAGarcia03 with his project, but it should be a useful addition in general.

utils.py gets new slicing methods where the goal is to create a new topology object that contains a subset of another topology's molecules or residues. This should live in utils.py as we have been wanting to reduce the amount of methods and code in general within Topology, and this file will be a good place to add other, more creative slicing methods.

This also adds the ability to filter by number in addition to tag within iter_sites_by_molecule and iter_sites_by_residue.

For example, if you want to get just the sites belonging to the 0th "Benzene" (instead of all of them) you can now do:

for site in topology.iter_sites_by_molecule(molecule_tag="Benzene", molecule_number=0)

A quick example:

import mbuild as mb
import gmso
from gmso.core.topology import Topology
from gmso.utils.slicing import slice_topology_by_molecule

benzene = mb.load("c1ccccc1", smiles=True)
benzene.name = "Benzene"
ethane = mb.load("CC", smiles=True)
ethane.name = "Ethane"

system = mb.fill_box(compound=[benzene, ethane], n_compounds=[2, 2], box=[2,2,2])
topology = system.to_gmso()
topology.identify_connections()

single_benzene_top = slice_topology_by_molecule(topology=topology, molecule_tag="Benzene", molecule_number=0)

all_benzene_top = slice_topology_by_molecule(topology=topology, molecule_tag="Benzene", molecule_number=None)

I still need to make the slicing tests more thorough, and add new tests for the changes to iter_sites_by_*

PR Checklist


  • Includes appropriate unit test(s)
  • Appropriate docstring(s) are added/updated
  • Code is (approximately) PEP8 compliant
  • Issue(s) raised/addressed?

@chrisjonesBSU chrisjonesBSU added the enhancement New feature or request label Feb 18, 2026
@codecov
Copy link
Copy Markdown

codecov bot commented Feb 18, 2026

Codecov Report

❌ Patch coverage is 56.89655% with 25 lines in your changes missing coverage. Please review.
✅ Project coverage is 93.19%. Comparing base (6af483e) to head (176bb78).

Files with missing lines Patch % Lines
gmso/external/convert_openmm.py 8.69% 21 Missing ⚠️
gmso/core/topology.py 77.77% 4 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #961      +/-   ##
==========================================
- Coverage   93.48%   93.19%   -0.29%     
==========================================
  Files          67       68       +1     
  Lines        7785     7837      +52     
==========================================
+ Hits         7278     7304      +26     
- Misses        507      533      +26     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

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

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants