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

Skip scaffold generation on invalid molecules #3520

Merged
merged 3 commits into from
Aug 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 24 additions & 13 deletions deepchem/splits/splitters.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,16 @@
Contains an abstract base class that supports chemically aware data splits.
"""
import inspect
import itertools
import logging
import os
import random
import tempfile
import itertools
import logging
from typing import Any, Dict, List, Iterator, Optional, Sequence, Tuple
from typing import Any, Dict, Iterator, List, Optional, Sequence, Tuple, Union

import deepchem as dc
import numpy as np
import pandas as pd

import deepchem as dc
from deepchem.data import Dataset, DiskDataset
from deepchem.utils import get_print_threshold

Expand Down Expand Up @@ -1274,7 +1273,8 @@ def split(
return train_inds, valid_inds, test_inds


def _generate_scaffold(smiles: str, include_chirality: bool = False) -> str:
def _generate_scaffold(smiles: str,
include_chirality: bool = False) -> Union[str, None]:
"""Compute the Bemis-Murcko scaffold for a SMILES string.

Bemis-Murcko scaffolds are described in DOI: 10.1021/jm9602928.
Expand Down Expand Up @@ -1309,6 +1309,12 @@ def _generate_scaffold(smiles: str, include_chirality: bool = False) -> str:
raise ImportError("This function requires RDKit to be installed.")

mol = Chem.MolFromSmiles(smiles)
if mol is None:
rbharath marked this conversation as resolved.
Show resolved Hide resolved
logger.info(
'Not generating scaffold for smiles %s - invalid smiles string' %
smiles)
return None
Copy link
Member

Choose a reason for hiding this comment

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

What happened previously when mol was None? We should make sure not to break existing functionality

Copy link
Contributor Author

@arunppsg arunppsg Aug 11, 2023

Choose a reason for hiding this comment

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

It throws ValueError and the code does not execute.

Copy link
Member

Choose a reason for hiding this comment

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

Can you add this to the docstring to explain the difference?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

added docs in 2f35436


scaffold = MurckoScaffoldSmiles(mol=mol, includeChirality=include_chirality)
return scaffold

Expand Down Expand Up @@ -1502,9 +1508,13 @@ class ScaffoldSplitter(Splitter):
.. [1] Bemis, Guy W., and Mark A. Murcko. "The properties of known drugs.
1. Molecular frameworks." Journal of medicinal chemistry 39.15 (1996): 2887-2893.

Note
----
This class requires RDKit to be installed.
Notes
-----
- This class requires RDKit to be installed.

- When a SMILES representation of a molecule is invalid, the splitter skips processing
the datapoint i.e it will not include the molecule in any splits.

"""

def split(
Expand Down Expand Up @@ -1588,10 +1598,11 @@ def generate_scaffolds(self,
if ind % log_every_n == 0:
logger.info("Generating scaffold %d/%d" % (ind, data_len))
scaffold = _generate_scaffold(smiles)
if scaffold not in scaffolds:
scaffolds[scaffold] = [ind]
else:
scaffolds[scaffold].append(ind)
if scaffold is not None:
Copy link
Member

Choose a reason for hiding this comment

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

What happened previously in these cases?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Previously, the code will not execute (ref: #3512) - but now, it can execute.

if scaffold not in scaffolds:
scaffolds[scaffold] = [ind]
else:
scaffolds[scaffold].append(ind)

# Sort from largest to smallest scaffold sets
scaffolds = {key: sorted(value) for key, value in scaffolds.items()}
Expand Down
11 changes: 11 additions & 0 deletions deepchem/splits/tests/test_scaffold_splitter.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,14 @@ def test_scaffolds(self):
# has to be smaller or equal than number of total molecules
scaffolds_separate_cnt = len(scaffolds_separate)
self.assertTrue(scaffolds_separate_cnt <= train_dataset.X.shape[0])

def test_generate_scaffold(self):
from deepchem.splits.splitters import _generate_scaffold
valid_smiles = r's1cc(nc1\[N]=C(\N)N)C'
scaffold = _generate_scaffold(valid_smiles)
self.assertTrue(scaffold == 'c1cscn1')

# Invalid because valence for atom 5 N is greater than permitted (4)
invalid_smiles = r's1cc(nc1\[NH]=C(\N)N)C'
scaffold = _generate_scaffold(invalid_smiles)
self.assertIsNone(scaffold)
Loading