DrawBoundingBox: Difference between revisions

From PyMOLWiki
Jump to navigation Jump to search
(New page: = OVERVIEW = Draws the minumum bounding box around your selection. <gallery> Image:DrawMinBB.png|Example min BB </gallery> = Example = <source lang="python"> run ~/drawMinBoundingBox.py ...)
 
m (separated the run command from the python code)
 
(9 intermediate revisions by 4 users not shown)
Line 1: Line 1:
= OVERVIEW =
= Overview =
Draws the minumum bounding box around your selection.
Draws a bounding box around a given selection.


<gallery>
<gallery>
Image:DrawMinBB.png|Example min BB
Image:DrawMinBB.png|Example of a bounding box
Image:bb_with_padding.png|Two bounding boxes, one with 0 padding, the other with 10 Ang padding.
</gallery>
</gallery>


= Example =
= Example =
<source lang="python">
<source lang="python">
run ~/drawMinBoundingBox.py
run ~/drawBoundingBox.py
fetch 1jsd
fetch 1jsd
drawMinBoundingBox 1jsd, 3, r=0.33, g=0.80
drawBoundingBox 1jsd, r=0.33, g=0.80
 
# example from above w/padding, draw it light blue
drawBoundingBox padding=10, r=0.5, g=0.8, b=1.0
</source>
</source>


= Installation =
= Installation =
Just copy the source to your computer.  Then run
Copy the source code to your computer, and execute it by issuing the command "run /path/to/drawBoundingBox.py" in PyMOL.
<source lang="python">
run /path/to/drawMinBoundingBox.py
</source>
 
= Usage =
<source lang="python">
drawMinBoundingBox [selection, [lineWidth, [redColor, [greenColor, [blueColor]]]]]
</source>
 
= The Code =
[[Media:DrawMinBoundBox.tar.bz2|Download drawMinBoundingBox.tar.bz2]]


<source lang="python">
<source lang="python">
Line 32: Line 25:
from pymol import cmd                                                                                       
from pymol import cmd                                                                                       
from random import randint                                                                                   
from random import randint                                                                                   
from string import split                                                                                   


#############################################################################
#############################################################################
#                                                                             
#                                                                             
# drawMinBoundingBox.py -- Draws the smallest box surrounding a selection  
# drawBoundingBox.py -- Draws a box surrounding a selection  
#
#                                                                             
#                                                                             
# AUTHOR: Jason Vertrees                                                    
# AUTHOR: Jason Vertrees                                                  
# DATE  : 2/20/2009                                                           
# DATE  : 2/20/2009                                                           
# NOTES : See comments below.                                                 
# NOTES : See comments below.                                                 
#                                                                             
#                                                                             
#############################################################################
#############################################################################
def drawMinBoundingBox(sel="(all)", lineWidth=2.0, r=1.0, g=1.0, b=1.0):     
def drawBoundingBox(selection="(all)", padding=0.0, linewidth=2.0, r=1.0, g=1.0, b=1.0):     
         """                                                                   
         """                                                                   
         DESCRIPTION                                                           
         DESCRIPTION                                                           
                 Given sel, draw the minimum bounding box around it.           
                 Given selection, draw the bounding box around it.           


         USAGE:
         USAGE:
                 drawMinBoundingBox [sel, [lineWidth, [r, [g, b]]]]
                 drawBoundingBox [selection, [padding, [linewidth, [r, [g, b]]]]]


         PARAMETERS:
         PARAMETERS:
                 sel,                   the selection to enboxen.  :-)
                 selection,             the selection to enboxen.  :-)
                                                defaults to (all)    
                                        defaults to (all)
 
                padding,                defaults to 0


                 lineWidth,              width of box lines
                 linewidth,              width of box lines
                                                defaults to 2.0
                                        defaults to 2.0


                 r,                             red color component, valid range is [0.0, 1.0]
                 r,                     red color component, valid range is [0.0, 1.0]
                                                defaults to 1.0                               
                                        defaults to 1.0                               


                 g,                             green color component, valid range is [0.0, 1.0]
                 g,                     green color component, valid range is [0.0, 1.0]
                                                defaults to 1.0                                 
                                        defaults to 1.0                                 


                 b,                             blue color component, valid range is [0.0, 1.0]
                 b,                     blue color component, valid range is [0.0, 1.0]
                                                defaults to 1.0                                 
                                        defaults to 1.0                                 


         RETURNS
         RETURNS
Line 71: Line 66:


         NOTES
         NOTES
                 * This function creates a randomly named CGO box that minimally spans the protein. The
                 * This function creates a randomly named CGO box that minimally spans the protein. The
                 user can specify the width of the lines and also the color.                             
                 user can specify the width of the lines, the padding and also the color.                             
        """                                                                                                   
 
        ([minX, minY, minZ],[maxX, maxY, maxZ]) = cmd.get_extent(selection)
 
        print "Box dimensions (%.2f, %.2f, %.2f)" % (maxX-minX, maxY-minY, maxZ-minZ)


                * Maybe add a padding function?  This might help docking folks -- eg. add a 5 Ang border around
        minX = minX - float(padding)
                  the protein then draw the box there?                                                       
        minY = minY - float(padding)
         """                                                                                                   
        minZ = minZ - float(padding)
        maxX = maxX + float(padding)
         maxY = maxY + float(padding)
        maxZ = maxZ + float(padding)


         ([minX, minY, minZ],[maxX, maxY, maxZ]) = cmd.get_extent(sel)
         if padding != 0:
                print "Box dimensions + padding (%.2f, %.2f, %.2f)" % (maxX-minX, maxY-minY, maxZ-minZ)


         minBB = [
         boundingBox = [
                 LINEWIDTH, float(lineWidth),
                 LINEWIDTH, float(linewidth),


                 BEGIN, LINES,
                 BEGIN, LINES,
Line 127: Line 131:
         ]
         ]


         curName = "minBB_" + str(randint(0,10000))
         boxName = "box_" + str(randint(0,10000))
         while curName in cmd.get_names():
         while boxName in cmd.get_names():
                 curName = "minBB_" + str(randint(0,10000))
                 boxName = "box_" + str(randint(0,10000))


         cmd.load_cgo(minBB,curName)
         cmd.load_cgo(boundingBox,boxName)
         return curName
         return boxName
 
cmd.extend ("drawBoundingBox", drawBoundingBox)
</source>
</source>
= See Also =
[[Bounding_Box]]




[[Category:Script_Library|DrawMinBoundingBox]]
[[Category:Script_Library|DrawMinBoundingBox]]
[[Category:Math_Scripts]]
[[Category:CGO]]
[[Category:CGO]]

Latest revision as of 07:36, 16 January 2013

Overview

Draws a bounding box around a given selection.

Example

run ~/drawBoundingBox.py
fetch 1jsd
drawBoundingBox 1jsd, r=0.33, g=0.80

# example from above w/padding, draw it light blue
drawBoundingBox padding=10, r=0.5, g=0.8, b=1.0

Installation

Copy the source code to your computer, and execute it by issuing the command "run /path/to/drawBoundingBox.py" in PyMOL.

# -*- coding: utf-8 -*-                                                                                     
from pymol.cgo import *                                                                                     
from pymol import cmd                                                                                       
from random import randint                                                                                  

#############################################################################
#                                                                            
# drawBoundingBox.py -- Draws a box surrounding a selection 
#
#                                                                            
# AUTHOR: Jason Vertrees                                                   
# DATE  : 2/20/2009                                                          
# NOTES : See comments below.                                                
#                                                                            
#############################################################################
def drawBoundingBox(selection="(all)", padding=0.0, linewidth=2.0, r=1.0, g=1.0, b=1.0):     
        """                                                                  
        DESCRIPTION                                                          
                Given selection, draw the bounding box around it.          

        USAGE:
                drawBoundingBox [selection, [padding, [linewidth, [r, [g, b]]]]]

        PARAMETERS:
                selection,              the selection to enboxen.  :-)
                                        defaults to (all)
   
                padding,                defaults to 0

                linewidth,              width of box lines
                                        defaults to 2.0

                r,                      red color component, valid range is [0.0, 1.0]
                                        defaults to 1.0                               

                g,                      green color component, valid range is [0.0, 1.0]
                                        defaults to 1.0                                 

                b,                      blue color component, valid range is [0.0, 1.0]
                                        defaults to 1.0                                

        RETURNS
                string, the name of the CGO box

        NOTES
                * This function creates a randomly named CGO box that minimally spans the protein. The
                user can specify the width of the lines, the padding and also the color.                            
        """                                                                                                    

        ([minX, minY, minZ],[maxX, maxY, maxZ]) = cmd.get_extent(selection)

        print "Box dimensions (%.2f, %.2f, %.2f)" % (maxX-minX, maxY-minY, maxZ-minZ)

        minX = minX - float(padding)
        minY = minY - float(padding)
        minZ = minZ - float(padding)
        maxX = maxX + float(padding)
        maxY = maxY + float(padding)
        maxZ = maxZ + float(padding)

        if padding != 0:
                 print "Box dimensions + padding (%.2f, %.2f, %.2f)" % (maxX-minX, maxY-minY, maxZ-minZ)

        boundingBox = [
                LINEWIDTH, float(linewidth),

                BEGIN, LINES,
                COLOR, float(r), float(g), float(b),

                VERTEX, minX, minY, minZ,       #1
                VERTEX, minX, minY, maxZ,       #2

                VERTEX, minX, maxY, minZ,       #3
                VERTEX, minX, maxY, maxZ,       #4

                VERTEX, maxX, minY, minZ,       #5
                VERTEX, maxX, minY, maxZ,       #6

                VERTEX, maxX, maxY, minZ,       #7
                VERTEX, maxX, maxY, maxZ,       #8


                VERTEX, minX, minY, minZ,       #1
                VERTEX, maxX, minY, minZ,       #5

                VERTEX, minX, maxY, minZ,       #3
                VERTEX, maxX, maxY, minZ,       #7

                VERTEX, minX, maxY, maxZ,       #4
                VERTEX, maxX, maxY, maxZ,       #8

                VERTEX, minX, minY, maxZ,       #2
                VERTEX, maxX, minY, maxZ,       #6


                VERTEX, minX, minY, minZ,       #1
                VERTEX, minX, maxY, minZ,       #3

                VERTEX, maxX, minY, minZ,       #5
                VERTEX, maxX, maxY, minZ,       #7

                VERTEX, minX, minY, maxZ,       #2
                VERTEX, minX, maxY, maxZ,       #4

                VERTEX, maxX, minY, maxZ,       #6
                VERTEX, maxX, maxY, maxZ,       #8

                END
        ]

        boxName = "box_" + str(randint(0,10000))
        while boxName in cmd.get_names():
                boxName = "box_" + str(randint(0,10000))

        cmd.load_cgo(boundingBox,boxName)
        return boxName

cmd.extend ("drawBoundingBox", drawBoundingBox)

See Also

Bounding_Box