-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvisualization.py
59 lines (51 loc) · 2.09 KB
/
visualization.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
def show_oemol_struc(oemol, torsions=False, atom_indices=[], width=500, height=300):
"""
a routine to visualize the chemical structures using oemol backend and highlight the atoms involved in torsion
that is being driven
:param oemol: pass an oemol object
:param torsions: boolean to highlight or not
:param atom_indices: atom_indices to highlight
:param width: width of the png output
:param height: height of the png output
:return: a png string
"""
from IPython.display import Image
from openeye import oechem, oedepict
# Highlight element of interest
class NoAtom(oechem.OEUnaryAtomPred):
def __call__(self, atom):
return False
class AtomInTorsion(oechem.OEUnaryAtomPred):
def __call__(self, atom):
return atom.GetIdx() in atom_indices
class NoBond(oechem.OEUnaryBondPred):
def __call__(self, bond):
return False
class BondInTorsion(oechem.OEUnaryBondPred):
def __call__(self, bond):
return (bond.GetBgn().GetIdx() in atom_indices) and (
bond.GetEnd().GetIdx() in atom_indices
)
class CentralBondInTorsion(oechem.OEUnaryBondPred):
def __call__(self, bond):
return (bond.GetBgn().GetIdx() in atom_indices[1:3]) and (
bond.GetEnd().GetIdx() in atom_indices[1:3]
)
opts = oedepict.OE2DMolDisplayOptions(width, height, oedepict.OEScale_AutoScale)
opts.SetAtomPropertyFunctor(oedepict.OEDisplayAtomIdx())
oedepict.OEPrepareDepiction(oemol)
img = oedepict.OEImage(width, height)
display = oedepict.OE2DMolDisplay(oemol, opts)
if torsions:
atoms = oemol.GetAtoms(AtomInTorsion())
bonds = oemol.GetBonds(NoBond())
abset = oechem.OEAtomBondSet(atoms, bonds)
oedepict.OEAddHighlighting(
display,
oechem.OEColor(oechem.OEYellow),
oedepict.OEHighlightStyle_BallAndStick,
abset,
)
oedepict.OERenderMolecule(img, display)
png = oedepict.OEWriteImageToString("png", img)
return Image(png)