io#

irmsd.utils.io.check_frames(obj, src: str)[source]#

Check how many frames are in a provided object.

Parameters:
  • obj (single object or sequence) – Typically a single structure (Molecule or ASE Atoms) or a sequence of them as returned by a reader.

  • src (str) – Source label, usually the file path. Used only for informational messages.

Returns:

The input object unchanged. If a non-empty sequence with more than one frame is provided, an informational message is printed.

Return type:

obj

irmsd.utils.io.dump_results_to_pickle(molecules: Sequence[Any], outfile: str, results: Mapping[str, Any] | None = None, compress: str | None = None) → str[source]#

Dump a sequence of Molecule objects (and optionally a results mapping) into a pickle file, optionally compressed.

Parameters:
  • molecules (Sequence[Any]) – Sequence of Molecule objects.

  • outfile (str) – Output filename. If extension is missing or wrong, ‘.pkl’ is used.

  • results (Mapping[str, Any] or None) – Optional results dictionary. If provided, its entries become top-level keys in the payload and ‘molecules’ is added. If None, the payload is simply molecules.

  • compress ({None, "gz", "bz2", "xz"}, optional) – Compression type.

Returns:

Final filename used.

Return type:

str

irmsd.utils.io.read_structures(paths: str | Sequence[str]) → List[Molecule][source]#

Read an arbitrary number of structures and return them as Molecule objects.

For each path, this routine behaves as follows:

  • If the file extension is .xyz or .extxyz, it uses the internal read_extxyz helper to obtain one or more Molecule objects.

  • For all other file types, it attempts to import ASE via require_ase(), uses ase.io.read to read one or more ASE Atoms objects, and converts them into Molecule objects using ase_to_molecule.

Multi-frame files:

If a file contains multiple frames, all frames are read and appended to the output list. A short informational message is printed indicating the number of frames that were found.

Parameters:

paths (Sequence[str]) – File paths to read.

Returns:

structures – One Molecule per frame found across all input paths.

Return type:

list[Molecule]

irmsd.utils.io.write_structures(filename: str | Path, structures: Molecule, mode: str = 'w') → None[source]#
irmsd.utils.io.write_structures(filename: str | Path, structures: Sequence[Molecule], mode: str = 'w') → None

High-level structure writer for the irmsd Molecule type.

This routine mirrors the behaviour of read_structures on the output side: it chooses the appropriate backend based on the file extension and accepts either a single Molecule or a sequence of Molecule objects.

Dispatch rules#

  • If the filename has no extension, ‘.xyz’ is appended and the internal extended-XYZ writer is used.

  • If the filename ends with ‘.xyz’, ‘.extxyz’ or ‘.trj’ (case-insensitive), the structures are written using the internal extended-XYZ writer (write_extxyz). The mode argument is passed through and controls whether the file is overwritten (‘w’, default) or appended to (‘a’).

  • For all other filename extensions, ASE is used as a backend. The structures are first converted to ASE Atoms objects using molecule_to_ase, and then written via ase.io.write. In this case the mode argument is currently ignored and ASE’s default behaviour for the chosen format is used.

param filename:

Output filename. Its extension determines the backend.

type filename:

str or pathlib.Path

param structures:

A single Molecule or a sequence of Molecules to be written. For extended XYZ, multiple Molecules are written as consecutive frames in one file.

type structures:

Molecule or Sequence[Molecule]

param mode:

File open mode for extended XYZ output. Ignored for non-XYZ formats handled via ASE.

type mode:

{"w", "a"}, optional

raises RuntimeError:

If ASE is required (non-XYZ formats) but not installed.

raises TypeError:

If structures is not a Molecule or a sequence of Molecules.