scigantic-bindingdb 0.2.3__tar.gz → 0.2.5__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (30) hide show
  1. {scigantic_bindingdb-0.2.3/src/scigantic_bindingdb.egg-info → scigantic_bindingdb-0.2.5}/PKG-INFO +12 -5
  2. {scigantic_bindingdb-0.2.3 → scigantic_bindingdb-0.2.5}/README.md +8 -2
  3. {scigantic_bindingdb-0.2.3 → scigantic_bindingdb-0.2.5}/pyproject.toml +11 -4
  4. {scigantic_bindingdb-0.2.3 → scigantic_bindingdb-0.2.5}/src/scigantic_bindingdb/cache.py +29 -5
  5. {scigantic_bindingdb-0.2.3 → scigantic_bindingdb-0.2.5}/src/scigantic_bindingdb/connection.py +5 -2
  6. {scigantic_bindingdb-0.2.3 → scigantic_bindingdb-0.2.5/src/scigantic_bindingdb.egg-info}/PKG-INFO +12 -5
  7. scigantic_bindingdb-0.2.5/src/scigantic_bindingdb.egg-info/requires.txt +8 -0
  8. {scigantic_bindingdb-0.2.3 → scigantic_bindingdb-0.2.5}/tests/test_cache.py +44 -0
  9. {scigantic_bindingdb-0.2.3 → scigantic_bindingdb-0.2.5}/tests/test_connection.py +13 -0
  10. scigantic_bindingdb-0.2.3/src/scigantic_bindingdb.egg-info/requires.txt +0 -7
  11. {scigantic_bindingdb-0.2.3 → scigantic_bindingdb-0.2.5}/LICENSE +0 -0
  12. {scigantic_bindingdb-0.2.3 → scigantic_bindingdb-0.2.5}/setup.cfg +0 -0
  13. {scigantic_bindingdb-0.2.3 → scigantic_bindingdb-0.2.5}/src/scigantic_bindingdb/__init__.py +0 -0
  14. {scigantic_bindingdb-0.2.3 → scigantic_bindingdb-0.2.5}/src/scigantic_bindingdb/__main__.py +0 -0
  15. {scigantic_bindingdb-0.2.3 → scigantic_bindingdb-0.2.5}/src/scigantic_bindingdb/_constants.py +0 -0
  16. {scigantic_bindingdb-0.2.3 → scigantic_bindingdb-0.2.5}/src/scigantic_bindingdb/chembl_bridge.py +0 -0
  17. {scigantic_bindingdb-0.2.3 → scigantic_bindingdb-0.2.5}/src/scigantic_bindingdb/cli.py +0 -0
  18. {scigantic_bindingdb-0.2.3 → scigantic_bindingdb-0.2.5}/src/scigantic_bindingdb/dti_pairs.py +0 -0
  19. {scigantic_bindingdb-0.2.3 → scigantic_bindingdb-0.2.5}/src/scigantic_bindingdb/measurements.py +0 -0
  20. {scigantic_bindingdb-0.2.3 → scigantic_bindingdb-0.2.5}/src/scigantic_bindingdb/py.typed +0 -0
  21. {scigantic_bindingdb-0.2.3 → scigantic_bindingdb-0.2.5}/src/scigantic_bindingdb/releases.py +0 -0
  22. {scigantic_bindingdb-0.2.3 → scigantic_bindingdb-0.2.5}/src/scigantic_bindingdb.egg-info/SOURCES.txt +0 -0
  23. {scigantic_bindingdb-0.2.3 → scigantic_bindingdb-0.2.5}/src/scigantic_bindingdb.egg-info/dependency_links.txt +0 -0
  24. {scigantic_bindingdb-0.2.3 → scigantic_bindingdb-0.2.5}/src/scigantic_bindingdb.egg-info/entry_points.txt +0 -0
  25. {scigantic_bindingdb-0.2.3 → scigantic_bindingdb-0.2.5}/src/scigantic_bindingdb.egg-info/top_level.txt +0 -0
  26. {scigantic_bindingdb-0.2.3 → scigantic_bindingdb-0.2.5}/tests/test_chembl_bridge.py +0 -0
  27. {scigantic_bindingdb-0.2.3 → scigantic_bindingdb-0.2.5}/tests/test_cli.py +0 -0
  28. {scigantic_bindingdb-0.2.3 → scigantic_bindingdb-0.2.5}/tests/test_dti_pairs.py +0 -0
  29. {scigantic_bindingdb-0.2.3 → scigantic_bindingdb-0.2.5}/tests/test_measurements.py +0 -0
  30. {scigantic_bindingdb-0.2.3 → scigantic_bindingdb-0.2.5}/tests/test_releases.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: scigantic-bindingdb
3
- Version: 0.2.3
3
+ Version: 0.2.5
4
4
  Summary: Query BindingDB directly from a public S3 mirror with DuckDB, including a ChEMBL cross-reference bridge table and a ready drug-target-interaction training table.
5
5
  Author: Scigantic
6
6
  License: MIT-0
@@ -21,10 +21,11 @@ Classifier: Topic :: Scientific/Engineering :: Chemistry
21
21
  Requires-Python: >=3.10
22
22
  Description-Content-Type: text/markdown
23
23
  License-File: LICENSE
24
- Requires-Dist: duckdb>=0.10
25
- Requires-Dist: pandas>=1.5
24
+ Requires-Dist: duckdb<2,>=0.10
25
+ Requires-Dist: pandas<4,>=1.5
26
26
  Provides-Extra: dev
27
27
  Requires-Dist: pytest>=7; extra == "dev"
28
+ Requires-Dist: pytest-cov>=5; extra == "dev"
28
29
  Requires-Dist: mypy>=1.10; extra == "dev"
29
30
  Requires-Dist: pandas-stubs; extra == "dev"
30
31
  Dynamic: license-file
@@ -136,7 +137,7 @@ bindingdb.enable_cache()
136
137
  df = bindingdb.chembl_bridge() # downloads the bridge table once, then reads from disk
137
138
  ```
138
139
 
139
- `chembl_bridge()` and `dti_pairs()` each need exactly one derived file, so caching downloads that one file to `~/.cache/scigantic-bindingdb` (override with `enable_cache(cache_dir=...)` or the `SCIGANTIC_BINDINGDB_CACHE` environment variable) and reuses it after that.
140
+ `chembl_bridge()` and `dti_pairs()` each need exactly one derived file, so caching downloads that one file to `~/.cache/scigantic-bindingdb` (override with `enable_cache(cache_dir=...)` or the `SCIGANTIC_BINDINGDB_CACHE` environment variable) and reuses it after that. Concurrent callers racing the first download of the same file wait for it rather than each downloading their own copy.
140
141
 
141
142
  `connect()`, `query()` and `measurements()` don't participate in this: `connect()` registers five core tables as views the first time a release is used, so caching them there would mean the first call for any release eagerly downloads everything regardless of what the query actually touches. Cache one table yourself if you want it locally: `bindingdb.cache_resolve("202608/parquet/measurements.parquet")` downloads it and returns the local path, usable directly in `read_parquet(...)`.
142
143
 
@@ -163,4 +164,10 @@ $ scigantic-bindingdb query "SELECT count(*) FROM measurements" --release 202608
163
164
 
164
165
  ## License
165
166
 
166
- MIT-0. See [LICENSE](LICENSE).
167
+ MIT-0. See [LICENSE](LICENSE). This covers the code in this package only.
168
+
169
+ ## Data license
170
+
171
+ BindingDB's underlying data is not under one license: it's a row-by-row mix, determined by each measurement's `curation_source` column. BindingDB [licenses](https://www.bindingdb.org/rwd/bind/info.jsp) its own staff-curated rows under [CC BY 3.0](https://creativecommons.org/licenses/by/3.0/), but rows imported from ChEMBL (`curation_source = 'ChEMBL'`, 51.3% of measurements in the 202608 release) carry ChEMBL's own [CC BY-SA 3.0](https://creativecommons.org/licenses/by-sa/3.0/) terms instead, share-alike included. Check `curation_source` before redistributing a subset of `measurements` or `dti_pairs()` to know which terms apply to those specific rows.
172
+
173
+ `chembl_bridge()` results specifically warrant care regardless of a matched measurement's own `curation_source`: every row includes `chembl_id`/`chembl_molregno` matched from ChEMBL's own `molecule_dictionary`, and with the default `with_names=True`, ChEMBL's own `pref_name`. Those columns carry ChEMBL's CC BY-SA 3.0 terms, independent of whichever license applies to the rest of that row.
@@ -105,7 +105,7 @@ bindingdb.enable_cache()
105
105
  df = bindingdb.chembl_bridge() # downloads the bridge table once, then reads from disk
106
106
  ```
107
107
 
108
- `chembl_bridge()` and `dti_pairs()` each need exactly one derived file, so caching downloads that one file to `~/.cache/scigantic-bindingdb` (override with `enable_cache(cache_dir=...)` or the `SCIGANTIC_BINDINGDB_CACHE` environment variable) and reuses it after that.
108
+ `chembl_bridge()` and `dti_pairs()` each need exactly one derived file, so caching downloads that one file to `~/.cache/scigantic-bindingdb` (override with `enable_cache(cache_dir=...)` or the `SCIGANTIC_BINDINGDB_CACHE` environment variable) and reuses it after that. Concurrent callers racing the first download of the same file wait for it rather than each downloading their own copy.
109
109
 
110
110
  `connect()`, `query()` and `measurements()` don't participate in this: `connect()` registers five core tables as views the first time a release is used, so caching them there would mean the first call for any release eagerly downloads everything regardless of what the query actually touches. Cache one table yourself if you want it locally: `bindingdb.cache_resolve("202608/parquet/measurements.parquet")` downloads it and returns the local path, usable directly in `read_parquet(...)`.
111
111
 
@@ -132,4 +132,10 @@ $ scigantic-bindingdb query "SELECT count(*) FROM measurements" --release 202608
132
132
 
133
133
  ## License
134
134
 
135
- MIT-0. See [LICENSE](LICENSE).
135
+ MIT-0. See [LICENSE](LICENSE). This covers the code in this package only.
136
+
137
+ ## Data license
138
+
139
+ BindingDB's underlying data is not under one license: it's a row-by-row mix, determined by each measurement's `curation_source` column. BindingDB [licenses](https://www.bindingdb.org/rwd/bind/info.jsp) its own staff-curated rows under [CC BY 3.0](https://creativecommons.org/licenses/by/3.0/), but rows imported from ChEMBL (`curation_source = 'ChEMBL'`, 51.3% of measurements in the 202608 release) carry ChEMBL's own [CC BY-SA 3.0](https://creativecommons.org/licenses/by-sa/3.0/) terms instead, share-alike included. Check `curation_source` before redistributing a subset of `measurements` or `dti_pairs()` to know which terms apply to those specific rows.
140
+
141
+ `chembl_bridge()` results specifically warrant care regardless of a matched measurement's own `curation_source`: every row includes `chembl_id`/`chembl_molregno` matched from ChEMBL's own `molecule_dictionary`, and with the default `with_names=True`, ChEMBL's own `pref_name`. Those columns carry ChEMBL's CC BY-SA 3.0 terms, independent of whichever license applies to the rest of that row.
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
4
4
 
5
5
  [project]
6
6
  name = "scigantic-bindingdb"
7
- version = "0.2.3"
7
+ version = "0.2.5"
8
8
  description = "Query BindingDB directly from a public S3 mirror with DuckDB, including a ChEMBL cross-reference bridge table and a ready drug-target-interaction training table."
9
9
  readme = "README.md"
10
10
  requires-python = ">=3.10"
@@ -25,12 +25,12 @@ classifiers = [
25
25
  ]
26
26
 
27
27
  dependencies = [
28
- "duckdb>=0.10",
29
- "pandas>=1.5",
28
+ "duckdb>=0.10,<2",
29
+ "pandas>=1.5,<4",
30
30
  ]
31
31
 
32
32
  [project.optional-dependencies]
33
- dev = ["pytest>=7", "mypy>=1.10", "pandas-stubs"]
33
+ dev = ["pytest>=7", "pytest-cov>=5", "mypy>=1.10", "pandas-stubs"]
34
34
 
35
35
  [project.urls]
36
36
  Homepage = "https://scigantic.com"
@@ -49,5 +49,12 @@ scigantic_bindingdb = ["py.typed"]
49
49
  [tool.pytest.ini_options]
50
50
  testpaths = ["tests"]
51
51
 
52
+ [tool.coverage.run]
53
+ source = ["scigantic_bindingdb"]
54
+ # Not wired into CI as a gate: this suite runs real queries against the live
55
+ # S3 mirror, so a coverage threshold would be one more thing that can fail
56
+ # for reasons unrelated to a change. `pytest --cov` is available locally for
57
+ # anyone who wants the number.
58
+
52
59
  [tool.mypy]
53
60
  strict = true
@@ -23,6 +23,7 @@ from __future__ import annotations
23
23
 
24
24
  import os
25
25
  import sys
26
+ import threading
26
27
  import urllib.request
27
28
  import uuid
28
29
  from pathlib import Path
@@ -34,6 +35,24 @@ _cache_dir: Path | None = None
34
35
 
35
36
  _CHUNK_BYTES = 1024 * 1024
36
37
 
38
+ # One lock per key, created on first use. Guards resolve()'s
39
+ # check-then-download against concurrent callers asking for the same key
40
+ # at once: without this, N threads racing the first resolve() of a key
41
+ # each see it missing and each download it in full, rather than one
42
+ # downloading while the rest wait and reuse the result. Verified directly:
43
+ # 16 threads calling chembl_bridge() concurrently on an empty cache
44
+ # triggered 16 separate downloads of the same file before this fix.
45
+ _resolve_locks: dict[str, threading.Lock] = {}
46
+ _resolve_locks_guard = threading.Lock()
47
+
48
+
49
+ def _lock_for(key: str) -> threading.Lock:
50
+ lock = _resolve_locks.get(key)
51
+ if lock is not None:
52
+ return lock
53
+ with _resolve_locks_guard:
54
+ return _resolve_locks.setdefault(key, threading.Lock())
55
+
37
56
 
38
57
  def _default_cache_dir() -> Path:
39
58
  if sys.platform == "win32":
@@ -114,7 +133,9 @@ def resolve(key: str) -> str:
114
133
  `key` is a path relative to the bucket root, e.g.
115
134
  "202608/derived/bindingdb_chembl_bridge.parquet". Downloads to the
116
135
  cache on first access; later calls for the same key reuse the local
117
- file without touching the network.
136
+ file without touching the network. Concurrent callers asking for the
137
+ same key while it's still downloading wait for that download rather
138
+ than each starting their own.
118
139
  """
119
140
  if not _enabled:
120
141
  return f"s3://{BUCKET}/{key}"
@@ -124,8 +145,11 @@ def resolve(key: str) -> str:
124
145
  if local_path.exists():
125
146
  return str(local_path)
126
147
 
127
- local_path.parent.mkdir(parents=True, exist_ok=True)
128
- url = f"https://{BUCKET}.s3.{REGION}.amazonaws.com/{key}"
129
- print(f"scigantic-bindingdb: caching {key} ...", file=sys.stderr, flush=True)
130
- _atomic_download(url, local_path)
148
+ with _lock_for(key):
149
+ if local_path.exists(): # another thread finished it while we waited
150
+ return str(local_path)
151
+ local_path.parent.mkdir(parents=True, exist_ok=True)
152
+ url = f"https://{BUCKET}.s3.{REGION}.amazonaws.com/{key}"
153
+ print(f"scigantic-bindingdb: caching {key} ...", file=sys.stderr, flush=True)
154
+ _atomic_download(url, local_path)
131
155
  return str(local_path)
@@ -123,8 +123,11 @@ def query(sql: str, release: str | None = None) -> "pd.DataFrame":
123
123
  For several queries against the same release, call connect() once and
124
124
  reuse it instead, to avoid opening and closing a cursor per query.
125
125
  """
126
- con = connect(release)
126
+ resolved_release = release or latest()
127
+ con = connect(resolved_release)
127
128
  try:
128
- return con.execute(sql).df()
129
+ df = con.execute(sql).df()
130
+ df.attrs["bindingdb_release"] = resolved_release
131
+ return df
129
132
  finally:
130
133
  con.close()
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: scigantic-bindingdb
3
- Version: 0.2.3
3
+ Version: 0.2.5
4
4
  Summary: Query BindingDB directly from a public S3 mirror with DuckDB, including a ChEMBL cross-reference bridge table and a ready drug-target-interaction training table.
5
5
  Author: Scigantic
6
6
  License: MIT-0
@@ -21,10 +21,11 @@ Classifier: Topic :: Scientific/Engineering :: Chemistry
21
21
  Requires-Python: >=3.10
22
22
  Description-Content-Type: text/markdown
23
23
  License-File: LICENSE
24
- Requires-Dist: duckdb>=0.10
25
- Requires-Dist: pandas>=1.5
24
+ Requires-Dist: duckdb<2,>=0.10
25
+ Requires-Dist: pandas<4,>=1.5
26
26
  Provides-Extra: dev
27
27
  Requires-Dist: pytest>=7; extra == "dev"
28
+ Requires-Dist: pytest-cov>=5; extra == "dev"
28
29
  Requires-Dist: mypy>=1.10; extra == "dev"
29
30
  Requires-Dist: pandas-stubs; extra == "dev"
30
31
  Dynamic: license-file
@@ -136,7 +137,7 @@ bindingdb.enable_cache()
136
137
  df = bindingdb.chembl_bridge() # downloads the bridge table once, then reads from disk
137
138
  ```
138
139
 
139
- `chembl_bridge()` and `dti_pairs()` each need exactly one derived file, so caching downloads that one file to `~/.cache/scigantic-bindingdb` (override with `enable_cache(cache_dir=...)` or the `SCIGANTIC_BINDINGDB_CACHE` environment variable) and reuses it after that.
140
+ `chembl_bridge()` and `dti_pairs()` each need exactly one derived file, so caching downloads that one file to `~/.cache/scigantic-bindingdb` (override with `enable_cache(cache_dir=...)` or the `SCIGANTIC_BINDINGDB_CACHE` environment variable) and reuses it after that. Concurrent callers racing the first download of the same file wait for it rather than each downloading their own copy.
140
141
 
141
142
  `connect()`, `query()` and `measurements()` don't participate in this: `connect()` registers five core tables as views the first time a release is used, so caching them there would mean the first call for any release eagerly downloads everything regardless of what the query actually touches. Cache one table yourself if you want it locally: `bindingdb.cache_resolve("202608/parquet/measurements.parquet")` downloads it and returns the local path, usable directly in `read_parquet(...)`.
142
143
 
@@ -163,4 +164,10 @@ $ scigantic-bindingdb query "SELECT count(*) FROM measurements" --release 202608
163
164
 
164
165
  ## License
165
166
 
166
- MIT-0. See [LICENSE](LICENSE).
167
+ MIT-0. See [LICENSE](LICENSE). This covers the code in this package only.
168
+
169
+ ## Data license
170
+
171
+ BindingDB's underlying data is not under one license: it's a row-by-row mix, determined by each measurement's `curation_source` column. BindingDB [licenses](https://www.bindingdb.org/rwd/bind/info.jsp) its own staff-curated rows under [CC BY 3.0](https://creativecommons.org/licenses/by/3.0/), but rows imported from ChEMBL (`curation_source = 'ChEMBL'`, 51.3% of measurements in the 202608 release) carry ChEMBL's own [CC BY-SA 3.0](https://creativecommons.org/licenses/by-sa/3.0/) terms instead, share-alike included. Check `curation_source` before redistributing a subset of `measurements` or `dti_pairs()` to know which terms apply to those specific rows.
172
+
173
+ `chembl_bridge()` results specifically warrant care regardless of a matched measurement's own `curation_source`: every row includes `chembl_id`/`chembl_molregno` matched from ChEMBL's own `molecule_dictionary`, and with the default `with_names=True`, ChEMBL's own `pref_name`. Those columns carry ChEMBL's CC BY-SA 3.0 terms, independent of whichever license applies to the rest of that row.
@@ -0,0 +1,8 @@
1
+ duckdb<2,>=0.10
2
+ pandas<4,>=1.5
3
+
4
+ [dev]
5
+ pytest>=7
6
+ pytest-cov>=5
7
+ mypy>=1.10
8
+ pandas-stubs
@@ -127,6 +127,50 @@ def test_concurrent_downloads_of_the_same_key_never_raise(tmp_path):
127
127
  assert list(local_path.parent.glob("key.parquet.*.part")) == []
128
128
 
129
129
 
130
+ def test_concurrent_first_resolve_of_the_same_key_downloads_once(tmp_path):
131
+ # Regression test for a real inefficiency: resolve()'s check-then-
132
+ # download wasn't coordinated across threads, so N threads racing the
133
+ # first resolve() of a key each saw it missing and each downloaded it
134
+ # in full. Verified directly before this was fixed with a per-key
135
+ # lock: 16 threads calling resolve() concurrently on an empty cache
136
+ # triggered 16 separate downloads of the same file instead of one.
137
+ #
138
+ # mock.patch is entered/exited only once here, by the main thread,
139
+ # before the pool starts; the worker threads only read the already-
140
+ # patched attribute, so this doesn't hit the same-target mock.patch
141
+ # race that test_concurrent_downloads_of_the_same_key_never_raise's
142
+ # docstring describes for patching from multiple threads at once.
143
+ body = b"cached-body"
144
+
145
+ class FakeConnection:
146
+ def __enter__(self):
147
+ return self
148
+
149
+ def __exit__(self, *exc_info):
150
+ return False
151
+
152
+ def read(self, *_args):
153
+ if getattr(self, "_served", False):
154
+ return b""
155
+ self._served = True
156
+ return body
157
+
158
+ bindingdb.enable_cache(cache_dir=str(tmp_path))
159
+ try:
160
+ with mock.patch(
161
+ "urllib.request.urlopen", side_effect=lambda *a, **k: FakeConnection()
162
+ ) as urlopen:
163
+ with ThreadPoolExecutor(max_workers=16) as pool:
164
+ paths = list(pool.map(lambda _i: resolve("shared/once.parquet"), range(16)))
165
+
166
+ assert len(set(paths)) == 1
167
+ assert urlopen.call_count == 1 # only one thread actually downloaded
168
+ final = tmp_path / "shared" / "once.parquet"
169
+ assert final.read_bytes() == body
170
+ finally:
171
+ bindingdb.disable_cache()
172
+
173
+
130
174
  def test_cached_bridge_matches_uncached(tmp_path):
131
175
  # The one integration-level check: caching actually gets used by a real
132
176
  # function, not just by resolve() directly, and returns the same data.
@@ -20,6 +20,19 @@ def test_query_returns_dataframe_with_expected_columns():
20
20
  assert len(df) == 5
21
21
 
22
22
 
23
+ def test_query_records_bindingdb_release_in_attrs():
24
+ # Matches scigantic_chembl's query(), which stamps df.attrs["chembl_release"]
25
+ # the same way: the release actually used should travel with the result,
26
+ # not just be implicit in whatever the caller happened to pass.
27
+ df = bindingdb.query("SELECT 1", release="202608")
28
+ assert df.attrs["bindingdb_release"] == "202608"
29
+
30
+
31
+ def test_query_omitted_release_records_resolved_release_in_attrs():
32
+ df = bindingdb.query("SELECT 1")
33
+ assert df.attrs["bindingdb_release"] == bindingdb.latest()
34
+
35
+
23
36
  def test_connect_registers_all_five_core_tables():
24
37
  con = bindingdb.connect()
25
38
  try:
@@ -1,7 +0,0 @@
1
- duckdb>=0.10
2
- pandas>=1.5
3
-
4
- [dev]
5
- pytest>=7
6
- mypy>=1.10
7
- pandas-stubs