Lasso Highlighting In Datamol
Tags: datamol, rdkit, python, cheminformatics
Published: 2023-04-17 18:07:37

## TL;DR

In this blog post, we'll explore the `lasso_highlight_image()` function in Datamol, a Python library for scientists working with molecular data. We'll see how it can be used to highlight specific parts or features of a molecule quickly. We'll also provide some examples of how to use the function and discuss its limitations and areas for improvement for future contributors.

## Intro

If you work with chemical data and need to visualize it, you'll want to check out Datamol. This Python library has a new addition called lasso highlight, which was initially produced by [Christian W. Feldmann](https://github.com/c-feldmann/lassohighlight). The lasso highlighting function allows you to quickly identify and visualize specific parts or features of a molecule. It's useful for identifying functional groups, comparing and contrasting different molecules, and analyzing molecular structure and properties.

## Examples

To use the `lasso_highlight_image()` function, you supply a molecule and specify the substructures you want to highlight. The function returns an image of the molecule with the specified substructures highlighted. You can provide the target and search molecules in SMILES format or as a rdkit.Chem.mol object. The function also takes parameters to specify the image type (PNG or SVG), size, and image characteristics.The details of each parameter can be found [here](https://github.com/datamol-io/datamol/blob/main/datamol/viz/_lasso_highlight.py#L375).

Here are two examples to help you get started: 

1.Lasso highlight with multiple substructures with PNG image

```
import datamol as dm

target_molecule = "CO[C@@H](O)C1=C(O[C@H](F)Cl)C(C#N)=C1ONNC[NH3+]"
substructure = ["CONN", "N#CC~CO"]

dm.lasso_highlight_image(target_molecule, substructures, (400, 400), use_svg=False)
```

![Multi substructure highlighting](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0n3wmykgj4jf9c5qbks4.png)

2. Lasso highlight with single substructures with SVG image

```
import datamol as dm

target_molecule = "CO[C@@H](O)C1=C(O[C@H](F)Cl)C(C#N)=C1ONNC[NH3+]"
substructure = dm.to_smarts("CONN")

dm.lasso_highlight_image(target_molecule, substructure, (300, 300))
```

![Single substructure highlighting](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nvuzvwcyjfhe1phozcr9.png)

## Limitations and What's Next

Although the lasso_highlight_image() function is highly valuable, it does have a few limitations. To enhance its capabilities and overcome these constraints, the following features could be incorporated:

1. Add functionality to write to a file, similar to the `to_image()` function in `Datamol/viz/viz.py`.
2. Allow the analysis of multiple target molecules at once.
3. Canonicalize search molecules to prevent duplicate highlighting.
4. Update the documentation in `Visualization.ipynb`.

If you're interested in contributing to the project, check out the [Datamol](https://datamol.io/) website and the [contribution guidelines](https://docs.datamol.io/stable/contribute.html). Alternatively, feel free to contact me from my social media on my [personal website](https://desmondgilmour.com/) for any questions or feedback.