def slowpacking(pdb): "Derive mean packing density of pdb as pd.Series." cmd.delete('all') cmd.load(pdb) cmd.remove('solvent') # Only heavy atoms cmd.remove('hydro') # Compute SAS per atom cmd.set('dot_solvent') cmd.get_area('all', load_b=1) N = float(cmd.select('interior', 'b = 0')) internal_coords = [at.coord for at in cmd.get_model('interior').atom]#[1:50] all_coords = [at.coord for at in cmd.get_model('all').atom]#[1:50] # Count counts = pd.Series(0, index=RADS) for a, b in product(internal_coords, all_coords): es = euclid_step(a, b) if es is not None: counts.loc[es] += 1 counts = counts.cumsum() # Mean per center atom meancounts = counts / N # Normalize to density volumina = pd.Series(4 / 3.0 * sp.pi * (RADS ** 3), index=RADS) density = meancounts / volumina # Correct for center density -= 1 / (4/3 * sp.pi * RADS ** 3) # Results counts.index = ["{}_correctcount".format(i) for i in counts.index] density.index = ["{}_density".format(i) for i in density.index] return pd.concat(([counts, density]))
def homogenizechains(sel1,sel2): cmd.remove("hydro") cmd.remove("resn HOH") cmd.remove("(HET and not resn MSE+CSW)") a = cmd.get_model("%s and name ca"%(sel1)) b = cmd.get_model("%s and name ca"%(sel2)) sa = "".join([name1[x.resn] for x in a.atom]) sb = "".join([name1[x.resn] for x in b.atom]) if sa==sb: return True ra = [myint(x.resi) for x in a.atom] rb = [myint(x.resi) for x in b.atom] # if max(ra) - min(ra) + 1 != len(ra): print "missing residue numbers",max(ra),min(ra),len(ra) # if max(rb) - min(rb) + 1 != len(rb): print "missing residue numbers",rb mla,mua,mlb,mub = lcs(sa,sb) bla,bua,blb,bub = lcs(sa[ :mla],sb[ :mlb]) ala,aua,alb,aub = lcs(sa[mua+1:],sb[mub+1:]) ra = ra[mla:(mua+1)] rb = rb[mlb:(mub+1)] if len(ra[bla:(bua+1)]) > 10: ra = ra[bla:(bua+1)] + ra[mla:(mua+1)] + ra[ala:(aua+1)] rb = rb[blb:(bub+1)] + rb[mlb:(mub+1)] + rb[alb:(aub+1)] if len(ra[ala:(aua+1)]) > 10: ra += ra[ala:(aua+1)] rb += rb[alb:(aub+1)] for c,i in getres("%s"%(sel1)): if not i in ra: cmd.remove("%s and resi %i"%(sel1,i)) for c,i in getres("%s"%(sel2)): if not i in rb: cmd.remove("%s and resi %i"%(sel2,i)) return False
def revert(self): v = cmd.get_view() cmd.remove(self.rdes.obj+" and not chain A") m = cmd.get_model(self.rdes.obj) n = self.oldcrd if not n: cmd.create("tmp12345",self.rnat.sel()) cmd.align("tmp12345",self.rdes.sel()) n = cmd.get_model("tmp12345").atom cmd.delete("tmp12345") di,ni = 0,0 while m.atom[di].resi != str(self.rdes.resi): di += 1 dj = di while m.atom[dj].resi == str(self.rdes.resi): dj += 1 if self.oldcrd: self.oldcrd = None else: self.oldcrd = m.atom[di:dj] m.atom = m.atom[:di] + n + m.atom[dj:] for i in range(di,di+len(n)): m.atom[i].resi = str(self.rdes.resi) m.atom[i].chain = str(self.rdes.chain) cmd.load_model(m,self.rdes.obj,1) cmd.save("tmp.pdb",self.rdes.obj) cmd.delete(self.rdes.obj) cmd.load("tmp.pdb",self.rdes.obj,1) cmd.show('car',self.rdes.obj) cmd.show('lines') redopent(self.rdes.obj) cmd.set_view(v) print "revert removing "+"".join(self.aas)+" from aas!" self.aas = [getaa('A',self.rdes.resi,self.manager.d.obj)]
def bond_zns(sel): cmd.unbond(sel + " and resn ZNS", sel + " and not resn ZNS") # for c, i in getres("resn ZNS"): # cmd.bond(sel+" and chain %s and resi %i and name ZN1"%(c, i), # sel+" and chain %s and resi %i and name S*"%(c, i)) for c, i in getres("name ZN1"): cmd.bond( sel + " and chain %s and resi %i and name ZN1" % (c, i), sel + " and chain %s and resi %i and name S*, N2, N4" % (c, i), ) allsg = cmd.get_model(sel + " and (resn CYS and (name CB))").atom allsf = cmd.get_model(sel + " and (resn ZHC and (name S*, C1, C4))").atom while allsg: closest = [9e9, None, None] for sga in allsg: for sfa in allsf: sg = Vec(sga.coord) sf = Vec(sfa.coord) if (sg - sf).length() < closest[0]: closest = [(sg - sf).length(), sga, sfa] sga, sfa = closest[1:] allsg.remove(sga) allsf.remove(sfa) if closest[0] > 10.0: break cmd.bond( sel + " and resi %s and chain %s and name %s" % (sga.resi, sga.chain, sga.name), sel + " and resi %s and chain %s and name %s" % (sfa.resi, sfa.chain, sfa.name), )
def test_sort(self): cmd.pseudoatom('m1', name='PS2') cmd.pseudoatom('m1', name='PS1') v = [a.name for a in cmd.get_model().atom] self.assertEqual(['PS2', 'PS1'], v) cmd.sort() v = [a.name for a in cmd.get_model().atom] self.assertEqual(['PS1', 'PS2'], v)
def test_sort(self): cmd.pseudoatom('m1', name='PS2') cmd.pseudoatom('m1', name='PS1') cmd.alter('all', 'name = name_list.pop()', space={'name_list': ['PS1', 'PS2']}) v = [a.name for a in cmd.get_model().atom] self.assertEqual(['PS2', 'PS1'], v) cmd.sort() v = [a.name for a in cmd.get_model().atom] self.assertEqual(['PS1', 'PS2'], v)
def mirror(sel, nname="mirror", crd=0): cmd.delete(nname) a = [Vec(x.coord) for x in cmd.get_model(sel).atom] # print max(d), argmax(d), cmd.get_model("179L").atom[argmax(d)].resi # print min(d) cmd.create(nname, sel) m = cmd.get_model(sel) for j in range(len(m.atom)): m.atom[j].coord[crd] *= -1 cmd.load_model(m, nname, 1)
def testPSEBulkExportImport(self, pse_export_version, pse_binary_dump): with testing.mktemp('.pse') as filename: cmd.load(self.datafile("1oky-frag.pdb")) m1 = cmd.get_model() cmd.set("pse_export_version", pse_export_version) cmd.set("pse_binary_dump", pse_binary_dump) cmd.save(filename) cmd.reinitialize() cmd.load(filename) m2 = cmd.get_model() self.assertModelsAreSame(m1, m2)
def testMol2FormalChargeAssignment(self): cmd.fragment('lys') cmd.fragment('lys') with testing.mktemp('.mol2') as filename: cmd.save(filename, state=0) cmd.delete('*') cmd.load(filename) self.assertEqual(cmd.get_model('name NZ', state=1).atom[0].formal_charge, 1) self.assertEqual(cmd.get_model('name NZ', state=2).atom[0].formal_charge, 1)
def dimeraxis(sel1, sel2, nsamp=100): m1 = cmd.get_model(sel1) m2 = cmd.get_model(sel2) a, wtot = Vec(0), 0 for i in range(int(nsamp)): u = dimeraxis1(m1, m2) # print u a += u wtot += u.length() a /= wtot return a
def testClean(self): cmd.fragment('his') xyz1 = cmd.get_model('his').get_coord_list() energy = cmd.clean('his') xyz2 = cmd.get_model('his').get_coord_list() self.assertEqual(len(xyz1), len(xyz2)) # coords have changed self.assertNotEqual(xyz1, xyz2) # energy for HIS = 32.73887 self.assertTrue(30.0 < energy < 35.0)
def test_cycle_valence(self): cmd.fragment('gly') cmd.edit('ID 0', 'ID 1') cmd.cycle_valence() self.assertEqual(4, cmd.get_model('pkbond').bond[0].order) cmd.cycle_valence() self.assertEqual(2, cmd.get_model('pkbond').bond[0].order) cmd.cycle_valence() self.assertEqual(3, cmd.get_model('pkbond').bond[0].order)
def ens_rmsd(ens_selection, ref_selection, log_name = None): ''' DESCRIPTION Prints RMSD per structure in ensemble w.r.t. a reference structure USAGE ens_rmsd ensemble_selection, reference_selection, name, log, ARGUMENTS log = name of log file verbose = calculates structure by structure RMSD for ensemble w.r.t. a single reference structure EXAMPLE ens_rmsd ensemble_selection, reference_selection, name = 'rmsd', log 'ens.log' ''' if log_name == None: log = LogWriter(sys.stdout, 'log.txt') else: log = LogWriter(sys.stdout, log_name+'.txt') # initialize arrays ens_selection = ens_selection + ' and not resn hoh' ref_selection = ref_selection + ' and not resn hoh' rmsd_states = [] mean_coords = None number_models = cmd.count_states(ens_selection) # get models, mean coords print >> log, '\nRMSD by state' print >> log, '\n State | RMSD' for i in range(number_models): ens_coords = cmd.get_model(ens_selection,state=i+1).get_coord_list() ref_coords = cmd.get_model(ref_selection,state=1).get_coord_list() atom_sqr_dev = [] for atom in xrange(len(ref_coords)): x = ref_coords[atom] y = ens_coords[atom] atom_sqr_dev.append(distance(x,y)**2) rmsd = math.sqrt(sum(atom_sqr_dev) / len(atom_sqr_dev)) rmsd_states.append(rmsd) print >> log, ' %5d | %5.3f '%(i+1, rmsd) print_array_stats(array = rmsd_states, log = log) print '\nRMSD all states : %5.3f '%(cmd.rms(ens_selection, ref_selection))
def inversion(sel, nname="inv"): cmd.delete(nname) a = [Vec(x.coord) for x in cmd.get_model(sel).atom] # print max(d), argmax(d), cmd.get_model("179L").atom[argmax(d)].resi # print min(d) cmd.create(nname, sel) m = cmd.get_model(sel) for j in range(len(m.atom)): m.atom[j].coord[0] *= -1 m.atom[j].coord[1] *= -1 m.atom[j].coord[2] *= -1 cmd.load_model(m, nname, 1)
def testMMTFExportSele(self): cmd.fab('ACDE') with testing.mktemp('.mmtf') as filename: cmd.save(filename, 'resn CYS+ASP') cmd.delete('*') cmd.load(filename) self.assertEqual(cmd.count_atoms(), 23) self.assertEqual(cmd.count_atoms('bound_to ASP/CG'), 3) self.assertEqual(cmd.get_model('ASP/CG ASP/OD1').bond[0].order, 2) self.assertEqual(cmd.get_model('ASP/CG ASP/OD2').bond[0].order, 1) self.assertEqual(cmd.get_model('CYS/C ASP/N').bond[0].order, 1)
def meancoords(sel1, sel2, n="mix", w=0.5): cmd.delete(n) a = [Vec(x.coord) for x in cmd.get_model(sel1).atom] b = [Vec(x.coord) for x in cmd.get_model(sel2).atom] d = [(a[i] - b[i]).length() for i in range(len(a))] # print max(d), argmax(d), cmd.get_model("179L").atom[argmax(d)].resi # print min(d) cmd.create(n, sel1) m = cmd.get_model(sel1) for j in range(len(m.atom)): m.atom[j].coord[0] = w * b[j].x + (1.0 - w) * a[j].x m.atom[j].coord[1] = w * b[j].y + (1.0 - w) * a[j].y m.atom[j].coord[2] = w * b[j].z + (1.0 - w) * a[j].z cmd.load_model(m, n, 1)
def goQM(selside="sele",selbb="",qmprogram="MOPAC2012",method="Cheap", calctype="Optimization",dielectric="-1", charge=0,multiplicity=1, alphacut=True,dryrun=False): lens=[] states=[] q1=[] side=False print selside ##################3 if selside and not " " in selside: side=True m=cmd.get_model(selside) q1.append(m) lens.append(len(q1[0].atom)) states.append(1) bb=selbb.split(",") for i in bb: if i=="": continue q1.append(cmd.get_model(i)) lens.append(len(q1[-1].atom)) states.append(1) if side: bb.insert(0,selside) proc = Popen("goqm", shell=True, stdin=PIPE,stdout=PIPE) options=json.dumps({"SelNames":bb,"AtomsPerSel":lens,"StatesPerSel":states,"IntOptions":[[int(charge),int(multiplicity)]],"FloatOptions":[[float(dielectric)]],"StringOptions":[[qmprogram,method, calctype]],"BoolOptions":[[side,alphacut,dryrun]]}) print options ###### print "side", side proc.stdin.write(options+"\n") for j in q1: for i in j.atom: atom,coords=gochem.Atom2gcRef(i) proc.stdin.write(atom+"\n") proc.stdin.write(coords+"\n") proc.stdin.close() if proc.wait() != 0: print "There were some errors" if dryrun: return #in a dry run there is nothing to receive. The input files should be in the current directory. info=gochem.get_info(proc) energy=info["Energies"][0] print "Final energy: ", energy, " kcal/mol" if calctype=="Optimization": for k,v in enumerate(q1): if k==0 and side: exclude=["CA","HA","HA2","HA3", "O","N","H","C"] idexclude=[] else: idexclude=[v.atom[0].resi,v.atom[-1].resi] exclude=["CTZ","NTZ","HCZ","HNZ"] mod=gochem.get_coords(proc,v,exclude,idexclude,False,info,k) cmd.load_model(mod,bb[k]+"_H",discrete=1,zoom=1)
def axisofrot(sel1, sel2, sel3, nsamp=100): m1 = cmd.get_model(sel1).atom[0] m2 = cmd.get_model(sel2).atom[0] m3 = cmd.get_model(sel3).atom[0] m1 = Vec(m1.coord) m2 = Vec(m2.coord) m3 = Vec(m3.coord) return ((m2 - m1).cross(m2 - m3)).normalized() # a, wtot = Vec(0), 0 # for i in range(int(nsamp)): # u = axisofrot1(m1, m2, m3) # a += u # wtot += u.length() # a /= wtot return a
def getres(sele): try: r = list(sets.Set([(x.chain, int(x.resi)) for x in cmd.get_model(sele).atom])) r.sort() return r except: return []
def getchain(sele): try: c = list(sets.Set([x.chain for x in cmd.get_model(sele).atom])) c.sort() return c except: return []
def pir(selection='(all)', wrap=70): ''' DESCRIPTION Print sequence in PIR format SEE ALSO fasta ''' from . import one_letter from chempy import cpv wrap = int(wrap) for obj in cmd.get_object_list('(' + selection + ')'): seq = [] prev_coord = None model = cmd.get_model('/%s////CA and guide and (%s)' % (obj, selection)) for atom in model.atom: if prev_coord is not None and cpv.distance(atom.coord, prev_coord) > 4.0: seq.append('/\n') prev_coord = atom.coord seq.append(one_letter.get(atom.resn, 'X')) seq.append('*') print('>P1;%s' % (obj)) print('structure:%s:%s:%s:%s:%s::::' % (obj, model.atom[0].resi,model.atom[0].chain, model.atom[-1].resi,model.atom[-1].chain)) if wrap < 1: print(''.join(seq)) continue for i in range(0, len(seq), wrap): print(''.join(seq[i:i+wrap]))
def gyradius(selection='(all)', state=-1, quiet=1): ''' DESCRIPTION Radius of gyration Based on: http://pymolwiki.org/index.php/Radius_of_gyration SEE ALSO centerofmass ''' from chempy import cpv state, quiet = int(state), int(quiet) if state < 0: states = [cmd.get_state()] elif state == 0: states = list(range(1, cmd.count_states(selection)+1)) else: states = [state] rg_sq_list = [] for state in states: model = cmd.get_model(selection, state) x = [i.coord for i in model.atom] mass = [i.get_mass() * i.q for i in model.atom if i.q > 0] xm = [cpv.scale(v,m) for v,m in zip(x,mass)] tmass = sum(mass) rr = sum(cpv.dot_product(v,vm) for v,vm in zip(x,xm)) mm = sum((sum(i)/tmass)**2 for i in zip(*xm)) rg_sq_list.append(rr/tmass - mm) rg = (sum(rg_sq_list)/len(rg_sq_list))**0.5 if not quiet: print(' Radius of gyration: %.2f' % (rg)) return rg
def get_com(selection,state=1,mass=None, quiet=1): """ DESCRIPTION Calculates the center of mass Author: Sean Law Michigan State University slaw (at) msu . edu """ quiet = int(quiet) totmass=0.0 if mass!=None and not quiet: print "Calculating mass-weighted COM" state=int(state) model = cmd.get_model(selection,state) x,y,z=0,0,0 for a in model.atom: if (mass != None): m = a.get_mass() x+= a.coord[0]*m y+= a.coord[1]*m z+= a.coord[2]*m totmass+=m else: x+= a.coord[0] y+= a.coord[1] z+= a.coord[2] if (mass!=None): return x/totmass, y/totmass, z/totmass else: return x/len(model.atom), y/len(model.atom), z/len(model.atom)
def useTempRadii(sel="all"): for a in cmd.get_model(sel).atom: bfac = a.b if bfac >= 3: print "shrik radius" bfac <- 0.1 cmd.alter("%s and resi %s and name %s"%(sel,a.resi,a.name),"vdw=%f"%(bfac))
def testMAEchempy(self, molfilename): cmd.load(self.datafile(molfilename), 'test', object_props='*', atom_props='*') objs = cmd.get_object_list() for obj in objs: idxToVal = {} natoms = cmd.count_atoms(obj) for i in range(natoms): idxToVal[i+1] = i*10 cmd.set_atom_property('test_prop', i*10, "index %d and %s" % (i+1, obj)) model = cmd.get_model(obj) # test to make sure the properties that exist are the same prop_list= cmd.get_property_list(obj) mol_prop_list = [x[0] for x in model.molecule_properties] self.assertEqual(set(prop_list), set(mol_prop_list)) # need to test whether the values are the same for prop, val in model.molecule_properties: propval = cmd.get_property(prop, obj) self.assertEqual(propval, val) self.assertEqual(natoms, len(model.atom)) # need to test values of all atom properties, including the ones that were set above stored.prop_lookup = {} cmd.iterate(obj, "stored.prop_lookup[index-1] = properties.all") idx = 0 for at in model.atom: for prop in at.atom_properties.keys(): val = at.atom_properties[prop] self.assertEqual(val, stored.prop_lookup[idx][prop]) idx += 1
def computecenter(self,selection="(all)"): gcentx=0 gcenty=0 gcentz=0 gcnt=0 for selstr in selection.split(): sel=cmd.get_model(selstr) centx=0 centy=0 centz=0 cnt=len(sel.atom) for a in sel.atom: centx+=a.coord[0] centy+=a.coord[1] centz+=a.coord[2] centx/=cnt centy/=cnt centz/=cnt # fmttext="%lf\t%lf\t%lf\n" % (centx,centy,centz) # print centx,centy,centz gcentx+=centx gcenty+=centy gcentz+=centz gcnt+=1 gcentx/=gcnt gcenty/=gcnt gcentz/=gcnt return (gcentx,gcenty,gcentz)
def scaleCoords(amount=1.0,sel='all'): m = cmd.get_model(sel) for a in m.atom: if a.index == 80: delta = [float(k)*(amount-1) for k in a.coord] print amount, a.coord, delta cmd.translate( [ delta[0], delta[1], delta[2] ] , "index "+`a.index`)
def useOccRadii(sel="all"): for a in cmd.get_model(sel).atom: q = a.q if q >= 3: print "shrik radius" q <- 0.1 cmd.alter("%s and resi %s and name %s"%(sel,a.resi,a.name),"vdw=%f"%(q))
def amber99(selection="(all)",quiet=0,_self=cmd): cmd=_self result = 1 # first, set all parameters to zero cmd.alter(selection,"partial_charge=0") cmd.alter(selection,"elec_radius=0.0") cmd.alter(selection,"text_type=''") # next, flag all atoms so that we'll be able to detect what we miss cmd.flag(23,selection,'set') # get the amber99 dictionary if not hasattr(champ,'amber99_dict'): from chempy.champ.amber99 import amber99_dict champ.amber99_dict = amber99_dict # iterate through the residue dictionary matching each residue based on chemistry # and generating the expressions for reassigning formal charges alter_list = [] for resn in champ.amber99_dict.keys(): if cmd.select(tmp_sele1,"(%s) and resn %s"%(selection,resn))>0: entry = champ.amber99_dict[resn] for rule in entry: model = cmd.get_model(tmp_sele1) ch = Champ() model_pat = ch.insert_model(model) ch.pattern_detect_chirality(model_pat) assn_pat = ch.insert_pattern_string(rule[0]) ch.pattern_clear_tags(model_pat) if ch.match_1v1_n(assn_pat,model_pat,10000,2)>0: result = ch.pattern_get_ext_indices_with_tags(model_pat) for atom_tag in result[0]: # just iterate over atom tags if len(atom_tag[1])==1: # one and only one match tag = atom_tag[1][0] prop_list = rule[1][tag] # the following expression both changes the formal charge and resets flag 23 alter_list.append([atom_tag[0], "name='''%s''';text_type='''%s''';partial_charge=%f;elec_radius=%f;flags=flags&-8388609"%prop_list]) # now evaluate all of these expressions efficiently en-masse cmd.alter_list(selection,alter_list) # see if we missed any atoms missed_count = cmd.count_atoms("("+selection+") and flag 23") if missed_count>0: if not quiet: # looks like we did, so alter the user print " WARNING: %d atoms did not have properties assigned"%missed_count result = 0 # remove the temporary selection we used to select appropriate residues cmd.delete(tmp_sele1) return result
def gyration_by_selection(self, selection): global anal_id anal_id += 1 model = cmd.get_model(selection,1) get_COG_by_selection(self,selection) draw_COG(self,selection) calculate_tensor_by_selection(self, selection)
def multiSliceAsR(sel='all', fname='/tmp/points.R', a=0, b=0, c=1, interval=1, varname='slice'): origin = Plane(float(a), float(b), float(c), 0) mn = 99999999 mx = -99999999 for atom in cmd.get_model(sel).atom: d = origin.signedDisTo(atom) if mn > d: mn = d if mx < d: mx = d o = open(fname, 'w') o.write("%(varname)s = list() \n" % vars()) for ii, d in enumerate(range(mn, mx, interval)): print 'slice', ii, d variter = varname + "[[%i]]" % (ii + 1) o.write(projectionAsRString(sel, a, b, c, d, varname=variter)) o.close()
def get_sasa(params, points, const, selection='all', probe=1.4): """ Returns the solvent-accessible-surface area and empirical solvatation term Parameters ---------- probe : float. Radius of the solvent """ # compute the area each point represents areas = [] energies = [] model = cmd.get_model(selection) coord = np.array(model.get_coord_list()) for key, values in params.items(): # scale the points to the correct radius radius = values[0] + probe points_scaled = coord[key] + points * radius # get all the neighbors of the i residue neighbors = get_neighbors(coord, probe, key, params) # compute the distance between points and neighbors d_matrix = cdist(points_scaled, coord[neighbors], metric='euclidean') # create a matrix and store the vdW radii for each neighbor nb_matrix = np.zeros((len(points), len(neighbors))) for nb_i, nb in enumerate(neighbors): nb_matrix[:, nb_i] = values[0] # compute the number of buried points, we have to be carefull # because we have counted how many times a point is buried # and we only need how many points are buried buried = np.sum(np.sum(d_matrix < nb_matrix + probe, axis=1) > 0) exposed = len(points) - buried area_per_atom = const * exposed * radius**2 energy_per_atom = area_per_atom * values[1] areas.append(area_per_atom) energies.append(energy_per_atom) return sum(energies), sum(areas)
def centroid(selection='all', center=0, quiet=1): model = cmd.get_model(selection) nAtom = len(model.atom) centroid = cpv.get_null() for a in model.atom: centroid = cpv.add(centroid, a.coord) centroid = cpv.scale(centroid, 1. / nAtom) if not int(quiet): print(' centroid: [%8.3f,%8.3f,%8.3f]' % tuple(centroid)) if int(center): cmd.alter_state(1, selection, "(x,y,z)=sub((x,y,z), centroid)", space={ 'centroid': centroid, 'sub': cpv.sub }) return centroid
def COM(selection='all', center=0, quiet=1): model = cmd.get_model(selection) nAtom = len(model.atom) COM = cpv.get_null() for a in model.atom: COM = cpv.add(COM, a.coord) COM = cpv.scale(COM, 1. / nAtom) if not int(quiet): print ' COM: [%8.3f,%8.3f,%8.3f]' % tuple(COM) if int(center): cmd.alter_state(1, selection, "(x,y,z)=sub((x,y,z), COM)", space={ 'COM': COM, 'sub': cpv.sub }) return COM
def get_atoms_in_box(self, polymer=None): if not polymer: polymer = cmd.get_model('polymer') x_min = self.x_axis[0] x_max = self.x_axis[1] y_min = self.y_axis[0] y_max = self.y_axis[1] z_min = self.z_axis[0] z_max = self.z_axis[1] atoms = [ x for x in polymer.atom if x_min <= x.coord[0] <= x_max and y_min <= x.coord[1] <= y_max and z_min <= x.coord[2] <= z_max ] atom_ids = [y.id for y in atoms] cmd.select("binding_atoms", "ID %d" % atom_ids[0]) for atom_id in atom_ids: cmd.select("binding_atoms", "binding_atoms or ID %d" % atom_id) self.atoms_in_box = atoms
def _add_vis(self, mymol, row, l_cgo): if (self.is_only_water_access and (not "WAT" in [row.rnm1, row.rnm2])): return None mycolind = int((np.sign(row.sdiff) + 1) / 2) if self.df_rgn_seg_res_bb is None: cmd.select( "sel1", "/%s/%s and i. %d and %s and not h." % (mymol, row.seg1, row.res1, self.bb2selstr[row.bb1])) cmd.select( "sel2", "/%s/%s and i. %d and %s and not h." % (mymol, row.seg2, row.res2, self.bb2selstr[row.bb2])) else: if "bb" in self.df_rgn_seg_res_bb: df_unique_rgnsegbb = self.df_rgn_seg_res_bb[[ "seg", "bb" ]].drop_duplicates() for myrgn in [row.rgn1, row.rgn2]: selstr = "/%s and (" % (mymol) for sindex, srow in df_unique_rgnsegbb.iterrows(): df_sel = self.df_rgn_seg_res_bb.query( "rgn == '%s' and seg == '%s' and bb == %d" % (myrgn, srow.seg, srow.bb)) if df_sel.shape[0] > 0: selstr += "(c. %s and i. %s and %s) or" % ( srow.seg, "+".join( [str(x) for x in df_sel.res.values[0]]), srow.bb) selstr = selstr[:-3] + ")" cmd.select("%s" % myrgn, selstr) else: df_unique_rgnsegbb = self.df_rgn_seg_res_bb[[ "seg" ]].drop_duplicates() for myrgn in [row.rgn1, row.rgn2]: selstr = "/%s and (" % (mymol) for sindex, srow in df_unique_rgnsegbb.iterrows(): df_sel = self.df_rgn_seg_res_bb.query( "rgn == '%s' and seg == '%s'" % (row.rgn1, srow.seg)) if df_sel.shape[0] > 0: selstr += "(c. %s and i. %s) or" % ( srow.seg, "+".join( [str(x) for x in df_sel.res.values[0]])) selstr = selstr[:-3] + ")" cmd.select("%s" % myrgn, selstr) cmd.set_name("%s" % row.rgn1, "sel1") cmd.set_name("%s" % row.rgn2, "sel2") count1 = cmd.count_atoms("sel1") count2 = cmd.count_atoms("sel2") if count1 == 0: if count2 == 0: raise ValueError( "counts of both %s.%d.%s.%d and %s.%d.%s.%d are zero!" % (row.seg1, row.res1, row.rnm1, row.bb1, row.seg2, row.res2, row.rnm2, row.bb2)) else: if self.df_rgn_seg_res_bb is None: cmd.show("sticks", "sel2") #cmd.color(self.sgn2col[mycolind], "sel2") else: com("sel2", state=1) coord2 = cmd.get_model("sel2_COM", state=1).atom[0].coord cmd.delete("sel2_COM") cmd.color(self.sgn2col[mycolind], "sel2") l_cgo += [ \ COLOR, self.l_r[mycolind], self.l_g[mycolind], self.l_b[mycolind], \ SPHERE, coord2[0], coord2[1],coord2[2], self.sphererad \ ] elif count2 == 0: if self.df_rgn_seg_res_bb is None: cmd.show("sticks", "sel1") #cmd.color(self.sgn2col[mycolind], "sel1") else: com("sel1", state=1) cmd.color(self.sgn2col[mycolind], "sel1") coord1 = cmd.get_model("sel1_COM", state=1).atom[0].coord cmd.delete("sel1_COM") l_cgo += [ \ COLOR, self.l_r[mycolind], self.l_g[mycolind], self.l_b[mycolind], \ SPHERE, coord1[0], coord1[1],coord1[2], self.sphererad \ ] else: com("sel1", state=1) coord1 = cmd.get_model("sel1_COM", state=1).atom[0].coord cmd.delete("sel1_COM") com("sel2", state=1) coord2 = cmd.get_model("sel2_COM", state=1).atom[0].coord cmd.delete("sel2_COM") l_cgo += [ \ LINEWIDTH, self.linewidth, \ BEGIN, LINES, \ COLOR, self.l_r[mycolind], self.l_g[mycolind], self.l_b[mycolind], \ VERTEX, coord1[0], coord1[1],coord1[2], \ VERTEX, coord2[0], coord2[1],coord2[2], \ END \ ] if self.df_rgn_seg_res_bb is None: cmd.show("sticks", "sel1") cmd.show("sticks", "sel2") util.cbag("sel1") util.cbag("sel2") else: cmd.color(self.sgn2col[mycolind], "sel1") cmd.color(self.sgn2col[mycolind], "sel2") cmd.delete("sel1") cmd.delete("sel2")
def avg_states(object='all', object_sel=None, first=1, last=0, newobj=None, fitverdict='no', verb=0, pairs=1, writefiles=1): """ ARGUMENTS: object (string [defaults to 'all']): Starting PyMOL molecular object consisting of >1 states to be averaged object_sel (string [defaults to "name CA"]): An optional subset of atoms on which to operate (e.g., "name CA and resi 20-50") first (int [defaults to 1]): number of the first state to include in averaging last (int [defaults to last state]): Number of the last state to include in averaging. A value of '0' (zero) means use the last state in the object. newobj (string [defaults to name of object with string "_avg_AtoZ" appended, where A and Z are the numbers of the first and last frames included in the averaging]): Desired name of the new output pymol object (i.e., averaged structure) fitverdict (string [defaults to "no", any value != "no" is taken as a "yes"]): Use the pymol function intra_fit() to fit all the states of "object & object_sel" before calculating the average structure and RMSDs?? 'no' = NO, !'no' = yes verb (int [defaults to 0]): Verbosity level of output. 0 = quiet, !0 = verbose (e.g., write position-specific RMSDs to standard output). pairs (int [default 1]): Also calculate average pairwise RMSD for each residue position?? 0 = no, !0 = yes writefiles (int [default 1]): Write the calculated RMSD data to plaintext files?? 0 = no, !0 = yes NOTES: The state specified as 'first' is taken as the reference state for the (optional) intra_fit() alignment. If no selection is given, the string "and name CA" will be appended to the object and the averaging and rmsd calculations will be restricted to "object and name CA" (i.e., C-alpha atoms). To avoid this default behavior, specify a valid pymol atom selection for the object_sel argument (e.g., "name CA" or "name CA and resi 20-50" or whatever). """ pair_file = None object = str(object) object_sel = str(object_sel) first = int(first) last = int(last) num_states_tot = cmd.count_states(object) if last < 1: last = num_states_tot num_states2avg = last - first + 1 if newobj is None or newobj == '': newobj = "%s_avg_%dto%d" % (object, first, last) cmd.create(newobj, object, first, 1) if writefiles: print('%s' % ('-' * 80)) datfileprefix = '%s_%dto%d' % (replace(object, ' ', '_'), first, last) avg_rmsd_file = datfileprefix + '.avg_rmsd.dat' avg_file = open(avg_rmsd_file, 'w') print('Opened "%s" file for writing residue-specific RMSDs to ' 'average structure...' % avg_rmsd_file) if pairs: pair_rmsd_file = datfileprefix + '.pair_rmsd.dat' pair_file = open(pair_rmsd_file, 'w') print('Opened "%s" file for writing averaged residue-specific ' 'pairwise RMSDs...' % pair_rmsd_file) print('%s' % ('-' * 80)) else: # just do this instead of evaluating conditionals each time avg_file = open('/dev/null', 'w') if pairs: pair_file = open('/dev/null', 'w') # through the atom loops (below)... obj_sel_string = (lambda o, sel: (o and sel and "%s and %s" % (o, sel)) or (o and not sel and "%s and name CA" % o))(object, object_sel) newobj_sel_string = (lambda o, sel: (o and sel and "%s and %s" % (o, sel)) or (o and not sel and "%s and name CA" % o))(newobj, object_sel) print('%s' % ('-' * 80)) print( 'Averaging %d states [%d, %d] of object "%s" (%d total states) ' 'to get new single-state object "%s"...' % (num_states2avg, first, last, obj_sel_string, num_states_tot, newobj)) print('%s' % ('-' * 80)) tmpobject = '%s_tmp4avg_%dto%d' % (object, first, last) i = 0 for eachstate in range(first, last + 1): i += 1 # because pymol states are indexed starting from 1 (not 0) cmd.create(tmpobject, obj_sel_string, eachstate, i) if fitverdict != 'no': print("-> proceeding WITH FITTING to reference state...") tmpobject_intrafit_rmsds = cmd.intra_fit(tmpobject) if verb: print(' intrafit_rmsds = ', ) print(tmpobject_intrafit_rmsds) else: print("-> proceeding WITHOUT FITTING to reference state...") # create an atom index map between original (complete) object and subset # to be averaged: atindex_map = [None] for at in cmd.get_model(newobj_sel_string).atom: atindex_map.append(at.index) if verb: print("-> atom index_map = ", ) print(atindex_map) newobj_chempy = cmd.get_model(tmpobject) for at in newobj_chempy.atom: this_at_idx = at.index sum_x = sum_y = sum_z = 0.0 # avg_x = avg_y = avg_z = 0.0 state_coords = [] # rmsd_sum = rmsd = 0.0 rmsd_sum = 0.0 for s in range(1, num_states2avg + 1): this_x = cmd.get_model(tmpobject, s).atom[this_at_idx - 1].coord[0] this_y = cmd.get_model(tmpobject, s).atom[this_at_idx - 1].coord[1] this_z = cmd.get_model(tmpobject, s).atom[this_at_idx - 1].coord[2] sum_x += this_x sum_y += this_y sum_z += this_z state_coords.append([this_x, this_y, this_z]) avg_x = sum_x / num_states2avg avg_y = sum_y / num_states2avg avg_z = sum_z / num_states2avg cmd.alter_state( 1, '{0:s} and id {1:d}'.format(newobj, atindex_map[this_at_idx]), 'x=%f' % avg_x) cmd.alter_state( 1, '{0:s} and id {1:d}'.format(newobj, atindex_map[this_at_idx]), 'y=%f' % avg_y) cmd.alter_state( 1, '{0:s} and id {1:d}'.format(newobj, atindex_map[this_at_idx]), 'z=%f' % avg_z) # position-specific RMSDs with respect to average structure: for somept in state_coords: rmsd_sum += pow(calcDist(somept, [avg_x, avg_y, avg_z]), 2.0) rmsd = sqrt(rmsd_sum / num_states2avg) # DEBUG: # DEBUG print('newobj = %sel / id = %d / b = %0.3f' # %(newobj,atindex_map[this_at_idx],rmsd) cmd.alter( '{0:s} and id {1:d}'.format(newobj, atindex_map[this_at_idx]), 'b=%0.3f' % rmsd) if verb: print('"%s" index = %d' % (obj_sel_string, this_at_idx)) print('rmsd = %0.3f' % rmsd) avg_file.write('{0:d}\t{1:0.3f}\n'.format(atindex_map[this_at_idx], rmsd)) # position-specific RMSDs averaged pairwise over the bundle: if pairs: for at in newobj_chempy.atom: this_at_idx = at.index loop_counter = 0 running_sum = 0.0 for s1 in range(1, num_states2avg + 1): for s2 in range(s1 + 1, num_states2avg + 1): # print('computing position %d, %d x %d'%(this_at_idx,s1,s2) pos1 = [ cmd.get_model(tmpobject, s1).atom[this_at_idx - 1].coord[0], cmd.get_model(tmpobject, s1).atom[this_at_idx - 1].coord[1], cmd.get_model(tmpobject, s1).atom[this_at_idx - 1].coord[2] ] pos2 = [ cmd.get_model(tmpobject, s2).atom[this_at_idx - 1].coord[0], cmd.get_model(tmpobject, s2).atom[this_at_idx - 1].coord[1], cmd.get_model(tmpobject, s2).atom[this_at_idx - 1].coord[2] ] loop_counter += 1 running_sum += pow(calcDist(pos1, pos2), 2.0) pairwise_rmsd = sqrt(running_sum / loop_counter) if verb: print('calculated pairwise RMSD for {0:d} pairs at ' 'position {0:d} = {1:0.3f}'.format( loop_counter, atindex_map[this_at_idx], pairwise_rmsd)) pair_file.write('{0:d}\t{1:0.3f}\n'.format( atindex_map[this_at_idx], pairwise_rmsd)) if writefiles: avg_file.close() if pairs: pair_file.close() cmd.delete(tmpobject) return newobj
'cmd.load("dat/nonexistent.r3d")', 'cmd.load("dat/pept.pkl")', 'cmd.load("dat/nonexistent.pkl")', 'cmd.get_names()', 'cmd.delete("all")', 'cmd.get_names()', 'cmd.get_model("nonexistent")', 'cmd.space("cmyk")', 'cmd.space("unknown")', 'cmd.space("rgb")', 'cmd.space()', ]) cmd.delete("all") cmd.load("dat/pept.pdb") mdl = cmd.get_model("pept") print mdl.__class__ print len(mdl.atom) mdl = cmd.get_model("none") print mdl.__class__ print len(mdl.atom) map(x, [ 'cmd.get_model("nonexistent")', 'cmd.create("test","none",quiet=0)', 'cmd.create("test2","nonexistent")', 'cmd.create("test3","?allowed",quiet=0)', 'cmd.fragment("arg")', 'cmd.fragment("nonexistent")', ])
util.cbaw('solute') if solute == 'dna': cmd.show('sticks', 'solute') # DNA else: cmd.show('cartoon', 'solute') # DNA # speed up builds cmd.set('defer_builds_mode', 3) cmd.set('cache_frames', 0) cmd.cache('disable') cmd.set('async_builds', 1) cmd.set('ray_transparency_contrast', 3.0) cmd.set('ray_transparency_shadows', 0) model = cmd.get_model('system') #for atom in model.atom: # print "%8d %4s %3s %5d %8.3f %8.3f %8.3f" % (atom.index, atom.name, atom.resn, int(atom.resi), atom.coord[0], atom.coord[1], atom.coord[2]) #pymol.finish_launching() cmd.viewport(width,height) #niterations = 10 # DEBUG # Load trajectory cmd.load_traj(trajectory_temporary_filename, object='system') # Align all states if solute == 'dna': cmd.intra_fit('name P') # DNA else:
def get_atoms(sele='polymer'): polymer = cmd.get_model(sele) # this gets the protein's data from pdb atoms = [x for x in polymer.atom ] # gets all atom in protein. These are pyChem atom objects return atoms
def edge(name, i_node, j_node, color=None, r=1.0, g=0.0, b=0.0, dg=0.3, dl=0.5, dr=0.2, dir=1, dir_color=None, dir_r=0.0, dir_g=1.0, dir_b=0.0): ''' DESCRIPTION "edge" creates a cylinder (actually sausage) between the two selections that correspond to the 2 nodes. If the edge is directed, only half of the user-formatted cylinder will be drawn towards the target node n2 and the rest will be drawn as a thin cylinder. USAGE edge name, i_node, j_node [, color, r, g, b, dg, dl, dr, dir, dir_color, dir_r, dir_g, dir_b] name = name of edge i_node, j_node = atom selections for node 1 and node 2 color = color name (overwrites rgb) r, g, b = rgb color (default red) dg = dash gap (default 0 - alternative 0.3) dl = dash length (default 0.5) dr = dash radius (default 0.2) dir = directed edge (default 1-yes) dir_color = color name for the other half (overwrites dir_rgb) dir_[r, g, b] = rgb color for the other half (default green) ''' if color is not None: color_rgb = cmd.get_color_tuple(cmd.get_color_index(color)) r = color_rgb[0] g = color_rgb[1] b = color_rgb[2] else: # Convert arguments into floating point values r = float(r) g = float(g) b = float(b) if dir_color is not None: dir_color_rgb = cmd.get_color_tuple(cmd.get_color_index(dir_color)) dir_r = dir_color_rgb[0] dir_g = dir_color_rgb[1] dir_b = dir_color_rgb[2] else: dir_r = float(dir_r) dir_g = float(dir_g) dir_b = float(dir_b) dg = float(dg) dl = float(dl) dr = float(dr) directed = int(dir) frag = directed + 1 # Get tuple containing object and index of atoms in these # selections x1 = cmd.index(i_node, 1) x2 = cmd.index(j_node, 1) # Get number of atoms in each selection n1 = len(x1) n2 = len(x2) if (n1 < 1): print "Error: node " + n1 + " has no atoms" return if (n2 < 1): print "Error: node " + n2 + " has no atoms" return # Get objects and atom indices o1 = x1[0][0] i1 = x1[0][1] o2 = x2[0][0] i2 = x2[0][1] # Get ChemPy models m1 = cmd.get_model(o1) m2 = cmd.get_model(o2) # Get atoms a1 = m1.atom[i1 - 1] a2 = m2.atom[i2 - 1] # Get coords x1 = a1.coord[0] y1 = a1.coord[1] z1 = a1.coord[2] x2 = a2.coord[0] y2 = a2.coord[1] z2 = a2.coord[2] # Make some nice strings for user feedback #n1 = o1 + "/" + a1.segi + "/" + a1.chain + "/" + a1.resn + "." + a1.resi + "/" + a1.name #print n1 + "(" + str(x1) + "," + str(y1) + "," + str(z1) + ")" #n2 = o2 + "/" + a2.segi + "/" + "/" + a2.chain + "/" + a2.resn + "." + a2.resi + "/" + a2.name #print n2 + "(" + str(x2) + "," + str(y2) + "," + str(z2) + ")" # Calculate distances dx = (x2 - x1) / frag dy = (y2 - y1) / frag dz = (z2 - z1) / frag d = math.sqrt((dx * dx) + (dy * dy) + (dz * dz)) #print "distance = " + str(d) + "A" # Work out how many times (dash_len + gap_len) fits into d dash_tot = dl + dg n_dash = math.floor(d / dash_tot) # Work out step lengths dx1 = (dl / dash_tot) * (dx / n_dash) dy1 = (dl / dash_tot) * (dy / n_dash) dz1 = (dl / dash_tot) * (dz / n_dash) dx2 = (dx / n_dash) dy2 = (dy / n_dash) dz2 = (dz / n_dash) # Generate dashes x = x1 y = y1 z = z1 # Empty CGO object obj = [] for i in range(n_dash): # Generate a sausage obj.extend([ SAUSAGE, x, y, z, x + dx1, y + dy1, z + dz1, dr, r, g, b, r, g, b ]) # Move to start of next dash x = x + dx2 y = y + dy2 z = z + dz2 if directed == 1: obj.extend([ SAUSAGE, x, y, z, x2, y2, z2, 0.05, dir_r, dir_g, dir_b, dir_r, dir_g, dir_b ]) cmd.set("stick_quality", 24) # Load the object into PyMOL cmd.load_cgo(obj, name)
def bbPlane(selection='(all)', color='gray', transp=0.3, state=-1, name=None, quiet=1): """ DESCRIPTION Draws a plane across the backbone for a selection ARGUMENTS selection = string: protein object or selection {default: (all)} color = string: color name or number {default: white} transp = float: transparency component (0.0--1.0) {default: 0.0} state = integer: object state, 0 for all states {default: 1} NOTES You need to pass in an object or selection with at least two amino acids. The plane spans CA_i, O_i, N-H_(i+1), and CA_(i+1) """ from pymol.cgo import BEGIN, TRIANGLES, COLOR, VERTEX, END from pymol import cgo from chempy import cpv # format input transp = float(transp) state, quiet = int(state), int(quiet) if name is None: name = cmd.get_unused_name("backbonePlane") if state < 0: state = cmd.get_state() elif state == 0: for state in range(1, cmd.count_states(selection) + 1): bbPlane(selection, color, transp, state, name, quiet) return AAs = [] coords = dict() # need hydrogens on peptide nitrogen cmd.h_add('(%s) and n. N' % selection) # get the list of residue ids for obj in cmd.get_object_list(selection): sel = obj + " and (" + selection + ")" for a in cmd.get_model(sel + " and n. CA", state).atom: key = '/%s/%s/%s/%s' % (obj, a.segi, a.chain, a.resi) AAs.append(key) coords[key] = [a.coord, None, None] for a in cmd.get_model(sel + " and n. O", state).atom: key = '/%s/%s/%s/%s' % (obj, a.segi, a.chain, a.resi) if key in coords: coords[key][1] = a.coord for a in cmd.get_model(sel + " and ((n. N extend 1 and e. H) or (r. PRO and n. CD))", state).atom: key = '/%s/%s/%s/%s' % (obj, a.segi, a.chain, a.resi) if key in coords: coords[key][2] = a.coord # need at least two amino acids if len(AAs) <= 1: print("ERROR: Please provide at least two amino acids, the alpha-carbon on the 2nd is needed.") return # prepare the cgo obj = [ BEGIN, TRIANGLES, COLOR, ] obj.extend(cmd.get_color_tuple(color)) for res in range(0, len(AAs) - 1): curIdx, nextIdx = str(AAs[res]), str(AAs[res + 1]) # populate the position array pos = [coords[curIdx][0], coords[curIdx][1], coords[nextIdx][2], coords[nextIdx][0]] # if the data are incomplete for any residues, ignore if None in pos: if not quiet: print(' bbPlane: peptide bond %s -> %s incomplete' % (curIdx, nextIdx)) continue if cpv.distance(pos[0], pos[3]) > 4.0: if not quiet: print(' bbPlane: %s and %s not adjacent' % (curIdx, nextIdx)) continue normal = cpv.normalize(cpv.cross_product( cpv.sub(pos[1], pos[0]), cpv.sub(pos[2], pos[0]))) obj.append(cgo.NORMAL) obj.extend(normal) # need to order vertices to generate correct triangles for plane if cpv.dot_product(cpv.sub(pos[0], pos[1]), cpv.sub(pos[2], pos[3])) < 0: vorder = [0, 1, 2, 2, 3, 0] else: vorder = [0, 1, 2, 3, 2, 1] # fill in the vertex data for the triangles; for i in vorder: obj.append(VERTEX) obj.extend(pos[i]) # finish the CGO obj.append(END) # update the UI cmd.load_cgo(obj, name, state, zoom=0) cmd.set("cgo_transparency", transp, name)
def useTempColors(sel="all"): for a in cmd.get_model(sel).atom: q = a.b c = intcolors[int(floor(q)) % len(intcolors)] cmd.color(c, "%s and resi %s and name %s" % (sel, a.resi, a.name))
def save_traj(filename, selection='(all)', format='', box=0, quiet=1): ''' DESCRIPTION Save coordinates of a multi-state object to a trajectory file (DCD OR CRD). Based on http://pymolwiki.org/index.php/Save2traj by Sean Law USAGE save_traj filename [, selection [, format ]] ARGUMENTS filename = string: file path to be written selection = string: atoms to save format = string: 'dcd' or 'crd' (alias 'charmm' or 'amber') {default: determined from filename extension) ''' box = int(box) # Get NATOMS, NSTATES NATOMS = cmd.count_atoms(selection) NSTATES = cmd.count_states(selection) # Determine Trajectory Format if format == '' and '.' in filename: format = filename.rsplit('.', 1)[1] format = format.lower() if format in ['charmm', 'dcd']: Outfile = DCDOutfile elif format in ['amber', 'trj', 'crd']: Outfile = CRDOutfile elif format in ['rst', 'rst7']: Outfile = RSTOutfile else: print('Unknown format:', format) raise CmdException f = open(filename, 'wb') # size of periodic box if box: try: boxdim = cmd.get_symmetry(selection)[0:3] except: boxdim = [0, 0, 0] else: boxdim = None outfile = Outfile(filename, NSTATES, NATOMS, box=boxdim) # Write Trajectory Coordinates for state in range(1, NSTATES + 1): xyz = cmd.get_model(selection, state).get_coord_list() outfile.writeCoordSet(xyz) outfile.close() if not int(quiet): fmt = 'Wrote trajectory in %s format with %d atoms and %d frames to file %s' print(fmt % (format, NATOMS, NSTATES, filename))
def calculateDistanceMap(self, pymolObject1, chain_1, pymolObject2, chain_2, cBetaDM=False): print "Working on: %s_%s-%s_%s" %(pymolObject1, chain_1, pymolObject2, chain_2) stored.residueList = [] chain_1_residues = [] chain_1_labelPositions = [] chain_1_cBetaPositions = [] chain_1_numberOfConeAtoms = [] if cmd.count_atoms(pymolObject1) > 1: cmd.iterate("%s & chain %s & name CA & polymer" %(pymolObject1, chain_1), "stored.residueList.append((model, chain, resi,resn))") chain_1_residues = stored.residueList stored.residueList = [] chain_1_labelPositions, chain_1_cBetaPositions, chain_1_numberOfConeAtoms = self.calculateApproximateLabelCoordinate(chain_1_residues) else: cmd.iterate(pymolObject1, "stored.residueList.append((model, chain, resi,resn))") chain_1_residues = stored.residueList chain_1_residues[0] =(pymolObject1, chain_1, "1","CA") chain_1_labelPositions.append(numpy.array(cmd.get_model(pymolObject1, 1).get_coord_list()[0])) chain_1_cBetaPositions.append(numpy.array(cmd.get_model(pymolObject1, 1).get_coord_list()[0])) chain_1_numberOfConeAtoms.append(0) stored.residueList = [] chain_2_labelPositions = [] chain_2_cBetaPositions = [] chain_2_numberOfConeAtoms = [] chain_2_residues = [] if cmd.count_atoms(pymolObject2) > 1: cmd.iterate("%s & chain %s & name CA & polymer" %(pymolObject2, chain_2), "stored.residueList.append((model, chain, resi,resn))") chain_2_residues = stored.residueList chain_2_labelPositions, chain_2_cBetaPositions, chain_2_numberOfConeAtoms = self.calculateApproximateLabelCoordinate(chain_2_residues) else: cmd.iterate(pymolObject2, "stored.residueList.append((model, chain, resi,resn))") chain_2_residues = stored.residueList chain_2_residues[0] =(pymolObject2, chain_2, "1","CA") chain_2_labelPositions.append(numpy.array(cmd.get_model(pymolObject2, 1).get_coord_list()[0])) chain_2_cBetaPositions.append(numpy.array(cmd.get_model(pymolObject2, 1).get_coord_list()[0])) chain_2_numberOfConeAtoms.append(0) #print chain_1_residues, chain_2_residues #account for truncations, gaps, ... #find highest residue number minChain_1 = 100000 minChain_2 = 100000 maxChain_1 = 0 maxChain_2 = 0 for residue in chain_1_residues: if int(residue[2]) > maxChain_1: maxChain_1 = int(residue[2]) if int(residue[2]) < minChain_1: minChain_1 = int(residue[2]) for residue in chain_2_residues: if int(residue[2]) > maxChain_2: maxChain_2 = int(residue[2]) if int(residue[2]) < minChain_2: minChain_2 = int(residue[2]) if maxChain_1 >= maxChain_2: maxLength = maxChain_1 else: maxLength = maxChain_2 plotLimits = {"minChain_1": minChain_1, "maxChain_1": maxChain_1, "minChain_2": minChain_2, "maxChain_2": maxChain_2} #create nan arrays with maxLength rows new_chain_1_labelPositions = numpy.empty([maxLength, 3]) new_chain_1_labelPositions[:] = numpy.NAN new_chain_2_labelPositions = numpy.empty([maxLength, 3]) new_chain_2_labelPositions[:] = numpy.NAN new_chain_1_cBetaPositions = numpy.empty([maxLength, 3]) new_chain_1_cBetaPositions[:] = numpy.NAN new_chain_2_cBetaPositions = numpy.empty([maxLength, 3]) new_chain_2_cBetaPositions[:] = numpy.NAN new_chain_1_numberOfConeAtoms = numpy.empty([maxLength, 1]) new_chain_1_numberOfConeAtoms[:] = numpy.NAN new_chain_2_numberOfConeAtoms = numpy.empty([maxLength, 1]) new_chain_2_numberOfConeAtoms[:] = numpy.NAN #put the data in the nan arrays for idx, residue in enumerate(chain_1_residues): new_chain_1_labelPositions[int(residue[2])-1,:] = numpy.array(chain_1_labelPositions[idx]) new_chain_1_cBetaPositions[int(residue[2])-1,:] = numpy.array(chain_1_cBetaPositions[idx]) new_chain_1_numberOfConeAtoms[int(residue[2])-1,:] = numpy.array(chain_1_numberOfConeAtoms[idx]) for idx, residue in enumerate(chain_2_residues): new_chain_2_labelPositions[int(residue[2])-1,:] = numpy.array(chain_2_labelPositions[idx]) new_chain_2_cBetaPositions[int(residue[2])-1,:] = numpy.array(chain_2_cBetaPositions[idx]) new_chain_2_numberOfConeAtoms[int(residue[2])-1,:] = numpy.array(chain_2_numberOfConeAtoms[idx]) #calculate the distance matrix if cBetaDM: distances = self.quick_map(new_chain_1_cBetaPositions, new_chain_2_cBetaPositions) else: distances = self.quick_map(new_chain_1_labelPositions, new_chain_2_labelPositions) x = numpy.linspace(1, len(new_chain_1_labelPositions)+1, num = len(new_chain_1_labelPositions)+1) y = numpy.linspace(1, len(new_chain_2_labelPositions)+1, num = len(new_chain_2_labelPositions)+1) diagonal = numpy.diagonal(distances) diagonal = ma.masked_where(numpy.isnan(diagonal), diagonal) distances = ma.masked_where(numpy.isnan(distances), distances) new_chain_1_numberOfConeAtoms = ma.masked_where(numpy.isnan(new_chain_1_numberOfConeAtoms),new_chain_1_numberOfConeAtoms) new_chain_2_numberOfConeAtoms = ma.masked_where(numpy.isnan(new_chain_2_numberOfConeAtoms),new_chain_2_numberOfConeAtoms) return distances, x, y, diagonal, new_chain_1_numberOfConeAtoms, new_chain_2_numberOfConeAtoms, plotLimits
def save_pdb(filename, selection='(all)', state=-1, symm=1, ss=1, aniso=0, seqres=0, quiet=1): ''' DESCRIPTION Save the coordinates of a selection as pdb including the secondary structure information and, if possible, the unit cell. The latter requires the selction of a single object USAGE save_pdb filename, selection [, state [, symm [, ss [, aniso ]]]] ARGUMENTS filename = string: file path to be written selection = string: atoms to save {default: (all)} Note: to include the unit cell information you need to select a single object state = integer: state to save {default: -1 (current state)} symm = 0 or 1: save symmetry info if possible {default: 1} ss = 0 or 1: save secondary structure info {default: 1} aniso = 0 or 1: save ANISO records {default: 0} SEE ALSO save ''' selection = selector.process(selection) state, quiet = int(state), int(quiet) symm, ss = int(symm), int(ss) filename = cmd.exp_path(filename) f = open(filename, 'w') print('REMARK 200 Generated with PyMOL and psico'.ljust(80), file=f) # Write sequence if int(seqres): f.write(get_pdb_seqres(selection)) # Write the CRYST1 line if possible if symm: try: obj1 = cmd.get_object_list(selection)[0] sym = cmd.get_symmetry(obj1) if len(sym) != 7: raise f.write("CRYST1%9.3f%9.3f%9.3f%7.2f%7.2f%7.2f %-10s%4d\n" % tuple(sym + [1])) if not quiet: print(' Info: Wrote unit cell and space group info') except: if not quiet: print(' Info: No crystal information') # Write secondary structure if ss: try: sss = get_pdb_sss(selection, state, quiet) if not sss: raise f.write(sss) if not quiet: print(' Info: Wrote secondary structure info') except: if not quiet: print(' Info: No secondary structure information') # Write coordinates of selection pdbstr = cmd.get_pdbstr(selection, state) # fix END records if state == 0 and cmd.get_version()[1] < 1.6: pdbstr = '\n'.join( line for line in pdbstr.splitlines() if line != 'END') + '\nEND\n' # anisotropic b-factors from . import pymol_version if int(aniso) and pymol_version < 1.6 and \ cmd.get_model('first (%s)' % selection).atom[0].u_aniso[0] != 0.0: def mergeaniso(): atom_it = iter(cmd.get_model(selection, state).atom) for line in pdbstr.splitlines(True): yield line if line[:6] in ['ATOM ', 'HETATM']: yield 'ANISOU' + line[6:28] + \ ''.join('%7.0f' % (u*1e4) for u in atom_it.next().u_aniso) + \ line[70:] pdbstr = ''.join(mergeaniso()) f.write(pdbstr) f.close() if not quiet: print('Wrote PDB to \'' + filename + '\'')
def renumber(selection='all', start=1, startsele=None, quiet=1): ''' DESCRIPTION Set residue numbering (resi) based on connectivity. ARGUMENTS selection = string: atom selection to renumber {default: all} start = integer: counting start {default: 1} startsele = string: residue to start counting from {default: first in selection} ''' start, quiet = int(start), int(quiet) model = cmd.get_model(selection) cmd.iterate(selection, 'next(atom_it).model = model', space={ 'atom_it': iter(model.atom), 'next': next }) if startsele is not None: startidx = cmd.index('first (' + startsele + ')')[0] for atom in model.atom: if (atom.model, atom.index) == startidx: startatom = atom break else: print(' Error: startsele not in selection') raise CmdException else: startatom = model.atom[0] for atom in model.atom: atom.adjacent = [] atom.visited = False for bond in model.bond: atoms = [model.atom[i] for i in bond.index] atoms[0].adjacent.append(atoms[1]) atoms[1].adjacent.append(atoms[0]) minmax = [start, start] def traverse(atom, resi): atom.resi = resi atom.visited = True for other in atom.adjacent: if other.visited: continue if (atom.name, other.name) in [('C', 'N'), ("O3'", 'P')]: minmax[1] = resi + 1 traverse(other, resi + 1) elif (atom.name, other.name) in [('N', 'C'), ('P', "O3'")]: minmax[0] = resi - 1 traverse(other, resi - 1) elif (atom.name, other.name) not in [('SG', 'SG')]: traverse(other, resi) traverse(startatom, start) cmd.alter(selection, 'resi = next(atom_it).resi', space={ 'atom_it': iter(model.atom), 'next': next }) if not quiet: print(' Renumber: range (%d to %d)' % tuple(minmax))
def testGetModel(self): ''' Test coordinates and "reference" coordinates with various transformations. ''' # create two-state object # displace state 1, will do tests on state 2 cmd.fragment('ala', 'm1') cmd.create('m1', 'm1', 1, 2) cmd.translate([1, 2, 3], 'm1', 1) # prepare state 2 title = 'Alanin' cmd.set_title('m1', 2, title) cmd.reference(state=2) # with original coordinates m = cmd.get_model(state=2) a = m.atom[0] # title self.assertEqual(title, m.molecule.title) # bonds (covering count, indices and order) self.assertEqual( set(tuple(sorted(b.index)) + (b.order, ) for b in m.bond), set([(0, 1, 1), (0, 5, 1), (1, 2, 1), (1, 4, 1), (1, 6, 1), (2, 3, 2), (4, 7, 1), (4, 8, 1), (4, 9, 1)])) # expect equal coord and ref_coord coord = [-0.67689997, -1.23029995, -0.49050000] self.assertArrayEqual(a.ref_coord, coord, delta=1e-4) self.assertArrayEqual(a.coord, coord, delta=1e-4) # modify ttt cmd.set_object_ttt('m1', [ 0, 1, 0, 0, -1, 0, 0, 5, 0, 0, 1, 0, 0, 3, 0, 1, ]) # no state! API flaw, TTT object are not per state m = cmd.get_model('m1', state=2) a = m.atom[0] # ttt should affect both equally coord = [1.769700050354004, 5.6768999099731445, -0.49050000309944153] self.assertArrayEqual(a.ref_coord, coord, delta=1e-4) self.assertArrayEqual(a.coord, coord, delta=1e-4) # modify coords cmd.translate([10, 0, 0], state=2) m = cmd.get_model('m1', state=2) a = m.atom[0] # no effect of ref_coord ref = coord coord = [11.769700050354004, 5.6768999099731445, -0.49050000309944153] self.assertArrayEqual(a.ref_coord, ref, delta=1e-4) self.assertArrayEqual(a.coord, coord, delta=1e-4) # modify coords by alignment cmd.fragment('ala', 'm2') cmd.rotate('x', 90, 'm2') cmd.align('m1', 'm2', mobile_state=2) m = cmd.get_model('m1', state=2) a = m.atom[0] # no effect of ref_coord coord = [3.490499973297119, 5.6768999099731445, -1.230299949645996] self.assertArrayEqual(a.ref_coord, ref, delta=1e-4) self.assertArrayEqual(a.coord, coord, delta=1e-4)
def get_model(): while 1: time.sleep(random.random()*0.3) cmd.get_model("?obj1") cmd.get_model()
def formal_charges(selection="(all)", quiet=0, _self=cmd): cmd = _self result = 1 # assumes that hydogens are not present! # first, set all formal charges to zero cmd.alter(selection, "formal_charge=0") # next, flag all atoms so that we'll be able to detect what we miss cmd.flag(23, selection, 'set') # get the residue dictionary for formal charges if not hasattr(champ, 'formal_charge_dict'): from chempy.champ.formal_charges import formal_charge_dict champ.formal_charge_dict = formal_charge_dict # iterate through the residue dictionary matching each residue based on chemistry # and generating the expressions for reassigning formal charges alter_list = [] for resn in champ.formal_charge_dict.keys(): if cmd.select(tmp_sele1, "(%s) and resn %s" % (selection, resn)) > 0: entry = champ.formal_charge_dict[resn] for rule in entry: model = cmd.get_model(tmp_sele1) ch = Champ() model_pat = ch.insert_model(model) assn_pat = ch.insert_pattern_string(rule[0]) ch.pattern_clear_tags(model_pat) if ch.match_1v1_n(assn_pat, model_pat, 10000, 2) > 0: result = ch.pattern_get_ext_indices_with_tags(model_pat) for atom_tag in result[0]: # just iterate over atom tags if len(atom_tag[1]) == 1: # one and only one match tag = atom_tag[1][0] formal_charge = rule[1][tag] # the following expression both changes the formal charge and resets flag 23 alter_list.append([ atom_tag[0], "formal_charge=%d;flags=flags&-8388609" % formal_charge ]) if 1: # n-terminal amine # non-proline ch = Champ() model = cmd.get_model(selection) model_pat = ch.insert_model(model) assn_pat = ch.insert_pattern_string("[N;D1]<0>CC(=O)") ch.pattern_clear_tags(model_pat) if ch.match_1v1_n(assn_pat, model_pat, 10000, 2) > 0: result = ch.pattern_get_ext_indices_with_tags(model_pat) for atom_tag in result[0]: # just iterate over atom tags if len(atom_tag[1]) == 1: # one and only one match if atom_tag[1][0] == 0: # the following expression both changes the formal charge and resets flag 23 alter_list.append([ atom_tag[0], "formal_charge=1;flags=flags&-8388609" ]) # proline residues ch = Champ() model = cmd.get_model(selection) model_pat = ch.insert_model(model) assn_pat = ch.insert_pattern_string("C1CC[N;D2]<0>C1C(=O)") ch.pattern_clear_tags(model_pat) if ch.match_1v1_n(assn_pat, model_pat, 10000, 2) > 0: result = ch.pattern_get_ext_indices_with_tags(model_pat) for atom_tag in result[0]: # just iterate over atom tags if len(atom_tag[1]) == 1: # one and only one match if atom_tag[1][0] == 0: # the following expression both changes the formal charge and resets flag 23 alter_list.append([ atom_tag[0], "formal_charge=1;flags=flags&-8388609" ]) if 1: # c-terminal acid ch = Champ() model = cmd.get_model(selection) model_pat = ch.insert_model(model) assn_pat = ch.insert_pattern_string("NCC(=O<0>)[O;D1]<1>") ch.pattern_clear_tags(model_pat) if ch.match_1v1_n(assn_pat, model_pat, 10000, 2) > 0: result = ch.pattern_get_ext_indices_with_tags(model_pat) for atom_tag in result[0]: # just iterate over atom tags if len(atom_tag[1]) == 1: # one and only one match if atom_tag[1][0] == 1: # the following expression both changes the formal charge and resets flag 23 alter_list.append([ atom_tag[0], "formal_charge=-1;flags=flags&-8388609" ]) # now evaluate all of these expressions efficiently en-masse cmd.alter_list(selection, alter_list) # see if we missed any atoms missed_count = cmd.count_atoms("(" + selection + ") and flag 23") if missed_count > 0: if not quiet: # looks like we did, so alter the user print(" WARNING: %d atoms did not have formal charges assigned" % missed_count) result = 0 # remove the temporary selection we used to select appropriate residues cmd.delete(tmp_sele1) return result
def execute(self, result): if result == defaults["compute_command"]: if self.testBinary() == 0: return self.showCrisscross() #input sel1index = self.listbox1.curselection()[0] sel1text = self.listbox1.get(sel1index) self.whichModelSelect = sel1text; print 'selected ' + self.whichModelSelect sel=cmd.get_model(self.whichModelSelect) cnt=0 for a in sel.atom: cnt+=1 print cnt if cnt == 0: error_dialog = Pmw.MessageDialog(self.parent,title = 'Error', message_text = 'ERROR: No molecule loaded.',) junk = error_dialog.activate() return outdir = self.binlocation.getvalue() if os.path.isfile(outdir): error_dialog = Pmw.MessageDialog(self.parent,title = 'Error', message_text = 'ERROR: Output directory is file.',) junk = error_dialog.activate() return elif not os.path.exists(outdir): self.CreateDirectory(outdir) self.stdamString = string.join(self.stdam_list, "+") # jen to zaskrtnute generatedString = "" for key in self.s: if self.s[key].get() == 1: # pak pouzit do vyberu: if key == "AA": generatedString = generatedString + "+" + self.stdamString else: generatedString = generatedString + "+" + key generatedString = generatedString[1:] print "Checked: " + generatedString mmodel = cmd.get_model(self.whichModelSelect) print self.whichModelSelect + " asize: " + str(len(mmodel.atom)) newmodel = Indexed() for matom in mmodel.atom: if generatedString.find(matom.resn) > -1: #print matom.resn newmodel.atom.append(matom) cmd.load_model(newmodel,"tmpCaverModel") #cmd.label("example","name") #fix outdir slashes outdir = outdir.replace("\\","/") if (outdir.endswith("/")): outdir = outdir[:-1] input = "%s/out.pdb" % (outdir) #cmd.save(input, self.whichModelSelect) # to by ulozilo cely model whichModelSelect. cmd.save(input, "tmpCaverModel") cmd.delete("tmpCaverModel") cesta = os.getcwd() # set ignore waters to false -- the model is already filtered by input model and aminos self.varremovewater.set(0) if WINDOWZ: #commandXYZ = "java %s -jar \"%s/modules/Caver2_1_%s/Caver2_1.jar\" \"%s\" %f %f %f %s \"%s\" %d %d %d %s" % (JOPTS, self.pymollocation.getvalue(), VERS, input, float(self.xlocvar.get()), float(self.ylocvar.get()), float(self.zlocvar.get()), self.tunnels.getvalue(), outdir, self.varremovewater.get(), 0,self.methodvar.get(), "tun_" + self.whichModelSelect) commandXYZ = "java %s -jar \"%s\Caver2_1.jar\" \"%s\" %f %f %f %s \"%s\" %d %d %d %s" % (JOPTS, self.pymollocation.getvalue(),input, float(self.xlocvar.get()), float(self.ylocvar.get()), float(self.zlocvar.get()), self.tunnels.getvalue(), outdir, self.varremovewater.get(),0,self.methodvar.get(), "tun_" + self.whichModelSelect) else: commandXYZ = "java %s -jar \"%s/Caver2_1.jar\" \"%s\" %f %f %f %s \"%s\" %d %d %d %s" % (JOPTS, self.pymollocation.getvalue(),input, float(self.xlocvar.get()), float(self.ylocvar.get()), float(self.zlocvar.get()), self.tunnels.getvalue(), outdir, self.varremovewater.get(),0,self.methodvar.get(), "tun_" + self.whichModelSelect) tunnels = int(self.tunnels.getvalue()) # ted vymazat v output dir vsechny soubory s path_*.py os.path.isfile(string) for i in range(tunnels): tunpy = "%s/path_%i.py" % (outdir,i) if (os.path.isfile(tunpy)): os.remove(tunpy); print commandXYZ os.system(commandXYZ) for i in range(tunnels): pathpy = "%s/path_%i.py" % (outdir, i) if os.access(pathpy,os.F_OK): view = cmd.get_view() execfile(pathpy) cmd.set_view(view) if i != 0: cmd.disable("tunnel%i" % i); else: if i == 0: error_dialog = Pmw.MessageDialog(self.parent,title = 'Error', message_text = 'Error: No tunnel was found, the starting point is probably outside of the molecule.',) junk = error_dialog.activate() break else: messa = "No more tunnels than %i were found" % (i) error_dialog = Pmw.MessageDialog(self.parent,title = 'Info', message_text = messa,) junk = error_dialog.activate() break #pass #self.deleteTemporaryFiles() else: # # Doing it this way takes care of clicking on the x in the top of the # window, which as result set to None. # if __name__ == '__main__': # # dies with traceback, but who cares # self.parent.destroy() else: #self.dialog.deactivate(result) global CAVER_BINARY_LOCATION CAVER_BINARY_LOCATION = self.binlocation.getvalue() self.dialog.withdraw()
' and i. ' + str(firstAb + 1), 'chain ' + str(clistAb2[aB2]) + ' and i. ' + str(secondAb + 1), -1, 4), 1)) extra = "" sel1 = 'chain ' + str( clistAb1[aB1]) + ' and i. ' + str(firstAb + 1) sel2 = 'chain ' + str( clistAb2[aB2]) + ' and i. ' + str( secondAb + 1) max_dist = '10' # builds models m1 = cmd.get_model(sel2 + " around " + str(max_dist) + " and " + sel1 + extra) m1o = cmd.get_object_list(sel1) m2 = cmd.get_model(sel1 + " around " + str(max_dist) + " and " + sel2 + extra) m2o = cmd.get_object_list(sel2) # defines selections cmd.select( "__tsel1a", sel1 + " around " + str(max_dist) + " and " + sel2 + extra) cmd.select("__tsel1", "__tsel1a and " + sel2 + extra) cmd.select( "__tsel2a", sel2 + " around " +
def load_consurf(filename, selection, palette='red_white_blue', quiet=1): ''' DESCRIPTION Color by evolutionary conservation. Writes scores to b-factor. You need a "r4s.res" or "consurf.grades" input file. USAGE load_consurf filename, selection [, palette ] SEE ALSO consurfdb ''' import re from .seqalign import needle_alignment, alignment_mapping from . import one_letter # reduced pattern that matches both r4s.res and consurf.grades pattern = re.compile(r'\s*(\d+)\s+([A-Y])\s+([-.0-9]+)\s') scores = [] seqlist = [] if cmd.is_string(filename): handle = open(filename) else: handle = filename if len(cmd.get_chains(selection)) > 1: print(' Warning: selection spans multiple chains') for line in handle: if line.startswith('#') or line.strip() == '': continue m = pattern.match(line) if m is None: continue scores.append(float(m.group(3))) seqlist.append(m.group(2)) selection = '(%s) and polymer' % selection model_ca = cmd.get_model(selection + ' and guide') model_seq = ''.join(one_letter.get(a.resn, 'X') for a in model_ca.atom) sequence = ''.join(seqlist) aln = needle_alignment(model_seq, sequence) scores_resi = dict((model_ca.atom[i].resi, scores[j]) for (i, j) in alignment_mapping(*aln)) cmd.alter(selection, 'b=scores.get(resi, -10)', space={'scores': scores_resi}, quiet=quiet) if palette: cmd.color('yellow', selection + ' and b<-9') if ' ' in palette: from .viewing import spectrumany as spectrum else: spectrum = cmd.spectrum spectrum('b', palette, selection + ' and b>-9.5')
def rmsdByRes(referenceProteinChain, sel, targetProteinChain): """ Update Zhenting Gao on 7/28/2016 USAGE rmsf referenceProteinChain, targetProteinChain, selection [,byres=0], [reference_state=1] Calculate the RMSD for each residue pairs from two chains of the same protein from two crystal structures. Workflow Read reference and target pdb files Align two structures sel target, proA and chain A #define target protein chain sel refrence, proB and chain A #define reference protein chain align target, reference #automatical alignment Clean attributes otherwise rms_cur will fail """ # Create temporary objects, exclude alternative conformation B cmd.create("ref_gzt", referenceProteinChain + " and polymer and not alt B") cmd.alter("ref_gzt", "chain='A'") cmd.alter("ref_gzt", "segi=''") cmd.create("target_gzt", targetProteinChain + " and polymer and not alt B") cmd.alter("target_gzt", "chain='A'") cmd.alter("target_gzt", "segi=''") # cmd.align("target_gzt","ref_gzt",object="align") # parameters outputText = "" res2Check = [ 'HIS', 'ASP', 'ARG', 'PHE', 'GLN', 'GLU', 'LEU', 'ASN', 'TYR', 'VAL' ] # select alpha carbon of selected residues in reference structure calpha = cmd.get_model(sel + " and name CA and not alt B") for g in calpha.atom: # print g.resi+g.resn if cmd.count_atoms("ref_gzt and polymer and resi " + g.resi) == cmd.count_atoms( "target_gzt and polymer and resi " + g.resi): rmsdRes = cmd.rms_cur("ref_gzt and polymer and resi " + g.resi, "target_gzt and polymer and resi " + g.resi) rmsdResCa = cmd.rms_cur( "ref_gzt and polymer and resi " + g.resi + " and name ca", "target_gzt and polymer and resi " + g.resi + " and name ca") rmsdResBackbone = cmd.rms_cur( "ref_gzt and polymer and resi " + g.resi + " and name ca+n+c+o", "target_gzt and polymer and resi " + g.resi + " and name ca+n+c+o") # calculate minimum rmsd rmsdResMin = rmsdRes if g.resn in res2Check: flippedRes = flipAtomName("target_gzt and polymer and resi " + g.resi) rmsdFlippedRes = cmd.rms_cur( "ref_gzt and polymer and resi " + g.resi, flippedRes) if rmsdFlippedRes < rmsdRes: rmsdResMin = rmsdFlippedRes # print cmd.count_atoms("ref_gzt and polymer and resi "+g.resi),cmd.count_atoms("target_gzt and polymer and resi "+g.resi) outputText += "%s,%s,%s,%.3f,%.3f,%.3f,%.3f\n" % ( targetProteinChain, g.resn, g.resi, rmsdRes, rmsdResCa, rmsdResBackbone, rmsdResMin) print outputText # Destroy temporary objects cmd.delete("ref_gzt target_gzt align res_gzt " + flippedRes) # Save data into csv outputFile = 'rmsdByRes_' + sel + '.csv' f = open(outputFile, 'a') if not is_non_zero_file(outputFile): f.write( "targe,residueName,residueId,allAtomRMSD,rmsdResCa,rmsdResBackbone,allAtomRMSDMin\n" ) f.write(outputText) f.close() print "Results saved in " + outputFile
def modevectors(first_obj_frame, last_obj_frame, first_state=1, last_state=1, outname="modevectors", head=0.5, tail=0.1, head_length=0.3, headrgb="0,0,0", tailrgb="0.0,0.0,0.0", cutoff=4.0, skip=0, cut=0.5, atom="CA", stat="show", factor=1.0, notail=1): """ Authors Sean Law & Srinivasa Michigan State University slaw_(at)_msu_dot_edu Editor Sacha Yee USAGE While in PyMOL Parameter Preset Type Description first_obj_frame Undefined String Object name of the first structure. The mode vector will propagate from this structure. Defined by user. last_obj_frame Undefined String Object name of the last structure. The mode vector (arrow head) will end at this structure. Defined by user. first_state 1 Integer Defines state of first object last_state 1 Integer Defines state of last object outname modevectors String Name of object to store mode vectors in. head 1.0 Float Radius for the circular base of the arrow head (cone) tail 0.3 Float Radius for the cylinder of the arrow tail (cylinder) head_length 1.5 Float Length of the arrow head (from the base of the cone to the tip of cone) head_rgb 1.0,1.0,1.0 String RGB colour for the arrow head. tail_rgb 1.0,1.0,1.0 String RGB colour for the arrow tail. cutoff 4.0 Float Skips mode vectors that do not meet the cutoff distance (in Angstroms). skip 0 Integer Denotes how many atoms to skip. No arrows will be created for skipped atoms. cut 0.0 Float Truncates all arrow tail lengths (without disturbing the arrow head) (in Angstroms). atom CA String Designates the atom to derive mode vectors from. stat show String Keeps track and prints statistics (total modevectors, skipped, cutoff). factor 1.0 Float Multiplies each mode vector length by a specified factor. Values between 0 and 1 will decrease the relative mode vector length. Values greater than 1 will increase the relative mode vector length. notail 0 Integer Hides tails and only uses cones (porcupine plot) """ framefirst = cmd.get_model(first_obj_frame, first_state) framelast = cmd.get_model(last_obj_frame, last_state) objectname = outname factor = float(factor) arrow_head_radius = float(head) arrow_tail_radius = float(tail) arrow_head_length = float(head_length) cutoff = float(cutoff) skip = int(skip) cut = float(cut) atomtype = atom.strip('"[]()') objectname = objectname.strip('"[]()') headrgb = headrgb.strip('" []()') tailrgb = tailrgb.strip('" []()') hr, hg, hb = list(map(float, headrgb.split(','))) tr, tg, tb = list(map(float, tailrgb.split(','))) version = cmd.get_version()[1] arrow = [] arrowhead = [] arrowtail = [] x1 = [] y1 = [] z1 = [] x2 = [] y2 = [] z2 = [] exit_flag = False ############################################################## # # # Define an object called "tail" and store the tail and a # # circular base of the triangle in this object. # # # ############################################################## skipcount = 0 skipcounter = 0 keepcounter = 0 atom_lookup = {} for atom in framefirst.atom: if atom.name == atomtype: if skipcount == skip: x1.append(atom.coord[0]) y1.append(atom.coord[1]) z1.append(atom.coord[2]) ########################################## # # # Set atom_lookup for a specific atom # # equal to ONE for the first input set. # # This dictionary will be used as a # # reference for the second set. # # # ########################################## current_atom = "CHAIN " + atom.chain + " RESID "\ + atom.resi + " RESTYPE "\ + atom.resn +\ " ATMNUM " + str(atom.index) # print current_atom atom_lookup['current_atom'] = 1 skipcount = 0 keepcounter += 1 else: # print skipcount skipcount += 1 skipcounter += 1 skipcount = 0 for atom in framelast.atom: if atom.name == atomtype: if skipcount == skip: x2.append(atom.coord[0]) y2.append(atom.coord[1]) z2.append(atom.coord[2]) ######################################### # # # Get atom information from second set # # and compare with first set. All # # atoms from this second set MUST be # # found in the first set! Otherwise, # # the script will exit with an error # # since modevectors can only be created # # by calculating values from identical # # sources. # # # ######################################### current_atom = "CHAIN " + atom.chain + " RESID "\ + atom.resi + " RESTYPE "\ + atom.resn +\ " ATMNUM " + str(atom.index) # print current_atom if 'current_atom' not in atom_lookup: print("\nError: " + current_atom + " from \""\ + last_obj_frame +\ " \"is not found in \"" + first_obj_frame + "\".") print( "\nPlease check your input and/or selections and try again." ) exit_flag = True break skipcount = 0 else: skipcount += 1 if exit_flag == 1: ########################################### # # # Exit script because an atom cannot be # # found in both input files # # # ########################################### return cutoff_counter = 0 # Track number of atoms failing to meet the cutoff ################################################### # # # Check that the two selections/PDB files contain # # the same number of atoms. # # # ################################################### if len(x2) != len(x1): print("\nError: \"" + first_obj_frame +\ "\" and \"" + last_obj_frame +\ "\" contain different number of residue/atoms.") print("\nPlease check your input and/or selections and try again.") return else: # Continue with representing modevectors! ######################################### # # # Delete old selection or object if it # # exists so that it can be overwritten # # # ######################################### save_view = cmd.get_view(output=1, quiet=1) cmd.delete(objectname) cmd.hide(representation="everything", selection=first_obj_frame) cmd.hide(representation="everything", selection=last_obj_frame) ################################################### # # # Begin drawing arrow tails # # # ################################################### arrowtail = [] for mv in range(len(x1)): vectorx = x2[mv] - x1[mv] vectory = y2[mv] - y1[mv] vectorz = z2[mv] - z1[mv] length = sqrt(vectorx**2 + vectory**2 + vectorz**2) if length < cutoff: cutoff_counter += 1 continue t = 1.0 - (cut / length) x2[mv] = x1[mv] + factor * t * vectorx y2[mv] = y1[mv] + factor * t * vectory z2[mv] = z1[mv] + factor * t * vectorz vectorx = x2[mv] - x1[mv] vectory = y2[mv] - y1[mv] vectorz = z2[mv] - z1[mv] length = sqrt(vectorx**2 + vectory**2 + vectorz**2) d = arrow_head_length # Distance from arrow tip to arrow base t = 1.0 - (d / length) if notail: t = 0 tail = [ # Tail of cylinder CYLINDER, x1[mv], y1[mv], z1[mv]\ , x1[mv] + (t + 0.01) * vectorx, y1[mv] + (t + 0.01) * vectory, z1[mv] + (t + 0.01) * vectorz\ , arrow_tail_radius, tr, tg, tb, tr, tg, tb # Radius and RGB for each cylinder tail ] if notail == 0: arrow.extend(tail) x = x1[mv] + t * vectorx y = y1[mv] + t * vectory z = z1[mv] + t * vectorz dx = x2[mv] - x dy = y2[mv] - y dz = z2[mv] - z seg = d / 100 intfactor = int(factor) if version < 1.1: # Version >= 1.1 has cone primitive for i in range(100, 0, -1): # i=100 is tip of cone print(i) t1 = seg * i t2 = seg * (i + 1) radius = arrow_head_radius * ( 1.0 - i / (100.0)) # Radius of each disc that forms cone head = [ CYLINDER, x + t2 * dx, y + t2 * dy, z + t2 * dz\ , x + t1 * dx, y + t1 * dy, z + t1 * dz\ , radius, hr, hg, hb, hr, hg, hb # Radius and RGB for slice of arrow head ] arrow.extend(head) else: head = [ CONE, x, y, z, x + d * dx, y + d * dy, z + d * dz, arrow_head_radius, 0.0, hr, hg, hb, hr, hg, hb, 1.0, 1.0 ] arrow.extend(head) ############################################################## # # # Load the entire object into PyMOL # # # # Print statistics if requested by user # # # ############################################################## if stat == "show": natoms = skipcounter + keepcounter print("\nTotal number of atoms = " + str(natoms)) print("Atoms skipped = " + str(skipcounter)) if keepcounter - cutoff_counter > 0: print("Atoms counted = " + str(keepcounter - cutoff_counter) + " (see PyMOL object \"" + objectname + "\")") else: print("Atoms counted = " + str(keepcounter - cutoff_counter) + " (Empty CGO object not loaded)") print("Atoms cutoff = " + str(cutoff_counter)) # Note that cutoff occurs AFTER skipping! if keepcounter - cutoff_counter > 0: cmd.delete(objectname) cmd.load_cgo( arrow, objectname ) # Ray tracing an empty object will cause a segmentation fault. No arrows = Do not display in PyMOL!!! cmd.show(representation="cartoon", selection=first_obj_frame) if (first_obj_frame != last_obj_frame): cmd.show(representation="cartoon", selection=last_obj_frame) cmd.hide(representation="cartoon", selection=last_obj_frame) cmd.bg_color(color="white") cmd.set_view(save_view) return
def testLoadModel(self): cmd.fragment('gly') m = cmd.get_model() cmd.delete('*') cmd.load_model(m, 'm1') self.assertEqual(7, cmd.count_atoms())
def pairwise_dist(sel1, sel2, max_dist, output="N", output_filename="", sidechain="N", show="N"): """ usage: pairwise_dist sel1, sel2, max_dist, [output=S/P/N, [sidechain=N/Y, [show=Y/N]]] sel1 and sel2 can be any to pre-existing or newly defined selections max_dist: maximum distance in Angstrom between atoms in the two selections --optional settings: output: accepts Screen/Print/None (default N) sidechain: limits (Y) results to sidechain atoms (default N) show: shows (Y) individual distances in pymol menu (default=N) """ print "" cmd.delete("dist*") extra = "" if sidechain == "Y": extra = " and not name c+o+n" #builds models m1 = cmd.get_model(sel2 + " around " + str(max_dist) + " and " + sel1 + extra) m1o = cmd.get_object_list(sel1) m2 = cmd.get_model(sel1 + " around " + str(max_dist) + " and " + sel2 + extra) m2o = cmd.get_object_list(sel2) #defines selections cmd.select("__tsel1a", sel1 + " around " + str(max_dist) + " and " + sel2 + extra) cmd.select("__tsel1", "__tsel1a and " + sel2 + extra) cmd.select("__tsel2a", sel2 + " around " + str(max_dist) + " and " + sel1 + extra) cmd.select("__tsel2", "__tsel2a and " + sel1 + extra) cmd.select("IntAtoms_" + max_dist, "__tsel1 or __tsel2") cmd.select("IntRes_" + max_dist, "byres IntAtoms_" + max_dist) #controlers-1 if len(m1o) == 0: print "warning, '" + sel1 + extra + "' does not contain any atoms." return if len(m2o) == 0: print "warning, '" + sel2 + extra + "' does not contain any atoms." return #measures distances s = "" counter = 0 for c1 in range(len(m1.atom)): for c2 in range(len(m2.atom)): distance = math.sqrt( sum( map(lambda f: (f[0] - f[1])**2, zip(m1.atom[c1].coord, m2.atom[c2].coord)))) if distance < float(max_dist): s += "%s/%s/%s/%s/%s to %s/%s/%s/%s/%s: %.3f\n" % ( m1o[0], m1.atom[c1].chain, m1.atom[c1].resn, m1.atom[c1].resi, m1.atom[c1].name, m2o[0], m2.atom[c2].chain, m2.atom[c2].resn, m2.atom[c2].resi, m2.atom[c2].name, distance) counter += 1 if show == "Y": cmd.distance( m1o[0] + " and " + m1.atom[c1].chain + "/" + m1.atom[c1].resi + "/" + m1.atom[c1].name, m2o[0] + " and " + m2.atom[c2].chain + "/" + m2.atom[c2].resi + "/" + m2.atom[c2].name) #controler-2 if counter == 0: print "warning, no distances were measured! Check your selections/max_dist value" return #outputs if output == "S": print s if output == "P": f = open(output_filename, 'w') f.write("Number of distances calculated: %s\n" % (counter)) f.write(s) f.close() print "Results saved in %s" % output_filename print "Number of distances calculated: %s" % (counter) cmd.hide("lines", "IntRes_*") if show == "Y": cmd.show("lines", "IntRes_" + max_dist) cmd.deselect()
def test_chemical_conn_bond(self): cmd.load(self.datafile('1519159.cif'), 'm1') model = cmd.get_model() self.assertEqual(len(model.atom), 26) self.assertEqual(len(model.bond), 35)
cmd.deselect() cmd.hide('all') cmd.show('cartoon', 'receptor') cmd.show('spheres', 'ligand') cmd.show('spheres', 'ions') util.cbay('ligand') cmd.color('green', 'receptor') # speed up builds cmd.set('defer_builds_mode', 3) cmd.set('cache_frames', 0) cmd.set('ray_transparency_contrast', 3.0) cmd.set('ray_transparency_shadows', 0) model = cmd.get_model('complex') #for atom in model.atom: # print "%8d %4s %3s %5d %8.3f %8.3f %8.3f" % (atom.index, atom.name, atom.resn, int(atom.resi), atom.coord[0], atom.coord[1], atom.coord[2]) #pymol.finish_launching() # Read atoms from PDB pdbatoms = readAtomsFromPDB(reference_pdbfile) # Build mappings. pdb_indices = dict() for (index, atom) in enumerate(pdbatoms): if atom['chainID'] == ' ': atom['chainID'] = '' #if atom['resName'] == 'WAT': continue key = (atom['chainID'], int(atom['resSeq']), atom['name'].strip()) value = index
def amber99(selection="(all)", quiet=0, _self=cmd): cmd = _self result = 1 # first, set all parameters to zero cmd.alter(selection, "partial_charge=0") cmd.alter(selection, "elec_radius=0.0") cmd.alter(selection, "text_type=''") # next, flag all atoms so that we'll be able to detect what we miss cmd.flag(23, selection, 'set') # get the amber99 dictionary if not hasattr(champ, 'amber99_dict'): from chempy.champ.amber99 import amber99_dict champ.amber99_dict = amber99_dict # iterate through the residue dictionary matching each residue based on chemistry # and generating the expressions for reassigning formal charges alter_list = [] for resn in champ.amber99_dict.keys(): if cmd.select(tmp_sele1, "(%s) and resn %s" % (selection, resn)) > 0: entry = champ.amber99_dict[resn] for rule in entry: model = cmd.get_model(tmp_sele1) ch = Champ() model_pat = ch.insert_model(model) ch.pattern_detect_chirality(model_pat) assn_pat = ch.insert_pattern_string(rule[0]) ch.pattern_clear_tags(model_pat) if ch.match_1v1_n(assn_pat, model_pat, 10000, 2) > 0: result = ch.pattern_get_ext_indices_with_tags(model_pat) for atom_tag in result[0]: # just iterate over atom tags if len(atom_tag[1]) == 1: # one and only one match tag = atom_tag[1][0] prop_list = rule[1][tag] # the following expression both changes the formal charge and resets flag 23 alter_list.append([ atom_tag[0], "name='''%s''';text_type='''%s''';partial_charge=%f;elec_radius=%f;flags=flags&-8388609" % prop_list ]) # now evaluate all of these expressions efficiently en-masse cmd.alter_list(selection, alter_list) # see if we missed any atoms missed_count = cmd.count_atoms("(" + selection + ") and flag 23") if missed_count > 0: if not quiet: # looks like we did, so alter the user print(" WARNING: %d atoms did not have properties assigned" % missed_count) result = 0 # remove the temporary selection we used to select appropriate residues cmd.delete(tmp_sele1) return result