| 1 | class AtomPair(): |
| 2 | def __init__(self, atom1, atom2): |
| 3 | self.atom1 = atom1 |
| 4 | self.atom2 = atom2 |
| 5 | |
| 6 | def __repr__(self): |
| 7 | if self.atom1 and self.atom2: |
| 8 | return f'{self.atom1} {self.atom2}' |
| 9 | else: |
| 10 | return '' |
| 11 | |
| 12 | def __len__(self): |
| 13 | if self.atom1 and self.atom2: |
| 14 | return 2 |
| 15 | elif not self.atom1 or not self.atom2: |
| 16 | return 0 |
| 17 | |
| 18 | |
| 19 | class Bond: |
| 20 | """A covalent bond between two atoms with a measured distance. |
| 21 | |
| 22 | Attributes |
| 23 | ---------- |
| 24 | atom1, atom2 : :class:`~shelxfile.atoms.atom.Atom` |
| 25 | The bonded atom pair (atom1 always has the lower ``all_atoms`` index). |
| 26 | distance : float |
| 27 | Interatomic distance in Å. |
| 28 | """ |
| 29 | |
| 30 | def __init__(self, atom1, atom2, distance: float) -> None: |
| 31 | self.atom1 = atom1 |
| 32 | self.atom2 = atom2 |
| 33 | self.distance = distance |
| 34 | |
| 35 | def __iter__(self): |
| 36 | """Allows unpacking: ``a1, a2, dist = bond``.""" |
| 37 | yield self.atom1 |
| 38 | yield self.atom2 |
| 39 | yield self.distance |
| 40 | |
| 41 | def __repr__(self) -> str: |
| 42 | return (f"Bond({self.atom1.fullname_short}, " |
| 43 | f"{self.atom2.fullname_short}, " |
| 44 | f"{self.distance:.4f} Å)") |
| 45 | |
| 46 | def __str__(self) -> str: |
| 47 | return (f"{self.atom1.fullname_short:<6s}" |
| 48 | f" – " |
| 49 | f"{self.atom2.fullname_short:<6s}" |
| 50 | f" {self.distance:.4f} Å") |
| 51 | |
| 52 | |
| 53 | class SymBond(Bond): |
| 54 | """A covalent bond that may involve a symmetry-generated neighbor. |
| 55 | |
| 56 | Extends :class:`Bond` with a *symm_label* that describes the symmetry |
| 57 | operation (and any lattice translation) needed to generate the neighbor |
| 58 | atom from its position in the asymmetric unit. An empty label means |
| 59 | both atoms are in the asymmetric unit with no additional translation. |
| 60 | |
| 61 | The label uses SHELXL / CIF convention, e.g. ``'-x, y+1/2, -z+1/2'``. |
| 62 | |
| 63 | Attributes |
| 64 | ---------- |
| 65 | atom1 : :class:`~shelxfile.atoms.atom.Atom` |
| 66 | The asymmetric-unit atom that "owns" the bond (the central atom). |
| 67 | atom2 : :class:`~shelxfile.atoms.atom.Atom` |
| 68 | The neighbor in the asymmetric unit (its image is generated via |
| 69 | *symm_label* to obtain the actual bonded position). |
| 70 | distance : float |
| 71 | Interatomic distance in Å. |
| 72 | symm_label : str |
| 73 | Symmetry operation string, e.g. ``'-x, y+1/2, -z+1/2'``, or an |
| 74 | empty string for plain asymmetric-unit bonds. |
| 75 | symm_number : int |
| 76 | 0-based index of the symmetry operation in ``Shelxfile.symmcards`` |
| 77 | (0 = identity). Always 0 for plain asymmetric-unit bonds. |
| 78 | """ |
| 79 | |
| 80 | def __init__(self, atom1, atom2, distance: float, |
| 81 | symm_label: str = '', symm_number: int = 0) -> None: |
| 82 | super().__init__(atom1, atom2, distance) |
| 83 | self.symm_label = symm_label |
| 84 | self.symm_number = symm_number |
| 85 | |
| 86 | @property |
| 87 | def is_symmetry_bond(self) -> bool: |
| 88 | """``True`` when the bond involves a symmetry-generated image.""" |
| 89 | return bool(self.symm_label) |
| 90 | |
| 91 | def __iter__(self): |
| 92 | """Allows unpacking: ``a1, a2, dist, label = bond``.""" |
| 93 | yield self.atom1 |
| 94 | yield self.atom2 |
| 95 | yield self.distance |
| 96 | yield self.symm_label |
| 97 | |
| 98 | def __repr__(self) -> str: |
| 99 | if self.symm_label: |
| 100 | return (f"SymBond({self.atom1.fullname_short}, " |
| 101 | f"{self.atom2.fullname_short}, " |
| 102 | f"{self.distance:.4f} Å, " |
| 103 | f"#{self.symm_number} [{self.symm_label}])") |
| 104 | return (f"SymBond({self.atom1.fullname_short}, " |
| 105 | f"{self.atom2.fullname_short}, " |
| 106 | f"{self.distance:.4f} Å)") |
| 107 | |
| 108 | def __str__(self) -> str: |
| 109 | suffix = '' |
| 110 | if self.symm_label: |
| 111 | suffix = f' #{self.symm_number} [{self.symm_label}]' |
| 112 | return (f"{self.atom1.fullname_short:<6s}" |
| 113 | f" – " |
| 114 | f"{self.atom2.fullname_short:<6s}" |
| 115 | f" {self.distance:.4f} Å" |
| 116 | f"{suffix}") |