# Bulk-fetch all bonds, filter out H1-H2 bonds in SWM4, reassign in one shot # Get the set of H1/H2 atom indices in SWM4 residues set sel [atomselect top "resname SWM4 and name H1 H2"] set target_indices [$sel get index] $sel delete # Build a lookup array for O(1) membership test array unset is_target foreach idx $target_indices { set is_target($idx) 1 } # Get ALL atoms and ALL bonds in one call set all [atomselect top "all"] set all_bonds [$all getbonds] # Build new bond lists, stripping target-target bonds set new_bonds {} set modified 0 set removed 0 set i 0 foreach blist $all_bonds { if {[info exists is_target($i)]} { # This atom is an H1/H2 in SWM4 — filter its bonds set filtered {} foreach b $blist { if {![info exists is_target($b)]} { lappend filtered $b } else { incr removed } } lappend new_bonds $filtered if {[llength $filtered] != [llength $blist]} { set modified 1 } } else { lappend new_bonds $blist } incr i } # Reassign all bonds at once if {$modified} { $all setbonds $new_bonds } $all delete # Each bond counted from both ends puts "Deleted [expr {$removed / 2}] H1-H2 bonds in SWM4 residues."