Chai Discovery raises $400M at $3.8bn as AI drug discovery moves from promise to deployment
Back to Tutorials
techTutorialintermediate

Chai Discovery raises $400M at $3.8bn as AI drug discovery moves from promise to deployment

July 15, 20264 views5 min read

Learn how to build molecular fingerprint generators using RDKit and Python, a fundamental technique in AI drug discovery pipelines.

Introduction

AI drug discovery is revolutionizing the pharmaceutical industry by accelerating the identification and development of new medicines. In this tutorial, we'll explore how to build a simple molecular fingerprint generator using Python and RDKit, a powerful cheminformatics library. This foundational tool is essential for AI-driven drug discovery pipelines, where molecular structures are converted into numerical representations that machine learning models can process. Understanding molecular fingerprints is crucial for anyone working in computational drug discovery, as they form the backbone of virtual screening and drug design algorithms.

Prerequisites

  • Python 3.7 or higher installed on your system
  • Basic understanding of molecular chemistry concepts
  • Intermediate knowledge of Python programming
  • RDKit library installed (can be installed via pip)

Why we need these prerequisites: RDKit is essential because it provides the core functionality for molecular manipulation and analysis. Understanding basic chemistry helps you interpret the results correctly, while Python knowledge ensures you can modify and extend the code as needed.

Step-by-Step Instructions

1. Install Required Dependencies

First, we need to install RDKit and other necessary libraries:

pip install rdkit
pip install numpy
pip install matplotlib

Why this step is important: RDKit is the primary library for cheminformatics operations in Python. It provides molecular structure handling, fingerprint generation, and various cheminformatics algorithms that are fundamental to AI drug discovery.

2. Import Required Libraries

Create a new Python file and import the necessary modules:

from rdkit import Chem
from rdkit.Chem import AllChem
import numpy as np
import matplotlib.pyplot as plt

Why we import these libraries: Chem provides core molecular functionality, AllChem contains advanced cheminformatics algorithms including fingerprint generation, numpy handles numerical operations, and matplotlib allows us to visualize molecular fingerprints.

3. Create a Simple Molecule and Generate Its Fingerprint

Let's start by creating a simple molecule and generating its molecular fingerprint:

# Create a simple molecule (aspirin)
mol = Chem.MolFromSmiles('CC(=O)OC1=CC=CC=C1C(=O)O')

# Generate Morgan fingerprint (radius=2, length=1024)
fingerprint = AllChem.GetMorganFingerprintAsBitVect(mol, radius=2, nBits=1024)

print(f'Molecule: {Chem.MolToSmiles(mol)}')
print(f'Fingerprint length: {len(fingerprint)}')
print(f'Number of 1s in fingerprint: {fingerprint.GetNumOnBits()}')

Why this step is important: Morgan fingerprints are widely used in drug discovery because they capture molecular structure information at different levels of detail. The radius parameter controls the level of structural detail, while the bit vector length determines the resolution of the fingerprint.

4. Convert Fingerprint to Numerical Array

Convert the fingerprint to a numerical array for machine learning processing:

# Convert fingerprint to numpy array
fp_array = np.zeros((1,))
AllChem.DataToBitString(fingerprint, fp_array)

# Alternative method using GetRawData
fp_list = list(fingerprint)
fp_numpy = np.array(fp_list)

print(f'Fingerprint array shape: {fp_numpy.shape}')
print(f'First 10 elements: {fp_numpy[:10]}')

Why we convert to arrays: Machine learning models require numerical input. Converting fingerprints to numpy arrays makes them compatible with popular ML libraries like scikit-learn, TensorFlow, or PyTorch, which are commonly used in AI drug discovery pipelines.

5. Compare Multiple Molecules

Let's compare fingerprints of different molecules to understand similarity calculation:

# Create multiple molecules
molecules = [
    'CC(=O)OC1=CC=CC=C1C(=O)O',  # Aspirin
    'CC(C)C1=CC=C(C=C1)C(=O)O',   # Ibuprofen
    'c1ccccc1O'                   # Phenol
]

# Generate fingerprints for all molecules
fingerprints = []
for i, smiles in enumerate(molecules):
    mol = Chem.MolFromSmiles(smiles)
    fp = AllChem.GetMorganFingerprintAsBitVect(mol, radius=2, nBits=1024)
    fingerprints.append(fp)
    print(f'Molecule {i+1}: {smiles}')
    print(f'  Similarity to Aspirin: {AllChem.TanimotoSimilarity(fingerprints[0], fp):.3f}')
    print()

Why this comparison is important: Tanimoto similarity is a standard metric in cheminformatics for measuring molecular similarity. This approach is fundamental to virtual screening, where researchers search large compound libraries for molecules similar to a target structure, which is a core technique in AI drug discovery.

6. Visualize Fingerprint Similarity Matrix

Create a heatmap to visualize similarities between molecules:

# Create similarity matrix
n_molecules = len(fingerprints)
similarity_matrix = np.zeros((n_molecules, n_molecules))

for i in range(n_molecules):
    for j in range(n_molecules):
        similarity_matrix[i][j] = AllChem.TanimotoSimilarity(fingerprints[i], fingerprints[j])

# Plot heatmap
plt.figure(figsize=(8, 6))
plt.imshow(similarity_matrix, cmap='viridis', interpolation='none')
plt.colorbar(label='Tanimoto Similarity')
plt.xticks(range(n_molecules), [f'Mol {i+1}' for i in range(n_molecules)])
plt.yticks(range(n_molecules), [f'Mol {i+1}' for i in range(n_molecules)])
plt.title('Molecular Similarity Matrix')
plt.tight_layout()
plt.show()

Why visualization matters: Visualizing similarity matrices helps researchers quickly identify clusters of similar compounds, which is crucial for lead optimization and drug design. This kind of analysis is performed at scale in AI-driven drug discovery platforms like Chai Discovery.

7. Build a Simple Fingerprint Database

Create a simple database structure for storing and retrieving molecular fingerprints:

# Create a simple database structure
molecule_database = {}

for i, smiles in enumerate(molecules):
    mol = Chem.MolFromSmiles(smiles)
    fp = AllChem.GetMorganFingerprintAsBitVect(mol, radius=2, nBits=1024)
    molecule_database[smiles] = {
        'molecule': mol,
        'fingerprint': fp,
        'name': f'Molecule_{i+1}'
    }

# Function to search for similar molecules
def find_similar_molecules(query_smiles, database, threshold=0.7):
    query_mol = Chem.MolFromSmiles(query_smiles)
    query_fp = AllChem.GetMorganFingerprintAsBitVect(query_mol, radius=2, nBits=1024)
    
    similar_molecules = []
    for smiles, data in database.items():
        similarity = AllChem.TanimotoSimilarity(query_fp, data['fingerprint'])
        if similarity >= threshold:
            similar_molecules.append((smiles, similarity))
    
    return sorted(similar_molecules, key=lambda x: x[1], reverse=True)

# Test the search function
query = 'CC(=O)OC1=CC=CC=C1C(=O)O'
results = find_similar_molecules(query, molecule_database)
print('Similar molecules to aspirin:')
for smiles, similarity in results:
    print(f'  {smiles}: {similarity:.3f}')

Why this database approach is valuable: This structure mimics real-world AI drug discovery systems where large compound libraries are stored and searched for similar molecules. Modern platforms like Chai Discovery use similar techniques at massive scales to identify promising drug candidates.

Summary

In this tutorial, we've built a foundational molecular fingerprinting system using RDKit. We learned how to generate Morgan fingerprints, compare molecular structures, visualize similarity matrices, and create a simple search database. These concepts form the core of AI drug discovery pipelines, where machine learning models use these numerical representations to predict molecular properties, identify drug candidates, and optimize lead compounds. As companies like Chai Discovery scale their operations, they rely on exactly these types of computational tools to accelerate the drug discovery process from years to months.

The techniques demonstrated here are directly applicable to real-world AI drug discovery workflows, providing a practical foundation for building more sophisticated systems that can handle large-scale molecular screening and analysis.

Source: TNW Neural

Related Articles