@@ -59,9 +59,7 @@ def _discover_group_dihedral_topology(
5959 topologies : list [MoleculeDihedralTopology ] = []
6060
6161 for molecule_order , molecule_id in enumerate (molecules ):
62- mol = self ._universe_operations .extract_fragment (
63- data_container , molecule_id
64- )
62+ mol = self ._extract_topology_fragment (data_container , molecule_id )
6563 num_residues = len (mol .residues )
6664 ua_dihedrals_by_residue : dict [int , list [Any ]] = {}
6765 residue_dihedrals : list [Any ] = []
@@ -90,6 +88,26 @@ def _discover_group_dihedral_topology(
9088
9189 return topologies
9290
91+ def _extract_topology_fragment (self , data_container : Any , molecule_id : Any ) -> Any :
92+ """Return a molecule fragment for topology discovery.
93+
94+ This uses the lightweight AtomGroup extraction helper when available so
95+ static conformational topology discovery does not create a standalone
96+ in-memory universe or copy trajectory frames. The fallback preserves
97+ compatibility with older ``UniverseOperations`` implementations.
98+
99+ Args:
100+ data_container: Source MDAnalysis universe or universe-like container.
101+ molecule_id: Fragment index identifying the molecule to extract.
102+
103+ Returns:
104+ MDAnalysis AtomGroup for the selected molecule
105+ """
106+ return self ._universe_operations .extract_fragment_atomgroup (
107+ data_container ,
108+ int (molecule_id ),
109+ )
110+
93111 def _select_heavy_residue (self , mol : Any , res_id : int ) -> Any :
94112 """Select heavy atoms in a residue by residue index.
95113
@@ -100,13 +118,15 @@ def _select_heavy_residue(self, mol: Any, res_id: int) -> Any:
100118 Returns:
101119 AtomGroup containing heavy atoms in the residue selection.
102120 """
103- selection1 = mol .residues [res_id ].atoms .indices [0 ]
104- selection2 = mol .residues [res_id ].atoms .indices [- 1 ]
121+ residue_atoms = mol .residues [int (res_id )].atoms
122+ selection1 = residue_atoms .indices [0 ]
123+ selection2 = residue_atoms .indices [- 1 ]
105124
106- res_container = self ._universe_operations .select_atoms (
107- mol , f"index { selection1 } :{ selection2 } "
125+ res_container = mol .select_atoms (
126+ f"index { selection1 } :{ selection2 } " ,
127+ updating = False ,
108128 )
109- return self . _universe_operations . select_atoms (res_container , "prop mass > 1.1" )
129+ return res_container . select_atoms ("prop mass > 1.1" , updating = False )
110130
111131 def _get_dihedrals (self , data_container : Any , level : str ) -> list [Any ]:
112132 """Return dihedral AtomGroups for a container at a given level.
@@ -121,26 +141,93 @@ def _get_dihedrals(self, data_container: Any, level: str) -> list[Any]:
121141 atom_groups : list [Any ] = []
122142
123143 if level == "united_atom" :
144+ selected_indices = {int (index ) for index in data_container .indices }
145+
124146 for dihedral in data_container .dihedrals :
125- atom_groups .append (dihedral .atoms )
147+ dihedral_atoms = dihedral .atoms
148+ dihedral_indices = {int (index ) for index in dihedral_atoms .indices }
149+
150+ if len (dihedral_atoms ) == 4 and dihedral_indices .issubset (
151+ selected_indices
152+ ):
153+ atom_groups .append (dihedral_atoms )
126154
127155 if level == "residue" :
128156 num_residues = len (data_container .residues )
129157 if num_residues >= 4 :
130158 for residue in range (4 , num_residues + 1 ):
131- atom1 = data_container .select_atoms (
132- f"resindex { residue - 4 } and bonded resindex { residue - 3 } "
159+ residue1 = data_container .residues [residue - 4 ]
160+ residue2 = data_container .residues [residue - 3 ]
161+ residue3 = data_container .residues [residue - 2 ]
162+ residue4 = data_container .residues [residue - 1 ]
163+
164+ atom1 = self ._atoms_in_source_bonded_to_target (
165+ residue1 ,
166+ residue2 ,
133167 )
134- atom2 = data_container .select_atoms (
135- f"resindex { residue - 3 } and bonded resindex { residue - 4 } "
168+ atom2 = self ._atoms_in_source_bonded_to_target (
169+ residue2 ,
170+ residue1 ,
136171 )
137- atom3 = data_container .select_atoms (
138- f"resindex { residue - 2 } and bonded resindex { residue - 1 } "
172+ atom3 = self ._atoms_in_source_bonded_to_target (
173+ residue3 ,
174+ residue4 ,
139175 )
140- atom4 = data_container .select_atoms (
141- f"resindex { residue - 1 } and bonded resindex { residue - 2 } "
176+ atom4 = self ._atoms_in_source_bonded_to_target (
177+ residue4 ,
178+ residue3 ,
142179 )
143- atom_groups .append (atom1 + atom2 + atom3 + atom4 )
180+
181+ dihedral_atoms = atom1 + atom2 + atom3 + atom4
182+
183+ if len (dihedral_atoms ) == 4 :
184+ atom_groups .append (dihedral_atoms )
185+ else :
186+ logger .debug (
187+ "Skipping residue-level dihedral for local residues "
188+ "%s-%s-%s-%s because it produced %d atoms." ,
189+ residue - 4 ,
190+ residue - 3 ,
191+ residue - 2 ,
192+ residue - 1 ,
193+ len (dihedral_atoms ),
194+ )
144195
145196 logger .debug ("Level: %s, Dihedrals: %s" , level , atom_groups )
146197 return atom_groups
198+
199+ @staticmethod
200+ def _atoms_in_source_bonded_to_target (
201+ source_residue : Any ,
202+ target_residue : Any ,
203+ ) -> Any :
204+ """Return source-residue atoms bonded to atoms in a target residue.
205+
206+ This helper is used when constructing residue-level dihedral definitions
207+ from lightweight molecule AtomGroups. It selects atoms from the source
208+ residue that are bonded to any atom in the target residue without using
209+ global ``resindex`` selection strings.
210+
211+ Args:
212+ source_residue: Residue whose atoms should be tested for bonds.
213+ target_residue: Adjacent residue providing the target bonded atoms.
214+
215+ Returns:
216+ MDAnalysis AtomGroup containing atoms from ``source_residue`` that are
217+ bonded to at least one atom in ``target_residue``. If no matching
218+ atoms are found, an empty AtomGroup is returned.
219+ """
220+ source_atoms = source_residue .atoms
221+ target_indices = {int (index ) for index in target_residue .atoms .indices }
222+ selected_indices : list [int ] = []
223+
224+ for atom in source_atoms :
225+ bonded_atoms = getattr (atom , "bonded_atoms" , None )
226+ if bonded_atoms is None :
227+ continue
228+
229+ bonded_indices = {int (index ) for index in bonded_atoms .indices }
230+ if bonded_indices .intersection (target_indices ):
231+ selected_indices .append (int (atom .index ))
232+
233+ return source_atoms .universe .atoms [selected_indices ]
0 commit comments