SubstructLibrary#
- class nvmolkit.substruct_library.SubstructLibrary(
- config: SubstructSearchConfig | None = None,
- usePatternFingerprints: bool = True,
A collection of molecules kept on the GPU for repeated substructure searches.
This is the GPU counterpart of RDKit’s
rdSubstructLibrary.SubstructLibrary: target molecules are prepared and uploaded once, then any number of queries run against them. Use it when the same targets are searched many times; for a one-off batch of targets and queries,nvmolkit.substructure.hasSubstructMatch()avoids the upload step.Molecules added with
addMol()oraddMols()receive stable indices in insertion order and become searchable afterfinalize(). Queries keep seeing the last finalized collection while newly added molecules are pending, so the library can grow between query rounds.Which calls wait:
addMol(),addMols(), andfinalize()return when their work is done. They also wait for any queries currently running to finish.getMatches(),countMatches(), andhasMatch()return aconcurrent.futures.Futureright away and search in the background, so you can keep working or start more queries. Up tomaxConcurrentQueriesqueries run at once; the rest wait their turn. Callresult()on the future to get the answer.getMatchesSync(),countMatchesSync(), andhasMatchSync()start a query the same way and return its answer once it is ready.
For the best throughput, start all of your queries before calling
result()on the first one, rather than waiting for each answer before starting the next.A query’s errors, including querying before the first
finalize(), are raised by the future’sresult()or by the*Synccall. A query searches the molecules that were finalized when it begins running, which may include molecules finalized after it was started. Do not wait for a query’s result inside another future’s done-callback; that can hang.The library can be used from several threads at once.
Matching follows RDKit’s
SubstructLibrarywithuseChirality=False: recursive SMARTS are supported, and stereochemistry in targets and in SMILES queries is ignored. SMARTS queries that specify atom chirality (@or@@) are not supported; their results raiseRuntimeError. Targets outside the GPU representation limits (more than 128 atoms, high-degree atoms, isotopes above 255, or dative and other unsupported bond types) are matched with RDKit on the CPU, so results cover every added molecule.Example:
from rdkit import Chem from nvmolkit.substruct_library import SubstructLibrary library = SubstructLibrary() library.addMols(Chem.MolFromSmiles(smiles) for smiles in ["CCO", "c1ccccc1O", "CCN"]) library.finalize() library.getMatchesSync(Chem.MolFromSmarts("[OX2H]")) # [0, 1] # Start several queries, then collect their results. futures = [library.hasMatch(Chem.MolFromSmarts(smarts)) for smarts in ["N", "S"]] [future.result() for future in futures] # [True, False]
- Parameters:
config – Search configuration.
algorithmselects the"dfs"(default) or"gsi"backend, andgpuIdsspreads the molecules across several GPUs.batchSize,workerThreads, andpreprocessingThreadstune throughput;maxMatchesanduniquifydo not apply because queries return molecule indices.usePatternFingerprints – Prescreen queries using RDKit pattern fingerprints before doing a full match.
- __init__(
- config: SubstructSearchConfig | None = None,
- usePatternFingerprints: bool = True,
Create an empty library.
- property pendingSize: int#
Number of molecules added since the last
finalize().
- property maxConcurrentQueries: int#
How many queries can run at once, based on the GPU memory left after
finalize(); 0 before it.
- addMol(molecule: Mol) int#
Copy
moleculeinto the library and return its index.The molecule becomes searchable after the next
finalize().
- addMols(
- molecules: Iterable[Mol],
Copy
moleculesinto the library and return their indices.
- finalize() None#
Upload molecules added since the last call and make them searchable.
Must be called at least once before querying; until then, query results raise
RuntimeError. Queries that have not begun running yet will search the newly finalized molecules. If it raises, the previously finalized molecules stay searchable and the new ones stay pending.
- getMatches(
- query: Mol,
- maxResults: int = -1,
Start a query and return a future for the indices of matching molecules, without waiting.
- Parameters:
query – Query molecule, typically from
Chem.MolFromSmartsorChem.MolFromSmiles.maxResults – Return at most this many indices, keeping the lowest. -1 (default) returns all.
- Returns:
Future resolving to matching indices in ascending order. Its
result()raises any error from the search.
- countMatches(
- query: Mol,
Start a query and return a future for the number of matching molecules, without waiting.
- hasMatch(
- query: Mol,
Start a query and return a future for whether any molecule matches, without waiting.
- getMatchesSync(
- query: Mol,
- maxResults: int = -1,
Like
getMatches(), but wait for and return the matching indices.
- countMatchesSync(
- query: Mol,
Like
countMatches(), but wait for and return the number of matches.
- hasMatchSync(query: Mol) bool#
Like
hasMatch(), but wait for and return whether any molecule matches.