Reacter¶
The reacter module provides tools for chemical reactions and topology modification.
Base¶
Core Reacter implementation for chemical transformations.
This module defines the base Reacter class and ProductSet dataclass, providing the foundation for SMIRKS-style reaction semantics.
AnchorSelector
module-attribute
¶
AnchorSelector = Callable[[Atomistic, Atom], Atom]
Select anchor atom given a port atom.
Port atoms are SMILES-marked connection sites (e.g. $, , <, >) stored on atoms via the "port" / "ports" attributes. An anchor* is the actual atom that participates in bond formation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
assembly
|
The Atomistic structure to select from |
required | |
port_atom
|
The port atom entity (SMILES-marked atom) |
required |
Returns:
| Name | Type | Description |
|---|---|---|
Atom |
The anchor atom where the new bond should be formed |
BondFormer
module-attribute
¶
BondFormer = Callable[[Atomistic, Atom, Atom], Bond | None]
Create or modify bonds between two anchor atoms in an assembly.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
assembly
|
The atomistic assembly to modify |
required | |
i
|
First anchor atom |
required | |
j
|
Second anchor atom |
required |
Side effects
Adds or updates bonds in assembly.links
LeavingSelector
module-attribute
¶
LeavingSelector = Callable[[Atomistic, Atom], list[Atom]]
Select leaving group atoms given an anchor atom.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
assembly
|
The Atomistic structure containing the atoms |
required | |
anchor_atom
|
The anchor atom entity (actual reaction site) |
required |
Returns:
| Type | Description |
|---|---|
|
List of atom entities to be removed |
ProductInfo
dataclass
¶
ProductInfo(product, anchor_L=None, anchor_R=None)
Information about the reaction product.
This captures the final product structure and the anchor atoms where the new bond was formed.
ReactantInfo
dataclass
¶
ReactantInfo(merged_reactants, port_atom_L=None, port_atom_R=None)
Information about the reactants in a reaction.
Ports vs anchors: - Ports are SMILES-marked connection atoms (e.g. $, , <, >) - Anchors* are the actual atoms where bonds are formed
This dataclass tracks the merged reactants and the original port atoms on each side before the reaction is executed.
Reacter ¶
Reacter(name, anchor_selector_left, anchor_selector_right, leaving_selector_left, leaving_selector_right, bond_former)
Programmable chemical reaction executor.
A Reacter represents one specific chemical reaction type by composing: 1. Anchor selectors - map port atoms to anchor atoms 2. Leaving selectors - identify atoms to remove 3. Bond former - create new bonds between anchor atoms
The reaction is executed on copies of input monomers, ensuring original structures remain unchanged.
Port Selection Philosophy: Reacter does NOT handle port selection. The caller (e.g., MonomerLinker) must explicitly specify which ports to connect via port_L and port_R. Ports are marked directly on atoms using the "port" or "ports" attribute. This makes the reaction execution deterministic and explicit.
Attributes:
| Name | Type | Description |
|---|---|---|
name |
Descriptive name for this reaction type |
|
anchor_selector_left |
Function to map left port atom to anchor atom |
|
anchor_selector_right |
Function to map right port atom to anchor atom |
|
leaving_selector_left |
Function to select left leaving group from left anchor |
|
leaving_selector_right |
Function to select right leaving group from right anchor |
|
bond_former |
Function to create bond between anchor atoms |
Example
from molpy.reacter import Reacter, select_port_atom, select_one_hydrogen, form_single_bond from molpy import Atomistic
Mark ports on atoms¶
atom_a["port"] = "1" atom_b["port"] = "2"
cc_coupling = Reacter( ... name="C-C_coupling_with_H_loss", ... port_selector_left=select_port_atom, ... port_selector_right=select_port_atom, ... leaving_selector_left=select_one_hydrogen, ... leaving_selector_right=select_one_hydrogen, ... bond_former=form_single_bond, ... )
Port selection is explicit!¶
product = cc_coupling.run(structA, structB, port_L="1", port_R="2") print(product.removed_atoms) # [H1, H2]
Initialize a Reacter with reaction components.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Descriptive name for this reaction |
required |
anchor_selector_left
|
AnchorSelector
|
Selector that maps left port atom to anchor atom |
required |
anchor_selector_right
|
AnchorSelector
|
Selector that maps right port atom to anchor atom |
required |
leaving_selector_left
|
LeavingSelector
|
Selector for left leaving group (from left anchor) |
required |
leaving_selector_right
|
LeavingSelector
|
Selector for right leaving group (from right anchor) |
required |
bond_former
|
BondFormer
|
Function to create bond between anchor atoms |
required |
Source code in src/molpy/reacter/base.py
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 | |
run ¶
run(left, right, port_atom_L, port_atom_R, compute_topology=True, record_intermediates=False, typifier=None)
Execute the reaction between two Atomistic structures.
IMPORTANT: port_atom_L and port_atom_R must be explicit Atom objects. Use find_port_atom() or find_port_atom_by_node() to get them first.
Workflow: 1. Transform port atoms to reaction sites via port selectors 2. Merge structures (or work on single copy for ring closure) 3. Select leaving groups from reaction sites 4. Create bond between reaction sites 5. Remove leaving groups 6. (Optional) Compute new angles/dihedrals 7. Return ReactionResult
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
left
|
Atomistic
|
Left reactant Atomistic structure |
required |
right
|
Atomistic
|
Right reactant Atomistic structure |
required |
port_atom_L
|
Entity
|
Port atom from left structure (the atom with port marker) |
required |
port_atom_R
|
Entity
|
Port atom from right structure (the atom with port marker) |
required |
compute_topology
|
bool
|
If True, compute new angles/dihedrals (default True) |
True
|
record_intermediates
|
bool
|
If True, record intermediate states |
False
|
typifier
|
TypifierBase | None
|
Optional typifier for incremental retypification |
None
|
Returns:
| Type | Description |
|---|---|
ReactionResult
|
ReactionResult containing product and metadata |
Raises:
| Type | Description |
|---|---|
ValueError
|
If port atoms invalid |
Source code in src/molpy/reacter/base.py
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 | |
ReactionMetadata
dataclass
¶
ReactionMetadata(reaction_name, requires_retype=False, entity_maps=list(), intermediates=list())
Metadata about the reaction.
ReactionResult
dataclass
¶
ReactionResult(reactant_info, product_info, topology_changes, metadata)
Container for reaction products and metadata with organized structure.
This class organizes reaction information into logical groups:
reactant_info: Information about the reactants and their port atomsproduct_info: Information about the product and the anchor atomstopology_changes: All topology changes (bonds, angles, dihedrals)metadata: Reaction metadata (name, retyping info, etc.)
TopologyChanges
dataclass
¶
TopologyChanges(new_bonds=list(), new_angles=list(), new_dihedrals=list(), removed_angles=list(), removed_dihedrals=list(), removed_atoms=list(), modified_atoms=set())
Topology changes resulting from the reaction.
Connector¶
Connector interface for integrating Reacter with the linear polymer builder.
This module provides connectors that use Reacter for polymer assembly, allowing flexible reaction specification with default and specialized reactors.
MonomerLinker ¶
MonomerLinker(default_reaction, specialized_reactions=None)
Connector adapter that manages Reacter instances for polymer assembly.
This connector allows specifying a default reaction for most connections, with the ability to override specific monomer pair connections with specialized reacters.
Port Selection Philosophy: This connector does NOT handle port selection. The caller must explicitly provide port_L and port_R when calling connect(). Ports must be marked directly on atoms using the "port" or "ports" attribute. Port selection should be handled by the higher-level builder logic.
Example
from molpy.reacter import Reacter, MonomerLinker from molpy import Atomistic
Mark ports on atoms¶
atom_a["port"] = "1" atom_b["port"] = "2"
Default reaction for most connections¶
default_reacter = Reacter(...)
Special reaction for A-B connection¶
special_reacter = Reacter(...)
connector = MonomerLinker( ... default_reaction=default_reacter, ... specialized_reactions={('A', 'B'): special_reacter} ... )
Explicit port specification required¶
product = connector.connect( ... left=struct_a, right=struct_b, ... left_type='A', right_type='B', ... port_L='1', port_R='2' # REQUIRED ... )
Initialize connector with default and specialized reacters.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
default_reaction
|
Reacter
|
Default Reacter used for most connections |
required |
specialized_reactions
|
dict[tuple[str, str], Reacter] | None
|
Dict mapping (left_type, right_type) -> specialized Reacter |
None
|
Source code in src/molpy/reacter/connector.py
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 | |
clear_history ¶
clear_history()
Clear reaction history.
Source code in src/molpy/reacter/connector.py
200 201 202 | |
connect ¶
connect(left, right, port_L, port_R, left_type=None, right_type=None)
Connect two Atomistic structures using appropriate reacter.
IMPORTANT: port_L and port_R must be explicitly specified. Ports must be marked on atoms using the "port" or "ports" attribute. No automatic port selection or fallback is performed.
This connector is responsible for locating port atoms by name.
The underlying :class:Reacter only operates on anchors and
receives explicit port atoms which it converts to anchors via its
anchor_selector_left / anchor_selector_right callables.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
left
|
Atomistic
|
Left Atomistic structure |
required |
right
|
Atomistic
|
Right Atomistic structure |
required |
port_L
|
str
|
Port name on left structure (REQUIRED) |
required |
port_R
|
str
|
Port name on right structure (REQUIRED) |
required |
left_type
|
str | None
|
Type label for left structure (e.g., 'A', 'B') |
None
|
right_type
|
str | None
|
Type label for right structure |
None
|
Returns:
| Type | Description |
|---|---|
Atomistic
|
Connected Atomistic assembly |
Raises:
| Type | Description |
|---|---|
ValueError
|
If ports not found on structures |
Source code in src/molpy/reacter/connector.py
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 | |
get_all_modified_atoms ¶
get_all_modified_atoms()
Get all atoms that have been modified across all reactions.
Returns:
| Type | Description |
|---|---|
set[Entity]
|
Set of all atoms that need retypification |
Source code in src/molpy/reacter/connector.py
179 180 181 182 183 184 185 186 187 188 189 | |
get_history ¶
get_history()
Get history of all reactions performed.
Useful for batch retypification after polymer assembly.
Returns:
| Type | Description |
|---|---|
list[ReactionResult]
|
List of ReactionResult for each connection made |
Source code in src/molpy/reacter/connector.py
168 169 170 171 172 173 174 175 176 177 | |
needs_retypification ¶
needs_retypification()
Check if any reactions require retypification.
Returns:
| Type | Description |
|---|---|
bool
|
True if retypification needed |
Source code in src/molpy/reacter/connector.py
191 192 193 194 195 196 197 198 | |
Selectors¶
Selector functions for identifying reaction sites and leaving groups.
Selectors are composable functions that identify specific atoms in a structure for use in reactions.
Selector Types:
- Port Selectors (port_selector_left, port_selector_right):
- Signature:
(struct: Atomistic, port_atom: Entity) -> Entity - Transform a port atom to its actual reaction site
-
Example: O port → C neighbor for dehydration
-
Leaving Selectors (leaving_selector_left, leaving_selector_right):
- Signature:
(struct: Atomistic, reaction_site: Entity) -> list[Entity] - Identify atoms to remove from the reaction site
- Example: C → [O, H] for hydroxyl removal
Naming:
- struct: The Atomistic structure containing atoms
- port_atom: The specific atom with port marker
- reaction_site: The atom where the bond will form
find_port_atom ¶
find_port_atom(struct, port_name)
Find an atom with the specified port marker.
This is a utility function, not a selector. Use it to find port atoms before passing them to selectors.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
struct
|
Atomistic
|
Atomistic structure containing ports |
required |
port_name
|
str
|
Name of port to find |
required |
Returns:
| Type | Description |
|---|---|
Atom
|
Atom with matching port |
Raises:
| Type | Description |
|---|---|
ValueError
|
If port not found |
Source code in src/molpy/reacter/selectors.py
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 | |
find_port_atom_by_node ¶
find_port_atom_by_node(struct, port_name, node_id)
Find port atom for a specific node ID.
Useful when structure has multiple nodes and you need to find the port for a specific one.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
struct
|
Atomistic
|
Atomistic structure |
required |
port_name
|
str
|
Name of port |
required |
node_id
|
int
|
The monomer_node_id to match |
required |
Returns:
| Type | Description |
|---|---|
Atom
|
Atom with matching port and node_id |
Raises:
| Type | Description |
|---|---|
ValueError
|
If port not found for the specified node |
Source code in src/molpy/reacter/selectors.py
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 | |
select_all_hydrogens ¶
select_all_hydrogens(struct, reaction_site)
Select all hydrogen atoms bonded to the reaction site.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
struct
|
Atomistic
|
Atomistic structure |
required |
reaction_site
|
Atom
|
Atom to find H neighbors of |
required |
Returns:
| Type | Description |
|---|---|
list[Atom]
|
List of all H atoms bonded to reaction site |
Source code in src/molpy/reacter/selectors.py
172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 | |
select_c_neighbor ¶
select_c_neighbor(struct, port_atom)
Select C neighbor of the port atom as reaction site.
Useful when port is on O but reaction site is adjacent C.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
struct
|
Atomistic
|
Atomistic structure |
required |
port_atom
|
Atom
|
Port atom (typically O) |
required |
Returns:
| Type | Description |
|---|---|
Atom
|
First C neighbor of port_atom |
Source code in src/molpy/reacter/selectors.py
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 | |
select_dehydration_left ¶
select_dehydration_left(struct, port_atom)
Left-side selector for dehydration reactions.
Returns C as reaction site, handling ports on either O or C: - If port on O: returns C neighbor - If port on C: returns C itself
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
struct
|
Atomistic
|
Atomistic structure |
required |
port_atom
|
Atom
|
Port atom (O or C) |
required |
Returns:
| Type | Description |
|---|---|
Atom
|
C atom as reaction site |
Source code in src/molpy/reacter/selectors.py
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 | |
select_dehydration_right ¶
select_dehydration_right(struct, port_atom)
Right-side selector for dehydration reactions.
Returns O as reaction site, handling ports on either O or C: - If port on O: returns O itself - If port on C: returns O neighbor
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
struct
|
Atomistic
|
Atomistic structure |
required |
port_atom
|
Atom
|
Port atom (O or C) |
required |
Returns:
| Type | Description |
|---|---|
Atom
|
O atom as reaction site |
Source code in src/molpy/reacter/selectors.py
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 | |
select_dummy_atoms ¶
select_dummy_atoms(struct, reaction_site)
Select dummy atoms (*) bonded to the reaction site.
Useful for BigSMILES-style reactions where * marks connection points.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
struct
|
Atomistic
|
Atomistic structure |
required |
reaction_site
|
Atom
|
Atom to find dummy neighbors of |
required |
Returns:
| Type | Description |
|---|---|
list[Atom]
|
List of dummy atoms (symbol='*') |
Source code in src/molpy/reacter/selectors.py
190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 | |
select_hydroxyl_group ¶
select_hydroxyl_group(struct, reaction_site)
Select hydroxyl group (-OH) bonded to the reaction site.
Finds single-bonded O neighbor, then finds H bonded to that O. Used for esterification and dehydration reactions.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
struct
|
Atomistic
|
Atomistic structure |
required |
reaction_site
|
Atom
|
Atom (typically C) with -OH attached |
required |
Returns:
| Type | Description |
|---|---|
list[Atom]
|
[O, H] atoms from hydroxyl group |
Source code in src/molpy/reacter/selectors.py
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 | |
select_hydroxyl_h_only ¶
select_hydroxyl_h_only(struct, reaction_site)
Select only the H from hydroxyl group bonded to reaction site.
The O remains (becomes part of the new bond).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
struct
|
Atomistic
|
Atomistic structure |
required |
reaction_site
|
Atom
|
Atom with -OH attached (typically O itself) |
required |
Returns:
| Type | Description |
|---|---|
list[Atom]
|
[H] if found, otherwise [] |
Source code in src/molpy/reacter/selectors.py
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 | |
select_none ¶
select_none(struct, reaction_site)
Select no leaving group - returns empty list.
Useful for addition reactions where nothing is eliminated.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
struct
|
Atomistic
|
Atomistic structure (ignored) |
required |
reaction_site
|
Atom
|
Reaction site atom (ignored) |
required |
Returns:
| Type | Description |
|---|---|
list[Atom]
|
Empty list |
Source code in src/molpy/reacter/selectors.py
300 301 302 303 304 305 306 307 308 309 310 311 312 313 | |
select_o_neighbor ¶
select_o_neighbor(struct, port_atom)
Select O neighbor of the port atom as reaction site.
Useful when port is on C but reaction site is adjacent O.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
struct
|
Atomistic
|
Atomistic structure |
required |
port_atom
|
Atom
|
Port atom (typically C) |
required |
Returns:
| Type | Description |
|---|---|
Atom
|
First O neighbor of port_atom |
Source code in src/molpy/reacter/selectors.py
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 | |
select_one_hydrogen ¶
select_one_hydrogen(struct, reaction_site)
Select one hydrogen atom bonded to the reaction site.
Useful for condensation reactions where H is eliminated.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
struct
|
Atomistic
|
Atomistic structure |
required |
reaction_site
|
Atom
|
Atom to find H neighbor of |
required |
Returns:
| Type | Description |
|---|---|
list[Atom]
|
List with one H atom, or empty list if none |
Source code in src/molpy/reacter/selectors.py
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 | |
select_port ¶
select_port(struct, port_atom)
Port selector - returns the port atom as the reaction site.
Use this when the port atom itself is the reaction site.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
struct
|
Atomistic
|
Atomistic structure containing the atoms |
required |
port_atom
|
Atom
|
The atom with port marker |
required |
Returns:
| Type | Description |
|---|---|
Atom
|
The same port_atom |
Source code in src/molpy/reacter/selectors.py
34 35 36 37 38 39 40 41 42 43 44 45 46 47 | |
Template¶
TemplateReacter: Specialized Reacter for LAMMPS fix bond/react template generation.
This module provides a wrapper around the base Reacter that handles: 1. react_id assignment for atom tracking across reactions 2. Pre/post template extraction with correct atom/bond references 3. Writing LAMMPS molecule and map files
TemplateReacter ¶
TemplateReacter(name, anchor_selector_left, anchor_selector_right, leaving_selector_left, leaving_selector_right, bond_former, radius=4)
Reacter wrapper for LAMMPS fix bond/react template generation.
Composes a Reacter internally and adds: 1. react_id assignment before reaction 2. Subgraph extraction for templates 3. Correct atom/bond reference handling
The function signature matches Reacter, but run_with_template() returns both ReactionResult and TemplateResult.
Example
from molpy.reacter import select_port, select_one_hydrogen, form_single_bond template_reacter = TemplateReacter( ... name="C-C_coupling",
... anchor_selector_left=select_port, ... anchor_selector_right=select_port, ... leaving_selector_left=select_one_hydrogen, ... leaving_selector_right=select_one_hydrogen, ... bond_former=form_single_bond, ... radius=4 ... ) >>> result, template = template_reacter.run_with_template( ... left, right, port_atom_L, port_atom_R ... ) >>> template.pre # Pre-reaction subgraph >>> template.post # Post-reaction subgraph
Initialize TemplateReacter.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Descriptive name for this reaction |
required |
anchor_selector_left
|
AnchorSelector
|
Selector that maps left port atom to anchor atom |
required |
anchor_selector_right
|
AnchorSelector
|
Selector that maps right port atom to anchor atom |
required |
leaving_selector_left
|
LeavingSelector
|
Selector for left leaving group (from left anchor) |
required |
leaving_selector_right
|
LeavingSelector
|
Selector for right leaving group (from right anchor) |
required |
bond_former
|
BondFormer
|
Function to create bond between anchor atoms |
required |
radius
|
int
|
Topological radius for subgraph extraction |
4
|
Source code in src/molpy/reacter/template.py
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 | |
run_with_template ¶
run_with_template(left, right, port_atom_L, port_atom_R, compute_topology=True, record_intermediates=False, typifier=None)
Run reaction and generate templates.
Function signature matches Reacter.run() but returns both ReactionResult and TemplateResult.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
left
|
Atomistic
|
Left reactant structure |
required |
right
|
Atomistic
|
Right reactant structure |
required |
port_atom_L
|
Entity
|
Port atom in left structure |
required |
port_atom_R
|
Entity
|
Port atom in right structure |
required |
compute_topology
|
bool
|
If True, compute new angles/dihedrals (default True) |
True
|
record_intermediates
|
bool
|
If True, record intermediate states |
False
|
typifier
|
TypifierBase | None
|
Optional typifier for incremental retypification |
None
|
Returns:
| Type | Description |
|---|---|
tuple[ReactionResult, TemplateResult]
|
Tuple of (ReactionResult, TemplateResult) |
Source code in src/molpy/reacter/template.py
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 | |
TemplateResult
dataclass
¶
TemplateResult(pre, post, init_atoms, edge_atoms, removed_atoms, pre_react_id_to_atom, post_react_id_to_atom)
Result of template generation.
write_template_files ¶
write_template_files(base_path, template, typifier=None)
Write LAMMPS fix bond/react template files.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
base_path
|
Path
|
Base path for files (e.g., "rxn1" -> rxn1_pre.mol, rxn1_post.mol, rxn1.map) |
required |
template
|
TemplateResult
|
TemplateResult from TemplateReacter |
required |
typifier
|
Optional typifier to ensure types are set |
None
|
Source code in src/molpy/reacter/template.py
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 | |
Topology Detector¶
Topology detection and update for chemical reactions.
This module provides intelligent detection and updating of angles and dihedrals after bond formation in reactions. It identifies affected old topology items and replaces them with new ones based on the updated bond structure.
TopologyDetector ¶
Detects and updates angles and dihedrals after reaction bond formation.
This class implements intelligent topology detection that: 1. Identifies atoms affected by new bonds 2. Removes old topology items (angles/dihedrals) involving affected atoms 3. Generates new topology items based on current bond structure
detect_and_update_topology
classmethod
¶
detect_and_update_topology(assembly, new_bonds, removed_atoms)
Detect and update topology structure after reaction.
This method: 1. Removes topology items involving removed atoms 2. Generates new topology items based on new bonds 3. Adds new topology items (deduplicated with existing)
Note: We do NOT remove topology involving "affected atoms" (bond endpoints and neighbors) because those angles/dihedrals don't need to change - they still exist with the same atoms. We only need to add NEW topology created by the new bond.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
assembly
|
Atomistic
|
The Atomistic structure (will be modified) |
required |
new_bonds
|
list[Bond]
|
List of newly formed bonds |
required |
removed_atoms
|
Collection[Entity]
|
List of atoms that were removed from the structure |
required |
Returns:
| Type | Description |
|---|---|
tuple[list[Angle], list[Dihedral], list[Angle], list[Dihedral]]
|
Tuple of (new_angles, new_dihedrals, removed_angles, removed_dihedrals) |
Source code in src/molpy/reacter/topology_detector.py
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 | |
Transformers¶
Bond-making transformer functions for reactions.
Transformers create or modify bonds between anchor atoms in the product assembly.
break_bond ¶
break_bond(assembly, i, j)
Remove existing bond between two atoms.
Opposite of bond makers - used for bond-breaking reactions.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
assembly
|
Atomistic
|
Struct containing the bond |
required |
i
|
Entity
|
First atom |
required |
j
|
Entity
|
Second atom |
required |
Side effects
Removes bond from assembly.links
Example
break_bond(assembly, carbon1, oxygen1)
Breaks C-O bond¶
Source code in src/molpy/reacter/transformers.py
209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 | |
create_bond_former ¶
create_bond_former(order)
Factory function to create bond former with specific order.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
order
|
int
|
Bond order (1, 2, 3, or 1.5 for aromatic) |
required |
Returns:
| Type | Description |
|---|---|
BondFormer
|
BondFormer function that creates bonds with specified order |
Example
double_bond_former = create_bond_former(2) reacter = Reacter( ... bond_former=double_bond_former, ... ... ... )
Source code in src/molpy/reacter/transformers.py
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 | |
form_aromatic_bond ¶
form_aromatic_bond(assembly, i, j)
Create an aromatic bond between two atoms.
If a bond already exists, updates it to aromatic (order=1.5 by convention).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
assembly
|
Atomistic
|
Struct to add bond to |
required |
i
|
Entity
|
First atom |
required |
j
|
Entity
|
Second atom |
required |
Returns:
| Type | Description |
|---|---|
Bond | None
|
The created or updated Bond object |
Side effects
Adds Bond(i, j, order=1.5, kind=':') to assembly.links
Source code in src/molpy/reacter/transformers.py
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 | |
form_double_bond ¶
form_double_bond(assembly, i, j)
Create a double bond between two atoms.
If a bond already exists, updates it to double bond (order=2).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
assembly
|
Atomistic
|
Struct to add bond to |
required |
i
|
Entity
|
First atom |
required |
j
|
Entity
|
Second atom |
required |
Returns:
| Type | Description |
|---|---|
Bond | None
|
The created or updated Bond object |
Side effects
Adds Bond(i, j, order=2) to assembly.links
Source code in src/molpy/reacter/transformers.py
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 | |
form_single_bond ¶
form_single_bond(assembly, i, j)
Create a single bond between two atoms.
If a bond already exists, updates it to single bond (order=1).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
assembly
|
Atomistic
|
Struct to add bond to |
required |
i
|
Entity
|
First atom |
required |
j
|
Entity
|
Second atom |
required |
Returns:
| Type | Description |
|---|---|
Bond | None
|
The created or updated Bond object |
Side effects
Adds Bond(i, j, order=1) to assembly.links
Example
bond = form_single_bond(merged, carbon1, carbon2)
Source code in src/molpy/reacter/transformers.py
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 | |
form_triple_bond ¶
form_triple_bond(assembly, i, j)
Create a triple bond between two atoms.
If a bond already exists, updates it to triple bond (order=3).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
assembly
|
Atomistic
|
Struct to add bond to |
required |
i
|
Entity
|
First atom |
required |
j
|
Entity
|
Second atom |
required |
Returns:
| Type | Description |
|---|---|
Bond | None
|
The created or updated Bond object |
Side effects
Adds Bond(i, j, order=3) to assembly.links
Source code in src/molpy/reacter/transformers.py
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 | |
skip_bond_formation ¶
skip_bond_formation(assembly, i, j)
Do not create any bond.
Useful for reactions that only remove atoms without forming new bonds.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
assembly
|
Atomistic
|
Struct (ignored) |
required |
i
|
Entity
|
First atom (ignored) |
required |
j
|
Entity
|
Second atom (ignored) |
required |
Side effects
None
Example
reacter = Reacter( ... bond_former=skip_bond_formation, # Just remove leaving groups ... ... ... )
Source code in src/molpy/reacter/transformers.py
186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 | |
Utils¶
Utility functions for assembly manipulation in reactions.
This module provides helper functions for finding neighbors and other common operations needed for reactions.
count_bonds ¶
count_bonds(assembly, atom)
Count the number of bonds connected to an atom.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
assembly
|
Atomistic
|
Atomistic assembly containing the atom |
required |
atom
|
Entity
|
Atom entity to count bonds for |
required |
Returns:
| Type | Description |
|---|---|
int
|
Number of bonds |
Example
valence = count_bonds(asm, carbon_atom) print(f"Carbon has {valence} bonds")
Source code in src/molpy/reacter/utils.py
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 | |
create_atom_mapping ¶
create_atom_mapping(pre_atoms, post_atoms)
Create atom mapping between pre-reaction and post-reaction states.
Creates a mapping of atom IDs from pre-reaction template to post-reaction template. This is used to generate map files for LAMMPS fix bond/react.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
pre_atoms
|
list
|
List of atoms in pre-reaction state |
required |
post_atoms
|
list
|
List of atoms in post-reaction state |
required |
Returns:
| Type | Description |
|---|---|
dict[int, int]
|
Dictionary mapping pre-reaction atom indices to post-reaction indices |
dict[int, int]
|
(1-indexed for LAMMPS) |
Note
Atoms that are deleted during the reaction will not appear in the mapping. Atoms are matched by their 'id' attribute if present, otherwise by position.
Example
from molpy.reacter.utils import create_atom_mapping mapping = create_atom_mapping(pre_atoms, post_atoms)
Write to map file¶
with open("reaction.map", 'w') as f: ... for pre_id, post_id in sorted(mapping.items()): ... f.write(f"{pre_id} {post_id}\n")
Source code in src/molpy/reacter/utils.py
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 | |
find_neighbors ¶
find_neighbors(assembly, atom, *, element=None)
Find neighboring atoms of a given atom.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
assembly
|
Atomistic
|
Atomistic assembly containing the atom |
required |
atom
|
Entity
|
Atom entity to find neighbors of |
required |
element
|
str | None
|
Optional element symbol to filter by (e.g., 'H', 'C') |
None
|
Returns:
| Type | Description |
|---|---|
list[Entity]
|
List of neighboring atom entities |
Example
h_neighbors = find_neighbors(asm, carbon_atom, element='H') all_neighbors = find_neighbors(asm, carbon_atom)
Source code in src/molpy/reacter/utils.py
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 | |
get_bond_between ¶
get_bond_between(assembly, i, j)
Find existing bond between two atoms.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
assembly
|
Atomistic
|
Atomistic assembly containing the atoms |
required |
i
|
Entity
|
First atom entity |
required |
j
|
Entity
|
Second atom entity |
required |
Returns:
| Type | Description |
|---|---|
Bond | None
|
Bond entity if found, None otherwise |
Example
bond = get_bond_between(asm, itom, jtom) if bond: ... print(f"Bond order: {bond.get('order', 1)}")
Source code in src/molpy/reacter/utils.py
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 | |
remove_dummy_atoms ¶
remove_dummy_atoms(assembly)
Remove all dummy atoms (element '' or symbol '') from assembly.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
assembly
|
Atomistic
|
Atomistic assembly to clean |
required |
Returns:
| Type | Description |
|---|---|
list[Entity]
|
List of removed dummy atoms |
Example
removed = remove_dummy_atoms(merged_asm) print(f"Removed {len(removed)} dummy atoms")
Source code in src/molpy/reacter/utils.py
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 | |