From: Axel Kohlmeyer (
Date: Thu Sep 01 2005 - 01:16:43 CDT

On Wed, 31 Aug 2005, Daniel Barsky wrote:


DB> Hey you vmd scripting wizards, forgive me if this has been asked (and
DB> answered) before. I looked through the archive and saw your script for
DB> counting hydrogen bonds ("hbonds time series"), but I have something even
DB> easier (I think) that I'd like to do: count atoms/residues/etc that qualify
DB> for a given selection. For example, if I ask for say "resname SOD within 7
DB> of nucleic" the script would return a number. This could then be started in

this kind of information is already implemented directly into the
atomselect mechanism. the atomselect command will create an internal
function (atomselect0, atomselect1 and so on) and from that function
you can retrieve all kinds of information about the selection. since
the name of that internal function depends on how many time the atomselect
command has been used before, it is usually stored into a variable and
then used like this.

set sel [atomselect [molinfo top] {resname SOD within 7.0 of nucleic}]

to retrieve the number of atoms in that selection you do:

$sel num

this number is valid for the current frame, if you want to get
the number for a different frame you just do:

$sel frame 200
$sel update
$sel num

DB> tandem with "selection_update". When you "play" the trajectory, you'd then
DB> get a selection time series.

you can use that snippet of vmd/tcl code in various ways now.
you can hook into the vmd_frame loop to have this value computed
whenever you display a different frame and, e.g. print the resulting
number into the VMD window, or you just loop over the whole trajectory
and write the numbers to a file. e.g. with (totally untested):

set mol [molinfo top]
set sel [atomselect $mol {resname SOD and within 7.0 of nucleic}
set frames [molinfo $mol get numframes]
set fp [ open "numsod.dat" w ]
for {set i 0} {$i < $frames} {incr i} {
    $sel frame $i
    $sel update
    set n [$sel num]
    puts $fp "$i $n"
$sel delete
close $fp
unset mol sel frames fp i n

there are many more examples of that type in the VMD users
guide and the various tutorials and example scripts (see the
VMD script library) available.


DB> Thanks in advance for your help.
DB> Daniel
DB> Daniel Barsky, Ph.D.
DB> University of California/
DB> Lawrence Livermore National Lab
DB> 7000 East Avenue, L-448
DB> Livermore, CA 94550
DB> Phone: 925-422-1540 Fax: 925-424-6605
DB> Secretary: 925-424-2422 (8 to 5 Pacific Time)
DB> Email: barsky_at_LLNL.GOV

Dr. Axel Kohlmeyer   e-mail:
Lehrstuhl fuer Theoretische Chemie          Phone: ++49 (0)234/32-26673
Ruhr-Universitaet Bochum - NC 03/53         Fax:   ++49 (0)234/32-14045
D-44780 Bochum
If you make something idiot-proof, the universe creates a better idiot.