next up previous contents index
Next: Viewing selections which change Up: Trajectory frames Previous: Trajectory frames

Animating the secondary structure

     

The secondary structure definitions for the molecules in VMD don't change during an animation but they can be made to do so with a trace on the vmd_frame($molecule) Tcl variable. The simplest way is to call vmd_calculate_structure(molecule) every time the frame changes, e.g.,

proc structure_trace {name index op} {
      vmd_calculate_structure $index
}

trace variable vmd_frame w structure_trace
but this isn't the fastest solution for a couple of reasons. First off, the secondary structure code is somewhat slow (and about 2/3 of the time is spent in the Tcl interface; mostly in writing a temporary PDB file). If you don't plan to modify the coordinates, it is possible to store, or cache, the secondary structure from one frame to the next. Second, if there are multiple molecules loaded and animated, the secondary structures of all of them are computed.

The following script, sscache (for ``secondary structure cache'') addresses those two problems. It is turned on with the command start_sscache followed by the molecule number of the molecule whose secondary structure should be saved (the default is ``top'', which gets converted to the correct molecule index). Whenever the frame for that molecule changes, the procedure sscache is called.

sscache is the heart of the script. It checks if a secondary structure definition for the given molecule number and frame already exists in the Tcl array sscache_data(molecule,frame). If so, it uses the data to redefine the ``structure'' keyword values (but only for the protein residues). If not, it calls the secondary structure routine to evaluate the secondary structure based on the new coordinates. The results are saved in the sscache_data array.

Once the secondary structure values are saved, the molecule can be animated rather quickly and the updates can be controlled by the animate form.

To turn off the trace, use the command stop_sscache, which also takes the molecule number. There must be one stop_sscache for each start_sscache. The command clear_sscache resets the saved secondary structure data for all the molecules and all the frames.

# Cache secondary structure information for a given molecule

# reset the secondary structure data cache
proc reset_sscache {{molid top}} {
    global sscache_data
    if {! [string compare $molid top]} {
      set molid [molinfo top]
    }
    if [info exists sscache_data($molid)] {
        unset sscache_data
    }
}

# start the cache for a given molecule
proc start_sscache {{molid top}} {
    if {! [string compare $molid top]} {
      set molid [molinfo top]
    }
    global vmd_frame
    # set a trace to detect when an animation frame changes
    trace variable vmd_frame($molid) w sscache
    return
}

# remove the trace (need one stop for every start)
proc stop_sscache {{molid top}} {
    if {! [string compare $molid top]} {
      set molid [molinfo top]
    }
    global vmd_frame
    trace vdelete vmd_frame($molid) w sscache
    return
}


# when the frame changes, trace calls this function
proc sscache {name index op} {
    # name == vmd_frame
    # index == molecule id of the newly changed frame
    # op == w
    
    global sscache_data

    # get the protein CA atoms
    set sel [atomselect $index "protein name CA"]

    ## get the new frame number
    # Tcl doesn't yet have it, but VMD does ...
    set frame [molinfo $index get frame]

    # see if the ss data exists in the cache
    if [info exists sscache_data($index,$frame)] {
        $sel set structure $sscache_data($index,$frame)
        return
    }

    # doesn't exist, so (re)calculate it
    vmd_calculate_structure $index
    # save the data for next time
    set sscache_data($index,$frame) [$sel get structure]

    return
}


next up previous contents index
Next: Viewing selections which change Up: Trajectory frames Previous: Trajectory frames

Justin Gullingsrud
Tue Apr 6 09:22:39 CDT 1999