Skip to content

Commit a4dd4e3

Browse files
committed
Added reaction drawing support and svg size options for reaction schemes.
1 parent 1cd3738 commit a4dd4e3

1 file changed

Lines changed: 18 additions & 5 deletions

File tree

molplotly/main.py

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import plotly.graph_objects as go
1717

1818
from rdkit import Chem
19+
from rdkit.Chem.rdChemReactions import ReactionFromSmarts
1920
from rdkit.Chem.rdchem import Mol
2021
from rdkit.Chem.Draw import rdMolDraw2D
2122

@@ -143,7 +144,8 @@ def add_molecules(
143144
smiles_col: str | list[str] = "SMILES",
144145
mol_col: Mol | list[Mol] = None,
145146
show_img: bool = True,
146-
svg_size: int = 200,
147+
svg_height: int = 200,
148+
svg_width: int = 200,
147149
alpha: float = 0.75,
148150
mol_alpha: float = 0.7,
149151
title_col: str = None,
@@ -158,6 +160,7 @@ def add_molecules(
158160
width: int = 150,
159161
fontfamily: str = "Arial",
160162
fontsize: int = 12,
163+
reaction: bool = False,
161164
) -> JupyterDash:
162165
"""
163166
A function that takes a plotly figure and a dataframe with molecular SMILES
@@ -180,8 +183,10 @@ def add_molecules(
180183
If provided as a list, will add a slider to choose which column is used for rendering the structures.
181184
show_img : bool, optional
182185
whether or not to generate the molecule image in the dash app (default True).
183-
svg_size : float, optional
184-
the size in pixels of the molecule drawing (default 200).
186+
svg_height : float, optional
187+
the svg_height in pixels of the molecule drawing (default 200).
188+
svg_width : float, optional
189+
the svg_width in pixels of the molecule drawing (default 200).
185190
alpha : float, optional
186191
the transparency of the hoverbox, 0 for full transparency 1 for full opaqueness (default 0.7).
187192
mol_alpha : float, optional
@@ -211,6 +216,8 @@ def add_molecules(
211216
the font family used in the hover box (default 'Arial').
212217
fontsize : int, optional
213218
the font size used in the hover box - the font of the title line is fontsize+2 (default 12).
219+
reaction: bool, optional
220+
toggles rdkit to process the image like a reaction
214221
"""
215222
df_data = df.copy()
216223
if color_col is not None:
@@ -347,10 +354,16 @@ def display_hover(hoverData, value):
347354
if isinstance(smiles, str):
348355
# Generate 2D SVG if smiles column is a string
349356

350-
d2d = rdMolDraw2D.MolDraw2DSVG(svg_size, svg_size)
357+
d2d = rdMolDraw2D.MolDraw2DSVG(svg_width, svg_height)
351358
opts = d2d.drawOptions()
352359
opts.clearBackground = False
353-
d2d.DrawMolecule(Chem.MolFromSmiles(smiles))
360+
if reaction:
361+
try:
362+
d2d.DrawReaction(ReactionFromSmarts(smiles, useSmiles=True))
363+
except:
364+
d2d.DrawMolecule(Chem.MolFromSmiles(smiles))
365+
else:
366+
d2d.DrawMolecule(Chem.MolFromSmiles(smiles))
354367
d2d.FinishDrawing()
355368
img_str = d2d.GetDrawingText()
356369
buffered.write(str.encode(img_str))

0 commit comments

Comments
 (0)