Skip to main content

batchmatch

This package accelerates the matching of a query molecule to a large batch of patterns, given as SMARTS strings. This is a common subroutine in template-based retrosynthesis, where one is interested in which of 10-100k reaction templates are applicable for any given molecule. The key insight is that we can first use cheap heuristics to rule out patterns that can never match to avoid solving an expensive subgraph isomorphism problem.

Installation

Install batchmatch using pip:

pip install batchmatch 

Usage

The batchmatch library has one core object PatternLibrary, which takes as input a list of SMARTS strings and allows users to quickly query molecules against them using its match() method:

import batchmatch
from rdkit import Chem

with open("smarts.txt", "r") as f:
    smarts = f.read().splitlines()
query = Chem.MolFromSmiles("Cn1c(=O)c2c(ncn2C)n(C)c1=O")

# Instantiate the pattern library 
# Importantly, this only has to be done once, as it computes some indices  
patterns = batchmatch.PatternLibrary(
    smarts=smarts,
    consolidate=True,  # default; whether to deduplicate SMARTS 
    filters=batchmatch.DEFAULT_FILTERS,  # default; the filter heuristics used 
    chiral=True,  # default; whether to use chirality when matching
)

# Match the query molecule
for i in patterns.match(query):
    print(f"Query matches {smarts[i]}")

How does it work?

Under the hood, PatternLibrary first consolidates (deduplicates) the input SMARTS. Then, it applies a series of heuristics (filters) to rule out blatantly unmatching patterns, which can be run in isolation with the could_match() method. Currently, batchmatch supports 4 filters:

  1. RingFilter: rules out patterns with rings not present in the query.
  2. AtomCountFilter: rules out patterns based on their atom type counts. For example, a pattern with three oxygens can never match a molecule with only one.
  3. RDKitFilter: uses a modified version of the RDKit fingerprint. Specifically, we rule out a pattern if their fingerprint's on bits are not a subset of the query's.
  4. PatternFilter: the same as above but using RDKit's PatternFingerprint, which was adapted from MHN-React.

Empirically, we find the Ring > Atom > RDKit filter combination (in that order) works best and is what we use by default. Note that the bulk of the computation used in the filters (namely, computing statistics about the patterns) is done and cached on instantiation of PatternLibrary. Finally, we run an exhaustive substructure match with the remaining patterns to yield the final results.

Limitations

While batchmatch is well-tested on templates from AIZynthFinder and queries from ChEMBL, it is difficult to anticipate every use case due to the expansiveness of SMARTS. The strategies used herein are highly tailored towards SMARTS extracted from reaction templates, which only use a narrow subset of the specification language. I would always recommend running some spot checks and benchmarks on your data to see if batchmatch works well. For convenience,

batchmatch.PatternLibrary(smarts=smarts, consolidate=False, filters=[])

performs the brute-force strategy of matching against every SMARTS with no extra tricks!

Benchmarks

Below, I have run an informal benchmarks (test/benchmark.py) on my laptop (Intel Core i7-12800H processor), using the AIZynthFinder template set and 1k random molecules from ZINC. For each combination of 0-4 filters, I report the best permutation below in terms of match time (s) per molecule. I also consider the precision, the number of true matches divided by number of exhaustive substructure matches. An ideal filter (Ideal) would filter out exactly the true matches and therefore have a precision of 1, but not a match time of 0 because one still has to perform the matching routine.

Strategy Match Time Precision
Naive (no consolidate) 0.224459 -
Naive 0.146469 0.002347
Ring 0.094124 0.004005
Atom 0.029731 0.022558
Ring > Atom 0.023717 0.029551
Pattern 0.01542 0.076733
Ring > Pattern 0.013881 0.076841
RDKit 0.013821 0.102975
RDKit > Pattern 0.013694 0.122205
Ring > RDKit > Pattern 0.012047 0.122327
Ring > RDKit 0.011598 0.118188
Ring > Atom > Pattern 0.008725 0.143111
Atom > Pattern 0.008391 0.142975
Ring > Atom > RDKit > Pattern 0.007981 0.210596
Atom > RDKit > Pattern 0.007611 0.210462
Atom > RDKit 0.00745 0.181535
Ring > Atom > RDKit 0.007396 0.20821
Ideal 0.000496 1.0

Overall, we find that our default setting is almost 20x faster than a naive implementation and over 80x as precise! Compared to the Pattern strategy in MHN-React, we are 2x faster. However, there is still a lot of potential for improvement with a 15x slowdown and 5x decreased precision compared to the theoretical optimum. Likely, porting our filters to C++ will yield easy speed improvements, though I lack the knowledge to do this currently.

Metadata

Release files for batchmatch 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for batchmatch 0.1.0
File Size Uploaded Provenance
batchmatch-0.1.0.tar.gz 954.1 kB With provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for batchmatch 0.1.0
File Interpreter ABI Platform
batchmatch-0.1.0-py3-none-any.whl Python 3 none any Details

Total release size: 962.7 kB

Release files / batchmatch-0.1.0.tar.gz

Download URL batchmatch-0.1.0.tar.gz
Size 954.1 kB
Tags Source
SHA-256 checksum
How to use checksums
bd7f49b2d409c5dba419c99d8ce5cc39152842b869a0f4b500426ab179c959b9
BLAKE2b-256 checksum
How to use checksums
4fa2fbf7b6a255e23af5d8ec7c910f81565d82848cc7df324e977d4805bba8e6
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.12.9

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Jun 10, 2025.

Transparency log

Release files / batchmatch-0.1.0-py3-none-any.whl

Download URL batchmatch-0.1.0-py3-none-any.whl
Size 8.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
ee99c0ec70a74e548fc6480e02c3895e314067b74d1bed5626c2599377aec365
BLAKE2b-256 checksum
How to use checksums
887b584fde90754e977e0f272ab6616dfe80908c5c9c51086e9beff02aa2e88c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.12.9

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Jun 10, 2025.

Transparency log

Security for batchmatch 0.1.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

All 2 files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for batchmatch-0.1.0.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Jun 10, 2025.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files Full provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page