PYTHON FOR STRUCTURAL BIOINFORMATICS1 February 3-6 2003 BioCon 2003 - San Diego, CA - 1 PYTHON FOR...
Transcript of PYTHON FOR STRUCTURAL BIOINFORMATICS1 February 3-6 2003 BioCon 2003 - San Diego, CA - 1 PYTHON FOR...
1
February 3-6 2003 BioCon 2003 - San Diego, CA - 1
PYTHON FOR STRUCTURALBIOINFORMATICS
Sophie COON & Michel SANNER
MGL laboratoryThe Scripps Research Institute, La Jolla, CA
February 3-6 2003 BioCon 2003 - San Diego, CA - 2
SCHEDULE
I- Introduction 15 minutes
• Background and motivation• Brief Python overview
II- PMV 20 minutes
• Fundamental concepts, main commands …III- PMV BUILDING BLOCS 30 minutes
• MolKit, DejaVu, ViewerFrameworkBREAK 15 minutes
2
February 3-6 2003 BioCon 2003 - San Diego, CA - 3
SCHEDULE
IV- Extending PMV 1 hour
• Scripting capabilities in PMV? scripts? macros? commands
• Visual Programming Environment in PMV
V- Conclusion 15 minutes
Questions 15 minutes
February 3-6 2003 BioCon 2003 - San Diego, CA - 4
I- INTRODUCTION
?Background and motivation
• Laboratory
• Challenge
• Software development strategy
?Introduction to Python
3
February 3-6 2003 BioCon 2003 - San Diego, CA - 5
Protein-Ligand
Complex AssembliesProtein-Protein
Molecular SurfacesAutoDock
SurfDock
SymGen
MSMS
HARMONY
SymSearch
February 3-6 2003 BioCon 2003 - San Diego, CA - 6
The Challenge
Visualization
DockingMethods
Folding
ProteinEngineering
SequenceAnalysis
Modeling
MM - MD
Ab InitioMethods
ElectrostaticsCalculations
MolecularSurfaces
Etc ...
4
February 3-6 2003 BioCon 2003 - San Diego, CA - 7
“Traditional” solution
Visualization
DockingMethods
Folding
ProteinEngineering
SequenceAnalysis
Modeling
MM - MD
Ab InitioMethods
ElectrostaticsCalculations
MolecularSurfaces
Etc ...
N2 interfaces
low interoperability
No code Reuse
February 3-6 2003 BioCon 2003 - San Diego, CA - 8
Our solution
High level language as a scripting environment
MolecularSurfaces
Molecules
DataBaseElectrostatics
Delaunay
HomologyCSG
3DViewer
MM-MDNewMethod
Your
Method
Interactive
Dynamic
Platformindependent
5
February 3-6 2003 BioCon 2003 - San Diego, CA - 9
Writing an application
Molecules
MolecularSurfaces
Electrostatics
?High level coding?Code re-use?Extensible
3D Viewer
February 3-6 2003 BioCon 2003 - San Diego, CA - 10
Why Python ?
?Object oriented? Advanced data structures? Powerful data-parallel arrays?Readability and modularity?High level? Platform independence? Interpreted
Tcl, Perl, C, ..Tcl, PerlTclPerlC, C++, FortranC, C++, Java, …C, C++, Java
features: Not met by:
6
February 3-6 2003 BioCon 2003 - San Diego, CA - 11
I- INTRODUCTION
? Background and motivation? Python overview
• Language characteristics
• Basics types
• Control flow
• Functions
• More...
February 3-6 2003 BioCon 2003 - San Diego, CA - 12
Language characteristics
?Interpreted, high level, object oriented?Flexible and extensible?Introspection and self-documentation?Platform independent?Open source?Rapidly gaining acceptance?Access to C, C++ fortran code…
7
February 3-6 2003 BioCon 2003 - San Diego, CA - 13
Data types and data structures
• basic data types:
>>> a=2int
>>> b=7.3float
>>> s=“name”>>> s=‘name’
string
No memory allocation and no variable declaration.
• data structures:
>>> l = [1,2,5]>>> l[0]1>>> l[2] = 6>>> l[1:2][6,5]
list:mutable sequence
>>> t = (1,2,5)>>> t[-1] 5>>> t[3] = 6>>> t[:2] (1,2)
Tupleimmutable sequence
>>> t = {‘so’:23}>>> t[‘so’]23>>> t[‘new’] = ‘45’>>> t.items()[(‘so’,23), (‘new’, ‘45’)
Dictionaryassociative arrays
indexing
Slicing[from:to[
February 3-6 2003 BioCon 2003 - San Diego, CA - 14
Control Flow
? While :>>> b = 0>>> while b < 5:. . . print b; b=b+10 1 2 3 4
? If :>>> q = ‘Please Enter a number'>>> x = int( raw_input (q) )>>> if x == 0:. . . print ‘x equals 0’. . . elif x < 0:. . . print ‘ x is negative’. . . else:. . . print ‘x is positive’
? For, range(), break, continue :>>> seq = range(-3, 4, 1)>>> print seq[-3,-2,-1,0,1,2,3]>>> for s in seq:. . . if s < 0:. . . continue. . . else:. . . print s0,1,2,3
8
February 3-6 2003 BioCon 2003 - San Diego, CA - 15
Function and arguments
def func( a, b, n1=10, n2=‘hello’ ):
Positional arguments (required)
Named arguments (optional)Function name
>>> func( 2, 'string', 3.14 )
>>> func( 7.2, 'string', n2=15)
>>> func( 'hello', 2 , 5, ’bye’)
>>> func( n1 = 5 )
>>> func( n1=12, 3.14 )
Argument matching:
2 'string’ 3.14 'hello'
7.2 'string' 10 15
'hello' 2 5 'bye'
a b n1 n2
ERROR: missing argumentERROR: positional argument after named argument
February 3-6 2003 BioCon 2003 - San Diego, CA - 16
More ...
?Classes?Exceptions?Packages?etc…?Tutorial:
• http://www.python.org/doc
9
February 3-6 2003 BioCon 2003 - San Diego, CA - 17
II- PMV:
? Fundamentals? Basic commands? More advanced manipulations
February 3-6 2003 BioCon 2003 - San Diego, CA - 18
PMV: Python-based Molecule Viewer
PMV
10
February 3-6 2003 BioCon 2003 - San Diego, CA - 19
III- PMV BUILDING BLOCS
? MolKit• Hierarchical data structures
• Derived classes
• Parsers
• Examples
? DejaVu
? ViewerFramework
February 3-6 2003 BioCon 2003 - San Diego, CA - 20
Hierachical data-structure
TreeNode
.parent
.children
...
TreeNode
adopt(child)TreeNodeSet(ListSet)
[TreeNode, TreeNode, … ]
.top
.elementType
TreeNode Numeric
MolKit
PyBabel
11
February 3-6 2003 BioCon 2003 - San Diego, CA - 21
TreeNode and TreeNodeSet specialization
Residue Chain Protein ...
Molecule Atom
Helix Strand Turn Coil
SecondayStructure ...
TreeNode
ResidueSet ChainSet ProteinSet ...
MoleculeSet AtomSet
HelixSet StrandSet TurnSet CoilSet
SecondayStructureSet ...
TreeNodeSet
TreeNode Specialization
TreeNodeSet Specialization:
February 3-6 2003 BioCon 2003 - San Diego, CA - 22
Parsers
PDBMol2PQR...
Parser
MoleculeSetfrom MolKit.pdbParser import PdbParserparser = PdbParser(‘./1crn.pdb’)mols = parser.parse( )
Molecule
C1, C2, C3 chainSet
R1, R2, R3, ... ResidueSet
AtomSetA1, A2, A3, ...
12
February 3-6 2003 BioCon 2003 - San Diego, CA - 23
Example
# Importing the Read function from the MolKit package.>>> from MolKit import Read# Read calls the proper parser depending on the file extension>>> mols = Read(“./1crn.pdb”)>>> mol = mols[0]# print the name of all the residues of all the chains of mol>>> print mol.chains.residues.name# call the full_name method of the atoms 20 to 85 and print the# result>>> print mol.chains.residues.atoms[28:85].full_name()
February 3-6 2003 BioCon 2003 - San Diego, CA - 24
Example
# import the Atom class located in the molecule module in the# MolKit package>>> from MolKit.molecule import Atom# find in the ‘mol’ tree all the nodes of type Atom.>>> allAtoms = mol.findType(Atom)# get the subset of these atoms for which the value of the# temperatureFactor attribute is strictly superior to 20.>>> set1 = allAtoms.get(lambda x: x.temperatureFactor > 20)
13
February 3-6 2003 BioCon 2003 - San Diego, CA - 25
Example
# get the parent of the atoms and get rid off the duplicates>>> allResidues = allAtoms.parent.uniq()>>> allResidues = allAtoms.residues.uniq()# import the Numeric extension>>> import Numeric# Compute the geomCenter of each residue and store it in# a new attribute called geomCenter.>>>for r in allResidues:. . . coords = r.atoms.coords. . . r.geomCenter = Numeric.sum(coords)/len(coords)
or
February 3-6 2003 BioCon 2003 - San Diego, CA - 26
III- PMV BUILDING BLOCS
? MolKit? DejaVu:
• Overview• Features• DejaVu and MolKit
?ViewerFramework
14
February 3-6 2003 BioCon 2003 - San Diego, CA - 27
Overview
DEMO
from DejaVu import Viewervi = Viewer( )
from DejaVu.Spheres import Spherescenters = =[[0,0,0],[3,0,0],[0,3,0]] s = Spheres(‘sph’, centers = centers)s.Set(quality=10)vi.AddObject(s)
Numeric
PyOpenGL
Tkinter
DejaVu
February 3-6 2003 BioCon 2003 - San Diego, CA - 28
Demo Code
>>> from DejaVu import Viewer>>> vi = Viewer( )
>>> from DejaVu.Spheres import Spheres>>> centers = =[[0,0,0],[3,0,0],[0,3,0]]>>> s = Spheres(‘sph’, centers = centers)>>> s.Set(quality=10)
>>> vi.AddObject(s)
15
February 3-6 2003 BioCon 2003 - San Diego, CA - 29
Features
?OpenGL Lighting and Material model?Object hierarchy with transformation and
rendering properties inheritance?Arbitrary clipping planes?Material editor?DepthCueing (fog), global anti-aliasing?glSscissors/magic lens?Multi-level picking?Extensible set of geometries
February 3-6 2003 BioCon 2003 - San Diego, CA - 30
DejaVu and MolKit
DEMO
16
February 3-6 2003 BioCon 2003 - San Diego, CA - 31
Demo Code
>>> from MolKit import Read>>> molecules = Read(‘/tsri/pdb/struct/1crn.pdb’)>>> mol = molecules[0] # Read returns a ProteinSet
>>> coords = allAtoms.coords>>> radii = allAtoms.radius
>>> sph = Spheres(‘sph’, centers = coords, radii = radii, quality=10)
>>> vi.AddObject(sph)
February 3-6 2003 BioCon 2003 - San Diego, CA - 32
III - PMV BUILDING BLOCS
? MolKit? DejaVu? ViewerFramework
• Overview• Design features• Putting it all together
17
February 3-6 2003 BioCon 2003 - San Diego, CA - 33
Overview
ViewerFramework
Idle
Numeric
PyOpenGL
Tkinter
DejaVu
February 3-6 2003 BioCon 2003 - San Diego, CA - 34
Design features
?Dynamic loading of commands?Python shell for scripting?Dual interaction mode (GUI/Shell)?Support for command:
• development, logging, GUI, dependencies
?Lightweight commands: Macros?Dynamic commands (introspection)?Extensible set of commands
18
February 3-6 2003 BioCon 2003 - San Diego, CA - 35
Putting it all together
Numeric PyOpenGL Tkinter
DejaVu
ViewerFramework
IdleMolKitMslib
MsmsCommands
February 3-6 2003 BioCon 2003 - San Diego, CA - 36
BREAK
19
February 3-6 2003 BioCon 2003 - San Diego, CA - 37
IV- EXTENDING PMV
? Writing a resource file? Writing a python script? Writing a Macro? Writing a PMV Commands? Visual Programming in Pmv
February 3-6 2003 BioCon 2003 - San Diego, CA - 38
Writing a resource file
?Execute a set of PMV commands• read a molecule (protease.pdb)• load and compute msms surface• color by atomType
?Open myrc.py in an editor?Copy and paste the command log
string?In a new PMV session:
• File -> source -> myrc.py
20
February 3-6 2003 BioCon 2003 - San Diego, CA - 39
Write a distance script
?Goal:• color the molecular surface of the protease by
the smallest distance to all atoms in theligand.
?What do we need:• compute this distance and store the value in a
new atom attribute. Use this new property tocolor the molecular surface of the protease
February 3-6 2003 BioCon 2003 - San Diego, CA - 40
closestDistance.py
import Numeric# Get a handle on the protease atomsproteaseAtoms = self.Mols[0].allAtoms# Get a handle on the ligand atomsligandAtoms = self.Mols[1].allAtomsdef distanceToClosestPoint(point, setOfPoints): """computes the shortest distance between ‘point’ and
‘setOfPoints’""" diff = Numeric.array(point) -
Numeric.array(setOfPoints) diff = diff*diff len = Numeric.sqrt(Numeric.sum(diff,1)) return min(len)
21
February 3-6 2003 BioCon 2003 - San Diego, CA - 41
closestDistance.py
# Get a handle on the coordinates of all the atoms of the ligand
ligandAtomsCoords = ligandAtoms.coords# Call the distanceToClosestPoint for each atom of the protease# and store the result in a new atom attribute called “closest”
for a in proteaseAtoms: a.closest = distanceToClosestPoint( a.coords, ligandAtomsCoords)
February 3-6 2003 BioCon 2003 - San Diego, CA - 42
In PMV Color msms by distance.
File -> ReadMolecule protease.pdb
File -> ReadMoleculeindinavir.pdb
File -> source-> script ->distance.py
Select -> Select From String ->“protease”
Compute -> MSMS for MolColor -> By Property ->
Atom, ‘closest’, RGB
22
February 3-6 2003 BioCon 2003 - San Diego, CA - 43
Writing the distanceMac.py macro
import Numeric# Implement a classdef ClosestAtom(): """ask for 2 molecules, then compute for each atom the distance to the closest atom in the other molecule. After execution, each atom has a new attribute 'closest' holding the distance""" from Pmv.guiTools import MoleculeChooser # Create a gui to let the user choose the two molecules p = MoleculeChooser(self, 'extended', 'Choose 2 molecules') mols = p.go(modal=1) if len(mols) != 2:
print “ERROR: two molecules need to be selected “ return
mol1Atoms = mols[0].allAtoms mol2Atoms = mols[1].allAtoms
February 3-6 2003 BioCon 2003 - San Diego, CA - 44
Writing the distanceMac.py macro
def distanceToClosestPoint(point, setOfPoints): """computes the shortest distance between point and setOfPoints""" diff = Numeric.array(point) - Numeric.array(setOfPoints) diff = diff*diff len = Numeric.sqrt(Numeric.sum(diff,1)) return min(len)
mol2AtomsCoords = mol2Atoms.coords for a in mol1Atoms: a.closest = distanceToClosestPoint( a.coords, mol2AtomsCoords)
mol1AtomsCoords = mol1Atoms.coords for a in mol2Atoms: a.closest = distanceToClosestPoint( a.coords, mol1AtomsCoords)
23
February 3-6 2003 BioCon 2003 - San Diego, CA - 45
Loading the macro in PMV
File -> loadMacro -> Open Macro LibrarydistanceMac.py
Macros -> distanceMac -> closestDistanceprotease, ligand
February 3-6 2003 BioCon 2003 - San Diego, CA - 46
Command overview
def __call__: Hook to call the command from pyShell
def doit : implements what the command does
def guiCallback: Hook to get user inputs
class MyCommand(MVCommand):
def setUndo: Hook to have an undoable command
def checkDependencies : Where command’s dependencies are checked
def onAddObjectToViewer: Where new molecules geometries are created ...
Etc ...
24
February 3-6 2003 BioCon 2003 - San Diego, CA - 47
IV- EXTENDING PMV
? Visual Programming• overview• viperCommands• radii of cpk by properties• color msms of ligand by distance to the protease.
February 3-6 2003 BioCon 2003 - San Diego, CA - 48
Overview
Enabling scientists (non-programmers)to build computational networks
25
February 3-6 2003 BioCon 2003 - San Diego, CA - 49
Overview
Computational Flow: no data duplication
Dynamic: on-the-fly node editing
High-level: node programmed using Python
Portable: SunOS, IRIX, OSF, Linux, windows
Scriptable: Python interpreter
Component based: NetworkEditor, DejaVu, MolKit,…
A User Interface NOT an Environment
February 3-6 2003 BioCon 2003 - San Diego, CA - 50
ViPEr architecture
NetworkEditor
Numeric
PyOpenGL
Tkinter
DejaVu
Visualization
MolKit
MolKitStandard
Web
PIL
ImagingSymServ
SymServerYourCode
YourLibrary
Mslib
26
February 3-6 2003 BioCon 2003 - San Diego, CA - 51
Example 1: Goal
DEMO
GOAL:Modulate the radiiof the CPK by anatom property
February 3-6 2003 BioCon 2003 - San Diego, CA - 52
1- Start pmv2- Read molecule, display by CPK and color by
Atom TypeFile -> ReadMolecule -> 1crn.pdbUn/display -> by CPK -> OKColor -> by AtomType ->”cpk” ->OK
3- start VPE:buttonbar -> VPEdrag the needed nodes on the canvascreate and run the network
Example 1: Instructions
27
February 3-6 2003 BioCon 2003 - San Diego, CA - 53
Example 1: Nodes location
Library Name Category Name Node Name
Pmv3D VisualizationStandardStandardPmvMolKitMolKitStandard3D Visualization
PMVFilterPythonMapperMoleculesFilterInputInputoutput
Pmv viewerChoose GeomCall Methodarray Ufunc21crnselect NodesExtract Atom PropertyDialRedraw
February 3-6 2003 BioCon 2003 - San Diego, CA - 54
Example 1: Create the network
1crn
Dial
0.02
Select Nodes.*
Atom
Extract Atom PropertytemperatureFactor
Array Ufunc2multiply
Set radiicall method
root|1crn|cpkChoose Geom
Redraw
Pmv viewer
28
February 3-6 2003 BioCon 2003 - San Diego, CA - 55
Example 2: Goal
Goal:Color the molecular surface of the protease by the closest distance to the ligand
DEMO
February 3-6 2003 BioCon 2003 - San Diego, CA - 56
Example 2: Instructions
1- Start pmv2- Read protease and indinavir
File -> ReadMolecule -> protease.pdbFile -> ReadMolecule -> indinavir.pdbselectFromString -> “protease”compute -> MSMSMol
3- start VPE:buttonbar -> VPEdrag the needed nodes on the canvascreate and run the network
4- Color the surface with the new atom propertycolor -> By Property -> msms -> Atom, closest
29
February 3-6 2003 BioCon 2003 - San Diego, CA - 57
Example 1: Nodes location
Library Name Category Name Node Name
PmvPmvStandardStandardStandardStandardStandardStandardStandardSymserver
MoleculeMoleculePythonPythonPythonPythonPythonPythonbuiltinMapper
proteaseindinavirgetattr getattrgetattriterateiteratesetattrmindistanceToPoint
February 3-6 2003 BioCon 2003 - San Diego, CA - 58
Example 1: Create the network
distanceToPoint
proteaseindinavir
allAtoms.coords
getattr iterateallAtoms
getattr
coordsgetattr iterate
minbuiltin
closestsetattr
30
February 3-6 2003 BioCon 2003 - San Diego, CA - 59
V- CONCLUSION
? Validity of the approach? Python? Availability and support
February 3-6 2003 BioCon 2003 - San Diego, CA - 60
Validity of the approach
?Set of components• extensible• inter-operable• re-usable• short development cycle
?User base expanding beyond our lab.?Components re-use outside the field of
structural biology
31
February 3-6 2003 BioCon 2003 - San Diego, CA - 61
Python
?Appropriate language for this approach• modularity, extensibility, dynamic loading, object-
oriented, virtually on any platform, manyextensions from third party
?Rapidly growing community of programmersusing Python for biological applications?Short comings
• reference counting, distribution mechanism, nostrong typing
February 3-6 2003 BioCon 2003 - San Diego, CA - 62
Availability and support
?Online Download site:
http://www.scripps.edu/~sanner/python
?pmv mailing listemail : [email protected]
subject : subscribe pmv
?Reporting bugs and asking for features
http://mgldev.scripps.edu/bugs/index.html
32
February 3-6 2003 BioCon 2003 - San Diego, CA - 63
Acknowledgments
?Christian Carrillo, Kevin Chan?Ruth Huey, Daniel Stoffler?Vincenzo Tchinke, Greg Paris?MGL at TSRI?Pat Walters, Matt Stahl?Don Bashford?Guido van Rossum & Python community
February 3-6 2003 BioCon 2003 - San Diego, CA - 64
Useful links
?Python website:• http://www.python.org
?MGL web site:• http://www.scripps.edu/pub/olson-web/index.html
?MGLTOOLS web site:• http://www.scripps.edu/~sanner/index.html