def writable_volume(): from VolumeViewer import active_volume dr = active_volume() if dr == None: return None # No volume chosen in volume viewer return dr.writable_copy()
def filter_cb(self, event = None): from VolumeViewer import active_volume v = active_volume() if v is None: return ftype = self.filter_type.get() uv,r,params = self.unfiltered_volume(v, ftype) if uv: self.update_filtered_volume(v, ftype) else: step, subreg = self.step_and_subregion() try: fv, params = self.apply_filter(v, ftype, step, subreg) except MemoryError: from chimera.replyobj import warning warning('Out of memory calculating %s filter of %s' % (ftype, v.name)) return if fv is None: return self.update_thresholds(fv, v) fv.show() r = v.subregion(step, subreg) fv.filtered_volume = (ftype, v, r, params)
def select_atoms_outside_map(): from chimera.replyobj import status, info from VolumeViewer import active_volume dr = active_volume() if dr is None: status('No density map opened.') return if dr.surface_model() == None or not dr.surface_model().display: status('No surface shown for map.') return levels = dr.surface_levels if len(levels) == 0: status('No surface shown for map.') return contour_level = min(levels) from chimera import selection atoms = selection.currentAtoms() aolist = atoms_outside_map(atoms, dr, contour_level) msg = ('%d of %d selected atoms outside %s at level %.5g' % (len(aolist), len(atoms), dr.name, contour_level)) status(msg) info(msg + '\n') selection.setCurrent(aolist)
def split_volume_by_color_zone(v = None): if v is None: from VolumeViewer import active_volume v = active_volume() if v is None: return m = v.surface_model() import ColorZone if not ColorZone.coloring_zone(m): return points, colors, radius = ColorZone.zone_points_colors_and_distance(m) grids = split_zones_by_color(v, points, colors, radius) from VolumeViewer import volume_from_grid_data drlist = [volume_from_grid_data(g, show_data = False) for g in grids] n = len(v.surface_colors) for dr in drlist: dr.copy_settings_from(v, copy_region = False) dr.set_parameters(surface_colors = [dr.data.zone_color]*n) dr.show() v.unshow() return drlist
def select_atoms_outside_map(): from chimera.replyobj import status, info from VolumeViewer import active_volume dr = active_volume() if dr is None: status('No density map opened.') return if dr.surface_model() == None or not dr.surface_model().display: status('No surface shown for map.') return levels = dr.surface_levels if len(levels) == 0: status('No surface shown for map.') return contour_level = min(levels) from chimera import selection atoms = selection.currentAtoms() aolist = atoms_outside_map(atoms, dr, contour_level) msg = ('%d of %d selected atoms outside %s at level %.5g' % (len(aolist), len(atoms), dr.name, contour_level)) status(msg) info(msg + '\n') selection.setCurrent(aolist)
def move_selected_atoms_to_maximum(max_steps = 100, ijk_step_size_min = 0.01, ijk_step_size_max = 0.5, optimize_translation = True, optimize_rotation = True, move_whole_molecules = True, request_stop_cb = None): from VolumeViewer import active_volume volume = active_volume() if volume == None or volume.model_transform() == None: if request_stop_cb: request_stop_cb('No volume data set.') return {} from chimera import selection atoms = selection.currentAtoms() if len(atoms) == 0: if request_stop_cb: request_stop_cb('No atoms selected.') return {} stats = move_atoms_to_maximum(atoms, volume, max_steps, ijk_step_size_min, ijk_step_size_max, optimize_translation, optimize_rotation, move_whole_molecules, request_stop_cb) return stats
def test(): from Segger import SelectedRegions rlist = SelectedRegions() from VolumeViewer import active_volume volume = active_volume() make_orthoslice_images(rlist, volume)
def volume_viewer_time(self, ts): from VolumeViewer import active_volume dr = active_volume() if dr == None: t = None else: t = ts.data_time(dr.data) return t
def atoms_outside_contour(atoms, volume = None): if volume is None: from VolumeViewer import active_volume volume = active_volume() points = atom_coordinates(atoms) from chimera import Xform poc, clevel = points_outside_contour(points, Xform(), volume) return poc, clevel
def fourier_transform(v = None, step = None, subregion = None, model_id = None): if v is None: from VolumeViewer import active_volume v = active_volume() if v is None: return m = v.matrix(step = step, subregion = subregion) from numpy.fft import fftn cftm = fftn(m) # Complex128 result, same array size as input from numpy import absolute, float32 aftm = absolute(cftm).astype(float32) cftm = None # Release memory aftm *= 1.0/aftm.size aftm[0,0,0] = 0 # Constant term often huge making histogram hard to use ftm = fftshift(aftm) aftm = None # Release memory # Place FT centered on data, scaled to keep same volume xyz_min, xyz_max = v.xyz_bounds() xyz_center = map(lambda a,b: 0.5*(a+b), xyz_min, xyz_max) ijk_size = list(ftm.shape) ijk_size.reverse() if step is None: ijk_step = v.region[2] elif isinstance(step, int): ijk_step = (step,step,step) else: ijk_step = step xyz_size = map(lambda a,b: a-b, xyz_max, xyz_min) vol = xyz_size[0]*xyz_size[1]*xyz_size[2] cell_size = map(lambda a,b: a*b, v.data.step, ijk_step) cell_vol = cell_size[0]*cell_size[1]*cell_size[2] scale = pow(vol*cell_vol, 1.0/3) step = map(lambda a: scale/a, xyz_size) origin = map(lambda c,s,z: c-0.5*s*z, xyz_center, step, ijk_size) from VolumeData import Array_Grid_Data ftd = Array_Grid_Data(ftm, origin, step) ftd.name = v.name + ' FT' from VolumeViewer import volume_from_grid_data ftr = volume_from_grid_data(ftd, show_data = False, model_id = model_id) ftr.copy_settings_from(v, copy_thresholds = False, copy_colors = False, copy_region = False) ftr.initialize_thresholds() ftr.set_parameters(show_outline_box = True) ftr.show() v.unshow() # Hide original map return ftr
def filter_parameter_changed_cb(self, event = None): if not self.immediate_update.get(): return from VolumeViewer import active_volume fv = active_volume() if fv is None: return self.update_filtered_volume(fv, self.filter_type.get())
def set_scale_range(self, initial = False): from VolumeViewer import active_volume v = active_volume() if v is None: return s = min(v.data.step) if s > 0: self.sdev.set_range(0.5*s, 5*s) if initial: self.sdev.set_value(s, invoke_callbacks = False)
def set_radius_for_volume_data(self): dr = active_volume() if dr == None: return xyz_min, xyz_max = dr.xyz_bounds() xyz_size = map(lambda a,b: a-b, xyz_max, xyz_min) size = max(xyz_size) rs = self.sphere_radius_scale rs.set_range(0, size) rs.set_value(.1 * size)
def scaled_volume(v = None, scale = 1, shift = 0, type = None, step = None, subregion = None, model_id = None): if v is None: from VolumeViewer import active_volume v = active_volume() if v is None: return sd = scaled_grid(v, scale, shift, type, subregion, step) import VolumeViewer sv = VolumeViewer.volume_from_grid_data(sd, model_id = model_id) return sv
def show_volume_statistics(show_reply_log = True): from VolumeViewer import active_volume dr = active_volume() if dr == None: message('No volume data opened', show_reply_log) return m = dr.matrix() mean, sd, rms = mean_sd_rms(m) descrip = subregion_description(dr) message('%s%s: mean = %.5g, SD = %.5g, RMS = %.5g' % (dr.name, descrip, mean, sd, rms), show_reply_log)
def show_volume_statistics(show_reply_log=True): from VolumeViewer import active_volume dr = active_volume() if dr == None: message('No volume data opened', show_reply_log) return m = dr.matrix() mean, sd, rms = mean_sd_rms(m) descrip = subregion_description(dr) message( '%s%s: mean = %.5g, SD = %.5g, RMS = %.5g' % (dr.name, descrip, mean, sd, rms), show_reply_log)
def permute_axes(v = None, axis_order = (0,1,2), step = None, subregion = None, model_id = None): if v is None: from VolumeViewer import active_volume v = active_volume() if v is None: return d = v.grid_data(subregion, step, mask_zone = False) pd = Permuted_Grid(d, axis_order) import VolumeViewer pv = VolumeViewer.volume_from_grid_data(pd, model_id = model_id) return pv
def mask_volume_using_selected_surfaces(axis = (0,1,0), pad = None): from VolumeViewer import active_volume v = active_volume() if v is None: return from Surface import selected_surface_pieces plist = selected_surface_pieces() from Matrix import xform_matrix, invert_matrix tf = invert_matrix(xform_matrix(v.model_transform())) surfaces = surface_geometry(plist, tf, pad) if surfaces: masked_volume(v, surfaces, axis)
def mask_volume_using_selected_surfaces(axis=(0, 1, 0), pad=None): from VolumeViewer import active_volume v = active_volume() if v is None: return from Surface import selected_surface_pieces plist = selected_surface_pieces() from Matrix import xform_matrix, invert_matrix tf = invert_matrix(xform_matrix(v.model_transform())) surfaces = surface_geometry(plist, tf, pad) if surfaces: masked_volume(v, surfaces, axis)
def average_map_value_at_atom_positions(atoms, volume = None): if volume is None: from VolumeViewer import active_volume volume = active_volume() points = atom_coordinates(atoms) if volume is None or len(points) == 0: return 0, len(points) xyz_to_ijk_transform = data_region_xyz_to_ijk_transform(volume) data_array = volume.matrix(step = 1) amv, npts = average_map_value(points, xyz_to_ijk_transform, data_array) return amv, npts
def bin(v = None, bin_size = (2,2,2), step = None, subregion = None, model_id = None): if v is None: from VolumeViewer import active_volume v = active_volume() if v is None: return None bd = bin_grid(v, bin_size, step, subregion) import VolumeViewer bv = VolumeViewer.volume_from_grid_data(bd, model_id = model_id) bv.copy_settings_from(v, copy_region = False) v.unshow() # Hide original map return bv
def permute_axes(v=None, axis_order=(0, 1, 2), step=None, subregion=None, model_id=None): if v is None: from VolumeViewer import active_volume v = active_volume() if v is None: return d = v.grid_data(subregion, step, mask_zone=False) pd = Permuted_Grid(d, axis_order) import VolumeViewer pv = VolumeViewer.volume_from_grid_data(pd, model_id=model_id) return pv
def scaled_volume(v=None, scale=1, shift=0, type=None, step=None, subregion=None, model_id=None): if v is None: from VolumeViewer import active_volume v = active_volume() if v is None: return sd = scaled_grid(v, scale, shift, type, subregion, step) import VolumeViewer sv = VolumeViewer.volume_from_grid_data(sd, model_id=model_id) return sv
def laplacian(v = None, step = None, subregion = None, model_id = None): if v is None: from VolumeViewer import active_volume v = active_volume() if v is None: return None m = v.matrix(step = step, subregion = subregion) from numpy import float32, multiply, add lm = m.astype(float32) # Copy array multiply(lm, -6.0, lm) add(lm[:-1,:,:], m[1:,:,:], lm[:-1,:,:]) add(lm[1:,:,:], m[:-1,:,:], lm[1:,:,:]) add(lm[:,:-1,:], m[:,1:,:], lm[:,:-1,:]) add(lm[:,1:,:], m[:,:-1,:], lm[:,1:,:]) add(lm[:,:,:-1], m[:,:,1:], lm[:,:,:-1]) add(lm[:,:,1:], m[:,:,:-1], lm[:,:,1:]) lm[0,:,:] = 0 lm[-1,:,:] = 0 lm[:,0,:] = 0 lm[:,-1,:] = 0 lm[:,:,0] = 0 lm[:,:,-1] = 0 origin, step = v.data_origin_and_step(subregion = subregion, step = step) d = v.data from VolumeData import Array_Grid_Data ld = Array_Grid_Data(lm, origin, step, d.cell_angles, d.rotation, name = v.name + ' Laplacian') ld.polar_values = True from VolumeViewer import volume_from_grid_data lv = volume_from_grid_data(ld, show_data = False, model_id = model_id) lv.copy_settings_from(v, copy_thresholds = False, copy_colors = False) lv.set_parameters(cap_faces = False) lv.initialize_thresholds() lv.show() v.unshow() # Hide original map return lv
def front_of_volume_at_screen_center(): v = active_volume() if v == None: return None import chimera wx_size, wy_size = chimera.viewer.windowSize from VolumeViewer.slice import volume_segment xyz_in, xyz_out, shown = volume_segment(v, wx_size/2, wy_size/2) if xyz_in == None: return None xf = v.model_transform() eye_xyz_in = xf.apply(chimera.Point(*xyz_in)) return eye_xyz_in
def set_volume_icosahedral_symmetry(): from VolumeViewer import active_volume v = active_volume() if v is None: from chimera.replyobj import status status('Set icosahedral symmetry: No active volume data.') return from Icosahedron.gui import icosahedron_dialog d = icosahedron_dialog() if d is None: orientation = '222' else: orientation = d.orientation_name() from Icosahedron import icosahedral_symmetry_matrices v.data.symmetries = icosahedral_symmetry_matrices(orientation) from chimera.replyobj import status status('Set icosahedral symmetry %s of volume %s.' % (orientation, v.name))
def set_volume_icosahedral_symmetry(): from VolumeViewer import active_volume v = active_volume() if v is None: from chimera.replyobj import status status('Set icosahedral symmetry: No active volume data.') return from Icosahedron.gui import icosahedron_dialog d = icosahedron_dialog() if d is None: orientation = '222' else: orientation = d.orientation_name() from Icosahedron import icosahedral_symmetry_matrices v.data.symmetries = icosahedral_symmetry_matrices(orientation) from chimera.replyobj import status status('Set icosahedral symmetry %s of volume %s.' % (orientation, v.name))
def move_atom_to_maximum(a, max_steps = 100, ijk_step_size_min = 0.001, ijk_step_size_max = 0.5): from VolumeViewer import active_volume dr = active_volume() if dr == None or dr.model_transform() == None: from chimera.replyobj import status status('No data shown by volume viewer dialog') return points = atom_coordinates([a]) point_weights = None move_tf, stats = motion_to_maximum(points, point_weights, dr, max_steps, ijk_step_size_min, ijk_step_size_max, optimize_translation = True, optimize_rotation = False) # Update atom position. from Matrix import chimera_xform xf = chimera_xform(move_tf) mxf = a.molecule.openState.xform p = mxf.inverse().apply(xf.apply(a.xformCoord())) a.setCoord(p)
def fillInUI(self, parent): self.phantom_device = None self.cursor_model = None self.phantom_handler = None self.gradient_force = None self.phantom_button_down = 0 self.last_phantom_transform = None self.last_roll = None self.key_callback_registered = None self.mode = 'cursor' # 'cursor', 'move models', 'zoom', # 'contour level', 'move marker' self.command_list_shown = False import Tkinter from CGLtk import Hybrid row = 0 po = Hybrid.Checkbutton_Row(parent, 'Enable', ('cursor', 'forces', 'key commands')) po.frame.grid(row = row, column = 0, sticky = 'w') row = row + 1 self.phantom_on, self.force_field, self.commands = \ [c.variable for c in po.checkbuttons] self.phantom_on.add_callback(self.settings_changed_cb) self.force_field.add_callback(self.settings_changed_cb) self.commands.add_callback(self.toggle_commands_cb) cf = Tkinter.Frame(parent) cf.grid(row = row, column = 0, sticky = 'w') row = row + 1 cs = Hybrid.Option_Menu(cf, 'Cursor shape ', 'cross', 'jack', 'sphere', 'volume crosshair') cs.frame.grid(row = 0, column = 0, sticky = 'nw') row = row + 1 cs.variable.set('jack') cs.add_callback(self.settings_changed_cb) self.cursor_shape = cs.variable from VolumeViewer import active_volume v = active_volume() if v: csize = '%.3g' % (10 * max(v.data.step)) else: csize = '1' cs = Hybrid.Entry(cf, ' size ', 5, csize) cs.frame.grid(row = 0, column = 1, sticky = 'w') cs.entry.bind('<KeyPress-Return>', self.settings_changed_cb) self.cursor_size = cs.variable cl = Tkinter.Label(cf, text = ' color ') cl.grid(row = 0, column = 2, sticky = 'w') from CGLtk.color import ColorWell cc = ColorWell.ColorWell(cf, callback = self.settings_changed_cb) self.cursor_color = cc cc.showColor((0,0.5,1), doCallback = 0) cc.grid(row = 0, column = 3, sticky = 'w') sp = Hybrid.Popup_Panel(parent) spf = sp.frame spf.grid(row = row, column = 0, sticky = 'news') spf.grid_remove() spf.columnconfigure(0, weight=1) self.settings_panel = sp.panel_shown_variable row += 1 srow = 0 cb = sp.make_close_button(spf) cb.grid(row = srow, column = 1, sticky = 'e') pr = Hybrid.Entry(spf, 'Phantom physical range (mm) ', 5, '200') pr.frame.grid(row = srow, column = 0, sticky = 'w') srow += 1 pr.entry.bind('<KeyPress-Return>', self.settings_changed_cb) self.phantom_range = pr.variable mf = Hybrid.Entry(spf, 'Maximum force (lbs) ', 5, '.4') mf.frame.grid(row = srow, column = 0, sticky = 'w') srow += 1 mf.entry.bind('<KeyPress-Return>', self.settings_changed_cb) self.maximum_force = mf.variable af = Hybrid.Checkbutton(spf, 'Auto-adjust force constant', 1) af.button.grid(row = srow, column = 0, sticky = 'w') srow += 1 self.auto_adjust_force_constant = af.variable self.force_field.add_callback(self.settings_changed_cb) fc = Hybrid.Logarithmic_Scale(spf, 'Force constant ', .0001, 100, 1, '%.2g') fc.frame.grid(row = srow, column = 0, sticky = 'ew') srow += 1 fc.set_value(1) fc.callback(self.force_constant_changed_cb) fc.entry.bind('<KeyPress-Return>', self.force_constant_changed_cb) self.force_constant = fc vf = Tkinter.Frame(spf) vf.grid(row = srow, column = 0, sticky = 'ew') srow += 1 self.label_row(vf, 0, ('', 'x', 'y', 'z')) pl = self.label_row(vf, 1, ('Phantom position', '', '', '')) self.phantom_position_labels = pl[1:] cpl = self.label_row(vf, 2, ('Chimera position', '', '', '')) self.chimera_position_labels = cpl[1:] vpl = self.label_row(vf, 3, ('Volume position', '', '', '')) self.volume_position_labels = vpl[1:] vil = self.label_row(vf, 4, ('Volume index', '', '', '')) self.volume_index_labels = vil[1:] gl = self.label_row(vf, 5, ('Gradient', '', '', '')) self.gradient_labels = gl[1:] fl = self.label_row(vf, 6, ('Force', '', '', '')) self.force_labels = fl[1:] dvl = self.label_row(vf, 7, ('Data value', '')) self.data_value_label = dvl[1]
def data_region(self): from VolumeViewer import active_volume return active_volume()
def active_volume(): from VolumeViewer import active_volume return active_volume()
def fillInUI(self, parent): self.phantom_device = None self.cursor_model = None self.phantom_handler = None self.gradient_force = None self.phantom_button_down = 0 self.last_phantom_transform = None self.last_roll = None self.key_callback_registered = None self.mode = 'cursor' # 'cursor', 'move models', 'zoom', # 'contour level', 'move marker' self.command_list_shown = False import Tkinter from CGLtk import Hybrid row = 0 po = Hybrid.Checkbutton_Row(parent, 'Enable', ('cursor', 'forces', 'key commands')) po.frame.grid(row=row, column=0, sticky='w') row = row + 1 self.phantom_on, self.force_field, self.commands = \ [c.variable for c in po.checkbuttons] self.phantom_on.add_callback(self.settings_changed_cb) self.force_field.add_callback(self.settings_changed_cb) self.commands.add_callback(self.toggle_commands_cb) cf = Tkinter.Frame(parent) cf.grid(row=row, column=0, sticky='w') row = row + 1 cs = Hybrid.Option_Menu(cf, 'Cursor shape ', 'cross', 'jack', 'sphere', 'volume crosshair') cs.frame.grid(row=0, column=0, sticky='nw') row = row + 1 cs.variable.set('jack') cs.add_callback(self.settings_changed_cb) self.cursor_shape = cs.variable from VolumeViewer import active_volume v = active_volume() if v: csize = '%.3g' % (10 * max(v.data.step)) else: csize = '1' cs = Hybrid.Entry(cf, ' size ', 5, csize) cs.frame.grid(row=0, column=1, sticky='w') cs.entry.bind('<KeyPress-Return>', self.settings_changed_cb) self.cursor_size = cs.variable cl = Tkinter.Label(cf, text=' color ') cl.grid(row=0, column=2, sticky='w') from CGLtk.color import ColorWell cc = ColorWell.ColorWell(cf, callback=self.settings_changed_cb) self.cursor_color = cc cc.showColor((0, 0.5, 1), doCallback=0) cc.grid(row=0, column=3, sticky='w') sp = Hybrid.Popup_Panel(parent) spf = sp.frame spf.grid(row=row, column=0, sticky='news') spf.grid_remove() spf.columnconfigure(0, weight=1) self.settings_panel = sp.panel_shown_variable row += 1 srow = 0 cb = sp.make_close_button(spf) cb.grid(row=srow, column=1, sticky='e') pr = Hybrid.Entry(spf, 'Phantom physical range (mm) ', 5, '200') pr.frame.grid(row=srow, column=0, sticky='w') srow += 1 pr.entry.bind('<KeyPress-Return>', self.settings_changed_cb) self.phantom_range = pr.variable mf = Hybrid.Entry(spf, 'Maximum force (lbs) ', 5, '.4') mf.frame.grid(row=srow, column=0, sticky='w') srow += 1 mf.entry.bind('<KeyPress-Return>', self.settings_changed_cb) self.maximum_force = mf.variable af = Hybrid.Checkbutton(spf, 'Auto-adjust force constant', 1) af.button.grid(row=srow, column=0, sticky='w') srow += 1 self.auto_adjust_force_constant = af.variable self.force_field.add_callback(self.settings_changed_cb) fc = Hybrid.Logarithmic_Scale(spf, 'Force constant ', .0001, 100, 1, '%.2g') fc.frame.grid(row=srow, column=0, sticky='ew') srow += 1 fc.set_value(1) fc.callback(self.force_constant_changed_cb) fc.entry.bind('<KeyPress-Return>', self.force_constant_changed_cb) self.force_constant = fc vf = Tkinter.Frame(spf) vf.grid(row=srow, column=0, sticky='ew') srow += 1 self.label_row(vf, 0, ('', 'x', 'y', 'z')) pl = self.label_row(vf, 1, ('Phantom position', '', '', '')) self.phantom_position_labels = pl[1:] cpl = self.label_row(vf, 2, ('Chimera position', '', '', '')) self.chimera_position_labels = cpl[1:] vpl = self.label_row(vf, 3, ('Volume position', '', '', '')) self.volume_position_labels = vpl[1:] vil = self.label_row(vf, 4, ('Volume index', '', '', '')) self.volume_index_labels = vil[1:] gl = self.label_row(vf, 5, ('Gradient', '', '', '')) self.gradient_labels = gl[1:] fl = self.label_row(vf, 6, ('Force', '', '', '')) self.force_labels = fl[1:] dvl = self.label_row(vf, 7, ('Data value', '')) self.data_value_label = dvl[1]
def data_region(self): from VolumeViewer import active_volume return active_volume()