def read_swc(session, path): f = open(path, 'r') lines = f.readlines() f.close() points = parse_swc_points(lines) i2m = {} from chimerax.markers import MarkerSet, create_link from os.path import basename name = basename(path) mset = MarkerSet(session, name) tcolors = { 1: (255, 255, 255, 255), # soma, white 2: (128, 128, 128, 255), # axon gray 3: (0, 255, 0, 255), # basal dendrite, green 4: (255, 0, 255, 255), # apical dendrite, magenta } other_color = (255, 255, 0, 255) # yellow for n, t, x, y, z, r, pid in points: if r < 0: r = 0.5 color = tcolors.get(t, other_color) i2m[n] = m = mset.create_marker((x, y, z), color, r, id=n) if pid in i2m: m2 = i2m[pid] rlink = min(r, m2.radius) create_link(m, m2, color, rlink) msg = 'Opened neuron traces %s' % name return [mset], msg
def _show_axis(session, tf, color, length, radius, coordinate_system): axis, axis_point, angle, axis_shift = tf.axis_center_angle_shift() if angle < 0.1: from chimerax.core.errors import UserError raise UserError('Rotation angle is near zero (%g degrees)' % angle) b = coordinate_system.bounds() if b is None: from chimerax.core.errors import UserError raise UserError('Model %s must be visible to show axis' % coordinate_system) from chimerax.geometry import project_to_axis axis_center = project_to_axis(b.center(), axis, axis_point) axis_length = b.width() if length is None else length hl = 0.5 * axis_length ap1 = axis_center - hl * axis ap2 = axis_center + hl * axis from chimerax.markers import MarkerSet, create_link mset = MarkerSet(session, 'rotation axis') mset.scene_position = coordinate_system.scene_position r = 0.025 * axis_length if radius is None else radius m1 = mset.create_marker(ap1, color, r) m2 = mset.create_marker(ap2, color, r) b = create_link(m1, m2, color, r) b.halfbond = True session.models.add([mset]) return mset
def surface_path(session, surface, length=100, rgba=(255, 255, 0, 255), radius=1, geodesic=True): ep = edge_pairs(surface.triangles) from random import choice e = choice(tuple(ep.keys())) # e = first_element(ep.keys()) vertices = surface.vertices if geodesic: tnormals = edge_triangle_normals(surface.vertices, surface.triangles) # Start in direction perpendicular to starting edge. eo = (e[1], e[0]) ev = edge_vector(eo, vertices) from chimerax.geometry import cross_product, normalize_vector direction = normalize_vector(cross_product(tnormals[eo], ev)) else: direction, tnormals = None points = make_surface_path(e, ep, length, vertices, direction, tnormals) from chimerax.markers import MarkerSet, create_link ms = MarkerSet(session, 'surface path') mprev = None for id, xyz in enumerate(points): m = ms.create_marker(xyz, rgba, radius, id) if mprev is not None: create_link(mprev, m, rgba=rgba, radius=radius) mprev = m session.models.add([ms])
def create_centroid_path(session, name, xyz, radius, color): from chimerax.markers import MarkerSet, create_link mset = MarkerSet(session, name) mprev = None for p in xyz: m = mset.create_marker(p, color, radius) if mprev: create_link(mprev, m, rgba = color, radius = radius/2) mprev = m session.models.add([mset]) return mset
def markblobs(session, surface, radius=0.5, color=(255, 255, 0, 255)): centers = surface_blob_centers(surface) scene_centers = surface.scene_position * centers from chimerax.markers import MarkerSet marker_set = MarkerSet(session, name=surface.name) markers = [ marker_set.create_marker(center, color, radius) for center in scene_centers ] session.models.add([marker_set]) session.logger.status('Found %d connected surface pieces' % len(markers), log=True) return markers
def __init__(self, session, name, volume, center, range=None, base_area=10.0, height=1.0, marker_radius=1.0, marker_color=(100, 200, 100, 255), color_surface=True, all_extrema=False): MarkerSet.__init__(self, session, name) self.bump_map = volume self.bump_center = center self.bump_range = range self.bump_base_area = base_area self.bump_min_height = height r, ijk = radial_extrema(volume, center, range) indices, size_hvc, mask = protrusion_sizes(r, ijk, volume.data, base_area, height, all_extrema, log=session.logger) self.bump_mask = mask xyz = volume.data.ijk_to_xyz_transform * ijk[indices] colors = marker_colors(size_hvc, height, marker_color) markers = [ self.create_marker(p, rgba, marker_radius, i + 1) for i, (p, rgba) in enumerate(zip(xyz, colors)) ] for i, m in enumerate(markers): h, v, c = size_hvc[i] m.bump_id = i + 1 m.bump_tip_ijk = ijk[indices[i]] m.bump_height = h m.bump_points = v m.bump_connected = c session.models.add([self]) if color_surface: color_surface_from_mask(volume, mask)
def place_marker(session, xyz, color, radius, model_name, model_id=None): # Locate specified marker model mset = None from chimerax.markers import MarkerSet if model_id is not None: msets = session.models.list(model_id=model_id, type=MarkerSet) if len(msets) >= 1: mset = msets[0] if mset is None: # Create a new marker model mset = MarkerSet(session, model_name) mset.id = model_id session.models.add([mset]) # Place marker at center position. mset.create_marker(xyz, color, radius)
def restore_snapshot(session, data): s = MarkerSet.restore_snapshot(session, data['marker set state']) ei = {tuple(a.residue.number for a in b.atoms): b for b in s.bonds} for ei1, ei2 in data['joined edges']: e1, e2 = ei[ei1], ei[ei2] e1.joined_edge = e2 e2.joined_edge = e1 for pstate in data['polygons']: Polygon.polygon_from_state(pstate, s) return s
def place_markers(session, coords, radius=0.5, color=(180, 180, 180, 255), name='path', pair_map={}): ''' Create a MarkerSet with markers at positions specified by coords which maps an integer index to a Place instance. The origin of the Place i.e. where it maps (0,0,0) is the location of the marker. Markers with consecutive integer indices are connected with links, and pairs of markers specified in pair_map (mapping index to index) are also connected by links. Markers have residue number equal to their index. The MarkerSet is returned. ''' from chimerax.markers import MarkerSet, create_link mset = MarkerSet(session, name) # Create markers. mmap = {} btf = sorted(coords.items()) for b, tf in btf: xyz = tf.origin() mmap[b] = m = mset.create_marker(xyz, color, radius, id=b) m.extra_attributes = e = {'base_placement': tf} if b in pair_map: e['paired_with'] = pair_map[b] # Link consecutive markers. rl = 0.5 * radius for b, tf in btf: if b + 1 in mmap: create_link(mmap[b], mmap[b + 1], color, rl) # Link base pairs. for b1, b2 in pair_map.items(): if b1 < b2 and b1 in mmap and b2 in mmap: create_link(mmap[b1], mmap[b2], color, rl) return mset
def __init__(self, session, name, coords, signals): MarkerSet.__init__(self, session, name) self.coords = coords self.signals = signals self.num_times = len(signals) self.current_time = 0 self.signal_range = r = (signals.min(), signals.max()) self.color_range = r from chimerax.core.colors import BuiltinColormaps self.colormap = BuiltinColormaps['blue-white-red'] # self.colormap = BuiltinColormaps['spectral'] # self.colormap = BuiltinColormaps['ylgnbu-8'].rescale_range(0,.4) # self.colormap = BuiltinColormaps['ylorrd'] xyz_min, xyz_max = coords.min(axis=0), coords.max(axis=0) box_size = (xyz_max - xyz_min).max() from math import sqrt r = box_size / sqrt(len(coords)) self.radius_range = (r, 3 * r) self.create_markers(r) self.set_time(0)
def take_snapshot(self, session, flags): ei = {b: tuple(a.residue.number for a in b.atoms) for b in self.bonds} joined_edges = [(ei[b], ei[b.joined_edge]) for b in self.bonds if hasattr(b, 'joined_edge')] polygons = set( [a.polygon for a in self.atoms if hasattr(a, 'polygon')] + [b.polygon for b in self.bonds if hasattr(b, 'polygon')]) pstates = [p.polygon_state() for p in polygons] data = { 'marker set state': MarkerSet.take_snapshot(self, session, flags), 'joined edges': joined_edges, 'polygons': pstates, 'version': 1 } return data
def _create_line(self, end_point): mset = self._marker_set if mset is None or mset.deleted: from chimerax.markers import MarkerSet self._marker_set = mset = MarkerSet(self.session, 'tape measure') self.session.models.add([mset]) rgba = self._color r = self._radius # Create end-point markers m1 = mset.create_marker(self._start_point, rgba, r) m2 = mset.create_marker(end_point, rgba, r) self._markers = (m1, m2) # Create line between markers from chimerax.markers import create_link self._link = create_link(m1, m2, rgba, r)
def __init__(self, session, name='Cage'): MarkerSet.__init__(self, session, name=name)
def imod_models(session, chunk_list, name, mesh, contours): # Get coordinate transform before mesh and contour chunks tf = None use_minx = False if use_minx: for c in chunk_list: if c['id'] == b'MINX': # MINX chunk comes after mesh and contours. print('MINX cscale', c['cscale'], 'ctrans', c['ctrans'], 'otrans', c['otrans'], 'crot', c['crot']) # t = [xo-xc for xo,xc in zip(c['otrans'], c['ctrans'])] t = [-xc for xc in c['ctrans']] rx, ry, rz = c['crot'] rx = ry = rz = 0 from chimerax.geometry import rotation, translation, scale tf = (rotation((0, 0, 1), rz) * rotation( (0, 1, 0), ry) * rotation( (1, 0, 0), rx) * translation(t) * scale(c['cscale'])) surfaces = [] msets = [] pixel_size = None for c in chunk_list: cid = c['id'] if cid == b'IMOD': xyz_scale = c['xscale'], c['yscale'], c['zscale'] pixel_size = c['pixsize'] units = c['units'] # Units: 0 = pixels, 3 = km, 1 = m, -2 = cm, -3 = mm, # -6 = microns, -9 = nm, -10 = Angstroms, -12 = pm # print ('IMOD pixel size =', pixel_size, 'units', units, ' scale', xyz_scale) # print 'IMOD flags', bin(c['flags']) u = -10 if units == 0 else (0 if units == 1 else units) import math pixel_size_angstroms = pixel_size * math.pow(10, 10 + u) if tf is None: xs, ys, zs = [s * pixel_size_angstroms for s in xyz_scale] from chimerax.geometry import scale tf = scale((xs, ys, zs)) elif cid == b'OBJT': alpha = 1.0 - 0.01 * c['trans'] object_rgba = (c['red'], c['green'], c['blue'], alpha) obj_name = c['name'].decode('ascii') pds = c['pdrawsize'] radius = pds * pixel_size_angstroms if pds > 0 else pixel_size_angstroms fill = c['flags'] & (1 << 8) lines = not (c['flags'] & (1 << 11)) only_points = (not lines and not fill) link = not only_points open_contours = c['flags'] & (1 << 3) mset = None elif cid == b'MESH': if mesh: surf = create_mesh(session, c, tf, object_rgba, obj_name) surfaces.append(surf) elif cid == b'CONT': if contours: if mset is None: from chimerax.markers import MarkerSet mname = '%s %s contours' % (name, obj_name) mset = MarkerSet(session, mname) msets.append(mset) create_contour(c, tf, radius, object_rgba, link, open_contours, mset) return surfaces, msets, pixel_size