Skip to main content

Chemical viewer for 2D structures of small molecules

Project description

👀 mols2grid

Pypi Version Build status

mols2grid is an interactive chemical viewer for 2D structures of small molecules, based on RDKit.

Demo showing mols2grid's integration in a Jupyter notebook

➡️ Check the demo notebook here

🐍 Installation


mols2grid was developped for Python 3.6+ and requires rdkit (>=2019.09.1), pandas and jinja2 as dependencies.

To install mols2grid from a clean conda environment:

conda install -c conda-forge 'rdkit>=2019.09.1'
pip install mols2grid

📜 Usage


import mols2grid

mols2grid.display("path/to/molecules.sdf",
                  # RDKit's MolDrawOptions parameters
                  fixedBondLength=25,
                  # rename fields for the output document
                  mapping={"SOL": "Solubility",
                           "SOL_classification": "Class",
                           "NAME": "Name"},
                  # set what's displayed on the grid
                  subset=["ID", "img", "Solubility"],
                  # set what's displayed on the tooltips
                  tooltip=["Name", "smiles", "Class", "Solubility"],
                  # style for the grid labels and tooltips
                  style={"Solubility": lambda x: "color: red" if x < -3 else "color: black"})

Input parameters

You can setup the grid from various inputs:

  • a pandas DataFrame (with a column of SMILES or RDKit molecules, controlled by the smiles_col and mol_col parameters),
  • a list of RDKit molecules (with properties accessible through the mol.GetPropsAsDict() method),
  • or an SDF file

You can also rename each field of your input with the mapping parameter. Please note that 3 fields are automatically added regardless of your input: mols2grid-id, SMILES and img. If a "SMILES" field already exists, it will not be overwritten.

Parameters for the drawing of each molecule

  • useSVG=True: use SVG images or PNG
  • coordGen=True: use the coordGen library instead of the RDKit one to depict the molecules in 2D
  • size=(160, 120): size of each image
  • use_coords=True: use the coordinates of the input molecules if available
  • MolDrawOptions=None: RDKit's MolDrawOptions class. Useful for making highly customized drawings. You can also leave this to None, and directly use the attributes of this class as parameters like addStereoAnnotation=True

Parameters for the grid

You can control the general look of the document through the template argument:

  • template="pages" (default) which is displayed above. It integrates nicely with Jupyter notebooks and has a search bar
  • template="table", which displays the full list of molecules (no pages). Useful if you ever need to print the full list of molecules on paper (or print to PDF)

Both templates can be configured with the same parameters (a lot of which are CSS declarations):

  • subset=None: list or None
    Columns to be displayed in each cell of the grid. Each column's value will be displayed from top to bottom in the same order given here. Use "img" for the image of the molecule. Default: all columns (with "img" in first position)
  • tooltip=None: list or None
    Columns to be displayed as a tooltip when hovering/clicking on the image of a cell. Use None for no tooltip.
  • tooltip_fmt="<strong>{key}</strong>: {value}": str
    Format string of each key/value pair in the tooltip
  • tooltip_trigger="click hover": str
    Sequence of triggers for the tooltip: (click, hover, focus)
  • tooltip_placement="bottom": str
    Position of the tooltip: auto, top, bottom, left, right
  • n_cols=5: int
    Number of columns per page
  • border="1px solid #cccccc": str
    Styling of the border around each cell (CSS)
  • gap=0: int or str
    Size of the margin around each cell (CSS)
  • fontsize="12pt": str
    Font size of the text displayed in each cell (CSS)
  • fontfamily"'DejaVu', sans-serif": str
    Font used for the text in each cell (CSS)
  • textalign="center": str
    Alignment of the text in each cell (CSS)
  • hover_color="#e7e7e7": str
    Background color when hovering a cell (CSS)
  • style=None: dict or None
    CSS styling applied to each item in a cell. The dict must follow a key: function structure where the key must correspond to one of the columns in subset or tooltip. The function takes the item's value as input, and outputs a valid CSS styling. For example, if you want to color the text corresponding to the "Solubility" column in your dataframe:
    style={"Solubility": lambda x: "color: red" if x < -3 else "color: black"}
    

The pages template comes with additional parameters:

  • n_rows=3 : int
    Number of rows per page
  • selection=True : bool Enables the selection of molecules using a checkbox. Only usefull in the context of a Jupyter notebook (not compatible with Visual Studio notebook). You can retrieve your selection of molecules (index and SMILES) through mols2grid.selection

Output parameters

You can either:

  • save the grid with mols2grid.save(output_path, ...). The file that is generated is a standalone HTML document that should work with most web browsers.
  • display it directly in a Jupyter notebook with mols2grid.display(...) (optionnal argument: width="100%", height=None)

⚖ License


Unless otherwise noted, all files in this directory and all subdirectories are distributed under the Apache License, Version 2.0:

    Copyright 2021 Cédric BOUYSSET

    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

        http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

mols2grid-0.0.3.tar.gz (19.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

mols2grid-0.0.3-py3-none-any.whl (21.1 kB view details)

Uploaded Python 3

File details

Details for the file mols2grid-0.0.3.tar.gz.

File metadata

  • Download URL: mols2grid-0.0.3.tar.gz
  • Upload date:
  • Size: 19.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/3.10.0 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.59.0 CPython/3.9.2

File hashes

Hashes for mols2grid-0.0.3.tar.gz
Algorithm Hash digest
SHA256 11ff76eb461ef55da47805d15c0ba5ed069c6824a51d5242d033d1757149d054
MD5 bb2cf7816c09dcbfc9e005c0b22e301b
BLAKE2b-256 d3cc5d87e8ffd4916aab7375f5bd95c987236297004875c08277fe04496dbc8c

See more details on using hashes here.

File details

Details for the file mols2grid-0.0.3-py3-none-any.whl.

File metadata

  • Download URL: mols2grid-0.0.3-py3-none-any.whl
  • Upload date:
  • Size: 21.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/3.10.0 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.59.0 CPython/3.9.2

File hashes

Hashes for mols2grid-0.0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 450396cb26fce05d993aa2ec27c5ea4a1c73136e694530b60925baab6c180419
MD5 783dc08e8e3553227c9e4763b976857f
BLAKE2b-256 ae1a23119c704c10fdf2aaf0e3e9635626ea1d4f2b1570ed1d9ee1ed11ef992c

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page