From: Axel Kohlmeyer (
Date: Sat May 19 2007 - 12:28:29 CDT

On Sat, 19 May 2007, Kailee wrote:

KL> Dear Axel,

dear kailee,

in the future, please send mails that are not personal,
but about programs like VMD always to the corresponding
mailing lists so that that people like me, who get a _lot_
of e-mails can have mails properly presorted into the
corresponding folders. also you have a larger chance of
getting help, since there may be more people on the list
that can help you. thanks.

KL> Thank you very much for you reply.
KL> As I am really new in Tcl script, I have spent some time reading the basics
KL> of tcl scripting, and tried to write the script based on your suggestions,

ok. this is very commendable. your script looks already
as you are on the right track, however, there are a bunch
of tiny typos or misconceptions that make it not work.
with a little bit more experience those will be easy to
spot and you will be able to write much more complicated
vmd analysis scripts. the major general recommendation is,
to build scripts in stages, either from the 'outside in'
i.e. write the loop construct(s) and when they work add
the analysis in steps.

now in detail to your script.

KL> as follows:
KL> proc nearfe {} {

it is usually a good idea to make scripts rather general
when it can be done without much additional effort. in
your case one could think about having 3 arguments:
the molecule id you operate on, the selection string and
the filename. this way you can use the same kind of analysis
in many more ways without having to edit the script file itself.
so the first line would look like:

proc res_near_sel {mol selstr fname} {

and you would call it:

res_near_sel [molinfo top] "resname O2 exwithin 5.0 of name FE" "c:/analysis"

thus the inner part of the script would be changed to:

KL> set selvar [atomselect top "resname O2 exwithin 5.0 of name FE"]

set selvar [atomselect $mol $selstr]
set numf [molinfo $mol get numframes]

KL> set f [open "c:/analysis" "w+"]

you want the access to be "w" and not "w+", so:

set f [open $fname "w"]

KL> for {set i 0} {$i<501} {incr 1} {

here is a small mistake. it has to be {incr i} and
not {incr 1} so try:

for {set i 0} {$i<$numf} {incr i} {

KL> $selvar frame $i
KL> $selvar update
KL> set inrange [lsort -unique [$selvar get resid]]

KL> puts $fp "$i [llenght $inrange] $inrange" }

it has to be puts $f (look at the open/close) and
there is a typo: llength

KL> close $f}

in order to have a clean procedure without a memory leak
you should delete the selection function. so the end
of the procedure would be:

  close $f
  $selvar delete

KL> However, it does not work, one thing I don't quite understand is $selvar you
KL> used, is it the selection of region of interest? In the above script, I have

this is a key element in VMD scripting and a bit unusual at first
but makes perfect sense later. whenever you call the atomselect
keyword it will create a new VMD/tcl procedure with the name
atomselectXXX (where XXX is a unique number starting from 0 every
time you start VMD fresh). since the name of this function depends
on how many selection have already been created, one usually stores
the name in a variable and then uses the variable instead. just
look at a few other scripts and you will see.

the main trick to get everything to work, to build scripts
in stages, so you can check out each command individually.


KL> a 500 frames in my trajecotry, and I want to writet the results in a new
KL> file called 'analysis' in c drive (I am using the VMD windows version). I
KL> know there must be quite a few very trivial mistakes in my script however
KL> can't spot it myself, can anyone help to correct it please? Thank you in
KL> advance!
KL> Best regards,
KL> Kailee
KL> On 5/18/07, Axel Kohlmeyer <> wrote:
KL> >
KL> > On Fri, 18 May 2007, Kailee wrote:
KL> >
KL> > K> Dear all,
KL> >
KL> > dear kailee,
KL> >
KL> > K> into the protein. What I want to do is, I want to know at what time,
KL> > which
KL> > K> gas molecule (ID) get close to a certain distance to the active site?
KL> > so for
KL> > K> example, when one gas molecule become within 5 angstrom to the Fe atom
KL> > in
KL> > K> the active site, the atom ID of that gas molecule and the frame number
KL> > can
KL> > K> be recorded automatically.
KL> >
KL> > K> I looked at the VMD script library but couldn't find similar script
KL> > K> available, can I ask has anyone wrote similar script already? or have
KL> > some
KL> >
KL> > there should be plenty of similar scripts (not to do this specific
KL> > kind of analysis, but just to track a property across a trajectory)
KL> > that use the kind of framework that you need to step through the
KL> > trajectory. what you need to do is roughly:
KL> >
KL> > create a selection for your region of interest eg using
KL> > 'resname XX exwithin 5.0 of name Fe' as selection string.
KL> > open a file, figure out how many frames your trajectory has.
KL> > then do a loop over the frame numbers (starting from 0) and
KL> > do a
KL> > $selvar frame $i
KL> > $selvar update
KL> > to advance the selection to the next frame and recompute it
KL> > and finally do a:
KL> > set inrange [lsort -unique [$selvar get resid]]
KL> > puts $fp "$i [llength $inrage] $inrange"
KL> >
KL> > and after the end of the loop you have close the file.
KL> > then your file will contain for each frame:
KL> > the frame number, the number of molecules in range
KL> > and a list of their residue ids.
KL> >
KL> > hope that helps,
KL> > axel.
KL> >
KL> >
KL> >
KL> > K> suggestions how to do this? as there might be some better ideas~ any
KL> > help is
KL> > K> appreciated! Thank you.
KL> > K>
KL> > K> Best regards,
KL> > K>
KL> > K> Kailee
KL> > K>
KL> >
KL> > --
KL> > =======================================================================
KL> > Axel Kohlmeyer
KL> > Center for Molecular Modeling -- University of Pennsylvania
KL> > Department of Chemistry, 231 S.34th Street, Philadelphia, PA 19104-6323
KL> > tel: 1-215-898-1582, fax: 1-215-573-6233, office-tel: 1-215-898-5425
KL> > =======================================================================
KL> > If you make something idiot-proof, the universe creates a better idiot.
KL> >
KL> >

Axel Kohlmeyer
   Center for Molecular Modeling   --   University of Pennsylvania
Department of Chemistry, 231 S.34th Street, Philadelphia, PA 19104-6323
tel: 1-215-898-1582,  fax: 1-215-573-6233,  office-tel: 1-215-898-5425
If you make something idiot-proof, the universe creates a better idiot.