torchref.io.data_router module

Data Router - Automatic file type detection and reader selection.

This module provides a smart router that automatically detects the file type and returns the appropriate reader (CIF or legacy format).

Supported file types: - Structure factors: .mtz, .cif (with reflection data) - Structure models: .pdb, .cif (with atom_site data) - Restraints: .cif (with restraint dictionaries)

Usage:

from torchref.io import DataRouter

# Automatic detection router = DataRouter(“data.mtz”) reader = router.get_reader() data_type = router.data_type # ‘reflections’, ‘structure’, or ‘restraints’

# Or use the factory method reader, data_type = DataRouter.route(“data.cif”)

exception torchref.io.data_router.DataRouterError[source]

Bases: Exception

Exception raised when file type cannot be determined or is unsupported.

class torchref.io.data_router.DataRouter(filepath, verbose=1)[source]

Bases: object

Automatic file type detection and reader selection.

This class examines a file and automatically selects the appropriate reader based on file extension and content.

Parameters:
  • filepath (str or Path) – Path to the file to read.

  • verbose (int, default 1) – Verbosity level (0=quiet, 1=normal, 2+=debug).

filepath

Path to the file to read.

Type:

Path

verbose

Verbosity level for logging.

Type:

int

data_type

Type of data detected (‘reflections’, ‘structure’, ‘restraints’, ‘ihm_ensemble’, or None).

Type:

str or None

file_format

File format detected (‘mtz’, ‘pdb’, ‘cif’, or None).

Type:

str or None

reader

The appropriate reader instance (or None if not yet created).

Type:

object or None

Examples

router = DataRouter("structure.cif")
reader = router.get_reader()
print(router.data_type)  # 'structure'
MTZ_EXTENSIONS = {'.mtz'}
PDB_EXTENSIONS = {'.ent', '.pdb'}
CIF_EXTENSIONS = {'.cif', '.mmcif'}
__init__(filepath, verbose=1)[source]

Initialize the DataRouter.

Parameters:
  • filepath (str or Path) – Path to the data file.

  • verbose (int, default 1) – Verbosity level (0=quiet, 1=normal, 2+=debug).

data_type: str | None
file_format: str | None
reader: Any | None
get_reader()[source]

Get the appropriate reader for this file.

Returns:

Reader instance (ReflectionCIFReader, ModelCIFReader, RestraintCIFReader, MTZ, or PDB depending on file type).

Return type:

object

Raises:

DataRouterError – If file type is not supported or cannot be determined.

get_data()[source]

Get the data from the file using the appropriate reader.

This is a convenience method that calls get_reader() and then invokes the reader to get the data.

Returns:

For reflections: (data_dict, cell, spacegroup) For structure: (dataframe, residues, spacegroup) For restraints: Restraint data (format depends on reader)

Return type:

tuple

classmethod route(filepath, verbose=1)[source]

Factory method to quickly route a file to the appropriate reader.

Parameters:
  • filepath (str or Path) – Path to the data file.

  • verbose (int, default 1) – Verbosity level.

Returns:

Tuple of (reader, data_type) where: - reader: The appropriate reader instance - data_type: String indicating the type (‘reflections’, ‘structure’, ‘restraints’)

Return type:

tuple

Examples

reader, data_type = DataRouter.route("7JI4-sf.cif")
if data_type == 'reflections':
    data_dict, cell, spacegroup = reader()
__repr__()[source]

String representation of the DataRouter.

__str__()[source]

User-friendly string representation.