From: Dr. Paul Fons (paul-fons_at_aist.go.jp)
Date: Sat Aug 25 2001 - 19:54:48 CDT

Hi in attempt to work on my construction of bonds, I quickly wrote the
following code. The code seems to work but it uses an extravagent amount
of memory for reasons I don't quite understand. I suspect that I am
making numerous atomselect objects and not releasing memory. Does this
mean I have to allocate a handle to each atomselect object so I can
later manual delete it - e.g. embedding an atomselect object in a
function reference (square brackets) does not result in a temporary
stack allocated structure which automatically goes away. Anyway here is
my attempt. Comments appreciated.

proc makebonds {limit} { ;# generate bonds for an arbitrary structure
puts "Zapping old bonds"
zapbonds ;# zap all current bonds
puts "Generating new bonds"
set allatomlist [ [atomselect top "all"] list] ;# all atoms
  foreach atom1 $allatomlist { ;# loop over all atoms
    puts "Now working on neighbors of atom $atom1"
    foreach atom2 $allatomlist { ;# loop over all atoms
    if {$atom1 == $atom2} {continue}
    set atom1sel [atomselect top "index $atom1"]
    set atom2sel [atomselect top "index $atom2"]
    if {[measure rmsd $atom1sel $atom2sel] > $limit} {continue}
    set mybonds [$atom1sel getbonds]
# puts stdout "Adding atom:$atom2 to atom $atom1 bond list"
    lappend mybonds $atom2 ;# append this neighbor to atoms list
    set mybonds [join $mybonds]
# puts "the bond list is now: $mybonds"
    $atom1sel setbonds [list $mybonds]
    $atom1sel delete
    $atom2sel delete
   }
  }
return 0
}

proc zapbonds {} {
  set allatomlist [[atomselect top "all"] list]
  foreach atom $allatomlist {
     set theatom [atomselect top "index $atom"]
     $theatom setbonds {{}}
     $theatom delete
  }
return 0
}