cbouy / mols2grid

Interactive molecule viewer for 2D structures
https://mols2grid.readthedocs.io
Apache License 2.0
206 stars 25 forks source link

How to use prolif.LigNetwork as callback? #43

Closed ale94mleon closed 1 year ago

ale94mleon commented 1 year ago

Hi, excellent package, so useful!!! I am trying to combine the LigNetwork output of ProLIF as callback but without succes. I am trying to do the following:

# Here is the problem
ProLIF_callback = mols2grid.callbacks.make_popup_callback(
    title='Prolif',
    html=net._get_html(),
    js = net._get_js()
)

But it doe not work. I let the full code example just for reproducibility:

import mols2grid
from pathlib import Path
from rdkit import RDConfig
from rdkit.Chem import Descriptors
from ipywidgets import interact, widgets
from prolif.plotting.network import LigNetwork
import MDAnalysis as mda
import prolif as plf
import numpy as np

output = widgets.Output()
SDF_FILE = (f"{RDConfig.RDDocsDir}/Book/data/solubility.test.sdf"
            if Path(RDConfig.RDDocsDir).is_dir() else "solubility.test.sdf")

# ProLIF example
# load topology
u = mda.Universe(plf.datafiles.TOP, plf.datafiles.TRAJ)
lig = u.select_atoms("resname LIG")
prot = u.select_atoms("protein")
# create RDKit-like molecules for visualisation
lmol = plf.Molecule.from_mda(lig)
pmol = plf.Molecule.from_mda(prot)
fp = plf.Fingerprint()
fp.run(u.trajectory[0:1], lig, prot)
fp = plf.Fingerprint()
fp.run(u.trajectory[::10], lig, prot)
df_fp = fp.to_dataframe(return_atoms=True)

net = LigNetwork.from_ifp(
    df_fp,
    lmol,
    # replace with `kind="frame", frame=0` for the other depiction
    kind="aggregate",
    threshold=0.3,
    rotation=270,
)

# Here is the problem
ProLIF_callback = mols2grid.callbacks.make_popup_callback(
    title='Prolif',
    html=net._get_html(),
    js = net._get_js()
)

# mols2grid example
df = mols2grid.sdf_to_dataframe(SDF_FILE)
# compute some descriptors

grid = mols2grid.MolGrid(
  df,
  size=(120, 100),
  name="filters",
)

view = grid.display(
    n_rows=2,
    subset=["img", "ID"],
    tooltip=["SOL", 'SMILES'],
    callback = ProLIF_callback
    )

view
cbouy commented 1 year ago

Thanks!

You'd have to replace the "mols2grid example" code with:

prolif_ligplot_html_document = net.display(height="400px").data

prolif_plot_callback = mols2grid.make_popup_callback(
    title="${data['SMILES']}",
    html=prolif_ligplot_html_document,
    style="max-width: 80%;"
)

df = mols2grid.sdf_to_dataframe(SDF_FILE)
mols2grid.display(
    df,
    n_rows=4,
    subset=["mols2grid-id", "img"],
    tooltip=["SOL", 'SMILES'],
    callback=prolif_plot_callback ,
)

I've set the height="400px" and n_rows=4 so that it displays things nicely otherwise the callback display was a bit cramped but feel free to adjust those to your use case.

ale94mleon commented 1 year ago

Thank you so much @cbouy, it works perfect!