Reaction Verifier
The ReactionVerifier computes rewards for chemical reaction and retro-synthesis tasks, validating synthesis paths, SMARTS predictions, and reaction product verification.
Overview
The Reaction Verifier supports various reaction-related tasks:
- Retro-synthesis Planning: Validate multi-step synthesis routes
- SMARTS Prediction: Evaluate predicted reaction SMARTS patterns
- Product/Reactant Prediction: Compare predicted molecules to ground truth
- Analog Generation: Generate molecular analogs via synthesis
Supported Task Types
The following task types are supported by the Reaction Verifier:
| Task Type | Description |
|---|---|
final_product |
Predict the final product of a reaction |
reactant |
Predict a single reactant |
all_reactants |
Predict all reactants for a reaction |
smarts |
Predict the SMARTS pattern for a reaction |
full_path |
Provide a complete retro-synthesis path |
full_path_bb_ref |
Synthesis path with building block constraints |
full_path_smarts_ref |
Synthesis path with SMARTS constraints |
analog_gen |
Generate molecular analogs |
ReactionVerifierConfigModel
Bases: BaseModel
Pydantic model for molecular verifier configuration.
This model defines the configuration parameters for the MolecularVerifier class, providing validation and documentation for all configuration options.
Attributes:
| Name | Type | Description |
|---|---|---|
path_to_mappings |
Optional path to property mappings and docking targets configuration directory. |
|
rescale |
Whether to rescale the rewards to a normalized range. |
|
reaction_matrix_path |
str
|
Path to the reaction matrix pickle file used for reaction verification. |
oracle_kwargs |
str
|
Dictionary of keyword arguments to pass to the docking oracle. Can include: - exhaustiveness: Docking exhaustiveness parameter - n_cpu: Number of CPUs for docking - docking_oracle: Type of docking oracle ("pyscreener" or "autodock_gpu") - vina_mode: Command mode for AutoDock GPU |
docking_concurrency_per_gpu |
str
|
Number of concurrent docking runs to allow per GPU. Default is 2 (uses ~1GB per run on 80GB GPU). |
Source code in mol_gen_docking/reward/verifiers/reaction_reward/reaction_verifier_pydantic_model.py
Config
Pydantic configuration.
Source code in mol_gen_docking/reward/verifiers/reaction_reward/reaction_verifier_pydantic_model.py
check_reaction_matrix_path()
Validate that the reaction matrix path exists.
Source code in mol_gen_docking/reward/verifiers/reaction_reward/reaction_verifier_pydantic_model.py
ReactionVerifierInputMetadataModel
Bases: BaseModel
Input metadata model for reaction verifier.
Defines the verification criteria for chemical reaction and retro-synthesis tasks, including objectives, target molecules, reactants, products, and validation constraints.
Attributes:
| Name | Type | Description |
|---|---|---|
objectives |
List[ReactionObjT]
|
List of objective types for the reaction verification. Valid values:
|
target |
List[str]
|
List of target molecules (SMILES strings) for verification. For synthesis tasks: The desired final product molecule For SMARTS tasks: The expected product after reaction Empty list if not applicable to the objective type. |
reactants |
List[List[str]]
|
List of reactant lists for each reaction step. Each inner list contains SMILES strings for reactants in one reaction. For ground truth verification in SMARTS prediction tasks. Empty list if not applicable to the objective type. |
products |
List[str]
|
List of product molecules (SMILES strings) for each reaction step. For ground truth verification in multi-step synthesis. Empty list if not applicable to the objective type. |
building_blocks |
List[str] | None
|
List of valid building block SMILES strings. Optional constraint for synthesis tasks requiring specific building blocks. None if no building block constraints apply. |
smarts |
List[str]
|
Reference SMARTS strings for the reaction steps. Used to verify that reactions follow specific reaction templates. Empty list if not applicable to the objective type. |
or_smarts |
List[str]
|
Original reference SMARTS strings for the reaction steps. Alternative SMARTS patterns that can also be valid. Empty list if not applicable to the objective type. |
n_steps_max |
int
|
Maximum number of reaction steps allowed in the synthesis route. Default is 5. Only applies to full_path objectives. |
idx_chosen |
int
|
Index of the chosen reaction for multi-reaction tasks. Default is 0. Used for tracking in batch processing. |
Source code in mol_gen_docking/reward/verifiers/reaction_reward/input_metadata.py
ReactionVerifierOutputModel
Bases: VerifierOutputModel
Output model for reaction verifier results.
Attributes:
| Name | Type | Description |
|---|---|---|
reward |
float
|
The computed reward for the reaction verification. |
parsed_answer |
str
|
The parsed answer extracted from the model completion. |
verifier_metadata |
ReactionVerifierMetadataModel
|
Metadata related to the reaction verification process. |
Source code in mol_gen_docking/reward/verifiers/reaction_reward/reaction_verifier_pydantic_model.py
ReactionVerifierMetadataModel
Bases: BaseModel
Metadata model for reaction verifier results.
Contains detailed information about the reaction verification process, including validity, product correctness, and reactant validation.
Attributes:
| Name | Type | Description |
|---|---|---|
valid |
float
|
Proportion of valid reaction steps (0.0 to 1.0). For single reaction tasks: 1.0 if valid, 0.0 if invalid. For synthesis route tasks (full_path): proportion of reaction steps that are chemically valid. |
correct_product |
float
|
Whether the product is correct or similarity to the target molecule. For SMARTS prediction tasks: 1.0 if reaction produces the correct product, 0.0 otherwise. For synthesis tasks with tanimoto similarity: Tanimoto similarity score (0.0 to 1.0) between the final product and the target molecule. For exact match tasks: 1.0 if exact match, 0.0 otherwise. |
correct_reactant |
bool
|
Whether all reactants are correct. For building block constrained tasks: True if all reactants are in the allowed building blocks list. For unconstrained tasks: True if all reactants are chemically valid. False if any reactant is invalid or not allowed. |
Source code in mol_gen_docking/reward/verifiers/reaction_reward/reaction_verifier_pydantic_model.py
Reaction verifier for chemical reaction and retro-synthesis tasks.
This module provides the ReactionVerifier class which computes rewards for chemical reaction tasks including retro-synthesis planning, SMARTS prediction, and reaction product verification.
ReactionVerifier
Bases: Verifier
Verifier for chemical reaction and retro-synthesis tasks.
This verifier computes rewards for various reaction-related tasks including: - Final product prediction - Reactant identification - SMARTS pattern prediction - Full retro-synthesis path validation
The verifier uses a reaction matrix to validate synthesis steps and supports both binary and Tanimoto-based reward computation.
Attributes:
| Name | Type | Description |
|---|---|---|
verifier_config |
ReactionVerifierConfigModel
|
Configuration for the reaction verifier. |
rxn_matrix |
ReactantReactionMatrix
|
Pre-loaded reaction matrix for validation. |
check_ground_truth_tasks |
List of task types requiring ground truth comparison. |
|
run_validation_tasks |
List of task types requiring path validation. |
|
logger |
Logger instance for the verifier. |
Source code in mol_gen_docking/reward/verifiers/reaction_reward/reaction_verifier.py
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 | |
__init__(verifier_config)
Initialize the ReactionVerifier.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
verifier_config
|
ReactionVerifierConfigModel
|
Configuration containing reaction matrix path and reward type settings. |
required |
Source code in mol_gen_docking/reward/verifiers/reaction_reward/reaction_verifier.py
get_score(inputs)
Compute reaction rewards for a batch of completions.
This method routes each completion to the appropriate reward function based on the objective type specified in the metadata.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
inputs
|
BatchVerifiersInputModel
|
Batch of completions and metadata for verification. |
required |
Returns:
| Type | Description |
|---|---|
List[ReactionVerifierOutputModel]
|
List of ReactionVerifierOutputModel containing rewards and metadata. |
Notes
- Ground truth tasks: final_product, reactant, all_reactants
- SMARTS tasks: smarts prediction with reaction validation
- Path tasks: full_path with step-by-step validation
Source code in mol_gen_docking/reward/verifiers/reaction_reward/reaction_verifier.py
541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 | |
ground_truth_reward_mol(answer, labels, reactants, product, smarts, objective)
Compute reward for molecule prediction tasks.
Notes The answer must be contained in a JSON object with an "answer" key, which can be either a single SMILES string or a list of SMILES strings.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
answer
|
Dict[str, Any]
|
Model completion containing the answer. |
required |
labels
|
List[str]
|
List of ground truth SMILES strings. |
required |
reactants
|
List[str]
|
List of reactant SMILES strings. |
required |
product
|
str
|
Expected product SMILES string. |
required |
objective
|
str
|
The type of objective for the reaction verification. |
required |
Returns:
| Type | Description |
|---|---|
float
|
Reward value between 0.0 and 1.0. |
Source code in mol_gen_docking/reward/verifiers/reaction_reward/reaction_verifier.py
parse_json_content(content)
Parse JSON content from the model completion.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
content
|
str
|
The extracted answer content from the model completion. |
required |
Returns: Parsed JSON as a dictionary.
Source code in mol_gen_docking/reward/verifiers/reaction_reward/reaction_verifier.py
r_ground_truth_mols(mol_y, mol_label, reactants, product, smarts, objective)
Compute reward for molecule prediction against ground truth.
Source code in mol_gen_docking/reward/verifiers/reaction_reward/reaction_verifier.py
reward_run_path(answer, label, building_blocks, smarts, n_steps_max, reward_type='binary')
Compute reward for retro-synthesis path validation.
Validates a multi-step synthesis path by checking: 1. All reactants are valid building blocks or previous products 2. Each reaction step has a valid SMARTS pattern 3. The final product matches the target (exactly or by Tanimoto similarity)
Notes The answer must be contained in a JSON object with an "answer" key, which should contain a list of steps: Dictionaries containing the keys:
- reactants: List of reactant SMILES strings for this step
- product: List of product SMILES strings for this step
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
answer
|
Dict[str, Any]
|
Model completion containing the synthesis path. |
required |
label
|
str
|
Target product SMILES string. |
required |
building_blocks
|
List[str]
|
List of valid starting building block SMILES. |
required |
smarts
|
List[str]
|
List of allowed SMARTS patterns (empty = use reaction matrix). |
required |
n_steps_max
|
int
|
Maximum allowed number of synthesis steps. |
required |
reward_type
|
Literal['binary', 'tanimoto']
|
"binary" for exact match or "tanimoto" for similarity-based. |
'binary'
|
Returns:
| Type | Description |
|---|---|
Tuple[float, Dict[str, Any]]
|
Tuple of (reward, metadata_dict) containing validation results. |
Source code in mol_gen_docking/reward/verifiers/reaction_reward/reaction_verifier.py
371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 | |
reward_smarts(answer, labels, reactants, product)
Compute reward for SMARTS prediction tasks.
Notes The answer must be contained in a JSON object with an "answer" key, which should be a SMARTS string representing the reaction. The reward is computed based on whether the proposed SMARTS can produce the expected product from the given reactants. A reward of 1.0 is given for an exact match with the ground truth SMARTS, 0.1 if the SMARTS is valid and produces the correct product, and 0.0 otherwise.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
answer
|
Dict[str, Any]
|
Model completion containing the SMARTS answer. |
required |
labels
|
List[str]
|
List containing the ground truth SMARTS string. |
required |
reactants
|
List[str]
|
List of reactant SMILES strings. |
required |
product
|
str
|
Expected product SMILES string. |
required |
Returns:
| Type | Description |
|---|---|
float
|
Tuple of (reward, metadata_dict) where metadata contains |
Dict[str, Any]
|
'Reactants_contained' and 'Products_contained' flags. |
Source code in mol_gen_docking/reward/verifiers/reaction_reward/reaction_verifier.py
Related
- Molecular Verifier - Main orchestrator
- Generation Verifier - De novo generation tasks
- Molecular Property Verifier - Molecular property prediction tasks