next up previous contents index
Next: Atom information Up: molinfo Previous: Using molinfo to access   Contents   Index

Using molinfo to access information about a molecule

The molinfo command can also be used to access and, in some cases, modify information specific to a given molecule. A query is in the form:

and the result is a list of elements, one for each keyword.
vmd > molinfo top get numatoms
Info) 568
vmd > molinfo 0 get {source filename}
Info) File /home/dalke/pdb/pti.pdb
This next example is a bit more complicated. It loops through all the graphical representation (numreps) and, for each one, gets the representation used (rep), the selection text (selection), and the coloring method (color).
vmd > for {set i 0} {$i < [molinfo top get numreps]} {incr i} {
? lassign [molinfo top get "{rep $i} {selection $i} {color $i}"] a b c
? puts "view $i:"
? puts " representation: $a"
? puts " selection: $b"
? puts " coloring method: $c"
? }
view 0:
 representation: Tube 0.300000 8.000000
 selection: protein backbone
 coloring: method Structure
view 1:
 representation: Lines 2.000000
 selection: same residue as name "S.*"
 coloring: method ResName
view 2:
 representation: VDW 1.000000 6.000000
 selection: name "S.*"
 coloring: method Name
A similar behavior can be achieved by using mol list top , in which case, however, there is no direct access to the data. A complete list of keywords is given in Table 12.1.2.

Table 12.1: molinfo keywords
Keyword Aliases Arg Set Description
id   int N molecular id
index   int N index on the molecule list
numatoms   int N number of atoms
source   str N one of File, Graphics, Remote, or Sigma
name   str N the name of the molecule (usually the name of the file)
filename   str N full filename (if two files, the topology file)
filetype   str N corresponding file type (PSF, PDB, XYZ, ...)
filename2   str N full filename of coordinate file (if two files)
filetype2   str N corresponding file type (DCD, PDB, XTC, ...)
active   bool Y is/make the molecule active
drawn displayed bool Y is/make the molecule drawn
fixed   bool Y is/make the molecule fixed
top   bool Y is/make the molecule top
center   vector Y get/set the coordinate used as the center
center_matrix   matrix Y get/set the centering matrix
rotate_matrix   matrix Y get/set the rotation matrix
scale_matrix   matrix Y get/set the scaling matrix
global_matrix   matrix Y get/set the global (rotation/scaling) matrix
view_matrix   matrix N get/set the overall viewing matrix
numreps   int N the number of representations
selection i   string N the string for the i'th selection
rep i   string N the string for the i'th representation
color i colour string N the string for the i'th coloring method
numframes   int N number of animation frames
frame   int Y current frame number
bond   float N the bond energy (for the current frame)
angle   float N the angle energy
dihedral   float N the dihedral energy
improper   float N the improper energy
vdw   float N the van der Waal energy
electrostatic elec float N the electrostatic energy
hbond   float N the hydrogen bond energy
kinetic   float N the total kinetic energy
potential   float N the total potential energy
energy   float N the total energy
temperature temp float N the overall temperature

The molinfo command, contrary to its name, can also be used to set some keyword values, such as the current frame number and the display state flags. This duplicates some of the functionality of the mol command, though there are distinct differences in the implementation. Specifically, the mol command uses the internal command queue which, among other things, notifies the appropriate forms that a change occured, redraws the graphics, and logs the commands to the log file, of logging is enabled. In future versions of VMD there will be only one command; for now we suggest only using the molinfo command to get information and to set the frame value and the various viewing matrices.


Two functions, one to save the current view position, the other to restore it. The position of the axis is not changed by these operations.

proc save_viewpoint {} {
   global viewpoints
   if [info exists viewpoints] {unset viewpoints}
   # get the current matricies
   foreach mol [molinfo list] {
      set viewpoints($mol) [molinfo $mol get {
	center_matrix rotate_matrix scale_matrix global_matrix}]
proc restore_viewpoint {} {
   global viewpoints
   foreach mol [molinfo list] {
      puts "Trying $mol"
      if [info exists viewpoints($mol)] {
         molinfo $mol set {center_matrix rotate_matrix scale_matrix
	   global_matrix} $viewpoints($mol)

Cycle through the list of displayed molecules, turning each one on one at a time. At the end, return the display flags to their original state.

# save the current display state
foreach mol [molinfo list] {
  set disp($mol) [molinfo $mol get drawn]
# turn everything off
mol off all
# turn each molecule on then off again
foreach mol [molinfo list] {
  if $disp($mol) {
     mol on $mol
     sleep 1
     mol off $mol
# turn the original ones back on
foreach mol [molinfo list] {
  if $disp($mol) {mol on $mol }

The last loop, which turns the originally drawn molecules back on, doesn't turn them on at the same time. That's because some commands (those which use the command queue) redraw the graphics when they are used. This can be disabled with the display update (see section §  for more information). Using this, the final loop becomes

#turn the original ones back on
display update off
foreach mol [molinfo list] {
  if $disp($mol) {mol on $mol }
display update on
Alternatively, since the drawn option is settable, you could do:
foreach mol [molinfo list] {
  if $disp($mol) {molinfo $mol set drawn 1}
However, that won't set the flag to redraw the scene so you need to force a redraw with display redraw.

next up previous contents index
Next: Atom information Up: molinfo Previous: Using molinfo to access   Contents   Index