Esempio n. 1
0
def generate_ligands_figure(molecules, figsize=None, filename='ligands.png'):
    """ Plot an image with all of the ligands passed in

    Parameters
    ----------
    molecules : list
        list of openeye.oemol objects
    figsize : list or tuple
        list or tuple of len() == 2 of the horizontal and vertical lengths of image
    filename : string
        name of file to save the image

    Returns
    -------

    """
    from openeye import oechem, oedepict

    to_draw = []
    for lig in molecules:
        oedepict.OEPrepareDepiction(lig)
        to_draw.append(oechem.OEGraphMol(lig))

    dim = int(np.ceil(len(to_draw)**0.5))

    if figsize is None:
        x_len = 1000 * dim
        y_len = 500 * dim
        image = oedepict.OEImage(x_len, y_len)
    else:
        assert (len(figsize) == 2
                ), "figsize arguement should be a tuple or list of length 2"
        image = oedepict.OEImage(figsize[0], figsize[1])

    rows, cols = dim, dim
    grid = oedepict.OEImageGrid(image, rows, cols)

    opts = oedepict.OE2DMolDisplayOptions(grid.GetCellWidth(),
                                          grid.GetCellHeight(),
                                          oedepict.OEScale_AutoScale)

    minscale = float("inf")
    for mol in to_draw:
        minscale = min(minscale, oedepict.OEGetMoleculeScale(mol, opts))
    #     print(mol.GetTitle())

    opts.SetScale(minscale)
    for idx, cell in enumerate(grid.GetCells()):
        mol = to_draw[idx]
        disp = oedepict.OE2DMolDisplay(mol, opts)
        oedepict.OERenderMolecule(cell, disp)

    oedepict.OEWriteImage(filename, image)

    return
Esempio n. 2
0
def visualize_mols(smiles,
                   fname,
                   rows,
                   cols,
                   bond_idx,
                   wbos,
                   colors,
                   align_to=0):
    """
    Visualize molecules with highlighted bond and labeled with WBO
    Parameters
    ----------
    smiles : list of SMILES to visualize.
        bond atoms should have map indices
    fname : str
        filename
    rows : int
    cols : int
    bond_idx : tuple of atom maps of bond to highlight.
    wbos : list of floats
    colors : list of hex values for colors
    align_to: int, optional, default 0
        index for which molecule to align to. If zero, will align to first molecules in SMILES list

    """
    itf = oechem.OEInterface()

    ropts = oedepict.OEReportOptions(rows, cols)
    ropts.SetHeaderHeight(25)
    ropts.SetFooterHeight(25)
    ropts.SetCellGap(2)
    ropts.SetPageMargins(10)
    report = oedepict.OEReport(ropts)

    cellwidth, cellheight = report.GetCellWidth(), report.GetCellHeight()
    opts = oedepict.OE2DMolDisplayOptions(cellwidth, cellheight,
                                          oedepict.OEScale_AutoScale)
    oedepict.OESetup2DMolDisplayOptions(opts, itf)

    # align to chosen molecule
    ref_mol = oechem.OEGraphMol()
    oechem.OESmilesToMol(ref_mol, smiles[align_to])
    oedepict.OEPrepareDepiction(ref_mol)

    mols = []
    minscale = float("inf")
    for s in smiles:
        mol = oechem.OEMol()
        oechem.OESmilesToMol(mol, s)
        mols.append(mol)
        oedepict.OEPrepareDepiction(mol, False, True)
        minscale = min(minscale, oedepict.OEGetMoleculeScale(mol, opts))
        print(minscale)

    print(minscale)
    opts.SetScale(minscale)
    for i, mol in enumerate(mols):

        cell = report.NewCell()
        oedepict.OEPrepareDepiction(mol, False, True)
        bond = get_bond(mol, bond_idx)
        atom_bond_set = oechem.OEAtomBondSet()
        atom_bond_set.AddAtoms([bond.GetBgn(), bond.GetEnd()])
        atom_bond_set.AddBond(bond)

        hstyle = oedepict.OEHighlightStyle_BallAndStick
        hcolor = oechem.OEColor(*colors[i])

        overlaps = oegraphsim.OEGetFPOverlap(
            ref_mol, mol, oegraphsim.OEGetFPType(oegraphsim.OEFPType_Tree))
        oedepict.OEPrepareMultiAlignedDepiction(mol, ref_mol, overlaps)

        #opts.SetBondPropLabelFontScale(4.0)
        disp = oedepict.OE2DMolDisplay(mol, opts)
        oedepict.OEAddHighlighting(disp, hcolor, hstyle, atom_bond_set)

        #font = oedepict.OEFont(oedepict.OEFontFamily_Default, oedepict.OEFontStyle_Bold, 12,
        #                       oedepict.OEAlignment_Default, oechem.OEBlack)
        bond_label = oedepict.OEHighlightLabel("{:.2f}".format((wbos[i])),
                                               hcolor)
        bond_label.SetFontScale(1.4)
        #bond_label.SetFont(font)

        oedepict.OEAddLabel(disp, bond_label, atom_bond_set)
        oedepict.OERenderMolecule(cell, disp)
        # oedepict.OEDrawCurvedBorder(cell, oedepict.OELightGreyPen, 10.0)

    return (oedepict.OEWriteReport(fname, report))
Esempio n. 3
0
def visualize_bond_atom_sensitivity(mols,
                                    bonds,
                                    scores,
                                    fname,
                                    rows,
                                    cols,
                                    atoms=None,
                                    min_scale=True):
    """

    Parameters
    ----------
    mols :
    bonds :
    scores :
    fname :
    wbos :
    rows :
    cols :
    atoms :
    height :
    width :

    Returns
    -------

    """

    itf = oechem.OEInterface()
    ropts = oedepict.OEReportOptions(rows, cols)
    ropts.SetHeaderHeight(0.01)
    ropts.SetFooterHeight(0.01)
    ropts.SetCellGap(0.0001)
    ropts.SetPageMargins(0.01)
    report = oedepict.OEReport(ropts)

    cellwidth, cellheight = report.GetCellWidth(), report.GetCellHeight()
    opts = oedepict.OE2DMolDisplayOptions(cellwidth, cellheight,
                                          oedepict.OEScale_AutoScale)
    oedepict.OESetup2DMolDisplayOptions(opts, itf)
    opts.SetAromaticStyle(oedepict.OEAromaticStyle_Circle)
    opts.SetAtomColorStyle(oedepict.OEAtomColorStyle_WhiteMonochrome)

    pen = oedepict.OEPen(oechem.OEBlack, oechem.OEBlack, oedepict.OEFill_Off,
                         0.9)
    opts.SetDefaultBondPen(pen)
    oedepict.OESetup2DMolDisplayOptions(opts, itf)

    if min_scale:
        minscale = float("inf")
        for m in mols:
            oedepict.OEPrepareDepiction(m, False, True)
            minscale = min(minscale, oedepict.OEGetMoleculeScale(m, opts))

        opts.SetScale(minscale)
    for i, mol in enumerate(mols):
        cell = report.NewCell()
        oedepict.OEPrepareDepiction(mol, False, True)
        atom_bond_sets = []
        for j, bond in enumerate(bonds[i]):
            bo = get_bond(mol, bond)
            atom_bond_set = oechem.OEAtomBondSet()
            atom_bond_set.AddBond(bo)
            atom_bond_sets.append(atom_bond_set)

        opts.SetTitleLocation(oedepict.OETitleLocation_Hidden)

        disp = oedepict.OE2DMolDisplay(mol, opts)
        hstyle = oedepict.OEHighlightStyle_Stick
        hstyle_2 = oedepict.OEHighlightStyle_Color
        score = scores[i]
        norm = plt.Normalize(0, max(score))
        colors = plt.cm.coolwarm(norm(score))
        colors_oe = [rbg_to_int(c, 200) for c in colors]

        for j, atom_bond_set in enumerate(atom_bond_sets):
            highlight = oechem.OEColor(*colors_oe[j])

            oedepict.OEAddHighlighting(disp, highlight, hstyle, atom_bond_set)
            oedepict.OEAddHighlighting(disp, highlight, hstyle_2,
                                       atom_bond_set)

        highlight = oedepict.OEHighlightByCogwheel(oechem.OEDarkPurple)
        highlight.SetBallRadiusScale(5.0)

        if not atoms is None:
            for a_b in atoms[i]:
                if isinstance(a_b[-1], list):
                    for k, c in enumerate(a_b[-1]):
                        print(c)
                        color = oechem.OEColor(*colors_oe[c])
                        highlight.SetBallRadiusScale(5.0 - 2.5 * k)
                        highlight.SetColor(color)
                        atom_bond_set_a = oechem.OEAtomBondSet()
                        if len(a_b[0]) == 1:
                            a = mol.GetAtom(oechem.OEHasMapIdx(a_b[0][0]))
                            atom_bond_set_a.AddAtom(a)
                        oedepict.OEAddHighlighting(disp, highlight,
                                                   atom_bond_set_a)
                else:
                    color = oechem.OEColor(*colors_oe[a_b[-1]])
                    highlight.SetColor(color)
                    atom_bond_set_a = oechem.OEAtomBondSet()
                    if len(a_b[0]) == 1:
                        a = mol.GetAtom(oechem.OEHasMapIdx(a_b[0][0]))
                        atom_bond_set_a.AddAtom(a)
                    else:
                        for b in itertools.combinations(a_b[0], 2):
                            bo = get_bond(mol, b)
                            if not bo:
                                continue
                            atom_bond_set_a.AddAtom(bo.GetBgn())
                            atom_bond_set_a.AddAtom(bo.GetEnd())
                            atom_bond_set_a.AddBond(bo)
                    oedepict.OEAddHighlighting(disp, highlight,
                                               atom_bond_set_a)
        oedepict.OERenderMolecule(cell, disp)
        # oedepict.OEDrawCurvedBorder(cell, oedepict.OELightGreyPen, 10.0)

    return oedepict.OEWriteReport(fname, report)
Esempio n. 4
0
oedepict.OEPrepareDepiction(fitmol)

image = oedepict.OEImage(500, 300)

rows, cols = 1, 2
grid = oedepict.OEImageGrid(image, rows, cols)

overlaps = oegraphsim.OEGetFPOverlap(refmol, fitmol,
                                     oegraphsim.OEGetFPType(oegraphsim.OEFPType_Tree))
oedepict.OEPrepareMultiAlignedDepiction(fitmol, refmol, overlaps)

opts = oedepict.OE2DMolDisplayOptions(grid.GetCellWidth(), grid.GetCellHeight(),
                                      oedepict.OEScale_AutoScale)
opts.SetTitleLocation(oedepict.OETitleLocation_Hidden)

refscale = oedepict.OEGetMoleculeScale(refmol, opts)
fitscale = oedepict.OEGetMoleculeScale(fitmol, opts)
opts.SetScale(min(refscale, fitscale))

refdisp = oedepict.OE2DMolDisplay(refmol, opts)
fitdisp = oedepict.OE2DMolDisplay(fitmol, opts)

refcell = grid.GetCell(1, 1)
oedepict.OERenderMolecule(refcell, refdisp)

fitcell = grid.GetCell(1, 2)
oedepict.OERenderMolecule(fitcell, fitdisp)

oedepict.OEWriteImage("FPAlign.png", image)
# @ </SNIPPET-FINGERPRINT-ALIGN>
oedepict.OEWriteImage("FPAlign.pdf", image)
Esempio n. 5
0
                                      oedepict.OEScale_AutoScale)
opts.SetAromaticStyle(oedepict.OEAromaticStyle_Circle)
#opts.SetAtomColorStyle(oedepict.OEAtomColorStyle_WhiteMonochrome)

pen = oedepict.OEPen(oechem.OEBlack, oechem.OEBlack, oedepict.OEFill_Off, 0.6)
opts.SetDefaultBondPen(pen)
oedepict.OESetup2DMolDisplayOptions(opts, itf)
mols = []

minscale = float("inf")
for s in smiles:
    mol = oechem.OEMol()
    oechem.OESmilesToMol(mol, s)
    mols.append(mol)
    oedepict.OEPrepareDepiction(mol, False, True)
    minscale = min(minscale, oedepict.OEGetMoleculeScale(mol, opts))
    opts.SetScale(minscale)

print(minscale)
opts.SetScale(minscale * 2)

for mol in mols[::2]:
    mol = oechem.OEMol()
    oechem.OESmilesToMol(mol, s)
    cell = report.NewCell()
    oedepict.OEPrepareDepiction(mol, False, suppress_h)
    #opts.SetTitleLocation(oedepict.OETitleLocation_Hidden)

for idx, cell in enumerate(report.GetCells()):
    #i = (len(mols)-1) - idx
    disp = oedepict.OE2DMolDisplay(mols[idx], opts)