def get_sd_data(mol, tag):
    try:
        if oechem.OEHasSDData(mol, tag):
            return oechem.OEGetSDData(mol, tag)
        if oechem.OEHasSDData(mol.GetActive(), tag):
            return oechem.OEGetSDData(mol.GetActive(), tag)
    except AttributeError as e:
        print(e)
        return ""
Exemple #2
0
 def GetScoreToCmp(mol):
     if oechem.OEHasSDData(mol, "ShapeTanimoto"):
         # sort by shape tanimoto
         if oechem.OEHasSDData(mol, "TanimotoCombo"):
             return float(oechem.OEGetSDData(mol, "TanimotoCombo"))
         return float(oechem.OEGetSDData(mol, "ShapeTanimoto"))
     else:
         # sort by shape tversky
         if oechem.OEHasSDData(mol, "TverskyCombo"):
             return float(oechem.OEGetSDData(mol, "TverskyCombo"))
         return float(oechem.OEGetSDData(mol, "ShapeTversky"))
Exemple #3
0
def extractXY(fname, tag):

    ifs = oechem.oemolistream()
    ifs.SetConfTest(oechem.OEAbsoluteConfTest())
    if not ifs.open(fname):
        oechem.OEThrow.Warning("Unable to open %s for reading" % fname)

    xlist = []
    ylist = []

    for mol in ifs.GetOEMols():
        for j, conf in enumerate(mol.GetConfs()):
            xlist.append(int(mol.GetTitle()))
            try:
                ylist.append(float(oechem.OEGetSDData(conf, tag)))
            except ValueError as e:
                print("Missing tag data for mol {}, conf {}! Skipping.".format(
                    mol.GetTitle(), j))
                print(e)
                xlist.pop()

    ### convert to numpy array, take relative e, convert to kcal/mol
    ylist = np.array(ylist)
    ylist = ylist - ylist[0]
    ylist = 627.5095 * ylist
    return xlist, ylist
def main(**kwargs):
    outfn = os.path.splitext(opt['infn'])[0] + '_' + opt['suffix'] + '.mol2'
    success = False

    ### Read in .sdf file and distinguish each molecule's conformers
    ifs = oechem.oemolistream()
    ifs.SetConfTest(oechem.OEAbsoluteConfTest())
    if not ifs.open(opt['infn']):
        oechem.OEThrow.Warning("Unable to open %s for reading" % opt['infn'])
        return

    for mol in ifs.GetOEMols():
        if mol.GetTitle() == opt['title']:
            # write out all confs in this mol if no SD tag is specified
            if opt['sdtag'] == "":
                success = True
                write_conf_mol(outfn, mol)
                return
            # look for the conformer in this mol with specific SD tag value
            for i, conf in enumerate(mol.GetConfs()):
                if oechem.OEGetSDData(conf, opt['sdtag']) == opt['value']:
                    success = True
                    write_conf_mol(outfn, conf)
    if not success:
        print("\n** Found no confs matching your criteria. **")
    ifs.close()
Exemple #5
0
 def nfragments_score(molecule):
     overlapping_fragments = oechem.OEGetSDData(molecule,
                                                'overlapping_fragments')
     if overlapping_fragments == '':
         return 0
     else:
         return -len(overlapping_fragments.split(','))
Exemple #6
0
def SDF2CSV(ifs, csv):
    taglist = []
    # read through once to find all unique tags
    for mol in ifs.GetOEGraphMols():
        for dp in oechem.OEGetSDDataPairs(mol):
            if dp.GetTag() not in taglist:
                taglist.append(dp.GetTag())

    ifs.rewind()
    # print out column labels
    header = "Title"
    for tag in taglist:
        header += ",%s" % tag
    header += '\n'
    csv.write(header)

    # build csv file
    for mol in ifs.GetOEGraphMols():
        line = [mol.GetTitle()]
        for tag in taglist:
            if oechem.OEHasSDData(mol, tag):
                value = oechem.OEGetSDData(mol, tag)
            else:
                value = ''
            line.append(',')
            line.append(value)
        csv.write(''.join(line))
        csv.write('\n')
Exemple #7
0
def generate_restricted_conformers_star(args):
    core_smarts_list = [
        'C1(CCOc2ccccc12)C(=O)Nc1cncc2ccccc12',  # benzopyran-linker-isoquinoline
        'C(=O)Nc1cncc2ccccc12',  # linker-isoquinoline
    ]

    mols = [
        generate_restricted_conformers(*args, core_smarts=core_smarts)
        for core_smarts in core_smarts_list
    ]

    # Sprint 5 special: Select unproblematic enantiomers by energy
    if mols[1] is None:
        return mols[0]

    try:
        from openeye import oechem
        energies = [
            float(oechem.OEGetSDData(mol, 'MMFF_internal_energy'))
            for mol in mols
        ]
        if energies[0] > energies[1] + 100:
            return mols[1]
        else:
            return mols[0]
    except Exception as e:
        print(e)
        return None
    def get_result(self, query_id):
        cur_rank = list()

        url = self.args.url + "/queries/{}/".format(query_id)
        response = None
        tries = 0
        while response == None or data["status"]["job"] != "COMPLETED":
            time.sleep(60 * tries)
            tries += 1
            response = requests.get(url)
            data = response.json()
        results_url = data["results"]
        results_data = requests.get(self.args.url + results_url)

        with tempfile.NamedTemporaryFile(suffix='.oeb',
                                         mode='wb',
                                         delete=False) as temp:
            temp.write(results_data.content)
            temp.flush()
            with oechem.oemolistream(temp.name) as results:
                for mol in results.GetOEGraphMols():
                    cur_rank.append(
                        (oechem.OEMolToSmiles(mol), mol.GetTitle(),
                         float(oechem.OEGetSDData(mol, 'TanimotoCombo')),
                         self.baitset[0], False))
            os.remove(temp.name)
        return cur_rank
    def get_results(self):
        self.cur_scores = {}

        for query_id in self.query_id_list:
            url = self.args.url + "/queries/{}/".format(query_id)
            response = None
            tries = 0
            while response == None or data["status"]["job"] != "COMPLETED":
                tries += 1
                time.sleep(tries)
                response = requests.get(url)
                data = response.json()
            results_url = data["results"]
            results_data = requests.get(self.args.url + results_url)

            with tempfile.NamedTemporaryFile(suffix='.oeb',
                                             mode='wb',
                                             delete=False) as temp:
                temp.write(results_data.content)
                temp.flush()
                with oechem.oemolistream(temp.name) as results:
                    for mol in results.GetOEGraphMols():
                        if self.dataset_infos[1][
                                mol.GetTitle()] not in self.baitset[1]:
                            tanimoto_combo = float(
                                oechem.OEGetSDData(mol, "TanimotoCombo"))
                            if mol.GetTitle() in self.cur_scores.keys():
                                if self.cur_scores[
                                        mol.GetTitle()][0] < tanimoto_combo:
                                    self.cur_scores[mol.GetTitle()] = (
                                        tanimoto_combo, mol.CreateCopy())
                            else:
                                self.cur_scores[mol.GetTitle()] = (
                                    tanimoto_combo, mol.CreateCopy())
                os.remove(temp.name)
Exemple #10
0
def score(mol):
    text = oechem.OEGetSDData(mol, 'docking_score')
    try:
        score = float(text)
        return score
    except Exception as e:
        return 0
Exemple #11
0
def RenderData(image, mol, tags):
    from openeye import oechem
    from openeye import oedepict

    data = []
    for tag in tags:
        value = "N/A"
        if oechem.OEHasSDData(mol, tag):
            value = oechem.OEGetSDData(mol, tag)
        data.append((tag, value))

    nrdata = len(data)

    tableopts = oedepict.OEImageTableOptions(
        nrdata, 2, oedepict.OEImageTableStyle_LightBlue)
    tableopts.SetColumnWidths([10, 20])
    tableopts.SetMargins(2.0)
    tableopts.SetHeader(False)
    tableopts.SetStubColumn(True)
    table = oedepict.OEImageTable(image, tableopts)

    for row, (tag, value) in enumerate(data):
        cell = table.GetCell(row + 1, 1)
        table.DrawText(cell, tag + ":")
        cell = table.GetBodyCell(row + 1, 1)
        table.DrawText(cell, value)
def generate_restricted_conformers_star(args):
    core_smarts_list = [
        'C1(CCOc2ccccc12)C(=O)Nc1cncc2ccccc12', # benzopyran-linker-isoquinoline
        'CNc1cncc2ccccc12', # linker-isoquinoline
        'c1cncc2ccccc12', # isoquinoline
    ]

    # Build with all core_smarts options
    mols = [ generate_restricted_conformers(*args, core_smarts=core_smarts) for core_smarts in core_smarts_list ]

    # Prune unsuccessful builds
    mols = [ mol for mol in mols if mol!=None ]

    # DEBUG
    if len(mols) == 0:
        #  No options available
        logging.warning('No core_smarts variations succeeded.')
        return None

    if len(mols) == 1:
        # Return the only choice
        return mols[0]

    try:
        # Prefer the second option if strain energy of first option is poor
        from openeye import oechem
        energies = [ float(oechem.OEGetSDData(mol, 'MMFF_internal_energy')) for mol in mols ]
        if energies[0] > energies[1] + 100:
            return mols[1]
        else:
            return mols[0]
    except Exception as e:
        logging.warning(e)
        return None
Exemple #13
0
def min_ffxml(mol, ffxml):

    # 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}'")
        print(f"{e}\n")
        return

    print(" >>> successful OpenMM system creation for openforcefield "
         f"mol \"{oe_mol.GetTitle()}\"")
Exemple #14
0
def generate_training_input(mol_file):
    '''


    :param mol_file: str
    :return: pd.DataFrame
    '''
    ifs = oechem.oemolistream(mol_file)
    training_data = []
    for mol in ifs.GetOEGraphMols():
        energy = float(oechem.OEGetSDData(mol, ENERGY_KEY))
        sf_elements = get_sf_elements(mol)
        dihe_inchi = get_dihedral_inchi_key(mol)

        data = [dihe_inchi, energy]
        data.extend(sf_elements)
        training_data.append(data)

    ifs.close()

    columns = [INCHI_KEY, ENERGY_KEY]
    num_sf_elements = len(training_data[0]) - 2
    sf_columns = ['sf_%d' % (i + 1) for i in range(num_sf_elements)]
    columns.extend(sf_columns)

    df = pd.DataFrame(training_data, columns=columns)

    # calculate relative energy for each profile
    grouped = df.loc[:, [INCHI_KEY, ENERGY_KEY]].groupby(INCHI_KEY)
    df2 = grouped.transform(lambda x: x - x.min())
    df[ENERGY_KEY] = df2[ENERGY_KEY]

    return df
def has_ic50(mol):
    """Return True if this molecule has fluorescence IC50 data"""
    from openeye import oechem
    try:
        pIC50 = oechem.OEGetSDData(mol, 'f_avg_pIC50')
        pIC50 = float(pIC50)
        return True
    except Exception as e:
        return False
Exemple #16
0
def main(argv=[__name__]):
    itf = oechem.OEInterface(InterfaceData, argv)
    if not (itf.HasDouble("-min") or itf.HasDouble("-max")):
        oechem.OEThrow.Fatal("Please set a filter value with -min or -max")

    ifs = oechem.oemolistream()
    if not ifs.open(itf.GetString("-i")):
        oechem.OEThrow.Fatal("Unable to open %s for reading" %
                             itf.GetString("-i"))

    if not oechem.OEIsSDDataFormat(ifs.GetFormat()):
        oechem.OEThrow.Fatal(
            "Only works for input file formats that support SD data (sdf,oeb,csv)"
        )

    ofs = oechem.oemolostream()
    if not ofs.open(itf.GetString("-o")):
        oechem.OEThrow.Fatal("Unable to open %s for writing" %
                             itf.GetString("-i"))

    if not oechem.OEIsSDDataFormat(ofs.GetFormat()):
        oechem.OEThrow.Fatal(
            "Only works for output file formats that support SD data \
                             (sdf,oeb,csv)")

    tag = itf.GetString("-tag")

    minval = float("-inf")
    if itf.HasDouble("-min"):
        minval = itf.GetDouble("-min")

    maxval = float("inf")
    if itf.HasDouble("-max"):
        maxval = itf.GetDouble("-max")

    for mol in ifs.GetOEGraphMols():
        if not oechem.OEHasSDData(mol, tag):
            oechem.OEThrow.Warning("Unable to find %s tag on %s" %
                                   (tag, mol.GetTitle()))
            continue

        value = oechem.OEGetSDData(mol, tag)
        try:
            tagvalue = float(value)
        except ValueError:
            oechem.OEThrow.Warning("Failed to convert (%s) to a number in %s" %
                                   (value, mol.GetTitle()))
            continue

        if tagvalue < minval:
            continue

        if tagvalue > maxval:
            continue

        oechem.OEWriteMolecule(ofs, mol)
Exemple #17
0
def Rename(ifs, ofs, fieldname):
    for mol in ifs.GetOEGraphMols():
        if oechem.OEHasSDData(mol, fieldname):
            mol.SetTitle(oechem.OEGetSDData(mol, fieldname))
        else:
            title = mol.GetTitle()
            oechem.OEThrow.Warning(
                "Renaming of molecule %s failed; no field %s" %
                (title, fieldname))
        oechem.OEWriteMolecule(ofs, mol)
def dock_molecule(molecule, default_receptor='x0387'):
    """
    Dock the specified molecules, writing out to specified file

    Parameters
    ----------
    molecule : OEMol
        The molecule to dock
    default_receptor : str, optional, default='0387'
        The default receptor to dock to

    Returns
    -------
    all_docked_molecules : list of OEMol
        All docked molecules
    """
    import os
    import oechem

    # Make a copy of the molecule
    molecule = oechem.OEMol(molecule)

    # Extract list of corresponding receptor(s)
    fragments = list()
    fragments = oechem.OEGetSDData(molecule, "fragments").split(',')
    fragments = [
        fragment for fragment in fragments
        if os.path.exists(f'../receptors/Mpro-{fragment}-receptor.oeb.gz')
    ]

    if len(fragments) == 0:
        fragments = [default_receptor]

    # Dock them
    all_docked_molecules = list()
    for fragment in fragments:
        molecule_to_dock = oechem.OEMol(molecule)

        import os
        receptor_filename = os.path.join(
            f'../receptors/Mpro-{fragment}-receptor.oeb.gz')
        oechem.OESetSDData(molecule_to_dock, "fragments", fragment)

        # Enumerate reasonable protomers/tautomers
        from openeye import oequacpac
        protomer = oechem.OEMol()
        protomers = [
            oechem.OEMol(protomer) for protomer in
            oequacpac.OEGetReasonableProtomers(molecule_to_dock)
        ]
        docked_molecules = dock_molecules_to_receptor(receptor_filename,
                                                      protomers)
        all_docked_molecules += docked_molecules

    return all_docked_molecules
def is_active(molecule):
    """
    """
    from openeye import oechem
    value = oechem.OEGetSDData(molecule, 'IC50 (uM)')
    try:
        IC50 = float(value)
        if (IC50 < 99.0):
            return True
    except:
        pass

    return False
Exemple #20
0
def GetUrlSDF2SMI(url, fout, ntries=20, poll_wait=10):
    '''Get PubChem SDF.GZ, convert to SMILES using the SD tag PUBCHEM_OPENEYE_CAN_SMILES
or PUBCHEM_OPENEYE_ISO_SMILES, and PUBCHEM_COMPOUND_CID or PUBCHEM_COMPOUND_SID for name.'''
    import openeye.oechem as oechem

    def HandleOEErrors(oeerrs, nowarn):
        errstr = oeerrs.str()
        for line in errstr.split('\n'):
            if not line.rstrip(): continue
            if re.search('Warning', line, re.I) and nowarn: continue
            sys.stderr.write("%s\n" % line)
        oeerrs.clear()

    fout_tmp = tempfile.NamedTemporaryFile(prefix='pubchem_ftp_',
                                           suffix='.sdf.gz',
                                           delete=False)
    GetUrl(url, fout_tmp, ntries, poll_wait)
    fpath_tmp = fout_tmp.name
    logging.debug('fpath_tmp = %s' % fpath_tmp)
    fout_tmp.close()

    ims = oechem.oemolistream(fpath_tmp)
    ims.SetFormat(oechem.OEFormat_SDF)
    ims.Setgz(True)

    mol = oechem.OEGraphMol()
    nbytes = 0
    oeerrs = oechem.oeosstream()
    oechem.OEThrow.SetOutputStream(oeerrs)
    while oechem.OEReadMolecule(ims, mol):
        cid = oechem.OEGetSDData(mol, 'PUBCHEM_COMPOUND_CID')
        cansmi = oechem.OEGetSDData(mol, 'PUBCHEM_OPENEYE_CAN_SMILES')
        isosmi = oechem.OEGetSDData(mol, 'PUBCHEM_OPENEYE_ISO_SMILES')
        buff = ("%s %s\n" % (isosmi, cid))
        fout.write(buff)
        nbytes += len(buff)
        HandleOEErrors(oeerrs, True)
    os.remove(fpath_tmp)
    return nbytes
Exemple #21
0
def read_molecules(file_path, verbose=True):
    """
    Read molecules from an OpenEye-supported file.

    Parameters
    ----------
    file_path : str
        Filename from which molecules are to be read (e.g. mol2, sdf)

    Returns
    -------
    molecules : list of OEMol
        List of molecules read from file

    """
    warnings.warn(DEPRECATION_WARNING_TEXT, PendingDeprecationWarning)

    from openeye import oechem
    from openforcefield.utils import get_data_file_path

    if not os.path.exists(file_path):
        built_in = get_data_file_path(f"molecules/{file_path}")
        if not os.path.exists(built_in):
            raise Exception(f"File '{file_path}' not found.")
        file_path = built_in

    if verbose: print(f"Loading molecules from '{file_path}'...")
    start_time = time.time()
    molecules = list()
    input_molstream = oechem.oemolistream(file_path)

    flavor = oechem.OEIFlavor_Generic_Default | oechem.OEIFlavor_MOL2_Default | oechem.OEIFlavor_MOL2_Forcefield
    input_molstream.SetFlavor(oechem.OEFormat_MOL2, flavor)

    molecule = oechem.OECreateOEGraphMol()
    while oechem.OEReadMolecule(input_molstream, molecule):
        # If molecule has no title, try getting SD 'name' tag
        if molecule.GetTitle() == '':
            name = oechem.OEGetSDData(molecule, 'name').strip()
            molecule.SetTitle(name)
        # Append to list.
        molecule_copy = oechem.OEMol(molecule)
        molecules.append(molecule_copy)
    input_molstream.close()
    if verbose: print(f"{len(molecules)} molecules read")
    end_time = time.time()
    elapsed_time = end_time - start_time
    if verbose: print(f"{elapsed_time:.3f} s elapsed")

    return molecules
Exemple #22
0
def plotSDF(infile, tag, figname='lineplot.png'):
    """
    Parameters
    ----------
    """

    ### Read in .sdf file and distinguish each molecule's conformers
    ifs = oechem.oemolistream()
    ifs.SetConfTest(oechem.OEAbsoluteConfTest())
    if not ifs.open(infile):
        oechem.OEThrow.Warning("Unable to open %s for reading" % infile)
        return

    xlist = []
    ylist = []

    for mol in ifs.GetOEMols():
        print(mol.GetTitle(), mol.NumConfs())
        for j, conf in enumerate(mol.GetConfs()):
            try:
                ylist.append(float(oechem.OEGetSDData(conf, tag)))
                xlist.append(int(mol.GetTitle()))
            except ValueError as err:
                pass  # mols not converged may not have tag

    ### convert to numpy array, take relative e, convert to kcal/mol
    ylist = np.array(ylist)
    ylist = ylist - ylist[0]
    ylist = 627.5095 * ylist

    ### Plot.
    xlabel = 'conformation number'
    ylabel = "Relative energy (kcal/mol)"

    fig = plt.figure()
    #    ax = fig.gca()
    #    ax.set_xticks(np.arange(-1,RefNumConfs+1,2))

    plt.ylabel(ylabel, fontsize=14)
    plt.xlabel(xlabel, fontsize=14)
    plt.scatter(xlist, ylist)
    #    plt.plot(xlist, ylist)
    plt.ylim(-1, 16)
    #    plt.xticks(range(RefNumConfs),xlabs,fontsize=12)
    #    plt.yticks(fontsize=12)
    plt.grid()

    plt.savefig(figname, bbox_inches='tight')
    plt.show()
Exemple #23
0
def find_string_tag(infile):
    # read input file
    ifs = oechem.oemolistream()
    ifs.SetConfTest(oechem.OEAbsCanonicalConfTest())
    if not ifs.open(infile):
        oechem.OEThrow.Warning("Unable to open input file for reading")
    # loop through and evaluate tags
    for mol in ifs.GetOEMols():
        for conf in mol.GetConfs():
            mytag = oechem.OEGetSDData(conf, 'SMILES QCArchive')
            count1 = mytag.count('S')
            count2 = mytag.count('P')
            count3 = mytag.count('C#N')
            count4 = mytag.count('N/N')
            print(f"{conf.GetTitle()}\t{count1}\t{count2}\t{count3}\t{count4}")
Exemple #24
0
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)
Exemple #25
0
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 dock_molecule(molecule, ofs, default_receptor='x0387'):
    """
    Dock the specified molecules, writing out to specified file

    Parameters
    ----------
    molecule : OEMol
        The molecule to dock
    ofs : oechem.oemolostream
        The filename to stream docked molecules to
    default_receptor : str, optional, default='0387'
        The default receptor to dock to
    """
    # Make a copy of the molecule
    molecule = oechem.OEMol(molecule)

    import os
    # Extract list of corresponding receptor(s)
    import oechem
    print(f'\n{molecule.GetTitle()}')
    if oechem.OEHasSDData(molecule, "fragments"):
        fragments = oechem.OEGetSDData(molecule, "fragments").split(',')
        print(f'fragments before filter: {fragments}')
        fragments = [
            fragment for fragment in fragments
            if os.path.exists(f'../receptors/Mpro-{fragment}-receptor.oeb.gz')
        ]
        print(f'fragments after filter: {fragments}')
        if len(fragments) == 0:
            fragments = [default_receptor]
        for fragment in fragments:
            molecule_to_dock = oechem.OEMol(molecule)

            import os
            receptor_filename = os.path.join(
                f'../receptors/Mpro-{fragment}-receptor.oeb.gz')
            oechem.OESetSDData(molecule_to_dock, "fragments", fragment)

            # Enumerate reasonable protomers/tautomers
            from openeye import oequacpac
            protomer = oechem.OEMol()
            protomers = [
                oechem.OEMol(protomer) for protomer in
                oequacpac.OEGetReasonableProtomers(molecule_to_dock)
            ]
            dock_molecules_to_receptor(receptor_filename, protomers, ofs)
Exemple #27
0
def mols_from_file(mol_file):
    """
    Parses a standard molecule file into chemper molecules using OpenEye toolkits

    Parameters
    ----------
    mol_file: str
              relative or full path to molecule containing the molecule file
              that is accessible from the current working directory

    Returns
    -------
    mols: list of chemper Mols
          list of molecules in the mol2 file as chemper Mols
    """
    import os
    if not os.path.exists(mol_file):
        from chemper.chemper_utils import get_data_path
        mol_path = get_data_path(os.path.join('molecules', mol_file))

        if not os.path.exists(mol_path):
            raise IOError(
                "File '%s' not found locally or in chemper/data/molecules." %
                mol_file)
        else:
            mol_file = mol_path

    molecules = list()

    # make Openeye input file stream
    ifs = oechem.oemolistream(mol_file)

    oemol = oechem.OECreateOEGraphMol()
    while oechem.OEReadMolecule(ifs, oemol):
        # if an SD file, the molecule name may be in the SD tags
        if oemol.GetTitle() == '':
            name = oechem.OEGetSDData(oemol, 'name').strip()
            oemol.SetTitle(name)
        # Append to list.
        molecules.append(Mol(oechem.OEMol(oemol)))
    ifs.close()

    return molecules
def main(infile):

    # open multi-molecule, multi-conformer file
    ifs = oechem.oemolistream()
    ifs.SetConfTest(oechem.OEAbsCanonicalConfTest())
    if not ifs.open(infile):
        raise FileNotFoundError(f"Unable to open {infile} for reading")
    mols = ifs.GetOEMols()

    for i, mol in enumerate(mols):

        # perceive stereochemistry for mol
        oechem.OEPerceiveChiral(mol)
        oechem.OEAssignAromaticFlags(mol, oechem.OEAroModel_MDL)

        # assign charges to copy of mol
        # note that chg_mol does NOT have conformers
        try:
            chg_mol = charge_mol(mol)

        except RuntimeError:

            # perceive stereochem
            #find_unspecified_stereochem(mol)
            oechem.OE3DToInternalStereo(mol)

            # reset perceived and call OE3DToBondStereo, since it may be missed
            # by OE3DToInternalStereo if it thinks mol is flat
            mol.ResetPerceived()
            oechem.OE3DToBondStereo(mol)

            try:
                chg_mol = charge_mol(mol)
                print(f'fixed stereo: {mol.GetTitle()}')
            except RuntimeError:
                find_unspecified_stereochem(mol)

                title = mol.GetTitle()
                smilabel = oechem.OEGetSDData(mol, "SMILES QCArchive")
                print(' >>> Charge assignment failed due to unspecified '
                      f'stereochemistry {title} {smilabel}')

                continue
Exemple #29
0
def get_fragment_to_parent_atom_mapping(parent_mol, frag_mol):
    try:
        mapping_data = oechem.OEGetSDData(frag_mol,
                                          FRAGMENT_TO_PARENT_ATOMS_KEY)
        idx_map = dict(
            map(int, idx_pair.split('_'))
            for idx_pair in mapping_data.split('-'))
        atom_map = {}
        for frag_idx, parent_idx in idx_map.items():
            frag_atom = frag_mol.GetAtom(oechem.OEHasAtomIdx(frag_idx - 1))
            parent_atom = parent_mol.GetAtom(
                oechem.OEHasAtomIdx(parent_idx - 1))
            if frag_atom is not None and parent_atom is not None:
                atom_map[frag_atom] = parent_atom

        return atom_map

    except Exception as e:
        logging.warning(e)
        return {}
def reorder_sd_props(mol: oechem.OEGraphMol):
    strain1 = oechem.OEGetSDData(mol, TOTAL_STRAIN_TAG)

    data_pairs = [(TOTAL_STRAIN_TAG, strain1)]
    for dp in oechem.OEGetSDDataPairs(mol):
        if dp.GetTag() == TOTAL_STRAIN_TAG:
            pass
        else:
            data_pairs.append((dp.GetTag(), dp.GetValue()))

    oechem.OEClearSDData(mol)

    for k, v in data_pairs:
        oechem.OESetSDData(mol, k, v)

    data_pairs = []
    for dp in oechem.OEGetSDDataPairs(mol):
        data_pairs.append((dp.GetTag(), dp.GetValue()))
    oechem.OEClearSDData(mol)

    for k, v in data_pairs:
        oechem.OESetSDData(mol, k, v)