Spaces:
Sleeping
Sleeping
File size: 41,397 Bytes
72a3513 |
1 2 3 4 5 6 7 8 9 10 11 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 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 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 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 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 |
from CGRtools.algorithms.depict import (
Depict,
DepictMolecule,
DepictCGR,
rotate_vector,
_render_charge,
)
from CGRtools.containers import ReactionContainer, MoleculeContainer, CGRContainer
from collections import defaultdict
from uuid import uuid4
from math import hypot
from functools import partial
class WideBondDepictCGR(DepictCGR):
"""
Like DepictCGR, but all DynamicBonds
are drawn 2.5× wider than the standard bond width.
"""
__slots__ = ()
def _render_bonds(self):
"""
Renders the bonds of the CGR as SVG lines, with DynamicBonds drawn wider.
This method overrides the base `_render_bonds` to apply a wider stroke
to DynamicBonds, highlighting changes in bond order during a reaction.
It iterates through all bonds, calculates their positions based on
2D coordinates, and generates SVG `<line>` elements with appropriate
styles (color, width, dash array) based on the bond's original (`order`)
and primary (`p_order`) states. Aromatic bonds are handled separately
using a helper method.
Returns:
list: A list of strings, where each string is an SVG element
representing a bond.
"""
plane = self._plane
config = self._render_config
# get the normal width (default 1.0) and compute a 4× wide stroke
normal_width = config.get("bond_width", 0.02)
wide_width = normal_width * 2.5
broken = config["broken_color"]
formed = config["formed_color"]
dash1, dash2 = config["dashes"]
double_space = config["double_space"]
triple_space = config["triple_space"]
svg = []
ar_bond_colors = defaultdict(dict)
for n, m, bond in self.bonds():
order, p_order = bond.order, bond.p_order
nx, ny = plane[n]
mx, my = plane[m]
# invert Y for SVG
ny, my = -ny, -my
rv = partial(rotate_vector, 0, x2=mx - nx, y2=ny - my)
if order == 1:
if p_order == 1:
svg.append(
f' <line x1="{nx:.2f}" y1="{ny:.2f}" x2="{mx:.2f}" y2="{my:.2f}"/>'
)
elif p_order == 4:
ar_bond_colors[n][m] = ar_bond_colors[m][n] = formed
svg.append(
f' <line x1="{nx:.2f}" y1="{ny:.2f}" x2="{mx:.2f}" y2="{my:.2f}"/>'
)
elif p_order == 2:
dx, dy = rv(double_space)
svg.append(
f' <line x1="{nx + dx:.2f}" y1="{ny - dy:.2f}" '
f'x2="{mx + dx:.2f}" y2="{my - dy:.2f}"/>'
)
svg.append(
f' <line x1="{nx - dx:.2f}" y1="{ny + dy:.2f}" '
f'x2="{mx - dx:.2f}" y2="{my + dy:.2f}" stroke="{formed}" stroke-width="{wide_width:.2f}"/>'
)
elif p_order == 3:
dx, dy = rv(triple_space)
svg.append(
f' <line x1="{nx:.2f}" y1="{ny:.2f}" x2="{mx:.2f}" y2="{my:.2f}"'
f' stroke="{formed}" stroke-width="{wide_width:.2f}"/>'
)
svg.append(
f' <line x1="{nx + dx:.2f}" y1="{ny - dy:.2f}" '
f'x2="{mx + dx:.2f}" y2="{my - dy:.2f}" stroke-width="{wide_width:.2f}"/>'
)
svg.append(
f' <line x1="{nx - dx:.2f}" y1="{ny + dy:.2f}" '
f'x2="{mx - dx:.2f}" y2="{my + dy:.2f}" stroke="{formed}" stroke-width="{wide_width:.2f}"/>'
)
elif p_order is None:
svg.append(
f' <line x1="{nx:.2f}" y1="{ny:.2f}" x2="{mx:.2f}" y2="{my:.2f}"'
f' stroke="{broken}" stroke-width="{wide_width:.2f}"/>'
)
else:
dx, dy = rv(double_space)
svg.append(
f' <line x1="{nx + dx:.2f}" y1="{ny - dy:.2f}" x2="{mx + dx:.2f}"'
f' y2="{my - dy:.2f}" stroke-dasharray="{dash1:.2f} {dash2:.2f}" stroke="{formed}" stroke-width="{wide_width:.2f}"/>'
)
svg.append(
f' <line x1="{nx - dx:.2f}" y1="{ny + dy:.2f}" '
f'x2="{mx - dx:.2f}" y2="{my + dy:.2f}" stroke="{broken}" stroke-width="{wide_width:.2f}"/>'
)
elif order == 4:
if p_order == 4:
svg.append(
f' <line x1="{nx:.2f}" y1="{ny:.2f}" x2="{mx:.2f}" y2="{my:.2f}"/>'
)
elif p_order == 1:
ar_bond_colors[n][m] = ar_bond_colors[m][n] = broken
svg.append(
f' <line x1="{nx:.2f}" y1="{ny:.2f}" x2="{mx:.2f}" y2="{my:.2f}"/>'
)
elif p_order == 2:
ar_bond_colors[n][m] = ar_bond_colors[m][n] = broken
dx, dy = rv(double_space)
svg.append(
f' <line x1="{nx + dx:.2f}" y1="{ny - dy:.2f}" '
f'x2="{mx + dx:.2f}" y2="{my - dy:.2f}"/>'
)
svg.append(
f' <line x1="{nx - dx:.2f}" y1="{ny + dy:.2f}" '
f'x2="{mx - dx:.2f}" y2="{my + dy:.2f}" stroke="{formed}" stroke-width="{wide_width:.2f}"/>'
)
elif p_order == 3:
ar_bond_colors[n][m] = ar_bond_colors[m][n] = broken
dx, dy = rv(triple_space)
svg.append(
f' <line x1="{nx + dx:.2f}" y1="{ny - dy:.2f}" '
f'x2="{mx + dx:.2f}" y2="{my - dy:.2f}" stroke="{formed}" stroke-width="{wide_width:.2f}"/>'
)
svg.append(
f' <line x1="{nx:.2f}" y1="{ny:.2f}" x2="{mx:.2f}" y2="{my:.2f}"/>'
)
svg.append(
f' <line x1="{nx - dx:.2f}" y1="{ny + dy:.2f}" '
f'x2="{mx - dx:.2f}" y2="{my + dy:.2f}" stroke="{formed}" stroke-width="{wide_width:.2f}"/>'
)
elif p_order is None:
ar_bond_colors[n][m] = ar_bond_colors[m][n] = broken
svg.append(
f' <line x1="{nx:.2f}" y1="{ny:.2f}" x2="{mx:.2f}" y2="{my:.2f}"'
f' stroke="{broken}" stroke-width="{wide_width:.2f}"/>'
)
else:
ar_bond_colors[n][m] = ar_bond_colors[m][n] = None
svg.append(
f' <line x1="{nx:.2f}" y1="{ny:.2f}" x2="{mx:.2f}" y2="{my:.2f}"'
f' stroke="{broken}" stroke-width="{wide_width:.2f}"/>'
)
elif order == 2:
if p_order == 2:
dx, dy = rv(double_space)
svg.append(
f' <line x1="{nx + dx:.2f}" y1="{ny - dy:.2f}" '
f'x2="{mx + dx:.2f}" y2="{my - dy:.2f}"/>'
)
svg.append(
f' <line x1="{nx - dx:.2f}" y1="{ny + dy:.2f}" '
f'x2="{mx - dx:.2f}" y2="{my + dy:.2f}"/>'
)
elif p_order == 1:
dx, dy = rv(double_space)
svg.append(
f' <line x1="{nx + dx:.2f}" y1="{ny - dy:.2f}" '
f'x2="{mx + dx:.2f}" y2="{my - dy:.2f}"/>'
)
svg.append(
f' <line x1="{nx - dx:.2f}" y1="{ny + dy:.2f}" '
f'x2="{mx - dx:.2f}" y2="{my + dy:.2f}" stroke="{broken}" stroke-width="{wide_width:.2f}"/>'
)
elif p_order == 4:
ar_bond_colors[n][m] = ar_bond_colors[m][n] = formed
dx, dy = rv(double_space)
svg.append(
f' <line x1="{nx + dx:.2f}" y1="{ny - dy:.2f}" '
f'x2="{mx + dx:.2f}" y2="{my - dy:.2f}"/>'
)
svg.append(
f' <line x1="{nx - dx:.2f}" y1="{ny + dy:.2f}" '
f'x2="{mx - dx:.2f}" y2="{my + dy:.2f}" stroke="{broken}" stroke-width="{wide_width:.2f}"/>'
)
elif p_order == 3:
dx, dy = rv(triple_space)
svg.append(
f' <line x1="{nx + dx:.2f}" y1="{ny - dy:.2f}" '
f'x2="{mx + dx:.2f}" y2="{my - dy:.2f}"/>'
)
svg.append(
f' <line x1="{nx:.2f}" y1="{ny:.2f}" x2="{mx:.2f}" y2="{my:.2f}"/>'
)
svg.append(
f' <line x1="{nx - dx:.2f}" y1="{ny + dy:.2f}" '
f'x2="{mx - dx:.2f}" y2="{my + dy:.2f}" stroke="{formed} stroke-width="{wide_width:.2f}""/>'
)
elif p_order is None:
dx, dy = rv(double_space)
svg.append(
f' <line x1="{nx + dx:.2f}" y1="{ny - dy:.2f}" '
f'x2="{mx + dx:.2f}" y2="{my - dy:.2f}" stroke="{broken}" stroke-width="{wide_width:.2f}"/>'
)
svg.append(
f' <line x1="{nx - dx:.2f}" y1="{ny + dy:.2f}" '
f'x2="{mx - dx:.2f}" y2="{my + dy:.2f}" stroke="{broken}" stroke-width="{wide_width:.2f}"/>'
)
else:
dx, dy = rv(triple_space)
svg.append(
f' <line x1="{nx + dx:.2f}" y1="{ny - dy:.2f}" x2="{mx + dx:.2f}"'
f' y2="{my - dy:.2f}" stroke-dasharray="{dash1:.2f} {dash2:.2f}" stroke="{formed}" stroke-width="{wide_width:.2f}"/>'
)
svg.append(
f' <line x1="{nx:.2f}" y1="{ny:.2f}" x2="{mx:.2f}" y2="{my:.2f}"'
f' stroke="{broken}" stroke-width="{wide_width:.2f}"/>'
)
svg.append(
f' <line x1="{nx - dx:.2f}" y1="{ny + dy:.2f}" '
f'x2="{mx - dx:.2f}" y2="{my + dy:.2f}" stroke="{broken}" stroke-width="{wide_width:.2f}"/>'
)
elif order == 3:
if p_order == 3:
dx, dy = rv(triple_space)
svg.append(
f' <line x1="{nx + dx:.2f}" y1="{ny - dy:.2f}" '
f'x2="{mx + dx:.2f}" y2="{my - dy:.2f}"/>'
)
svg.append(
f' <line x1="{nx:.2f}" y1="{ny:.2f}" x2="{mx:.2f}" y2="{my:.2f}"/>'
)
svg.append(
f' <line x1="{nx - dx:.2f}" y1="{ny + dy:.2f}" '
f'x2="{mx - dx:.2f}" y2="{my + dy:.2f}"/>'
)
elif p_order == 1:
dx, dy = rv(triple_space)
svg.append(
f' <line x1="{nx + dx:.2f}" y1="{ny - dy:.2f}" '
f'x2="{mx + dx:.2f}" y2="{my - dy:.2f}"/>'
)
svg.append(
f' <line x1="{nx:.2f}" y1="{ny:.2f}" x2="{mx:.2f}" y2="{my:.2f}"'
f' stroke="{broken}" stroke-width="{wide_width:.2f}"/>'
)
svg.append(
f' <line x1="{nx - dx:.2f}" y1="{ny + dy:.2f}" '
f'x2="{mx - dx:.2f}" y2="{my + dy:.2f}" '
f'stroke="{broken}" stroke-width="{wide_width:.2f}"/>'
)
elif p_order == 4:
ar_bond_colors[n][m] = ar_bond_colors[m][n] = formed
dx, dy = rv(triple_space)
svg.append(
f' <line x1="{nx + dx:.2f}" y1="{ny - dy:.2f}" x2="{mx + dx:.2f}" '
f'y2="{my - dy:.2f}" stroke="{broken}" stroke-width="{wide_width:.2f}"/>'
)
svg.append(
f' <line x1="{nx:.2f}" y1="{ny:.2f}" x2="{mx:.2f}" y2="{my:.2f}"/>'
)
svg.append(
f' <line x1="{nx - dx:.2f}" y1="{ny + dy:.2f}" x2="{mx - dx:.2f}" '
f'y2="{my + dy:.2f}" stroke="{broken}" stroke-width="{wide_width:.2f}"/>'
)
elif p_order == 2:
dx, dy = rv(triple_space)
svg.append(
f' <line x1="{nx + dx:.2f}" y1="{ny - dy:.2f}" '
f'x2="{mx + dx:.2f}" y2="{my - dy:.2f}"/>'
)
svg.append(
f' <line x1="{nx:.2f}" y1="{ny:.2f}" x2="{mx:.2f}" y2="{my:.2f}"/>'
)
svg.append(
f' <line x1="{nx - dx:.2f}" y1="{ny + dy:.2f}" '
f'x2="{mx - dx:.2f}" y2="{my + dy:.2f}" stroke="{broken}" stroke-width="{wide_width:.2f}"/>'
)
elif p_order is None:
dx, dy = rv(triple_space)
svg.append(
f' <line x1="{nx + dx:.2f}" y1="{ny - dy:.2f}" '
f'x2="{mx + dx:.2f}" y2="{my - dy:.2f}" stroke="{broken}" stroke-width="{wide_width:.2f}"/>'
)
svg.append(
f' <line x1="{nx:.2f}" y1="{ny:.2f}" '
f'x2="{mx:.2f}" y2="{my:.2f}" stroke="{broken}" stroke-width="{wide_width:.2f}"/>'
)
svg.append(
f' <line x1="{nx - dx:.2f}" y1="{ny + dy:.2f}" '
f'x2="{mx - dx:.2f}" y2="{my + dy:.2f}" stroke="{broken}" stroke-width="{wide_width:.2f}"/>'
)
else:
dx, dy = rv(double_space)
dx3 = 3 * dx
dy3 = 3 * dy
svg.append(
f' <line x1="{nx + dx3:.2f}" y1="{ny - dy3:.2f}" x2="{mx + dx3:.2f}" '
f'y2="{my - dy3:.2f}" stroke-dasharray="{dash1:.2f} {dash2:.2f}" stroke="{formed}" stroke-width="{wide_width:.2f}"/>'
)
svg.append(
f' <line x1="{nx + dx:.2f}" y1="{ny - dy:.2f}" '
f'x2="{mx + dx:.2f}" y2="{my - dy:.2f}" stroke="{broken}" stroke-width="{wide_width:.2f}"/>'
)
svg.append(
f' <line x1="{nx - dx:.2f}" y1="{ny + dy:.2f}" '
f'x2="{mx - dx:.2f}" y2="{my + dy:.2f}" stroke="{broken}" stroke-width="{wide_width:.2f}"/>'
)
svg.append(
f' <line x1="{nx - dx3:.2f}" y1="{ny + dy3:.2f}" x2="{mx - dx3:.2f}" '
f'y2="{my + dy3:.2f}" stroke="{broken}" stroke-width="{wide_width:.2f}"/>'
)
elif order is None:
if p_order == 1:
svg.append(
f' <line x1="{nx:.2f}" y1="{ny:.2f}" x2="{mx:.2f}" y2="{my:.2f}"'
f' stroke="{formed}" stroke-width="{wide_width:.2f}"/>'
)
elif p_order == 4:
ar_bond_colors[n][m] = ar_bond_colors[m][n] = formed
svg.append(
f' <line x1="{nx:.2f}" y1="{ny:.2f}" x2="{mx:.2f}" y2="{my:.2f}"'
f' stroke="{formed}" stroke-width="{wide_width:.2f}"/>'
)
elif p_order == 2:
dx, dy = rv(double_space)
# dx = dx // 1.4
# dy = dy // 1.4
svg.append(
f' <line x1="{nx + dx:.2f}" y1="{ny - dy:.2f}" x2="{mx + dx:.2f}" '
f'y2="{my - dy:.2f}" stroke="{formed}" stroke-width="{wide_width:.2f}"/>'
)
svg.append(
f' <line x1="{nx - dx:.2f}" y1="{ny + dy:.2f}" x2="{mx - dx:.2f}" '
f'y2="{my + dy:.2f}" stroke="{formed}" stroke-width="{wide_width:.2f}"/>'
)
elif p_order == 3:
dx, dy = rv(triple_space)
svg.append(
f' <line x1="{nx + dx:.2f}" y1="{ny - dy:.2f}" '
f'x2="{mx + dx:.2f}" y2="{my - dy:.2f}" stroke="{formed}" stroke-width="{wide_width:.2f}"/>'
)
svg.append(
f' <line x1="{nx:.2f}" y1="{ny:.2f}" x2="{mx:.2f}" y2="{my:.2f}"'
f' stroke="{formed}" stroke-width="{wide_width:.2f}"/>'
)
svg.append(
f' <line x1="{nx - dx:.2f}" y1="{ny + dy:.2f}" '
f'x2="{mx - dx:.2f}" y2="{my + dy:.2f}" stroke="{formed}" stroke-width="{wide_width:.2f}"/>'
)
else:
svg.append(
f' <line x1="{nx:.2f}" y1="{ny:.2f}" x2="{mx:.2f}" y2="{my:.2f}" '
f'stroke-dasharray="{dash1:.2f} {dash2:.2f}" stroke="{formed}" stroke-width="{wide_width:.2f}"/>'
)
else:
if p_order == 8:
svg.append(
f' <line x1="{nx:.2f}" y1="{ny:.2f}" x2="{mx:.2f}" y2="{my:.2f}" '
f'stroke-dasharray="{dash1:.2f} {dash2:.2f}"/>'
)
elif p_order == 1:
dx, dy = rv(double_space)
svg.append(
f' <line x1="{nx + dx:.2f}" y1="{ny - dy:.2f}" x2="{mx + dx:.2f}"'
f' y2="{my - dy:.2f}" stroke-dasharray="{dash1:.2f} {dash2:.2f}" stroke="{broken}" stroke-width="{wide_width:.2f}"/>'
)
svg.append(
f' <line x1="{nx - dx:.2f}" y1="{ny + dy:.2f}" '
f'x2="{mx - dx:.2f}" y2="{my + dy:.2f}" stroke="{formed}" stroke-width="{wide_width:.2f}"/>'
)
elif p_order == 4:
ar_bond_colors[n][m] = ar_bond_colors[m][n] = None
svg.append(
f' <line x1="{nx:.2f}" y1="{ny:.2f}" x2="{mx:.2f}" y2="{my:.2f}"'
f' stroke="{formed}" stroke-width="{wide_width:.2f}"/>'
)
elif p_order == 2:
dx, dy = rv(triple_space)
svg.append(
f' <line x1="{nx + dx:.2f}" y1="{ny - dy:.2f}" x2="{mx + dx:.2f}"'
f' y2="{my - dy:.2f}" stroke-dasharray="{dash1:.2f} {dash2:.2f}" stroke="{broken}" stroke-width="{wide_width:.2f}"/>'
)
svg.append(
f' <line x1="{nx:.2f}" y1="{ny:.2f}" x2="{mx:.2f}" y2="{my:.2f}"'
f' stroke="{formed}" stroke-width="{wide_width:.2f}"/>'
)
svg.append(
f' <line x1="{nx - dx:.2f}" y1="{ny + dy:.2f}" '
f'x2="{mx - dx:.2f}" y2="{my + dy:.2f}" stroke="{formed}" stroke-width="{wide_width:.2f}"/>'
)
elif p_order == 3:
dx, dy = rv(double_space)
dx3 = 3 * dx
dy3 = 3 * dy
svg.append(
f' <line x1="{nx + dx3:.2f}" y1="{ny - dy3:.2f}" x2="{mx + dx3:.2f}" '
f'y2="{my - dy3:.2f}" stroke-dasharray="{dash1:.2f} {dash2:.2f}" stroke="{broken}" stroke-width="{wide_width:.2f}"/>'
)
svg.append(
f' <line x1="{nx + dx:.2f}" y1="{ny - dy:.2f}" '
f'x2="{mx + dx:.2f}" y2="{my - dy:.2f}" stroke="{formed}" stroke-width="{wide_width:.2f}"/>'
)
svg.append(
f' <line x1="{nx - dx:.2f}" y1="{ny + dy:.2f}" '
f'x2="{mx - dx:.2f}" y2="{my + dy:.2f}" stroke="{formed}" stroke-width="{wide_width:.2f}"/>'
)
svg.append(
f' <line x1="{nx - dx3:.2f}" y1="{ny + dy3:.2f}" '
f'x2="{mx - dx3:.2f}" y2="{my + dy3:.2f}" stroke="{formed}" stroke-width="{wide_width:.2f}"/>'
)
else:
svg.append(
f' <line x1="{nx:.2f}" y1="{ny:.2f}" x2="{mx:.2f}" y2="{my:.2f}" '
f'stroke-dasharray="{dash1:.2f} {dash2:.2f}" stroke="{broken}" stroke-width="{wide_width:.2f}"/>'
)
# aromatic rings - unchanged
for ring in self.aromatic_rings:
cx = sum(plane[x][0] for x in ring) / len(ring)
cy = sum(plane[x][1] for x in ring) / len(ring)
for n, m in zip(ring, ring[1:]):
nx, ny = plane[n]
mx, my = plane[m]
aromatic = self.__render_aromatic_bond(
nx, ny, mx, my, cx, cy, ar_bond_colors[n].get(m)
)
if aromatic:
svg.append(aromatic)
n, m = ring[-1], ring[0]
nx, ny = plane[n]
mx, my = plane[m]
aromatic = self.__render_aromatic_bond(
nx, ny, mx, my, cx, cy, ar_bond_colors[n].get(m)
)
if aromatic:
svg.append(aromatic)
return svg
def __render_aromatic_bond(self, n_x, n_y, m_x, m_y, c_x, c_y, color):
config = self._render_config
dash1, dash2 = config["dashes"]
dash3, dash4 = config["aromatic_dashes"]
aromatic_space = config["cgr_aromatic_space"]
normal_width = config.get("bond_width", 0.02)
wide_width = normal_width * 2
# n aligned xy
mn_x, mn_y, cn_x, cn_y = m_x - n_x, m_y - n_y, c_x - n_x, c_y - n_y
# nm reoriented xy
mr_x, mr_y = hypot(mn_x, mn_y), 0
cr_x, cr_y = rotate_vector(cn_x, cn_y, mn_x, -mn_y)
if cr_y and aromatic_space / cr_y < 0.65:
if cr_y > 0:
r_y = aromatic_space
else:
r_y = -aromatic_space
cr_y = -cr_y
ar_x = aromatic_space * cr_x / cr_y
br_x = mr_x - aromatic_space * (mr_x - cr_x) / cr_y
# backward reorienting
an_x, an_y = rotate_vector(ar_x, r_y, mn_x, mn_y)
bn_x, bn_y = rotate_vector(br_x, r_y, mn_x, mn_y)
if color:
# print('color')
return (
f' <line x1="{an_x + n_x:.2f}" y1="{-an_y - n_y:.2f}" x2="{bn_x + n_x:.2f}" '
f'y2="{-bn_y - n_y:.2f}" stroke-dasharray="{dash3:.2f} {dash4:.2f}" stroke="{color}" stroke-width="{wide_width:.2f}"/>'
)
elif color is None:
dash3, dash4 = dash1, dash2
return (
f' <line x1="{an_x + n_x:.2f}" y1="{-an_y - n_y:.2f}"'
f' x2="{bn_x + n_x:.2f}" y2="{-bn_y - n_y:.2f}" stroke-dasharray="{dash3:.2f} {dash4:.2f}"/>'
)
def cgr_display(cgr: CGRContainer) -> str:
"""
Generates an SVG string for displaying a CGR with wider DynamicBonds.
This function temporarily modifies the rendering methods of the
`CGRContainer` class to use the bond rendering logic from
`WideBondDepictCGR`, which draws DynamicBonds with a wider stroke.
It cleans the 2D coordinates of the input CGR and then calls its
`depict()` method to generate the SVG string using the modified
rendering behavior.
Args:
cgr (CGRContainer): The CGRContainer object to be depicted.
Returns:
str: An SVG string representing the depiction of the CGR
with wider DynamicBonds.
"""
CGRContainer._CGRContainer__render_aromatic_bond = (
WideBondDepictCGR._WideBondDepictCGR__render_aromatic_bond
)
CGRContainer._render_bonds = WideBondDepictCGR._render_bonds
CGRContainer._WideBondDepictCGR__render_aromatic_bond = (
WideBondDepictCGR._WideBondDepictCGR__render_aromatic_bond
)
cgr.clean2d()
return cgr.depict()
class CustomDepictMolecule(DepictMolecule):
"""
Custom molecule depiction class that uses atom.symbol for rendering.
"""
def _render_atoms(self):
bonds = self._bonds
plane = self._plane
charges = self._charges
radicals = self._radicals
hydrogens = self._hydrogens
config = self._render_config
carbon = config["carbon"]
mapping = config["mapping"]
span_size = config["span_size"]
font_size = config["font_size"]
monochrome = config["monochrome"]
other_size = config["other_size"]
atoms_colors = config["atoms_colors"]
mapping_font = config["mapping_size"]
dx_m, dy_m = config["dx_m"], config["dy_m"]
dx_ci, dy_ci = config["dx_ci"], config["dy_ci"]
symbols_font_style = config["symbols_font_style"]
# for cumulenes
try:
# Check if _cumulenes method exists and handle potential errors
cumulenes = {
y
for x in self._cumulenes(heteroatoms=True)
if len(x) > 2
for y in x[1:-1]
}
except AttributeError:
cumulenes = set() # Fallback if _cumulenes is not available or fails
if monochrome:
map_fill = other_fill = "black"
else:
map_fill = config["mapping_color"]
other_fill = config["other_color"]
svg = []
maps = []
others = []
font2 = 0.2 * font_size
font3 = 0.3 * font_size
font4 = 0.4 * font_size
font5 = 0.5 * font_size
font6 = 0.6 * font_size
font7 = 0.7 * font_size
font15 = 0.15 * font_size
font25 = 0.25 * font_size
mask = defaultdict(list)
for n, atom in self._atoms.items():
x, y = plane[n]
y = -y
# --- KEY CHANGE HERE ---
# Use atom.symbol if it exists, otherwise fallback to atomic_symbol
try:
symbol = atom.symbol
except AttributeError:
symbol = atom.atomic_symbol # Fallback if .symbol doesn't exist
# --- END KEY CHANGE ---
if (
not bonds.get(n)
or symbol != "C"
or carbon
or atom.charge
or atom.is_radical
or atom.isotope
or n in cumulenes
): # Added bonds.get(n) check for single atoms
# Calculate hydrogens if the attribute exists, otherwise default to 0
try:
h = hydrogens[n]
except (KeyError, AttributeError):
h = 0 # Default if _hydrogens is missing or key n is not present
if h == 1:
h_str = "H"
span = ""
elif h and h > 1: # Check if h is not None and greater than 1
span = f'<tspan dy="{config["span_dy"]:.2f}" font-size="{span_size:.2f}">{h}</tspan>'
h_str = "H"
else:
h_str = ""
span = ""
# Handle charges and radicals safely
charge_val = charges.get(n, 0)
is_radical = radicals.get(n, False)
if charge_val:
t = f'{_render_charge.get(charge_val, "")}{"↑" if is_radical else ""}' # Use .get for safety
if t: # Only add if charge text is generated
others.append(
f' <text x="{x:.2f}" y="{y:.2f}" dx="{dx_ci:.2f}" dy="-{dy_ci:.2f}">'
f"{t}</text>"
)
mask["other"].append(
f' <text x="{x:.2f}" y="{y:.2f}" dx="{dx_ci:.2f}" dy="-{dy_ci:.2f}">'
f"{t}</text>"
)
elif is_radical:
others.append(
f' <text x="{x:.2f}" y="{y:.2f}" dx="{dx_ci:.2f}" dy="-{dy_ci:.2f}">↑</text>'
)
mask["other"].append(
f' <text x="{x:.2f}" y="{y:.2f}" dx="{dx_ci:.2f}"'
f' dy="-{dy_ci:.2f}">↑</text>'
)
# Handle isotope safely
try:
iso = atom.isotope
if iso:
t = iso
others.append(
f' <text x="{x:.2f}" y="{y:.2f}" dx="-{dx_ci:.2f}" dy="-{dy_ci:.2f}" '
f'text-anchor="end">{t}</text>'
)
mask["other"].append(
f' <text x="{x:.2f}" y="{y:.2f}" dx="-{dx_ci:.2f}"'
f' dy="-{dy_ci:.2f}" text-anchor="end">{t}</text>'
)
except AttributeError:
pass # Atom might not have isotope attribute
# Determine atom color based on atomic_number, default to black if monochrome or not found
atom_color = "black"
if not monochrome:
try:
an = atom.atomic_number
if 0 < an <= len(atoms_colors):
atom_color = atoms_colors[an - 1]
else:
atom_color = atoms_colors[
5
] # Default to Carbon color if out of range
except AttributeError:
atom_color = atoms_colors[
5
] # Default to Carbon color if no atomic_number
svg.append(
f' <g fill="{atom_color}" '
f'font-family="{symbols_font_style }">'
)
# Adjust dx based on symbol length for better centering
if len(symbol) > 1:
dx = font7
dx_mm = dx_m + font5
if symbol[-1].lower() in (
"l",
"i",
"r",
"t",
): # Heuristic for narrow last letters
rx = font6
ax = font25
else:
rx = font7
ax = font15
mask["center"].append(
f' <ellipse cx="{x - ax:.2f}" cy="{y:.2f}" rx="{rx}" ry="{font4}"/>'
)
else:
if symbol == "I": # Special case for 'I'
dx = font15
dx_mm = dx_m
else: # Single character
dx = font4
dx_mm = dx_m + font2
mask["center"].append(
f' <circle cx="{x:.2f}" cy="{y:.2f}" r="{font4:.2f}"/>'
)
svg.append(
f' <text x="{x:.2f}" y="{y:.2f}" dx="-{dx:.2f}" dy="{font4:.2f}" '
f'font-size="{font_size:.2f}">{symbol}{h_str}{span}</text>'
)
mask["symbols"].append(
f' <text x="{x:.2f}" y="{y:.2f}" dx="-{dx:.2f}" '
f'dy="{font4:.2f}">{symbol}{h_str}</text>'
)
if span:
mask["span"].append(
f' <text x="{x:.2f}" y="{y:.2f}" dx="-{dx:.2f}" dy="{font4:.2f}">'
f"{symbol}{h_str}{span}</text>"
)
svg.append(" </g>")
if mapping:
maps.append(
f' <text x="{x:.2f}" y="{y:.2f}" dx="-{dx_mm:.2f}" dy="{dy_m + font3:.2f}" '
f'text-anchor="end">{n}</text>'
)
mask["aam"].append(
f' <text x="{x:.2f}" y="{y:.2f}" dx="-{dx_mm:.2f}" '
f'dy="{dy_m + font3:.2f}" text-anchor="end">{n}</text>'
)
elif mapping:
# Determine dx_mm for mapping based on symbol length even if atom itself isn't drawn
if len(symbol) > 1:
dx_mm = dx_m + font5
else:
dx_mm = dx_m + font2 if symbol != "I" else dx_m
maps.append(
f' <text x="{x:.2f}" y="{y:.2f}" dx="-{dx_mm:.2f}" dy="{dy_m:.2f}" '
f'text-anchor="end">{n}</text>'
)
mask["aam"].append(
f' <text x="{x:.2f}" y="{y:.2f}" dx="-{dx_mm:.2f}" dy="{dy_m:.2f}" '
f'text-anchor="end">{n}</text>'
)
if others:
svg.append(
f' <g font-family="{config["other_font_style"]}" fill="{other_fill}" '
f'font-size="{other_size:.2f}">'
)
svg.extend(others)
svg.append(" </g>")
if mapping:
svg.append(f' <g fill="{map_fill}" font-size="{mapping_font:.2f}">')
svg.extend(maps)
svg.append(" </g>")
return svg, mask
def depict_custom_reaction(reaction: ReactionContainer):
"""
Depicts a ReactionContainer using custom atom rendering logic (replace At to X).
This function generates an SVG string representing a reaction. It
temporarily modifies the classes of the molecules within the reaction
to use a custom depiction logic (`CustomDepictMolecule`) that alters
how atoms are rendered (specifically, it seems to use `atom.symbol`
instead of `atom.atomic_symbol`, potentially for replacing 'At' with 'X'
as mentioned in the original comment). After depicting each molecule
with the temporary class, it restores the original classes. The function
then combines the individual molecule depictions, reaction arrow, and
reaction signs into a single SVG.
Args:
reaction (ReactionContainer): The ReactionContainer object to be depicted.
Returns:
str: An SVG string representing the depiction of the reaction
with custom atom rendering.
"""
if not reaction._arrow:
reaction.fix_positions() # Ensure positions are calculated
r_atoms = []
r_bonds = []
r_masks = []
r_max_x = r_max_y = r_min_y = 0
original_classes = {} # Store original classes to restore later
try:
# Temporarily change the class of molecules to use the custom depiction
for mol in reaction.molecules():
if isinstance(mol, (MoleculeContainer, CGRContainer)):
original_classes[mol] = mol.__class__
custom_class_name = (
f"TempCustom_{mol.__class__.__name__}_{uuid4().hex}" # Unique name
)
# Combine custom depiction with original class methods
# Ensure the custom _render_atoms takes precedence
new_bases = (CustomDepictMolecule,) + original_classes[mol].__bases__
# Filter out DepictMolecule if it's already a base to avoid MRO issues
new_bases = tuple(b for b in new_bases if b is not DepictMolecule)
# If DepictMolecule wasn't a direct base, ensure its methods are accessible
if CustomDepictMolecule not in original_classes[mol].__mro__:
# Prioritize CustomDepictMolecule's methods
new_bases = (CustomDepictMolecule, original_classes[mol])
else:
# If DepictMolecule was a base, CustomDepictMolecule is already first
new_bases = (CustomDepictMolecule,) + tuple(
b
for b in original_classes[mol].__bases__
if b is not DepictMolecule
)
# Create the temporary class
mol.__class__ = type(custom_class_name, new_bases, {})
# Depict using the (potentially) modified class
atoms, bonds, masks, min_x, min_y, max_x, max_y = mol.depict(embedding=True)
r_atoms.append(atoms)
r_bonds.append(bonds)
r_masks.append(masks)
if max_x > r_max_x:
r_max_x = max_x
if max_y > r_max_y:
r_max_y = max_y
if min_y < r_min_y:
r_min_y = min_y
finally:
# Restore original classes
for mol, original_class in original_classes.items():
mol.__class__ = original_class
config = DepictMolecule._render_config # Access via the imported class
font_size = config["font_size"]
font125 = 1.25 * font_size
width = r_max_x + 3.0 * font_size
height = r_max_y - r_min_y + 2.5 * font_size
viewbox_x = -font125
viewbox_y = -r_max_y - font125
svg = [
f'<svg width="{width:.2f}cm" height="{height:.2f}cm" '
f'viewBox="{viewbox_x:.2f} {viewbox_y:.2f} {width:.2f} '
f'{height:.2f}" xmlns="http://www.w3.org/2000/svg" version="1.1">\n'
' <defs>\n <marker id="arrow" markerWidth="10" markerHeight="10" '
'refX="0" refY="3" orient="auto">\n <path d="M0,0 L0,6 L9,3"/>\n </marker>\n </defs>\n'
f' <line x1="{reaction._arrow[0]:.2f}" y1="0" x2="{reaction._arrow[1]:.2f}" y2="0" '
'fill="none" stroke="black" stroke-width=".04" marker-end="url(#arrow)"/>'
]
sings_plus = reaction._signs
if sings_plus:
svg.append(f' <g fill="none" stroke="black" stroke-width=".04">')
for x in sings_plus:
svg.append(
f' <line x1="{x + .35:.2f}" y1="0" x2="{x + .65:.2f}" y2="0"/>'
)
svg.append(
f' <line x1="{x + .5:.2f}" y1="0.15" x2="{x + .5:.2f}" y2="-0.15"/>'
)
svg.append(" </g>")
for atoms, bonds, masks in zip(r_atoms, r_bonds, r_masks):
# Use the static method from Depict directly
svg.extend(
Depict._graph_svg(atoms, bonds, masks, viewbox_x, viewbox_y, width, height)
)
svg.append("</svg>")
return "\n".join(svg)
def remove_and_shift(nested_dict, to_remove): # Under development
"""
Removes specified inner keys from a nested dictionary and renumbers the remaining keys.
Given a dictionary where values are themselves dictionaries, this function
iterates through each inner dictionary. For each inner dictionary, it
creates a new dictionary containing only the key-value pairs where the
inner key is NOT present in the `to_remove` list. The keys of the remaining
elements in the new inner dictionary are then renumbered sequentially
starting from 0, effectively removing gaps left by the removed keys.
Args:
nested_dict (dict): The input nested dictionary (dict of dicts).
to_remove (list): A list of keys to remove from the inner dictionaries.
Returns:
dict: A new nested dictionary with the specified keys removed from
inner dictionaries and the remaining inner keys renumbered.
"""
rem_set = set(to_remove)
result = {}
for outer_k, inner in nested_dict.items():
new_inner = {}
for old_k, v in inner.items():
if old_k in rem_set:
continue
shift = sum(1 for r in rem_set if r < old_k)
new_k = old_k - shift
new_inner[new_k] = v
result[outer_k] = new_inner
return result
|