DeepMol

repository·master·Indexed 41 days ago

https://github.com/biosystemsum/deepmol

A Python-based machine and deep learning framework for drug discovery and chemoinformatics. It integrates TensorFlow, Keras, Scikit-learn, DeepChem, and RDKit to support molecular data tasks, including featurization, dataset standardization, hyperparameter optimization, and model interpretability via SHAP values.

Tokens
70.2K
Snippets
226
Records
258
Agent score
51%

What's inside DeepMol

  1. Overview of DeepMol

    master

    DeepMol is a Python-based machine and deep learning framework designed for drug discovery. It facilitates chemoinformatics and drug discovery tasks by integrating several key libraries:

    • Tensorflow & Keras: For building and running deep learning models.
    • Scikit-learn: For traditional machine learning tasks.
    • DeepChem: For advanced deep learning applications in drug discovery.
    • RDKit: For molecular data manipulation and chemoinformatics operations.
  2. Overview of the deepmol package subpackages

    master

    The deepmol package is a modular framework for molecular modeling and machine learning. It is organized into several specialized subpackages that cover the entire machine learning lifecycle for molecular data:

    • Data Handling: deepmol.datasets, deepmol.loaders, deepmol.splitters
    • Feature Engineering: deepmol.compound_featurization, deepmol.encoders, deepmol.feature_selection, deepmol.feature_importance, deepmol.tokenizers, deepmol.scalers, deepmol.standardizer
    • Modeling & Optimization: deepmol.models, deepmol.pipeline, deepmol.pipeline_optimization, deepmol.parameter_optimization, deepmol.unsupervised, deepmol.parallelism
    • Evaluation & Metrics: deepmol.evaluator, deepmol.metrics
    • Utilities & Support: deepmol.base, deepmol.imbalanced_learn, deepmol.loggers, deepmol.utils
  3. Overview of the deepmol.compound_featurization package

    master

    The deepmol.compound_featurization package provides a comprehensive suite of tools for converting chemical compounds into numerical representations (features) suitable for machine learning models. It includes various featurization strategies ranging from traditional fingerprints to neural-based embeddings.

    Available Featurization Modules:

    • Base & Frameworks:

      • base_featurizer: Core base classes for featurization.
      • deepchem_featurizers: Integration with DeepChem featurization methods.
      • rdkit_descriptors: Molecular descriptors derived from RDKit.
      • rdkit_fingerprints: Molecular fingerprints derived from RDKit.
    • Fingerprints & Embeddings:

      • mhfp: MinHash Fingerprints.
      • mol2vec: Mol2Vec embeddings.
      • np_classifier_fp: Fingerprints from NP classifier models.
      • one_hot_encoder: One-hot encoding utilities.
      • mixed_descriptors: Combinations of different molecular descriptors.
    • Generators & Subpackages:

      • nc_mfp_generator (and nc_mfp subpackage): Generators for NC-MFP features.
      • neural_npfp_generator (and neural_npfp subpackage): Generators for Neural NPFP features.
    • Utilities:

      • similarity_matrix: Tools for computing similarity matrices between compounds.
  4. Use the deepmol.standardizer package

    master

    The deepmol.standardizer package provides various submodules for molecular standardization, allowing users to clean and normalize chemical structures. The package is organized into specialized modules depending on the standardization logic required:

    • deepmol.standardizer.basic_standardizer: Provides basic standardization functionality.
    • deepmol.standardizer.chembl_standardizer: Implements standardization rules consistent with ChEMBL.
    • deepmol.standardizer.custom_standardizer: Allows for user-defined or custom standardization workflows.
    • deepmol.standardizer.molecular_standardizer: Provides high-level molecular standardization capabilities.
  5. How sampling strategies work in DeepMol imbalanced-learn

    master

    All samplers in deepmol.imbalanced_learn use a sampling_strategy parameter to define how the dataset should be resampled. This parameter accepts several types:

    • float: The desired ratio of the number of samples in the minority class over the number of samples in the majority class after resampling.
    • str: Specifies the class targeted by the resampling. Possible choices are:
      • 'minority': resample only the minority class.
      • 'not minority': resample all classes but the minority class.
      • 'not majority': resample all classes but the majority class.
      • 'all': resample all classes.
      • 'auto': equivalent to 'not majority'.
    • dict: Keys correspond to targeted classes; values correspond to the desired number of samples for each targeted class.
    • callable: A function taking y and returning a dict where keys are targeted classes and values are the desired number of samples.
  6. How molecular standardization works in DeepMol

    master

    Standardization in DeepMol is the process of converting chemical structures into a consistent format using one of three primary approaches:

    1. BasicStandardizer: Performs only sanitization (Kekulize, valency checks, aromaticity, etc.). Use this for minimal changes that ensure chemical validity.
    2. CustomStandardizer: A highly configurable approach where you provide a dictionary of specific tasks (e.g., REMOVE_STEREO, NEUTRALISE_CHARGE). Use this when your machine learning pipeline requires specific data properties (like no chirality or no isotopes).
    3. ChEMBLStandardizer: Uses established ChEMBL protocols for standardization and salt stripping. Use this to align your dataset with ChEMBL-style chemical representations.

    All standardizers are used by calling .standardize(dataset, inplace=True) on the standardizer instance, where dataset is a SmilesDataset object.

  7. Understand molecular feature types in DeepMol

    master

    DeepMol categorizes molecular features into five dimensions based on the complexity and information they provide. As the dimensionality increases, so does the computational cost:

    • 0D features: Global descriptors of the whole molecule (e.g., molecular weight, atom counts, bond counts).
    • 1D features: Substructure descriptors, typically implemented as molecular fingerprints.
    • 2D features: Topological descriptors based on the molecular graph (e.g., number of rings, rotatable bonds).
    • 3D features: Geometrical descriptors describing the molecule's 3D structure. Note that calculating these requires generating 3D conformers, which is computationally intensive.
    • 4D features: Descriptors characterizing interactions with a receptor active site or multiple conformational states (e.g., molecular dynamics).
  8. Use featurization classes in DeepMol

    master

    Featurization methods are implemented as classes within the deepmol.compound_featurization module.

    To generate features, you instantiate a featurization class and call its .featurize() method. This method accepts a dataset object as input and returns a featurized dataset. These classes can be used individually or integrated into a pipeline.

    Available featurization methods:

    • MorganFingerprint
    • AtomPairFingerprint
    • LayeredFingerprint
    • RDKFingerprint
    • MACCSkeysFingerprint
    • TwoDimensionDescriptors
    • WeaveFeat
    • CoulombFeat
    • CoulombEigFeat
    • ConvMolFeat
    • MolGraphConvFeat
    • SmileImageFeat
    • SmilesSeqFeat
    • MolGanFeat
    • All3DDescriptors
  9. Optimize pipelines using PipelineOptimization

    master

    The PipelineOptimization class uses the Optuna library to automate hyperparameter tuning and architecture selection for machine learning pipelines. It can optimize between different Transformers (featurizers, scalers, etc.) and Predictors (models).

    To perform optimization, you must provide an objective function (or a preset) that defines the configuration space. The pipeline respects the order of steps returned by the objective function as a list of (name, object) tuples.

    # Example of the required return format for objective functions
    steps = [('featurizer', featurizer), ('scaler', scaler), ('model', model)]
  10. Perform hyperparameter tuning using cross-validation

    master

    Use HyperparameterOptimizerCV to tune a model using k-fold cross-validation. This is more robust than a single validation split but more computationally intensive.

    Workflow:

    1. Initialize HyperparameterOptimizerCV with the model class, Metric, maximize_metric, cv (number of folds), n_iter_search, params_dict, and model_type="sklearn".
    2. Call .fit(train_dataset=...) (Note: only train_dataset is required as CV handles the splits).
    3. Access best_hyperparams and all_results (which contains detailed CV scores).
    from deepmol.parameter_optimization import HyperparameterOptimizerCV
    from sklearn.svm import SVC
    from deepmol.metrics import Metric
    from sklearn.metrics import accuracy_score
    
    params_dict_svc = {"C": [1.0, 1.2, 0.8], "kernel": ['linear', 'poly', 'rbf']}
    optimizer = HyperparameterOptimizerCV(
        SVC, 
        metric=Metric(accuracy_score),
        maximize_metric=True,
        cv=3,
        n_iter_search=2,
        params_dict=params_dict_svc,
        model_type="sklearn"
    )
    
    best_svm, best_hyperparams, all_results = optimizer.fit(train_dataset=train_dataset)
    
    # Evaluate best model
    best_svm.evaluate(test_dataset, metrics=[Metric(accuracy_score)])
  11. Build a supervised learning pipeline

    master

    To build a supervised learning pipeline, define a list of tuples where each tuple contains a unique string name and a step object. The Pipeline constructor accepts this list and an optional path parameter specifying where the pipeline should be saved.

    Example workflow:

    1. Define steps (Standardization $\rightarrow$ Fingerprinting $\rightarrow$ Feature Selection $\rightarrow$ Model).
    2. Initialize Pipeline with steps and path.
    3. Load data using CSVLoader and split using SingletaskStratifiedSplitter.
    4. Call .fit(train) to train the pipeline.
    5. Call .predict(test) to get predictions.
    from deepmol.pipeline import Pipeline
    from sklearn.ensemble import RandomForestClassifier
    from deepmol.models import SklearnModel
    from deepmol.feature_selection import LowVarianceFS
    from deepmol.compound_featurization import MorganFingerprint
    from deepmol.standardizer import BasicStandardizer
    
    steps = [
        ('basic standardizing', BasicStandardizer()),
        ('morgan fingerprints', MorganFingerprint(radius=2, size=1024)),
        ('low variance feature selection', LowVarianceFS(threshold=0.1)),
        ('random forest', SklearnModel(model=RandomForestClassifier(n_jobs=-1, random_state=42)))
    ]
    pipeline = Pipeline(steps=steps, path="../../examples/notebooks/DRD2")
    
    # Training and prediction
    pipeline.fit(train)
    y_pred = pipeline.predict(test)
  12. Featurize datasets for GraphConvModel

    master

    Before performing hyperparameter tuning with a GraphConvModel, you must featurize your datasets using ConvMolFeat. This ensures the features are compatible with the specific model architecture.

    from deepmol.compound_featurization import ConvMolFeat
    
    ConvMolFeat().featurize(train_dataset, inplace=True)
    ConvMolFeat().featurize(valid_dataset, inplace=True)
    ConvMolFeat().featurize(test_dataset, inplace=True)