Scripting Biomolecular Simulations with Tcl Scripting Biomolecular Simulations with Tcl part of CS590v Michael McLennan Senior Research Scientist Rosen Center for Advanced Computing, Purdue University
Biomolecular Simulation developed by the Theoretical and Computational Biophysics Group in the Beckman Institute for Advanced Science and Technology at the University of Illinois at Urbana-Champaign Download from
What is scripting? Type in commands Write little programs Like Unix “shell” scripts
What is Tcl? Developed by John Ousterhout at UC Berkeley Released in 1989, currently maintained as Open Source Millions of users worldwide Used in commercial CAD tools and products (TiVo!) More info: Tcl: language button.b –text “Hello, World!” pack.b –pady 8 button.b –text “Hello, World!” pack.b –pady 8 Tk: widget toolkit
Application: Ubiquitin Ubiquitin 660 atoms Carbon backbone drawn in green
Application: Ubiquitin Ubiquitin 660 atoms Carbon backbone drawn in green (tube method)
Application: Ubiquitin Ubiquitin 660 atoms = starting point = after simulation
Application: Ubiquitin Ubiquitin 660 atoms = small change = medium change = large change Movement after simulation:
Coloring script for comparing molecules A little script to color according to atomic movements: proc tutorialcoloring {} { set mol1 [lindex [molinfo list] 0] set mol2 [lindex [molinfo list] 1] set sel0 [atomselect $mol1 "alpha and protein"] ;# crystal set sel1 [atomselect $mol2 "alpha and protein"] ;# simulation set mylist {} foreach v0 [$sel0 get {x y z}] v1 [$sel1 get {x y z}] { set dx [expr [lindex $v0 0] - [lindex $v1 0]] set dy [expr [lindex $v0 1] - [lindex $v1 1]] set dz [expr [lindex $v0 2] - [lindex $v1 2]] set disp [expr ($dx*$dx + $dy*$dy + $dz*$dz)] lappend mylist $disp } $sel0 set beta $mylist } proc tutorialcoloring {} { set mol1 [lindex [molinfo list] 0] set mol2 [lindex [molinfo list] 1] set sel0 [atomselect $mol1 "alpha and protein"] ;# crystal set sel1 [atomselect $mol2 "alpha and protein"] ;# simulation set mylist {} foreach v0 [$sel0 get {x y z}] v1 [$sel1 get {x y z}] { set dx [expr [lindex $v0 0] - [lindex $v1 0]] set dy [expr [lindex $v0 1] - [lindex $v1 1]] set dz [expr [lindex $v0 2] - [lindex $v1 2]] set disp [expr ($dx*$dx + $dy*$dy + $dz*$dz)] lappend mylist $disp } $sel0 set beta $mylist } Tcl code!
unix> tclsh % puts “Hello, World!” Hello, World! % puts –nonewline “hello” hello% % % exit unix> unix> tclsh % puts “Hello, World!” Hello, World! % puts –nonewline “hello” hello% % % exit unix> unix> tclsh % puts “Hello, World!” Hello, World! % puts –nonewline “hello” hello% unix> tclsh % puts “Hello, World!” Hello, World! % puts –nonewline “hello” hello% “Hello, World!” in Tcl Start up a Tcl application: unix> tclsh unix> wish unix> vmd unix> tclsh unix> wish unix> vmd or Like Unix shell, but speaks Tcl tclsh + widgets VMD molecular visualization unix> tclsh % puts “Hello, World!” Hello, World! % unix> tclsh % puts “Hello, World!” Hello, World! % For example: Command to write out a string Output written on stdout by default Add options to commands Output written without trailing newline Exit tclsh
Tcl command syntax puts –nonewline “Hello, World!” commandarg … First word is the command name Remaining arguments depend on command syntax Use double-quotes ( “” ) to wrap up text strings Any line starting with hash ( # ) is a comment Use semicolon ( ; ) to separate multiple commands on same line Use backslash ( \ ) to extend a command onto multiple lines # this is a comment puts “Hello”; puts “World!” puts –nonewline \ “Hello, World!”
Tcl Documentation
Tcl Variables Variables hold numbers, strings, and other values: set x “World” puts “Hello, $x!” Hello, World! Uses the value of variable named x set pi puts “Value of pi is $pi” Value of pi is Value can be a number set cmd puts $cmd “Hello, World!” Hello, World! Value can be a command name set var x set $var “Universe” puts “Hello, $x!” Hello, Universe! Value can be a variable name
Doing Math in Tcl Mathematical Expressions: set pi set r 10 puts “Circumference is 2*$pi*$r” Why didn’t we get a number here? Circumference is 2* *10 expr 2+2 4 Use the expr command to do math expr 2*$pi*$r The right way to get circumference puts “Circumference is expr 2*$pi*$r” Oops! Still not quite right! Circumference is expr 2* *10 puts “Circumference is [expr 2*$pi*$r]” Execute command and substitute result in its place Circumference is
Tcl Versus Other Languages Pythagorean Theorem expressed in Tcl: a b c c = a 2 +b 2 set a 3 set b 4 set c [expr sqrt($a*$a + $b*$b)] set a 3 set b 4 set c [expr sqrt($a*$a + $b*$b)] Use ()’s for functions/grouping Use C-like math functions In C you might say…In Tcl you say… x = x + 1; set x [expr $x + 1] x += 2; incr x 2 set x [expr $x+2] Y = sin(2*x/(x-1)); set y [expr sin(2*$x/($x-1))]
Tcl Quoting Rules Tcl also supports {} quotes: puts “Hello, World!” puts {Hello, World!} Both keep text together puts “Hello, World!” puts {Hello, World!} Both handle multi-line strings The difference is important when you have substitutions: set x “World” puts “Hello, $x!” Hello, World! set x “World” puts {Hello, $x!} Hello, $x! Curly braces prevent substitutions!
Quiz Problem #1 set x 10 puts x x x Problem #2 set set set puts $set set Problem #3 set pi set area {expr $pi*$r*$r} puts “Area is: $area” Area is: expr $pi*$r*$r
Command Scripts Save a series of commands in a command script file: # This is a command script puts –nonewline “What’s your name?” set name [gets stdin] puts “Hello, $name!” # This is a command script puts –nonewline “What’s your name?” set name [gets stdin] puts “Hello, $name!” File: hello.tcl unix> tclsh % source hello.tcl What’s your name? Fred Hello, Fred! % source hello.tcl What’s your name? Load the script interactively:Run as a program: unix> tclsh hello.tcl What’s your name? Fred Hello, Fred! unix>
Programming Statements Conditionals: if {$x > 0} { statements } if {$x > 0} { statements } elseif {$x < 0} { statements } else { statements } switch -regexp $x { a.*z { statements } [0-9]+ { statements } foo – bar { statements } } Looping: while {$x != 0} { statements } for {set x 0} {$x < 10} {incr x} { statements } foreach x {a b c d e} { statements } break continue Break out of loop Go back to top of loop
Lists of Values Variables can hold lists of values set x {a b c d e} llength $x 5 Space-separated list of values set x “a b c d e” llength $x 5 Nothing special about quotes lappend x f lappend x g h puts $x a b c d e f g h Add to list stored in variable x lindex $x 0 a lindex $x 1 b lindex $x end h Extract an element from a list
proc lreverse {list} { set rlist “” set max [expr [llength $list]-1] for {set i $max} {$i >= 0} {incr i -1} { set v [lindex $list $i] lappend rlist $v } return $rlist } set x {a b c d e f g} set rx [lreverse $x] proc lreverse {list} { set rlist “” set max [expr [llength $list]-1] for {set i $max} {$i >= 0} {incr i -1} { set v [lindex $list $i] lappend rlist $v } return $rlist } set x {a b c d e f g} set rx [lreverse $x] Empty list Get element from list Add to end of return list proc lreverse {list} { set rlist “” set max [expr [llength $list]-1] for {set i $max} {$i >= 0} {incr i -1} { set v [lindex $list $i] lappend rlist $v } return $rlist } set x {a b c d e f g} set rx [lreverse $x] proc lreverse {list} { set rlist “” set max [expr [llength $list]-1] for {set i $max} {$i >= 0} {incr i -1} { set v [lindex $list $i] lappend rlist $v } return $rlist } set x {a b c d e f g} set rx [lreverse $x] i = max; i >= 0; i-- proc lreverse {list} { set rlist “” set max [expr [llength $list]-1] for {set i $max} {$i >= 0} {incr i -1} { set v [lindex $list $i] lappend rlist $v } return $rlist } set x {a b c d e f g} set rx [lreverse $x] proc lreverse {list} { set rlist “” set max [expr [llength $list]-1] for {set i $max} {$i >= 0} {incr i -1} { set v [lindex $list $i] lappend rlist $v } return $rlist } set x {a b c d e f g} set rx [lreverse $x] Substitutions (innermost to outermost) proc lreverse {list} { set rlist “” set max [expr [llength $list]-1] for {set i $max} {$i >= 0} {incr i -1} { set v [lindex $list $i] lappend rlist $v } return $rlist } set x {a b c d e f g} set rx [lreverse $x] proc lreverse {list} { set rlist “” set max [expr [llength $list]-1] for {set i $max} {$i >= 0} {incr i -1} { set v [lindex $list $i] lappend rlist $v } return $rlist } set x {a b c d e f g} set rx [lreverse $x] nameargument list Procedure declaration Return value from procedure call Functions and Procedures Reverse a list:
VMD Commands VMD is just like tclsh, but with extra Tcl commands: vmd > molinfo list 0 1 vmd > atomselect0 num 660 top molecule vmd > atomselect0 set radius 10 vmd > atomselect0 set radius 2 vmd > atomselect top all atomselect0 molecules vmd > atomselect top alpha atomselect1 vmd > atomselect1 set radius 5 vmd > atomselect top hydrophobic atomselect2 vmd > atomselect2 set radius 10
VMD Commands Use variables to store selections: vmd > set s [atomselect top all] vmd > $s num 660 vmd > $s set radius 2 vmd > set s2 [atomselect top alpha] vmd > $s2 set radius 5 vmd > $s2 num 76
Coloring script for comparing molecules A little script to color according to atomic movements: proc tutorialcoloring {} { set mol1 [lindex [molinfo list] 0] set mol2 [lindex [molinfo list] 1] set sel0 [atomselect $mol1 "alpha and protein"] ;# crystal set sel1 [atomselect $mol2 "alpha and protein"] ;# simulation set mylist {} foreach v0 [$sel0 get {x y z}] v1 [$sel1 get {x y z}] { set dx [expr [lindex $v0 0] - [lindex $v1 0]] set dy [expr [lindex $v0 1] - [lindex $v1 1]] set dz [expr [lindex $v0 2] - [lindex $v1 2]] set disp [expr ($dx*$dx + $dy*$dy + $dz*$dz)] lappend mylist $disp } $sel0 set beta $mylist } proc tutorialcoloring {} { set mol1 [lindex [molinfo list] 0] set mol2 [lindex [molinfo list] 1] set sel0 [atomselect $mol1 "alpha and protein"] ;# crystal set sel1 [atomselect $mol2 "alpha and protein"] ;# simulation set mylist {} foreach v0 [$sel0 get {x y z}] v1 [$sel1 get {x y z}] { set dx [expr [lindex $v0 0] - [lindex $v1 0]] set dy [expr [lindex $v0 1] - [lindex $v1 1]] set dz [expr [lindex $v0 2] - [lindex $v1 2]] set disp [expr ($dx*$dx + $dy*$dy + $dz*$dz)] lappend mylist $disp } $sel0 set beta $mylist }