Skip to content

molfeat.viz

colors_from_feature_factory(feature_factory, cmap_name='Set1', alpha=1.0)

Get a list of colors for a given feature factory. For the same feature_factory the returned colors will be the same.

Parameters:

Name Type Description Default
feature_factory MolChemicalFeatureFactory

Feature factory to use.

required
cmap_name str

Matplotlib colormap name.

'Set1'
alpha float

Alpha value for the colors.

1.0

Returns:

Name Type Description
colors

Dict of feature_name as keys and colors as values.

Source code in molfeat/viz.py
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
def colors_from_feature_factory(
    feature_factory: rdMolChemicalFeatures.MolChemicalFeatureFactory,
    cmap_name: str = "Set1",
    alpha: float = 1.0,
):
    """Get a list of colors for a given feature factory. For the same
    `feature_factory` the returned colors will be the same.

    Args:
        feature_factory: Feature factory to use.
        cmap_name: Matplotlib colormap name.
        alpha: Alpha value for the colors.

    Returns:
        colors: Dict of feature_name as keys and colors as values.
    """
    cmap_name = "Set1"

    cmap = matplotlib.colormaps[cmap_name]
    cmap_n = cmap.N  # type: ignore

    colors = {}
    for i, name in enumerate(feature_factory.GetFeatureFamilies()):
        color: List[float] = list(cmap(i % cmap_n))
        color[3] = alpha
        colors[name] = color

    return colors

show_mols(mols)

Generate a view of the molecules.

Parameters:

Name Type Description Default
mols Union[Mol, List[Mol]]

A mol or a list of mols.

required

Returns:

Type Description

nglview.widget.NGLWidget

Source code in molfeat/viz.py
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
def show_mols(mols: Union[dm.Mol, List[dm.Mol]]):
    """Generate a view of the molecules.

    Args:
        mols: A mol or a list of mols.

    Returns:
        nglview.widget.NGLWidget
    """

    import nglview as nv

    if isinstance(mols, dm.Mol):
        mols = [mols]

    view = nv.NGLWidget()
    for mol in mols:
        component = view.add_component(mol)
        component.clear()  # type: ignore
        component.add_ball_and_stick(multipleBond=True)  # type: ignore

    return view

show_pharm_features(mols, features, feature_factory, alpha=1.0, sphere_radius=0.4, show_legend=True)

Generate a view of the molecules with pharmacophoric features.

Parameters:

Name Type Description Default
mols Union[Mol, List[Mol]]

A mol or a list of mols.

required
features DataFrame

Features data. Columns must contain at least "feature_name", "feature_id", and "feature_coords".

required
feature_factory MolChemicalFeatureFactory

Feature factory to display consistent colors.

required
alpha float

Alpha value for the colors (currently not working).

1.0
sphere_radius float

Radius of the spheres for the features.

0.4
show_legend bool

Display the legend (the layout is bad but at least it shows the legend).

True

Returns:

Type Description

nglview.widget.NGLWidget

Source code in molfeat/viz.py
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
def show_pharm_features(
    mols: Union[dm.Mol, List[dm.Mol]],
    features: pd.DataFrame,
    feature_factory: rdMolChemicalFeatures.MolChemicalFeatureFactory,
    alpha: float = 1.0,
    sphere_radius: float = 0.4,
    show_legend: bool = True,
):
    """Generate a view of the molecules with pharmacophoric features.

    Args:
        mols: A mol or a list of mols.
        features: Features data. Columns must contain at least
            "feature_name", "feature_id", and "feature_coords".
        feature_factory: Feature factory to display consistent colors.
        alpha: Alpha value for the colors (currently not working).
        sphere_radius: Radius of the spheres for the features.
        show_legend: Display the legend (the layout is bad but at least it
            shows the legend).

    Returns:
        nglview.widget.NGLWidget
    """

    import ipywidgets as ipy

    # Get mols view
    mol_view = show_mols(mols)

    # Get colors
    colors = colors_from_feature_factory(feature_factory, alpha=alpha)

    # Add features to the viz
    for _, row in features.iterrows():
        color = colors[row["feature_name"]]
        label = f"{row['feature_name']}_{row['feature_id']}"
        mol_view.shape.add_sphere(row["coords"], color, sphere_radius, label)  # type: ignore

    if not show_legend:
        return mol_view

    # Build legend widget
    colors_widget = _build_colors_widget(colors)

    main_layout = ipy.Layout(
        display="flex",
        flex_flow="column",
        align_content="center",
    )
    main_widget = ipy.HBox([mol_view, colors_widget], layout=main_layout)  # type: ignore

    return main_widget