def minimize(selection='all', forcefield='MMFF94s', method='Conjugate Gradients', nsteps0=500, conv=0.0001, cutoff=False, cut_vdw=6.0, cut_elec=8.0): mol_string = cmd.get_str('mol', selection) name = cmd.get_legal_name(selection) obconversion = ob.OBConversion() obconversion.SetInAndOutFormats('mol', 'mol') mol = ob.OBMol() obconversion.ReadString(mol, mol_string) ff = ob.OBForceField.FindForceField( forcefield) ## GAFF, MMFF94s, MMFF94, UFF, Ghemical ff.Setup(mol) if cutoff == True: ff.EnableCutOff(True) ff.SetVDWCutOff(cut_vdw) ff.SetElectrostaticCutOff(cut_elec) if method == 'Conjugate Gradients': ff.ConjugateGradients(nsteps0, conv) else: ff.SteepestDescent(nsteps0, conv) ff.GetCoordinates(mol) nrg = ff.Energy() mol_string = obconversion.WriteString(mol) cmd.delete(name) if name == 'all': name = 'all_' cmd.read_molstr(mol_string, name, state=0, finish=1, discrete=1) print('#########################################') print('The Energy of %s is %8.2f %s ' % (name, nrg, ff.GetUnit())) print('#########################################')
def rpcLabel(pos, labelText, id='lab1', color=(1, 1, 1)): """ create a text label Arguments: pos: a 3 tuple with the position of the label text: a string with the label color: a 3 tuple with the color of the label. (1,1,1) is white id: (OPTIONAL) the name of the object to be created NOTE: at the moment this is, how you say, a hack """ x, y, z = pos text = """ Atom 1 0 0 0 0 0 0 0 0 0999 V2000 % 10.4f% 10.4f%10.4f C 0 0 0 0 0 0 0 0 0 0 0 0 M END""" % (x, y, z) cmd.read_molstr(text, id) cmd.label("%s" % (id), '"%s"' % labelText) cmd.hide("nonbonded", id) cmd.set_color("%s-color" % id, color) cmd.color("%s-color" % id, id) return 1
def rpcLoadMolBlock(data, objName, colorScheme='', replace=1): """ loads a molecule from a mol block Arguments: data: the mol block objName: name of the object to create colorScheme: (OPTIONAL) name of the color scheme to use for the molecule (should be either 'std' or one of the color schemes defined in pymol.utils) replace: (OPTIONAL) if an object with the same name already exists, delete it before adding this one """ from pymol import util if replace: cmd.delete(objName) res = cmd.read_molstr(data, objName) colorObj(objName, colorScheme) if res is not None: return res else: return ''
def load_annotated_sdf(filename, object=None, state=1, discrete=1, _self=cmd): pymol = _self._pymol cmd = _self # get object name from file prefix if object == None: object = re.sub(r"\.[sS][dD][fF]$", "", filename) # open the SD file inp_sdf = SDF(filename) # create a persistent place to store the annotations if not hasattr(pymol.session, 'annotation'): pymol.session.annotation = {} # create a state-indexed dictionary for this object state_dict = {} pymol.session.annotation[object] = state_dict while 1: # get next record sdf_rec = inp_sdf.read() # if at end of list, break out of loop if not sdf_rec: break # get the MOL portion of the record mol_list = sdf_rec.get('MOL') # load it into PyMOL cmd.read_molstr(string.join(mol_list, ''), object, state, finish=0, discrete=discrete) # populate with tuple containing ordered list of keys # and associated data dictionary anno_list = ["\\955" + object] for key in sdf_rec.kees: if (key != 'MOL'): data = sdf_rec.data[key] print key, data anno_list.append( " \\595%s: \\559%s" % (key, string.join(map(string.strip, sdf_rec.data[key])))) state_dict[state] = anno_list # increment the state index state = state + 1 if state > 1: cmd.zoom(object) cmd.finish_object(object)
def testReadMolstr(self): cmd.read_molstr(molstr, 'm1') self.assertEqual(7, cmd.count_atoms())
def load_annotated_sdf(filename, object=None, state=1, discrete=1, _self=cmd): pymol=_self._pymol cmd=_self # get object name from file prefix if object==None: object = re.sub(r"\.[sS][dD][fF]$","",filename) # open the SD file inp_sdf = SDF(filename) # create a persistent place to store the annotations if not hasattr(pymol.session,'annotation'): pymol.session.annotation = {} # create a state-indexed dictionary for this object state_dict = {} pymol.session.annotation[object] = state_dict while 1: # get next record sdf_rec = inp_sdf.read() # if at end of list, break out of loop if not sdf_rec: break # get the MOL portion of the record mol_list = sdf_rec.get('MOL') # load it into PyMOL cmd.read_molstr(''.join(mol_list),object, state,finish=0,discrete=discrete) # populate with tuple containing ordered list of keys # and associated data dictionary anno_list = [ "\\955"+object ] for key in sdf_rec.kees: if (key!='MOL'): data = sdf_rec.data[key] print(key,data) anno_list.append(" \\595%s: \\559%s"%( key, ' '.join(map(str.strip, sdf_rec.data[key])))) state_dict[state] = anno_list # increment the state index state = state + 1 if state > 1: cmd.zoom(object) cmd.finish_object(object)
def run_comparison(references=None, conformers=None): references_file = args.references conformers_file = args.conformers print(' -- -- -- -- -- LOADING REFERENCES IN {} -- -- -- -- -- '.format( references_file)) print(' -- -- -- -- -- LOADING CONFORMERS IN {} -- -- -- -- -- '.format( conformers_file)) lowest_rmsd = [] #out_macro_refs = Chem.SDWriter('Macrocycles_references.sdf') out_RMSD = Chem.SDWriter('Aligment_RMSD.sdf') #out_macro_confs = Chem.SDWriter('Macrocycles_conformers.sdf') print(' -- -- -- -- -- STARTING ANALYSIS -- -- -- -- -- ') ref_index = 1 for ref in Chem.SDMolSupplier(references_file): print(ref_index, ')', ref.GetProp('_Name').split('_')[0]) macrocycle_atoms = [] all_cycles = Chem.GetSymmSSSR(ref) cycles_size = [i for i in all_cycles if len(i) >= 8] for element in cycles_size: macrocycle_atoms += list(element) all_atoms = [i.GetIdx() for i in ref.GetAtoms()] atoms_to_remove = (list(set(all_atoms) - set(macrocycle_atoms))) macrocycle = Chem.RWMol(ref) for i in sorted(atoms_to_remove, reverse=True): macrocycle.RemoveAtom(i) m_ref = macrocycle.GetMol() m_ref.UpdatePropertyCache() macrocycle_atoms = sorted(list(set(macrocycle_atoms))) print('Initial Num Atoms:', len(all_atoms)) print('Macrocycle length:', len(macrocycle_atoms)) m_ref_smiles = Chem.MolFragmentToSmiles(ref, macrocycle_atoms, kekuleSmiles=True) m_ref_smiles = Chem.MolFromSmiles(m_ref_smiles, sanitize=False) ref_index = ref_index + 1 mol_index = 0 table = pd.DataFrame() for mol in Chem.SDMolSupplier(conformers_file): if ref.GetProp('_Name').split('_')[0] == mol.GetProp( '_Name').split('_')[0]: table.loc[mol_index, 'Conformer'] = [mol.GetProp('_Name')] ref_atoms = ref.GetSubstructMatch(m_ref_smiles) mol_atoms = mol.GetSubstructMatch(m_ref_smiles) amap = zip(mol_atoms, ref_atoms) rms_macrocycle = AllChem.GetBestRMS(mol, ref, map=[list(amap)]) mol.SetProp('RMSD_macrocycle', str(rms_macrocycle)) table.loc[mol_index, 'RMSD_macrocycle'] = [rms_macrocycle] macrocycle_atoms = [] all_cycles = Chem.GetSymmSSSR(mol) cycles_size = [i for i in all_cycles if len(i) >= 8] for element in cycles_size: macrocycle_atoms += list(element) all_atoms = [i.GetIdx() for i in mol.GetAtoms()] atoms_to_remove = ( list(set(all_atoms) - set(macrocycle_atoms))) macrocycle = Chem.RWMol(mol) for i in sorted(atoms_to_remove, reverse=True): macrocycle.RemoveAtom(i) m_mol = macrocycle.GetMol() m_mol.UpdatePropertyCache() #m_mol=Chem.MolFragmentToSmiles(mol,macrocycle_atoms,kekuleSmiles=True) #m_mol=Chem.MolFromSmiles(m_mol,sanitize=False) radious_macro = Descriptors3D.RadiusOfGyration(m_mol) table.loc[mol_index, 'RoG_macrocycle'] = radious_macro tt_macro = rdMolDescriptors.GetTopologicalTorsionFingerprint( m_mol) table.loc[mol_index, 'TF_macrocycle'] = [tt_macro.GetTotalVal()] r_list = Chem.TorsionFingerprints.CalculateTorsionLists(m_ref) r_angles = Chem.TorsionFingerprints.CalculateTorsionAngles( m_ref, r_list[0], r_list[1]) c_list = Chem.TorsionFingerprints.CalculateTorsionLists(m_mol) c_angles = Chem.TorsionFingerprints.CalculateTorsionAngles( m_mol, c_list[0], c_list[1]) if len(r_angles) == len(c_angles): torsion_macro = Chem.TorsionFingerprints.CalculateTFD( r_angles, c_angles) table.loc[mol_index, 'TFD_macrocycle'] = [torsion_macro] else: table.loc[mol_index, 'TFD_macrocycle'] = ['NA'] cmd.read_molstr(Chem.MolToMolBlock(ref), 'ref') cmd.read_molstr(Chem.MolToMolBlock(mol), 'mol') rmsd = cmd.rms_cur('ref', 'mol') cmd.deselect() cmd.delete('all') mol.SetProp('RMSD_heavy_atoms', str(rmsd)) table.loc[mol_index, 'RMSD_heavy_atoms'] = [rmsd] out_RMSD.write(mol) radious = Descriptors3D.RadiusOfGyration(mol) table.loc[mol_index, 'RoG_heavy_atoms'] = radious tt = rdMolDescriptors.GetTopologicalTorsionFingerprint(mol) table.loc[mol_index, 'TF_heavy_atoms'] = [tt.GetTotalVal()] r_list = Chem.TorsionFingerprints.CalculateTorsionLists(ref) r_angles = Chem.TorsionFingerprints.CalculateTorsionAngles( ref, r_list[0], r_list[1]) c_list = Chem.TorsionFingerprints.CalculateTorsionLists(mol) c_angles = Chem.TorsionFingerprints.CalculateTorsionAngles( mol, c_list[0], c_list[1]) if len(r_angles) == len(c_angles): torsion = Chem.TorsionFingerprints.CalculateTFD( r_angles, c_angles) table.loc[mol_index, 'TFD_heavy_atoms'] = [torsion] else: table.loc[mol_index, 'TFD_heavy_atoms'] = ['NA'] mol_index = mol_index + 1 if len(table.index) > 0: sort = table.sort_values('RMSD_macrocycle', ascending=True) sort = sort.reset_index(drop=True) sort.to_csv(ref.GetProp('_Name') + '.csv') sort['Nconf'] = len(sort.index) print('Number of conformers analyzed:', len(sort.index)) print('data in file:', ref.GetProp('_Name') + '.csv') print('-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- \n') sort['Span_Rog_macrocycle'] = float( max(sort['RoG_macrocycle']) - min(sort['RoG_macrocycle'])) sort['Span_Rog_heavy_atoms'] = float( max(sort['RoG_heavy_atoms']) - min(sort['RoG_heavy_atoms'])) lowest_rmsd.append(sort.loc[0]) else: print('No reference or conformers found in input files for {}'. format(ref.GetProp('_Name'))) print(' ************************************ \n') #out_macro_refs.close() out_RMSD.close() #out_macro_confs.close() print('SAVING DATA OF LOWEST RMSD OF CONFORMERS') summary = pd.DataFrame(lowest_rmsd) summary = summary.reset_index(drop=True) summary.to_csv('Lowest_RMSD_Data.csv') print('Lowest RMSD Data in file: Lowest_RMSD_Data.csv') print('***************************************************\n') print('Structures in files: Alignment_RMSD.sdf') print('***************************************************\n') print( 'CALCULATION OF {} OUT OF {} REFERENCES DONE, FILES SAVED. THANK YOU FOR USING THIS SCRIPT \n' .format(len(summary.index), len(Chem.SDMolSupplier(references_file))))
def conf_search(selection='all', forcefield='MMFF94s', method='Weighted', nsteps1=500, conformers=25, lowest_conf=5): mol_string = cmd.get_str('mol', selection) name = cmd.get_legal_name(selection) obconversion = ob.OBConversion() obconversion.SetInAndOutFormats('mol', 'mol') mol = ob.OBMol() obconversion.ReadString(mol, mol_string) ff = ob.OBForceField.FindForceField( forcefield) ## GAFF, MMFF94s, MMFF94, UFF, Ghemical ff.Setup(mol) if method == 'Weighted': ff.WeightedRotorSearch(conformers, nsteps1) elif method == 'Random': ff.RandomRotorSearch(conformers, nsteps1) else: ff.SystematicRotorSearch(nsteps1) if name == 'all': name = 'all_' if method in ['Weighted', 'Random']: ff.GetConformers(mol) print('##############################################') print(' Conformer | Energy | RMSD') nrg_unit = ff.GetUnit() rmsd = 0 ff.GetCoordinates(mol) nrg = ff.Energy() conf_list = [] for i in range(conformers): mol.SetConformer(i) ff.Setup(mol) nrg = ff.Energy() conf_list.append((nrg, i)) conf_list.sort() lenght_conf_list = len(conf_list) if lowest_conf > lenght_conf_list: lowest_conf = lenght_conf_list for i in range(lowest_conf): nrg, orden = conf_list[i] name_n = '%s%02d' % (name, i) cmd.delete(name_n) mol.SetConformer(orden) mol_string = obconversion.WriteString(mol) cmd.read_molstr(mol_string, name_n, state=0, finish=1, discrete=1) if i != 0: rmsd = cmd.fit(name_n, '%s00' % name, quiet=1) print('%15s | %10.2f%9s |%6.1f' % (name_n, nrg, nrg_unit, rmsd)) print('##############################################') else: ff.GetCoordinates(mol) nrg = ff.Energy() mol_string = obconversion.WriteString(mol) cmd.delete(name) cmd.read_molstr(mol_string, name, state=0, finish=1, discrete=1) print('#########################################') print('The Energy of %s is %8.2f %s ' % (name, nrg, ff.GetUnit())) print('#########################################')