Functionalise & docking¶
Fragment attachment and host-guest docking utilities:
cage_isomer_builder.utils.functionalise.
max_guests_in_host ¶
max_guests_in_host(host, guests, ratios=None, max_attempts=1000, overlap_tolerance=0.75, inner_radius_fraction=0.9, seed=None)
Determine how many guest copies fit inside host's cavity at once via Random Sequential Adsorption (see _build_guest_packing for the algorithm and its jamming-limit/PACKMOL tradeoff), rather than estimating it from cavity/guest volume - a closed-form packing formula is either optimistic or overly conservative for anything but a spherical guest. This reuses the exact same per-atom overlap check used to actually build the complexes (see place_guest_in_host), so "fits" here means exactly what it means there too. It's empirical/ stochastic (depends on seed and max_attempts), not a guaranteed global optimum.
Before any random sampling, this also checks whether each guest type can physically enter the cavity at all (see _check_guest_fits_windows): if pywindow can identify the host's actual window(s) (aperture into the cavity) and a guest's narrowest possible cross-section is bigger than the largest one, this raises immediately instead of burning max_attempts on placements that could never succeed.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
host
|
Atoms
|
|
required |
guests
|
ase.Atoms or sequence of ase.Atoms
|
One guest type, or several to mix. |
required |
ratios
|
sequence of float
|
Relative proportion of placed copies to give each guest type
(only meaningful with more than one entry in |
None
|
max_attempts
|
int
|
Consecutive random-placement failures before concluding the packing has jammed (capacity reached). |
1000
|
overlap_tolerance
|
float
|
Fraction of the sum of covalent radii used as the hard-core exclusion distance between any two atoms (host-guest or guest-guest). Lower allows closer contacts, denser packing; 1.0 means atoms may not get closer than touching (their covalent-radius spheres just meet). |
0.75
|
inner_radius_fraction
|
float
|
Fraction of the docking sphere's radius to actually use - a small safety margin against sampling exactly at the cavity wall (the real host-guest clearance is already enforced by overlap_tolerance, so this only needs to be mild). The radius itself comes from pywindow's optimised intrinsic pore diameter when an enclosed cavity can be found (real computed geometry, not an approximation), falling back to the older mean-atom- distance-from-centre-of-mass heuristic otherwise (e.g. for an open, non-enclosing structure that isn't a real cage cavity). |
0.9
|
seed
|
int
|
Seed the random search for a reproducible answer. |
None
|
Returns:
| Type | Description |
|---|---|
int
|
The number of guest copies RSA managed to place without overlap. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If a guest type's narrowest cross-section exceeds the host's largest known window - see _check_guest_fits_windows. Not raised if pywindow can't determine any windows for this host. |
Source code in cage_isomer_builder/utils/functionalise.py
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 | |
place_guest_in_host ¶
place_guest_in_host(host, guests, n_complexes=1, n_guests=1, ratios=None, max_attempts=1000, overlap_tolerance=0.75, inner_radius_fraction=0.9, host_bond_matrix=None, seed=None)
RSA docking: generate n_complexes host-guest structures by placing n_guests copies of one or more guest types from a single, near- maximal Random Sequential Adsorption packing of the cage interior.
Strategy
- Compute a docking sphere for the cage interior: centre and radius from pywindow's optimised intrinsic pore analysis (a real cavity, geometrically determined) when an enclosed cavity can be found, falling back to a cruder mean-atom-distance-from-CoM heuristic otherwise - see _host_pore_geometry.
- If pywindow also found the host's window(s) (the aperture(s) into that cavity), reject up front - before any random sampling - any guest type whose narrowest possible cross-section is larger than the largest window, since it cannot enter in any orientation - see _check_guest_fits_windows. This is a fast, necessary-but-not- sufficient check: passing it means entry is geometrically possible, not guaranteed.
- Build one packing of the whole cavity via Random Sequential Adsorption (see _build_guest_packing): repeatedly try a random position/orientation, accept it if it doesn't overlap the host or any already-accepted copy (real per-atom distances - see _atoms_overlap), until one placement attempt exhausts max_attempts random tries. This packing, and its size, are shared with max_guests_in_host. Because every accepted copy was checked against every earlier one, any subset of the packing is itself a valid simultaneous placement, with no further checking needed.
- n_guests is capped to the packing's size if it's smaller (warning issued). Each of the n_complexes configurations then draws a distinct random n_guests-copy subset of the packing (no two complexes get the same combination - see point 5), preserving each copy's already-accepted position and orientation as-is.
- If n_complexes exceeds the number of distinct n_guests-copy combinations available in the packing, it's capped to that many (warning issued) rather than repeating a combination.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
host
|
Atoms
|
The cage structure. |
required |
guests
|
ase.Atoms or sequence of ase.Atoms
|
One guest type (applied to every copy), or several to mix - which copy gets which type is chosen by _next_type_index (a ratio-weighted round robin), not assigned up front. |
required |
n_complexes
|
int
|
Number of independent host-guest configurations to produce (default 1). |
1
|
n_guests
|
int
|
Number of guest copies requested per complex (default 1). Capped to whatever actually fits - see point 4 above. |
1
|
ratios
|
sequence of float
|
Relative proportion of guest copies to give each entry in
|
None
|
max_attempts
|
int
|
Consecutive random-placement failures before concluding the RSA packing has jammed (capacity reached) - see point 3 above. |
1000
|
overlap_tolerance
|
float
|
Fraction of the sum of covalent radii used as the hard-core exclusion distance between any two atoms (host-guest or guest-guest). Lower allows closer contacts, denser packing; 1.0 means atoms may not get closer than touching. |
0.75
|
inner_radius_fraction
|
float
|
Fraction of the docking sphere's radius to actually use - a small safety margin against sampling exactly at the cavity wall (the real host-guest clearance is already enforced by overlap_tolerance, so this only needs to be mild - see _host_pore_geometry). Reducing it shrinks the usable cavity volume and can noticeably lower how many guests fit. |
0.9
|
host_bond_matrix
|
(ndarray, shape(len(host), len(host)))
|
The host's own bond-order matrix. When omitted (the default), it is derived automatically from the host's geometry via gulp_setup.mmanalysis.analyze_mm - the same UFF4MOF atom-typing backend used elsewhere in this package, so metal-ligand coordination contacts get their usual fractional bond order instead of being missed by a plain covalent-radius cutoff. Pass an explicit matrix (e.g. CageBuilder.get_bond_matrix(), taken directly from the STK construction) to skip this and use known-exact bonds instead. Either way, every returned complex is a HostGuestComplex carrying the merged bond matrix - the host's bonds plus each placed guest copy's own internal bonds (perceived on that isolated guest alone via bonded_pairs, never on the merged host+guest structure - see Notes). No bond is ever added between host and guest, or between two guest copies, since encapsulation is non-covalent. Pass this matrix straight to write_run/write_gulp_gin-style writers instead of letting them re-perceive bonds from geometry on the whole complex, which risks a spurious "bond" wherever a guest happens to sit close to the host wall or to another guest copy. |
None
|
seed
|
int
|
Seed both the RSA packing construction (point 3) and the random draw of which subset of it each complex gets (point 4), for reproducible output. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
complexes |
list of HostGuestComplex
|
n_complexes structures, each a HostGuestComplex
(atoms, bond_matrix, guest_atom_indices, guest_labels) -
guest_labels is the index into |
Raises:
| Type | Description |
|---|---|
ValueError
|
If a guest type's narrowest cross-section exceeds the host's largest known window - see _check_guest_fits_windows. Not raised if pywindow can't determine any windows for this host. |
Notes
Perceiving bonds on a guest in isolation (bonded_pairs(guest_type)) is unambiguous - it's a small, complete molecule, so there's no risk of finding a bond across the cavity to the host or to a different guest copy. That's the whole reason this builds the merged bond matrix from known pieces (host_bond_matrix, plus one bonded_pairs call per guest type) instead of running bond perception once on the final merged structure, where a guest sitting near the host wall (or near another guest) could otherwise be misread as covalently bonded to it.
See _build_guest_packing for the RSA algorithm itself and its tradeoff against a constrained-optimisation packer like PACKMOL.
Source code in cage_isomer_builder/utils/functionalise.py
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 | |
functionalise_host ¶
functionalise_host(host_system, sub_fragment, bond_length=1.5, host_marker='X', fragment_marker='X', guests=None, n_complexes=1, n_guests=1, ratios=None, max_attempts=1000, overlap_tolerance=0.75, inner_radius_fraction=0.9, seed=None)
Functionalise a fragment onto a host cage and optionally dock one or more guest molecules inside the cage interior via RSA packing.
The sub_fragment is aligned to the X dummy atom site in the host, translated so its bonding atom sits at the X position, and merged into the host (removing dummy atoms from both). If guests are given, place_guest_in_host is called to produce n_complexes independent host–guest configurations via Random Sequential Adsorption docking.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
host_system
|
Atoms
|
Host cage containing one dummy atom marking the functionalisation site. |
required |
sub_fragment
|
Atoms
|
Fragment to attach, containing one dummy atom marking its bonding end. |
required |
bond_length
|
float
|
Desired bond length (Å) at the new host–fragment bond (default 1.5). |
1.5
|
host_marker
|
str
|
Element symbol of the bonding-site dummy atom in the host. Default 'X'. Use 'At' when the host was prepared via cage.functionalise() (full_functionalisation), which marks sites with Astatine. |
'X'
|
fragment_marker
|
str
|
Element symbol of the bonding-site dummy atom in sub_fragment (default 'X'). |
'X'
|
guests
|
ase.Atoms or sequence of ase.Atoms
|
Guest molecule(s) to dock inside the cage - one type, or several to mix (see place_guest_in_host). Default is None. |
None
|
n_complexes
|
int
|
Number of independent host–guest configurations to generate (default 1). Ignored when guests is None. |
1
|
n_guests
|
int
|
Number of guest copies requested per complex (default 1), capped to whatever fits - see place_guest_in_host. |
1
|
ratios
|
sequence of float
|
Relative proportion of guest copies per entry in |
None
|
max_attempts
|
int
|
Consecutive random-placement failures before concluding the RSA packing has jammed - see place_guest_in_host. |
1000
|
overlap_tolerance
|
float
|
Fraction of the sum of covalent radii used as the hard-core exclusion distance - see place_guest_in_host. |
0.75
|
inner_radius_fraction
|
float
|
Fraction of the docking sphere's radius to actually use (default 0.9). |
0.9
|
seed
|
int
|
Seed the RSA packing and per-complex subset draw - see place_guest_in_host. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
result |
ase.Atoms, HostGuestComplex, or list of HostGuestComplex
|
If guests is None: a single functionalised host (ase.Atoms). If guests is given and n_complexes == 1: a single HostGuestComplex. If guests is given and n_complexes > 1: a list of n_complexes HostGuestComplex. |
Notes
When guests is given, the bond matrix carried in each HostGuestComplex is derived by place_guest_in_host from the functionalised host's own geometry (via gulp_setup.mmanalysis.analyze_mm), not carried through from host_system - so it already reflects the newly attached fragment's bonds without this function needing to track how fragment attachment shifted atom indices. Pass host_bond_matrix explicitly to place_guest_in_host yourself (e.g. from CageBuilder.get_bond_matrix(), if you attach the fragment separately) to use known-exact bonds instead of this geometry-based guess.
Source code in cage_isomer_builder/utils/functionalise.py
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 710 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 756 757 758 759 760 761 762 763 764 765 | |
functionalise_isomer_sites ¶
functionalise_isomer_sites(host_system, fragments, ratios=None, site_marker='At', fragment_marker='X', bond_length=1.5, seed=None, host_bond_matrix=None)
Attach real functional-group fragments at every active site of an isomer, optionally mixing several fragment types by ratio.
Each active site is an atom of symbol site_marker (an "At" atom,
as produced by full_functionalisation/cage.functionalise() and
left in place at the sites selected by a given isomer descriptor - see
:func:~cage_isomer_builder.utils.read_write.generate_isomer_structure_file).
Each fragment is a small molecule carrying exactly one dummy atom of
symbol fragment_marker marking its own bonding end (e.g. an NH2
fragment built as Atoms('XNHH', ...)). This function is the
multi-site, multi-fragment generalisation of :func:functionalise_host,
which only ever handles a single site.
Which fragment type lands on which site is chosen at random (see
seed): ratios only fixes how many of the active sites get each
fragment overall, not which particular sites.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
host_system
|
Atoms
|
The isomer structure, containing one or more |
required |
fragments
|
ase.Atoms or sequence of ase.Atoms
|
One fragment (applied to every site), or several fragment types to mix across the sites. |
required |
ratios
|
sequence of float
|
Relative proportion of active sites to give each entry in
|
None
|
site_marker
|
str
|
Element symbol marking an active site in |
'At'
|
fragment_marker
|
str
|
Element symbol marking each fragment's own bonding atom. |
'X'
|
bond_length
|
float
|
Desired bond length (Angstrom) at each new host-fragment bond. |
1.5
|
seed
|
int
|
Seed for the random assignment of fragment types to sites, for
reproducible output. |
None
|
host_bond_matrix
|
(ndarray, shape(len(host_system), len(host_system)))
|
The host's own bond-order matrix (e.g. from
:func: |
None
|
Returns:
| Type | Description |
|---|---|
Atoms or FunctionalisedIsomer
|
If |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Source code in cage_isomer_builder/utils/functionalise.py
786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 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 911 912 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 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 | |
full_functionalisation ¶
A function that Identifies all aromatic C-H bonds in the cage and replace the H atoms with "At" (Astatine) atoms to mark functional group attachment points.
Each aromatic H atom is converted into an At atom (the FG anchor), with the C-At bond length extended to 1.47 Angstroms to approximate a C-N bond length. This avoids ambiguity with real N atoms already present in the ligand (e.g. amine groups in diaminobenzene).
Schematically: Before: Aromatic-C — H (C-H bond, 1.3 Ang cutoff) After: Aromatic-C — At (C-At bond, 1.47 Ang)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cage
|
Atoms
|
The cage structure as read from a PDB/XYZ file. Must contain aromatic C-H bonds that will be converted to C-At anchor bonds. The structure is modified in place. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
fg_anchors |
Atoms
|
Subset of the cage containing only the At anchor atoms, one per aromatic C-H bond found. |
fg_anchor_indices |
list of int
|
Indices of the At anchor atoms in the full cage Atoms object. Used to map between the FG anchor subset and the full cage, e.g. in fg2fg_distance_count and generate_isomer_structure_file. |
fg_anchor_h_positions |
(ndarray, shape(len(fg_anchor_indices), 3))
|
Each anchor's original (pre-extension) hydrogen position, aligned
with |
Source code in cage_isomer_builder/utils/functionalise.py
995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 | |