Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rdkit reb #177

Open
wants to merge 6 commits into
base: trunk
Choose a base branch
from
Open

Rdkit reb #177

wants to merge 6 commits into from

Conversation

rebadam
Copy link
Contributor

@rebadam rebadam commented Nov 22, 2024

I added three things:

  1. to_rdmol often fails, especially for the iffy compounds made with reactions discovery. I added an optional argument (presanitize) that iteratively sanitizes and update the bonds and atomic charges, untill sanitization succeeds.
  2. I added a to_image function that makes a 2D image of a molecule. This uses the presanitize by default. It creates an image (svg (default), png, eps, pdf, jpeg), and optionally prints it to file. It works with our RDKit version, and when using a newer version, it uses newer features. I also works from jupyter notebooks.
  3. I added a to_reaction_image function that tries to make a nice looking 2D reaction image.
    I added some example molecules and a test script here.
    Example.tar.gz

rebadam and others added 6 commits November 21, 2024 17:12
…ons + improved stability to_rdmol

- to_rdmol now has an argument presanitize, which iteratively sanitized - improved bonding/atomic charges,
  until sanitization succeeds.
- There are the functions to_image and get_reaction_image.
…n_image.

- to_image now works with svg in JupyterLab
@dormrod
Copy link
Contributor

dormrod commented Nov 25, 2024

Looks like a great feature!

Just a couple of overall comments:

  1. I tidied up a few little things I spotted as I reviewed this (type-hints, unit-test etc.)
  2. It might also be good to add a notebook example to https://github.com/SCM-NV/PLAMS/tree/trunk/examples, or modify one of the existing ones, to show how to use this new functionality?
  3. We have a new changelog which we will use at the end of the year to produce the "what's new" functionality - please could you also add a line in there? https://github.com/SCM-NV/PLAMS/blob/trunk/CHANGELOG.md

sanitize: bool = True,
properties: bool = True,
assignChirality: bool = False,
presanitize: bool = False,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add presanitize to doc-string with description - might be worth also mentioning that sanitize must also be True for this to apply

@@ -213,10 +219,11 @@ def plams_to_rd_bonds(bo):

if sanitize:
try:
Chem.SanitizeMol(rdmol)
if presanitize:
rdmol = _presanitize(plams_mol, rdmol)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be nice to have add unit-test cases which use this method in unit_tests/test_molecule_interfaces.TestRDKit (see e.g. test_to_rdmol_from_rdmol_roundtrip).

These could take some input molecules that have issues, and check the output is santised as expected.

Do you have some examples for which this santization is useful?

@@ -1290,3 +1297,602 @@ def canonicalize_mol(mol, inplace=False, **kwargs):
ret = mol.copy()
ret.atoms = [at for _, at in sorted(zip(idx_rank, ret.atoms), reverse=True)]
return ret


@requires_optional_package("rdkit")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated as also requires optional package pillow, as users may not be using amspython.

I have also just sorted out the optional dependencies for PLAMS to make them easier to install, so also added the pillow dependency here: https://github.com/SCM-NV/PLAMS/blob/trunk/pyproject.toml#L59



@requires_optional_package("rdkit")
def to_image(mol, remove_hydrogens=True, filename=None, format="svg", as_string=True):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(Added type hints)

* ``mol`` -- PLAMS Molecule object
* ``remove_hydrogens`` -- Wether or not to remove the H-atoms from the image
* ``filename`` -- Optional: Name of image file to be created.
* ``format`` -- One of "svg", "png", "eps", "pdf", "jpeg"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(renamed to fmt to avoid clash with python keyword)

# Draw the image
if classes[format.lower()] is None:
# With AMS version of RDKit MolsToGridImage fails for eps (because of paste)
img = Draw.MolToImage(rdmol, size=(200, 100))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might be nice to have the size as an option to the function?

return img_text


def get_reaction_image_pil(reactants, products, format, width=200, height=100, as_string=True):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Make these "private" (_get_reaction_image_pil) or add to __init__?

@@ -230,6 +238,38 @@ def test_to_smiles_and_from_smiles_requires_rdkit_package(self, plams_mols, smil
with pytest.raises(MissingOptionalPackageError):
from_smiles(smiles[0])

def test_to_image_and_get_reaction_image_can_generate_img_files(self, xyz_folder):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have converted the attached example to a unit test.

It's not too fancy, just makes sure the image files can be generated, but then they can be visually inspected too.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants