| 1 | # -----------------------------------------------------------------------------
|
|---|
| 2 | # Make a matplotlib plot of density values along a ray from center of a map.
|
|---|
| 3 | # Use the current view direction and update plot as models are rotated.
|
|---|
| 4 | # For Yong Zi Tan for 3D FSC plotting.
|
|---|
| 5 | #
|
|---|
| 6 | # This script registers command "fscplot" which takes one argument, the density
|
|---|
| 7 | # map for which the plot is made. For example,
|
|---|
| 8 | #
|
|---|
| 9 | # fscplot #0
|
|---|
| 10 | #
|
|---|
| 11 | def ray_values(v, direction):
|
|---|
| 12 | d = v.data
|
|---|
| 13 | center = [0.5*(s+1) for s in d.size]
|
|---|
| 14 | radius = 0.5*min([s*t for s,t in zip(d.size, d.step)])
|
|---|
| 15 | steps = max(d.size)
|
|---|
| 16 | from Matrix import norm
|
|---|
| 17 | dn = norm(direction)
|
|---|
| 18 | from numpy import array, arange, float32, outer
|
|---|
| 19 | dir = array(direction)/dn
|
|---|
| 20 | spacing = radius/dn
|
|---|
| 21 | radii = arange(0, steps, dtype = float32)*(radius/steps)
|
|---|
| 22 | ray_points = outer(radii, dir)
|
|---|
| 23 | values = v.interpolated_values(ray_points)
|
|---|
| 24 | return radii, values
|
|---|
| 25 |
|
|---|
| 26 | # -----------------------------------------------------------------------------
|
|---|
| 27 | #
|
|---|
| 28 | def plot(x, y, xlabel, ylabel, title, fig = None):
|
|---|
| 29 | import matplotlib.pyplot as plt
|
|---|
| 30 | if fig is None:
|
|---|
| 31 | fig = plt.figure()
|
|---|
| 32 | fig.plot = ax = fig.add_subplot(1,1,1)
|
|---|
| 33 | else:
|
|---|
| 34 | ax = fig.plot
|
|---|
| 35 | ax.clear()
|
|---|
| 36 | ax.plot(x, y, linewidth=1.0)
|
|---|
| 37 | ax.set_xlabel(xlabel)
|
|---|
| 38 | ax.set_ylabel(ylabel)
|
|---|
| 39 | ax.set_ylim(ymin = -0.2, ymax = 1.01)
|
|---|
| 40 | ax.set_title(title)
|
|---|
| 41 | ax.grid(True)
|
|---|
| 42 | fig.canvas.manager.show()
|
|---|
| 43 | return fig
|
|---|
| 44 |
|
|---|
| 45 | # -----------------------------------------------------------------------------
|
|---|
| 46 | #
|
|---|
| 47 | def update_plot(fsc_map, fig = None):
|
|---|
| 48 | xf = fsc_map.openState.xform
|
|---|
| 49 | from chimera import Vector
|
|---|
| 50 | direction = xf.inverse().apply(Vector(0,0,-1)).data()
|
|---|
| 51 | radii, values = ray_values(fsc_map, direction)
|
|---|
| 52 | title = '3D FSC plotted on axis %.3g,%.3g,%.3g' % direction
|
|---|
| 53 | fig = plot(radii, values, xlabel = 'Radius', ylabel = 'Correlation', title = title, fig = fig)
|
|---|
| 54 | return fig
|
|---|
| 55 |
|
|---|
| 56 | # -----------------------------------------------------------------------------
|
|---|
| 57 | #
|
|---|
| 58 | def fsc_plot(fscMap):
|
|---|
| 59 | fig = update_plot(fscMap)
|
|---|
| 60 | from chimera import triggers
|
|---|
| 61 | h = triggers.addHandler('OpenState', motion_cb, (fscMap, fig))
|
|---|
| 62 |
|
|---|
| 63 | # -----------------------------------------------------------------------------
|
|---|
| 64 | #
|
|---|
| 65 |
|
|---|
| 66 | def motion_cb(trigger_name, mf, trigger_data):
|
|---|
| 67 | if 'transformation change' in trigger_data.reasons:
|
|---|
| 68 | fsc_map, fig = mf
|
|---|
| 69 | update_plot(fsc_map, fig)
|
|---|
| 70 |
|
|---|
| 71 | # -----------------------------------------------------------------------------
|
|---|
| 72 | #
|
|---|
| 73 | def fscplot_cmd(cmdname, args):
|
|---|
| 74 | from Commands import volume_arg, parse_arguments
|
|---|
| 75 | req_args = [('fscMap', volume_arg)]
|
|---|
| 76 | kw = parse_arguments(cmdname, args, req_args)
|
|---|
| 77 | fsc_plot(**kw)
|
|---|
| 78 |
|
|---|
| 79 | # -----------------------------------------------------------------------------
|
|---|
| 80 | #
|
|---|
| 81 | from Midas.midas_text import addCommand
|
|---|
| 82 | addCommand('fscplot', fscplot_cmd)
|
|---|