chemrxn-cleaner 0.0.3__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.
- chemrxn_cleaner-0.0.3/LICENSE +21 -0
- chemrxn_cleaner-0.0.3/MANIFEST.in +8 -0
- chemrxn_cleaner-0.0.3/PKG-INFO +188 -0
- chemrxn_cleaner-0.0.3/README.md +161 -0
- chemrxn_cleaner-0.0.3/chemrxn_cleaner/__init__.py +48 -0
- chemrxn_cleaner-0.0.3/chemrxn_cleaner/cleaning.py +145 -0
- chemrxn_cleaner-0.0.3/chemrxn_cleaner/extractor.py +62 -0
- chemrxn_cleaner-0.0.3/chemrxn_cleaner/filters.py +181 -0
- chemrxn_cleaner-0.0.3/chemrxn_cleaner/loader.py +96 -0
- chemrxn_cleaner-0.0.3/chemrxn_cleaner/parsing.py +134 -0
- chemrxn_cleaner-0.0.3/chemrxn_cleaner/reporting.py +146 -0
- chemrxn_cleaner-0.0.3/chemrxn_cleaner/types.py +45 -0
- chemrxn_cleaner-0.0.3/chemrxn_cleaner/utils.py +0 -0
- chemrxn_cleaner-0.0.3/chemrxn_cleaner.egg-info/PKG-INFO +188 -0
- chemrxn_cleaner-0.0.3/chemrxn_cleaner.egg-info/SOURCES.txt +29 -0
- chemrxn_cleaner-0.0.3/chemrxn_cleaner.egg-info/dependency_links.txt +1 -0
- chemrxn_cleaner-0.0.3/chemrxn_cleaner.egg-info/requires.txt +6 -0
- chemrxn_cleaner-0.0.3/chemrxn_cleaner.egg-info/top_level.txt +2 -0
- chemrxn_cleaner-0.0.3/examples/clean_data_example.py +36 -0
- chemrxn_cleaner-0.0.3/pyproject.toml +39 -0
- chemrxn_cleaner-0.0.3/setup.cfg +4 -0
- chemrxn_cleaner-0.0.3/setup.py +28 -0
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Peiye Liu
|
|
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,188 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: chemrxn-cleaner
|
|
3
|
+
Version: 0.0.3
|
|
4
|
+
Summary: A lightweight toolkit for cleaning and standardizing organic reaction datasets.
|
|
5
|
+
Home-page: https://github.com/peiyeliu/chemrxn-cleaner
|
|
6
|
+
Author: Peiye Liu
|
|
7
|
+
Author-email: Peiye Liu <peiyeliu.work@outlook.com>
|
|
8
|
+
License: MIT
|
|
9
|
+
Project-URL: Homepage, https://github.com/peiyeliu/chemrxn-cleaner
|
|
10
|
+
Classifier: Programming Language :: Python :: 3
|
|
11
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
12
|
+
Classifier: Intended Audience :: Science/Research
|
|
13
|
+
Classifier: Topic :: Scientific/Engineering :: Chemistry
|
|
14
|
+
Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
|
|
15
|
+
Requires-Python: >=3.9
|
|
16
|
+
Description-Content-Type: text/markdown
|
|
17
|
+
License-File: LICENSE
|
|
18
|
+
Requires-Dist: rdkit-pypi
|
|
19
|
+
Requires-Dist: pandas>=1.5.0
|
|
20
|
+
Requires-Dist: tqdm>=4.64.0
|
|
21
|
+
Provides-Extra: ord
|
|
22
|
+
Requires-Dist: ord-schema>=0.3.0; extra == "ord"
|
|
23
|
+
Dynamic: author
|
|
24
|
+
Dynamic: home-page
|
|
25
|
+
Dynamic: license-file
|
|
26
|
+
Dynamic: requires-python
|
|
27
|
+
|
|
28
|
+
# chemrxn-cleaner
|
|
29
|
+
|
|
30
|
+
A lightweight toolkit for loading, cleaning, and standardizing organic reaction datasets before machine-learning or analytics workflows.
|
|
31
|
+
|
|
32
|
+
## Prerequisites
|
|
33
|
+
|
|
34
|
+
- Python 3.9–3.10 (Python 3.12+ currently has rdkit compatibility issues)
|
|
35
|
+
- [RDKit](https://www.rdkit.org/) (installable from `rdkit-pypi` on PyPI)
|
|
36
|
+
- Open Reaction Database schema utilities: `pip install ord-schema`
|
|
37
|
+
|
|
38
|
+
These dependencies are pulled in automatically when installing `chemrxn-cleaner`, with the exception of platform-specific RDKit wheels.
|
|
39
|
+
|
|
40
|
+
## Installation
|
|
41
|
+
|
|
42
|
+
```bash
|
|
43
|
+
pip install chemrxn-cleaner
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
If you are working from a clone of this repository, install in editable mode to develop locally:
|
|
47
|
+
|
|
48
|
+
```bash
|
|
49
|
+
pip install -e .
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
## How to Use the Package
|
|
53
|
+
|
|
54
|
+
ChemRxn-Cleaner aims to make cleaning reproducible. A typical workflow has five steps:
|
|
55
|
+
|
|
56
|
+
1. Load reaction SMILES and metadata from USPTO `.rsmi` files or ORD protocol bundles.
|
|
57
|
+
2. Parse the SMILES strings into structured `ReactionRecord` objects.
|
|
58
|
+
3. Apply built-in or custom filters (length, element, metadata predicates, etc.).
|
|
59
|
+
4. Canonicalize the surviving reactions to obtain consistent representations.
|
|
60
|
+
5. Summarize the cleaning results or export the cleaned reactions downstream.
|
|
61
|
+
|
|
62
|
+
### 1. Loading Reaction Data
|
|
63
|
+
|
|
64
|
+
```python
|
|
65
|
+
from chemrxn_cleaner.loader import load_uspto_rsmi, load_ord_pb_reaction_smiles
|
|
66
|
+
|
|
67
|
+
# USPTO .rsmi loader (metadata fields stored in meta["fields"])
|
|
68
|
+
uspto_rxns = load_uspto_rsmi("data/uspto_sample.rsmi", keep_meta=True)
|
|
69
|
+
|
|
70
|
+
# ORD dataset loader with optional metadata extraction
|
|
71
|
+
from chemrxn_cleaner.extractor import ord_procedure_yields_meta
|
|
72
|
+
ord_rxns = load_ord_pb_reaction_smiles(
|
|
73
|
+
"data/ord_dataset.pb.gz",
|
|
74
|
+
meta_extractor=ord_procedure_yields_meta,
|
|
75
|
+
)
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
Both loaders return lists of `(reaction_smiles, metadata_dict)` tuples which feed directly into the cleaning utilities.
|
|
79
|
+
|
|
80
|
+
### 2. Running the Built-in Cleaning Pipeline
|
|
81
|
+
|
|
82
|
+
```python
|
|
83
|
+
from chemrxn_cleaner import basic_cleaning_pipeline
|
|
84
|
+
|
|
85
|
+
cleaned_ord = basic_cleaning_pipeline(ord_rxns)
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
`basic_cleaning_pipeline` parses, filters, and canonicalizes every reaction using the default filter stack (`has_product`, `all_molecules_valid`). The result is a list of immutable `ReactionRecord` instances with `reactants`, `reagents`, `products`, and `meta` attributes.
|
|
89
|
+
|
|
90
|
+
### 3. Custom Filters and Canonicalization Options
|
|
91
|
+
|
|
92
|
+
The cleaning helpers are composable; you can control the filter order and canonicalization behavior explicitly:
|
|
93
|
+
|
|
94
|
+
```python
|
|
95
|
+
from chemrxn_cleaner.cleaning import clean_and_canonicalize
|
|
96
|
+
from chemrxn_cleaner.filters import (
|
|
97
|
+
default_filters,
|
|
98
|
+
max_smiles_length,
|
|
99
|
+
element_filter,
|
|
100
|
+
meta_filter,
|
|
101
|
+
)
|
|
102
|
+
from chemrxn_cleaner.types import ElementFilterRule
|
|
103
|
+
|
|
104
|
+
filters = default_filters() + [
|
|
105
|
+
max_smiles_length(250),
|
|
106
|
+
element_filter(
|
|
107
|
+
forbidList=ElementFilterRule(
|
|
108
|
+
reactantElements=[],
|
|
109
|
+
reagentElements=[],
|
|
110
|
+
productElements=["Cl", "Br"],
|
|
111
|
+
)
|
|
112
|
+
),
|
|
113
|
+
meta_filter(lambda meta: meta.get("procedure", {}).get("setup.atmosphere") == "N2"),
|
|
114
|
+
]
|
|
115
|
+
|
|
116
|
+
cleaned_custom = clean_and_canonicalize(
|
|
117
|
+
ord_rxns,
|
|
118
|
+
filters=filters,
|
|
119
|
+
isomeric=False, # drop stereochemistry if desired
|
|
120
|
+
drop_failed_parse=True,
|
|
121
|
+
)
|
|
122
|
+
```
|
|
123
|
+
|
|
124
|
+
Filters are simple callables accepting a `ReactionRecord` and returning `True`/`False`, so you can author domain-specific predicates without touching the core pipeline.
|
|
125
|
+
|
|
126
|
+
### 4. Working with Metadata
|
|
127
|
+
|
|
128
|
+
Metadata travels with each `ReactionRecord` through the pipeline. This makes it easy to slice cleaned reactions based on the original dataset context or to serialize extra descriptors:
|
|
129
|
+
|
|
130
|
+
```python
|
|
131
|
+
subset = [
|
|
132
|
+
rec for rec in cleaned_custom
|
|
133
|
+
if rec.meta.get("yields") and rec.meta["yields"][0]["yield_percent"] > 80
|
|
134
|
+
]
|
|
135
|
+
```
|
|
136
|
+
|
|
137
|
+
Custom metadata extractors (see `chemrxn_cleaner/extractor.py`) can capture procedure notes, yields, or any other ORD fields you care about.
|
|
138
|
+
|
|
139
|
+
### 5. Reporting and Exporting
|
|
140
|
+
|
|
141
|
+
```python
|
|
142
|
+
from chemrxn_cleaner import reporting
|
|
143
|
+
|
|
144
|
+
report = reporting.summarize_cleaning(ord_rxns, cleaned_custom)
|
|
145
|
+
report.pretty_print()
|
|
146
|
+
|
|
147
|
+
# Export canonical reaction SMILES + metadata for downstream use
|
|
148
|
+
import json
|
|
149
|
+
with open("cleaned_ord.jsonl", "w", encoding="utf-8") as f:
|
|
150
|
+
for rec in cleaned_custom:
|
|
151
|
+
payload = {
|
|
152
|
+
"reactants": rec.reactants,
|
|
153
|
+
"reagents": rec.reagents,
|
|
154
|
+
"products": rec.products,
|
|
155
|
+
"meta": rec.meta,
|
|
156
|
+
}
|
|
157
|
+
f.write(json.dumps(payload) + "\n")
|
|
158
|
+
```
|
|
159
|
+
|
|
160
|
+
## Quick Start
|
|
161
|
+
|
|
162
|
+
```python
|
|
163
|
+
from chemrxn_cleaner.loader import load_uspto_rsmi
|
|
164
|
+
from chemrxn_cleaner import basic_cleaning_pipeline, reporting
|
|
165
|
+
|
|
166
|
+
rxns = load_uspto_rsmi("/path/to/file.rsmi", keep_meta=True)
|
|
167
|
+
cleaned = basic_cleaning_pipeline(rxns)
|
|
168
|
+
|
|
169
|
+
report = reporting.summarize_cleaning(rxns, cleaned)
|
|
170
|
+
report.pretty_print()
|
|
171
|
+
```
|
|
172
|
+
|
|
173
|
+
## End-to-End Example Script
|
|
174
|
+
|
|
175
|
+
The repository ships with `examples/clean_data_example.py`, a runnable walkthrough that ties everything together:
|
|
176
|
+
|
|
177
|
+
1. Load USPTO `.rsmi` files and ORD `.pb.gz` datasets.
|
|
178
|
+
2. Apply default + custom filters (length, element whitelist/blacklist, metadata predicates).
|
|
179
|
+
3. Generate cleaning reports.
|
|
180
|
+
4. Persist cleaned reactions to disk.
|
|
181
|
+
|
|
182
|
+
Run it with:
|
|
183
|
+
|
|
184
|
+
```bash
|
|
185
|
+
python examples/clean_data_example.py
|
|
186
|
+
```
|
|
187
|
+
|
|
188
|
+
Use this script as a template—swap in your file paths, tweak the filter stack, and tailor the export code to match the format your downstream tools expect.
|
|
@@ -0,0 +1,161 @@
|
|
|
1
|
+
# chemrxn-cleaner
|
|
2
|
+
|
|
3
|
+
A lightweight toolkit for loading, cleaning, and standardizing organic reaction datasets before machine-learning or analytics workflows.
|
|
4
|
+
|
|
5
|
+
## Prerequisites
|
|
6
|
+
|
|
7
|
+
- Python 3.9–3.10 (Python 3.12+ currently has rdkit compatibility issues)
|
|
8
|
+
- [RDKit](https://www.rdkit.org/) (installable from `rdkit-pypi` on PyPI)
|
|
9
|
+
- Open Reaction Database schema utilities: `pip install ord-schema`
|
|
10
|
+
|
|
11
|
+
These dependencies are pulled in automatically when installing `chemrxn-cleaner`, with the exception of platform-specific RDKit wheels.
|
|
12
|
+
|
|
13
|
+
## Installation
|
|
14
|
+
|
|
15
|
+
```bash
|
|
16
|
+
pip install chemrxn-cleaner
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
If you are working from a clone of this repository, install in editable mode to develop locally:
|
|
20
|
+
|
|
21
|
+
```bash
|
|
22
|
+
pip install -e .
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
## How to Use the Package
|
|
26
|
+
|
|
27
|
+
ChemRxn-Cleaner aims to make cleaning reproducible. A typical workflow has five steps:
|
|
28
|
+
|
|
29
|
+
1. Load reaction SMILES and metadata from USPTO `.rsmi` files or ORD protocol bundles.
|
|
30
|
+
2. Parse the SMILES strings into structured `ReactionRecord` objects.
|
|
31
|
+
3. Apply built-in or custom filters (length, element, metadata predicates, etc.).
|
|
32
|
+
4. Canonicalize the surviving reactions to obtain consistent representations.
|
|
33
|
+
5. Summarize the cleaning results or export the cleaned reactions downstream.
|
|
34
|
+
|
|
35
|
+
### 1. Loading Reaction Data
|
|
36
|
+
|
|
37
|
+
```python
|
|
38
|
+
from chemrxn_cleaner.loader import load_uspto_rsmi, load_ord_pb_reaction_smiles
|
|
39
|
+
|
|
40
|
+
# USPTO .rsmi loader (metadata fields stored in meta["fields"])
|
|
41
|
+
uspto_rxns = load_uspto_rsmi("data/uspto_sample.rsmi", keep_meta=True)
|
|
42
|
+
|
|
43
|
+
# ORD dataset loader with optional metadata extraction
|
|
44
|
+
from chemrxn_cleaner.extractor import ord_procedure_yields_meta
|
|
45
|
+
ord_rxns = load_ord_pb_reaction_smiles(
|
|
46
|
+
"data/ord_dataset.pb.gz",
|
|
47
|
+
meta_extractor=ord_procedure_yields_meta,
|
|
48
|
+
)
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
Both loaders return lists of `(reaction_smiles, metadata_dict)` tuples which feed directly into the cleaning utilities.
|
|
52
|
+
|
|
53
|
+
### 2. Running the Built-in Cleaning Pipeline
|
|
54
|
+
|
|
55
|
+
```python
|
|
56
|
+
from chemrxn_cleaner import basic_cleaning_pipeline
|
|
57
|
+
|
|
58
|
+
cleaned_ord = basic_cleaning_pipeline(ord_rxns)
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
`basic_cleaning_pipeline` parses, filters, and canonicalizes every reaction using the default filter stack (`has_product`, `all_molecules_valid`). The result is a list of immutable `ReactionRecord` instances with `reactants`, `reagents`, `products`, and `meta` attributes.
|
|
62
|
+
|
|
63
|
+
### 3. Custom Filters and Canonicalization Options
|
|
64
|
+
|
|
65
|
+
The cleaning helpers are composable; you can control the filter order and canonicalization behavior explicitly:
|
|
66
|
+
|
|
67
|
+
```python
|
|
68
|
+
from chemrxn_cleaner.cleaning import clean_and_canonicalize
|
|
69
|
+
from chemrxn_cleaner.filters import (
|
|
70
|
+
default_filters,
|
|
71
|
+
max_smiles_length,
|
|
72
|
+
element_filter,
|
|
73
|
+
meta_filter,
|
|
74
|
+
)
|
|
75
|
+
from chemrxn_cleaner.types import ElementFilterRule
|
|
76
|
+
|
|
77
|
+
filters = default_filters() + [
|
|
78
|
+
max_smiles_length(250),
|
|
79
|
+
element_filter(
|
|
80
|
+
forbidList=ElementFilterRule(
|
|
81
|
+
reactantElements=[],
|
|
82
|
+
reagentElements=[],
|
|
83
|
+
productElements=["Cl", "Br"],
|
|
84
|
+
)
|
|
85
|
+
),
|
|
86
|
+
meta_filter(lambda meta: meta.get("procedure", {}).get("setup.atmosphere") == "N2"),
|
|
87
|
+
]
|
|
88
|
+
|
|
89
|
+
cleaned_custom = clean_and_canonicalize(
|
|
90
|
+
ord_rxns,
|
|
91
|
+
filters=filters,
|
|
92
|
+
isomeric=False, # drop stereochemistry if desired
|
|
93
|
+
drop_failed_parse=True,
|
|
94
|
+
)
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
Filters are simple callables accepting a `ReactionRecord` and returning `True`/`False`, so you can author domain-specific predicates without touching the core pipeline.
|
|
98
|
+
|
|
99
|
+
### 4. Working with Metadata
|
|
100
|
+
|
|
101
|
+
Metadata travels with each `ReactionRecord` through the pipeline. This makes it easy to slice cleaned reactions based on the original dataset context or to serialize extra descriptors:
|
|
102
|
+
|
|
103
|
+
```python
|
|
104
|
+
subset = [
|
|
105
|
+
rec for rec in cleaned_custom
|
|
106
|
+
if rec.meta.get("yields") and rec.meta["yields"][0]["yield_percent"] > 80
|
|
107
|
+
]
|
|
108
|
+
```
|
|
109
|
+
|
|
110
|
+
Custom metadata extractors (see `chemrxn_cleaner/extractor.py`) can capture procedure notes, yields, or any other ORD fields you care about.
|
|
111
|
+
|
|
112
|
+
### 5. Reporting and Exporting
|
|
113
|
+
|
|
114
|
+
```python
|
|
115
|
+
from chemrxn_cleaner import reporting
|
|
116
|
+
|
|
117
|
+
report = reporting.summarize_cleaning(ord_rxns, cleaned_custom)
|
|
118
|
+
report.pretty_print()
|
|
119
|
+
|
|
120
|
+
# Export canonical reaction SMILES + metadata for downstream use
|
|
121
|
+
import json
|
|
122
|
+
with open("cleaned_ord.jsonl", "w", encoding="utf-8") as f:
|
|
123
|
+
for rec in cleaned_custom:
|
|
124
|
+
payload = {
|
|
125
|
+
"reactants": rec.reactants,
|
|
126
|
+
"reagents": rec.reagents,
|
|
127
|
+
"products": rec.products,
|
|
128
|
+
"meta": rec.meta,
|
|
129
|
+
}
|
|
130
|
+
f.write(json.dumps(payload) + "\n")
|
|
131
|
+
```
|
|
132
|
+
|
|
133
|
+
## Quick Start
|
|
134
|
+
|
|
135
|
+
```python
|
|
136
|
+
from chemrxn_cleaner.loader import load_uspto_rsmi
|
|
137
|
+
from chemrxn_cleaner import basic_cleaning_pipeline, reporting
|
|
138
|
+
|
|
139
|
+
rxns = load_uspto_rsmi("/path/to/file.rsmi", keep_meta=True)
|
|
140
|
+
cleaned = basic_cleaning_pipeline(rxns)
|
|
141
|
+
|
|
142
|
+
report = reporting.summarize_cleaning(rxns, cleaned)
|
|
143
|
+
report.pretty_print()
|
|
144
|
+
```
|
|
145
|
+
|
|
146
|
+
## End-to-End Example Script
|
|
147
|
+
|
|
148
|
+
The repository ships with `examples/clean_data_example.py`, a runnable walkthrough that ties everything together:
|
|
149
|
+
|
|
150
|
+
1. Load USPTO `.rsmi` files and ORD `.pb.gz` datasets.
|
|
151
|
+
2. Apply default + custom filters (length, element whitelist/blacklist, metadata predicates).
|
|
152
|
+
3. Generate cleaning reports.
|
|
153
|
+
4. Persist cleaned reactions to disk.
|
|
154
|
+
|
|
155
|
+
Run it with:
|
|
156
|
+
|
|
157
|
+
```bash
|
|
158
|
+
python examples/clean_data_example.py
|
|
159
|
+
```
|
|
160
|
+
|
|
161
|
+
Use this script as a template—swap in your file paths, tweak the filter stack, and tailor the export code to match the format your downstream tools expect.
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
# chemrxn_cleaner/__init__.py
|
|
2
|
+
|
|
3
|
+
from .types import ReactionRecord
|
|
4
|
+
from .parsing import parse_reaction_smiles, canonicalize_reaction
|
|
5
|
+
from .filters import (
|
|
6
|
+
ReactionFilter,
|
|
7
|
+
has_product,
|
|
8
|
+
all_molecules_valid,
|
|
9
|
+
max_smiles_length,
|
|
10
|
+
element_filter,
|
|
11
|
+
meta_filter,
|
|
12
|
+
default_filters,
|
|
13
|
+
)
|
|
14
|
+
from .cleaning import (
|
|
15
|
+
clean_reactions,
|
|
16
|
+
clean_and_canonicalize,
|
|
17
|
+
basic_cleaning_pipeline,
|
|
18
|
+
)
|
|
19
|
+
from .loader import (
|
|
20
|
+
load_uspto_rsmi,
|
|
21
|
+
load_ord_pb_reaction_smiles,
|
|
22
|
+
)
|
|
23
|
+
from .extractor import ord_procedure_yields_meta
|
|
24
|
+
|
|
25
|
+
__all__ = [
|
|
26
|
+
# types
|
|
27
|
+
"ReactionRecord",
|
|
28
|
+
# parsing
|
|
29
|
+
"parse_reaction_smiles",
|
|
30
|
+
"canonicalize_reaction",
|
|
31
|
+
# filters
|
|
32
|
+
"ReactionFilter",
|
|
33
|
+
"has_product",
|
|
34
|
+
"all_molecules_valid",
|
|
35
|
+
"max_smiles_length",
|
|
36
|
+
"element_filter",
|
|
37
|
+
"meta_filter",
|
|
38
|
+
"default_filters",
|
|
39
|
+
# cleaning
|
|
40
|
+
"clean_reactions",
|
|
41
|
+
"clean_and_canonicalize",
|
|
42
|
+
"basic_cleaning_pipeline",
|
|
43
|
+
# io
|
|
44
|
+
"load_uspto_rsmi",
|
|
45
|
+
"load_ord_pb_reaction_smiles",
|
|
46
|
+
# extractor
|
|
47
|
+
"ord_procedure_yields_meta",
|
|
48
|
+
]
|
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
# chemrxn_cleaner/cleaning.py
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from typing import Iterable, List, Optional, Tuple, Dict, Any
|
|
6
|
+
|
|
7
|
+
from .types import ReactionRecord
|
|
8
|
+
from .parsing import parse_reaction_smiles, canonicalize_reaction
|
|
9
|
+
from .filters import ReactionFilter, default_filters
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
def clean_reactions(
|
|
13
|
+
rxn_smiles_list: Iterable[Tuple[str, Dict[str, Any]]],
|
|
14
|
+
filters: Optional[List[ReactionFilter]] = None,
|
|
15
|
+
drop_failed_parse: bool = True,
|
|
16
|
+
strict: bool = True,
|
|
17
|
+
) -> List[ReactionRecord]:
|
|
18
|
+
"""
|
|
19
|
+
Parse and clean a list of reaction SMILES.
|
|
20
|
+
|
|
21
|
+
This is the core entry point for the cleaning pipeline.
|
|
22
|
+
|
|
23
|
+
Args:
|
|
24
|
+
rxn_smiles_list:
|
|
25
|
+
An iterable of (reaction_smiles, meta_dict) tuples.
|
|
26
|
+
|
|
27
|
+
filters:
|
|
28
|
+
A list of predicate functions. Each filter takes a ReactionRecord and
|
|
29
|
+
returns True if the reaction should be kept, False otherwise.
|
|
30
|
+
If None, uses `default_filters()`.
|
|
31
|
+
|
|
32
|
+
drop_failed_parse:
|
|
33
|
+
- If True: silently drop reactions that cannot be parsed.
|
|
34
|
+
- If False: re-raise the exception from parsing.
|
|
35
|
+
|
|
36
|
+
strict:
|
|
37
|
+
Passed to `parse_reaction_smiles`:
|
|
38
|
+
- True: require exactly 3 parts ('reactants>reagents>products').
|
|
39
|
+
- False: auto-pad/truncate to 3 parts.
|
|
40
|
+
|
|
41
|
+
Returns:
|
|
42
|
+
A list of cleaned ReactionRecord objects which passed all filters.
|
|
43
|
+
"""
|
|
44
|
+
if filters is None:
|
|
45
|
+
filters = default_filters()
|
|
46
|
+
|
|
47
|
+
cleaned: List[ReactionRecord] = []
|
|
48
|
+
|
|
49
|
+
for rxn_entry in rxn_smiles_list:
|
|
50
|
+
if rxn_entry is None:
|
|
51
|
+
continue
|
|
52
|
+
rxn, meta = rxn_entry
|
|
53
|
+
if rxn is None:
|
|
54
|
+
continue
|
|
55
|
+
if meta is None:
|
|
56
|
+
meta = {}
|
|
57
|
+
|
|
58
|
+
try:
|
|
59
|
+
record = parse_reaction_smiles(rxn, strict=strict)
|
|
60
|
+
except Exception:
|
|
61
|
+
if drop_failed_parse:
|
|
62
|
+
continue
|
|
63
|
+
else:
|
|
64
|
+
raise
|
|
65
|
+
record.meta = meta
|
|
66
|
+
|
|
67
|
+
keep = True
|
|
68
|
+
for f in filters:
|
|
69
|
+
if not f(record):
|
|
70
|
+
keep = False
|
|
71
|
+
break
|
|
72
|
+
|
|
73
|
+
if keep:
|
|
74
|
+
cleaned.append(record)
|
|
75
|
+
|
|
76
|
+
return cleaned
|
|
77
|
+
|
|
78
|
+
|
|
79
|
+
def clean_and_canonicalize(
|
|
80
|
+
rxn_smiles_list: Iterable[Tuple[str, Dict[str, Any]]],
|
|
81
|
+
filters: Optional[List[ReactionFilter]] = None,
|
|
82
|
+
drop_failed_parse: bool = True,
|
|
83
|
+
strict: bool = True,
|
|
84
|
+
isomeric: bool = True,
|
|
85
|
+
) -> List[ReactionRecord]:
|
|
86
|
+
"""
|
|
87
|
+
Clean reactions and canonicalize all SMILES in one pass.
|
|
88
|
+
|
|
89
|
+
This is a convenience wrapper around `clean_reactions` + `canonicalize_reaction`.
|
|
90
|
+
|
|
91
|
+
Args:
|
|
92
|
+
rxn_smiles_list:
|
|
93
|
+
Iterable of (reaction_smiles, meta_dict) pairs.
|
|
94
|
+
|
|
95
|
+
filters:
|
|
96
|
+
List of ReactionFilter predicates. If None, uses default_filters().
|
|
97
|
+
|
|
98
|
+
drop_failed_parse:
|
|
99
|
+
Whether to silently drop unparseable reactions.
|
|
100
|
+
|
|
101
|
+
strict:
|
|
102
|
+
Whether to enforce exactly three '>' parts in reaction SMILES.
|
|
103
|
+
|
|
104
|
+
isomeric:
|
|
105
|
+
Whether to keep isomeric SMILES when canonicalizing.
|
|
106
|
+
|
|
107
|
+
Returns:
|
|
108
|
+
List[ReactionRecord] with canonicalized reactants/reagents/products.
|
|
109
|
+
"""
|
|
110
|
+
cleaned = clean_reactions(
|
|
111
|
+
rxn_smiles_list=rxn_smiles_list,
|
|
112
|
+
filters=filters,
|
|
113
|
+
drop_failed_parse=drop_failed_parse,
|
|
114
|
+
strict=strict,
|
|
115
|
+
)
|
|
116
|
+
|
|
117
|
+
canon_records: List[ReactionRecord] = []
|
|
118
|
+
for rec in cleaned:
|
|
119
|
+
canon_records.append(canonicalize_reaction(rec, isomeric=isomeric))
|
|
120
|
+
|
|
121
|
+
return canon_records
|
|
122
|
+
|
|
123
|
+
|
|
124
|
+
def basic_cleaning_pipeline(
|
|
125
|
+
rxn_smiles_list: Iterable[Tuple[str, Dict[str, Any]]],
|
|
126
|
+
) -> List[ReactionRecord]:
|
|
127
|
+
"""
|
|
128
|
+
A simple out-of-the-box cleaning pipeline.
|
|
129
|
+
|
|
130
|
+
Equivalent to:
|
|
131
|
+
clean_and_canonicalize(
|
|
132
|
+
rxn_smiles_list,
|
|
133
|
+
filters=default_filters(),
|
|
134
|
+
drop_failed_parse=True,
|
|
135
|
+
strict=True,
|
|
136
|
+
isomeric=True,
|
|
137
|
+
)
|
|
138
|
+
"""
|
|
139
|
+
return clean_and_canonicalize(
|
|
140
|
+
rxn_smiles_list=rxn_smiles_list,
|
|
141
|
+
filters=default_filters(),
|
|
142
|
+
drop_failed_parse=True,
|
|
143
|
+
strict=True,
|
|
144
|
+
isomeric=True,
|
|
145
|
+
)
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Helper utilities that extract structured metadata from ORD reactions.
|
|
3
|
+
"""
|
|
4
|
+
|
|
5
|
+
from __future__ import annotations
|
|
6
|
+
|
|
7
|
+
from typing import Any, Dict, List, Optional
|
|
8
|
+
|
|
9
|
+
from ord_schema.message_helpers import (
|
|
10
|
+
get_product_yield,
|
|
11
|
+
smiles_from_compound,
|
|
12
|
+
message_to_row,
|
|
13
|
+
)
|
|
14
|
+
from ord_schema.proto import reaction_pb2
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
def ord_procedure_yields_meta(
|
|
18
|
+
reaction: reaction_pb2.Reaction,
|
|
19
|
+
) -> Dict[str, Any]:
|
|
20
|
+
"""
|
|
21
|
+
Build a metadata dict containing procedure fields and product yields
|
|
22
|
+
for a single ORD reaction message.
|
|
23
|
+
"""
|
|
24
|
+
yields_info: List[Dict[str, Any]] = []
|
|
25
|
+
for outcome in reaction.outcomes:
|
|
26
|
+
for product in outcome.products:
|
|
27
|
+
prod_smi: Optional[str] = None
|
|
28
|
+
try:
|
|
29
|
+
prod_smi = smiles_from_compound(product, canonical=True)
|
|
30
|
+
except Exception:
|
|
31
|
+
prod_smi = None
|
|
32
|
+
|
|
33
|
+
y_val = get_product_yield(product, as_measurement=False)
|
|
34
|
+
if y_val is None:
|
|
35
|
+
continue
|
|
36
|
+
|
|
37
|
+
yields_info.append(
|
|
38
|
+
{
|
|
39
|
+
"product_smiles": prod_smi,
|
|
40
|
+
"yield_percent": float(y_val),
|
|
41
|
+
}
|
|
42
|
+
)
|
|
43
|
+
|
|
44
|
+
flat: Dict[str, Any] = message_to_row(reaction)
|
|
45
|
+
procedure_prefixes = (
|
|
46
|
+
"setup.",
|
|
47
|
+
"conditions.",
|
|
48
|
+
"workups.",
|
|
49
|
+
"workup.",
|
|
50
|
+
"notes.",
|
|
51
|
+
"observations.",
|
|
52
|
+
)
|
|
53
|
+
procedure: Dict[str, Any] = {
|
|
54
|
+
k: v
|
|
55
|
+
for k, v in flat.items()
|
|
56
|
+
if any(k.startswith(pref) for pref in procedure_prefixes)
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
return {
|
|
60
|
+
"procedure": procedure,
|
|
61
|
+
"yields": yields_info,
|
|
62
|
+
}
|