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 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 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 _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 mouse_drag(self, event): if not self._dragging and self._atom1: if self._is_drag(event): self._markerset = self.ms_cls(self.session, "bond") self._markerset.create_marker(self._atom1) self._atom1.structure.add([self._markerset]) return if self._markerset: x, y = event.position() pick = self.view.picked_object(x, y) if isinstance(pick, PickedAtom ) and pick.atom.structure is self._atom1.structure: atom2 = pick.atom if atom2 is self._atom1: return if self._markerset.num_atoms > 1: self._markerset.atoms[1].delete() self._atom2 = atom2 self._markerset.create_marker(atom2) fake_bond = create_link(*self._markerset.atoms) fake_bond.halfbond = True avg_radius = self.avg_bond_radius(self._atom1, self._atom2) fake_bond.radius = avg_radius elif self._atom1: self._atom2 = None x1, x2 = self.session.main_view.clip_plane_points(x, y) v = x2 - x1 v /= np.linalg.norm(v) p = np.dot(self._atom1.scene_coord - x1, v) pt = x1 + p * v + (self._atom1.coord - self._atom1.scene_coord) if self._markerset.num_atoms > 1: self._markerset.atoms[1].delete() avg_radius = self.avg_bond_radius(self._atom1) self._markerset.create_marker_from_point( pt, self._atom1.color, avg_radius, ) fake_bond = create_link(*self._markerset.atoms) fake_bond.halfbond = True fake_bond.radius = avg_radius
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 create_contour(c, transform, radius, rgba, link, open_contours, mset): from numpy import reshape varray = reshape(c['pt'], (c['psize'], 3)).copy() transform.transform_points(varray, in_place=True) mlist = [] links = [] r = radius if radius > 0 else 1 rgba8 = [int(r * 255) for r in rgba] from chimerax.markers import create_link for xyz in varray: m = mset.create_marker(xyz, rgba8, r) if link and mlist: l = create_link(m, mlist[-1], rgba8, r) links.append(l) mlist.append(m) if not open_contours: open_contours = (c['flags'] & (1 << 3)) if link and len(mlist) >= 3 and not open_contours: l = create_link(mlist[-1], mlist[0], rgba, radius) links.append(l)
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 add_link(a1, a2, color, radius): from chimerax.markers import create_link return create_link(a1, a2, rgba=color, radius=radius)
def highlight(session, selection, transparency=50, color=(0, 255, 0), scale=1.5): if not isinstance(color, tuple): color = color.uint8x4() selected_atoms = selection.atoms selected_bonds = selection.bonds color = [c for c in color[:3]] color.append(int(transparency * 255. / 100)) models = {} for atom in selected_atoms: if isinstance(atom.structure, Highlight): continue if atom.structure not in models: models[atom.structure] = ([atom], []) else: models[atom.structure][0].append(atom) for bond in selected_bonds: if isinstance(bond.structure, Highlight): continue if bond.structure not in models: models[bond.structure] = ([], [bond]) else: models[bond.structure][1].append(bond) for model in models: for child in model.child_models(): if isinstance(child, Highlight): ms = child break else: ms = Highlight(session, name="highlight") ms.ball_scale = model.ball_scale atoms = models[model][0] for atom in atoms: m = ms.create_marker(atom, color, scale) m.draw_mode = atom.draw_mode bonds = models[model][1] for bond in bonds: markers = [] for atom in bond.atoms: if not any(atom is a._follow for a in ms.atoms): # we want to hide the atom m = ms.create_marker(atom, color, 1e-16) markers.append(m) else: markers.extend([a for a in ms.atoms if a._follow is atom]) m1, m2 = markers for b in ms.bonds: if m1 in b.atoms and m2 in b.atoms: b.radius = scale * bond.radius break else: b = create_link(m1, m2, rgba=None, radius=scale * bond.radius) b.halfbond = True b.display = True b.hide = False model.add([ms])