def test_msms_surface(): eps = 1e-3 cmd.reinitialize() cmd.fragment('ala', 'm1') # default psico.msms.msms_surface(name='surf1') extent = cmd.get_extent('surf1') assert extent[0] == approx([-2.705, -3.208, -2.413], rel=eps) assert extent[1] == approx([3.530, 2.907, 2.676], rel=eps) # global solvent_radius cmd.set('solvent_radius', 3.5) psico.msms.msms_surface(name='surf2') extent = cmd.get_extent('surf2') assert extent[0] == approx([-2.705, -3.169, -2.436], rel=eps) assert extent[1] == approx([3.530, 2.907, 2.676], rel=eps) # object-level solvent_radius cmd.set('solvent_radius', 2.8, 'm1') psico.msms.msms_surface(name='surf3') extent = cmd.get_extent('surf3') assert extent[0] == approx([-2.705, -3.161, -2.427], rel=eps) assert extent[1] == approx([3.530, 2.907, 2.676], rel=eps) # modified atom radii cmd.alter('m1', 'vdw = 3.0') psico.msms.msms_surface(name='surf4') extent = cmd.get_extent('surf4') assert extent[0] == approx([-4.605, -5.162, -4.418], rel=eps) assert extent[1] == approx([5.030, 4.861, 4.681], rel=eps)
def testGetExtent(self): cmd.load(self.datafile("1ehz-5.pdb"), "m1") self.assertArrayEqual(cmd.get_extent(), [[49.85, 40.59, 41.73], [69.99, 51.73, 56.47]], delta=1e-2) self.assertArrayEqual(cmd.get_extent("resi 3"), [[58.29, 40.87, 48.52], [64.74, 50.42, 55.29]], delta=1e-2) cmd.load(self.datafile("h2o-elf.cube"), "map1") self.assertArrayEqual( cmd.get_extent("map1"), [[-0.075, -0.075, -0.075], [5.925, 5.925, 5.925]], delta=1e-2)
def test(self, filename): cmd.load(self.datafile(filename), 'map') ori = ORIGINS[filename] ext = [ori, cpv.add(ori, EDGELENGTHS)] self.assertArrayEqual(cmd.get_extent('map'), ext, delta=1e-3) with testing.mktemp('.map') as filename: cmd.save(filename, 'map') cmd.delete('*') cmd.load(filename, 'map') self.assertArrayEqual(cmd.get_extent('map'), ext, delta=1e-3)
def draw_AABB(selection): """ DESCRIPTION For a given selection, draw the Axes Aligned bounding box around it without padding. Code taken and modified from DrawBoundingBox.py. """ AA_original = selection + "_original" model_orig = cmd.create(AA_original, selection) ([min_X, min_Y, min_Z], [max_X, max_Y, max_Z]) = cmd.get_extent(AA_original) print( "The Axis Aligned Bounding Box (AABB) dimensions are (%.2f, %.2f, %.2f)" % (max_X - min_X, max_Y - min_Y, max_Z - min_Z)) print("The Axis Aligned Bounding Box (AABB) volume is %.2f A3" % ((max_X - min_X) * (max_Y - min_Y) * (max_Z - min_Z))) min_X = min_X min_Y = min_Y min_Z = min_Z max_X = max_X max_Y = max_Y max_Z = max_Z boundingBox = [ LINEWIDTH, float(2), BEGIN, LINES, COLOR, float(1), float(1), float(0), VERTEX, min_X, min_Y, min_Z, VERTEX, min_X, min_Y, max_Z, VERTEX, min_X, max_Y, min_Z, VERTEX, min_X, max_Y, max_Z, VERTEX, max_X, min_Y, min_Z, VERTEX, max_X, min_Y, max_Z, VERTEX, max_X, max_Y, min_Z, VERTEX, max_X, max_Y, max_Z, VERTEX, min_X, min_Y, min_Z, VERTEX, max_X, min_Y, min_Z, VERTEX, min_X, max_Y, min_Z, VERTEX, max_X, max_Y, min_Z, VERTEX, min_X, max_Y, max_Z, VERTEX, max_X, max_Y, max_Z, VERTEX, min_X, min_Y, max_Z, VERTEX, max_X, min_Y, max_Z, VERTEX, min_X, min_Y, min_Z, VERTEX, min_X, max_Y, min_Z, VERTEX, max_X, min_Y, min_Z, VERTEX, max_X, max_Y, min_Z, VERTEX, min_X, min_Y, max_Z, VERTEX, min_X, max_Y, max_Z, VERTEX, max_X, min_Y, max_Z, VERTEX, max_X, max_Y, max_Z, END ] p0 = '_0' + str(randint(0, 100)) p1 = '_1' + str(randint(0, 100)) p2 = '_2' + str(randint(0, 100)) p3 = '_3' + str(randint(0, 100)) cmd.pseudoatom(pos=[min_X, min_Y, min_Z], object=p0) cmd.pseudoatom(pos=[min_X, min_Y, max_Z], object=p1) cmd.pseudoatom(pos=[min_X, max_Y, min_Z], object=p2) cmd.pseudoatom(pos=[max_X, min_Y, min_Z], object=p3) cmd.distance(None, p0, p3) cmd.distance(None, p0, p2) cmd.distance(None, p0, p1) cmd.hide("nonbonded") boxName = "box_AABB_" + str(randint(0, 100)) cmd.load_cgo(boundingBox, boxName) return boxName
def show_points(points, color = [1.0, 0.0, 0.0], selection='all', name = 'samples', labels=[]): view = cmd.get_view(quiet=not DEBUG) # adjust radius to size of bounding box bb = cmd.get_extent(selection, quiet=not DEBUG) ll = vec3(bb[0]) tr = vec3(bb[1]) diag = tr - ll r = diag.length() / 2.0 # origin of rotation in model space #o = vec3(view[12:15]) c = com.COM(selection) o = vec3(c) #spheres = [BEGIN, TRIANGLE_STRIP] spheres = [COLOR] spheres.extend(color) i = 0.0 j = 0 for p in points: #spheres.extend([COLOR, 1.0, 1 - scaled_value, 1 - scaled_value]) spheres.extend([SPHERE, o[0]+r*p[0], o[1]+r*p[1], o[2]+r*p[2], 1.25]) #drawVector(o, o + r * vec3(p)) #spheres.extend([VERTEX, o[0]+r*p[0], o[1]+r*p[1], o[2]+r*p[2]]) #i += 1.0/len(values) l = 1.1 if (len(labels) > j): cmd.pseudoatom(labels[j] + "_label", pos = [o[0]+l * r*p[0], o[1]+l*r*p[1], o[2]+l*r*p[2], 1.25], label = labels[j]) j += 1 #spheres.extend([END]) cmd.load_cgo(spheres, name, 1)
def _(): has_props = ['no', 'no'] def callback(partial_charge, elec_radius): if partial_charge: has_props[0] = 'YES' if elec_radius > 0: has_props[1] = 'YES' n = _self.iterate(form.input_sele.currentText(), 'callback(partial_charge, elec_radius)', space={'callback': callback}) # grid auto-value (keep map size in the order of 200x200x200) if n > 1 and not form.apbs_grid_userchanged: e = _self.get_extent(form.input_sele.currentText()) volume = (e[1][0] - e[0][0]) * (e[1][1] - e[0][1]) * (e[1][2] - e[0][2]) grid = max(0.5, volume**0.333 / 200.0) form.apbs_grid.setValue(grid) if n < 1: label = 'Selection is invalid' color = '#f66' elif has_props == ['YES', 'YES']: label = 'No preparation necessary, selection has charges and radii' form.do_prepare.setChecked(False) color = '#6f6' else: label = 'Selection needs preparation (partial_charge: %s, elec_radius: %s)' % tuple( has_props) form.do_prepare.setChecked(True) color = '#fc6' form.label_sele_has.setText(label) form.label_sele_has.setStyleSheet('background: %s; padding: 5' % color)
def test_load_hypothesis_xvol(self): from epymol import ph4 ph4.load_hypothesis_xvol(self.datafile('phase/1M7Q_hypo.xvol')) extent = cmd.get_extent('1M7Q_hypo') self.assertArrayEqual( extent, [[33.622, 23.055, 23.741], [54.344, 38.530, 37.948]], delta=1e-2)
def test_load_hypothesis_xyz(self): from epymol import ph4 ph4.load_hypothesis_xyz(self.datafile('phase/hypothesis.xyz'), ref=self.datafile('phase/ALL.xyz')) extent = cmd.get_extent('hypothesis') self.assertArrayEqual( extent, [[-4.218, -2.742, -3.702], [10.536, 6.152, 3.536]], delta=1e-2)
def center_coords(file_name): pymol.finish_launching() cmd.delete('all') cmd.load(file_name) xyz_limits = cmd.get_extent() xyz_mean = [(xyz_limits[0][i] + xyz_limits[1][i]) / 2 for i in range(3)] cmd.translate([-1 * xyz_mean[i] for i in range(3)], 'all') cmd.save(file_name, 'all')
def test_load_hypothesis_xvol(self): from epymol import ph4 ph4.load_hypothesis_xvol( self.datafile('phase/1M7Q_hypo.xvol')) extent = cmd.get_extent('1M7Q_hypo') self.assertArrayEqual(extent, [[33.622, 23.055, 23.741], [54.344, 38.530, 37.948]], delta=1e-2)
def symexpcell(prefix='mate', object=None, a=0, b=0, c=0): """ DESCRIPTION Creates all symmetry-related objects for the specified object that occur with their bounding box center within the unit cell. USAGE symexpcell prefix, object, [a, b, c] ARGUMENTS prefix = string: prefix of new objects object = string: object for which to create symmetry mates a, b, c = integer: create neighboring cell {default: 0,0,0} SEE ALSO symexp, http://www.pymolwiki.org/index.php/SuperSym """ if object is None: object = cmd.get_object_list()[0] sym = cmd.get_symmetry(object) cell_edges = sym[0:3] cell_angles = sym[3:6] spacegroup = sym[6] basis = cellbasis(cell_angles, cell_edges) basis = numpy.matrix(basis) extent = cmd.get_extent(object) center = sum(numpy.array(extent)) * 0.5 center = numpy.matrix(center.tolist() + [1.0]).T center_cell = basis.I * center extra_shift = [[float(i)] for i in (a,b,c)] i = 0 matrices = xray.sg_sym_to_mat_list(spacegroup) for mat in matrices: i += 1 mat = numpy.matrix(mat) shift = numpy.floor(mat * center_cell) mat[0:3,3] -= shift[0:3,0] mat[0:3,3] += extra_shift mat = basis * mat * basis.I mat_list = list(mat.flat) name = '%s%d' % (prefix, i) cmd.create(name, object) cmd.transform_object(name, mat_list, 0) cmd.color(i+1, name)
def symexpcell(prefix='mate', object=None, a=0, b=0, c=0): ''' DESCRIPTION Creates all symmetry-related objects for the specified object that occur with their bounding box center within the unit cell. USAGE symexpcell prefix, object, [a, b, c] ARGUMENTS prefix = string: prefix of new objects object = string: object for which to create symmetry mates a, b, c = integer: create neighboring cell {default: 0,0,0} SEE ALSO symexp, http://www.pymolwiki.org/index.php/SuperSym ''' if object is None: object = cmd.get_object_list()[0] sym = cmd.get_symmetry(object) cell_edges = sym[0:3] cell_angles = sym[3:6] spacegroup = sym[6] basis = cellbasis(cell_angles, cell_edges) basis = numpy.matrix(basis) extent = cmd.get_extent(object) center = sum(numpy.array(extent)) * 0.5 center = numpy.matrix(center.tolist() + [1.0]).T center_cell = basis.I * center extra_shift = [[float(i)] for i in (a,b,c)] i = 0 matrices = xray.sg_sym_to_mat_list(spacegroup) for mat in matrices: i += 1 mat = numpy.matrix(mat) shift = numpy.floor(mat * center_cell) mat[0:3,3] -= shift[0:3,0] mat[0:3,3] += extra_shift mat = basis * mat * basis.I mat_list = list(mat.flat) name = '%s%d' % (prefix, i) cmd.create(name, object) cmd.transform_object(name, mat_list) cmd.color(i+1, name)
def testLoad_cube(self): cmd.load(self.datafile('h2o-elf.cube')) extent = cmd.get_extent('h2o-elf') self.assertArrayEqual( extent, [[-0.075, -0.075, -0.075], [5.925, 5.925, 5.925]], delta=1e3) field = cmd.get_volume_field('h2o-elf', copy=0) self.assertEqual(field.shape, (40, 40, 40)) self.assertAlmostEqual(field.mean(), 0.06915, delta=1e-4)
def testLoad_cube(self): cmd.load(self.datafile('h2o-elf.cube')) extent = cmd.get_extent('h2o-elf') self.assertArrayEqual(extent, [ [ -0.075, -0.075, -0.075], [ 5.925, 5.925, 5.925]], delta=1e3) field = cmd.get_volume_field('h2o-elf', copy=0) self.assertEqual(field.shape, (40, 40, 40)) self.assertAlmostEqual(field.mean(), 0.06915, delta=1e-4)
def test_load_hypothesis_xyz(self): from epymol import ph4 ph4.load_hypothesis_xyz( self.datafile('phase/hypothesis.xyz'), ref=self.datafile('phase/ALL.xyz')) extent = cmd.get_extent('hypothesis') self.assertArrayEqual(extent, [[-4.218, -2.742, -3.702], [10.536, 6.152, 3.536]], delta=1e-2)
def gen_cp2k_qmmm (filename, qmsele, mmsele): """ Generate &QMMM section of CP2K input N.B.: run "set retain_order, 1" command before loading QMMM object to PyMol to avoid atoms indices mismatch between PyMol and CP2K N.B.: The output is written to file "filename". "filename" will be overwritten! USAGE: gen_cp2k_qmmm filename, qm_part_selection_name, the_whole_system_object """ f = open(filename, 'w') print >> f, "&QMMM" # QMMM CELL setup # n.b. 6A padding # n.b. cell should be cubic in case of PSOLVER=WAVELET ext = cmd.get_extent(qmsele) cell = (ext[1][0]-ext[0][0] + 6, ext[1][1]-ext[0][1] + 6, ext[1][2]-ext[0][2] + 6) print >> f, " &CELL\n ABC %.2f %.2f %.2f\n &END CELL" % cell # KINDS stored.elems=[] cmd.iterate_state(1, qmsele, "stored.elems.append(elem)") kinds = set(stored.elems) for kind in kinds: print >> f, " &QM_KIND", kind stored.ids=[] cmd.iterate_state(1, qmsele + ' & elem '+kind, "stored.ids.append(ID)") print >> f, " MM_INDEX", for id in stored.ids: print >> f, id, print >> f, "\n &END QM_KIND" # LINKS stored.qm_ids = [] cmd.iterate_state(1, qmsele, "stored.qm_ids.append(ID)") model = cmd.get_model(mmsele) for bond in model.bond: [ia, ib] = bond.index a = model.atom[ia].id b = model.atom[ib].id link = 0 if ((a in stored.qm_ids) and not (b in stored.qm_ids)): qmid = a mmid = b link = 1 if ((b in stored.qm_ids) and not (a in stored.qm_ids)): mmid = a qmid = b link = 1 if link: print >> f, " &LINK\n QM_INDEX %d\n MM_INDEX %d\n QM_KIND H\n &END LINK" % (qmid, mmid) print >> f, "&END QMMM"
def draw_IABB(selection): """ DESCRIPTION For a given selection, draw the Inertia Axes Aligned bounding box around it without padding. Code taken and modified from DrawBoundingBox.py. """ transformar(selection) ([minX, minY, minZ], [maxX, maxY, maxZ]) = cmd.get_extent(selection) print( "The Inertia Axis Aligned Bounding Box (IABB) dimensions are (%.2f, %.2f, %.2f)" % (maxX - minX, maxY - minY, maxZ - minZ)) print("The Inertia Axis Aligned Bounding Box (IABB) volume is %.2f A3" % ((maxX - minX) * (maxY - minY) * (maxZ - minZ))) minX = minX minY = minY minZ = minZ maxX = maxX maxY = maxY maxZ = maxZ boundingBox = [ LINEWIDTH, float(2), BEGIN, LINES, COLOR, float(1), float(0), float(0), VERTEX, minX, minY, minZ, VERTEX, minX, minY, maxZ, VERTEX, minX, maxY, minZ, VERTEX, minX, maxY, maxZ, VERTEX, maxX, minY, minZ, VERTEX, maxX, minY, maxZ, VERTEX, maxX, maxY, minZ, VERTEX, maxX, maxY, maxZ, VERTEX, minX, minY, minZ, VERTEX, maxX, minY, minZ, VERTEX, minX, maxY, minZ, VERTEX, maxX, maxY, minZ, VERTEX, minX, maxY, maxZ, VERTEX, maxX, maxY, maxZ, VERTEX, minX, minY, maxZ, VERTEX, maxX, minY, maxZ, VERTEX, minX, minY, minZ, VERTEX, minX, maxY, minZ, VERTEX, maxX, minY, minZ, VERTEX, maxX, maxY, minZ, VERTEX, minX, minY, maxZ, VERTEX, minX, maxY, maxZ, VERTEX, maxX, minY, maxZ, VERTEX, maxX, maxY, maxZ, END ] p4 = '_4' + str(randint(0, 100)) p5 = '_5' + str(randint(0, 100)) p6 = '_6' + str(randint(0, 100)) p7 = '_7' + str(randint(0, 100)) cmd.pseudoatom(pos=[minX, minY, minZ], object=p4) cmd.pseudoatom(pos=[minX, minY, maxZ], object=p5) cmd.pseudoatom(pos=[minX, maxY, minZ], object=p6) cmd.pseudoatom(pos=[maxX, minY, minZ], object=p7) cmd.distance(None, p4, p7) cmd.distance(None, p4, p6) cmd.distance(None, p4, p5) cmd.hide("nonbonded") boxName = "box_IABB_" + str(randint(0, 100)) cmd.load_cgo(boundingBox, boxName) return boxName
def generate_grid(rna, outname="test", grid_size=2, padding=2.0, mindist=2.5, maxdist=5.0, debug=False): # goto working directory # setup 3D grid extent = cmd.get_extent(selection=rna) x = np.arange(extent[0][0] - padding, extent[1][0] + padding, grid_size) y = np.arange(extent[0][1] - padding, extent[1][1] + padding, grid_size) z = np.arange(extent[0][2] - padding, extent[1][2] + padding, grid_size) xx, yy, zz = np.meshgrid(x, y, z) nx, ny, nz = xx.shape[0], xx.shape[1], xx.shape[2] # place pseudoatoms along grid k = 1 for ix in tqdm(range(nx)): for iy in range(ny): for iz in range(nz): cmd.pseudoatom("tmpPoint", hetatm=1, name="C", resn="UNK", resi=k, chain="ZZ", pos=[ float(xx[ix, iy, iz]), float(yy[ix, iy, iz]), float(zz[ix, iy, iz]) ]) # prune cmd.remove("resn UNK and resi %s within %s of polymer" % (k, mindist)) cmd.remove("resn UNK and resi %s beyond %s of polymer" % (k, maxdist)) # write out grid file coor = "%s_grid.xyz" % (outname) xyz = cmd.get_coords('tmpPoint', 1) df = pd.DataFrame.from_records(xyz) df.insert(0, "element", "C") df.to_csv(coor, index=False, header=False, sep=" ") # write out complex cmd.create("complex", "%s tmpPoint" % rna) coor = "%s_grid.pdb" % (outname) cmd.save(coor, "complex") if debug: cmd.show("surface", "polymer") cmd.show("spheres", "resn UNK")
def testLoad_map(self, ext, check_extent): filename = self.datafile('emd_1155' + ext) if not os.path.exists(filename): self.skipTest("missing " + filename) cmd.load(filename, 'map1') field = cmd.get_volume_field('map1', copy=0) self.assertEqual(field.shape, (141, 91, 281)) if check_extent: extent = cmd.get_extent('map1') self.assertArrayEqual(extent, [[0.0, 0.0, 0.0], [2296.0, 1476.0, 4592.0]], delta=1e-2)
def getbox(selection = "(sele)", extending = 5.0): cmd.hide("spheres") cmd.show("spheres", selection) ([minX, minY, minZ],[maxX, maxY, maxZ]) = cmd.get_extent(selection) minX = minX - float(extending) minY = minY - float(extending) minZ = minZ - float(extending) maxX = maxX + float(extending) maxY = maxY + float(extending) maxZ = maxZ + float(extending) cmd.zoom(showbox(minX, maxX, minY, maxY, minZ, maxZ)) return
def save_sele_as_ligExp(selection='sele'): ([minX, minY, minZ], [maxX, maxY, maxZ]) = cmd.get_extent(selection) pdb_base = cmd.get_object_list(selection)[0] cmd.disable('*') cmd.enable('pdb_base', 1) replace_list = ['_aligned_rm_ion', '_aligned', '_rm', '_ion', '_preped'] pdb_base_short = pdb_base for rep in replace_list: pdb_base_short = pdb_base_short.replace(rep, '') cmd.save('ligExp_' + pdb_base_short + ".sdf", 'sele', -1, 'sdf') cmd.delete('sele') #cmd.disable(pdb_base) cmd.delete(pdb_base) return
def testLoadMTZ(self): cmd.load_mtz(self.datafile('4rwb.mtz'), 'foo', 'cryst_1/data_1/FP', 'cryst_1/data_1/PHIC') self.assertEqual(cmd.get_names(), ['foo']) extent = cmd.get_extent('foo') self.assertArrayEqual(extent, [ [ 0.000, 0.000, 0.000], [ 37.585, 40.586, 27.214]], delta=1e-3, msg='extend wrong') # min, max, mean, stdev mmms = cmd.get_volume_histogram('foo', 0) self.assertArrayEqual(mmms, [-2.6767, 4.9998, 0.0, 1.0], delta=1e-4, msg='histogram wrong')
def testLoadMTZ(self): cmd.load_mtz(self.datafile('4rwb.mtz'), 'foo', 'cryst_1/data_1/FP', 'cryst_1/data_1/PHIC') self.assertEqual(cmd.get_names(), ['foo']) extent = cmd.get_extent('foo') self.assertArrayEqual( extent, [[0.000, 0.000, 0.000], [37.585, 40.586, 27.214]], delta=1e-3, msg='extend wrong') # min, max, mean, stdev mmms = cmd.get_volume_histogram('foo', 0) self.assertArrayEqual(mmms, [-2.6767, 4.9998, 0.0, 1.0], delta=1e-4, msg='histogram wrong')
def Get_MaxWidth(selection,state): try: nAtoms = cmd.count_atoms(selection, state=state) if nAtoms > 0: MinMax = cmd.get_extent(selection, state=state) MaxWidth = 0.0 for i in range(0,3): if (MinMax[1][i]-MinMax[0][i]) > MaxWidth: MaxWidth = (MinMax[1][i]-MinMax[0][i]) return MaxWidth except: return -1
def testLoad_pqr_various(self): import glob pqrdir = self.datafile('pqr') extent_expect = { 19: [[1999.32, 2998.77, -901.70], [2008.4, 3006.35, -898.02]], 36: [[1998.629, 2998.033, -902.509], [2008.765, 3006.347, -898.022]], } for filename in glob.glob(os.path.join(pqrdir, '*.pqr')): cmd.load(filename) n_atom = cmd.count_atoms() extent = cmd.get_extent() self.assertArrayEqual(extent, extent_expect[n_atom], delta=1e-2, msg=filename) if 'chain' in os.path.basename(filename): self.assertEqual(cmd.get_chains(), ['A']) else: self.assertEqual(cmd.get_chains(), ['']) cmd.delete('*')
def Get_CenterOfMass2(selection, state): try: nAtoms = cmd.count_atoms(selection, state=state) if nAtoms > 0: MinMax = cmd.get_extent(selection, state=state) MaxWidth = 0.0 for i in range(0,3): if (MinMax[1][i]-MinMax[0][i]) > MaxWidth: MaxWidth = (MinMax[1][i]-MinMax[0][i]) return [ (MinMax[1][0]+MinMax[0][0])/2.0, (MinMax[1][1]+MinMax[0][1])/2.0, (MinMax[1][2]+MinMax[0][2])/2.0 ] except: return []
def protein_vacuum_esp(selection, mode=2, border=10.0, quiet = 1, _self=cmd): pymol=_self._pymol cmd=_self if ((string.split(selection)!=[selection]) or selection not in cmd.get_names('objects')): print " Error: must provide an object name" raise cmd.QuietException obj_name = selection + "_e_chg" map_name = selection + "_e_map" pot_name = selection + "_e_pot" cmd.disable(selection) cmd.delete(obj_name) cmd.delete(map_name) cmd.delete(pot_name) cmd.create(obj_name,"((polymer and ("+selection+ ") and (not resn A+C+T+G+U)) or ((bymol (polymer and ("+ selection+"))) and resn NME+NHE+ACE)) and (not hydro)") # try to just get protein... protein_assign_charges_and_radii(obj_name,_self=_self) ext = cmd.get_extent(obj_name) max_length = max(abs(ext[0][0] - ext[1][0]),abs(ext[0][1] - ext[1][1]),abs(ext[0][2]-ext[1][2])) + 2*border # compute an grid with a maximum dimension of 50, with 10 A borders around molecule, and a 1.0 A minimum grid sep = max_length/50.0 if sep<1.0: sep = 1.0 print " Util: Calculating electrostatic potential..." if mode==0: # absolute, no cutoff cmd.map_new(map_name,"coulomb",sep,obj_name,border) elif mode==1: # neutral, no cutoff cmd.map_new(map_name,"coulomb_neutral",sep,obj_name,border) else: # local, with cutoff cmd.map_new(map_name,"coulomb_local",sep,obj_name,border) cmd.ramp_new(pot_name, map_name, selection=obj_name,zero=1) cmd.hide("everything",obj_name) cmd.show("surface",selection) cmd.set("surface_color",pot_name,selection) cmd.set("surface_ramp_above_mode",1,selection)
def exportvdw(self, selection='(all)'): sel = cmd.get_model(selection) ext = cmd.get_extent(selection) mincorner = ext[0] maxcorner = ext[1] cnt = 0 f = open("vystup.dat", "w") # f.write("%lf\t%lf\t%lf\t%lf\t%lf\t%lf\n" % (mincorner[0],mincorner[1],mincorner[2],maxcorner[0],maxcorner[1],maxcorner[2])) f.write("%d\n" % len(sel.atom)) for a in sel.atom: x = a.coord[0] y = a.coord[1] z = a.coord[2] rad = a.vdw name = a.name text = "%s\t%lf\t%lf\t%lf\t%lf\n" % (name, x, y, z, rad) f.write(text) cnt += 1 f.close()
def find_center_of_coordinates(selection='(all)', state=-1): """ Find center of coordinates of the selection and return the value USAGE find_center_of_coordinates selection find_center_of_coordinates selection, state=state ARGUMENTS selection a selection-expression state a state index if positive int, 0 to all, or -1 to current """ # find middle x, y, z coordinate of the selection state = utils.int_to_state(state) minc, maxc = cmd.get_extent(selection, state=state) coc = [float(l + (u - l) / 2.0) for l, u in zip(minc, maxc)] return coc
def draw_up_vector(radius=1.0, rgb=[1.0, 1.0, 1.0], selection='all'): view = cmd.get_view(quiet=1) rot = get_rotation_from_view(view) cam = rot.getRow(2).normalize() model_up = rot.getRow(1).normalize() right = rot.getRow(0).normalize() bb = cmd.get_extent(quiet=not DEBUG, selection=selection) ll = vec3(bb[0]) tr = vec3(bb[1]) diag = tr - ll r = diag.length() / 4.0 c = com.COM(selection) cvec = vec3(c) #cvec = vec3(view[12:15]) drawVector(cvec, cvec + r * model_up, name='up') drawVector(cvec, cvec + r * right, name='right', rgb=[1.0, 0.0, 0.0]) drawVector(cvec, cvec + r * cam, name='cam', rgb=[0.0, 1.0, 0.0])
def test(self): try: import numpy except ImportError: self.skip('requires numpy') from chempy.brick import Brick spacing = (.5, .5, .5) origin = (3., 4., 5.) shape = (10, 10, 10) range_ = [a * (b - 1) for (a, b) in zip (spacing, shape)] data = numpy.zeros(shape, float) brick = Brick.from_numpy(data, spacing, origin) self.assertArrayEqual(brick.range, range_) cmd.load_brick(brick, 'map') extent = cmd.get_extent('map') self.assertArrayEqual(extent, [origin, cpv.add(origin, range_)])
def getgridfile(receptor, selection = "(sele)", extending = 5.0): cmd.hide("spheres") cmd.show("spheres", selection) ([minX, minY, minZ],[maxX, maxY, maxZ]) = cmd.get_extent(selection) minX = minX - float(extending) minY = minY - float(extending) minZ = minZ - float(extending) maxX = maxX + float(extending) maxY = maxY + float(extending) maxZ = maxZ + float(extending) SizeX = maxX - minX SizeY = maxY - minY SizeZ = maxZ - minZ CenterX = (maxX + minX)/2 CenterY = (maxY + minY)/2 CenterZ = (maxZ + minZ)/2 AutoDockBox =f'''-------- AutoDock Grid Map for {receptor} -------- spacing 0.375 # spacing (A) npts {SizeX/0.375:.3f} {SizeY/0.375:.3f} {SizeZ/0.375:.3f} gridcenter {CenterX:.3f} {CenterY:.3f} {CenterZ:.3f}''' return AutoDockBox
def reps(self, cleanup=0): rep_list = [ "lines", "sticks", "spheres", "surface", "mesh", "dots", "ribbon", "cartoon" ] try: if not cleanup: cmd.disable() cmd.set("suspend_updates", 1, quiet=1) cmd.load("$PYMOL_DATA/demo/pept.pdb", "rep1") cmd.alter("rep1///1-5+8-13/", "ss='S'") cmd.cartoon("auto") cmd.hide("everything", "rep1") for a in range(2, 9): cmd.create("rep%d" % a, "rep1") for x, y in enumerate(rep_list, 1): cmd.show(x, "rep%d" % y) cmd.reset() cmd.zoom("rep1", 24) util.cbay("rep2") util.cbac("rep3") util.cbas("rep4") util.cbab("rep5") util.cbaw("rep6") util.cbay("rep8") cmd.set("suspend_updates", 0, quiet=1) scale = 0.5 for b in range(1, 20): cmd.set("suspend_updates", 0, quiet=1) cmd.refresh() cmd.set("suspend_updates", 1, quiet=1) xt = -3.2 yt = 1.6 for a in range(1, 5): cmd.translate([xt * scale, yt * scale, 0], object="rep%d" % a, camera=0) xt = xt + 2 yt = -yt xt = -3.2 for a in range(5, 9): cmd.translate([xt * scale, yt * scale, 0], object="rep%d" % a, camera=0) xt = xt + 2 for a in range(1, 9): cmd.origin("rep%d" % a, object="rep%d" % a) cmd.mset("1") st = ' '.join( map( lambda x, y: "rotate angle=-3,object=rep%d,axis=%s;" % (x, y), list(range(1, 9)), ['x', 'y', 'x', 'y', 'x', 'y', 'x', 'y'])) cmd.mdo(1, st) cmd.set("suspend_updates", 0, quiet=1) cmd.mplay() cgo = [] axes = [[4.5, 0.0, 0.0], [0.0, 3.0, 0.0], [0.0, 0.0, 3.0]] c = 1 for a in rep_list: ext = cmd.get_extent("rep%d" % c) pos = [(ext[0][0] + ext[1][0]) / 2, (ext[0][1] + ext[1][1]) / 2 + 14, (ext[0][2] + ext[1][2]) / 2] c = c + 1 pos[0] = pos[0] - (measure_text(plain, a, axes) / 2) wire_text(cgo, plain, pos, a, axes) cmd.set("cgo_line_width", 1.5) cmd.set("auto_zoom", 0) cmd.load_cgo(cgo, 'reps') cmd.set("auto_zoom", 1) else: cmd.delete("rep*") cmd.mset() cmd.mstop() except: traceback.print_exc()
def _testLoad_dx(self, filename): cmd.load(self.datafile(filename), 'map1') extent = cmd.get_extent('map1') self.assertArrayEqual(extent, [[1.0, 2.0, 3.0], [5.0, 7.0, 9.0]], delta=1e-2)
def draw_AABB(selection): """ DESCRIPTION For a given selection, draw the Axes Aligned bounding box around it without padding. Code taken and modified from DrawBoundingBox.py. """ AA_original = selection + "_original" model_orig = cmd.create(AA_original, selection) ([min_X, min_Y, min_Z],[max_X, max_Y, max_Z]) = cmd.get_extent(AA_original) print("The Axis Aligned Bounding Box (AABB) dimensions are (%.2f, %.2f, %.2f)" % (max_X-min_X, max_Y-min_Y, max_Z-min_Z)) print("The Axis Aligned Bounding Box (AABB) volume is %.2f A3" % ((max_X-min_X)*(max_Y-min_Y)*(max_Z-min_Z))) min_X = min_X min_Y = min_Y min_Z = min_Z max_X = max_X max_Y = max_Y max_Z = max_Z boundingBox = [ LINEWIDTH, float(2), BEGIN, LINES, COLOR, float(1), float(1), float(0), VERTEX, min_X, min_Y, min_Z, VERTEX, min_X, min_Y, max_Z, VERTEX, min_X, max_Y, min_Z, VERTEX, min_X, max_Y, max_Z, VERTEX, max_X, min_Y, min_Z, VERTEX, max_X, min_Y, max_Z, VERTEX, max_X, max_Y, min_Z, VERTEX, max_X, max_Y, max_Z, VERTEX, min_X, min_Y, min_Z, VERTEX, max_X, min_Y, min_Z, VERTEX, min_X, max_Y, min_Z, VERTEX, max_X, max_Y, min_Z, VERTEX, min_X, max_Y, max_Z, VERTEX, max_X, max_Y, max_Z, VERTEX, min_X, min_Y, max_Z, VERTEX, max_X, min_Y, max_Z, VERTEX, min_X, min_Y, min_Z, VERTEX, min_X, max_Y, min_Z, VERTEX, max_X, min_Y, min_Z, VERTEX, max_X, max_Y, min_Z, VERTEX, min_X, min_Y, max_Z, VERTEX, min_X, max_Y, max_Z, VERTEX, max_X, min_Y, max_Z, VERTEX, max_X, max_Y, max_Z, END ] p0 = '_0' + str(randint(0, 100)) p1 = '_1' + str(randint(0, 100)) p2 = '_2' + str(randint(0, 100)) p3 = '_3' + str(randint(0, 100)) cmd.pseudoatom (pos=[min_X, min_Y, min_Z], object=p0) cmd.pseudoatom (pos=[min_X, min_Y, max_Z], object=p1) cmd.pseudoatom (pos=[min_X, max_Y, min_Z], object=p2) cmd.pseudoatom (pos=[max_X, min_Y, min_Z], object=p3) cmd.distance(None, p0, p3) cmd.distance(None, p0, p2) cmd.distance(None, p0, p1) cmd.hide("nonbonded") boxName = "box_AABB_" + str(randint(0, 100)) cmd.load_cgo(boundingBox,boxName) return boxName
def map_new_apbs(name, selection='all', grid=0.5, buffer=10.0, state=1, preserve=0, exe='apbs', assign=-1, quiet=1): ''' DESCRIPTION Create electrostatic potential map with APBS. For more control over parameters and a graphical user interface I recommend to use the APBS Tools Plugin instead. In case of missing atoms or residues I recommend to remodel the input structure with modeller before calculating the electrostatic potential. If selection has no charges and radii, they will be automatically assigned with PyMOL (not with pdb2pqr). SEE ALSO apbs_surface, map_new (coulomb), APBS Tools Plugin ''' import tempfile, os, shutil, glob, subprocess from pymol.util import protein_assign_charges_and_radii from .modelling import add_missing_atoms selection = '(%s) and not solvent' % (selection) grid, buffer, state = float(grid), float(buffer), int(state) preserve, assign, quiet = int(preserve), int(assign), int(quiet) exe = cmd.exp_path(exe) # temporary directory tempdir = tempfile.mkdtemp() if not quiet: print ' Tempdir:', tempdir # filenames pqrfile = os.path.join(tempdir, 'mol.pqr') infile = os.path.join(tempdir, 'apbs.in') stem = os.path.join(tempdir, 'map') # temporary object tmpname = cmd.get_unused_name('mol' if preserve else '_') cmd.create(tmpname, selection, state, 1) # partial charges assign = [assign] if assign[0] == -1: # auto detect if selection has charges and radii cmd.iterate('first ((%s) and elem O)' % (tmpname), 'assign[0] = (elec_radius * partial_charge) == 0.0', space=locals()) if assign[0]: cmd.remove('hydro and model ' + tmpname) add_missing_atoms(tmpname, quiet=quiet) protein_assign_charges_and_radii(tmpname) elif not quiet: print ' Notice: using exsiting charges and radii' cmd.save(pqrfile, tmpname, 1, format='pqr', quiet=quiet) # grid dimensions extent = cmd.get_extent(tmpname) fglen = [(emax-emin + 2*buffer) for (emin, emax) in zip(*extent)] cglen = [(emax-emin + 4*buffer) for (emin, emax) in zip(*extent)] if not preserve: cmd.delete(tmpname) apbs_in = defaults_apbs_in.copy() apbs_in['fglen'] = '%f %f %f' % tuple(fglen) apbs_in['cglen'] = '%f %f %f' % tuple(cglen) apbs_in['srad'] = cmd.get('solvent_radius') apbs_in['write'] = 'pot dx "%s"' % (stem) # apbs input file def write_input_file(): f = open(infile, 'w') print >> f, ''' read mol pqr "%s" end elec mg-auto ''' % (pqrfile) for (k,v) in apbs_in.items(): if v is None: print >> f, k elif isinstance(v, list): for vi in v: print >> f, k, vi else: print >> f, k, v print >> f, ''' end quit ''' f.close() try: # apbs will fail if grid does not fit into memory # -> on failure repeat with larger grid spacing for _ in range(3): dime = [1 + max(64, n / grid) for n in fglen] apbs_in['dime'] = '%d %d %d' % tuple(dime) write_input_file() # run apbs r = subprocess.call([exe, infile], cwd=tempdir) if r == 0: break if r == -6: grid *= 2.0 if not quiet: print ' Warning: retry with grid =', grid continue print ' Error: apbs failed with code', r raise CmdException dx_list = glob.glob(stem + '*.dx') if len(dx_list) != 1: print ' Error: dx file missing' raise CmdException # load map cmd.load(dx_list[0], name, quiet=quiet) except OSError: print ' Error: Cannot execute "%s"' % (exe) raise CmdException finally: if not preserve: shutil.rmtree(tempdir) elif not quiet: print ' Notice: not deleting %s' % (tempdir)
def testLoadPLY(self): cmd.load(self.datafile("test_PHE_pentamer.ply.gz")) e = cmd.get_extent('test_PHE_pentamer') self.assertArrayEqual( e, [[-314.1, -303.6, -280.9], [1592.0, 1042.5, 868.0]], delta=1e-3)
def testLoadMSMSSurface(self): cmd.load(self.datafile('surface.face')) e = cmd.get_extent('surface') self.assertArrayEqual( e, [[28.003, 25.573, 12.883], [34.238, 31.434, 17.835]], delta=1e-3)
def testLoadVaspCHGCAR(self): cmd.load(self.datafile("vasp.CHGCAR"), 'vasp.CHGCAR') self.assertEqual(cmd.get_type('vasp.CHGCAR'), 'object:map') extend = cmd.get_extent('vasp.CHGCAR') self.assertArrayEqual(extend, [[0.0, 0.0, 0.0], [6.5, 6.5, 7.7]], delta=1e-2)
def reps(self,cleanup=0): rep_list = [ "lines","sticks","spheres","surface","mesh","dots","ribbon","cartoon" ] try: if not cleanup: cmd.disable() cmd.set("suspend_updates",1,quiet=1) cmd.load("$PYMOL_DATA/demo/pept.pdb","rep1") cmd.alter("rep1///1-5+8-13/","ss='S'") cmd.cartoon("auto") cmd.hide("everything","rep1") for a in range(2,9): cmd.create("rep%d"%a,"rep1") for x, y in enumerate(rep_list, 1): cmd.show(x, "rep%d" % y) cmd.reset() cmd.zoom("rep1",24) util.cbay("rep2") util.cbac("rep3") util.cbas("rep4") util.cbab("rep5") util.cbaw("rep6") util.cbay("rep8") cmd.set("suspend_updates",0,quiet=1) scale=0.5 for b in range(1,20): cmd.set("suspend_updates",0,quiet=1) cmd.refresh() cmd.set("suspend_updates",1,quiet=1) xt=-3.2 yt=1.6 for a in range(1,5): cmd.translate([xt*scale,yt*scale,0],object="rep%d"%a,camera=0) xt=xt+2 yt=-yt xt=-3.2 for a in range(5,9): cmd.translate([xt*scale,yt*scale,0],object="rep%d"%a,camera=0) xt=xt+2 for a in range(1,9): cmd.origin("rep%d"%a,object="rep%d"%a) cmd.mset("1") st = ' '.join(map(lambda x,y:"rotate angle=-3,object=rep%d,axis=%s;"%(x,y),list(range(1,9)), ['x','y','x','y','x','y','x','y'])) cmd.mdo(1,st) cmd.set("suspend_updates",0,quiet=1) cmd.mplay() cgo = [] axes = [[4.5,0.0,0.0],[0.0,3.0,0.0],[0.0,0.0,3.0]] c = 1 for a in rep_list: ext = cmd.get_extent("rep%d"%c) pos = [(ext[0][0]+ext[1][0])/2, (ext[0][1]+ext[1][1])/2+14, (ext[0][2]+ext[1][2])/2] c = c + 1 pos[0]=pos[0]-(measure_text(plain,a,axes)/2) wire_text(cgo,plain,pos,a,axes) cmd.set("cgo_line_width",1.5) cmd.set("auto_zoom",0) cmd.load_cgo(cgo,'reps') cmd.set("auto_zoom",1) else: cmd.delete("rep*") cmd.mset() cmd.mstop() except: traceback.print_exc()
def angle_between_domains(selection1, selection2, method='align', state1=STATE, state2=STATE, visualize=1, quiet=1): ''' DESCRIPTION Angle by which a molecular selection would be rotated when superposing on a selection2. Do not use for measuring angle between helices, since the alignment of the helices might involve a rotation around the helix axis, which will result in a larger angle compared to the angle between helix axes. USAGE angle_between_domains selection1, selection2 [, method ] ARGUMENTS selection1 = string: atom selection of first helix selection2 = string: atom selection of second helix method = string: alignment command like "align" or "super" {default: align} EXAMPLE fetch 3iplA 3iplB, async=0 select domain1, resi 1-391 select domain2, resi 392-475 align 3iplA and domain1, 3iplB and domain1 angle_between_domains 3iplA and domain2, 3iplB and domain2 SEE ALSO align, super, angle_between_helices ''' import math try: import numpy except ImportError: print ' Error: numpy not available' raise CmdException state1, state2 = int(state1), int(state2) visualize, quiet = int(visualize), int(quiet) if cmd.is_string(method): try: method = cmd.keyword[method][0] except KeyError: print 'no such method:', method raise CmdException mobile_tmp = get_unused_name('_') cmd.create(mobile_tmp, selection1, state1, 1, zoom=0) try: method(mobile=mobile_tmp, target=selection2, mobile_state=1, target_state=state2, quiet=quiet) mat = cmd.get_object_matrix(mobile_tmp) except: print ' Error: superposition with method "%s" failed' % (method.__name__) raise CmdException finally: cmd.delete(mobile_tmp) try: # Based on transformations.rotation_from_matrix # Copyright (c) 2006-2012, Christoph Gohlke R33 = [mat[i:i+3] for i in [0,4,8]] R33 = numpy.array(R33, float) # direction: unit eigenvector of R33 corresponding to eigenvalue of 1 w, W = numpy.linalg.eig(R33.T) i = w.real.argmax() direction = W[:, i].real # rotation angle depending on direction m = direction.argmax() i,j,k,l = [ [2,1,1,2], [0,2,0,2], [1,0,0,1]][m] cosa = (R33.trace() - 1.0) / 2.0 sina = (R33[i, j] + (cosa - 1.0) * direction[k] * direction[l]) / direction[m] angle = math.atan2(sina, cosa) angle = abs(math.degrees(angle)) except: print ' Error: rotation from matrix failed' raise CmdException if not quiet: try: # make this import optional to support running this script standalone from .querying import centerofmass, gyradius except (ValueError, ImportError): gyradius = None try: # PyMOL 1.7.1.6+ centerofmass = cmd.centerofmass except AttributeError: centerofmass = lambda s: cpv.scale(cpv.add(*cmd.get_extent(s)), 0.5) center1 = centerofmass(selection1) center2 = centerofmass(selection2) print ' Angle: %.2f deg, Displacement: %.2f angstrom' % (angle, cpv.distance(center1, center2)) if visualize: center1 = numpy.array(center1, float) center2 = numpy.array(center2, float) center = (center1 + center2) / 2.0 if gyradius is not None: rg = numpy.array(gyradius(selection1), float) else: rg = 10.0 h1 = numpy.cross(center2 - center1, direction) h2 = numpy.dot(R33, h1) h1 *= rg / cpv.length(h1) h2 *= rg / cpv.length(h2) for pos in [center1, center2, center1 + h1, center1 + h2]: cmd.pseudoatom(mobile_tmp, pos=list(pos), state=1) # measurement object for angle and displacement name = get_unused_name('measurement') cmd.distance(name, *['%s`%d' % (mobile_tmp, i) for i in [1,2]]) cmd.angle(name, *['%s`%d' % (mobile_tmp, i) for i in [3,1,4]]) # CGO arrow for axis of rotation visualize_orientation(direction, center1, rg, color='blue') cmd.delete(mobile_tmp) return angle
def drawBoundingBox(selection="(all)", padding=0.0, linewidth=2.0, r=1.0, g=1.0, b=1.0): """ DESCRIPTION Given selection, draw the bounding box around it. USAGE: drawBoundingBox [selection, [padding, [linewidth, [r, [g, b]]]]] PARAMETERS: selection, the selection to enboxen. :-) defaults to (all) padding, defaults to 0 linewidth, width of box lines defaults to 2.0 r, red color component, valid range is [0.0, 1.0] defaults to 1.0 g, green color component, valid range is [0.0, 1.0] defaults to 1.0 b, blue color component, valid range is [0.0, 1.0] defaults to 1.0 RETURNS string, the name of the CGO box NOTES * This function creates a randomly named CGO box that minimally spans the protein. The user can specify the width of the lines, the padding and also the color. """ ([minX, minY, minZ],[maxX, maxY, maxZ]) = cmd.get_extent(selection) print "Box dimensions (%.2f, %.2f, %.2f)" % (maxX-minX, maxY-minY, maxZ-minZ) minX = minX - float(padding) minY = minY - float(padding) minZ = minZ - float(padding) maxX = maxX + float(padding) maxY = maxY + float(padding) maxZ = maxZ + float(padding) if padding != 0: print "Box dimensions + padding (%.2f, %.2f, %.2f)" % (maxX-minX, maxY-minY, maxZ-minZ) boundingBox = [ LINEWIDTH, float(linewidth), BEGIN, LINES, COLOR, float(r), float(g), float(b), VERTEX, minX, minY, minZ, #1 VERTEX, minX, minY, maxZ, #2 VERTEX, minX, maxY, minZ, #3 VERTEX, minX, maxY, maxZ, #4 VERTEX, maxX, minY, minZ, #5 VERTEX, maxX, minY, maxZ, #6 VERTEX, maxX, maxY, minZ, #7 VERTEX, maxX, maxY, maxZ, #8 VERTEX, minX, minY, minZ, #1 VERTEX, maxX, minY, minZ, #5 VERTEX, minX, maxY, minZ, #3 VERTEX, maxX, maxY, minZ, #7 VERTEX, minX, maxY, maxZ, #4 VERTEX, maxX, maxY, maxZ, #8 VERTEX, minX, minY, maxZ, #2 VERTEX, maxX, minY, maxZ, #6 VERTEX, minX, minY, minZ, #1 VERTEX, minX, maxY, minZ, #3 VERTEX, maxX, minY, minZ, #5 VERTEX, maxX, maxY, minZ, #7 VERTEX, minX, minY, maxZ, #2 VERTEX, minX, maxY, maxZ, #4 VERTEX, maxX, minY, maxZ, #6 VERTEX, maxX, maxY, maxZ, #8 END ] boxName = "box_" + str(randint(0,10000)) while boxName in cmd.get_names(): boxName = "box_" + str(randint(0,10000)) cmd.load_cgo(boundingBox,boxName) return boxName
def map_new_apbs(name, selection='all', grid=0.5, buffer=10.0, state=1, preserve=0, exe='', assign=-1, focus='', quiet=1, _template=''): ''' DESCRIPTION Create electrostatic potential map with APBS. For more control over parameters and a graphical user interface I recommend to use the APBS Tools Plugin instead. In case of missing atoms or residues I recommend to remodel the input structure with modeller before calculating the electrostatic potential. If selection has no charges and radii, they will be automatically assigned with PyMOL (not with pdb2pqr). SEE ALSO apbs_surface, map_new (coulomb), APBS Tools Plugin ''' import tempfile, os, shutil, glob, subprocess from pymol.util import protein_assign_charges_and_radii from .modelling import add_missing_atoms selection = '(%s) and not solvent' % (selection) grid, buffer, state = float(grid), float(buffer), int(state) preserve, assign, quiet = int(preserve), int(assign), int(quiet) # path to apbs executable exe = validate_apbs_exe(exe) # temporary directory tempdir = tempfile.mkdtemp() if not quiet: print(' Tempdir:', tempdir) # filenames pqrfile = os.path.join(tempdir, 'mol.pqr') infile = os.path.join(tempdir, 'apbs.in') stem = os.path.join(tempdir, 'map') # temporary object tmpname = cmd.get_unused_name('mol' if preserve else '_') cmd.create(tmpname, selection, state, 1) # partial charges assign = [assign] if assign[0] == -1: # auto detect if selection has charges and radii cmd.iterate(tmpname, 'assign[0] *= (elec_radius * partial_charge) == 0.0', space=locals()) if assign[0]: cmd.remove('hydro and model ' + tmpname) add_missing_atoms(tmpname, quiet=quiet) protein_assign_charges_and_radii(tmpname) elif not quiet: print(' Notice: using exsiting charges and radii') cmd.save(pqrfile, tmpname, 1, format='pqr', quiet=quiet) # grid dimensions extent = cmd.get_extent(tmpname) extentfocus = cmd.get_extent(focus) if focus else extent fglen = [(emax - emin + 2 * buffer) for (emin, emax) in zip(*extentfocus)] cglen = [(emax - emin + 4 * buffer) for (emin, emax) in zip(*extent)] if not preserve: cmd.delete(tmpname) apbs_in = { 'pqrfile': pqrfile, 'fgcent': 'mol 1', 'fglen': '%f %f %f' % tuple(fglen), 'cglen': '%f %f %f' % tuple(cglen), 'srad': cmd.get('solvent_radius'), 'mapfile': stem, } if focus: apbs_in['fgcent'] = '%f %f %f' % tuple( (emax + emin) / 2.0 for (emin, emax) in zip(*extentfocus)) try: # apbs will fail if grid does not fit into memory # -> on failure repeat with larger grid spacing for _ in range(3): dime = [1 + max(64, n / grid) for n in fglen] apbs_in['dime'] = '%d %d %d' % tuple(dime) # apbs input file with open(infile, 'w') as f: f.write((_template or template_apbs_in).format(**apbs_in)) # run apbs r = subprocess.call([exe, infile], cwd=tempdir) if r == 0: break if r in (-6, -9): grid *= 2.0 if not quiet: print(' Warning: retry with grid =', grid) continue raise CmdException('apbs failed with code ' + str(r)) dx_list = glob.glob(stem + '*.dx') if len(dx_list) != 1: raise CmdException('dx file missing') # load map cmd.load(dx_list[0], name, quiet=quiet) except OSError: raise CmdException('Cannot execute "%s"' % (exe)) finally: if not preserve: shutil.rmtree(tempdir) elif not quiet: print(' Notice: not deleting %s' % (tempdir))
def testLoadPLY(self): cmd.load(self.datafile("test_PHE_pentamer.ply.gz")) e = cmd.get_extent('test_PHE_pentamer') self.assertArrayEqual(e, [[-314.1,-303.6,-280.9], [1592.0,1042.5, 868.0]], delta=1e-3)
def find_bounding_box(selection='(all)', state=-1, padding=0, dimension=True): """ Find a bounding box of the selection and return the value USAGE find_bounding_box selection find_bounding_box selection, state=state ARGUMENTS selection a selection-expression state a state index if positive int, 0 to all, or -1 to current padding padding width of the box dimension True to return dimension instead of coordinates RETURN dimension (dimension=True) (minx, miny, minz, widthx, widthy, widthz) coordinate (dimension=False) (p1, p2, p3, p4, p5, p6, p7, p8) p{n} := (x, y, z) FIGURE Individual p{n} indicate the vertex of the following box 5-----6 /| /| y z / 8-- / 7 | / 1-----2 / |/ |/ |/ -----x 4-----3 """ state = utils.int_to_state(state) ((minx, miny, minz), (maxx, maxy, maxz)) = cmd.get_extent( selection, state=state ) minx = minx - padding miny = miny - padding minz = minz - padding maxx = maxx + padding maxy = maxy + padding maxz = maxz + padding if dimension: return ( minx, miny, minz, maxx - minx, maxy - miny, maxz - minz, ) else: return ( (minx, maxy, minz), (maxx, maxy, minz), (maxx, miny, minz), (minx, miny, minz), (minx, maxy, maxz), (maxx, maxy, maxz), (maxx, miny, maxz), (minx, miny, maxz), )
def draw_IABB(selection): """ DESCRIPTION For a given selection, draw the Inertia Axes Aligned bounding box around it without padding. Code taken and modified from DrawBoundingBox.py. """ transformar(selection) ([minX, minY, minZ],[maxX, maxY, maxZ]) = cmd.get_extent(selection) print("The Inertia Axis Aligned Bounding Box (IABB) dimensions are (%.2f, %.2f, %.2f)" % (maxX-minX, maxY-minY, maxZ-minZ)) print("The Inertia Axis Aligned Bounding Box (IABB) volume is %.2f A3" % ((maxX-minX)*(maxY-minY)*(maxZ-minZ))) minX = minX minY = minY minZ = minZ maxX = maxX maxY = maxY maxZ = maxZ boundingBox = [ LINEWIDTH, float(2), BEGIN, LINES, COLOR, float(1), float(0), float(0), VERTEX, minX, minY, minZ, VERTEX, minX, minY, maxZ, VERTEX, minX, maxY, minZ, VERTEX, minX, maxY, maxZ, VERTEX, maxX, minY, minZ, VERTEX, maxX, minY, maxZ, VERTEX, maxX, maxY, minZ, VERTEX, maxX, maxY, maxZ, VERTEX, minX, minY, minZ, VERTEX, maxX, minY, minZ, VERTEX, minX, maxY, minZ, VERTEX, maxX, maxY, minZ, VERTEX, minX, maxY, maxZ, VERTEX, maxX, maxY, maxZ, VERTEX, minX, minY, maxZ, VERTEX, maxX, minY, maxZ, VERTEX, minX, minY, minZ, VERTEX, minX, maxY, minZ, VERTEX, maxX, minY, minZ, VERTEX, maxX, maxY, minZ, VERTEX, minX, minY, maxZ, VERTEX, minX, maxY, maxZ, VERTEX, maxX, minY, maxZ, VERTEX, maxX, maxY, maxZ, END ] p4 = '_4' + str(randint(0, 100)) p5 = '_5' + str(randint(0, 100)) p6 = '_6' + str(randint(0, 100)) p7 = '_7' + str(randint(0, 100)) cmd.pseudoatom (pos=[minX, minY, minZ], object=p4) cmd.pseudoatom (pos=[minX, minY, maxZ], object=p5) cmd.pseudoatom (pos=[minX, maxY, minZ], object=p6) cmd.pseudoatom (pos=[maxX, minY, minZ], object=p7) cmd.distance(None, p4, p7) cmd.distance(None, p4, p6) cmd.distance(None, p4, p5) cmd.hide("nonbonded") boxName = "box_IABB_" + str(randint(0, 100)) cmd.load_cgo(boundingBox,boxName) return boxName
def drawgridbox(selection="(all)", nx=10, ny=10, nz=10, padding=0.0, lw=2.0, r=1.0, g=1.0, b=1.0): """ DESCRIPTION Given selection, draw a grid box around it. USAGE: drawgridbox [selection, [nx, [ny, [nz, [padding, [lw, [r, [g, b]]]]]]]] PARAMETERS: selection, the selection to enboxen defaults to (all) nx, number of grids on axis X defaults to 10 ny, number of grids on axis Y defaults to 10 nz, number of grids on axis Z defaults to 10 padding, defaults to 0 lw, line width defaults to 2.0 r, red color component, valid range is [0.0, 1.0] defaults to 1.0 g, green color component, valid range is [0.0, 1.0] defaults to 1.0 b, blue color component, valid range is [0.0, 1.0] defaults to 1.0 RETURNS string, the name of the CGO box NOTES * This function creates a randomly named CGO grid box. The user can specify the number of grids on X/Y/Z axis, the width of the lines, the padding and also the color. """ ([minX, minY, minZ],[maxX, maxY, maxZ]) = cmd.get_extent(selection) print "Box dimensions (%.2f, %.2f, %.2f)" % (maxX-minX, maxY-minY, maxZ-minZ) minX = minX - float(padding) minY = minY - float(padding) minZ = minZ - float(padding) maxX = maxX + float(padding) maxY = maxY + float(padding) maxZ = maxZ + float(padding) nX=int(nx) nY=int(ny) nZ=int(nz) dX = (maxX-minX)/nX dY = (maxY-minY)/nY dZ = (maxZ-minZ)/nZ if padding != 0: print "Box dimensions + padding (%.2f, %.2f, %.2f)" % (maxX-minX, maxY-minY, maxZ-minZ) gridbox = [ LINEWIDTH, float(lw), BEGIN, LINES, COLOR, float(r), float(g), float(b), ] for i in range(nX): for j in range(nY): for k in range(nZ): dots= [ VERTEX, minX+i*dX, minY+j*dY, minZ+k*dZ, VERTEX, minX+i*dX, minY+j*dY, minZ+(k+1)*dZ, VERTEX, minX+i*dX, minY+(j+1)*dY, minZ+k*dZ, VERTEX, minX+i*dX, minY+(j+1)*dY, minZ+(k+1)*dZ, VERTEX, minX+(i+1)*dX, minY+j*dY, minZ+k*dZ, VERTEX, minX+(i+1)*dX, minY+j*dY, minZ+(k+1)*dZ, VERTEX, minX+(i+1)*dX, minY+(j+1)*dY, minZ+k*dZ, VERTEX, minX+(i+1)*dX, minY+(j+1)*dY, minZ+(k+1)*dZ, VERTEX, minX+i*dX, minY+j*dY, minZ+k*dZ, VERTEX, minX+(i+1)*dX, minY+j*dY, minZ+k*dZ, VERTEX, minX+i*dX, minY+(j+1)*dY, minZ+k*dZ, VERTEX, minX+(i+1)*dX, minY+(j+1)*dY, minZ+k*dZ, VERTEX, minX+i*dX, minY+(j+1)*dY, minZ+(k+1)*dZ, VERTEX, minX+(i+1)*dX, minY+(j+1)*dY, minZ+(k+1)*dZ, VERTEX, minX+i*dX, minY+j*dY, minZ+(k+1)*dZ, VERTEX, minX+(i+1)*dX, minY+j*dY, minZ+(k+1)*dZ, VERTEX, minX+i*dX, minY+j*dY, minZ+k*dZ, VERTEX, minX+i*dX, minY+(j+1)*dY, minZ+k*dZ, VERTEX, minX+(i+1)*dX, minY+j*dY, minZ+k*dZ, VERTEX, minX+(i+1)*dX, minY+(j+1)*dY, minZ+k*dZ, VERTEX, minX+i*dX, minY+j*dY, minZ+(k+1)*dZ, VERTEX, minX+i*dX, minY+(j+1)*dY, minZ+(k+1)*dZ, VERTEX, minX+(i+1)*dX, minY+j*dY, minZ+(k+1)*dZ, VERTEX, minX+(i+1)*dX, minY+(j+1)*dY, minZ+(k+1)*dZ, ] gridbox += dots gridbox.append(END) boxName = "gridbox_" + str(randint(0,10000)) while boxName in cmd.get_names(): boxName = "gridbox_" + str(randint(0,10000)) cmd.load_cgo(gridbox,boxName) return boxName
def testLoadMSMSSurface(self): cmd.load(self.datafile('surface.face')) e = cmd.get_extent('surface') self.assertArrayEqual(e, [ [28.003, 25.573, 12.883], [34.238, 31.434, 17.835]], delta=1e-3)
def map_new_apbs(name, selection='all', grid=0.5, buffer=10.0, state=1, preserve=0, exe='', assign=-1, focus='', quiet=1, _template=''): ''' DESCRIPTION Create electrostatic potential map with APBS. For more control over parameters and a graphical user interface I recommend to use the APBS Tools Plugin instead. In case of missing atoms or residues I recommend to remodel the input structure with modeller before calculating the electrostatic potential. If selection has no charges and radii, they will be automatically assigned with PyMOL (not with pdb2pqr). SEE ALSO apbs_surface, map_new (coulomb), APBS Tools Plugin ''' import tempfile, os, shutil, glob, subprocess from pymol.util import protein_assign_charges_and_radii from .modelling import add_missing_atoms selection = '(%s) and not solvent' % (selection) grid, buffer, state = float(grid), float(buffer), int(state) preserve, assign, quiet = int(preserve), int(assign), int(quiet) # path to apbs executable exe = validate_apbs_exe(exe) # temporary directory tempdir = tempfile.mkdtemp() if not quiet: print(' Tempdir:', tempdir) # filenames pqrfile = os.path.join(tempdir, 'mol.pqr') infile = os.path.join(tempdir, 'apbs.in') stem = os.path.join(tempdir, 'map') # temporary object tmpname = cmd.get_unused_name('mol' if preserve else '_') cmd.create(tmpname, selection, state, 1) # partial charges assign = [assign] if assign[0] == -1: # auto detect if selection has charges and radii cmd.iterate(tmpname, 'assign[0] *= (elec_radius * partial_charge) == 0.0', space=locals()) if assign[0]: cmd.remove('hydro and model ' + tmpname) add_missing_atoms(tmpname, quiet=quiet) protein_assign_charges_and_radii(tmpname) elif not quiet: print(' Notice: using exsiting charges and radii') cmd.save(pqrfile, tmpname, 1, format='pqr', quiet=quiet) # grid dimensions extent = cmd.get_extent(tmpname) extentfocus = cmd.get_extent(focus) if focus else extent fglen = [(emax-emin + 2*buffer) for (emin, emax) in zip(*extentfocus)] cglen = [(emax-emin + 4*buffer) for (emin, emax) in zip(*extent)] if not preserve: cmd.delete(tmpname) apbs_in = { 'pqrfile': pqrfile, 'fgcent': 'mol 1', 'fglen': '%f %f %f' % tuple(fglen), 'cglen': '%f %f %f' % tuple(cglen), 'srad': cmd.get('solvent_radius'), 'mapfile': stem, } if focus: apbs_in['fgcent'] = '%f %f %f' % tuple((emax + emin) / 2.0 for (emin, emax) in zip(*extentfocus)) try: # apbs will fail if grid does not fit into memory # -> on failure repeat with larger grid spacing for _ in range(3): dime = [1 + max(64, n / grid) for n in fglen] apbs_in['dime'] = '%d %d %d' % tuple(dime) # apbs input file with open(infile, 'w') as f: f.write((_template or template_apbs_in).format(**apbs_in)) # run apbs r = subprocess.call([exe, infile], cwd=tempdir) if r == 0: break if r in (-6, -9): grid *= 2.0 if not quiet: print(' Warning: retry with grid =', grid) continue raise CmdException('apbs failed with code ' + str(r)) dx_list = glob.glob(stem + '*.dx') if len(dx_list) != 1: raise CmdException('dx file missing') # load map cmd.load(dx_list[0], name, quiet=quiet) except OSError: raise CmdException('Cannot execute "%s"' % (exe)) finally: if not preserve: shutil.rmtree(tempdir) elif not quiet: print(' Notice: not deleting %s' % (tempdir))
def drawBoundingBox(selection="(all)", padding=0.0, linewidth=2.0, r=1.0, g=1.0, b=1.0): """ DESCRIPTION Given selection, draw the bounding box around it. USAGE: drawBoundingBox [selection, [padding, [linewidth, [r, [g, b]]]]] PARAMETERS: selection, the selection to enboxen. :-) defaults to (all) padding, defaults to 0 linewidth, width of box lines defaults to 2.0 r, red color component, valid range is [0.0, 1.0] defaults to 1.0 g, green color component, valid range is [0.0, 1.0] defaults to 1.0 b, blue color component, valid range is [0.0, 1.0] defaults to 1.0 RETURNS string, the name of the CGO box NOTES * This function creates a randomly named CGO box that minimally spans the protein. The user can specify the width of the lines, the padding and also the color. """ ([minX, minY, minZ], [maxX, maxY, maxZ]) = cmd.get_extent(selection) print "Box dimensions (%.2f, %.2f, %.2f)" % (maxX - minX, maxY - minY, maxZ - minZ) minX = minX - float(padding) minY = minY - float(padding) minZ = minZ - float(padding) maxX = maxX + float(padding) maxY = maxY + float(padding) maxZ = maxZ + float(padding) if padding != 0: print "Box dimensions + padding (%.2f, %.2f, %.2f)" % ( maxX - minX, maxY - minY, maxZ - minZ) boundingBox = [ LINEWIDTH, float(linewidth), BEGIN, LINES, COLOR, float(r), float(g), float(b), VERTEX, minX, minY, minZ, #1 VERTEX, minX, minY, maxZ, #2 VERTEX, minX, maxY, minZ, #3 VERTEX, minX, maxY, maxZ, #4 VERTEX, maxX, minY, minZ, #5 VERTEX, maxX, minY, maxZ, #6 VERTEX, maxX, maxY, minZ, #7 VERTEX, maxX, maxY, maxZ, #8 VERTEX, minX, minY, minZ, #1 VERTEX, maxX, minY, minZ, #5 VERTEX, minX, maxY, minZ, #3 VERTEX, maxX, maxY, minZ, #7 VERTEX, minX, maxY, maxZ, #4 VERTEX, maxX, maxY, maxZ, #8 VERTEX, minX, minY, maxZ, #2 VERTEX, maxX, minY, maxZ, #6 VERTEX, minX, minY, minZ, #1 VERTEX, minX, maxY, minZ, #3 VERTEX, maxX, minY, minZ, #5 VERTEX, maxX, maxY, minZ, #7 VERTEX, minX, minY, maxZ, #2 VERTEX, minX, maxY, maxZ, #4 VERTEX, maxX, minY, maxZ, #6 VERTEX, maxX, maxY, maxZ, #8 END ] boxName = "box_" + str(randint(0, 10000)) while boxName in cmd.get_names(): boxName = "box_" + str(randint(0, 10000)) cmd.load_cgo(boundingBox, boxName) return boxName