From: J T (jtibbitt_at_odu.edu)
Date: Tue Aug 21 2007 - 12:40:19 CDT

Dear VMD Community,

I've been working on a script that will select the N-closest waters
to a specified atom selection by following the algorithm suggested by
John Stone in an earlier post. It successfully returns a sorted
list of increasing distances, but there are duplicate water
residues. The script generates a list of all water indices within a
max cutoff distance. Then each water index in that list is replaced
with a double valued element containing the distance and the water
residue. Is there a way I can use the -unique option somehow to fix
this?

Thank-you for reading,
Jeff Tibbitt
jtibbitt_at_odu.edu

John Stone's algorithm:
  1) select all waters within the max cutoff distance M
  2) calculate the distance D of each water molecule and add the
index and distance to a list or lists
  3) sort the list(s) by the distance (keeping the water index and
distance assocation intact, if they are in separate lists..)
  4) select the closest N waters from the sorted list

My Script:
proc nwat {n sel} {
   set lists [measure contacts 6 [atomselect top water] $sel]
   set wlist [lindex $lists 0]
   set slist [lindex $lists 1]
   set n [llength $wlist]
   for {set i 0} {$i < $n} {incr i} {
     set satom [lindex $slist $i]
     set watom [lindex $wlist $i]
     set wres [[atomselect top "index $watom"] get resid]
     set d [measure bond "$watom $satom"]
     lset wlist $i "$d $wres"
   }
   set nwatlist [lsort $wlist]
}