From: Ashar Malik (
Date: Sat Jan 30 2016 - 23:26:21 CST

I am not sure if there exists a direct selection command to go across
molecules. It may exist so it maybe helpful to wait, however I have put
together a small script which should pretty much do what you asked.

Assuming you have two molecules loaded into VMD with ID 0 and 1.
Use the following code.

set id_ 0 ;#select reference molecules ID
set s0 [atomselect $id_ "index 1" ] ;#select 1 atom from this molecule
which is your reference
set ref_ [lindex [$s0 get {x y z}] 0] ;#grab the x,y and z coordinates

#the additional lindex command in the last line is a sanity check to ensure
1 atom is selected.

set id_ 1 ;#select next molecules ID
set s1 [atomselect $id_ all] ;#select everything in this new molecule
set tar_ [$s1 get {x y z}] ;#get atom coordinates of everything in as list

set n_o_A [llength $tar_] ;#get number of atoms for the for loop

for {set i 0} {$i < $n_o_A} {incr i} { ;# loop over all atoms in selection
to compare with reference
set temp [lindex $tar_ $i] ;# store current atom in a temp variable
    set dist_ [vecdist $ref_ $temp] ;# get distance between current atom
and reference
if {$dist_ < 5} {
lappend tar_atoms [expr $i+1] ;# if distance below cut off save index of
atom from current molecule.
# note that in the previous line $i is the index of the atom in the list
which is probably off by 1 -- i just added 1 to it to fix this
# and haven't tested it. Be careful when using it.

When you run this code -- a list of index of atoms in molecule id 1 will be
generated which are within a cutoff (5A) of the reference atom in molecule
id 0. This is basically a template. You could have the lower part of this
in a for loop and run the same changing the "id_" variable in each
iteration for the total number of molecules you have. For that you may also
have to log the molecule id along with target atoms or you might loose the
distinction in the target atoms identified as to which molecule do they
originate from.

If you are unable to log the molID along with the target atoms identified,
post again and you will get a reply.

This code is not optimized - meaning thre might (probably does) exist a
better way of accomplishing this.

Good luck and hope this helps.


On Sat, Jan 30, 2016 at 5:45 AM, Shashidhar Rao <>

> Hello VMD Gurus,
> I am trying to find a way by which I can select residues/atoms within
> molecule ID 1 which are within 5 angstroms of a given residue in molecule
> ID 2. Thus, my selection is across multiple molecules. as an extension, I
> may have 10 molecule IDs and I would like to select all the residues within
> 5 angstroms of a given residue in molecule ID 2 in all the remaining
> molecule IDs.
> I need this recipe to identify a set of water molecules simulated
> separately in a PDB file using the PDB structure of a solute molecule which
> is placed in the context of the water molecules.
> thanks for your help
> --
> Shashidhar N. Rao