Skip to main content

scigantic-chembl

CI PyPI PyPI - Python Version License

Query ChEMBL directly from a public S3 mirror with DuckDB.

import scigantic_chembl as chembl

df = chembl.query("""
    SELECT chembl_id, pref_name
    FROM molecule_dictionary
    WHERE pref_name IS NOT NULL
    LIMIT 5
""")

That query runs against s3://scigantic-chembl over DuckDB's httpfs extension. Nothing is downloaded first, and there's no local database file sitting on disk afterward.

Installation

$ pip install scigantic-chembl

Compared to chembl-downloader

chembl-downloader is the standard way to work with ChEMBL in Python. It covers every release back to chembl_1, and once the SQLite dump is downloaded it works fully offline. It can also do similarity and substructure search, through chemfp and an RDKit SubstructLibrary it builds locally. This package trades that release range for less setup: a pre-joined potency table, and similarity and substructure search over fingerprints the mirror already precomputes, so there's no local index to build before a query runs. The mirror here only carries chembl_35 through chembl_37, and only chembl_37 has the pre-joined and search layers, so an older release or fully offline work is still a job for chembl-downloader.

Potency data, pre-joined

activities needs a five-table join and a few correctness filters before it's usable for structure-activity work. That join is already done, stored as derived/activities_enriched.parquet:

df = chembl.activities(target_chembl_id="CHEMBL203")  # EGFR: 18,998 rows, 11,202 compounds

The filters already applied are about correctness, not taste: pchembl_value present, standard_relation = '=', no data_validity_comment, not a potential_duplicate. confidence_score and target_type stay as columns rather than filters, since which rows count as usable SAR data is an analysis choice:

df = chembl.activities(min_confidence=8, limit=50_000)

min_confidence won't do anything combined with a single target_chembl_id: ChEMBL curates confidence_score per target entry, not per measurement, so every row for one target shares the same confidence class (verified against the live corpus: zero targets have a mixed score). It narrows results when querying across targets, like the example above.

chembl.query() still reaches the raw tables directly for anything the join leaves out.

Similarity search

gefitinib = "COC1=C(C=C2C(=C1)N=CN=C2NC3=CC(=C(C=C3)F)Cl)OCCCN4CCOCC4"
hits = chembl.similar_compounds(gefitinib, top_k=5)
    chembl_id  tanimoto
    CHEMBL939  1.000000
  CHEMBL14699  0.919355
CHEMBL4165375  0.916667
 CHEMBL299672  0.857143
CHEMBL4448162  0.857143

CHEMBL939 is gefitinib itself. Every compound with a comparable potency measurement (1.68M of ChEMBL's 2.9M structures) has a precomputed 2048-bit Morgan fingerprint, kept packed in memory and compared with numpy's bitwise_count rather than unpacked bit by bit. The corpus loads once per process, about 18 seconds on a typical home connection and faster from inside AWS; every call after that in the same process is under half a second. chembl-downloader can do similarity search too, through chemfp, but its own docs put building that index at tens of minutes.

Similarity search needs rdkit to encode the query molecule, so it's kept as an optional extra:

$ pip install "scigantic-chembl[similarity]"

Substructure search

Similarity search answers "what's like this molecule." This answers a different question: "what actually contains this fragment."

from rdkit import Chem

gefitinib = "COC1=C(C=C2C(=C1)N=CN=C2NC3=CC(=C(C=C3)F)Cl)OCCCN4CCOCC4"
gefitinib_scaffold = Chem.MolToSmarts(Chem.MolFromSmiles(gefitinib))
hits = chembl.substructure_search(gefitinib_scaffold, limit=50)
    chembl_id                                                        canonical_smiles
CHEMBL4165375   Fc1ccc(Nc2ncnc3cc(OCCCN4CCOCC4)c(OCCCN4CCOCC4)cc23)cc1Cl
CHEMBL1788321   COc1cc2ncnc(Nc3ccc(F)c(Cl)c3)c2cc1OC[C@H](O)CN1CCOCC1
    CHEMBL939   COc1cc2ncnc(Nc3ccc(F)c(Cl)c3)c2cc1OCCCN1CCOCC1

(9 matches total, truncated for space here; CHEMBL939 is gefitinib itself, the rest are close analogues and prodrugs.)

RDKit's exact HasSubstructMatch is too slow to run against 1.68M compounds per query, so this prescreens first with a precomputed PatternFingerprint: a molecule can only contain the query fragment if every bit the query sets is also set in the candidate, a cheap containment test over packed bytes. Only prescreen survivors get the expensive exact check, stopping as soon as limit confirmed matches are found.

How narrow the prescreen is depends on how specific the query fragment is, not on this package. A small, generic ring system (a bare quinazoline, say) is a weak filter, over a million of the 1.68M compounds pass it, because PatternFingerprint discriminates on structural complexity, and small fragments have little of it. A large, specific fragment like the full example above prescreens to 34 candidates before the exact stage even starts. max_candidates (default 20,000) bounds how many prescreen survivors get exact-matched, so an overly generic query can't turn into an unbounded scan; hitting that cap before finding limit matches raises a warning and sets result.attrs["truncated"] = True, rather than silently returning a partial answer that looks complete.

What's mirrored

chembl.releases()
release raw tables pre-joined activities similarity search substructure search CYP training set
chembl_37 yes yes yes yes yes
chembl_36 yes no no no yes
chembl_35 yes no no no yes

chembl_36 and chembl_35 are raw-table access only: their activities table is missing a column chembl_37's has (modality), so calling activities(), similar_compounds(), or substructure_search() on either one raises ReleaseCapabilityError up front instead of failing partway through a join with a confusing error.

This table isn't hardcoded. releases() reads a small manifest that the mirror's own weekly cron regenerates by probing the bucket directly, so a new ChEMBL release shows up here without waiting on a new version of this package. If the manifest can't be reached, calls fall back to the snapshot shipped with whatever version you have installed and print a warning, rather than failing outright.

Command line

$ scigantic-chembl info
$ scigantic-chembl query "SELECT count(*) FROM activities" --release chembl_37

License

MIT-0. See LICENSE.

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

scigantic_chembl-0.3.1.tar.gz (18.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

scigantic_chembl-0.3.1-py3-none-any.whl (16.9 kB view details)

Uploaded Python 3

File details

Details for the file scigantic_chembl-0.3.1.tar.gz.

File metadata

  • Download URL: scigantic_chembl-0.3.1.tar.gz
  • Upload date:
  • Size: 18.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/7.0.0 CPython/3.11.16

File hashes

Hashes for scigantic_chembl-0.3.1.tar.gz
Algorithm Hash digest
SHA256 b3af78e65de9e7fe63ca446d454922e0eae7dc6ac24aaed937a4593ed0c29b4b
MD5 a356dedd39ea9a6785e316bd660c95ec
BLAKE2b-256 0ab0635b3eebbb283a3d0e034dd8e58ab694ca238509c8b1e017172578fd373e

See more details on using hashes here.

File details

Details for the file scigantic_chembl-0.3.1-py3-none-any.whl.

File metadata

File hashes

Hashes for scigantic_chembl-0.3.1-py3-none-any.whl
Algorithm Hash digest
SHA256 d08a070ecd26b85c7a538d01fdeb98231fc0886740ab38069c3ee40234c5c8a2
MD5 a108fd035067d7c076aa51c19162f971
BLAKE2b-256 e57dc900043fb712d715d757de765d31d7fad8b8ebef338390dfd9946f4e40d2

See more details on using hashes here.

Release history Release notifications | RSS feed

0.5.0

2 files

0.4.5

2 files

0.4.4

2 files

0.4.1

2 files

0.4.0

2 files

This release

0.3.1 This release

2 files

0.3.0

2 files

0.2.1

2 files

0.2.0

2 files

0.1.1

2 files

0.1.0

2 files

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