def interpolateIndependent(xf, c0, c1, f): """Interpolate by splitting the transformation into a rotation and a translation.""" import chimera vr, a = xf.getRotation() # a is in degrees xt = xf.getTranslation() Tinv = chimera.Xform.translation(-xt) T = chimera.Xform.translation(xt * f) X0 = chimera.Xform(T) X0.multiply(chimera.Xform.rotation(vr, a * f)) X1 = chimera.Xform(T) X1.multiply(chimera.Xform.rotation(vr, -a * (1 - f))) X1.multiply(Tinv) return X0, X1
def create_object(self): import chimera xf = chimera.Xform() trans = apply(chimera.Vector, self.translation) xf.translate(trans) axis = apply(chimera.Vector, self.rotation_axis) xf.rotate(axis, self.rotation_angle) return xf
def BoxArrowMesh (w, h, l, al, color, xf, mol) : if mol == None : import _surface mol = _surface.SurfaceModel() chimera.openModels.add ( [mol] ) # , sameAs = alignTo) mol.name = "Box" v = numpy.zeros ( [14,3] ) # print "BoxMesh:", div at = 0 w = w / 2.0 h = h / 2.0 l = l / 2.0 if xf == None : xf = chimera.Xform() v[0] = xf.apply ( chimera.Point ( -w,-h,-l ) ) v[1] = xf.apply ( chimera.Point ( w,-h,-l ) ) v[2] = xf.apply ( chimera.Point ( w,h,-l ) ) v[3] = xf.apply ( chimera.Point ( -w,h,-l ) ) v[4] = xf.apply ( chimera.Point ( -w,-h,l-al ) ) v[5] = xf.apply ( chimera.Point ( w,-h,l-al ) ) v[6] = xf.apply ( chimera.Point ( w,h,l-al ) ) v[7] = xf.apply ( chimera.Point ( -w,h,l-al ) ) vi = [] vi.extend( Quad2Tri ( [0,3,2,1] ) ) vi.extend( Quad2Tri ( [1,5,6,2] ) ) vi.extend( Quad2Tri ( [2,6,7,3] ) ) vi.extend( Quad2Tri ( [0,4,5,1] ) ) vi.extend( Quad2Tri ( [0,4,7,3] ) ) vi.extend( Quad2Tri ( [5,4,7,6] ) ) v[8] = xf.apply ( chimera.Point ( -w,-h-al/2.0,l-al ) ) v[9] = xf.apply ( chimera.Point ( w,-h-al/2.0,l-al ) ) v[10] = xf.apply ( chimera.Point ( w,h+al/2.0,l-al ) ) v[11] = xf.apply ( chimera.Point ( -w,h+al/2.0,l-al ) ) v[12] = xf.apply ( chimera.Point ( -w,0,l ) ) v[13] = xf.apply ( chimera.Point ( w,0,l ) ) vi.extend( Quad2Tri ( [8,11,10,9] ) ) vi.extend( Quad2Tri ( [8,9,13,12] ) ) vi.extend( Quad2Tri ( [11,12,13,10] ) ) vi.extend( [(8,12,11)] ) vi.extend( [(9,10,13)] ) sph = mol.addPiece ( v, vi, color ) return sph
def interpolateLinear(xf, c0, c1, f): """Interpolate by translating c1 to c0 linearly along with rotation about translation point.""" import chimera vr, a = xf.getRotation() # a is in degrees c0v = c0.toVector() c1v = c1.toVector() Tinv0 = chimera.Xform.translation(-c0v) Tinv1 = chimera.Xform.translation(-c1v) dt = c1 - c0 R0 = chimera.Xform.rotation(vr, a * f) R1 = chimera.Xform.rotation(vr, -a * (1 - f)) T = chimera.Xform.translation(c0v + dt * f) X0 = chimera.Xform(T) X0.multiply(R0) X0.multiply(Tinv0) X1 = chimera.Xform(T) X1.multiply(R1) X1.multiply(Tinv1) return X0, X1
def handle_model_drag(self): if self.mode == 'move models': xf = self.phantom_device.transform() if self.last_phantom_transform: delta = chimera.Xform() delta.multiply(self.last_phantom_transform) delta.invert() delta.premultiply(xf) move_active_models(delta) self.last_phantom_transform = xf else: self.last_phantom_transform = None
def orient_bar(self, horizontal=1): m = self.model if m == None: return xf = chimera.Xform() xyz = m.openState.xform.getTranslation() xf.translate(xyz) if not horizontal: xf.zRotate(-90) m.openState.xform = xf
def topology(self): if self.gui.ui_input_note.index(self.gui.ui_input_note.select()): return self.gui.var_path.get() # else: model = self.gui.ui_chimera_models.getvalue() if model: sanitized_path = '{0[0]}{1}{0[1]}'.format( os.path.splitext(model.name), '_fixed') if os.path.isfile(sanitized_path): return sanitized_path else: output = getattr(model, 'openedAs', (model.name + '.pdb', ))[0] chimera.pdbWrite([model], chimera.Xform(), output) return output
def matchPositions(fixedPositions, movablePositions): m = Match(fixedPositions, movablePositions) mat = m.matrix() rot = chimera.Xform.xform(mat[0, 0], mat[0, 1], mat[0, 2], 0, mat[1, 0], mat[1, 1], mat[1, 2], 0, mat[2, 0], mat[2, 1], mat[2, 2], 0, True) fC = m.center(fixedPositions) fT = chimera.Xform.translation(chimera.Vector(fC[0], fC[1], fC[2])) mC = m.center(movablePositions) mT = chimera.Xform.translation(chimera.Vector(-mC[0], -mC[1], -mC[2])) xform = chimera.Xform() xform.multiply(fT) xform.multiply(rot) xform.multiply(mT) return xform, m.rms
def sanitize_model(self): # Each model in a single model #Getting molecule attributes model = self.ui_chimera_models.getvalue() modelfile_path = getattr(model, 'openedAs', (model.name, ))[0] basename, ext = os.path.splitext(modelfile_path) output_file = '{0}{1}{2}'.format(basename, '_fixed', ext or '.pdb') chimera.pdbWrite([model], chimera.Xform(), output_file) self.fix_pdb(output_file, out=output_file) m = chimera.openModels.open(output_file, sameAs=model)[0] m.name = model.name + ' - Fixed' self.ui_chimera_models.selection_clear() self.ui_chimera_models.selection_set( chimera.openModels.list().index(m)) model.display = False
def same_positions(self, mp1, mp2): import chimera angle_tolerance = 1e-5 translation_tolerance = 1e-5 for os, xf1 in mp1.items(): if mp2.has_key(os): xf2 = mp2[os] xf = chimera.Xform() xf.multiply(xf1) xf.invert() xf.multiply(xf2) trans = xf.getTranslation() axis, angle = xf.getRotation() if (abs(angle) > angle_tolerance or trans.length > translation_tolerance): return False return True
def _apply_pdbfix(molecule, pH=7.0, add_hydrogens=False): """ Run PDBFixer to ammend potential issues in PDB format. Parameters ---------- molecule : chimera.Molecule Chimera Molecule object to fix. pH : float, optional Target pH for adding missing hydrogens. add_hydrogens : bool, optional Whether to add missing hydrogens or not. Returns ------- memfile : StringIO An in-memory file with the modified PDB contents """ memfile = StringIO() chimera.pdbWrite([molecule], chimera.Xform(), memfile) chimera.openModels.close([molecule]) memfile.seek(0) fixer = PDBFixer(pdbfile=memfile) fixer.findMissingResidues() fixer.findNonstandardResidues() fixer.replaceNonstandardResidues() fixer.findMissingAtoms() fixer.addMissingAtoms() fixer.removeHeterogens(True) if add_hydrogens: fixer.addMissingHydrogens(pH) memfile.close() memfile = StringIO() PDBFile.writeFile(fixer.topology, fixer.positions, memfile) memfile.seek(0) molecule = chimera.openModels.open(memfile, type="PDB", identifyAs=molecule.name) chimera.openModels.remove(molecule) memfile.close() return molecule[0]
def flatten_icosahedron(multiscale_models, radius): clist = chains(multiscale_models) it = icosahedron_triangles(radius) itc = map(center, it) centers = map(chain_center, clist) ci = map(lambda c: closest_point_index(c, itc), centers) fit = flattened_icosahedron_triangles(radius) xforms = map(triangle_transformation, it, fit) for k in range(len(clist)): c = clist[k] c.unflatten_xform = c.xform import chimera xf = chimera.Xform() xf.multiply(c.xform) xf.premultiply(xforms[ci[k]]) c.set_xform(xf)
def PlaneMesh ( w, h, d, color, xf, mol ) : if mol == None : import _surface mol = _surface.SurfaceModel() chimera.openModels.add ( [mol] ) # , sameAs = alignTo) mol.name = "Box" atX = -w/2 atY = -h/2 if xf == None : xf = chimera.Xform() numx = int ( max ( numpy.ceil ( w / d ), 2 ) ) numy = int ( max ( numpy.ceil ( h / d ), 2 ) ) dx = w / float(numx-1) dy = h / float(numx-1) print " - plane - w %.2f, h %.2f, %d/%d" % (w, h, numx, numy) v = numpy.zeros ( [numx*numy,3] ) vi = [] for j in range ( numy ) : for i in range ( numx ) : v[j*numx+i] = xf.apply ( chimera.Point ( atX + dx*i, atY + dy*j, 0 ) ) #vs.append ( p.data() ) if i > 0 and j > 0 : p1 = j*numx+i p2 = j*numx+i-1 p3 = (j-1)*numx+i-1 p4 = (j-1)*numx+i vi.extend( Quad2Tri ( [p1,p2,p3,p4] ) ) sph = mol.addPiece ( v, vi, color ) return sph
def rotate_around_axis(axis, origin, screen_xy, last_screen_xy): # Measure drag around axis with mouse on front clip plane. sx, sy = screen_xy from VolumeViewer import slice xyz, back_xyz = slice.clip_plane_points(sx, sy) lsx, lsy = last_screen_xy last_xyz, back_last_xyz = slice.clip_plane_points(lsx, lsy) a = apply(chimera.Vector, axis) a.normalize() o = apply(chimera.Vector, origin) v = apply(chimera.Vector, xyz) lv = apply(chimera.Vector, last_xyz) t = chimera.cross(a, v - o) t2 = t.sqlength() if t2 > 0: angle = (t * (v - lv)) / t2 else: angle = 0 import math angle_deg = 180 * angle / math.pi vo = apply(chimera.Vector, origin) va = apply(chimera.Vector, axis) xf = chimera.Xform() xf.translate(vo) xf.rotate(va, angle_deg) xf.translate(-vo) # Applying xf performs the two translations and rotations in the # opposite order of the function calls. First it translates by -vo, # then rotates, then translates by +vo. move_active_models(xf)
def original_positions(): 'Move all models to original positions on open. Like reset command.' identity = chimera.Xform() for m in chimera.openModels.list(): m.openState.xform = identity
def interpolate(self, m, xform, **kw): """Interpolate to new conformation 'm'.""" # # Get all the options # if kw.has_key("method"): self.method = kw["method"] if kw.has_key("rate"): self.rate = kw["rate"] if kw.has_key("frames"): self.frames = kw["frames"] if kw.has_key("cartesian"): self.cartesian = kw["cartesian"] if kw.has_key("minimize"): self.minimize = kw["minimize"] if kw.has_key("steps"): self.steps = kw["steps"] if self.minimize: self.callback = self.minimizeCallback else: self.callback = self.interpolateCallback # # Find matching set of residues. First try for # one-to-one residue match, and, if that fails, # then finding a common set of residues. # import Segment sm = self.mol try: results = Segment.segmentHingeExact(sm, m) except ValueError: results = Segment.segmentHingeApproximate(sm, m) segments, atomMap, unusedResidues, unusedAtoms = results for r in unusedResidues: sm.deleteResidue(r) for a in unusedAtoms: sm.deleteAtom(a) if unusedResidues or unusedAtoms: from chimera import Sequence Sequence.invalidate(sm) if self.minimize: from chimera.baseDialog import AskYesNoDialog from chimera.tkgui import app d = AskYesNoDialog( "Cannot minimize with non-identical models.\n" "Continue without minimization?", default="Yes") if d.run(app) != "yes": raise ValueError("terminated at user request") self.minimize = False self.callback = self.interpolateCallback # # Interpolate between last conformation in trajectory # and new conformations # sm.activeCoordSet = sm.coordSets[max(sm.coordSets.keys())] from Interpolate import interpolate import chimera combinedXform = chimera.Xform(self.inverseXform) combinedXform.multiply(xform) interpolate(sm, combinedXform, segments, atomMap, self.method, self.rate, self.frames, self.cartesian, self.callback)