| 1 |
import datetime |
| 2 |
from pathlib import Path |
| 3 |
from string import Template |
| 4 |
from typing import TYPE_CHECKING, Dict |
| 5 |
from shelxfile.version import VERSION |
| 6 |
from shelxfile.atoms.atoms import Atoms |
| 7 |
|
| 8 |
if TYPE_CHECKING: |
| 9 |
from shelxfile.shelx.shelxfile import Shelxfile |
| 10 |
|
| 11 |
|
| 12 |
class CifFile(): |
| 13 |
"""Class for writing IUCr CIF-1.1 of DDL1 compliant Version 2.4.5 files.""" |
| 14 |
|
| 15 |
def __init__(self, shelx_file: 'Shelxfile', template: str = None): |
| 16 |
if template is not None: |
| 17 |
self.template = template |
| 18 |
else: |
| 19 |
self.template = Path(__file__).parent / "cif_template.tmpl" |
| 20 |
self.data = shelx_file |
| 21 |
self._cif = None |
| 22 |
|
| 23 |
def __str__(self): |
| 24 |
return self._cif |
| 25 |
|
| 26 |
def __repr__(self): |
| 27 |
return self._cif |
| 28 |
|
| 29 |
def write_cif(self, cif_path: Path) -> None: |
| 30 |
with open(self.template, "r") as f: |
| 31 |
template = f.read() |
| 32 |
cif = Template(template) |
| 33 |
sub = cif.substitute(self._cif_dict()) |
| 34 |
cif_path.write_text(sub) |
| 35 |
|
| 36 |
def _cif_dict(self) -> Dict[str, str]: |
| 37 |
cif_dict = {} |
| 38 |
cif_dict["data_name"] = self.data.titl.split()[0].lower() or "unknown" |
| 39 |
cif_dict["version"] = VERSION |
| 40 |
cif_dict["creation_date"] = f"{datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')}" |
| 41 |
cif_dict["sum_formula"] = self.data.sum_formula |
| 42 |
cif_dict["formula_weight"] = self.data.formula_weight |
| 43 |
cif_dict.update(self._cell_data()) |
| 44 |
cif_dict.update(self._symmetry_data()) |
| 45 |
cif_dict.update(self._atoms_data()) |
| 46 |
cif_dict.update(self._adp_data()) |
| 47 |
cif_dict.update(self._misc_dict()) |
| 48 |
return cif_dict |
| 49 |
|
| 50 |
def _cell_data(self) -> Dict[str, float]: |
| 51 |
cell = self.data.cell |
| 52 |
return { |
| 53 |
"cell_a" : cell.a, |
| 54 |
"cell_b" : cell.b, |
| 55 |
"cell_c" : cell.c, |
| 56 |
"cell_alpha" : cell.alpha, |
| 57 |
"cell_beta" : cell.beta, |
| 58 |
"cell_gamma" : cell.gamma, |
| 59 |
"cell_volume": round(cell.volume, 4), |
| 60 |
"cell_z" : self.data.zerr.Z, |
| 61 |
} |
| 62 |
|
| 63 |
def _symmetry_data(self) -> dict[str, str | None]: |
| 64 |
symmcards_ = [f" '{sym.to_cif()}'" for sym in self.data.symmcards] |
| 65 |
return { |
| 66 |
"space_group" : self.data.space_group, |
| 67 |
|
| 68 |
"symmetry_loop": '\n'.join(symmcards_), |
| 69 |
} |
| 70 |
|
| 71 |
def _atoms_data(self) -> Dict[str, str]: |
| 72 |
atoms: Atoms = self.data.atoms |
| 73 |
lines = [] |
| 74 |
loop_header = ("loop_\n" |
| 75 |
" _atom_site_label\n" |
| 76 |
" _atom_site_type_symbol\n" |
| 77 |
" _atom_site_fract_x\n" |
| 78 |
" _atom_site_fract_y\n" |
| 79 |
" _atom_site_fract_z\n" |
| 80 |
" _atom_site_adp_type\n" |
| 81 |
" _atom_site_occupancy\n" |
| 82 |
" _atom_site_disorder_group") |
| 83 |
for atom in atoms.all_atoms: |
| 84 |
if not atom.qpeak: |
| 85 |
lines.append(f"{atom.fullname_short} {atom.element} " |
| 86 |
f"{atom.x} {atom.y} {atom.z} " |
| 87 |
f"{'Uiso' if atom.is_isotropic else 'Uani'} " |
| 88 |
f"{atom.occupancy} {atom.part.n}") |
| 89 |
return { |
| 90 |
"atom_loop_header": loop_header, |
| 91 |
"atom_loop" : '\n'.join(lines), |
| 92 |
} |
| 93 |
|
| 94 |
def _adp_data(self) -> Dict[str, str]: |
| 95 |
atoms: Atoms = self.data.atoms |
| 96 |
lines = [] |
| 97 |
loop_header = ( |
| 98 |
"loop_\n" |
| 99 |
" _atom_site_aniso_label\n" |
| 100 |
" _atom_site_aniso_U_11\n" |
| 101 |
" _atom_site_aniso_U_22\n" |
| 102 |
" _atom_site_aniso_U_33\n" |
| 103 |
" _atom_site_aniso_U_23\n" |
| 104 |
" _atom_site_aniso_U_13\n" |
| 105 |
" _atom_site_aniso_U_12") |
| 106 |
for atom in atoms.all_atoms: |
| 107 |
if not atom.qpeak and not atom.is_isotropic: |
| 108 |
lines.append(f"{atom.fullname_short} " |
| 109 |
f"{atom.U11} {atom.U22} {atom.U33} " |
| 110 |
f"{atom.U23} {atom.U13} {atom.U12} ") |
| 111 |
return { |
| 112 |
"aniso_loop_header": loop_header, |
| 113 |
"aniso_loop" : '\n'.join(lines), |
| 114 |
} |
| 115 |
|
| 116 |
def _misc_dict(self) -> Dict[str, str]: |
| 117 |
misc_dict = {} |
| 118 |
misc_dict["temperature"] = round(self.data.temp_in_kelvin, 3) or '?' |
| 119 |
misc_dict["crystal_size_max"] = self.data.size.max or '?' |
| 120 |
misc_dict["crystal_size_mid"] = self.data.size.mid or '?' |
| 121 |
misc_dict["crystal_size_min"] = self.data.size.min or '?' |
| 122 |
misc_dict["wavelength"] = self.data.wavelength or '?' |
| 123 |
misc_dict["R1"] = self.data.R1 or '?' |
| 124 |
misc_dict["wR2"] = self.data.wr2 or '?' |
| 125 |
misc_dict["goodness_of_fit"] = self.data.goof or '?' |
| 126 |
return misc_dict |
| 127 |
|
| 128 |
|
| 129 |
if __name__ == '__main__': |
| 130 |
from shelxfile import Shelxfile |
| 131 |
|
| 132 |
shx = Shelxfile(debug=True) |
| 133 |
|
| 134 |
shx.read_file('./tests/resources/p21c.res') |
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
shx.to_cif('p21c-test.cif') |