Filtering

Filtering#

Open In Colab

It’s possible to integrate the grid with other widgets to complement the searchbar.

# uncomment and run if you're on Google Colab
# !pip install mols2grid

# for development purposes only
# %load_ext autoreload
# %autoreload 2
# %env ANYWIDGET_HMR=1
from ipywidgets import interact, widgets
from rdkit.Chem import Descriptors

import mols2grid

We’ll use ipywidgets to add sliders for the molecular weight and the other molecular descriptors, and define a function that queries the internal dataframe using the values in the sliders.

Everytime the sliders are moved, the function is called to filter our grid.

df = mols2grid.sdf_to_dataframe(mols2grid.datafiles.SOLUBILITY_SDF)
# compute some descriptors
df["MolWt"] = df["mol"].apply(Descriptors.ExactMolWt)
df["LogP"] = df["mol"].apply(Descriptors.MolLogP)
df["NumHDonors"] = df["mol"].apply(Descriptors.NumHDonors)
df["NumHAcceptors"] = df["mol"].apply(Descriptors.NumHAcceptors)
grid = mols2grid.MolGrid(
    df,
    size=(120, 100),
    name="filters",
)
view = grid.display(n_items_per_page=12)


@interact(
    MolWt=widgets.IntRangeSlider(value=[0, 600], min=0, max=600, step=10),
    LogP=widgets.IntRangeSlider(value=[-10, 10], min=-10, max=10, step=1),
    NumHDonors=widgets.IntRangeSlider(value=[0, 20], min=0, max=20, step=1),
    NumHAcceptors=widgets.IntRangeSlider(value=[0, 20], min=0, max=20, step=1),
)
def filter_grid(MolWt, LogP, NumHDonors, NumHAcceptors):
    results = grid.dataframe.query(
        "@MolWt[0] <= MolWt <= @MolWt[1] and "
        "@LogP[0] <= LogP <= @LogP[1] and "
        "@NumHDonors[0] <= NumHDonors <= @NumHDonors[1] and "
        "@NumHAcceptors[0] <= NumHAcceptors <= @NumHAcceptors[1]"
    )
    return grid.filter_by_index(results.index)


view