From: Windle,Stephen (
Date: Sun Dec 17 2017 - 13:21:09 CST

Hey all,

Thanks. I did already have a script built that loads all the pdb files in a folder, I just kept getting an error in my original loop when it moved to the second loaded file. This seems to work though, thanks! As far as separate files, I do want to keep them on the same file. I'm building a histogram in xmgrace and want to run statistical analysis on side chain spacing, and I want to be able to run it over large groups of proteins (ideally in the thousands), so loading into xmgrace in a single file is the end goal, as I won't really need to be parsing out the data into individual proteins after (though I'll have to use one of the computers at my school as I don't think mine will handle that). Thanks again for your help!


From: Vermaas, Joshua <>
Sent: Friday, December 15, 2017 9:03:57 PM
To: Ashar Malik; Windle,Stephen
Subject: Re: vmd-l: Cycling between files using the same output file

Hi Stephen,

A variant on Ashar's method if the files are already loaded is to wrap your existing script like this:

foreach mid [molinfo list] {
set allsel [atomselect $mid "sidechain and resname ALA"]
set residuelist [lsort -unique -integer [$allsel get residue]]

set ressellist [list ]
foreach residue $residuelist {
lappend ressellist [atomselect $mid "residue $residue"]

set file [open "myoutput$mid.dat" w]

for { set i 0 } { $i < [llength $ressellist] } { incr i } {
for { set j [expr {$i+1}] } { $j < [llength $ressellist] } { incr j } {
puts $file "$i $j [veclength [vecsub [measure center [lindex $ressellist $i]] [measure center [lindex $ressellist $j]]]]"


close $file

If you are deadset on writing it one giant file (which I can't recommend! Its awful to parse out what goes where afterward!), move the file opening and closing outside the loop. You may also want to consider deleting atomselections after you are done with them, as VMD can run out of them given a large enough problemset.


On 12/15/2017 05:23 PM, Ashar Malik wrote:

To get you to write your own code here is a hint.

(Assuming I understand your question -- this may be way off)

Think that all your PDB files are in a folder.
In your script use glob to populate a list of pdb files using something like

set pdb_list [glob *.pdb]

Now use a for loop over this list. Do something like

foreach pdb_file $pdb_list {



Inside the for loop load the PDB

mol load pdb_file $pdb

Note that every time you load a molecule it becomes "top". So running the code you sent with "top" will work for the last molecule added.

So now there are two ways -- I can think of.

Given that there is no relationship between the PDB files and all calculations are done with one pdb file. You can simply run through the for loop above loading a new PDB file in each iteration
Making the selections doing your work and deleting those selections to proper memory management.

The other way is that each time a new PDB file is loaded a ID will be assigned to that molecule
Grab that ID using the molinfo command with the ID (<> )

Append that to a list using "lappend" and then iterate over that list of molecular IDs.

Note that you may have to put two for loops, one to load the molecules and grab their IDs. The second to iterate over the list of IDs and do the work. If you just want to work on PDB files without having to go back and forth between them you should just used the earlier approach and not log the IDs. To use the molecule's ID you can do something like

[atomselect 17 all] # note that I replaced top with an integer 17 so if I have 30 molecules loaded into VMD - I can use this to select everything in molecule which was assigned mol ID 17.

Hope this helps.

On Sat, Dec 16, 2017 at 11:39 AM, Windle,Stephen <<>> wrote:

Good evening.

I am running a script to output distances on selected molecules to a specific file. I was trying to get it to loop between all the files loaded, but my loops don't seem to be working. What I want it to do is to run this script, but inside a loop that moves onto the next loaded .pdb file, and do the same, but output it to the end of the .dat file, so that it will cycle through however many files I have loaded and just add them to the end, making one really long file. The script is below (this is what it looks like before I started trying to add a for loop to have it run through the loaded files):

set allsel [atomselect top "sidechain and resname ALA"]
set residuelist [lsort -unique -integer [$allsel get residue]]

set ressellist [list ]
foreach residue $residuelist {
lappend ressellist [atomselect top "residue $residue"]

set file [open "myoutput.dat" w]

for { set i 0 } { $i < [llength $ressellist] } { incr i } {
for { set j [expr {$i+1}] } { $j < [llength $ressellist] } { incr j } {
puts $file "$i $j [veclength [vecsub [measure center [lindex $ressellist $i]] [measure center [lindex $ressellist $j]]]]"


close $file

..I am very new to the VMD environment, and haven't had luck looking through the manuals and readme files, so I'm not sure what I'm doing wrong. Thanks!