Last active
October 22, 2019 15:25
-
-
Save goraj/b96ff733d60d81318c99bf728fa2a14d to your computer and use it in GitHub Desktop.
holoviews hover support for RDKit molobjects
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
import pandas as pd | |
from rdkit import Chem | |
from rdkit.Chem import Draw | |
from bokeh.models import HoverTool | |
import holoviews as hv | |
hv.notebook_extension("bokeh") | |
df = pd.DataFrame( | |
dict( | |
x=np.random.randint(0, 10, 10), | |
y=np.random.randint(0, 10, 10), | |
smiles=[x * "C" for x in range(10)], | |
) | |
) | |
df["molobj"] = df.smiles.apply( | |
lambda e: Chem.MolFromSmiles(e) | |
) | |
def embed_molobj_svg(molobj): | |
svg_obj = Draw.MolsToGridImage( | |
[molobj], | |
molsPerRow=1, | |
subImgSize=(200, 200), | |
useSVG=True, | |
) | |
svg_html = f"""<img src='data:image/svg+xml;utf8,{svg_obj.data}'/>""" | |
# print(svg_html) | |
return svg_html | |
df["img"] = df["molobj"].apply(embed_molobj_svg) | |
def hover_cb(*arg): | |
return HoverTool( | |
tooltips=f""" | |
<div> | |
@img | |
</div> | |
""" | |
) | |
hover = hover_cb() | |
opts = dict(Scatter=dict(plot=dict(tools=[hover]))) | |
scatter_plot = hv.Scatter( | |
df, kdims=["x", "y"], vdims=["img"] | |
).opts(size=200) | |
figure = scatter_plot(opts) | |
figure |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
import pandas as pd | |
from rdkit import Chem | |
from rdkit.Chem import Draw | |
from bokeh.models import HoverTool | |
import holoviews as hv | |
hv.notebook_extension("bokeh") | |
df = pd.DataFrame( | |
dict( | |
x=np.random.randint(0, 10, 10), | |
y=np.random.randint(0, 10, 10), | |
smiles=[x * "C" for x in range(10)], | |
) | |
) | |
df["molobj"] = df.smiles.apply( | |
lambda e: Chem.MolFromSmiles(e) | |
) | |
def embed_molobj_svg(molobj): | |
return f"""<img src='data:image/svg+xml,<svg xmlns="http://www.w3.org/2000/svg"/>'/>""" | |
df["img"] = df["molobj"].apply(embed_molobj_svg) | |
def hover_cb(*arg): | |
return HoverTool( | |
tooltips=f"""@img""" | |
) | |
hover = hover_cb() | |
opts = dict(Scatter=dict(plot=dict(tools=[hover]))) | |
scatter_plot = hv.Scatter( | |
df, kdims=["x", "y"], vdims=["img"] | |
).opts(size=10) | |
figure = scatter_plot(opts) | |
figure |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment