def genConfs(c_mol, ofsff, ofsTri, index): # set omega settings omega = oeomega.OEOmega() omega.SetMaxConfs(1) omega.SetIncludeInput(False) omega.SetEnergyWindow(15.0) strict_stereo = True omega.SetStrictStereo(strict_stereo) omega.SetSampleHydrogens(True) omega.SetStrictAtomTypes(True) mol = oechem.OEMol(c_mol) status = omega(mol) if status: # change title mol.SetTitle(f'DrugBank_{index}') # save force field type mol1 = oechem.OEMol(mol) oechem.OETriposAtomNames(mol1) oechem.OEWriteConstMolecule(ofsff, mol1) # save Tripos atom types mol2 = oechem.OEMol(mol) oechem.OETriposAtomTypeNames(mol2) oechem.OEWriteConstMolecule(ofsTri, mol2) return status
def main(argv=sys.argv): if len(argv) != 2: oechem.OEThrow.Usage("%s <infile (oeb file prefix)>" % argv[0]) ifs = oechem.oemolistream() if not ifs.open(argv[1] + '.oeb'): oechem.OEThrow.Fatal("Unable to open %s for reading" % argv[1] + '.oeb') ofsff = oechem.oemolostream() ofsff.SetFlavor(oechem.OEFormat_MOL2, oechem.OEOFlavor_MOL2_Forcefield) if not ofsff.open(argv[1] + '_ff.mol2'): oechem.OEThrow.Fatal("Unable to open %s for writing" % argv[1] + '_ff.mol2') ofsTri = oechem.oemolostream() ofsTri.SetFlavor(oechem.OEFormat_MOL2, oechem.OEOFlavor_MOL2_Forcefield) if not ofsTri.open(argv[1] + '_tripos.mol2'): oechem.OEThrow.Fatal("Unable to open %s for writing" % argv[1] + '_tripos.mol2') for mol in ifs.GetOEMols(): oechem.OETriposAtomNames(mol) oechem.OEWriteConstMolecule(ofsff, mol) oechem.OETriposAtomTypeNames(mol) oechem.OEWriteConstMolecule(ofsTri, mol) ifs.close() ofsff.close() ofsTri.close() return 0
def charge_mols(infile, outfile, reffile=None): ### Read in molecules ifs = oechem.oemolistream() if not ifs.open(infile): oechem.OEThrow.Warning("Unable to open %s for reading" % infile) return ### Open output file ofs = oechem.oemolostream() if not ofs.open(outfile): oechem.OEThrow.Fatal("Unable to open %s for writing" % outfile) ### Charge the molecules and write output if reffile is None: for mol in ifs.GetOEMols(): if not oequacpac.OEAssignCharges(mol, oequacpac.OEAM1BCCELF10Charges()): oechem.OEThrow.Warning("Unable to charge mol {}".format( mol.GetTitle())) oechem.OEWriteConstMolecule(ofs, mol) ifs.close() ofs.close() else: ### Read in molecules rfs = oechem.oemolistream() if not rfs.open(reffile): oechem.OEThrow.Warning("Unable to open %s for reading" % reffile) return ### Set coordinates of desired molecule on the mol with charges for in_mol, ref_mol in zip(ifs.GetOEMols(), rfs.GetOEMols()): ref_mol.SetCoords(in_mol.GetCoords()) oechem.OEWriteConstMolecule(ofs, ref_mol) ifs.close() ofs.close()
def traj_conf_extraction(ctx): for record in ctx.obj['records']: sys_id = check_sys_id(record) if not record.has_field(Fields.Analysis.oetraj_rec): raise ValueError("Multi Conf Trajectory record field is missing") oetraj_rec = record.get_value(Fields.Analysis.oetraj_rec) traj_names = ["LigTraj", "ProtTraj_OPLMD", "WatTraj"] ofs = oechem.oemolostream(sys_id + "_traj_confs.oeb") for fd in oetraj_rec.get_fields(): name = fd.get_name() if name in traj_names: mol = oetraj_rec.get_value(fd) mol.SetTitle(name) oechem.OEWriteConstMolecule(ofs, mol) ofs.close()
def convert_extension(infile, outfile, canonical=False): """ Convert one molecule file format into another using OpenEye tools. The user may also assign canonical smiles as name before writing output. """ # open input file mols = reader.read_mols(infile) # open output file ofs = oechem.oemolostream() if not ofs.open(outfile): oechem.OEThrow.Fatal("Unable to open %s for writing" % outfile) # write to output for mol in mols: if canonical: smi = oechem.OEMolToSmiles(mol) for conf in mol.GetConfs(): if canonical: conf.SetTitle(smi) oechem.OEWriteConstMolecule(ofs, conf) # close filestreams ofs.close()
def writeUpdatedMol(Mol, fname, log): """ Parameters ---------- Mol: an OEChem molecule fname: str - name of the output mol2 file log: open file to write output to Returns ------- True if the function completed successfully """ # Open output file to write molecule. ofs = oechem.oemolostream() if os.path.exists(fname): log.write("Output .mol2 file already exists. Skipping.\n") return False if not ofs.open(fname): oechem.OEThrow.Fatal("Unable to open %s for writing" % fname) # write out mol2 file and close the output filestream. oechem.OEWriteConstMolecule(ofs, Mol) ofs.close() return True
def combineSDF(infiles, ftype, outfile): ### Glob for input files to combine. ext = '*.'+ftype molfiles = glob.glob(os.path.join(infiles, ext)) ### Open output file to write molecules. ofs = oechem.oemolostream() if os.path.exists(outfile) and os.path.getsize(outfile) > 10: sys.exit("Output .sdf file already exists. Exiting.\n") return if not ofs.open(outfile): oechem.OEThrow.Fatal("Unable to open %s for writing" % outfile) ### Loop over mols. for f in molfiles: print(f) ifs = oechem.oemolistream() if not ifs.open(f): oechem.OEThrow.Warning("Unable to open %s for reading" % f) try: mol = next(ifs.GetOEMols()) except StopIteration: print('No mol loaded for %s' % mol.GetTitle()) ifs.close() mol.SetTitle(f.split('/')[-1].split('.')[0]) oechem.OEWriteConstMolecule(ofs, mol) ofs.close()
def write_mols(mols_dict, outfile): """ Save all mols in the given dictionary to 'outfile'. Parameters ---------- mols_dict : dict of dicts the first level key is the SMILES string and the value of that key is a dict with the following key/value pairs-- metric geometric measurement structure OEGraphMol of the structure outfile : string name of output file """ # open an outstream file ofs = oechem.oemolostream() if not ofs.open(outfile): oechem.OEThrow.Fatal("Unable to open %s for writing" % outfile) # go through all the molecules for key in mols_dict: print(f"writing out {key}") mymol = mols_dict[key]['structure'] oechem.OEWriteConstMolecule(ofs, mymol)
def smi2sdf(sdfout, smiles): """ From a file containing smiles strings, generate omega conformers, resolve steric clashes, do a quick MM opt, and write SDF output. Parameters ---------- sdfout: str - output sdf file. E.g. "name.sdf" smiles: str - name of the smiles file. E.g. "name.smi" """ ### Read in smiles file. ifs = oechem.oemolistream() if not ifs.open(smiles): oechem.OEThrow.Warning("Unable to open %s for reading" % smiles) ### Open output file to write molecules. ofs = oechem.oemolostream() if os.path.exists(sdfout): #sys.exit("Output .sdf file already exists. Exiting.\n") print("Output .sdf file already exists. Exiting.\n") return if not ofs.open(sdfout): oechem.OEThrow.Fatal("Unable to open %s for writing" % sdfout) ### For each molecule: label atoms, generate 1 conf, write output. for smimol in ifs.GetOEMols(): oechem.OETriposAtomNames(smimol) mol = GenerateConfs(smimol) oechem.OEWriteConstMolecule(ofs, mol) ### Close files. ifs.close() ofs.close()
def main(argv=[__name__]): if len(argv) != 4: oechem.OEThrow.Usage("%s <refmol> <fitmol> <outfile>" % argv[0]) reffs = oechem.oemolistream() if not reffs.open(argv[1]): oechem.OEThrow.Fatal("Unable to open %s for reading" % argv[1]) if not oechem.OEIs3DFormat(reffs.GetFormat()): oechem.OEThrow.Fatal("Invalid input format: need 3D coordinates") refmol = oechem.OEGraphMol() if not oechem.OEReadMolecule(reffs, refmol): oechem.OEThrow.Fatal("Unable to read molecule in %s" % argv[1]) if not refmol.GetDimension() == 3: oechem.OEThrow.Fatal("%s doesn't have 3D coordinates" % refmol.GetTitle()) fitfs = oechem.oemolistream() if not fitfs.open(argv[2]): oechem.OEThrow.Fatal("Unable to open %s for reading" % argv[2]) if not oechem.OEIs3DFormat(fitfs.GetFormat()): oechem.OEThrow.Fatal("Invalid input format: need 3D coordinates") ofs = oechem.oemolostream() if not ofs.open(argv[3]): oechem.OEThrow.Fatal("Unable to open %s for writing" % argv[3]) if not oechem.OEIs3DFormat(ofs.GetFormat()): oechem.OEThrow.Fatal("Invalid output format: need 3D coordinates") oechem.OEWriteConstMolecule(ofs, refmol) oechem.OESuppressHydrogens(refmol) for fitmol in fitfs.GetOEGraphMols(): if not fitmol.GetDimension() == 3: oechem.OEThrow.Warning("%s doesn't have 3D coordinates" % fitmol.GetTitle()) continue MCSAlign(refmol, fitmol, ofs)
def cluster_extraction(ctx): for record in ctx.obj['records']: sys_id = check_sys_id(record) if not record.has_field(Fields.Analysis.oeclus_rec): raise ValueError("Cluster record field is missing") oeclus_rec = record.get_value(Fields.Analysis.oeclus_rec) clust_names = [ "ClusLigAvgMol", "ClusProtAvgMol", "ClusLigMedMol", "ClusProtMedMol" ] ofs = oechem.oemolostream(sys_id + "_clusters.oeb") for fd in oeclus_rec.get_fields(): name = fd.get_name() if name in clust_names: mol = oeclus_rec.get_value(fd) mol.SetTitle(name) oechem.OEWriteConstMolecule(ofs, mol) ofs.close()
def convertSDFfile(reffile, filtfile, writeout): refifs = oechem.oemolistream() filtifs = oechem.oemolistream() ofs = oechem.oemolostream() ### Read in reference file, but don't need its old conformers if not refifs.open(reffile): oechem.OEThrow.Warning("Unable to open %s for reading" % reffile) return ### Read in filtered file and distinguish each molecule's conformers filtifs.SetConfTest( oechem.OEAbsoluteConfTest() ) if not filtifs.open(filtfile): oechem.OEThrow.Warning("Unable to open %s for reading" % filtfile) return ### Open outstream file. if os.path.exists(writeout): print("File already exists: %s. Skip getting results.\n" % (writeout)) return if not ofs.open(writeout): oechem.OEThrow.Fatal("Unable to open %s for writing" % writeout) ### Loop and write molecules. (though refifs should only have ONE mol) for rmol in refifs.GetOEMols(): for fmol in filtifs.GetOEMols(): for i, conf in enumerate( fmol.GetConfs()): rmol.SetCoords(conf.GetCoords()) oechem.OEWriteConstMolecule(ofs, rmol) refifs.close() filtifs.close() ofs.close()
def filter_confs(rmsdfile, tag, rmsdout): """ Read in OEMols (and each of their conformers) in 'rmsdfile'. For each molecule: rough filter conformers based on energy differences specified by 'tag', fine filter conformers based on RMSD values. Parameters ---------- rmsdfile : str Name of SDF file with conformers to be filtered tag : str SD tag name with the energy value to roughly screen conformers before RMSD Screening works by removing conformers of very similar energies, where "similar" is defined by thresE parameter. Examples: - "QM Psi4 Final Opt. Energy (Har) mp2/def-sv(p)" - "QM Psi4 Final Single Pt. Energy (Har) mp2/def-sv(p)" rmsdout : str Name of the output file with filtered conformers """ # Parameters for distinguishing cutoff of conformer similarity thresE = 5.E-4 # declare confs diff & skip RMSD comparison above this threshold thresRMSD = 0.2 # above this threshold (Angstrom), confs are "diff" minima wdir, fname = os.path.split(rmsdfile) numConfsF = open(os.path.join(os.getcwd(), "numConfs.txt"), 'a') numConfsF.write("\n{}\n".format(tag)) # Open file to be processed. rmsd_ifs = oechem.oemolistream() if not rmsd_ifs.open(rmsdfile): oechem.OEThrow.Fatal("Unable to open %s for reading" % rmsdfile) rmsd_ifs.SetConfTest(oechem.OEAbsoluteConfTest()) rmsd_molecules = rmsd_ifs.GetOEMols() # Open outstream file. rmsd_ofs = oechem.oemolostream() if os.path.exists(rmsdout): print("%s output file already exists in %s. Skip filtering.\n" % (rmsdout, os.getcwd())) return if not rmsd_ofs.open(rmsdout): oechem.OEThrow.Fatal("Unable to open %s for writing" % rmsdout) # Identify minima and write output file. for mol in rmsd_molecules: if identify_minima(mol, tag, thresE, thresRMSD): numConfsF.write("%s\t%s\n" % (mol.GetTitle(), mol.NumConfs())) oechem.OEWriteConstMolecule(rmsd_ofs, mol) else: numConfsF.write("%s\t0\n" % (mol.GetTitle())) rmsd_ifs.close() numConfsF.close() rmsd_ofs.close() print("Done filtering %s to %s.\n" % (fname, rmsdout))
def optMMFF(Mol, FF, fname): """ Take an OEMol, conduct an energy minimization, and write this molecule's output to a .mol2 file. Note: the optimization type is BFGS. Parameters ---------- Mol: an OEChem molecule FF: string for OEForceFieldType to use. Either "MMFF94" or "MMFF94S" fname: string name of the output .mol2 file to save molecule. Returns ------- boolean True if the function successfully completed, False otherwise """ tmpmol = oechem.OEMol( Mol) # work on a copy of the molecule # Open output file to write molecule. ofs = oechem.oemolostream() if os.path.exists(fname): print("Output .mol2 file already exists. Skipping.\n") return if not ofs.open(fname): oechem.OEThrow.Fatal("Unable to open %s for writing" % fname) # set general energy options along with the run type specification optSzybki = oeszybki.OESzybkiOptions() optSzybki.SetSolventModel(oeszybki.OESolventModel_Sheffield) optSzybki.SetOptimizerType(oeszybki.OEOptType_BFGS) # set the particular force field if FF == "MMFF94": optSzybki.SetForceFieldType(oeszybki.OEForceFieldType_MMFF94) elif FF == "MMFF94S": optSzybki.SetForceFieldType(oeszybki.OEForceFieldType_MMFF94S) else: print( 'optMMFF failed for %s' % tmpmol.GetTitle() ) return False # create additional dependencies, then perform opt (in if statement) szOpt = oeszybki.OESzybki( optSzybki) # generate minimization engine szResults = oeszybki.OESzybkiResults() # make object to hold szybki results if not szOpt(tmpmol, szResults): print( 'optMMFF failed for %s' % tmpmol.GetTitle() ) return False # write out mol2 file and close the output filestream. oechem.OEWriteConstMolecule(ofs, tmpmol) ofs.close() return True
def hasAmberParams(mol, cmd_string): ofslig = oechem.oemolostream('lig.mol2') ofslig.SetFlavor(oechem.OEFormat_MOL2, oechem.OEOFlavor_MOL2_Forcefield) oechem.OEWriteConstMolecule(ofslig, mol) ofslig.close() os.system(cmd_string) paramsNotSaved = 'Parameter file was not saved' leaplog = open('leap_lig.stdout', 'r').read() hasParams = not paramsNotSaved in leaplog return hasParams
def dump_query(prefix, name, qmol, receptor): """ Writes the Molecule or receptor out to file """ tag = "{0}_{1}.query".format(prefix, name) query_file = "{0}.oeb.gz".format(tag) with oechem.oemolostream(query_file) as ofs: oechem.OEWriteConstMolecule(ofs, qmol) if receptor.IsValid(): receptor_file = "{0}.receptor.oeb.gz".format(tag) oechem.OEWriteReceptorFile(receptor, receptor_file) return tag, query_file
def main(argv=[__name__]): if len(argv) != 5: oechem.OEThrow.Usage("%s <refmol> <fitmol> <outfile> <smarts>" % argv[0]) reffs = oechem.oemolistream() if not reffs.open(argv[1]): oechem.OEThrow.Fatal("Unable to open %s for reading" % argv[1]) if not oechem.OEIs3DFormat(reffs.GetFormat()): oechem.OEThrow.Fatal("Invalid input format: need 3D coordinates") refmol = oechem.OEGraphMol() if not oechem.OEReadMolecule(reffs, refmol): oechem.OEThrow.Fatal("Unable to read molecule in %s" % argv[1]) if not refmol.GetDimension() == 3: oechem.OEThrow.Fatal("%s doesn't have 3D coordinates" % refmol.GetTitle()) fitfs = oechem.oemolistream() if not fitfs.open(argv[2]): oechem.OEThrow.Fatal("Unable to open %s for reading" % argv[2]) if not oechem.OEIs3DFormat(fitfs.GetFormat()): oechem.OEThrow.Fatal("Invalid input format: need 3D coordinates") ofs = oechem.oemolostream() if not ofs.open(argv[3]): oechem.OEThrow.Fatal("Unable to open %s for writing" % argv[3]) if not oechem.OEIs3DFormat(ofs.GetFormat()): oechem.OEThrow.Fatal("Invalid output format: need 3D coordinates") oechem.OEWriteConstMolecule(ofs, refmol) ss = oechem.OESubSearch() if not ss.Init(argv[4]): oechem.OEThrow.Fatal("Unable to parse SMARTS: %s" % argv[4]) oechem.OEPrepareSearch(refmol, ss) if not ss.SingleMatch(refmol): oechem.OEThrow.Fatal("SMARTS fails to match refmol") for fitmol in fitfs.GetOEGraphMols(): if not fitmol.GetDimension() == 3: oechem.OEThrow.Warning("%s doesn't have 3D coordinates" % fitmol.GetTitle()) continue oechem.OEPrepareSearch(fitmol, ss) if not ss.SingleMatch(fitmol): oechem.OEThrow.Warning("SMARTS fails to match fitmol %s" % fitmol.GetTitle()) continue SmartsAlign(refmol, fitmol, ss, ofs)
def SmartsAlign(refmol, fitmol, ss, ofs): unique = True for match1 in ss.Match(refmol, unique): for match2 in ss.Match(fitmol, unique): match = oechem.OEMatch() for mp1, mp2 in zip(match1.GetAtoms(), match2.GetAtoms()): match.AddPair(mp1.target, mp2.target) overlay = True rmat = oechem.OEDoubleArray(9) trans = oechem.OEDoubleArray(3) oechem.OERMSD(refmol, fitmol, match, overlay, rmat, trans) oechem.OERotate(fitmol, rmat) oechem.OETranslate(fitmol, trans) oechem.OEWriteConstMolecule(ofs, fitmol)
def getGaffStructure(self, molecule=None, forcefield=None): if not molecule: molecule = self.molecule if not forcefield: forcefield = self.forcefield # Try to check if tleap is going to fail self.checkCharges(molecule) # Determine formal charge (antechamber needs as argument) chg = 0 for atom in molecule.GetAtoms(): chg += atom.GetFormalCharge() # Write out mol to a mol2 file to process via AmberTools mol2file = tempfile.NamedTemporaryFile(suffix='.mol2') mol2filename = mol2file.name with oechem.oemolostream(mol2filename) as ofs: res = oechem.OEWriteConstMolecule(ofs, molecule) if res != oechem.OEWriteMolReturnCode_Success: raise RuntimeError("Error writing molecule %s to mol2." % molecule.GetTitle()) # Run antechamber to type and parmchk for frcmod # requires openmoltools 0.7.5 or later, which should be conda-installable via omnia gaff_mol2_filename, frcmod_filename = openmoltools.amber.run_antechamber( self.prefix_name, mol2filename, gaff_version=forcefield.lower(), charge_method=None) # Run tleap using specified forcefield prmtop, inpcrd = openmoltools.amber.run_tleap(self.prefix_name, gaff_mol2_filename, frcmod_filename, leaprc='leaprc.%s' % forcefield.lower()) # Load via ParmEd molecule_structure = parmed.amber.AmberParm(prmtop, inpcrd) if self.delete_out_files: os.remove(gaff_mol2_filename) os.remove(frcmod_filename) os.remove(prmtop) os.remove(inpcrd) return molecule_structure
def min_mmff94x(mol, ofs, mmff94s=False): """ Minimize the mol with MMFF94 or MMFF94S force field. Parameters ---------- mol : OpenEye single-conformer molecule ofs : OpenEye output filestream mmff94s : Boolean True to minimize with MMFF94S """ # make copy of the input mol oe_mol = oechem.OEGraphMol(mol) # set general energy options along with the run type specification optSzybki = oeszybki.OESzybkiOptions() optSzybki.SetSolventModel(oeszybki.OESolventModel_NoSolv) optSzybki.SetOptimizerType(oeszybki.OEOptType_BFGS) # minimize with input charges not mmff94(s) charges # https://docs.eyesopen.com/toolkits/python/szybkitk/examples.html#optimization-of-all-conformers-of-a-ligand optSzybki.GetSolventOptions().SetChargeEngine(oequacpac.OEChargeEngineNoOp()) # set the particular force field if mmff94s: sdlabel = "MMFF94S" optSzybki.SetForceFieldType(oeszybki.OEForceFieldType_MMFF94S) else: sdlabel = "MMFF94" optSzybki.SetForceFieldType(oeszybki.OEForceFieldType_MMFF94) # generate minimization engine szOpt = oeszybki.OESzybki(optSzybki) # make object to hold szybki results szResults = oeszybki.OESzybkiResults() # perform minimization if not szOpt(oe_mol, szResults): smilabel = oechem.OEGetSDData(oe_mol, "SMILES QCArchive") print( ' >>> MMFF94x minimization failed for %s\n' % smilabel ) energy = szResults.GetTotalEnergy() # save geometry, save energy as tag, write mol to file oechem.OESetSDData(oe_mol, f"Energy {sdlabel}", str(energy)) oechem.OEWriteConstMolecule(ofs, oe_mol)
def protein_extraction(ctx): for record in ctx.obj['records']: mdrecord = MDDataRecord(record) if not record.has_value(Fields.protein): print("No protein have been found in the selected record") return else: title = mdrecord.get_title fn = title.split('_')[0] + ".oeb" with oechem.oemolostream(fn) as ofs: oechem.OEWriteConstMolecule(ofs, record.get_value(Fields.protein)) print("Protein file generated: {}".format(fn))
def call_opt(infile, outfile): ifs = oechem.oemolistream() if not ifs.open(infile): oechem.OEThrow.Warning("Unable to open %s for reading" % smiles) ofs = oechem.oemolostream() if not ofs.open(outfile): oechem.OEThrow.Fatal("Unable to open %s for writing" % outfile) for mol in ifs.GetOEMols(): oechem.OETriposAtomNames(mol) oechem.OEAddExplicitHydrogens(mol) initialize_confs.quick_opt(mol) oechem.OEWriteConstMolecule(ofs, mol) call_writer(mol) return mol
def min_ffxml(mol, ofs, ffxml): """ Minimize the mol with force field input from FFXML file. Parameters ---------- mol : OpenEye single-conformer molecule ofs : OpenEye output filestream ffxml : string name of FFXML file """ # make copy of the input mol oe_mol = oechem.OEGraphMol(mol) try: # create openforcefield molecule ==> prone to triggering Exception off_mol = Molecule.from_openeye(oe_mol) # load in force field ff = ForceField(ffxml) # create components for OpenMM system topology = Topology.from_molecules(molecules=[off_mol]) # create openmm system ==> prone to triggering Exception #system = ff.create_openmm_system(topology, charge_from_molecules=[off_mol]) system = ff.create_openmm_system(topology) except Exception as e: smilabel = oechem.OEGetSDData(oe_mol, "SMILES QCArchive") print( ' >>> openforcefield failed to create OpenMM system: ' f'{oe_mol.GetTitle()} {smilabel}: {e}') return positions = structure.extractPositionsFromOEMol(oe_mol) # minimize structure with ffxml newpos, energy = run_openmm(topology, system, positions) # save geometry, save energy as tag, write mol to file oe_mol.SetCoords(oechem.OEFloatArray(newpos)) oechem.OESetSDData(oe_mol, "Energy FFXML", str(energy)) oechem.OEWriteConstMolecule(ofs, oe_mol) return
def main(argv=[__name__]): itf = oechem.OEInterface(InterfaceData) if not oechem.OEParseCommandLine(itf, argv): oechem.OEThrow.Fatal("Unable to interpret command line!") rname = itf.GetString("-ref") fname = itf.GetString("-fit") oname = itf.GetString("-out") rifs = oechem.oemolistream() if not rifs.open(rname): oechem.OEThrow.Fatal("Cannot open reference molecule file!") refmol = oechem.OEGraphMol() if not oechem.OEReadMolecule(rifs, refmol): oechem.OEThrow.Fatal("Cannot read reference molecule!") fifs = oechem.oemolistream() if not fifs.open(fname): oechem.OEThrow.Fatal("Cannot open align molecule file!") ofs = oechem.oemolostream() if not ofs.open(oname): oechem.OEThrow.Fatal("Cannot open output file!") if not oechem.OEIs2DFormat(ofs.GetFormat()): oechem.OEThrow.Fatal("Invalid output format for 2D coordinates") oedepict.OEPrepareDepiction(refmol) mcss = oechem.OEMCSSearch(oechem.OEMCSType_Approximate) atomexpr = oechem.OEExprOpts_DefaultAtoms bondexpr = oechem.OEExprOpts_DefaultBonds mcss.Init(refmol, atomexpr, bondexpr) mcss.SetMCSFunc(oechem.OEMCSMaxBondsCompleteCycles()) oechem.OEWriteConstMolecule(ofs, refmol) for fitmol in fifs.GetOEGraphMols(): alignres = oedepict.OEPrepareAlignedDepiction(fitmol, mcss) if alignres.IsValid(): oechem.OEThrow.Info("%s mcs size: %d" % (fitmol.GetTitle(), alignres.NumAtoms())) oechem.OEWriteMolecule(ofs, fitmol) return 0
def convert_extension_separate(infile, presuffix, canonical=False, separate='mol'): """ Convert one molecule file format into another using OpenEye tools. The user may also assign canonical smiles as name before writing output. Separate output into (each mol with all confs) or (each conf). presuffix : list first item contains prefix of output name, last item contains extension ex. ['alkyl', '.xyz'] separate : string 'mol' or 'conf' """ # open input file mols = reader.read_mols(infile) # write to output for i, mol in enumerate(mols): # open output file if separate == 'mol': ofs = oechem.oemolostream() if not ofs.open('{}_{}{}'.format(presuffix[0], str(i), presuffix[1])): oechem.OEThrow.Fatal("Unable to open %s for writing" % outfile) if canonical: smi = oechem.OEMolToSmiles(mol) for j, conf in enumerate(mol.GetConfs()): # open output file if separate == 'conf': ofs = oechem.oemolostream() if not ofs.open('{}_{}_{}{}'.format(presuffix[0], str(i), str(j), presuffix[1])): oechem.OEThrow.Fatal("Unable to open %s for writing" % outfile) if canonical: conf.SetTitle(smi) oechem.OEWriteConstMolecule(ofs, conf) # close filestreams ofs.close()
def ligand_extraction(ctx): for record in ctx.obj['records']: mdrecord = MDDataRecord(record) if not record.has_value(Fields.ligand): print("No ligand have been found in the selected record") return else: title = mdrecord.get_title.split("_")[1:] title = "_".join(title) id = mdrecord.get_flask_id fn = title + "_" + str(id) + ".oeb" with oechem.oemolostream(fn) as ofs: oechem.OEWriteConstMolecule(ofs, record.get_value(Fields.ligand)) print("Ligand file generated: {}".format(fn))
def write_conf_mol(outfn, mol): """ Parameters ---------- outfn : str Filename of the output file mol : OpenEye oemol Single or multi-conformer molecule to write to output """ ofs = oechem.oemolostream() if not ofs.open(outfn): oechem.OEThrow.Fatal("Unable to open %s for writing" % outfn) oechem.OEWriteConstMolecule(ofs, mol) ofs.close()
def makeOEB(oemolList, tag): """ Take mollist and create oeb file using the tag as the .oeb file name Parameters ---------- molList : List of objects List of oemols with datatags generated in genData function tag : String Title of the oeb file Returns ------- """ ofile = oechem.oemolostream(tag + ".oeb") for mol in oemolList: oechem.OEWriteConstMolecule(ofile, mol) ofile.close() return
def write(self, filePath, oeMol, constantMol=False, addSdTags=True): """Write an oeMol with format type inferred from the filePath extension (e.g. .mol) Args: filePath (str): filepath with a chemical type extension constantMol (bool, optional): copies molecule before performing format specific perceptions Returns: bool: True for success or False otherwise """ try: molId = os.path.splitext(os.path.basename(filePath))[0] fmt = os.path.splitext(os.path.basename(filePath))[1][1:].lower() # if addSdTags: oemf = OeMoleculeFactory() oemf.setOeMol(oeMol, molId) oemf.addSdTags() oeMol = oemf.getMol() # self.__mU.mkdir(os.path.dirname(filePath)) ofs = oechem.oemolostream() ofs.open(filePath) logger.debug("Writing (fmt=%s) molId %s path %s title %s", fmt, molId, filePath, oeMol.GetTitle()) # if constantMol: oechem.OEWriteConstMolecule(ofs, oeMol) else: oechem.OEWriteMolecule(ofs, oeMol) # # If this is a mol2 file, we need to replace the resname if fmt.startswith("mol2"): # If this is a mol2/mol2h substitute the default substructure id with open(filePath, "r", encoding="utf-8") as ifh: lines = ifh.readlines() lines = [line.replace("<0>", molId) for line in lines] with open(filePath, "w", encoding="utf-8") as ofh: ofh.writelines(lines) return True except Exception as e: logger.exception("Failing for %s with %s", filePath, str(e)) return False
def smi2indivSdf(wdir, smiles): """ From a file containing smiles strings, generate omega conformers, resolve steric clashes, do a quick MM opt, and write SDF output as individual files. Parameters ---------- wdir: str - working directory containing .smi file smiles: str - name of the smiles file. E.g. "name.smi" """ os.chdir(wdir) ### Read in smiles file. ifs = oechem.oemolistream() if not ifs.open(smiles): oechem.OEThrow.Warning("Unable to open %s for reading" % smiles) ### For each molecule: label atoms, generate 1 conf, write output. for i, smimol in enumerate(ifs.GetOEMols()): oechem.OETriposAtomNames(smimol) mol = GenerateConfs(smimol) ### Open output file to write molecules. sdfout = smiles.split('.')[0] + '-%d.sdf' %(i) ofs = oechem.oemolostream() if os.path.exists(sdfout): #sys.exit("Output .sdf file already exists. Exiting.\n") print("Output .sdf file already exists. Exiting.\n") return if not ofs.open(sdfout): oechem.OEThrow.Fatal("Unable to open %s for writing" % sdfout) oechem.OEWriteConstMolecule(ofs, mol) ofs.close() ### Close files. ifs.close()