Read / write¶
Structure and complex I/O helpers, including the STK-to-ASE bridge and
GULP/AMS writers: cage_isomer_builder.utils.read_write.
Structure conversion and writers: STK to ASE with bond orders, isomer files, AMS run scripts and host-guest complexes.
stk_2_ase_atoms ¶
Convert an STK molecule to ASE Atoms and its bond-order matrix.
The bond matrix keeps the bonds of the construction, which matters where
bonded atoms are further apart than a covalent-radius cutoff would allow
(e.g. before optimisation). STK dummy atoms (At) become X.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
stk_atoms
|
Molecule
|
|
required |
pbc
|
bool
|
Periodic boundary conditions of the returned Atoms. |
False
|
Returns:
| Name | Type | Description |
|---|---|---|
ase_atoms |
Atoms
|
|
bond_matrix |
(ndarray, shape(n_atoms, n_atoms))
|
Bond orders (0 for no bond). |
Examples:
>>> import stk
>>> from cage_isomer_builder.utils.read_write import stk_2_ase_atoms
>>> atoms, bonds = stk_2_ase_atoms(stk.BuildingBlock("NCCN"))
>>> atoms.get_chemical_formula(), int(bonds.sum() / 2) # 3 heavy-atom + 8 X-H bonds
('C2H8N2', 11)
Source code in cage_isomer_builder/utils/read_write.py
write_to_xyz ¶
Write ase_atoms to a plain XYZ file (symbols and positions only).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
filename
|
str
|
|
required |
ase_atoms
|
Atoms
|
|
required |
Examples:
>>> import os, tempfile
>>> from ase.build import molecule
>>> from cage_isomer_builder.utils.read_write import write_to_xyz
>>> path = os.path.join(tempfile.mkdtemp(), "water.xyz")
>>> write_to_xyz(path, molecule("H2O"))
>>> open(path).read().splitlines()[0]
'3'
Source code in cage_isomer_builder/utils/read_write.py
build_isomer_atoms ¶
build_isomer_atoms(cage, fg_anchors, isomer_descriptor, fg_anchor_indices, fg_anchor_h_positions=None)
One single-group isomer as ase.Atoms: the slots in
isomer_descriptor are R1 sites (X with "rgroup" = 1, see
:mod:cage_isomer_builder.utils.sites) and every other slot is turned
back into its original H.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cage
|
Atoms
|
The structure with every slot marked as a site
( |
required |
fg_anchors
|
Atoms
|
The site atoms in slot order, giving each active site's position. |
required |
isomer_descriptor
|
list of int
|
Slot indices that are active, e.g. |
required |
fg_anchor_indices
|
list of int
|
Index in |
required |
fg_anchor_h_positions
|
(ndarray, shape(n_slots, 3))
|
Original H position of each slot. Without it an inactive slot gets an H at 1.09 Angstrom along its C-site direction. |
None
|
Examples:
>>> from ase.build import molecule
>>> from cage_isomer_builder.utils.sites import mark_site
>>> benzene = molecule("C6H6")
>>> h_positions = benzene.positions[6:].copy()
>>> for h in range(6, 12):
... mark_site(benzene, h, 1)
>>> slots = list(range(6, 12))
>>> from cage_isomer_builder.utils.read_write import build_isomer_atoms
>>> isomer = build_isomer_atoms(benzene, benzene[slots], [0, 3], slots, h_positions)
>>> isomer.get_chemical_symbols()[6:] # slots 0 and 3 active, the rest H again
['X', 'H', 'H', 'X', 'H', 'H']
Source code in cage_isomer_builder/utils/read_write.py
generate_isomer_structure_file ¶
generate_isomer_structure_file(cage, fg_anchors, isomer_descriptor, fg_anchor_indices, output_path, fg_anchor_h_positions=None, name=None, info=None)
Write one single-group isomer (see :func:build_isomer_atoms) to
<output_path>/<slot>-<slot>-....xyz as extended XYZ, with its sites
listed in info["r_sites"] (see
:func:cage_isomer_builder.utils.sites.write_isomer).
name replaces the default file name (the site indices joined by
-), and info adds entries to the file's info.
Returns:
| Type | Description |
|---|---|
str
|
Path of the written file. |
Examples:
>>> from ase.build import molecule
>>> from cage_isomer_builder.utils.sites import mark_site
>>> benzene = molecule("C6H6")
>>> h_positions = benzene.positions[6:].copy()
>>> for h in range(6, 12):
... mark_site(benzene, h, 1)
>>> slots = list(range(6, 12))
>>> import tempfile
>>> from cage_isomer_builder.utils.read_write import generate_isomer_structure_file
>>> from cage_isomer_builder.utils.sites import read_isomer
>>> path = generate_isomer_structure_file(benzene, benzene[slots], [0, 3], slots,
... tempfile.mkdtemp(), h_positions)
>>> path.endswith("0-3.xyz"), read_isomer(path).info["r_sites"]
(True, {6: 'R1', 9: 'R1'})
>>> path = generate_isomer_structure_file(benzene, benzene[slots], [0, 3], slots,
... tempfile.mkdtemp(), h_positions,
... name="para", info={"note": "test"})
>>> path.endswith("para.xyz"), read_isomer(path).info["isomer"], read_isomer(path).info["note"]
(True, '0-3', 'test')
Source code in cage_isomer_builder/utils/read_write.py
write_run ¶
Write an AMS geometry-optimisation run script (Engine DFTB) with
explicit bond orders, for atoms and its bond-order matrix bonds. The
cell is written as Lattice for periodic structures.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str or Path
|
|
required |
atoms
|
Atoms
|
|
required |
bonds
|
(ndarray, shape(len(atoms), len(atoms)))
|
Bond-order matrix (only entries above zero are written). |
required |
Examples:
>>> import os, tempfile
>>> import numpy as np
>>> from ase.build import molecule
>>> from cage_isomer_builder.utils.read_write import write_run
>>> path = os.path.join(tempfile.mkdtemp(), "water.run")
>>> write_run(path, molecule("H2O"), np.array([[0, 1, 1], [1, 0, 0], [1, 0, 0]]))
>>> "BondOrders" in open(path).read()
True
Source code in cage_isomer_builder/utils/read_write.py
write_host_guest_complex ¶
Write a HostGuestComplex to disk, picking the writer from path's
file extension.
.gin-> GULP molecular-mechanics input, viagulp_setup.mmanalysis.write_gin(UFF4MOF atom types assigned byanalyze_mmfromcomplex_.bond_matrix). Bond orders are taken fromcomplex_.bond_matrix, never re-perceived from the merged host+guest geometry (see place_guest_in_host)..run-> AMS geometry-optimisation run script, via utils.read_write.write_run, same bond matrix.- anything else -> plain
ase.io.write(path, atoms). No bond matrix is used (or needed) here, since none of ASE's own formats (xyz, cif, pdb, ...) carry bond order.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
complex_
|
HostGuestComplex or Atoms
|
A HostGuestComplex is required for |
required |
path
|
str or Path
|
Output file path; its suffix selects the writer. |
required |
lattice
|
str
|
Forwarded to |
"conv"
|
mechanical
|
bool
|
Forwarded to |
False
|
Raises:
| Type | Description |
|---|---|
TypeError
|
If |
Examples:
>>> import os, tempfile
>>> from ase.build import molecule
>>> from cage_isomer_builder.utils.read_write import write_host_guest_complex
>>> from cage_isomer_builder.utils.functionalise import HostGuestComplex
>>> import numpy as np
>>> water = molecule("H2O")
>>> complex_ = HostGuestComplex(water, np.array([[0, 1, 1], [1, 0, 0], [1, 0, 0]]), [], [])
>>> path = os.path.join(tempfile.mkdtemp(), "complex.run")
>>> write_host_guest_complex(complex_, path) # .gin, .run or any ASE format
>>> os.path.exists(path)
True
Source code in cage_isomer_builder/utils/read_write.py
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 | |