def get_SASA(pdbfile, pdb1='pdb1'): ## set cmd.load(pdbfile, pdb1) oldDS = cmd.get("dot_solvent") cmd.h_add() cmd.flag("ignore", "none") cmd.set("dot_solvent", 1) cmd.set("dot_density", 2) cmd.set("solvent_radius", 3) ## calculate area = cmd.get_area(pdb1, load_b=1) stored.r = [] cmd.iterate(pdb1, 'stored.r.append((model,chain,resi,resn,name,b))') areas = {} for model, chain, idx, char, name, sasa in stored.r: if idx == '653': print chain, idx, char, name, sasa base = areas.get((chain, idx, char), 0) if (char == 'A' and name == 'N1') or (char == 'C' and name == 'N3'): base += float(sasa) areas[(chain, idx, char)] = base ## reset cmd.set("dot_solvent", oldDS) cmd.delete(pdb1) print pdbfile, area, sum(areas.values()) return areas
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 collect(cls): for id in range(0, 50): obj_mask = "consensus.{id:03}.*".format(id=id) objs = fnmatch.filter(pm.get_object_list(), obj_mask) if len(objs) == 0: break if len(objs) > 1: raise Exception(f"Too much objects found: {', '.join(objs)}") obj = objs[0] pm.flag("ignore", obj, "clear", True) coords = pm.get_coords(obj) yield Cluster(id, obj, coords)
def mse2met(selection='all', quiet=1): ''' DESCRIPTION Mutate selenomethionine to methionine ''' quiet = int(quiet) x = cmd.alter('(%s) and MSE/SE' % selection, 'name="SD";elem="S"') cmd.flag('ignore', '(%s) and MSE/' % (selection), 'clear') cmd.alter('(%s) and MSE/' % selection, 'resn="MET";type="ATOM"') if not quiet: print('Altered %d MSE residues to MET' % (x)) cmd.sort()
def mse2met(selection="all", quiet=1): """ DESCRIPTION Mutate selenomethionine to methionine """ quiet = int(quiet) x = cmd.alter("(%s) and MSE/SE" % selection, 'name="SD";elem="S"') cmd.flag("ignore", "(%s) and MSE/" % (selection), "clear") cmd.alter("(%s) and MSE/" % selection, 'resn="MET";type="ATOM"') if not quiet: print "Altered %d MSE residues to MET" % (x) cmd.sort()
def clean_ob(selection, present='', state=-1, fix='', restrain='', method='mmff', save_undo=1, message=None, _self=cmd): ''' DESCRIPTION Replacement for pymol.computing._clean, using openbabel. Side effects: clears "fix" flag if "present" argument is given. import pymol.computing import psico.minimizing pymol.computing._clean = psico.minimizing.clean_ob SEE ALSO minimize_ob ''' if present: cmd.flag('fix', present, 'set') cmd.flag('fix', selection, 'clear') selection = '({})|({})'.format(selection, present) ff = {'mmff': 'MMFF94'}.get(method, method) minimize_ob(selection, state, ff, nsteps=50, _self=_self) if present: cmd.flag('fix', present, 'clear')
def build(self): """Build the peptide""" name = self.targetObjectNameLineEdit.text() if name in cmd.get_object_list(): if QtWidgets.QMessageBox.question( self, 'Overwrite existing model?', 'Model {} already exists. Do you want to overwrite it?'. format(name), QtWidgets.QMessageBox.Yes | QtWidgets.QMessageBox.No, QtWidgets.QMessageBox.No) == QtWidgets.QMessageBox.NoRole: # "No" was selected by the user, return without building return cmd.delete('model {}'.format(name)) residues = self.model.residues() try: betafab2(name, *residues) except Exception as exc: QtWidgets.QMessageBox.critical( self, 'Error while building peptide', 'Error while building peptide:\n{}'.format(exc.args[0])) if self.cleanupCheckBox.isChecked(): # try to energy minimize the structure select_bbb('_bbone_of_{}'.format(name), 'model {}'.format(name)) cmd.flag('fix', '_bbone_of_{}'.format(name), 'set') try: import freemol.mengine cmd.clean('model {}'.format(name)) except: warnings.warn( 'Cannot run clean(): This PyMOL build appears not to include full modeling capabilities ' '(could not import the freemol.mengine module). Using the inferior sculpting facility.' ) cmd.sculpt_activate(name) cmd.sculpt_iterate(name, cycles=1000) cmd.sculpt_deactivate(name) cmd.flag('fix', '_bbone_of_{}'.format(name), 'clear') cmd.delete('_bbone_of_{}'.format(name))
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 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 prune_grid(rna, score_file, outname, quantile=0.99, sasa_cutoff=20.0): # make sure all atoms within an object occlude one another cmd.flag("ignore", "none") # use solvent-accessible surface with high sampling density cmd.set('dot_solvent', 1) cmd.set('dot_density', 3) cmd.set('solvent_radius', 2.0) k = 1 df = pd.read_csv(score_file, header=0, sep=",") means = df[['pred_MLP', 'pred_XGB', 'pred_RF', 'pred_LR', 'pred_Extra']].apply(np.mean, 'columns') cutoff = np.quantile(a=means, q=quantile) for pred, pos in zip( df[['pred_MLP', 'pred_XGB', 'pred_RF', 'pred_LR', 'pred_Extra']].values, df[['x', 'y', 'z']].values): if np.mean(pred) > cutoff: # create tmp complex cmd.pseudoatom("tmpPoint3", hetatm=1, name="C", resn="UNK", pos=[pos[0], pos[1], pos[2]]) cmd.create("complextmp", "%s tmpPoint3" % rna) sasa = cmd.get_area('resn UNK and not polymer and complextmp') cmd.delete("complextmp tmpPoint3") # remove really highly exposed points if sasa < sasa_cutoff: cmd.pseudoatom("tmpPoint", hetatm=1, b=np.mean(pred), q=sasa, name="C", resn="UNK", resi=k, chain="ZZ", pos=[pos[0], pos[1], pos[2]]) print(pred, pos, np.mean(pred), cutoff, sasa) k += 1 # write out grid file coor = "%s_pruned_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_pruned_grid.pdb" % (outname) cmd.save(coor, "complex") coor = "cavity_pruned_grid.sd" cmd.save(coor, "tmpPoint") # remove isolated remove_isolated() # write out grid file coor = "%s_pruned_grid_clusters.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_pruned_grid_clusters.pdb" % (outname) cmd.save(coor, "complex") coor = "cavity_pruned_grid.sd" cmd.save(coor, "tmpPoint")