duckSMILES GitHub ↗

Embedded Python API

Use typed arguments with a DuckDB connection configured by your application.

View the Python source

DescriptorRow

  • formula: str | None
  • weight: float | None

MoleculeReader

  • __init__(self, connection: DuckDBPyConnection)
  • describe(self, molecules: list[str | None]) → list[DescriptorRow]

    Compute each descriptor per distinct SMILES, then restore input order.

DescriptorRevision

  • implementation: str
  • standardization: str

CachedDescriptors

  • rows: tuple[DescriptorRow, ...]
  • new_entries: int

DescriptorCache

Keep native descriptor results, including invalid inputs, per host-provided revision.

  • __init__(self, connection: DuckDBPyConnection)
  • describe(self, revision: DescriptorRevision, molecules: list[str | None]) → CachedDescriptors

MorganFingerprint

  • radius: int = 2
  • bits: int = 2048

MassRange

  • lower: float
  • upper: float

PatternId

  • value: str

FingerprintFilter

  • reference: str
  • threshold: float
  • definition: MorganFingerprint = MorganFingerprint()

MoleculeSearchSpec

  • mass: MassRange | None = None
  • fingerprint: FingerprintFilter | None = None
  • pattern: PatternId | None = None
  • limit: int = 100

MoleculeSearchHit

  • molecule_id: int
  • mass: float | None
  • score: float | None

MoleculeSearchResult

  • state: str
  • hits: tuple[MoleculeSearchHit, ...]

MoleculeSearch

Search one host-registered relation with a closed set of predicate types.

  • __init__(self, connection: DuckDBPyConnection, relation: str, patterns: Mapping[PatternId, str])
  • search(self, spec: MoleculeSearchSpec) → MoleculeSearchResult

SimilarityHit

  • molecule_id: int
  • score: float

SimilarityResult

  • hits: tuple[SimilarityHit, ...]
  • candidates_scored: int

SimilarityEdge

  • source: int
  • target: int
  • score: float

SimilarityGraphSummary

  • vertices: int
  • comparisons: int
  • edges: int

ScaffoldSample

  • molecule_id: int
  • scaffold: str
  • activity: str | None

ScaffoldSplit

  • molecule_id: int
  • scaffold: str | None
  • partition: str | None

ScaffoldSampler

Sample a registered relation with unique BIGINT molecule IDs and SMILES.

  • __init__(self, connection: DuckDBPyConnection, relation: str)
  • sample(self, *, per_stratum: int, seed: int) → list[ScaffoldSample]

    Choose stable hash ranks by Murcko scaffold and activity class.

  • split(self, *, train: float, validation: float, seed: int) → list[ScaffoldSplit]

    Keep each scaffold together, including the empty acyclic scaffold.

SparseFeatures

Store nonzero and explicitly missing coordinates against an immutable feature dictionary.

  • __init__(self, connection: DuckDBPyConnection, features: Mapping[str, int])
  • store(self, version: str, molecules: list[int], terms: list[tuple[int, str, float | None]]) → int

    Write one immutable matrix. A missing contribution marks its coordinate missing.

  • coordinates(self, version: str, molecules: list[int]) → list[tuple[int, int, float | None]]

    Return stored coordinates in molecule and feature ID order.

  • column(self, version: str, name: str, *, include_zeros: bool=True) → list[tuple[int, float | None]]

    Read one feature directly from DuckDB without binding a list of molecule IDs.

  • dense(self, version: str, molecules: list[int], names: list[str]) → list[tuple[int, tuple[float | None, ...]]]

    Read at most 262144 cells, retaining request order and explicit missing values.

FingerprintIndex

Find molecules whose Morgan fingerprints meet a Tanimoto similarity threshold. The default search uses the number of set bits in the query and the threshold to compute an inclusive range once. DuckDB scores the fingerprints inside that range with tanimoto_bit. It returns the highest scores first, with smaller molecule IDs first for ties.

Search a molecule table

Install the duckdb Python package. The community extension supplies the SQL functions. The Python helper ships in the repository. From the root of a duckSMILES checkout, save the example as example.py and run PYTHONPATH=python python example.py.

import duckdb
from ducksmiles_embedded import FingerprintIndex, MorganFingerprint

db = duckdb.connect()
db.execute("INSTALL ducksmiles FROM community")
db.execute("LOAD ducksmiles")

index = FingerprintIndex(db)
definition = MorganFingerprint(radius=2, bits=2048)
index.replace("library-v1", definition, [(1, "CCO"), (2, "CCN")])
result = index.search("library-v1", definition, "CCO", threshold=0.9, limit=10)
print([(hit.molecule_id, hit.score) for hit in result.hits])  # [(1, 1.0)]
print(result.candidates_scored)

candidates_scored counts rows admitted to exact scoring, including rows below the final threshold. Hits at the threshold qualify. Use search_bits when you already have the query fingerprint.

Experimental eight-segment filter

To try a tighter bound, use FingerprintIndex(db, use_segments=True) for both replace and search. For each new 2048-bit fingerprint, the helper saves the set-bit counts of eight 256-bit segments. The sum of the smaller count in each pair of segments bounds the possible overlap. Rows without stored counts and other fingerprint widths use the total-count range. Both modes return the same ranked hits.

In a warm top-ten search on 249,443 ZINC records at threshold 0.9, the original API took 128.03 ms per query and the new default took 79.47 ms (medians on one Apple M1 thread). The segment filter reduced mean candidates from 93,181 to 84.5 and took 124.59 ms. Across the benchmark, all 4,968 timed searches matched the ordered hits from exhaustive SQL. Benchmark data and reproduction steps.

Methods

  • __init__(self, connection: DuckDBPyConnection, *, use_segments: bool=False)
  • replace(self, generation: str, definition: MorganFingerprint, molecules: list[tuple[int, str | None]]) → int

    Replace one fingerprint generation atomically and return the valid row count.

  • search(self, generation: str, definition: MorganFingerprint, smiles: str, *, threshold: float=0.0, limit: int=10) → SimilarityResult
  • search_bits(self, generation: str, definition: MorganFingerprint, bits: bytes, *, threshold: float=0.0, limit: int=10) → SimilarityResult
  • build_graph(self, generation: str, definition: MorganFingerprint, *, version: str, threshold: float, block_size: int=256) → SimilarityGraphSummary

    Materialize a new immutable exact graph from a single fingerprint snapshot.

  • graph_vertices(self, version: str) → list[int]
  • graph_edges(self, version: str, *, threshold: float) → list[SimilarityEdge]

DaltonMass

  • value: float

MassMatch

  • reference_index: int
  • peak_index: int
  • error_ppm: float

MassWindowSearch

Trusted embedded operation for neutral masses expressed in daltons.

  • __init__(self, connection: DuckDBPyConnection)
  • match(self, references: tuple[DaltonMass, ...], peaks: tuple[DaltonMass, ...], tolerance_ppm: float) → list[MassMatch]

    Return inclusive matches in input order, with zero-based indices.

ConcentrationBasis

  • MASS = 'g/L'
  • MOLAR = 'mol/L'

NormalizedConcentration

  • measurement_id: int
  • source_value: float | None
  • source_unit: str | None
  • value: float | None
  • unit: str
  • rule_version: str
  • reason: str | None

ConcentrationTable

Convert a registered measurement relation to a compatible concentration basis.

  • RULE_VERSION = 'concentration-1'
  • __init__(self, connection: DuckDBPyConnection, relation: str)
  • read(self, basis: ConcentrationBasis) → list[NormalizedConcentration]
  • summarize(self, basis: ConcentrationBasis) → tuple[int, int, float | None]

    Return valid count, excluded count and total without exporting raw rows.