Molfeat - Molecular Featurization Hub
Overview
Molfeat is a comprehensive Python library for molecular featurization that unifies 100+ pre-trained embeddings and hand-crafted featurizers. Convert chemical structures (SMILES strings or RDKit molecules) into numerical representations for machine learning tasks including QSAR modeling, virtual screening, similarity searching, and deep learning applications. Features fast parallel processing, scikit-learn compatible transformers, and built-in caching.
When to Use This Skill
This skill should be used when working with:
- Molecular machine learning: Building QSAR/QSPR models, property prediction
- Virtual screening: Ranking compound libraries for biological activity
- Similarity searching: Finding structurally similar molecules
- Chemical space analysis: Clustering, visualization, dimensionality reduction
- Deep learning: Training neural networks on molecular data
- Featurization pipelines: Converting SMILES to ML-ready representations
- Cheminformatics: Any task requiring molecular feature extraction
Installation
uv pip install molfeat
uv pip install "molfeat[all]"
Optional dependencies for specific featurizers:
molfeat[dgl] - GNN models (GIN variants)
molfeat[graphormer] - Graphormer models
molfeat[transformer] - ChemBERTa, ChemGPT, MolT5
molfeat[fcd] - FCD descriptors
molfeat[map4] - MAP4 fingerprints
Core Concepts
Molfeat organizes featurization into three hierarchical classes:
1. Calculators (molfeat.calc)
Callable objects that convert individual molecules into feature vectors. Accept RDKit Chem.Mol objects or SMILES strings.
Use calculators for:
- Single molecule featurization
- Custom processing loops
- Direct feature computation
Example:
from molfeat.calc import FPCalculator
calc = FPCalculator("ecfp", radius=3, fpSize=2048)
features = calc("CCO")
2. Transformers (molfeat.trans)
Scikit-learn compatible transformers that wrap calculators for batch processing with parallelization.
Use transformers for:
- Batch featurization of molecular datasets
- Integration with scikit-learn pipelines
- Parallel processing (automatic CPU utilization)
Example:
from molfeat.trans import MoleculeTransformer
from molfeat.calc import FPCalculator
transformer = MoleculeTransformer(FPCalculator("ecfp"), n_jobs=-1)
features = transformer(smiles_list)
3. Pretrained Transformers (molfeat.trans.pretrained)
Specialized transformers for deep learning models with batched inference and caching.
Use pretrained transformers for:
- State-of-the-art molecular embeddings
- Transfer learning from large chemical datasets
- Deep learning feature extraction
Example:
from molfeat.trans.pretrained import PretrainedMolTransformer
transformer = PretrainedMolTransformer("ChemBERTa-77M-MLM", n_jobs=-1)
embeddings = transformer(smiles_list)
Quick Start Workflow
Basic Featurization
import datamol as dm
from molfeat.calc import FPCalculator
from molfeat.trans import MoleculeTransformer
smiles = ["CCO", "CC(=O)O", "c1ccccc1", "CC(C)O"]
calc = FPCalculator("ecfp", radius=3)
transformer = MoleculeTransformer(calc, n_jobs=-1)
features = transformer(smiles)
print(f"Shape: {features.shape}")
Save and Load Configuration
transformer.to_state_yaml_file("featurizer_config.yml")
loaded = MoleculeTransformer.from_state_yaml_file("featurizer_config.yml")
Handle Errors Gracefully
transformer = MoleculeTransformer(
calc,
n_jobs=-1,
ignore_errors=True,
verbose=True
)
features = transformer(smiles_with_errors)
Choosing the Right Featurizer
For Traditional Machine Learning (RF, SVM, XGBoost)
Start with fingerprints:
FPCalculator("ecfp", radius=3, fpSize=2048)
FPCalculator("maccs")
FPCalculator("map4")
For interpretable models:
from molfeat.calc import RDKitDescriptors2D
RDKitDescriptors2D()
from molfeat.calc import MordredDescriptors
MordredDescriptors()
Combine multiple featurizers:
from molfeat.trans import FeatConcat
concat = FeatConcat([
FPCalculator("maccs"),
FPCalculator("ecfp")
])
For Deep Learning
Transformer-based embeddings:
PretrainedMolTransformer("ChemBERTa-77M-MLM")
PretrainedMolTransformer("ChemGPT-1.2B")
Graph neural networks:
PretrainedMolTransformer("gin-supervised-masking")
PretrainedMolTransformer("gin-supervised-infomax")
PretrainedMolTransformer("Graphormer-pcqm4mv2")
For Similarity Searching
FPCalculator("ecfp")
FPCalculator("maccs")
FPCalculator("map4")
from molfeat.calc import USRDescriptors
USRDescriptors()
For Pharmacophore-Based Approaches
FPCalculator("fcfp")
from molfeat.calc import CATSCalculator
CATSCalculator(mode="2D")
FPCalculator("gobbi2D")
Common Workflows
Building a QSAR Model
from molfeat.trans import MoleculeTransformer
from molfeat.calc import FPCalculator
from sklearn.ensemble import RandomForestRegressor
from sklearn.model_selection import cross_val_score
transformer = MoleculeTransformer(FPCalculator("ecfp"), n_jobs=-1)
X = transformer(smiles_train)
model = RandomForestRegressor(n_estimators=100)
scores = cross_val_score(model, X, y_train, cv=5)
print(f"RΒ² = {scores.mean():.3f}")
transformer.to_state_yaml_file("production_featurizer.yml")
Virtual Screening Pipeline
from sklearn.ensemble import RandomForestClassifier
transformer = MoleculeTransformer(FPCalculator("ecfp"), n_jobs=