|
| 1 | +""" |
| 2 | +This script was initially copied from https://pymolwiki.org/index.php/CgoCircle on Sept. 23, 2024. |
| 3 | +
|
| 4 | +The PyMOL Wiki page containing the code that was copied is licensed under GNU Free Documentation License 1.2. |
| 5 | +
|
| 6 | +Probable Author: Jason Vertrees. |
| 7 | +""" |
| 8 | + |
| 9 | +import math |
| 10 | +import pymol |
| 11 | +from pymol.cgo import * |
| 12 | +from pymol import cmd |
| 13 | + |
| 14 | +def cgoCircle(x, y, z, r=8.0, cr=1.0, cg=0.4, cb=0.8, w=2.0): |
| 15 | + """ |
| 16 | + Create a CGO circle |
| 17 | +
|
| 18 | + PARAMS |
| 19 | + x, y, z |
| 20 | + X, Y and Z coordinates of the origin |
| 21 | +
|
| 22 | + r |
| 23 | + Radius of the circle |
| 24 | +
|
| 25 | + cr, cg, cb |
| 26 | + Color triplet, [r,g,b] where r,g,b are all [0.0,1.0]. |
| 27 | +
|
| 28 | + w |
| 29 | + Line width of the circle |
| 30 | +
|
| 31 | + RETURNS |
| 32 | + the CGO object (it also loads it into PyMOL, too). |
| 33 | +
|
| 34 | + """ |
| 35 | + x = float(x) |
| 36 | + y = float(y) |
| 37 | + z = float(z) |
| 38 | + r = abs(float(r)) |
| 39 | + cr = abs(float(cr)) |
| 40 | + cg = abs(float(cg)) |
| 41 | + cb = abs(float(cb)) |
| 42 | + w = float(w) |
| 43 | + |
| 44 | + obj = [ BEGIN, LINES, COLOR, cr, cg, cb ] |
| 45 | + for i in range(180): |
| 46 | + obj.append( VERTEX ) |
| 47 | + obj.append(r*math.cos(i) + x ) |
| 48 | + obj.append(r*math.sin(i) + y ) |
| 49 | + obj.append(z) |
| 50 | + obj.append( VERTEX ) |
| 51 | + obj.append(r*math.cos(i+0.1) + x ) |
| 52 | + obj.append(r*math.sin(i+0.1) + y ) |
| 53 | + obj.append(z) |
| 54 | + obj.append(END) |
| 55 | + |
| 56 | + cName = cmd.get_unused_name("circle_") |
| 57 | + cmd.load_cgo( obj, cName ) |
| 58 | + cmd.set("cgo_line_width", w, cName ) |
| 59 | + return obj |
| 60 | + |
| 61 | + |
| 62 | +def circleSelection( selName, r=None, cr=1.0, cg=0.4, cb=0.8, w=2.0 ): |
| 63 | + """ |
| 64 | + circleSelection -- draws a cgo circle around a given selection or object |
| 65 | +
|
| 66 | + PARAMS |
| 67 | + selName |
| 68 | + Name of the thing to encircle. |
| 69 | +
|
| 70 | + r |
| 71 | + Radius of circle. |
| 72 | + DEFAULT: This cript automatically defines the radius for you. If |
| 73 | + you select one atom and the resultant circle is too small, then |
| 74 | + you can override the script's calculation of r and specify your own. |
| 75 | +
|
| 76 | + cr, cg, cb |
| 77 | + red, green and blue coloring, each a value in the range [0.0, 1.0] |
| 78 | +
|
| 79 | + RETURNS |
| 80 | + The circle object. |
| 81 | +
|
| 82 | + """ |
| 83 | + ((minX, minY, minZ), (maxX, maxY, maxZ)) = cmd.get_extent(selName) |
| 84 | + |
| 85 | + if r==None: |
| 86 | + r = max( [maxX-minX, maxY-minY, maxZ-minZ] ) |
| 87 | + |
| 88 | + stored.coords = [] |
| 89 | + cmd.iterate_state(1, selName, "stored.coords.append([x,y,z])") |
| 90 | + l = len(stored.coords) |
| 91 | + |
| 92 | + centerX = sum(map(lambda x: x[0], stored.coords)) / l |
| 93 | + centerY = sum(map(lambda x: x[1], stored.coords)) / l |
| 94 | + centerZ = sum(map(lambda x: x[2], stored.coords)) / l |
| 95 | + |
| 96 | + return cgoCircle( centerX, centerY, centerZ, r, cr, cg, cb, w ) |
| 97 | + |
| 98 | + |
| 99 | +cmd.extend( "cgoCircle", cgoCircle ) |
| 100 | +cmd.extend( "circleSelection", circleSelection ) |
0 commit comments