def draw_dihedral_circle(image, center, radius, nrbins, nrconfs): """ Draws the base radial histogram. :type image: oedepict.OEImageBase :type center: oedepict.OE2DPoint :type radius: float :type nrbins: int :type nrconfs: int """ grey = oechem.OEColor(210, 210, 210) pen = oedepict.OEPen(grey, grey, oedepict.OEFill_On, 1.0) image.DrawCircle(center, radius, pen) linegrey = oechem.OEColor(220, 220, 220) linepen = oedepict.OEPen(linegrey, linegrey, oedepict.OEFill_On, 1.0) angleinc = 360.0 / float(nrbins) v = oedepict.OE2DPoint(0.0, -1.0) for i in range(0, nrbins): end = oedepict.OELengthenVector( oedepict.OERotateVector(v, i * angleinc), radius) image.DrawLine(center, center + end, linepen) fontsize = int(math.floor(radius * 0.1)) font = oedepict.OEFont(oedepict.OEFontFamily_Default, oedepict.OEFontStyle_Bold, fontsize, oedepict.OEAlignment_Center, oechem.OEBlack) for i in range(0, 4): angle = i * 90.0 end = oedepict.OELengthenVector(oedepict.OERotateVector(v, angle), radius * 1.20) text = '{:.1f}'.format(angle) dim = radius / 2.5 textframe = oedepict.OEImageFrame( image, dim, dim, center + end - oedepict.OE2DPoint(dim / 2.0, dim / 2.0)) oedepict.OEDrawTextToCenter(textframe, text, font) minradius = radius / 3.0 whitepen = oedepict.OEPen(oechem.OEWhite, oechem.OEWhite, oedepict.OEFill_On, 1.0, oedepict.OEStipple_NoLine) image.DrawCircle(center, minradius, whitepen) font.SetSize(int(fontsize * 1.5)) top = oedepict.OE2DPoint(image.GetWidth() / 2.0, -10.0) image.DrawText(top, 'torsion histogram', font) top = oedepict.OE2DPoint(image.GetWidth() / 2.0, -30.0) image.DrawText(top, 'MM: blue; ANI: red', font) bottom = oedepict.OE2DPoint(image.GetWidth() / 2.0, image.GetHeight() + 26.0) image.DrawText(bottom, 'number of conformations: {}'.format(nrconfs), font)
def draw_frag_pdf(frag_dict, scaffold=None, pdf_filename='fragments.pdf'): from openeye import oechem, oedepict import re itf = oechem.OEInterface() PageByPage = True suppress_h = True rows = 7 cols = 5 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_Default * 0.5) opts.SetAromaticStyle(oedepict.OEAromaticStyle_Circle) pen = oedepict.OEPen(oechem.OEBlack, oechem.OEBlack, oedepict.OEFill_On, 1.0) opts.SetDefaultBondPen(pen) oedepict.OESetup2DMolDisplayOptions(opts, itf) if scaffold: oemol = oechem.OEGraphMol() scaffold_smi = re.sub(r"\(\[R([1-9])+]\)", r"([*])", scaffold.smiles) oechem.OESmilesToMol(oemol, scaffold_smi) cell = report.NewCell() mol = oechem.OEMol(oemol) mol.SetTitle(f'{scaffold.smiles}') oedepict.OEPrepareDepiction(mol, False, suppress_h) disp = oedepict.OE2DMolDisplay(mol, opts) oedepict.OERenderMolecule(cell, disp) headerpen = oedepict.OEPen(oechem.OEWhite, oechem.OELightGrey, oedepict.OEFill_Off, 1.0) oedepict.OEDrawBorder(cell, headerpen) for rx, smis in frag_dict.items(): for idx, smi in enumerate(smis): if smi != None: # Create oemol oemol = oechem.OEGraphMol() oechem.OESmilesToMol(oemol, smi) # Render molecule cell = report.NewCell() mol = oechem.OEMol(oemol) mol.SetTitle(f'R{rx} #{idx+1}') oedepict.OEPrepareDepiction(mol, False, suppress_h) disp = oedepict.OE2DMolDisplay(mol, opts) oedepict.OERenderMolecule(cell, disp) oedepict.OEWriteReport(pdf_filename, report)
def CreateScaleReportLayout(opts, scale): opts = ScaleReportOptions(opts, scale) report = oedepict.OEReport(opts) borderpen = oedepict.OEPen(oechem.OEGrey, oechem.OEGrey, oedepict.OEFill_Off, 1.0) first = report.NewBody() oedepict.OEDrawBorder(first, borderpen) nrgridpages = 1 for p in range(0, nrgridpages): for c in range(0, report.NumRowsPerPage() * report.NumColsPerPage()): cell = report.NewCell() oedepict.OEDrawBorder(cell, borderpen) for page in report.GetPages(): oedepict.OEDrawBorder(page, oedepict.OEBlackPen) if opts.GetHeaderHeight() > 0.0: for header in report.GetHeaders(): oedepict.OEDrawBorder(header, borderpen) if opts.GetFooterHeight() > 0.0: for footer in report.GetFooters(): oedepict.OEDrawBorder(footer, borderpen) return report
def CreateReportLayout(): rows, cols = 3, 2 reportopts = oedepict.OEReportOptions(rows, cols) reportopts.SetPageWidth(100.0) reportopts.SetPageHeight(150.0) reportopts.SetPageMargins(5.0) reportopts.SetCellGap(5.0) reportopts.SetHeaderHeight(15.0) reportopts.SetFooterHeight(15.0) report = oedepict.OEReport(reportopts) borderpen = oedepict.OEPen(oechem.OELightGrey, oechem.OELightGrey, oedepict.OEFill_Off, 1.0) first = report.NewBody() oedepict.OEDrawBorder(first, borderpen) nrgridpages = 3 for p in range(0, nrgridpages): for c in range(0, report.NumRowsPerPage() * report.NumColsPerPage()): cell = report.NewCell() oedepict.OEDrawBorder(cell, borderpen) last = report.NewBody() oedepict.OEDrawBorder(last, borderpen) for page in report.GetPages(): oedepict.OEDrawBorder(page, oedepict.OEBlackPen) for header in report.GetHeaders(): oedepict.OEDrawBorder(header, borderpen) for footer in report.GetFooters(): oedepict.OEDrawBorder(footer, borderpen) return report
def __call__(self, image, arc): adisp = arc.GetAtomDisplay() if adisp is None or not adisp.IsVisible(): return False atom = adisp.GetAtom() if atom is None: return False charge = atom.GetPartialCharge() if charge == 0.0: return True color = self.colorg.GetColorAt(charge) pen = oedepict.OEPen() pen.SetForeColor(color) pen.SetLineWidth(2.0) center = arc.GetCenter() radius = arc.GetRadius() bAngle = arc.GetBgnAngle() eAngle = arc.GetEndAngle() edgeAngle = 5.0 dir = oegrapheme.OEPatternDirection_Outside patternAngle = 10.0 oegrapheme.OEDrawBrickRoadSurfaceArc(image, center, bAngle, eAngle, radius, pen, edgeAngle, dir, patternAngle) return True
def prepare(self): """[summary] # OESuppressHydrogens(self.__oeMol, retainPolar=False,retainStereo=True,retainIsotope=True) oechem.OESuppressHydrogens(self.__oeMol) """ self.__setupImage() for idx, cell in enumerate(self.__grid.GetCells()): ccId, oeMol, title = self._molTitleList[idx] logger.debug("Preparing %s %r", ccId, title) # if self._params["suppressHydrogens"]: # mol = oeMol.getGraphMolSuppressH() # OESuppressHydrogens(self.__oeMol, retainPolar=False,retainStereo=True,retainIsotope=True) mol = oechem.OESuppressHydrogens(oechem.OEGraphMol(oeMol)) else: mol = oeMol # if self.__useTitle and title: mol.SetTitle(title) self._opts.SetTitleHeight(5.0) else: mol.SetTitle("") # # oedepict.OEPrepareDepiction(mol) self._opts.SetDimensions(cell.GetWidth(), cell.GetHeight(), oedepict.OEScale_AutoScale) self._assignDisplayOptions() disp = oedepict.OE2DMolDisplay(mol, self._opts) oedepict.OERenderMolecule(cell, disp) if self._params["cellBorders"]: oedepict.OEDrawBorder(cell, oedepict.OEPen(oedepict.OEBlackPen))
def draw_reference_dihedral(image, group, itag, center, radius): """ Draws dihedral angle of the reference molecule. :type image: oedepict.OEImageBase :type group: oechem.OEGroupBase :type itag: int :type center: oedepict.OE2DPoint :type radius: float """ if not group.HasData(itag): return angle = group.GetData(itag) v = oedepict.OE2DPoint(0.0, -1.0) bgn = oedepict.OELengthenVector(oedepict.OERotateVector(v, angle), radius / 6.0) end = oedepict.OELengthenVector(oedepict.OERotateVector(v, angle), radius / 3.0) redpen = oedepict.OEPen(oechem.OERed, oechem.OERed, oedepict.OEFill_Off, 2.0) image.DrawLine(center + bgn, center + end, redpen) fontsize = int(math.floor(radius * 0.12)) font = oedepict.OEFont(oedepict.OEFontFamily_Default, oedepict.OEFontStyle_Bold, fontsize, oedepict.OEAlignment_Center, oechem.OERed) dim = radius / 2.5 textframe = oedepict.OEImageFrame( image, dim, dim, center - oedepict.OE2DPoint(dim / 2.0, dim / 2.0)) oedepict.OEDrawTextToCenter(textframe, "{:.1f}".format(angle), font)
def __call__(self, image, arc): pen = oedepict.OEPen(oechem.OEGrey, oechem.OEGrey) pen.SetLineWidth(2.0) oegrapheme.OEDrawDefaultSurfaceArc(image, arc.GetCenter(), arc.GetBgnAngle(), arc.GetEndAngle(), arc.GetRadius(), pen) return True
def Draw2DSurface(disp, atompred, radius, color): penA = oedepict.OEPen(color, color, oedepict.OEFill_Off, 2.0) arcfxnA = oegrapheme.OEDefaultArcFxn(penA) penB = oedepict.OEPen(oechem.OELightGrey, oechem.OELightGrey, oedepict.OEFill_Off, 2.0, oedepict.OEStipple_ShortDash) arcfxnB = oegrapheme.OEDefaultArcFxn(penB) layer = disp.GetLayer(oedepict.OELayerPosition_Below) for adisp in disp.GetAtomDisplays(): for arc in oegrapheme.OEGet2DSurfaceArcs(disp, adisp, radius): if atompred(adisp.GetAtom()): arcfxnA(layer, arc) else: arcfxnB(layer, arc)
def DrawHelloWorld(image, font): w = image.GetWidth() h = image.GetHeight() image.Clear(oechem.OEWhite) pen = oedepict.OEPen() image.DrawRectangle(oedepict.OE2DPoint(1.0, 1.0), oedepict.OE2DPoint(w - 1.0, h - 1.0), pen) image.DrawText(oedepict.OE2DPoint(w / 2.0, h / 2.0), "Hello World!", font)
def HighlightCell(cell, idx): font = oedepict.OEFont(oedepict.OEFontFamily_Default, oedepict.OEFontStyle_Default, 10, oedepict.OEAlignment_Center, oechem.OEBlack) color = oechem.OEColor(oechem.OELightGrey) borderpen = oedepict.OEPen(color, color, oedepict.OEFill_On, 1.0) oedepict.OEDrawBorder(cell, borderpen) p = oedepict.OE2DPoint(cell.GetWidth() / 2.0, cell.GetHeight() / 2.0 + font.GetSize() / 2.0) cell.DrawText(p, "(%d)" % idx, font)
def DrawSurfaces(image, mol): oechem.OEAssignCovalentRadii(mol) minradius = oechem.OEGetCovalentRadius(oechem.OEElemNo_H) radiusScales = oechem.OEDoubleVector(mol.GetMaxAtomIdx(), 0.0) maxrscale = float("-inf") for atom in mol.GetAtoms(): rscale = (atom.GetRadius() - minradius) + oegrapheme.OESurfaceArcScale_Minimum radiusScales[atom.GetIdx()] = rscale maxrscale = max(maxrscale, rscale) opts = oedepict.OE2DMolDisplayOptions(image.GetWidth(), image.GetHeight(), oedepict.OEScale_AutoScale) opts.SetTitleLocation(oedepict.OETitleLocation_Hidden) opts.SetScale(oegrapheme.OEGetMoleculeSurfaceScale(mol, opts, maxrscale)) disp = oedepict.OE2DMolDisplay(mol, opts) layer = disp.GetLayer(oedepict.OELayerPosition_Below) penA = oedepict.OEPen(oechem.OELightGrey, oechem.OELightGrey, oedepict.OEFill_Off, 2.0, oedepict.OEStipple_ShortDash) arcfxnA = oegrapheme.OEDefaultArcFxn(penA) for arc in oegrapheme.OEGet2DSurfaceArcs( disp, oegrapheme.OESurfaceArcScale_Minimum): arcfxnA(layer, arc) penB = oedepict.OEPen(oechem.OEGrey, oechem.OEGrey, oedepict.OEFill_Off, 2.0) arcfxnB = oegrapheme.OEDefaultArcFxn(penB) for arc in oegrapheme.OEGet2DSurfaceArcs(disp, radiusScales): arcfxnB(layer, arc) oedepict.OERenderMolecule(image, disp)
def oedepict_pdf(all_probe_mols, subdir): """ Generate a PDF report of all molecules together, color-coded by parameter ID and labeled with parameter ID and SMILES tag. Parameters ---------- all_probe_mols: key is string of a parameter id to be probed; value is a list of oegraphmols with this parameter id subdir : string Name of subdirectory in which to save results.pdf file """ multi = oedepict.OEMultiPageImageFile(oedepict.OEPageOrientation_Landscape, oedepict.OEPageSize_US_Letter) image = multi.NewPage() opts = oedepict.OE2DMolDisplayOptions() rows, cols = 4, 4 grid = oedepict.OEImageGrid(image, rows, cols) grid.SetCellGap(20) grid.SetMargins(20) citer = grid.GetCells() colors = list(oechem.OEGetContrastColors()) for i, (param, mol_list) in enumerate(all_probe_mols.items()): pen = oedepict.OEPen(oechem.OEWhite, colors[i], oedepict.OEFill_Off, 4.0) for mol in mol_list: # go to next page if not citer.IsValid(): image = multi.NewPage() grid = oedepict.OEImageGrid(image, rows, cols) grid.SetCellGap(20) grid.SetMargins(20) citer = grid.GetCells() cell = citer.Target() mol.SetTitle(f"{param} {oechem.OEGetSDData(mol, 'SMILES QCArchive')}") oedepict.OEPrepareDepiction(mol) opts.SetDimensions(cell.GetWidth(), cell.GetHeight(), oedepict.OEScale_AutoScale) disp = oedepict.OE2DMolDisplay(mol, opts) oedepict.OERenderMolecule(cell, disp) oedepict.OEDrawBorder(cell, pen) citer.Next() oedepict.OEWriteMultiPageImage(f"{subdir}/results.pdf", multi)
def DrawQuadraticBezier(): # @ <SNIPPET-DRAW-QUADRATIC-BEZIER> image = oedepict.OEImage(100, 100) b = oedepict.OE2DPoint(20, 70) e = oedepict.OE2DPoint(80, 70) c = b + oedepict.OE2DPoint(30, -80) pen = oedepict.OEPen(oechem.OELightGreen, oechem.OEBlack, oedepict.OEFill_On, 2.0) image.DrawQuadraticBezier(b, c, e, pen) # @ </SNIPPET-DRAW-QUADRATIC-BEZIER> oedepict.OEWriteImage("DrawQuadraticBezier.png", image) oedepict.OEWriteImage("DrawQuadraticBezier.pdf", image)
def draw_highlight(image, disp, abset): """ Highlights the atoms of the dihedral angle on the molecule display. :type image: oedepict.OEImageBase :type disp: oedepict.OE2DMolDisplay :type abset: oechem.OEAtomBondSet """ linewidth = disp.GetScale() / 2.0 pen = oedepict.OEPen(oechem.OEBlueTint, oechem.OEBlueTint, oedepict.OEFill_On, linewidth) for bond in abset.GetBonds(): adispB = disp.GetAtomDisplay(bond.GetBgn()) adispE = disp.GetAtomDisplay(bond.GetEnd()) image.DrawLine(adispB.GetCoords(), adispE.GetCoords(), pen)
def __call__(self, image, arc): adisp = arc.GetAtomDisplay() if adisp is None or not adisp.IsVisible(): return False color = adisp.GetLabelFont().GetColor() pen = oedepict.OEPen(color, color, oedepict.OEFill_Off, 1.0) center = arc.GetCenter() radius = arc.GetRadius() bgnAngle = arc.GetBgnAngle() endAngle = arc.GetEndAngle() oegrapheme.OEDrawEyelashSurfaceArc(image, center, bgnAngle, endAngle, radius, pen) return True
def RenderGlyph(self, disp, atom): adisp = disp.GetAtomDisplay(atom) if adisp is None or not adisp.IsVisible(): return False res = oechem.OEAtomGetResidue(atom) bfactor = res.GetBFactor() color = self.colorg.GetColorAt(bfactor) pen = oedepict.OEPen(color, color, oedepict.OEFill_On, 1.0) radius = disp.GetScale() / 3.0 layer = disp.GetLayer(oedepict.OELayerPosition_Below) oegrapheme.OEDrawCircle(layer, oegrapheme.OECircleStyle_Default, adisp.GetCoords(), radius, pen) return True
def DrawCubicBezier(): # @ <SNIPPET-CUBIC-BEZIER> image = oedepict.OEImage(100, 100) b = oedepict.OE2DPoint(20, 70) e = oedepict.OE2DPoint(60, 70) c1 = b + oedepict.OE2DPoint(50, -60) c2 = e + oedepict.OE2DPoint(50, -60) pen = oedepict.OEPen(oechem.OELightGreen, oechem.OEBlack, oedepict.OEFill_On, 2.0) image.DrawCubicBezier(b, c1, c2, e, pen) # @ </SNIPPET-CUBIC-BEZIER> oedepict.OEWriteImage("DrawCubicBezier.png", image) oedepict.OEWriteImage("DrawCubicBezier.pdf", image)
def DrawPolygon(): # @ <SNIPPET-DRAW-POLYGON> image = oedepict.OEImage(100, 100) polygon = [] polygon.append(oedepict.OE2DPoint(20, 20)) polygon.append(oedepict.OE2DPoint(40, 40)) polygon.append(oedepict.OE2DPoint(60, 20)) polygon.append(oedepict.OE2DPoint(80, 40)) polygon.append(oedepict.OE2DPoint(80, 80)) polygon.append(oedepict.OE2DPoint(20, 80)) pen = oedepict.OEPen(oechem.OELightGreen, oechem.OEBlack, oedepict.OEFill_On, 2.0) image.DrawPolygon(polygon, pen) # @ </SNIPPET-DRAW-POLYGON> oedepict.OEWriteImage("DrawPolygon.png", image) oedepict.OEWriteImage("DrawPolygon.pdf", image)
def prepare(self): self.__setupImage() rows = self._params["gridRows"] cols = self._params["gridCols"] grid = oedepict.OEImageGrid(self.__image, rows, cols) citer = grid.GetCells() for ccId, oeMol, title in self._molTitleList: logger.debug("Preparing %s %r", ccId, title) if not citer.IsValid(): # go to next page self.__image = self.__multi.NewPage() grid = oedepict.OEImageGrid(self.__image, rows, cols) grid.SetCellGap(self._params["cellGap"]) grid.SetMargins(self._params["cellMargin"]) citer = grid.GetCells() cell = citer.Target() # if self._params["suppressHydrogens"]: # mol = oeMol.getGraphMolSuppressH() # OESuppressHydrogens(self.__oeMol, retainPolar=False,retainStereo=True,retainIsotope=True) mol = oechem.OESuppressHydrogens(oechem.OEGraphMol(oeMol)) else: mol = oeMol if self.__useTitle and title: mol.SetTitle(title) self._opts.SetTitleHeight(5.0) else: mol.SetTitle("") # # oedepict.OEPrepareDepiction(mol) self._opts.SetDimensions(cell.GetWidth(), cell.GetHeight(), oedepict.OEScale_AutoScale) self._assignDisplayOptions() disp = oedepict.OE2DMolDisplay(mol, self._opts) oedepict.OERenderMolecule(cell, disp) oedepict.OEDrawBorder(cell, oedepict.OEPen(oedepict.OEBlackPen)) citer.Next()
def RenderGlyph(self, disp, bond): bdisp = disp.GetBondDisplay(bond) if bdisp is None or not bdisp.IsVisible(): return False if not bond.HasData(self.tag): return False linewidth = disp.GetScale() / 2.0 color = self.colorlist[bond.GetData(self.tag)] pen = oedepict.OEPen(color, color, oedepict.OEFill_Off, linewidth) adispB = disp.GetAtomDisplay(bond.GetBgn()) adispE = disp.GetAtomDisplay(bond.GetEnd()) layer = disp.GetLayer(oedepict.OELayerPosition_Below) layer.DrawLine(adispB.GetCoords(), adispE.GetCoords(), pen) return True
def _assignDisplayOptions(self): if self._params["labelAtomCIPStereo"]: # self._opts.SetAtomStereoStyle( oedepict.OEAtomStereoStyle_Display_All) # self._opts.SetAtomStereoStyle(oedepict.OEAtomStereoStyle_Display_CIPAtomStereo) if self._params["labelBondCIPStereo"]: # will include bowties for undefined stereo # self._opts.SetBondStereoStyle(oedepict.OEBondStereoStyle_Display_All) self._opts.SetBondStereoStyle( oedepict.OEBondStereoStyle_Display_CIPBondStereo) self._opts.SetAtomPropLabelFontScale(0.650) if self._params["labelAtomIndex"]: self._opts.SetAtomPropertyFunctor(oedepict.OEDisplayAtomIdx()) self._opts.SetAtomPropLabelFont(oedepict.OEFont( oechem.OEDarkGreen)) self._opts.SetAtomPropLabelFontScale(0.650) if self._params["labelBondIndex"]: self._opts.SetBondPropertyFunctor(oedepict.OEDisplayBondIdx()) self._opts.SetBondPropLabelFont(oedepict.OEFont(oechem.OEDarkBlue)) if self._params["labelAtomName"]: atomlabel = LabelAtoms() self._opts.SetAtomPropertyFunctor(atomlabel) self._opts.SetAtomPropLabelFont(oedepict.OEFont( oechem.OEDarkGreen)) self._opts.SetAtomPropLabelFontScale(0.650) if self._params["bondDisplayWidth"] is not None: pen = oedepict.OEPen(oechem.OEBlack, oechem.OEBlack, oedepict.OEFill_On, self._params["bondDisplayWidth"]) self._opts.SetDefaultBondPen(pen) # remove for the moment. not supported on all platforms # self._opts.SetBondWidthScaling(False) # # 5.0 is minimum size - self._opts.SetTitleHeight(5.0)
def RenderGlyph(self, disp, atom): adisp = disp.GetAtomDisplay(atom) if adisp is None or not adisp.IsVisible(): return False charge = atom.GetPartialCharge() if charge == 0.0: return True color = self.colorg.GetColorAt(charge) pen = oedepict.OEPen() pen.SetForeColor(oechem.OEColor(color)) color.SetA(100) pen.SetBackColor(oechem.OEColor(color)) pen.SetFill(oedepict.OEFill_On) radius = disp.GetScale() / 2.5 layer = disp.GetLayer(oedepict.OELayerPosition_Below) oegrapheme.OEDrawCircle(layer, oegrapheme.OECircleStyle_Simpson, adisp.GetCoords(), radius, pen) return True
def DrawPath(): # @ <SNIPPET-DRAW-PATH> image = oedepict.OEImage(100, 100) path = oedepict.OE2DPath(oedepict.OE2DPoint(20, 80)) path.AddLineSegment(oedepict.OE2DPoint(80, 80)) path.AddLineSegment(oedepict.OE2DPoint(80, 40)) path.AddCurveSegment(oedepict.OE2DPoint(80, 10), oedepict.OE2DPoint(20, 10), oedepict.OE2DPoint(20, 40)) pen = oedepict.OEPen(oechem.OELightGreen, oechem.OEBlack, oedepict.OEFill_On, 2.0) image.DrawPath(path, pen) # @ </SNIPPET-DRAW-PATH> oedepict.OEWriteImage("DrawPath.png", image) oedepict.OEWriteImage("DrawPath.pdf", image) # @ <SNIPPET-GET-PATH-POINTS> for p in path.GetPoints(): pos = p.GetPoint() print(" %.1f %.1f %d" % (pos.GetX(), pos.GetY(), p.GetPointType()))
def __call__(self, image, arc): adisp = arc.GetAtomDisplay() if adisp is None or not adisp.IsVisible(): return False atom = adisp.GetAtom() if atom is None: return False avgresiduebfactor = atom.GetDoubleData(self.itag) if avgresiduebfactor == 0.0: return True color = self.colorg.GetColorAt(avgresiduebfactor) pen = oedepict.OEPen(color, color, oedepict.OEFill_Off, 5.0) center = arc.GetCenter() bAngle = arc.GetBgnAngle() eAngle = arc.GetEndAngle() radius = arc.GetRadius() oegrapheme.OEDrawDefaultSurfaceArc(image, center, bAngle, eAngle, radius, pen) return True
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)
a = oedepict.OE2DPoint(60.0, 20.0) b = oedepict.OE2DPoint(20.0, 60.0) image.DrawLine(a, b, pen) def DrawFigures(pen, basefilename): image = oedepict.OEImage(80, 80) DrawFigure(image, pen) oedepict.OEWriteImage(basefilename + ".png", image) oedepict.OEWriteImage(basefilename + ".pdf", image) pen = oedepict.OEPen() DrawFigures(pen, "OEPen_Default") # @ <SNIPPET-OEPEN-SET-FORECOLOR> pen = oedepict.OEPen() pen.SetForeColor(oechem.OERed) # @ </SNIPPET-OEPEN-SET-FORECOLOR> DrawFigures(pen, "OEPen_SetForeColor") # @ <SNIPPET-OEPEN-SET-BACKCOLOR> pen = oedepict.OEPen() pen.SetFill(oedepict.OEFill_On) pen.SetBackColor(oechem.OEBlueTint) # @ </SNIPPET-OEPEN-SET-BACKCOLOR> DrawFigures(pen, "OEPen_SetBackColor")
itf = oechem.OEInterface() PageByPage = True suppress_h = True rows = 10 cols = 6 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_Default * 0.5) opts.SetAromaticStyle(oedepict.OEAromaticStyle_Circle) pen = oedepict.OEPen(oechem.OEBlack, oechem.OEBlack, oedepict.OEFill_On, 1.0) opts.SetDefaultBondPen(pen) oedepict.OESetup2DMolDisplayOptions(opts, itf) for json_molecule in json_molecules.values(): # Create oemol oemol = cmiles.utils.load_molecule(json_molecule['initial_molecules'][0]) # Get atom indices atom_indices = json_molecule['atom_indices'][0] # Render molecule cell = report.NewCell() mol = oechem.OEMol(oemol) oedepict.OEPrepareDepiction(mol, False, suppress_h) disp = oedepict.OE2DMolDisplay(mol, opts)
def depict_dihedrals(image, dimage, mol1, mol2, refmol, opts, itag, nrbins, colorg): """ Highlights the dihedral atoms of a torsion and the depicts the corresponding dihedral angle histogram when hovering over the center of the torsion on the molecule display. :type image: oedepict.OEImageBase :type dimage: oedepict.OEImageBase :type mol: oechem.OEMol :type refmol: oechem.OEMol :type opts: oedepict.OE2DMolDisplayOptions :type itag: int :type nrbins: int :type oechem.OEColorGradientBase """ nrconfs = mol1.NumConfs() center = oedepict.OEGetCenter(dimage) radius = min(dimage.GetWidth(), dimage.GetHeight()) * 0.40 draw_dihedral_circle(dimage, center, radius, nrbins, nrconfs) suppressH = True oegrapheme.OEPrepareDepictionFrom3D(mol1, suppressH) if refmol is not None: oegrapheme.OEPrepareDepictionFrom3D(refmol, suppressH) disp = oedepict.OE2DMolDisplay(mol1, opts) dihedrals = [] ref_dihedrals = [] centers = [] agroups = [] dgroups = [] dihedrals_ref_dist = [] for group in mol2.GetGroups(oechem.OEHasGroupType(itag)): dihedrals_ref_dist.append(group) nrdihedrals = 0 for group in mol1.GetGroups(oechem.OEHasGroupType(itag)): uniqueid = uuid.uuid4().hex agroup = image.NewSVGGroup("torsion_area_" + uniqueid) dgroup = image.NewSVGGroup("torsion_data_" + uniqueid) oedepict.OEAddSVGHover(agroup, dgroup) dihedrals.append(group) if refmol is not None: ref_dihedrals.append(get_reference_dihedral(group, refmol, itag)) centers.append(get_center(disp, group)) agroups.append(agroup) dgroups.append(dgroup) nrdihedrals += 1 for didx in range(0, nrdihedrals): image.PushGroup(dgroups[didx]) dihedral = dihedrals[didx] abset = oechem.OEAtomBondSet(dihedral.GetAtoms(), dihedral.GetBonds()) draw_highlight(image, disp, abset) dihedral_histogram = dihedral.GetData(itag) dihedral_histogram_ref = dihedrals_ref_dist[didx].GetData(itag) print(dihedral_histogram) print(dihedral_histogram_ref) draw_dihedral_histogram(dimage, dihedral_histogram, dihedral_histogram_ref, center, radius, nrbins, nrconfs) image.PopGroup(dgroups[didx]) clearbackground = True oedepict.OERenderMolecule(image, disp, not clearbackground) markpen = oedepict.OEPen(oechem.OEBlack, oechem.OEWhite, oedepict.OEFill_On, 1.0) farpen = oedepict.OEPen(oechem.OEBlack, oechem.OERed, oedepict.OEFill_Off, 2.0) angleinc = 360.0 / float(nrbins) for didx in range(0, nrdihedrals): image.PushGroup(agroups[didx]) dihedral = dihedrals[didx] dihedral_histogram = dihedral.GetData(itag) flexibility = determine_flexibility(dihedral_histogram) color = colorg.GetColorAt(flexibility) markpen.SetBackColor(color) markradius = disp.GetScale() / 8.0 image.DrawCircle(centers[didx], markradius, markpen) if refmol is not None and ref_dihedrals[didx] is not None: ref_dihedral = ref_dihedrals[didx] if get_closest_dihedral_angle(mol1, dihedral, ref_dihedral, itag) > angleinc: image.DrawCircle(centers[didx], markradius, farpen) radius = disp.GetScale() / 4.0 image.DrawCircle(centers[didx], radius, oedepict.OESVGAreaPen) image.PopGroup(agroups[didx])
def draw_dihedral_histogram(image, histogram, histogram_ref, center, radius, nrbins, nrconfs): """ Draws the radial histogram of a torsional angle. :type image: oedepict.OEImageBase :type histogram: list(int) :type center: oedepict.OE2DPoint :type radius: float :type nrbins: int :type nrconfs: int :type nrbins: int """ minradius = radius / 3.0 maxvalue = max(max(histogram), max(histogram_ref)) radiusinc = (radius - minradius) / maxvalue angleinc = 360.0 / float(nrbins) valuepen = oedepict.OEPen(oechem.OERoyalBlue, oechem.OERoyalBlue, oedepict.OEFill_On, 2.0) maxvalue = 0 maxvalueidx = 0 for i in range(0, len(histogram)): value = histogram[i] if value == 0: continue if value > maxvalue: maxvalue = value maxvalueidx = i arcradius = value * radiusinc + minradius if arcradius < 1.0: continue bgnangle = i * angleinc endangle = (i + 1) * angleinc image.DrawPie(center, bgnangle, endangle, arcradius, valuepen) valuepen = oedepict.OEPen(oechem.OERed, oechem.OERed, oedepict.OEFill_Off, 2.0) for i in range(0, len(histogram_ref)): value = histogram_ref[i] if value == 0: continue if value > maxvalue: maxvalue = value maxvalueidx = i arcradius = value * radiusinc + minradius if arcradius < 1.0: continue bgnangle = i * angleinc endangle = (i + 1) * angleinc image.DrawPie(center, bgnangle, endangle, arcradius, valuepen) percent = maxvalue / (nrconfs / 100.0) whitepen = oedepict.OEPen(oechem.OEWhite, oechem.OEWhite, oedepict.OEFill_On, 0.2, oedepict.OEStipple_NoLine) image.DrawCircle(center, minradius, whitepen) fontsize = int(math.floor(radius * 0.1)) font = oedepict.OEFont(oedepict.OEFontFamily_Default, oedepict.OEFontStyle_Bold, fontsize, oedepict.OEAlignment_Center, oechem.OEWhite) angle = maxvalueidx * angleinc if angle >= 180.0: angle += angleinc * 0.3 else: angle += angleinc * 0.7 textangle = get_text_angle(angle) v = oedepict.OE2DPoint(0.0, -1.0) pos = oedepict.OELengthenVector(oedepict.OERotateVector(v, angle), radius * 0.80) font.SetRotationAngle(textangle) image.DrawText(center + pos, "{:.1f}%".format(percent * 100), font)