fairfetched 0.0.1.dev202607271__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 (39) hide show
  1. fairfetched-0.0.1.dev202607271/LICENSE +21 -0
  2. fairfetched-0.0.1.dev202607271/PKG-INFO +153 -0
  3. fairfetched-0.0.1.dev202607271/README.md +129 -0
  4. fairfetched-0.0.1.dev202607271/fairfetched/__init__.py +0 -0
  5. fairfetched-0.0.1.dev202607271/fairfetched/get/__init__.py +9 -0
  6. fairfetched-0.0.1.dev202607271/fairfetched/get/chembl.py +219 -0
  7. fairfetched-0.0.1.dev202607271/fairfetched/get/dataset.py +167 -0
  8. fairfetched-0.0.1.dev202607271/fairfetched/get/papyrus.py +156 -0
  9. fairfetched-0.0.1.dev202607271/fairfetched/standardize/___mol_expr.pyi +23 -0
  10. fairfetched-0.0.1.dev202607271/fairfetched/standardize/__init__.py +0 -0
  11. fairfetched-0.0.1.dev202607271/fairfetched/standardize/_optional.py +56 -0
  12. fairfetched-0.0.1.dev202607271/fairfetched/standardize/compound_fns.py +340 -0
  13. fairfetched-0.0.1.dev202607271/fairfetched/standardize/mol_expr.py +302 -0
  14. fairfetched-0.0.1.dev202607271/fairfetched/standardize/mol_functions.py +156 -0
  15. fairfetched-0.0.1.dev202607271/fairfetched/standardize/pipe.py +432 -0
  16. fairfetched-0.0.1.dev202607271/fairfetched/standardize/pipeline.py +31 -0
  17. fairfetched-0.0.1.dev202607271/fairfetched/utils/__init__.py +18 -0
  18. fairfetched-0.0.1.dev202607271/fairfetched/utils/_optional.py +98 -0
  19. fairfetched-0.0.1.dev202607271/fairfetched/utils/_track.py +31 -0
  20. fairfetched-0.0.1.dev202607271/fairfetched/utils/ensure.py +44 -0
  21. fairfetched-0.0.1.dev202607271/fairfetched/utils/files.py +48 -0
  22. fairfetched-0.0.1.dev202607271/fairfetched/utils/polars.py +369 -0
  23. fairfetched-0.0.1.dev202607271/fairfetched/utils/storage.py +18 -0
  24. fairfetched-0.0.1.dev202607271/fairfetched/utils/typing.py +75 -0
  25. fairfetched-0.0.1.dev202607271/fairfetched/vis.py +92 -0
  26. fairfetched-0.0.1.dev202607271/fairfetched.egg-info/PKG-INFO +153 -0
  27. fairfetched-0.0.1.dev202607271/fairfetched.egg-info/SOURCES.txt +66 -0
  28. fairfetched-0.0.1.dev202607271/fairfetched.egg-info/dependency_links.txt +1 -0
  29. fairfetched-0.0.1.dev202607271/fairfetched.egg-info/requires.txt +18 -0
  30. fairfetched-0.0.1.dev202607271/fairfetched.egg-info/top_level.txt +4 -0
  31. fairfetched-0.0.1.dev202607271/pyproject.toml +55 -0
  32. fairfetched-0.0.1.dev202607271/scripts/chembl.py +7 -0
  33. fairfetched-0.0.1.dev202607271/setup.cfg +4 -0
  34. fairfetched-0.0.1.dev202607271/tests/__init__.py +1 -0
  35. fairfetched-0.0.1.dev202607271/tests/test_ensure.py +159 -0
  36. fairfetched-0.0.1.dev202607271/tests/test_get.py +1085 -0
  37. fairfetched-0.0.1.dev202607271/tests/test_integration_chembl.py +285 -0
  38. fairfetched-0.0.1.dev202607271/tests/test_standardize.py +190 -0
  39. fairfetched-0.0.1.dev202607271/tests/test_utils.py +162 -0
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 lucina-may nollen
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,153 @@
1
+ Metadata-Version: 2.4
2
+ Name: fairfetched
3
+ Version: 0.0.1.dev202607271
4
+ Summary: data APIs for reproducible data fetching in cheminformatics
5
+ Project-URL: Homepage, https://github.com/lucinamay/fairfetched
6
+ Requires-Python: >=3.11
7
+ Description-Content-Type: text/markdown
8
+ License-File: LICENSE
9
+ Requires-Dist: polars>=1.38.1
10
+ Provides-Extra: track
11
+ Requires-Dist: rich>=14.3.3; extra == "track"
12
+ Provides-Extra: rdkit
13
+ Requires-Dist: rdkit>=2025.9.6; extra == "rdkit"
14
+ Provides-Extra: standardise
15
+ Requires-Dist: fairfetched[rdkit]; extra == "standardise"
16
+ Requires-Dist: chembl-structure-pipeline>=1.2.4; extra == "standardise"
17
+ Requires-Dist: papyrus-structure-pipeline>=0.0.5; extra == "standardise"
18
+ Provides-Extra: dev
19
+ Requires-Dist: pytest>=9.0.2; extra == "dev"
20
+ Requires-Dist: sqlalchemy>=2.0.46; extra == "dev"
21
+ Requires-Dist: fairfetched[standardise]; extra == "dev"
22
+ Requires-Dist: rust-just>=1.46.0; extra == "dev"
23
+ Dynamic: license-file
24
+
25
+ # fairfetched
26
+
27
+ data APIs for reproducible data fetching in cheminformatics in line with FAIR principles.
28
+ the tool is designed such that all raw data is downloaded and kept in one central place (i.e. CHEMBL 37 as the original .db files), and the user has a fast, lightweight, intuitive API to filter / query the data as required for any particular project.
29
+
30
+ # installation
31
+
32
+ you can install this package through
33
+ `uv add fairfetched` (recommended)
34
+
35
+ or if you do not use the uv package manager:
36
+ `pip install fairfetched`
37
+
38
+ # examples
39
+
40
+ you can download Chembl or Papyrus through:
41
+
42
+ ```python
43
+ from fairfetched.get import Chembl, Papyrus
44
+ mychembl = Chembl.from_latest() # this downloads Chembl raw files + extracts parquet files to wherever you
45
+ # have set the environment variable FAIRFETCHED_HOME, PYSTOW_HOME,
46
+ # or <HOME>/.data if not in environment variables.
47
+ # from there, fairfetched saves it to a folder chembl/<version>
48
+
49
+ mychembl.lfs # a dictionary of all chembl files in polars LazyFrame format, scanned directly from the extracted .parquet files
50
+
51
+
52
+ mychembl.parquet_paths # the paths to the parquet-converted tabular data files in the Chembl .db file
53
+
54
+ mychembl.raw_paths # the paths to the raw chembl file as downloaded from Chembl. currently does include an uncompressed .db file
55
+
56
+ mychembl.compounds # NOT YET IMPLEMENTED !! convenience alias for mychembl.compose()["compounds"], which uses mychembl.lfs LazyFrame joins to obtain an intuitive join of the data.
57
+ # from there, you can
58
+ ```
59
+
60
+ ### examples of how to use the LazyFrames:
61
+
62
+ #### checking which columns+datatypes are in the file, so that you can choose to join them:
63
+
64
+ ```python
65
+ >>> mychembl.lfs["activities"].collect_schema()
66
+ Schema({'activity_id': Int64, 'assay_id': Int64, 'doc_id': Int64, 'record_id': Int64, 'molregno': Int64, 'standard_relation': String, 'standard_value': Float64, 'standard_units': String, 'standard_flag': Int64, 'standard_type': String, 'activity_comment': String, 'data_validity_comment': String, 'potential_duplicate': Int64, 'pchembl_value': Float64, 'bao_endpoint': String, 'uo_units': String, 'qudt_units': String, 'toid': Int64, 'upper_value': Float64, 'standard_upper_value': Null, 'src_id': Int64, 'type': String, 'relation': String, 'value': Float64, 'units': String, 'text_value': String, 'standard_text_value': String, 'action_type': String})
67
+ ```
68
+
69
+ #### selecting all entries based on doc_id:
70
+
71
+ ```python
72
+ >>> mychembl.lfs["activities"].filter(doc_id=89530).drop_nulls("units").collect()
73
+ shape: (107, 28)
74
+ ┌─────────────┬──────────┬────────┬───────────┬───┬───────┬────────────┬─────────────────────┬─────────────┐
75
+ │ activity_id ┆ assay_id ┆ doc_id ┆ record_id ┆ … ┆ units ┆ text_value ┆ standard_text_value ┆ action_type │
76
+ │ --- ┆ --- ┆ --- ┆ --- ┆ ┆ --- ┆ --- ┆ --- ┆ --- │
77
+ │ i64 ┆ i64 ┆ i64 ┆ i64 ┆ ┆ str ┆ str ┆ str ┆ str │
78
+ ╞═════════════╪══════════╪════════╪═══════════╪═══╪═══════╪════════════╪═════════════════════╪═════════════╡
79
+ │ 15120638 ┆ 1431503 ┆ 89530 ┆ 2256150 ┆ … ┆ uM ┆ null ┆ null ┆ null │
80
+ │ 15120639 ┆ 1431503 ┆ 89530 ┆ 2256151 ┆ … ┆ uM ┆ null ┆ null ┆ null │
81
+ │ 15120640 ┆ 1431503 ┆ 89530 ┆ 2256152 ┆ … ┆ uM ┆ null ┆ null ┆ null │
82
+ │ 15120641 ┆ 1431503 ┆ 89530 ┆ 2256153 ┆ … ┆ uM ┆ null ┆ null ┆ null │
83
+ │ 15120642 ┆ 1431503 ┆ 89530 ┆ 2256154 ┆ … ┆ uM ┆ null ┆ null ┆ null │
84
+ │ … ┆ … ┆ … ┆ … ┆ … ┆ … ┆ … ┆ … ┆ … │
85
+ │ 15125200 ┆ 1431507 ┆ 89530 ┆ 2256167 ┆ … ┆ uM ┆ null ┆ null ┆ null │
86
+ │ 15125201 ┆ 1431507 ┆ 89530 ┆ 2256168 ┆ … ┆ uM ┆ null ┆ null ┆ null │
87
+ │ 15125202 ┆ 1431507 ┆ 89530 ┆ 2256169 ┆ … ┆ uM ┆ null ┆ null ┆ null │
88
+ │ 15125203 ┆ 1431507 ┆ 89530 ┆ 2256170 ┆ … ┆ uM ┆ null ┆ null ┆ null │
89
+ │ 15125204 ┆ 1431507 ┆ 89530 ┆ 2256171 ┆ … ┆ uM ┆ null ┆ null ┆ null │
90
+ └─────────────┴──────────┴────────┴───────────┴───┴───────┴────────────┴─────────────────────┴─────────────┘
91
+ ```
92
+
93
+ #### adding compound structure info to the activities on molregno
94
+
95
+ ```python
96
+ >>> mychembl.lfs["activities"].join(mychembl.lfs["compound_structures"],on="molregno",how="left",validate="m:1").head().collect()
97
+ shape: (5, 32)
98
+ ┌─────────────┬──────────┬────────┬───────────┬───┬────────────────────────┬─────────────────────────────────┬─────────────────────────────┬─────────────────────────────────┐
99
+ │ activity_id ┆ assay_id ┆ doc_id ┆ record_id ┆ … ┆ molfile ┆ standard_inchi ┆ standard_inchi_key ┆ canonical_smiles │
100
+ │ --- ┆ --- ┆ --- ┆ --- ┆ ┆ --- ┆ --- ┆ --- ┆ --- │
101
+ │ i64 ┆ i64 ┆ i64 ┆ i64 ┆ ┆ str ┆ str ┆ str ┆ str │
102
+ ╞═════════════╪══════════╪════════╪═══════════╪═══╪════════════════════════╪═════════════════════════════════╪═════════════════════════════╪═════════════════════════════════╡
103
+ │ 31863 ┆ 54505 ┆ 6424 ┆ 206172 ┆ … ┆ ┆ InChI=1S/C20H12N2O2/c1-2-7-13(… ┆ BEBACPIIZGRKGG-UHFFFAOYSA-N ┆ c1ccc(-c2nc3c(-c4nc5ccccc5o4)c… │
104
+ │ ┆ ┆ ┆ ┆ ┆ RDKit 2D ┆ ┆ ┆ │
105
+ │ ┆ ┆ ┆ ┆ ┆ ┆ ┆ ┆ │
106
+ │ ┆ ┆ ┆ ┆ ┆ 24 2… ┆ ┆ ┆ │
107
+ │ 31864 ┆ 83907 ┆ 6432 ┆ 208970 ┆ … ┆ ┆ InChI=1S/C23H14N2O5/c1-12-5-8-… ┆ SUKVIELCKKEBOJ-UHFFFAOYSA-N ┆ Cc1ccc2oc(-c3cccc(N4C(=O)c5ccc… │
108
+ │ ┆ ┆ ┆ ┆ ┆ RDKit 2D ┆ ┆ ┆ │
109
+ │ ┆ ┆ ┆ ┆ ┆ ┆ ┆ ┆ │
110
+ │ ┆ ┆ ┆ ┆ ┆ 30 3… ┆ ┆ ┆ │
111
+ │ 31865 ┆ 88152 ┆ 6432 ┆ 208970 ┆ … ┆ ┆ InChI=1S/C23H14N2O5/c1-12-5-8-… ┆ SUKVIELCKKEBOJ-UHFFFAOYSA-N ┆ Cc1ccc2oc(-c3cccc(N4C(=O)c5ccc… │
112
+ │ ┆ ┆ ┆ ┆ ┆ RDKit 2D ┆ ┆ ┆ │
113
+ │ ┆ ┆ ┆ ┆ ┆ ┆ ┆ ┆ │
114
+ │ ┆ ┆ ┆ ┆ ┆ 30 3… ┆ ┆ ┆ │
115
+ │ 31866 ┆ 83907 ┆ 6432 ┆ 208987 ┆ … ┆ ┆ InChI=1S/C30H20N2O7/c1-37-24-6… ┆ ZFJHZUAZBGPPQK-UHFFFAOYSA-N ┆ COc1ccccc1-c1ccc2oc(-c3ccc(OC)… │
116
+ │ ┆ ┆ ┆ ┆ ┆ RDKit 2D ┆ ┆ ┆ │
117
+ │ ┆ ┆ ┆ ┆ ┆ ┆ ┆ ┆ │
118
+ │ ┆ ┆ ┆ ┆ ┆ 39 4… ┆ ┆ ┆ │
119
+ │ 31867 ┆ 88153 ┆ 6432 ┆ 208987 ┆ … ┆ ┆ InChI=1S/C30H20N2O7/c1-37-24-6… ┆ ZFJHZUAZBGPPQK-UHFFFAOYSA-N ┆ COc1ccccc1-c1ccc2oc(-c3ccc(OC)… │
120
+ │ ┆ ┆ ┆ ┆ ┆ RDKit 2D ┆ ┆ ┆ │
121
+ │ ┆ ┆ ┆ ┆ ┆ ┆ ┆ ┆ │
122
+ │ ┆ ┆ ┆ ┆ ┆ 39 4… ┆ ┆ ┆ │
123
+ └─────────────┴──────────┴────────┴───────────┴───┴────────────────────────┴─────────────────────────────────┴─────────────────────────────┴─────────────────────────────────┘
124
+ ```
125
+
126
+ #### move it to pandas for direct drop-in use (if you really want pandas...)
127
+
128
+ ideally as far down the line after you complete all filtering, you call `.collect().to_pandas()` (see polars documentation for more info)
129
+
130
+ ```
131
+ mychembl.lfs["activities"].collect().to_pandas()
132
+ ```
133
+
134
+ # roadmap
135
+
136
+ - [ ] papyrus database support
137
+ - [x] papyrus latest version download
138
+ - [x] simple nested filtering
139
+ - [ ] efficient nested filtering
140
+ - [ ] all-version support
141
+ - [ ] built-in pivots
142
+ - [ ] chembl database support
143
+ - [x] database to tables (parquet)
144
+ - [ ] intuitive pre-merged flat files
145
+ - [ ] database visualisation
146
+ - [ ] remove the need for storing uncompressed .db
147
+ - [ ] reproducion from downloaded raw file
148
+ - [ ] reproducible molecular (and protein?) standardisation
149
+ - [ ] automated time-url logging and manifest files
150
+ - [ ] well-organised logging
151
+ - [ ] dependency minimisation
152
+ - [ ] other database support
153
+ - [ ] preservation of api and parsing logic per major version
@@ -0,0 +1,129 @@
1
+ # fairfetched
2
+
3
+ data APIs for reproducible data fetching in cheminformatics in line with FAIR principles.
4
+ the tool is designed such that all raw data is downloaded and kept in one central place (i.e. CHEMBL 37 as the original .db files), and the user has a fast, lightweight, intuitive API to filter / query the data as required for any particular project.
5
+
6
+ # installation
7
+
8
+ you can install this package through
9
+ `uv add fairfetched` (recommended)
10
+
11
+ or if you do not use the uv package manager:
12
+ `pip install fairfetched`
13
+
14
+ # examples
15
+
16
+ you can download Chembl or Papyrus through:
17
+
18
+ ```python
19
+ from fairfetched.get import Chembl, Papyrus
20
+ mychembl = Chembl.from_latest() # this downloads Chembl raw files + extracts parquet files to wherever you
21
+ # have set the environment variable FAIRFETCHED_HOME, PYSTOW_HOME,
22
+ # or <HOME>/.data if not in environment variables.
23
+ # from there, fairfetched saves it to a folder chembl/<version>
24
+
25
+ mychembl.lfs # a dictionary of all chembl files in polars LazyFrame format, scanned directly from the extracted .parquet files
26
+
27
+
28
+ mychembl.parquet_paths # the paths to the parquet-converted tabular data files in the Chembl .db file
29
+
30
+ mychembl.raw_paths # the paths to the raw chembl file as downloaded from Chembl. currently does include an uncompressed .db file
31
+
32
+ mychembl.compounds # NOT YET IMPLEMENTED !! convenience alias for mychembl.compose()["compounds"], which uses mychembl.lfs LazyFrame joins to obtain an intuitive join of the data.
33
+ # from there, you can
34
+ ```
35
+
36
+ ### examples of how to use the LazyFrames:
37
+
38
+ #### checking which columns+datatypes are in the file, so that you can choose to join them:
39
+
40
+ ```python
41
+ >>> mychembl.lfs["activities"].collect_schema()
42
+ Schema({'activity_id': Int64, 'assay_id': Int64, 'doc_id': Int64, 'record_id': Int64, 'molregno': Int64, 'standard_relation': String, 'standard_value': Float64, 'standard_units': String, 'standard_flag': Int64, 'standard_type': String, 'activity_comment': String, 'data_validity_comment': String, 'potential_duplicate': Int64, 'pchembl_value': Float64, 'bao_endpoint': String, 'uo_units': String, 'qudt_units': String, 'toid': Int64, 'upper_value': Float64, 'standard_upper_value': Null, 'src_id': Int64, 'type': String, 'relation': String, 'value': Float64, 'units': String, 'text_value': String, 'standard_text_value': String, 'action_type': String})
43
+ ```
44
+
45
+ #### selecting all entries based on doc_id:
46
+
47
+ ```python
48
+ >>> mychembl.lfs["activities"].filter(doc_id=89530).drop_nulls("units").collect()
49
+ shape: (107, 28)
50
+ ┌─────────────┬──────────┬────────┬───────────┬───┬───────┬────────────┬─────────────────────┬─────────────┐
51
+ │ activity_id ┆ assay_id ┆ doc_id ┆ record_id ┆ … ┆ units ┆ text_value ┆ standard_text_value ┆ action_type │
52
+ │ --- ┆ --- ┆ --- ┆ --- ┆ ┆ --- ┆ --- ┆ --- ┆ --- │
53
+ │ i64 ┆ i64 ┆ i64 ┆ i64 ┆ ┆ str ┆ str ┆ str ┆ str │
54
+ ╞═════════════╪══════════╪════════╪═══════════╪═══╪═══════╪════════════╪═════════════════════╪═════════════╡
55
+ │ 15120638 ┆ 1431503 ┆ 89530 ┆ 2256150 ┆ … ┆ uM ┆ null ┆ null ┆ null │
56
+ │ 15120639 ┆ 1431503 ┆ 89530 ┆ 2256151 ┆ … ┆ uM ┆ null ┆ null ┆ null │
57
+ │ 15120640 ┆ 1431503 ┆ 89530 ┆ 2256152 ┆ … ┆ uM ┆ null ┆ null ┆ null │
58
+ │ 15120641 ┆ 1431503 ┆ 89530 ┆ 2256153 ┆ … ┆ uM ┆ null ┆ null ┆ null │
59
+ │ 15120642 ┆ 1431503 ┆ 89530 ┆ 2256154 ┆ … ┆ uM ┆ null ┆ null ┆ null │
60
+ │ … ┆ … ┆ … ┆ … ┆ … ┆ … ┆ … ┆ … ┆ … │
61
+ │ 15125200 ┆ 1431507 ┆ 89530 ┆ 2256167 ┆ … ┆ uM ┆ null ┆ null ┆ null │
62
+ │ 15125201 ┆ 1431507 ┆ 89530 ┆ 2256168 ┆ … ┆ uM ┆ null ┆ null ┆ null │
63
+ │ 15125202 ┆ 1431507 ┆ 89530 ┆ 2256169 ┆ … ┆ uM ┆ null ┆ null ┆ null │
64
+ │ 15125203 ┆ 1431507 ┆ 89530 ┆ 2256170 ┆ … ┆ uM ┆ null ┆ null ┆ null │
65
+ │ 15125204 ┆ 1431507 ┆ 89530 ┆ 2256171 ┆ … ┆ uM ┆ null ┆ null ┆ null │
66
+ └─────────────┴──────────┴────────┴───────────┴───┴───────┴────────────┴─────────────────────┴─────────────┘
67
+ ```
68
+
69
+ #### adding compound structure info to the activities on molregno
70
+
71
+ ```python
72
+ >>> mychembl.lfs["activities"].join(mychembl.lfs["compound_structures"],on="molregno",how="left",validate="m:1").head().collect()
73
+ shape: (5, 32)
74
+ ┌─────────────┬──────────┬────────┬───────────┬───┬────────────────────────┬─────────────────────────────────┬─────────────────────────────┬─────────────────────────────────┐
75
+ │ activity_id ┆ assay_id ┆ doc_id ┆ record_id ┆ … ┆ molfile ┆ standard_inchi ┆ standard_inchi_key ┆ canonical_smiles │
76
+ │ --- ┆ --- ┆ --- ┆ --- ┆ ┆ --- ┆ --- ┆ --- ┆ --- │
77
+ │ i64 ┆ i64 ┆ i64 ┆ i64 ┆ ┆ str ┆ str ┆ str ┆ str │
78
+ ╞═════════════╪══════════╪════════╪═══════════╪═══╪════════════════════════╪═════════════════════════════════╪═════════════════════════════╪═════════════════════════════════╡
79
+ │ 31863 ┆ 54505 ┆ 6424 ┆ 206172 ┆ … ┆ ┆ InChI=1S/C20H12N2O2/c1-2-7-13(… ┆ BEBACPIIZGRKGG-UHFFFAOYSA-N ┆ c1ccc(-c2nc3c(-c4nc5ccccc5o4)c… │
80
+ │ ┆ ┆ ┆ ┆ ┆ RDKit 2D ┆ ┆ ┆ │
81
+ │ ┆ ┆ ┆ ┆ ┆ ┆ ┆ ┆ │
82
+ │ ┆ ┆ ┆ ┆ ┆ 24 2… ┆ ┆ ┆ │
83
+ │ 31864 ┆ 83907 ┆ 6432 ┆ 208970 ┆ … ┆ ┆ InChI=1S/C23H14N2O5/c1-12-5-8-… ┆ SUKVIELCKKEBOJ-UHFFFAOYSA-N ┆ Cc1ccc2oc(-c3cccc(N4C(=O)c5ccc… │
84
+ │ ┆ ┆ ┆ ┆ ┆ RDKit 2D ┆ ┆ ┆ │
85
+ │ ┆ ┆ ┆ ┆ ┆ ┆ ┆ ┆ │
86
+ │ ┆ ┆ ┆ ┆ ┆ 30 3… ┆ ┆ ┆ │
87
+ │ 31865 ┆ 88152 ┆ 6432 ┆ 208970 ┆ … ┆ ┆ InChI=1S/C23H14N2O5/c1-12-5-8-… ┆ SUKVIELCKKEBOJ-UHFFFAOYSA-N ┆ Cc1ccc2oc(-c3cccc(N4C(=O)c5ccc… │
88
+ │ ┆ ┆ ┆ ┆ ┆ RDKit 2D ┆ ┆ ┆ │
89
+ │ ┆ ┆ ┆ ┆ ┆ ┆ ┆ ┆ │
90
+ │ ┆ ┆ ┆ ┆ ┆ 30 3… ┆ ┆ ┆ │
91
+ │ 31866 ┆ 83907 ┆ 6432 ┆ 208987 ┆ … ┆ ┆ InChI=1S/C30H20N2O7/c1-37-24-6… ┆ ZFJHZUAZBGPPQK-UHFFFAOYSA-N ┆ COc1ccccc1-c1ccc2oc(-c3ccc(OC)… │
92
+ │ ┆ ┆ ┆ ┆ ┆ RDKit 2D ┆ ┆ ┆ │
93
+ │ ┆ ┆ ┆ ┆ ┆ ┆ ┆ ┆ │
94
+ │ ┆ ┆ ┆ ┆ ┆ 39 4… ┆ ┆ ┆ │
95
+ │ 31867 ┆ 88153 ┆ 6432 ┆ 208987 ┆ … ┆ ┆ InChI=1S/C30H20N2O7/c1-37-24-6… ┆ ZFJHZUAZBGPPQK-UHFFFAOYSA-N ┆ COc1ccccc1-c1ccc2oc(-c3ccc(OC)… │
96
+ │ ┆ ┆ ┆ ┆ ┆ RDKit 2D ┆ ┆ ┆ │
97
+ │ ┆ ┆ ┆ ┆ ┆ ┆ ┆ ┆ │
98
+ │ ┆ ┆ ┆ ┆ ┆ 39 4… ┆ ┆ ┆ │
99
+ └─────────────┴──────────┴────────┴───────────┴───┴────────────────────────┴─────────────────────────────────┴─────────────────────────────┴─────────────────────────────────┘
100
+ ```
101
+
102
+ #### move it to pandas for direct drop-in use (if you really want pandas...)
103
+
104
+ ideally as far down the line after you complete all filtering, you call `.collect().to_pandas()` (see polars documentation for more info)
105
+
106
+ ```
107
+ mychembl.lfs["activities"].collect().to_pandas()
108
+ ```
109
+
110
+ # roadmap
111
+
112
+ - [ ] papyrus database support
113
+ - [x] papyrus latest version download
114
+ - [x] simple nested filtering
115
+ - [ ] efficient nested filtering
116
+ - [ ] all-version support
117
+ - [ ] built-in pivots
118
+ - [ ] chembl database support
119
+ - [x] database to tables (parquet)
120
+ - [ ] intuitive pre-merged flat files
121
+ - [ ] database visualisation
122
+ - [ ] remove the need for storing uncompressed .db
123
+ - [ ] reproducion from downloaded raw file
124
+ - [ ] reproducible molecular (and protein?) standardisation
125
+ - [ ] automated time-url logging and manifest files
126
+ - [ ] well-organised logging
127
+ - [ ] dependency minimisation
128
+ - [ ] other database support
129
+ - [ ] preservation of api and parsing logic per major version
File without changes
@@ -0,0 +1,9 @@
1
+ from .dataset import Chembl, Papyrus
2
+
3
+ __all__ = ["Chembl", "Papyrus"]
4
+
5
+ if __name__ == "__main__":
6
+ from .papyrus import ensure_raw_files, latest
7
+
8
+ print(latest())
9
+ ensure_raw_files(latest())
@@ -0,0 +1,219 @@
1
+ from collections.abc import Sequence
2
+ from pathlib import Path
3
+ from typing import Any
4
+
5
+ import polars as pl
6
+
7
+ from fairfetched.utils import (
8
+ BASE_DIR,
9
+ ensure_sqlite_db_to_parquets,
10
+ ensure_untarred_sqlite,
11
+ ensure_url,
12
+ file_suffix_from_url,
13
+ lowercase_columns,
14
+ )
15
+ from fairfetched.utils.typing import BioactivityDBViews
16
+
17
+ CHEMBL_DIR = BASE_DIR / "chembl"
18
+
19
+
20
+ def _format_version(version: float | str) -> str:
21
+ if isinstance(version, int | float):
22
+ version = str(version)
23
+ if isinstance(version, Sequence) and not isinstance(version, str):
24
+ raise TypeError(f"invalid version type: {type(version)}")
25
+ if not isinstance(version, str):
26
+ try:
27
+ version = str(version)
28
+ except Exception:
29
+ raise TypeError(f"invalid version type: {type(version)}")
30
+
31
+ version = version.lstrip("0")
32
+ if "." in version:
33
+ version = version.split(".")[0].zfill(2) + "." + version.split(".")[1]
34
+ # for canonicalize the version number 22.1 and 24.1 and left pad with a zero if needed
35
+ return version.replace(".", "_").replace("_0", "").zfill(2)
36
+
37
+
38
+ def _version_to_url(version: str):
39
+ base = "https://ftp.ebi.ac.uk/pub/databases/chembl/ChEMBLdb/releases"
40
+ return f"{base}/chembl_{version}/chembl_{version}_sqlite.tar.gz"
41
+
42
+
43
+ CHEMBL_VERSIONS: dict[str, dict[str, str]] = {
44
+ version: {"sql_db": _version_to_url(version)}
45
+ for version in sorted(map(_format_version, list(range(1, 38)) + ["24_1", "22_1"]))
46
+ }
47
+
48
+
49
+ def available_versions() -> tuple[str, ...]:
50
+ return tuple(CHEMBL_VERSIONS.keys())
51
+
52
+
53
+ def latest() -> str:
54
+ return available_versions()[-1]
55
+
56
+
57
+ def source_urls(version: str) -> dict[str, str]:
58
+ return CHEMBL_VERSIONS[str(version)]
59
+
60
+
61
+ def ensure_raw_files(
62
+ version: str, raw_dir: Path | str | None = None, force=False
63
+ ) -> dict[str, Path]:
64
+ """Download the original SQL database with its original name and compression."""
65
+ if raw_dir is None:
66
+ raw_dir = CHEMBL_DIR / version
67
+ raw_dir = Path(raw_dir)
68
+ return {
69
+ name: ensure_url(
70
+ url=url, path=raw_dir / f"{name}{file_suffix_from_url(url)}", force=force
71
+ )
72
+ for name, url in source_urls(version).items()
73
+ }
74
+
75
+
76
+ def ensure_parquet_tables(
77
+ raw_paths: dict[str, Path], table_dir: Path | str | Any | None = None
78
+ ) -> dict[str, Path]:
79
+ sql_tar_gz_path = raw_paths["sql_db"]
80
+ if table_dir is None:
81
+ table_dir = Path(sql_tar_gz_path).parent / "extracted"
82
+ table_dir = Path(table_dir)
83
+ table_dir.mkdir(exist_ok=True, parents=True)
84
+
85
+ raw_sql = ensure_untarred_sqlite(sql_tar_gz_path)
86
+ # the untarred should also stay so that we have access.....
87
+ # #@TODO: perhaps make tables deterministic for chembl to circumvent
88
+ parquets = ensure_sqlite_db_to_parquets(raw_sql, cache_dir=table_dir, force=False)
89
+
90
+ return parquets
91
+
92
+
93
+ def cleanly_scan_parquet(path_: Path | str) -> pl.LazyFrame:
94
+ """scans parquet paths and lazily handles null value conversion to None"""
95
+ return (
96
+ pl.scan_parquet(path_)
97
+ .pipe(lowercase_columns)
98
+ .fill_nan(None)
99
+ .with_columns(
100
+ pl.col(pl.String).replace({"": None}),
101
+ )
102
+ )
103
+
104
+
105
+ def cleanly_scan_parquet_tables(
106
+ parquet_paths: dict[str, Path],
107
+ ) -> dict[str, pl.LazyFrame]:
108
+ """scans parquet paths and lazily handles null value conversion to None"""
109
+ return {name: cleanly_scan_parquet(path_) for name, path_ in parquet_paths.items()}
110
+
111
+
112
+ # def build_views(lfs: dict[str, pl.LazyFrame]) -> ComposedLFDict:
113
+ def build_views(parquet_paths: dict[str, Path]) -> BioactivityDBViews:
114
+ """Build joined domain views from the scanned source tables."""
115
+ return {
116
+ "bioactivity": _bioactivities(parquet_paths),
117
+ "compounds": _compounds(parquet_paths),
118
+ "proteins": cleanly_scan_parquet(parquet_paths["protein"]),
119
+ "components": _components(parquet_paths),
120
+ }
121
+
122
+
123
+ def _bioactivities(parquet_paths: dict[str, Path]) -> pl.LazyFrame:
124
+ lfs = cleanly_scan_parquet_tables(parquet_paths)
125
+ return (
126
+ # dfs["activity_properties"]
127
+ # .join(dfs["activities"], on="activity_id", how="left")
128
+ # .join(dfs["action_type"], on="action_type", how="left")
129
+ # .join
130
+ lfs["bioactivity"]
131
+ .join(
132
+ lfs["protein"],
133
+ on="target_id",
134
+ how="left",
135
+ maintain_order="left",
136
+ validate="m:1", # one unique protein only from right, can reoccur within compounds.
137
+ )
138
+ .join(lfs["action_type"], on="action_type", how="left", suffix="_action_type")
139
+ .join(
140
+ lfs["assays"], on="assay_id", how="left", suffix="_assay"
141
+ ) # doc_ids to this
142
+ .join(lfs["assay_type"], on="assay_type", how="left", suffix="_assay_type")
143
+ # .join(lfs, on="bao_format")
144
+ # .join(dfs[])
145
+ )
146
+
147
+
148
+ def _components(parquet_paths: dict[str, Path]) -> pl.LazyFrame:
149
+ lfs = cleanly_scan_parquet_tables(parquet_paths)
150
+ return (
151
+ lfs["component_sequences"]
152
+ .join(
153
+ lfs["component_class"],
154
+ on="component_id",
155
+ how="left",
156
+ suffix="_class",
157
+ validate="1:m",
158
+ )
159
+ .join(
160
+ lfs["component_domains"].join(
161
+ lfs["domains"],
162
+ on="domain_id",
163
+ how="left",
164
+ suffix="_domains",
165
+ validate="m:1",
166
+ ),
167
+ on="component_id",
168
+ how="left",
169
+ suffix="_domains",
170
+ validate="1:m",
171
+ )
172
+ # .join(
173
+ # dfs["component_synonyms"],
174
+ # on="component_id",
175
+ # how="left",
176
+ # suffix="_synonyms",
177
+ # validate="m:m",
178
+ # )
179
+ )
180
+
181
+
182
+ def _compounds(parquet_paths: dict[str, Path]) -> pl.LazyFrame:
183
+ lfs = cleanly_scan_parquet_tables(parquet_paths)
184
+ return (
185
+ lfs["molecule_dictionary"]
186
+ .join(
187
+ lfs["compound_properties"],
188
+ on="molregno",
189
+ how="left",
190
+ suffix="_compound_properties",
191
+ validate="1:1",
192
+ )
193
+ .join(
194
+ lfs["compound_structures"],
195
+ on="molregno",
196
+ how="left",
197
+ suffix="_compound_structures",
198
+ validate="1:1",
199
+ )
200
+ .join(
201
+ lfs["compound_records"].join(
202
+ lfs["docs"], on="doc_id", how="left", suffix="_doc", validate="m:1"
203
+ ),
204
+ on="molregno",
205
+ how="left",
206
+ suffix="_compound_records",
207
+ validate="1:m",
208
+ )
209
+ .join(
210
+ lfs["compound_structural_alerts"],
211
+ # .join(
212
+ # dfs["docs"], on="doc_id", how="left", suffix="_doc",validate="m:1"
213
+ # )
214
+ on="molregno",
215
+ how="left",
216
+ suffix="_compound_structural_alerts",
217
+ validate="1:m",
218
+ )
219
+ )