scripting biomolecular simulations with tcl part of cs590v

24
10111010101011000111010111010101011110101111010111010101011000001010010100010101000101010101010110101010101000101010010101001010101000101010 10111010101011000111010111010101011110101111010111010101011000001010010100010101000101010101010110101010101000101010010101001010101000101010 Scripting Biomolecular Simulations with Scripting Biomolecular Simulations with Tcl Tcl part of CS590v Michael McLennan Senior Research Scientist Rosen Center for Advanced Computing, Purdue University [email protected]

Upload: bazyli

Post on 08-Feb-2016

55 views

Category:

Documents


0 download

DESCRIPTION

Scripting Biomolecular Simulations with Tcl part of CS590v. Michael McLennan Senior Research Scientist Rosen Center for Advanced Computing, Purdue University [email protected]. Biomolecular Simulation. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Scripting Biomolecular Simulations with Tcl part of CS590v

10111010101011000111010111010101011110101111010111010101011000001010010100010101000101010101010110101010101000101010010101001010101000101010

10111010101011000111010111010101011110101111010111010101011000001010010100010101000101010101010110101010101000101010010101001010101000101010

Scripting Biomolecular Simulations with TclScripting Biomolecular Simulations with Tclpart of CS590v

Michael McLennanSenior Research Scientist

Rosen Center for Advanced Computing, Purdue [email protected]

Page 2: Scripting Biomolecular Simulations with Tcl part of CS590v

10111010101011000111010111010101011110101111010111010101011000001010010100010101000101010101010110101010101000101010010101001010101000101010

10111010101011000111010111010101011110101111010111010101011000001010010100010101000101010101010110101010101000101010010101001010101000101010

Biomolecular SimulationBiomolecular 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 http://www.ks.uiuc.edu/Research/vmd/

Page 3: Scripting Biomolecular Simulations with Tcl part of CS590v

10111010101011000111010111010101011110101111010111010101011000001010010100010101000101010101010110101010101000101010010101001010101000101010

10111010101011000111010111010101011110101111010111010101011000001010010100010101000101010101010110101010101000101010010101001010101000101010

What is scripting?What is scripting?

Type in commands Write little programs Like Unix “shell” scripts

Page 4: Scripting Biomolecular Simulations with Tcl part of CS590v

10111010101011000111010111010101011110101111010111010101011000001010010100010101000101010101010110101010101000101010010101001010101000101010

10111010101011000111010111010101011110101111010111010101011000001010010100010101000101010101010110101010101000101010010101001010101000101010

What is Tcl?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: http://www.tcl.tk/

Tcl: language

button .b –text “Hello, World!”pack .b –pady 8

Tk: widget toolkit

Page 5: Scripting Biomolecular Simulations with Tcl part of CS590v

10111010101011000111010111010101011110101111010111010101011000001010010100010101000101010101010110101010101000101010010101001010101000101010

10111010101011000111010111010101011110101111010111010101011000001010010100010101000101010101010110101010101000101010010101001010101000101010

Application: UbiquitinApplication: Ubiquitin

Ubiquitin660 atoms

Carbon backbone drawn in green

Page 6: Scripting Biomolecular Simulations with Tcl part of CS590v

10111010101011000111010111010101011110101111010111010101011000001010010100010101000101010101010110101010101000101010010101001010101000101010

10111010101011000111010111010101011110101111010111010101011000001010010100010101000101010101010110101010101000101010010101001010101000101010

Application: UbiquitinApplication: Ubiquitin

Ubiquitin660 atoms

Carbon backbone drawn in green(tube method)

Page 7: Scripting Biomolecular Simulations with Tcl part of CS590v

10111010101011000111010111010101011110101111010111010101011000001010010100010101000101010101010110101010101000101010010101001010101000101010

10111010101011000111010111010101011110101111010111010101011000001010010100010101000101010101010110101010101000101010010101001010101000101010

Application: UbiquitinApplication: Ubiquitin

Ubiquitin660 atoms

= starting point= after simulation

Page 8: Scripting Biomolecular Simulations with Tcl part of CS590v

10111010101011000111010111010101011110101111010111010101011000001010010100010101000101010101010110101010101000101010010101001010101000101010

10111010101011000111010111010101011110101111010111010101011000001010010100010101000101010101010110101010101000101010010101001010101000101010

Application: UbiquitinApplication: Ubiquitin

Ubiquitin660 atoms

= small change= medium change= large change

Movement after simulation:

Page 9: Scripting Biomolecular Simulations with Tcl part of CS590v

10111010101011000111010111010101011110101111010111010101011000001010010100010101000101010101010110101010101000101010010101001010101000101010

10111010101011000111010111010101011110101111010111010101011000001010010100010101000101010101010110101010101000101010010101001010101000101010

Coloring script for comparing moleculesColoring 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}

Tcl code!

Page 10: Scripting Biomolecular Simulations with Tcl part of CS590v

10111010101011000111010111010101011110101111010111010101011000001010010100010101000101010101010110101010101000101010010101001010101000101010

10111010101011000111010111010101011110101111010111010101011000001010010100010101000101010101010110101010101000101010010101001010101000101010

unix> tclsh

% puts “Hello, World!”Hello, World!

% puts –nonewline “hello”hello% %% exitunix>

unix> tclsh

% puts “Hello, World!”Hello, World!

% puts –nonewline “hello”hello%

““Hello, World!” in TclHello, World!” in Tcl

Start up a Tcl application:unix> tclsh

unix> wish

unix> vmd

oror

Like Unix shell, but speaks Tcltclsh + widgetsVMD molecular visualization

unix> tclsh

% puts “Hello, World!”Hello, World!%

For example:

Command to write out a stringOutput written on stdout by defaultAdd options to commandsOutput written without trailing newline

Exit tclsh

Page 11: Scripting Biomolecular Simulations with Tcl part of CS590v

10111010101011000111010111010101011110101111010111010101011000001010010100010101000101010101010110101010101000101010010101001010101000101010

10111010101011000111010111010101011110101111010111010101011000001010010100010101000101010101010110101010101000101010010101001010101000101010

Tcl command syntaxTcl command syntax

puts –nonewline “Hello, World!”

command arg arg …

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 commentputs “Hello”; puts “World!”puts –nonewline \ “Hello, World!”

Page 12: Scripting Biomolecular Simulations with Tcl part of CS590v

10111010101011000111010111010101011110101111010111010101011000001010010100010101000101010101010110101010101000101010010101001010101000101010

10111010101011000111010111010101011110101111010111010101011000001010010100010101000101010101010110101010101000101010010101001010101000101010

Tcl DocumentationTcl Documentation

http://www.tcl.tk/doc/

Page 13: Scripting Biomolecular Simulations with Tcl part of CS590v

10111010101011000111010111010101011110101111010111010101011000001010010100010101000101010101010110101010101000101010010101001010101000101010

10111010101011000111010111010101011110101111010111010101011000001010010100010101000101010101010110101010101000101010010101001010101000101010

Tcl VariablesTcl 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 3.14159 puts “Value of pi is $pi” Value of pi is 3.14159

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

Page 14: Scripting Biomolecular Simulations with Tcl part of CS590v

10111010101011000111010111010101011110101111010111010101011000001010010100010101000101010101010110101010101000101010010101001010101000101010

10111010101011000111010111010101011110101111010111010101011000001010010100010101000101010101010110101010101000101010010101001010101000101010

Doing Math in TclDoing Math in Tcl

Mathematical Expressions: set pi 3.14159 set r 10 puts “Circumference is 2*$pi*$r”

Why didn’t we get a number here? Circumference is 2*3.14159*10

expr 2+2 4

Use the expr command to do math

expr 2*$pi*$r 62.8318

The right way to get circumference

puts “Circumference is expr 2*$pi*$r”Oops! Still not quite right! Circumference is expr 2*3.14159*10

puts “Circumference is [expr 2*$pi*$r]”

Execute command and substitute result in its place Circumference is 62.8318

Page 15: Scripting Biomolecular Simulations with Tcl part of CS590v

10111010101011000111010111010101011110101111010111010101011000001010010100010101000101010101010110101010101000101010010101001010101000101010

10111010101011000111010111010101011110101111010111010101011000001010010100010101000101010101010110101010101000101010010101001010101000101010

Tcl Versus Other LanguagesTcl Versus Other Languages

Pythagorean Theorem expressed in Tcl:

ab c

c = a2+b2

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))]

Page 16: Scripting Biomolecular Simulations with Tcl part of CS590v

10111010101011000111010111010101011110101111010111010101011000001010010100010101000101010101010110101010101000101010010101001010101000101010

10111010101011000111010111010101011110101111010111010101011000001010010100010101000101010101010110101010101000101010010101001010101000101010

Tcl Quoting RulesTcl 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!

Page 17: Scripting Biomolecular Simulations with Tcl part of CS590v

10111010101011000111010111010101011110101111010111010101011000001010010100010101000101010101010110101010101000101010010101001010101000101010

10111010101011000111010111010101011110101111010111010101011000001010010100010101000101010101010110101010101000101010010101001010101000101010

QuizQuiz

Problem #1 set x 10 puts x x

Problem #2 set set set puts $set set

Problem #3 set pi 3.14159 set area {expr $pi*$r*$r} puts “Area is: $area” Area is: expr $pi*$r*$r

Page 18: Scripting Biomolecular Simulations with Tcl part of CS590v

10111010101011000111010111010101011110101111010111010101011000001010010100010101000101010101010110101010101000101010010101001010101000101010

10111010101011000111010111010101011110101111010111010101011000001010010100010101000101010101010110101010101000101010010101001010101000101010

Command ScriptsCommand Scripts

Save a series of commands in a command script file:

# This is a command scriptputs –nonewline “What’s your name?”set name [gets stdin]puts “Hello, $name!”

File: hello.tcl

unix> tclsh% source hello.tclWhat’s your name? FredHello, Fred!

% source hello.tclWhat’s your name?

Load the script interactively: Run as a program:unix> tclsh hello.tclWhat’s your name? FredHello, Fred!

unix>

Page 19: Scripting Biomolecular Simulations with Tcl part of CS590v

10111010101011000111010111010101011110101111010111010101011000001010010100010101000101010101010110101010101000101010010101001010101000101010

10111010101011000111010111010101011110101111010111010101011000001010010100010101000101010101010110101010101000101010010101001010101000101010

Programming StatementsProgramming 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

Page 20: Scripting Biomolecular Simulations with Tcl part of CS590v

10111010101011000111010111010101011110101111010111010101011000001010010100010101000101010101010110101010101000101010010101001010101000101010

10111010101011000111010111010101011110101111010111010101011000001010010100010101000101010101010110101010101000101010010101001010101000101010

Lists of ValuesLists 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

Page 21: Scripting Biomolecular Simulations with Tcl part of CS590v

10111010101011000111010111010101011110101111010111010101011000001010010100010101000101010101010110101010101000101010010101001010101000101010

10111010101011000111010111010101011110101111010111010101011000001010010100010101000101010101010110101010101000101010010101001010101000101010

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 listAdd 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]

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]

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]

name argument list

Procedure declaration

Return value from procedure call

Functions and ProceduresFunctions and Procedures

Reverse a list:

Page 22: Scripting Biomolecular Simulations with Tcl part of CS590v

10111010101011000111010111010101011110101111010111010101011000001010010100010101000101010101010110101010101000101010010101001010101000101010

10111010101011000111010111010101011110101111010111010101011000001010010100010101000101010101010110101010101000101010010101001010101000101010

VMD CommandsVMD 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

Page 23: Scripting Biomolecular Simulations with Tcl part of CS590v

10111010101011000111010111010101011110101111010111010101011000001010010100010101000101010101010110101010101000101010010101001010101000101010

10111010101011000111010111010101011110101111010111010101011000001010010100010101000101010101010110101010101000101010010101001010101000101010

VMD CommandsVMD 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

Page 24: Scripting Biomolecular Simulations with Tcl part of CS590v

10111010101011000111010111010101011110101111010111010101011000001010010100010101000101010101010110101010101000101010010101001010101000101010

10111010101011000111010111010101011110101111010111010101011000001010010100010101000101010101010110101010101000101010010101001010101000101010

Coloring script for comparing moleculesColoring 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}