Skip to content

Commit

Permalink
suppress deprecation warning in RDKitDescriptors
Browse files Browse the repository at this point in the history
  • Loading branch information
PatWalters committed Jul 31, 2024
1 parent 610dc60 commit d55309e
Showing 1 changed file with 7 additions and 24 deletions.
31 changes: 7 additions & 24 deletions useful_rdkit_utils/descriptors.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
from rdkit.Chem.Descriptors import MolWt, MolLogP, NumHDonors, NumHAcceptors, TPSA
from rdkit.Chem.rdchem import Mol
from tqdm.auto import tqdm
import warnings
from rdkit import RDLogger



# ----------- Descriptors and fingerprints
Expand Down Expand Up @@ -74,25 +75,6 @@ def smi2numpy_fp(smi: str, radius: int = 2, nBits: int = 2048) -> np.ndarray:
return arr


# Code borrowed from Brian Kelley's Descriptastorus
# https://github.com/bp-kelley/descriptastorus
FUNCS = {name: func for name, func in Descriptors.descList}


def apply_func(name, mol):
"""Apply an RDKit descriptor calculation to a molecule
:param name: descriptor name
:param mol: RDKit molecule
:return:
"""
try:
return FUNCS[name](mol)
except:
logging.exception("function application failed (%s->%s)", name, Chem.MolToSmiles(mol))
return None


class RDKitDescriptors:
""" Calculate RDKit descriptors"""

Expand All @@ -108,7 +90,7 @@ def __init__(self: "RDKitDescriptors", hide_progress: bool = False) -> None:
:rtype: None
"""
self.hide_progress = hide_progress
self.desc_names: List[str] = [desc_name for desc_name, _ in sorted(Descriptors.descList)]
self.desc_names: List[str] = sorted([x[0] for x in Descriptors.descList])

def calc_mol(self, mol: Mol) -> np.ndarray:
"""Calculate descriptors for an RDKit molecule
Expand All @@ -117,9 +99,10 @@ def calc_mol(self, mol: Mol) -> np.ndarray:
:return: a numpy array with descriptors
"""
if mol is not None:
with warnings.catch_warnings():
warnings.simplefilter("ignore", category=DeprecationWarning)
res = np.array([apply_func(name, mol) for name in self.desc_names], dtype=float)
RDLogger.DisableLog('rdApp.warning')
res_dict = Descriptors.CalcMolDescriptors(mol)
RDLogger.EnableLog('rdApp.warning')
res = np.array([res_dict[x] for x in self.desc_names])
else:
res = np.array([None] * len(self.desc_names))
return res
Expand Down

0 comments on commit d55309e

Please sign in to comment.