Skip to content

Commit dbfef08

Browse files
authored
Feature/ipython_repr (#19)
IPython repr for rings & ring systems
1 parent 4d72d49 commit dbfef08

1 file changed

Lines changed: 54 additions & 0 deletions

File tree

scaffoldgraph/core/scaffold.py

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,14 @@
88

99
from rdkit.Chem import MolToSmiles, MolFromSmiles, BondType, GetSymmSSSR
1010

11+
try:
12+
kernel_name = get_ipython().__class__.__name__
13+
if kernel_name == 'ZMQInteractiveShell':
14+
from rdkit.Chem.Draw import IPythonConsole
15+
from rdkit.Chem import Draw
16+
except NameError:
17+
pass
18+
1119

1220
class Scaffold(object):
1321
"""A convenience class for defining scaffolds in scaffoldgraph.
@@ -497,6 +505,29 @@ def __len__(self):
497505
"""Returns the size of the ring (number of atoms)."""
498506
return len(self.aix)
499507

508+
def _repr_png_(self):
509+
"""Return PNG representation of the rdkit mol (ring highlighted)."""
510+
mol = self.owner.mol
511+
keku = IPythonConsole.kekulizeStructures
512+
size = IPythonConsole.molSize
513+
opts = IPythonConsole.drawOptions
514+
return Draw._moltoimg(
515+
mol, size, self.aix, "", returnPNG=True, drawOptions=opts,
516+
kekulize=keku, highlightBonds=self.bix
517+
)
518+
519+
def _repr_svg_(self):
520+
"""Return SVG representation of the rdkit mol (ring highlighted)."""
521+
if not IPythonConsole.ipython_useSVG:
522+
return None
523+
mol = self.owner.mol
524+
keku = IPythonConsole.kekulizeStructures
525+
size = IPythonConsole.molSize
526+
opts = IPythonConsole.drawOptions
527+
return Draw._moltoSVG(
528+
mol, size, self.aix, "", keku, drawOptions=opts, highlightBonds=self.bix
529+
)
530+
500531
def __repr__(self):
501532
return '<{_cls} at {address}>'.format(
502533
_cls=self.__class__.__name__,
@@ -838,6 +869,29 @@ def __getitem__(self, index):
838869
def __len__(self):
839870
return len(self.aix)
840871

872+
def _repr_png_(self):
873+
"""Return PNG representation of the rdkit mol (ring highlighted)."""
874+
mol = self.owner.mol
875+
keku = IPythonConsole.kekulizeStructures
876+
size = IPythonConsole.molSize
877+
opts = IPythonConsole.drawOptions
878+
return Draw._moltoimg(
879+
mol, size, self.aix, "", returnPNG=True, drawOptions=opts,
880+
kekulize=keku, highlightBonds=self.bix
881+
)
882+
883+
def _repr_svg_(self):
884+
"""Return SVG representation of the rdkit mol (ring highlighted)."""
885+
if not IPythonConsole.ipython_useSVG:
886+
return None
887+
mol = self.owner.mol
888+
keku = IPythonConsole.kekulizeStructures
889+
size = IPythonConsole.molSize
890+
opts = IPythonConsole.drawOptions
891+
return Draw._moltoSVG(
892+
mol, size, self.aix, "", keku, drawOptions=opts, highlightBonds=self.bix
893+
)
894+
841895
def __repr__(self):
842896
return '<{_cls} at {address}>'.format(
843897
_cls=self.__class__.__name__,

0 commit comments

Comments
 (0)