molpy.adapter¶
RDKit adapter for MolPy.
Provides bidirectional conversion between RDKit molecules and MolPy structures, as well as utilities for 3D generation, visualization, and optimization.
RDKitWrapper ¶
RDKitWrapper(inner, **props)
Bases: Wrapper[Atomistic]
Bidirectional wrapper associating RDKit Mol with MolPy Atomistic.
This class provides a bridge between RDKit's molecular representation and MolPy's Atomistic structure, maintaining consistent atom mappings and enabling coordinate synchronization, 3D generation, and visualization.
The wrapper extends Wrapper[Atomistic] and stores both representations internally, with methods to convert between them and synchronize data.
Key Features
- Bidirectional conversion between Chem.Mol and Atomistic
- Stable atom mapping using MP_ID property tags
- Coordinate synchronization (RDKit ↔ Atomistic)
- 3D structure generation with ETKDG + MMFF optimization
- 2D molecular visualization (SVG)
- Geometry optimization (MMFF/UFF)
Attributes:
| Name | Type | Description |
|---|---|---|
mol |
Mol
|
The RDKit Mol object. |
atomistic |
Atomistic
|
The wrapped Atomistic structure (alias for |
inner |
TInner | Wrapper[TInner]
|
The wrapped Atomistic structure (from Wrapper base class). |
Examples:
Create from SMILES and generate 3D coordinates:
>>> from rdkit import Chem
>>> mol = Chem.MolFromSmiles("CCO")
>>> wrapper = RDKitWrapper.from_mol(mol)
>>> wrapper.generate_3d(optimize=True)
>>> atomistic = wrapper.inner # Access MolPy structure
Create from existing Atomistic:
>>> wrapper = RDKitWrapper.from_atomistic(atomistic)
>>> svg = wrapper.draw(show=False) # Generate 2D drawing
Source code in src/molpy/core/wrappers/base.py
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 | |
draw ¶
draw(*, size=(320, 260), show_indices=True, show_explicit_H=False, highlight_atoms=None, highlight_bonds=None, title=None, show=True)
Generate 2D molecular structure drawing as SVG.
Creates a 2D depiction of the molecule using RDKit's drawing tools. Automatically computes 2D coordinates if not present.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
size
|
tuple[int, int]
|
Image dimensions (width, height) in pixels. |
(320, 260)
|
show_indices
|
bool
|
Whether to display atom indices. |
True
|
show_explicit_H
|
bool
|
Whether to show explicit hydrogen atoms. |
False
|
highlight_atoms
|
list[int] | None
|
List of atom indices to highlight. |
None
|
highlight_bonds
|
list[int] | None
|
List of bond indices to highlight. |
None
|
title
|
str | None
|
Title text to display on the image. |
None
|
show
|
bool
|
Whether to display in Jupyter (requires IPython). |
True
|
Returns:
| Type | Description |
|---|---|
str
|
SVG string representation of the molecule. |
Source code in src/molpy/adapter/rdkit_adapter.py
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 | |
from_atomistic
classmethod
¶
from_atomistic(atomistic)
Create RDKitWrapper from an Atomistic structure or a Wrapper[Atomistic].
This method creates RDKitWrapper that wraps the input directly. When accessing Atomistic methods/properties, the wrapper automatically forwards to the innermost Atomistic via getattr.
Type Preservation:
- wrapper.inner returns the direct input (Atomistic or Wrapper)
- wrapper.unwrap() returns the innermost Atomistic
- wrapper.atoms, wrapper.bonds, etc. auto-forward to innermost Atomistic
Process:
1. Unwrap to get Atomistic for creating RDKit Mol
2. Pass the ORIGINAL input to RDKitWrapper.init()
3. Wrapper.init() stores it directly in _inner
4. Accessing wrapper.atoms auto-forwards to innermost Atomistic
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
atomistic
|
Atomistic | Wrapper[Atomistic]
|
Atomistic structure or Wrapper[Atomistic] (e.g., Monomer[Atomistic]) The wrapper chain is preserved. |
required |
Returns:
| Type | Description |
|---|---|
RDKitWrapper
|
RDKitWrapper wrapping the input |
Examples:
>>> # From plain Atomistic
>>> wrapper = RDKitWrapper.from_atomistic(atomistic)
>>> wrapper.inner # Atomistic (same object)
>>> wrapper.atoms # Auto-forwards to atomistic.atoms
>>> # From Monomer[Atomistic]
>>> monomer: Monomer[Atomistic] = Monomer(atomistic)
>>> wrapper = RDKitWrapper.from_atomistic(monomer)
>>> wrapper.inner # Monomer[Atomistic] (preserved!)
>>> wrapper.unwrap() # Atomistic (innermost)
>>> wrapper.atoms # Auto-forwards to atomistic.atoms via __getattr__
Source code in src/molpy/adapter/rdkit_adapter.py
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 | |
from_mol
classmethod
¶
from_mol(mol, sync_coords=False)
Create RDKitWrapper from a Chem.Mol.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
mol
|
Mol
|
RDKit molecule object |
required |
sync_coords
|
bool
|
If True, synchronize coordinates from RDKit conformer |
False
|
Returns:
| Type | Description |
|---|---|
RDKitWrapper
|
RDKitWrapper instance |
Source code in src/molpy/adapter/rdkit_adapter.py
328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 | |
generate_3d ¶
generate_3d(*, optimize=True, random_seed=42, max_iters=200, add_hydrogens=True)
Generate 3D coordinates using RDKit ETKDG + optional MMFF optimization.
Uses RDKit's ETKDGv3 (Experimental Torsion Knowledge Distance Geometry) method to generate a 3D conformer, optionally followed by MMFF force field optimization. Coordinates and added hydrogens are synchronized back to the Atomistic structure.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
optimize
|
bool
|
Whether to optimize with MMFF force field after embedding. |
True
|
random_seed
|
int
|
Random seed for reproducible coordinate generation. |
42
|
max_iters
|
int
|
Maximum iterations for MMFF optimization. |
200
|
add_hydrogens
|
bool
|
Whether to add explicit hydrogen atoms. |
True
|
Returns:
| Type | Description |
|---|---|
RDKitWrapper
|
Self for method chaining. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If molecule is empty. |
RuntimeError
|
If ETKDG embedding fails after retries. |
Source code in src/molpy/adapter/rdkit_adapter.py
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 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 | |
optimize_geometry ¶
optimize_geometry(*, max_iters=200, force_field='MMFF')
Optimize molecular geometry using force field minimization.
Performs energy minimization on the existing 3D conformer using either MMFF94 or UFF force field. Coordinates are synchronized back to Atomistic.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
max_iters
|
int
|
Maximum optimization iterations. |
200
|
force_field
|
str
|
Force field to use ("MMFF" or "UFF"). |
'MMFF'
|
Returns:
| Type | Description |
|---|---|
RDKitWrapper
|
Self for method chaining. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If no conformer exists or invalid force field specified. |
Source code in src/molpy/adapter/rdkit_adapter.py
711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 | |
sync_coords_from_mol ¶
sync_coords_from_mol()
Synchronize coordinates from RDKit conformer to Atomistic.
Updates atom positions in Atomistic based on RDKit conformer.
Source code in src/molpy/adapter/rdkit_adapter.py
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 | |
sync_coords_to_mol ¶
sync_coords_to_mol()
Synchronize coordinates from Atomistic to RDKit conformer.
Updates RDKit conformer based on atom positions in Atomistic.
Source code in src/molpy/adapter/rdkit_adapter.py
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 | |
atomistic_to_mol ¶
atomistic_to_mol(atomistic)
Convert MolPy Atomistic structure to RDKit Mol.
Creates an RDKit molecule from Atomistic, setting MP_ID property tags on each atom for stable round-trip conversion. If atom positions exist, creates a conformer with 3D coordinates.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
atomistic
|
Atomistic
|
Atomistic structure to convert. |
required |
Returns:
| Type | Description |
|---|---|
Mol
|
RDKit Mol object with MP_ID tags and optional conformer. |
Source code in src/molpy/adapter/rdkit_adapter.py
913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 | |
mol_to_atomistic ¶
mol_to_atomistic(mol)
Convert RDKit Mol to MolPy Atomistic structure.
Performs a straightforward conversion preserving: - Atomic symbols and atomic numbers - Formal charges - Coordinates (if conformer exists) - Bond orders (mapped to 1.0/2.0/3.0/1.5)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
mol
|
Mol
|
RDKit Mol object to convert. |
required |
Returns:
| Type | Description |
|---|---|
Atomistic
|
Atomistic structure with atoms and bonds. |
Source code in src/molpy/adapter/rdkit_adapter.py
867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 | |
monomer_to_mol ¶
monomer_to_mol(monomer)
Convert MolPy Monomer to RDKit Mol.
Convenience function that unwraps the Monomer and converts the underlying Atomistic structure.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
monomer
|
Monomer
|
Monomer wrapper to convert. |
required |
Returns:
| Type | Description |
|---|---|
Mol
|
RDKit Mol object. |
Source code in src/molpy/adapter/rdkit_adapter.py
967 968 969 970 971 972 973 974 975 976 977 978 979 | |
smilesir_to_atomistic ¶
smilesir_to_atomistic(ir)
Convert SmilesIR to Atomistic structure.
Convenience function that converts SmilesIR to RDKit Mol, then to Atomistic.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ir
|
SmilesIR
|
SmilesIR object from MolPy parser. |
required |
Returns:
| Type | Description |
|---|---|
Atomistic
|
Atomistic structure with atoms and bonds. |
Source code in src/molpy/adapter/rdkit_adapter.py
208 209 210 211 212 213 214 215 216 217 218 219 220 | |
smilesir_to_mol ¶
smilesir_to_mol(ir)
Convert SmilesIR to RDKit Mol.
Converts MolPy's internal SMILES representation to an RDKit molecule, preserving aromaticity, charges, stereochemistry, and explicit hydrogens.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ir
|
SmilesIR
|
SmilesIR object from MolPy parser. |
required |
Returns:
| Type | Description |
|---|---|
Mol
|
Sanitized RDKit Mol object. |
Raises:
| Type | Description |
|---|---|
RuntimeError
|
If molecule sanitization fails. |
Source code in src/molpy/adapter/rdkit_adapter.py
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 | |