Last active
December 21, 2017 15:22
-
-
Save BielStela/b0e8e1d842db5dcf2c09bdc23bbd1736 to your computer and use it in GitHub Desktop.
Show the molecular 2D structure in the hover tool of a Bokeh plot
This file contains hidden or 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
from bokeh.plotting import figure, output_notebook, show | |
from bokeh.models import ColumnDataSource, HoverTool, CrosshairTool, WheelZoomTool | |
from bokeh.models import ColorBar, ResetTool, PanTool, LinearColorMapper | |
import bokeh.palettes as palettes | |
import matplotlib as mpl | |
output_notebook() | |
def get_structures(mol_df): | |
#mol_df["img_path"] = "" | |
img_path = [] | |
for mol in range(len(mol_df)): | |
Draw.MolToFile(mol_df.ROMol[mol], | |
"static/imgs/"+mol_df.name[mol]+".svg", | |
imageType="svg", | |
fitImage=False, | |
size=(200, 200)) | |
#mol_df["img_path"][mol] = "static/imgs/"+mol_df.name[mol]+".svg" | |
img_path.append("static/imgs/"+mol_df.name[mol]+".svg") | |
mol_df["img_path"] = img_path | |
return mol_df | |
def interactiveplot(df): | |
source = ColumnDataSource(mols_pr) | |
source.data['HCPSA'] = source.data['HCPSA']/6 | |
color_maper = LinearColorMapper(palette='Viridis256', low=min(mols_pr.activity), high=max(mols_pr.activity)) | |
#preparing the hover tooltip with HTML template | |
hover = HoverTool(tooltips = """ | |
<div> | |
<img src="@img_path" width="170" height="170"></img> | |
</div> | |
<div> | |
<span style="font-size: 12px; font-weight: bold;">@name</span> | |
</div> | |
<div> | |
<span style="font-size: 12px; font-weight: bold;">x,y:</span> | |
<span style="font-size: 12px;">@x, @y</span> | |
</div> | |
""" | |
) | |
TOOLS=[hover, CrosshairTool(), WheelZoomTool(), ResetTool(), PanTool()] | |
p = figure(plot_width=800, plot_height=800,title="Mouse over the dots", tools=TOOLS, toolbar_location="above") | |
p.xgrid.grid_line_color = None | |
p.ygrid.grid_line_color = None | |
p.scatter('x','y',size='HCPSA',alpha = 0.7,color='activity_color',source = source) | |
color_bar = ColorBar(color_mapper=color_maper, label_standoff=12, | |
border_line_color=None, location=(0,0)) | |
p.add_layout(color_bar, 'right') | |
#output_file("test_mol.html") | |
show(p) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
PD: Run it in a Jupyter Notebook